Merge pull request #28 from balaskoa/master
[deliverable/titan.core.git] / compiler2 / Code.cc
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
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 * Cserveni, Akos
12 * Forstner, Matyas
13 * Kovacs, Ferenc
14 * Raduly, Csaba
15 * Szabo, Janos Zoltan – initial implementation
16 *
17 ******************************************************************************/
18 #include "Code.hh"
19 #include "../common/memory.h"
20 #include "error.h"
21
22 #include <ctype.h>
23
24 namespace Common {
25
26 // =================================
27 // ===== Code
28 // =================================
29
30 void Code::init_output(output_struct *output)
31 {
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;
57 }
58
59 void Code::merge_output(output_struct *dest, output_struct *src)
60 {
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);
116 }
117
118 void Code::free_output(output_struct *output)
119 {
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);
145 init_output(output);
146 }
147
148 void Code::init_cdef(const_def *cdef)
149 {
150 cdef->decl = NULL;
151 cdef->def = NULL;
152 //cdef->cdef = NULL;
153 cdef->init = NULL;
154 }
155
156 void Code::merge_cdef(output_struct *dest, const_def *cdef)
157 {
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);
161 }
162
163 void Code::free_cdef(const_def *cdef)
164 {
165 Free(cdef->decl);
166 Free(cdef->def);
167 //Free(cdef->cdef);
168 Free(cdef->init);
169 }
170
171 void Code::init_expr(expression_struct *expr)
172 {
173 expr->preamble = NULL;
174 expr->expr = NULL;
175 expr->postamble = NULL;
176 }
177
178 void Code::clean_expr(expression_struct *expr)
179 {
180 Free(expr->expr);
181 expr->expr = NULL;
182 }
183
184 void Code::free_expr(expression_struct *expr)
185 {
186 Free(expr->preamble);
187 Free(expr->expr);
188 Free(expr->postamble);
189 }
190
191 char* Code::merge_free_expr(char* str, expression_struct *expr,
192 bool is_block)
193 {
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);
199 }
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");
210 }
211 free_expr(expr);
212 return str;
213 }
214
215 char *Code::translate_character(char *str, char c, bool in_string)
216 {
217 int i = (unsigned char)c;
218 switch (i) {
219 case '\a':
220 return mputstr(str, "\\a");
221 case '\b':
222 return mputstr(str, "\\b");
223 case '\f':
224 return mputstr(str, "\\f");
225 case '\n':
226 return mputstr(str, "\\n");
227 case '\r':
228 return mputstr(str, "\\r");
229 case '\t':
230 return mputstr(str, "\\t");
231 case '\v':
232 return mputstr(str, "\\v");
233 case '\\':
234 return mputstr(str, "\\\\");
235 case '\'':
236 if (in_string) return mputc(str, '\'');
237 else return mputstr(str, "\\'");
238 case '"':
239 if (in_string) return mputstr(str, "\\\"");
240 else return mputc(str, '"');
241 case '?':
242 // to avoid recognition of trigraphs
243 if (in_string) return mputstr(str, "\\?");
244 else return mputc(str, '?');
245 default:
246 if (isascii(i) && isprint(i)) return mputc(str, c);
247 return mputprintf(str, in_string ? "\\%03o" : "\\%o", i);
248 }
249 }
250
251 char *Code::translate_string(char *str, const char *src)
252 {
253 for (size_t i = 0; src[i] != '\0'; i++)
254 str = translate_character(str, src[i], true);
255 return str;
256 }
257
258 } // namespace Common
This page took 0.045238 seconds and 5 git commands to generate.