1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
15 * Szabo, Janos Zoltan – initial implementation
17 ******************************************************************************/
19 #include "../common/memory.h"
26 // =================================
28 // =================================
30 void Code::init_output(output_struct
*output
)
32 output
->header
.includes
= NULL
;
33 output
->header
.class_decls
= NULL
;
34 output
->header
.typedefs
= NULL
;
35 output
->header
.class_defs
= NULL
;
36 output
->header
.function_prototypes
= NULL
;
37 output
->header
.global_vars
= NULL
;
38 output
->header
.testport_includes
= NULL
;
39 output
->source
.includes
= NULL
;
40 output
->source
.static_function_prototypes
= NULL
;
41 output
->source
.static_conversion_function_prototypes
= NULL
;
42 output
->source
.string_literals
= NULL
;
43 output
->source
.class_defs
= NULL
;
44 output
->source
.global_vars
= NULL
;
45 output
->source
.methods
= NULL
;
46 output
->source
.function_bodies
= NULL
;
47 output
->source
.static_function_bodies
= NULL
;
48 output
->source
.static_conversion_function_bodies
= NULL
;
49 output
->functions
.pre_init
= NULL
;
50 output
->functions
.post_init
= NULL
;
51 output
->functions
.set_param
= NULL
;
52 output
->functions
.get_param
= NULL
;
53 output
->functions
.log_param
= NULL
;
54 output
->functions
.init_comp
= NULL
;
55 output
->functions
.start
= NULL
;
56 output
->functions
.control
= NULL
;
59 void Code::merge_output(output_struct
*dest
, output_struct
*src
)
61 dest
->header
.includes
=
62 mputstr(dest
->header
.includes
, src
->header
.includes
);
63 dest
->header
.class_decls
=
64 mputstr(dest
->header
.class_decls
, src
->header
.class_decls
);
65 dest
->header
.typedefs
=
66 mputstr(dest
->header
.typedefs
, src
->header
.typedefs
);
67 dest
->header
.class_defs
=
68 mputstr(dest
->header
.class_defs
, src
->header
.class_defs
);
69 dest
->header
.function_prototypes
=
70 mputstr(dest
->header
.function_prototypes
,
71 src
->header
.function_prototypes
);
72 dest
->header
.global_vars
=
73 mputstr(dest
->header
.global_vars
, src
->header
.global_vars
);
74 dest
->header
.testport_includes
=
75 mputstr(dest
->header
.testport_includes
, src
->header
.testport_includes
);
76 dest
->source
.includes
=
77 mputstr(dest
->source
.includes
, src
->source
.includes
);
78 dest
->source
.static_function_prototypes
=
79 mputstr(dest
->source
.static_function_prototypes
,
80 src
->source
.static_function_prototypes
);
81 dest
->source
.static_conversion_function_prototypes
=
82 mputstr(dest
->source
.static_conversion_function_prototypes
,
83 src
->source
.static_conversion_function_prototypes
);
84 dest
->source
.string_literals
=
85 mputstr(dest
->source
.string_literals
, src
->source
.string_literals
);
86 dest
->source
.class_defs
=
87 mputstr(dest
->source
.class_defs
, src
->source
.class_defs
);
88 dest
->source
.global_vars
=
89 mputstr(dest
->source
.global_vars
, src
->source
.global_vars
);
90 dest
->source
.methods
=
91 mputstr(dest
->source
.methods
, src
->source
.methods
);
92 dest
->source
.function_bodies
=
93 mputstr(dest
->source
.function_bodies
, src
->source
.function_bodies
);
94 dest
->source
.static_function_bodies
=
95 mputstr(dest
->source
.static_function_bodies
,
96 src
->source
.static_function_bodies
);
97 dest
->source
.static_conversion_function_bodies
=
98 mputstr(dest
->source
.static_conversion_function_bodies
,
99 src
->source
.static_conversion_function_bodies
);
100 dest
->functions
.pre_init
=
101 mputstr(dest
->functions
.pre_init
, src
->functions
.pre_init
);
102 dest
->functions
.post_init
=
103 mputstr(dest
->functions
.post_init
, src
->functions
.post_init
);
104 dest
->functions
.set_param
=
105 mputstr(dest
->functions
.set_param
, src
->functions
.set_param
);
106 dest
->functions
.get_param
=
107 mputstr(dest
->functions
.get_param
, src
->functions
.get_param
);
108 dest
->functions
.log_param
=
109 mputstr(dest
->functions
.log_param
, src
->functions
.log_param
);
110 dest
->functions
.init_comp
=
111 mputstr(dest
->functions
.init_comp
, src
->functions
.init_comp
);
112 dest
->functions
.start
=
113 mputstr(dest
->functions
.start
, src
->functions
.start
);
114 dest
->functions
.control
=
115 mputstr(dest
->functions
.control
, src
->functions
.control
);
118 void Code::free_output(output_struct
*output
)
120 Free(output
->header
.includes
);
121 Free(output
->header
.class_decls
);
122 Free(output
->header
.typedefs
);
123 Free(output
->header
.class_defs
);
124 Free(output
->header
.function_prototypes
);
125 Free(output
->header
.global_vars
);
126 Free(output
->header
.testport_includes
);
127 Free(output
->source
.includes
);
128 Free(output
->source
.static_function_prototypes
);
129 Free(output
->source
.static_conversion_function_prototypes
);
130 Free(output
->source
.string_literals
);
131 Free(output
->source
.class_defs
);
132 Free(output
->source
.global_vars
);
133 Free(output
->source
.methods
);
134 Free(output
->source
.function_bodies
);
135 Free(output
->source
.static_function_bodies
);
136 Free(output
->source
.static_conversion_function_bodies
);
137 Free(output
->functions
.pre_init
);
138 Free(output
->functions
.post_init
);
139 Free(output
->functions
.set_param
);
140 Free(output
->functions
.get_param
);
141 Free(output
->functions
.log_param
);
142 Free(output
->functions
.init_comp
);
143 Free(output
->functions
.start
);
144 Free(output
->functions
.control
);
148 void Code::init_cdef(const_def
*cdef
)
156 void Code::merge_cdef(output_struct
*dest
, const_def
*cdef
)
158 dest
->header
.global_vars
= mputstr(dest
->header
.global_vars
, cdef
->decl
);
159 dest
->source
.global_vars
= mputstr(dest
->source
.global_vars
, cdef
->def
);
160 dest
->functions
.pre_init
= mputstr(dest
->functions
.pre_init
, cdef
->init
);
163 void Code::free_cdef(const_def
*cdef
)
171 void Code::init_expr(expression_struct
*expr
)
173 expr
->preamble
= NULL
;
175 expr
->postamble
= NULL
;
178 void Code::clean_expr(expression_struct
*expr
)
184 void Code::free_expr(expression_struct
*expr
)
186 Free(expr
->preamble
);
188 Free(expr
->postamble
);
191 char* Code::merge_free_expr(char* str
, expression_struct
*expr
,
194 if (expr
->preamble
|| expr
->postamble
) {
195 // open a statement block if the expression has a preamble or postamble
196 str
= mputstr(str
, "{\n");
197 // append the preamble if present
198 if (expr
->preamble
) str
= mputstr(str
, expr
->preamble
);
200 // append the expression itself
201 str
= mputstr(str
, expr
->expr
);
202 // terminate it with a bracket or semi-colon
203 if (is_block
) str
= mputstr(str
, "}\n");
204 else str
= mputstr(str
, ";\n");
205 if (expr
->preamble
|| expr
->postamble
) {
206 // append the postamble if present
207 if (expr
->postamble
) str
= mputstr(str
, expr
->postamble
);
208 // close the statement block
209 str
= mputstr(str
, "}\n");
215 char *Code::translate_character(char *str
, char c
, bool in_string
)
217 int i
= (unsigned char)c
;
220 return mputstr(str
, "\\a");
222 return mputstr(str
, "\\b");
224 return mputstr(str
, "\\f");
226 return mputstr(str
, "\\n");
228 return mputstr(str
, "\\r");
230 return mputstr(str
, "\\t");
232 return mputstr(str
, "\\v");
234 return mputstr(str
, "\\\\");
236 if (in_string
) return mputc(str
, '\'');
237 else return mputstr(str
, "\\'");
239 if (in_string
) return mputstr(str
, "\\\"");
240 else return mputc(str
, '"');
242 // to avoid recognition of trigraphs
243 if (in_string
) return mputstr(str
, "\\?");
244 else return mputc(str
, '?');
246 if (isascii(i
) && isprint(i
)) return mputc(str
, c
);
247 return mputprintf(str
, in_string
? "\\%03o" : "\\%o", i
);
251 char *Code::translate_string(char *str
, const char *src
)
253 for (size_t i
= 0; src
[i
] != '\0'; i
++)
254 str
= translate_character(str
, src
[i
], true);
258 } // namespace Common