|
1 /* |
|
2 * (c) 2003 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 |
|
27 /* Determine the data type of a variable. |
|
28 * The variable may be a simple variable, a function block instance, a |
|
29 * struture element within a data structured type (a struct or a fb), or |
|
30 * an array element. |
|
31 * A mixture of array element of a structure element of a structure element |
|
32 * of a .... is also suported! |
|
33 * |
|
34 * A reference to the relevant base type __definition__ is returned. |
|
35 * This means that if we find that the variable is of type MY_INT, |
|
36 * which was previously declared to be |
|
37 * TYPE MY_INT: INT := 9; |
|
38 * this class wil return INT, and __not__ MY_INT !! |
|
39 * |
|
40 * |
|
41 * example: |
|
42 * window.points[1].coordinate.x |
|
43 * window.points[1].colour |
|
44 * etc... ARE ALLOWED! |
|
45 * |
|
46 * This class must be passed the scope within which the |
|
47 * variable was declared, and the variable name... |
|
48 */ |
|
49 class search_varfb_instance_type_c: public search_base_type_c { |
|
50 private: |
|
51 search_var_instance_decl_c search_var_instance_decl; |
|
52 decompose_var_instance_name_c *decompose_var_instance_name; |
|
53 symbol_c *current_structelement_name; |
|
54 |
|
55 public: |
|
56 search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { |
|
57 this->decompose_var_instance_name = NULL; |
|
58 this->current_structelement_name = NULL; |
|
59 } |
|
60 |
|
61 |
|
62 symbol_c *get_type(symbol_c *variable_name) { |
|
63 this->current_structelement_name = NULL; |
|
64 this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
|
65 if (NULL == decompose_var_instance_name) ERROR; |
|
66 |
|
67 /* find the part of the variable name that will appear in the |
|
68 * variable declaration, for e.g., in window.point.x, this would be |
|
69 * window! |
|
70 */ |
|
71 symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
72 if (NULL == var_name_part) ERROR; |
|
73 |
|
74 /* Now we try to find the variable instance declaration, to determine its type... */ |
|
75 symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
76 if (NULL == var_decl) { |
|
77 /* variable instance declaration not found! */ |
|
78 return NULL; |
|
79 } |
|
80 |
|
81 /* if it is a struct or function block, we must search the type |
|
82 * of the struct or function block member. |
|
83 * This is done by this class visiting the var_decl. |
|
84 * This class, while visiting, will recursively call |
|
85 * decompose_var_instance_name->get_next() when and if required... |
|
86 */ |
|
87 symbol_c *res = (symbol_c *)var_decl->accept(*this); |
|
88 if (NULL == res) ERROR; |
|
89 |
|
90 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
91 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
92 if (NULL != var_name) ERROR; |
|
93 |
|
94 return res; |
|
95 } |
|
96 |
|
97 unsigned int get_vartype(symbol_c *variable_name) { |
|
98 this->current_structelement_name = NULL; |
|
99 this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
|
100 if (NULL == decompose_var_instance_name) ERROR; |
|
101 |
|
102 /* find the part of the variable name that will appear in the |
|
103 * variable declaration, for e.g., in window.point.x, this would be |
|
104 * window! |
|
105 */ |
|
106 symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
107 if (NULL == var_name_part) ERROR; |
|
108 |
|
109 /* Now we try to find the variable instance declaration, to determine its type... */ |
|
110 symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
111 if (NULL == var_decl) { |
|
112 /* variable instance declaration not found! */ |
|
113 return 0; |
|
114 } |
|
115 |
|
116 /* if it is a struct or function block, we must search the type |
|
117 * of the struct or function block member. |
|
118 * This is done by this class visiting the var_decl. |
|
119 * This class, while visiting, will recursively call |
|
120 * decompose_var_instance_name->get_next() when and if required... |
|
121 */ |
|
122 unsigned int res = search_var_instance_decl.get_vartype(); |
|
123 |
|
124 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
125 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
126 if (NULL != var_name) ERROR; |
|
127 |
|
128 return res; |
|
129 } |
|
130 |
|
131 private: |
|
132 /* a helper function... */ |
|
133 void *visit_list(list_c *list) { |
|
134 if (NULL == current_structelement_name) ERROR; |
|
135 |
|
136 for(int i = 0; i < list->n; i++) { |
|
137 void *res = list->elements[i]->accept(*this); |
|
138 if (res != NULL) |
|
139 return res; |
|
140 } |
|
141 /* not found! */ |
|
142 return NULL; |
|
143 } |
|
144 |
|
145 /* a helper function... */ |
|
146 void *base_type(symbol_c *symbol) { |
|
147 search_base_type_c search_base_type; |
|
148 return symbol->accept(search_base_type); |
|
149 } |
|
150 |
|
151 |
|
152 private: |
|
153 /* We override the base class' visitor to identifier_c. |
|
154 * This is so because the base class does not consider a function block |
|
155 * to be a type, unlike this class that allows a variable instance |
|
156 * of a function block type... |
|
157 */ |
|
158 void *visit(identifier_c *type_name) { |
|
159 /* look up the type declaration... */ |
|
160 symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); |
|
161 if (fb_decl != function_block_type_symtable.end_value()) |
|
162 /* Type declaration found!! */ |
|
163 return fb_decl->accept(*this); |
|
164 |
|
165 /* No. It is not a function block, so we let |
|
166 * the base class take care of it... |
|
167 */ |
|
168 return search_base_type_c::visit(type_name); |
|
169 } |
|
170 |
|
171 /********************************/ |
|
172 /* B 1.3.3 - Derived data types */ |
|
173 /********************************/ |
|
174 /* structure_type_name ':' structure_specification */ |
|
175 void *visit(structure_type_declaration_c *symbol) { |
|
176 return symbol->structure_specification->accept(*this); |
|
177 /* NOTE: structure_specification will point to either a |
|
178 * initialized_structure_c |
|
179 * OR A |
|
180 * structure_element_declaration_list_c |
|
181 */ |
|
182 } |
|
183 |
|
184 /* structure_type_name ASSIGN structure_initialization */ |
|
185 /* structure_initialization may be NULL ! */ |
|
186 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
187 void *visit(initialized_structure_c *symbol) { |
|
188 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
189 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
190 if (NULL == var_name) { |
|
191 /* this is it... ! |
|
192 * No need to look any further... |
|
193 */ |
|
194 /* NOTE: we could simply do a |
|
195 * return (void *)symbol; |
|
196 * nevertheless, note that this search_varfb_instance_type_c |
|
197 * class inherits from the search_base_type_c class, |
|
198 * which means that it will usually return the base type, |
|
199 * and not the derived type (*). If we are to be consistent, |
|
200 * we should guarantee that we always return the base type. |
|
201 * To do this we could use |
|
202 * return (void *)symbol->accept(*this); |
|
203 * since this class inherits from the search_base_type_c. |
|
204 * However, in this case we don't want it to follow |
|
205 * the structs as this search_varfb_instance_type_c does. |
|
206 * We therefore have to create a new search_base_type_c |
|
207 * instance to search through this type without going |
|
208 * through the structs... |
|
209 */ |
|
210 return base_type(symbol->structure_type_name); |
|
211 } |
|
212 |
|
213 /* now search the structure declaration */ |
|
214 current_structelement_name = var_name; |
|
215 /* recursively find out the data type of var_name... */ |
|
216 return symbol->structure_type_name->accept(*this); |
|
217 } |
|
218 |
|
219 /* helper symbol for structure_declaration */ |
|
220 /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
221 /* structure_element_declaration_list structure_element_declaration ';' */ |
|
222 void *visit(structure_element_declaration_list_c *symbol) {return visit_list(symbol);} |
|
223 |
|
224 /* structure_element_name ':' spec_init */ |
|
225 void *visit(structure_element_declaration_c *symbol) { |
|
226 if (NULL == current_structelement_name) ERROR; |
|
227 |
|
228 if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) |
|
229 return symbol->spec_init->accept(*this); |
|
230 |
|
231 return NULL; |
|
232 } |
|
233 |
|
234 /* helper symbol for structure_initialization */ |
|
235 /* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
236 /* structure_element_initialization_list ',' structure_element_initialization */ |
|
237 void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
238 /* structure_element_name ASSIGN value */ |
|
239 void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
240 |
|
241 |
|
242 |
|
243 /**************************************/ |
|
244 /* B.1.5 - Program organization units */ |
|
245 /**************************************/ |
|
246 /*****************************/ |
|
247 /* B 1.5.2 - Function Blocks */ |
|
248 /*****************************/ |
|
249 /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
250 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
251 void *visit(function_block_declaration_c *symbol) { |
|
252 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
253 |
|
254 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
255 if (NULL == var_name) { |
|
256 /* this is it... ! |
|
257 * No need to look any further... |
|
258 * Note also that, unlike for the struct types, a function block may |
|
259 * not be defined based on another (i.e. no inheritance is allowed), |
|
260 * so this function block is already the most base type. |
|
261 * We simply return it. |
|
262 */ |
|
263 return (void *)symbol; |
|
264 } |
|
265 |
|
266 /* now search the function block declaration for the variable... */ |
|
267 search_var_instance_decl_c search_decl(symbol); |
|
268 symbol_c *var_decl = search_decl.get_decl(var_name); |
|
269 if (NULL == var_decl) { |
|
270 /* variable instance declaration not found! */ |
|
271 return NULL; |
|
272 } |
|
273 |
|
274 /* We have found the declaration. |
|
275 * Should we look any further? |
|
276 */ |
|
277 var_name = decompose_var_instance_name->next_part(); |
|
278 if (NULL == var_name) { |
|
279 /* this is it... ! */ |
|
280 return base_type(var_decl); |
|
281 } |
|
282 |
|
283 current_structelement_name = var_name; |
|
284 /* recursively find out the data type of var_name... */ |
|
285 return symbol->var_declarations->accept(*this); |
|
286 } |
|
287 |
|
288 }; |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |