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