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 ///////////////////////////////////////////////////////////////////////////////
9 #include "../common/memory.h"
16 // =================================
18 // =================================
20 void Code::init_output(output_struct
*output
)
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
;
49 void Code::merge_output(output_struct
*dest
, output_struct
*src
)
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
);
108 void Code::free_output(output_struct
*output
)
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
);
138 void Code::init_cdef(const_def
*cdef
)
146 void Code::merge_cdef(output_struct
*dest
, const_def
*cdef
)
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
);
153 void Code::free_cdef(const_def
*cdef
)
161 void Code::init_expr(expression_struct
*expr
)
163 expr
->preamble
= NULL
;
165 expr
->postamble
= NULL
;
168 void Code::clean_expr(expression_struct
*expr
)
174 void Code::free_expr(expression_struct
*expr
)
176 Free(expr
->preamble
);
178 Free(expr
->postamble
);
181 char* Code::merge_free_expr(char* str
, expression_struct
*expr
,
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
);
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");
205 char *Code::translate_character(char *str
, char c
, bool in_string
)
207 int i
= (unsigned char)c
;
210 return mputstr(str
, "\\a");
212 return mputstr(str
, "\\b");
214 return mputstr(str
, "\\f");
216 return mputstr(str
, "\\n");
218 return mputstr(str
, "\\r");
220 return mputstr(str
, "\\t");
222 return mputstr(str
, "\\v");
224 return mputstr(str
, "\\\\");
226 if (in_string
) return mputc(str
, '\'');
227 else return mputstr(str
, "\\'");
229 if (in_string
) return mputstr(str
, "\\\"");
230 else return mputc(str
, '"');
232 // to avoid recognition of trigraphs
233 if (in_string
) return mputstr(str
, "\\?");
234 else return mputc(str
, '?');
236 if (isascii(i
) && isprint(i
)) return mputc(str
, c
);
237 return mputprintf(str
, in_string
? "\\%03o" : "\\%o", i
);
241 char *Code::translate_string(char *str
, const char *src
)
243 for (size_t i
= 0; src
[i
] != '\0'; i
++)
244 str
= translate_character(str
, src
[i
], true);
248 } // namespace Common