1 /* |
1 /* |
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
3 * |
3 * |
4 * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
4 * Copyright (C) 2003-2012 Mario de Sousa (msousa@fe.up.pt) |
5 * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
5 * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
6 * |
6 * |
7 * This program is free software: you can redistribute it and/or modify |
7 * This program is free software: you can redistribute it and/or modify |
8 * it under the terms of the GNU General Public License as published by |
8 * it under the terms of the GNU General Public License as published by |
9 * the Free Software Foundation, either version 3 of the License, or |
9 * the Free Software Foundation, either version 3 of the License, or |
53 |
53 |
54 |
54 |
55 |
55 |
56 search_base_type_c::search_base_type_c(void) {current_type_name = NULL;} |
56 search_base_type_c::search_base_type_c(void) {current_type_name = NULL;} |
57 |
57 |
58 void *search_base_type_c::visit(identifier_c *type_name) { |
58 |
59 this->current_type_name = type_name; |
59 |
60 /* look up the type declaration... */ |
60 |
61 symbol_c *type_decl = type_symtable.find_value(type_name); |
61 symbol_c *search_base_type_c::get_basetype_decl(symbol_c *symbol) { |
62 if (type_decl == type_symtable.end_value()) |
62 if (NULL == symbol) |
63 /* Type declaration not found!! */ |
63 return NULL; |
64 ERROR; |
64 |
65 |
65 return (symbol_c *)symbol->accept(*this); |
66 return type_decl->accept(*this); |
66 } |
67 } |
67 |
|
68 symbol_c *search_base_type_c::get_basetype_id (symbol_c *symbol) { |
|
69 if (NULL == symbol) |
|
70 return NULL; |
|
71 |
|
72 current_type_name = NULL; /* just to be on the safe side... */ |
|
73 symbol->accept(*this); |
|
74 return (symbol_c *)current_type_name; |
|
75 } |
|
76 |
|
77 |
|
78 /* Note by MJS: The following two functions definately do not belong in this class!! Maybe create a new utility class? |
|
79 * I will need to clean this up when the opportunity arises! |
|
80 */ |
68 |
81 |
69 bool search_base_type_c::type_is_subrange(symbol_c* type_decl) { |
82 bool search_base_type_c::type_is_subrange(symbol_c* type_decl) { |
70 this->is_subrange = false; |
83 this->is_subrange = false; |
71 type_decl->accept(*this); |
84 type_decl->accept(*this); |
72 return this->is_subrange; |
85 return this->is_subrange; |
75 bool search_base_type_c::type_is_enumerated(symbol_c* type_decl) { |
88 bool search_base_type_c::type_is_enumerated(symbol_c* type_decl) { |
76 this->is_enumerated = false; |
89 this->is_enumerated = false; |
77 type_decl->accept(*this); |
90 type_decl->accept(*this); |
78 return this->is_enumerated; |
91 return this->is_enumerated; |
79 } |
92 } |
|
93 |
|
94 |
|
95 /*************************/ |
|
96 /* B.1 - Common elements */ |
|
97 /*************************/ |
|
98 |
|
99 /*******************************************/ |
|
100 /* B 1.1 - Letters, digits and identifiers */ |
|
101 /*******************************************/ |
|
102 void *search_base_type_c::visit(identifier_c *type_name) { |
|
103 symbol_c *type_decl; |
|
104 |
|
105 this->current_type_name = type_name; |
|
106 |
|
107 /* look up the type declaration... */ |
|
108 type_decl = type_symtable.find_value(type_name); |
|
109 if (type_decl != type_symtable.end_value()) |
|
110 return type_decl->accept(*this); |
|
111 |
|
112 type_decl = function_block_type_symtable.find_value(type_name); |
|
113 if (type_decl != function_block_type_symtable.end_value()) |
|
114 return type_decl->accept(*this); |
|
115 |
|
116 /* Type declaration not found!! */ |
|
117 ERROR; |
|
118 |
|
119 return NULL; |
|
120 } |
|
121 |
80 |
122 |
81 /*********************/ |
123 /*********************/ |
82 /* B 1.2 - Constants */ |
124 /* B 1.2 - Constants */ |
83 /*********************/ |
125 /*********************/ |
84 |
126 |
203 } |
245 } |
204 |
246 |
205 /* helper symbol for enumerated_specification->enumerated_spec_init */ |
247 /* helper symbol for enumerated_specification->enumerated_spec_init */ |
206 /* enumerated_value_list ',' enumerated_value */ |
248 /* enumerated_value_list ',' enumerated_value */ |
207 void *search_base_type_c::visit(enumerated_value_list_c *symbol) { |
249 void *search_base_type_c::visit(enumerated_value_list_c *symbol) { |
208 if (NULL == this->current_type_name) ERROR; |
250 return (void *)symbol; |
209 return (void *)this->current_type_name; |
|
210 } |
251 } |
211 |
252 |
212 /* enumerated_type_name '#' identifier */ |
253 /* enumerated_type_name '#' identifier */ |
213 // SYM_REF2(enumerated_value_c, type, value) |
254 // SYM_REF2(enumerated_value_c, type, value) |
214 void *search_base_type_c::visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
255 void *search_base_type_c::visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
220 } |
261 } |
221 |
262 |
222 /* array_specification [ASSIGN array_initialization} */ |
263 /* array_specification [ASSIGN array_initialization} */ |
223 /* array_initialization may be NULL ! */ |
264 /* array_initialization may be NULL ! */ |
224 void *search_base_type_c::visit(array_spec_init_c *symbol) { |
265 void *search_base_type_c::visit(array_spec_init_c *symbol) { |
|
266 /* Note that the 'array_specification' may be either an identifier of a previsously defined array type, |
|
267 * or an array_specification_c, so we can not stop here and simply return a array_spec_init_c, |
|
268 * especially if we are looking for the base class! |
|
269 */ |
225 return symbol->array_specification->accept(*this); |
270 return symbol->array_specification->accept(*this); |
226 } |
271 } |
227 |
272 |
228 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
273 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
229 void *search_base_type_c::visit(array_specification_c *symbol) { |
274 void *search_base_type_c::visit(array_specification_c *symbol) { |
230 if (NULL == this->current_type_name) |
275 return symbol; |
231 this->current_type_name = symbol->non_generic_type_name; |
|
232 return symbol->non_generic_type_name->accept(*this); |
|
233 } |
276 } |
234 |
277 |
235 /* helper symbol for array_specification */ |
278 /* helper symbol for array_specification */ |
236 /* array_subrange_list ',' subrange */ |
279 /* array_subrange_list ',' subrange */ |
237 void *search_base_type_c::visit(array_subrange_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
280 void *search_base_type_c::visit(array_subrange_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |