author | Mario de Sousa <msousa@fe.up.pt> |
Mon, 04 Apr 2011 11:19:48 +0100 | |
changeset 269 | 98fc461e1888 |
parent 265 | 4d222f46f8cc |
child 279 | c0453b7f99df |
permissions | -rwxr-xr-x |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
5 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
6 |
* This program is free software: you can redistribute it and/or modify |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
8 |
* the Free Software Foundation, either version 3 of the License, or |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
9 |
* (at your option) any later version. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
10 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
14 |
* GNU General Public License for more details. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
15 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
18 |
* |
181 | 19 |
* |
20 |
* This code is made available on the understanding that it will not be |
|
21 |
* used in safety-critical situations without a full and competent review. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
233
diff
changeset
|
25 |
* An IEC 61131-3 compiler. |
181 | 26 |
* |
27 |
* Based on the |
|
28 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
29 |
* |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
/* |
|
34 |
* Function parameter iterator. |
|
35 |
* Iterate through the in/out parameters of a function declaration. |
|
36 |
* Function blocks are also suported. |
|
37 |
* |
|
38 |
* This is part of the 4th stage that generates |
|
39 |
* a c++ source program equivalent to the IL and ST |
|
40 |
* code. |
|
41 |
*/ |
|
42 |
||
43 |
/* Given a function_declaration_c, iterate through each |
|
44 |
* function in/out/inout parameter, returning the name |
|
45 |
* of each parameter...function_param_iterator_c |
|
46 |
*/ |
|
47 |
||
48 |
||
49 |
#include "../absyntax/visitor.hh" |
|
50 |
||
51 |
||
52 |
class function_param_iterator_c : public null_visitor_c { |
|
53 |
public: |
|
54 |
/* A type to specify the type of parameter. |
|
55 |
* VAR_INPUT => direction_in |
|
56 |
* VAR_OUTPUT => direction_out |
|
57 |
* VAR_IN_OUT => direction_inout |
|
58 |
* VAR_EXTERNAL => direction_extref |
|
59 |
* |
|
60 |
* Note that VAR_EXTERNAL declares variables that are in reality references |
|
61 |
* to global variables. This is used only inside programs! |
|
62 |
* These references to external variables must be correctly initialised to refer |
|
63 |
* to the correct global variable. Note that the user may define which variable is to be |
|
64 |
* referenced in a CONFIGURATION, and that different instantiations of a program |
|
65 |
* may have the same external variable reference diffenrent global variables! |
|
66 |
* The references must therefore be correctly initialised when the program instance |
|
67 |
* is created. This may be done by the PROGRAM class constructor since the ST and IL |
|
68 |
* languages do not allow the VAR_EXTERNAL reference to change at runtime |
|
69 |
* for a specific instance. |
|
70 |
* |
|
71 |
* We therefore need to call a PROGRAM class constructor with the variables |
|
72 |
* that should be refernced by the VAR_EXTERNAL variables. The direction_extref will |
|
73 |
* be used to identify these parameters! |
|
74 |
*/ |
|
75 |
typedef enum {direction_in, direction_out, direction_inout, direction_extref} param_direction_t ; |
|
76 |
||
77 |
||
78 |
private: |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
79 |
/* a pointer to the function_block_declaration_c |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
80 |
* or function_declaration_c currently being analysed. |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
81 |
*/ |
181 | 82 |
symbol_c *f_decl; |
83 |
int next_param, param_count; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
84 |
/* used when called to search() for a parameter by name */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
85 |
identifier_c *search_param_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
86 |
/* used when called to iterate() for a parameter */ |
181 | 87 |
identifier_c *current_param_name; |
88 |
symbol_c *current_param_type; |
|
89 |
symbol_c *current_param_default_value; |
|
90 |
param_direction_t current_param_direction; |
|
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
202
diff
changeset
|
91 |
bool en_eno_param_implicit; |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
92 |
/* Which operation of the class was called... |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
93 |
* Search a parameter, or iterate to the next parameter. |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
94 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
95 |
typedef enum {iterate_op, search_op} operation_t; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
96 |
operation_t current_operation; |
181 | 97 |
|
98 |
private: |
|
99 |
void* handle_param_list(list_c *list); |
|
100 |
void* handle_single_param(symbol_c *var_name); |
|
101 |
||
102 |
void* iterate_list(list_c *list); |
|
103 |
||
104 |
public: |
|
105 |
/* start off at the first parameter once again... */ |
|
106 |
void reset(void); |
|
107 |
||
108 |
/* initialise the iterator object. |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
109 |
* We must be given a reference to one of the following |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
110 |
* - function_declaration_c |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
111 |
* - function_block_declaration_c |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
112 |
* - program_declaration_c |
181 | 113 |
* that will be analysed... |
114 |
*/ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
115 |
function_param_iterator_c(symbol_c *pou_decl); |
181 | 116 |
|
117 |
/* Skip to the next parameter. After object creation, |
|
118 |
* the object references on parameter _before_ the first, so |
|
119 |
* this function must be called once to get the object to |
|
120 |
* reference the first parameter... |
|
121 |
* |
|
122 |
* Returns the parameter's name! |
|
123 |
*/ |
|
124 |
identifier_c *next(void); |
|
125 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
126 |
/* Search for the parameter named <param_name>... */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
127 |
/* The seach() function does not in any way affect the internal state related |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
128 |
* to the iterate() function. |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
129 |
* It will, however, affect the internal state necessary to correctly |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
130 |
* return the param_type() and default_value() of the found parameter. |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
131 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
132 |
identifier_c *search(symbol_c *param_name); |
181 | 133 |
|
134 |
/* Returns the currently referenced parameter's default value, |
|
135 |
* or NULL if none is specified in the function declrataion itself. |
|
136 |
*/ |
|
137 |
symbol_c *default_value(void); |
|
138 |
||
139 |
/* Returns the currently referenced parameter's type name. */ |
|
140 |
symbol_c *param_type(void); |
|
141 |
||
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
202
diff
changeset
|
142 |
/* Returns if currently referenced parameter is an implicit defined EN/ENO parameter. */ |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
202
diff
changeset
|
143 |
bool is_en_eno_param_implicit(void); |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
202
diff
changeset
|
144 |
|
181 | 145 |
/* Returns the currently referenced parameter's data passing direction. |
146 |
* i.e. VAR_INPUT, VAR_OUTPUT or VAR_INOUT |
|
147 |
*/ |
|
148 |
param_direction_t param_direction(void); |
|
149 |
||
150 |
private: |
|
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
202
diff
changeset
|
151 |
void *visit(implicit_definition_c *symbol); |
181 | 152 |
/******************************************/ |
153 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
154 |
/******************************************/ |
|
155 |
void *visit(input_declarations_c *symbol); |
|
156 |
||
157 |
void *visit(input_declaration_list_c *symbol); |
|
158 |
||
159 |
void *visit(edge_declaration_c *symbol); |
|
160 |
void *visit(en_param_declaration_c *symbol); |
|
161 |
/* var1_list ':' array_spec_init */ |
|
162 |
//SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) |
|
163 |
void *visit(array_var_init_decl_c *symbol); |
|
164 |
||
165 |
/* var1_list ':' initialized_structure */ |
|
166 |
//SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) |
|
167 |
void *visit(structured_var_init_decl_c *symbol); |
|
168 |
||
169 |
#if 0 |
|
170 |
/* name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
171 |
/* structure_initialization -> may be NULL ! */ |
|
172 |
SYM_REF4(fb_name_decl_c, fb_name_list, function_block_type_name, structure_initialization, unused) |
|
173 |
||
174 |
/* name_list ',' fb_name */ |
|
175 |
SYM_LIST(fb_name_list_c) |
|
176 |
#endif |
|
177 |
||
178 |
void *visit(output_declarations_c *symbol); |
|
179 |
void *visit(eno_param_declaration_c *symbol); |
|
180 |
void *visit(input_output_declarations_c *symbol); |
|
181 |
void *visit(var_declaration_list_c *symbol); |
|
182 |
||
183 |
||
184 |
/* var1_list ':' array_specification */ |
|
185 |
//SYM_REF2(array_var_declaration_c, var1_list, array_specification) |
|
186 |
void *visit(array_var_declaration_c *symbol); |
|
187 |
||
188 |
/* var1_list ':' structure_type_name */ |
|
189 |
//SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) |
|
190 |
void *visit(structured_var_declaration_c *symbol); |
|
191 |
||
192 |
/* VAR [CONSTANT] var_init_decl_list END_VAR */ |
|
193 |
void *visit(var_declarations_c *symbol); |
|
194 |
||
195 |
#if 0 |
|
196 |
/* VAR RETAIN var_init_decl_list END_VAR */ |
|
197 |
SYM_REF2(retentive_var_declarations_c, var_init_decl_list, unused) |
|
198 |
||
199 |
/* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ |
|
200 |
/* option -> may be NULL ! */ |
|
201 |
SYM_REF2(located_var_declarations_c, option, located_var_decl_list) |
|
202 |
||
203 |
/* helper symbol for located_var_declarations */ |
|
204 |
/* located_var_decl_list located_var_decl ';' */ |
|
205 |
SYM_LIST(located_var_decl_list_c) |
|
206 |
||
207 |
/* [variable_name] location ':' located_var_spec_init */ |
|
208 |
/* variable_name -> may be NULL ! */ |
|
209 |
SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
210 |
#endif |
|
211 |
||
212 |
/*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ |
|
213 |
/* option -> may be NULL ! */ |
|
214 |
// SYM_REF2(external_var_declarations_c, option, external_declaration_list) |
|
215 |
void *visit(external_var_declarations_c *symbol); |
|
216 |
||
217 |
/* helper symbol for external_var_declarations */ |
|
218 |
/*| external_declaration_list external_declaration';' */ |
|
219 |
// SYM_LIST(external_declaration_list_c) |
|
220 |
void *visit(external_declaration_list_c *symbol); |
|
221 |
||
222 |
/* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ |
|
223 |
//SYM_REF2(external_declaration_c, global_var_name, specification) |
|
224 |
void *visit(external_declaration_c *symbol); |
|
225 |
||
226 |
||
227 |
#if 0 |
|
228 |
/*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ |
|
229 |
/* option -> may be NULL ! */ |
|
230 |
SYM_REF2(global_var_declarations_c, option, global_var_decl_list) |
|
231 |
||
232 |
/* helper symbol for global_var_declarations */ |
|
233 |
/*| global_var_decl_list global_var_decl ';' */ |
|
234 |
SYM_LIST(global_var_decl_list_c) |
|
235 |
||
236 |
/*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
237 |
/* type_specification ->may be NULL ! */ |
|
238 |
SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
239 |
||
240 |
/*| global_var_name location */ |
|
241 |
SYM_REF2(global_var_spec_c, global_var_name, location) |
|
242 |
||
243 |
/* AT direct_variable */ |
|
244 |
SYM_REF2(location_c, direct_variable, unused) |
|
245 |
||
246 |
/*| global_var_list ',' global_var_name */ |
|
247 |
SYM_LIST(global_var_list_c) |
|
248 |
||
249 |
/* var1_list ':' single_byte_string_spec */ |
|
250 |
SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) |
|
251 |
||
252 |
/* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ |
|
253 |
/* integer ->may be NULL ! */ |
|
254 |
/* single_byte_character_string ->may be NULL ! */ |
|
255 |
SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) |
|
256 |
||
257 |
/* var1_list ':' double_byte_string_spec */ |
|
258 |
SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) |
|
259 |
||
260 |
/* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ |
|
261 |
/* integer ->may be NULL ! */ |
|
262 |
/* double_byte_character_string ->may be NULL ! */ |
|
263 |
SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) |
|
264 |
||
265 |
/*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */ |
|
266 |
/* option ->may be NULL ! */ |
|
267 |
SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list) |
|
268 |
||
269 |
/* helper symbol for incompl_located_var_declarations */ |
|
270 |
/*| incompl_located_var_decl_list incompl_located_var_decl ';' */ |
|
271 |
SYM_LIST(incompl_located_var_decl_list_c) |
|
272 |
||
273 |
/* variable_name incompl_location ':' var_spec */ |
|
274 |
SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) |
|
275 |
||
276 |
/* AT incompl_location_token */ |
|
277 |
SYM_TOKEN(incompl_location_c) |
|
278 |
#endif |
|
279 |
||
280 |
||
281 |
void *visit(var1_init_decl_c *symbol); |
|
282 |
void *visit(var1_list_c *symbol); |
|
283 |
void *visit(var_init_decl_list_c *symbol); |
|
284 |
||
285 |
||
286 |
/***********************/ |
|
287 |
/* B 1.5.1 - Functions */ |
|
288 |
/***********************/ |
|
289 |
void *visit(function_declaration_c *symbol); |
|
290 |
/* intermediate helper symbol for function_declaration */ |
|
291 |
void *visit(var_declarations_list_c *symbol); |
|
292 |
void *visit(function_var_decls_c *symbol); |
|
293 |
||
294 |
||
295 |
/*****************************/ |
|
296 |
/* B 1.5.2 - Function Blocks */ |
|
297 |
/*****************************/ |
|
298 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
299 |
void *visit(function_block_declaration_c *symbol); |
|
300 |
||
301 |
/* intermediate helper symbol for function_declaration */ |
|
302 |
/* { io_var_declarations | other_var_declarations } */ |
|
303 |
/* |
|
304 |
* NOTE: we re-use the var_declarations_list_c |
|
305 |
*/ |
|
306 |
||
307 |
/* VAR_TEMP temp_var_decl_list END_VAR */ |
|
308 |
void *visit(temp_var_decls_c *symbol); |
|
309 |
void *visit(temp_var_decls_list_c *symbol); |
|
310 |
||
311 |
/* VAR NON_RETAIN var_init_decl_list END_VAR */ |
|
312 |
void *visit(non_retentive_var_decls_c *symbol); |
|
313 |
||
314 |
||
315 |
/**********************/ |
|
316 |
/* B 1.5.3 - Programs */ |
|
317 |
/**********************/ |
|
318 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
319 |
// SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
320 |
void *visit(program_declaration_c *symbol); |
|
321 |
||
322 |
/* intermediate helper symbol for program_declaration_c */ |
|
323 |
/* { io_var_declarations | other_var_declarations } */ |
|
324 |
/* |
|
325 |
* NOTE: we re-use the var_declarations_list_c |
|
326 |
*/ |
|
327 |
||
328 |
}; // function_param_iterator_c |
|
329 |
||
330 |
||
331 |
||
332 |
||
333 |
||
334 |
||
335 |