Commit | Line | Data |
---|---|---|
970ed795 EL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2014 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 | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #include "Code.hh" | |
9 | #include "../common/memory.h" | |
10 | #include "error.h" | |
11 | ||
12 | #include <ctype.h> | |
13 | ||
14 | namespace Common { | |
15 | ||
16 | // ================================= | |
17 | // ===== Code | |
18 | // ================================= | |
19 | ||
20 | void Code::init_output(output_struct *output) | |
21 | { | |
22 | output->header.includes = NULL; | |
23 | output->header.class_decls = NULL; | |
24 | output->header.typedefs = NULL; | |
25 | output->header.class_defs = NULL; | |
26 | output->header.function_prototypes = NULL; | |
27 | output->header.global_vars = NULL; | |
28 | output->header.testport_includes = NULL; | |
29 | output->source.includes = NULL; | |
30 | output->source.static_function_prototypes = NULL; | |
31 | output->source.static_conversion_function_prototypes = NULL; | |
32 | output->source.string_literals = NULL; | |
33 | output->source.class_defs = NULL; | |
34 | output->source.global_vars = NULL; | |
35 | output->source.methods = NULL; | |
36 | output->source.function_bodies = NULL; | |
37 | output->source.static_function_bodies = NULL; | |
38 | output->source.static_conversion_function_bodies = NULL; | |
39 | output->functions.pre_init = NULL; | |
40 | output->functions.post_init = NULL; | |
41 | output->functions.set_param = NULL; | |
42 | output->functions.log_param = NULL; | |
43 | output->functions.init_comp = NULL; | |
44 | output->functions.start = NULL; | |
45 | output->functions.control = NULL; | |
46 | } | |
47 | ||
48 | void Code::merge_output(output_struct *dest, output_struct *src) | |
49 | { | |
50 | dest->header.includes = | |
51 | mputstr(dest->header.includes, src->header.includes); | |
52 | dest->header.class_decls = | |
53 | mputstr(dest->header.class_decls, src->header.class_decls); | |
54 | dest->header.typedefs = | |
55 | mputstr(dest->header.typedefs, src->header.typedefs); | |
56 | dest->header.class_defs = | |
57 | mputstr(dest->header.class_defs, src->header.class_defs); | |
58 | dest->header.function_prototypes = | |
59 | mputstr(dest->header.function_prototypes, | |
60 | src->header.function_prototypes); | |
61 | dest->header.global_vars = | |
62 | mputstr(dest->header.global_vars, src->header.global_vars); | |
63 | dest->header.testport_includes = | |
64 | mputstr(dest->header.testport_includes, src->header.testport_includes); | |
65 | dest->source.includes = | |
66 | mputstr(dest->source.includes, src->source.includes); | |
67 | dest->source.static_function_prototypes = | |
68 | mputstr(dest->source.static_function_prototypes, | |
69 | src->source.static_function_prototypes); | |
70 | dest->source.static_conversion_function_prototypes = | |
71 | mputstr(dest->source.static_conversion_function_prototypes, | |
72 | src->source.static_conversion_function_prototypes); | |
73 | dest->source.string_literals = | |
74 | mputstr(dest->source.string_literals, src->source.string_literals); | |
75 | dest->source.class_defs = | |
76 | mputstr(dest->source.class_defs, src->source.class_defs); | |
77 | dest->source.global_vars = | |
78 | mputstr(dest->source.global_vars, src->source.global_vars); | |
79 | dest->source.methods = | |
80 | mputstr(dest->source.methods, src->source.methods); | |
81 | dest->source.function_bodies = | |
82 | mputstr(dest->source.function_bodies, src->source.function_bodies); | |
83 | dest->source.static_function_bodies = | |
84 | mputstr(dest->source.static_function_bodies, | |
85 | src->source.static_function_bodies); | |
86 | dest->source.static_conversion_function_bodies = | |
87 | mputstr(dest->source.static_conversion_function_bodies, | |
88 | src->source.static_conversion_function_bodies); | |
89 | dest->functions.pre_init = | |
90 | mputstr(dest->functions.pre_init, src->functions.pre_init); | |
91 | dest->functions.post_init = | |
92 | mputstr(dest->functions.post_init, src->functions.post_init); | |
93 | dest->functions.set_param = | |
94 | mputstr(dest->functions.set_param, src->functions.set_param); | |
95 | dest->functions.log_param = | |
96 | mputstr(dest->functions.log_param, src->functions.log_param); | |
97 | dest->functions.init_comp = | |
98 | mputstr(dest->functions.init_comp, src->functions.init_comp); | |
99 | dest->functions.start = | |
100 | mputstr(dest->functions.start, src->functions.start); | |
101 | dest->functions.control = | |
102 | mputstr(dest->functions.control, src->functions.control); | |
103 | } | |
104 | ||
105 | void Code::free_output(output_struct *output) | |
106 | { | |
107 | Free(output->header.includes); | |
108 | Free(output->header.class_decls); | |
109 | Free(output->header.typedefs); | |
110 | Free(output->header.class_defs); | |
111 | Free(output->header.function_prototypes); | |
112 | Free(output->header.global_vars); | |
113 | Free(output->header.testport_includes); | |
114 | Free(output->source.includes); | |
115 | Free(output->source.static_function_prototypes); | |
116 | Free(output->source.static_conversion_function_prototypes); | |
117 | Free(output->source.string_literals); | |
118 | Free(output->source.class_defs); | |
119 | Free(output->source.global_vars); | |
120 | Free(output->source.methods); | |
121 | Free(output->source.function_bodies); | |
122 | Free(output->source.static_function_bodies); | |
123 | Free(output->source.static_conversion_function_bodies); | |
124 | Free(output->functions.pre_init); | |
125 | Free(output->functions.post_init); | |
126 | Free(output->functions.set_param); | |
127 | Free(output->functions.log_param); | |
128 | Free(output->functions.init_comp); | |
129 | Free(output->functions.start); | |
130 | Free(output->functions.control); | |
131 | init_output(output); | |
132 | } | |
133 | ||
134 | void Code::init_cdef(const_def *cdef) | |
135 | { | |
136 | cdef->decl = NULL; | |
137 | cdef->def = NULL; | |
138 | //cdef->cdef = NULL; | |
139 | cdef->init = NULL; | |
140 | } | |
141 | ||
142 | void Code::merge_cdef(output_struct *dest, const_def *cdef) | |
143 | { | |
144 | dest->header.global_vars = mputstr(dest->header.global_vars, cdef->decl); | |
145 | dest->source.global_vars = mputstr(dest->source.global_vars, cdef->def); | |
146 | dest->functions.pre_init = mputstr(dest->functions.pre_init, cdef->init); | |
147 | } | |
148 | ||
149 | void Code::free_cdef(const_def *cdef) | |
150 | { | |
151 | Free(cdef->decl); | |
152 | Free(cdef->def); | |
153 | //Free(cdef->cdef); | |
154 | Free(cdef->init); | |
155 | } | |
156 | ||
157 | void Code::init_expr(expression_struct *expr) | |
158 | { | |
159 | expr->preamble = NULL; | |
160 | expr->expr = NULL; | |
161 | expr->postamble = NULL; | |
162 | } | |
163 | ||
164 | void Code::clean_expr(expression_struct *expr) | |
165 | { | |
166 | Free(expr->expr); | |
167 | expr->expr = NULL; | |
168 | } | |
169 | ||
170 | void Code::free_expr(expression_struct *expr) | |
171 | { | |
172 | Free(expr->preamble); | |
173 | Free(expr->expr); | |
174 | Free(expr->postamble); | |
175 | } | |
176 | ||
177 | char* Code::merge_free_expr(char* str, expression_struct *expr, | |
178 | bool is_block) | |
179 | { | |
180 | if (expr->preamble || expr->postamble) { | |
181 | // open a statement block if the expression has a preamble or postamble | |
182 | str = mputstr(str, "{\n"); | |
183 | // append the preamble if present | |
184 | if (expr->preamble) str = mputstr(str, expr->preamble); | |
185 | } | |
186 | // append the expression itself | |
187 | str = mputstr(str, expr->expr); | |
188 | // terminate it with a bracket or semi-colon | |
189 | if (is_block) str = mputstr(str, "}\n"); | |
190 | else str = mputstr(str, ";\n"); | |
191 | if (expr->preamble || expr->postamble) { | |
192 | // append the postamble if present | |
193 | if (expr->postamble) str = mputstr(str, expr->postamble); | |
194 | // close the statement block | |
195 | str = mputstr(str, "}\n"); | |
196 | } | |
197 | free_expr(expr); | |
198 | return str; | |
199 | } | |
200 | ||
201 | char *Code::translate_character(char *str, char c, bool in_string) | |
202 | { | |
203 | int i = (unsigned char)c; | |
204 | switch (i) { | |
205 | case '\a': | |
206 | return mputstr(str, "\\a"); | |
207 | case '\b': | |
208 | return mputstr(str, "\\b"); | |
209 | case '\f': | |
210 | return mputstr(str, "\\f"); | |
211 | case '\n': | |
212 | return mputstr(str, "\\n"); | |
213 | case '\r': | |
214 | return mputstr(str, "\\r"); | |
215 | case '\t': | |
216 | return mputstr(str, "\\t"); | |
217 | case '\v': | |
218 | return mputstr(str, "\\v"); | |
219 | case '\\': | |
220 | return mputstr(str, "\\\\"); | |
221 | case '\'': | |
222 | if (in_string) return mputc(str, '\''); | |
223 | else return mputstr(str, "\\'"); | |
224 | case '"': | |
225 | if (in_string) return mputstr(str, "\\\""); | |
226 | else return mputc(str, '"'); | |
227 | case '?': | |
228 | // to avoid recognition of trigraphs | |
229 | if (in_string) return mputstr(str, "\\?"); | |
230 | else return mputc(str, '?'); | |
231 | default: | |
232 | if (isascii(i) && isprint(i)) return mputc(str, c); | |
233 | return mputprintf(str, in_string ? "\\%03o" : "\\%o", i); | |
234 | } | |
235 | } | |
236 | ||
237 | char *Code::translate_string(char *str, const char *src) | |
238 | { | |
239 | for (size_t i = 0; src[i] != '\0'; i++) | |
240 | str = translate_character(str, src[i], true); | |
241 | return str; | |
242 | } | |
243 | ||
244 | } // namespace Common |