author | Catarina Boucinha <ccb@fe.up.pt> |
Thu, 27 Aug 2009 16:18:56 +0100 | |
changeset 203 | b8a2f4c86745 |
parent 202 | da1a8186f86f |
child 257 | 90782e241346 |
permissions | -rwxr-xr-x |
181 | 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 |
/* Determine the data type on which another data type is based on. |
|
26 |
* If a new default initial value is given, we DO NOT consider it a |
|
27 |
* new base class, and continue looking further! |
|
28 |
* |
|
29 |
* E.g. TYPE new_int_t : INT; END_TYPE; |
|
30 |
* TYPE new_int2_t : INT = 2; END_TYPE; |
|
31 |
* TYPE new_subr_t : INT (4..5); END_TYPE; |
|
32 |
* |
|
33 |
* new_int_t is really an INT!! |
|
34 |
* new_int2_t is also really an INT!! |
|
35 |
* new_subr_t is also really an INT!! |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
36 |
* |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
37 |
* Note that a FB declaration is also considered a base type, as |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
38 |
* we may have FB instances declared of a specific FB type. |
181 | 39 |
*/ |
40 |
#include "absyntax_utils.hh" |
|
41 |
||
42 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
43 |
/* function defined in main.cc */ |
|
44 |
extern void error_exit(const char *file_name, int line_no); |
|
45 |
||
46 |
||
47 |
||
48 |
search_base_type_c::search_base_type_c(void) {current_type_name = NULL;} |
|
49 |
||
50 |
void *search_base_type_c::visit(identifier_c *type_name) { |
|
51 |
this->current_type_name = type_name; |
|
52 |
/* look up the type declaration... */ |
|
53 |
symbol_c *type_decl = type_symtable.find_value(type_name); |
|
54 |
if (type_decl == type_symtable.end_value()) |
|
55 |
/* Type declaration not found!! */ |
|
56 |
ERROR; |
|
57 |
||
58 |
return type_decl->accept(*this); |
|
59 |
} |
|
60 |
||
61 |
bool search_base_type_c::type_is_subrange(symbol_c* type_decl) { |
|
62 |
this->is_subrange = false; |
|
63 |
type_decl->accept(*this); |
|
64 |
return this->is_subrange; |
|
65 |
} |
|
66 |
||
67 |
bool search_base_type_c::type_is_enumerated(symbol_c* type_decl) { |
|
68 |
this->is_enumerated = false; |
|
69 |
type_decl->accept(*this); |
|
70 |
return this->is_enumerated; |
|
71 |
} |
|
72 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
73 |
/*********************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
74 |
/* B 1.2 - Constants */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
75 |
/*********************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
76 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
77 |
/******************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
78 |
/* B 1.2.1 - Numeric Literals */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
79 |
/******************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
80 |
/* Numeric literals without any explicit type cast have unknown data type, |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
81 |
* so we continue considering them as their own basic data types until |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
82 |
* they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
83 |
* numeric literal '30' must then be considered a LINT so the ADD function may be called |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
84 |
* with all inputs of the same data type. |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
85 |
* If 'x' were a SINT, then the '30' would have to be a SINT too! |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
86 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
87 |
void *search_base_type_c::visit(real_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
88 |
void *search_base_type_c::visit(integer_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
89 |
void *search_base_type_c::visit(binary_integer_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
90 |
void *search_base_type_c::visit(octal_integer_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
91 |
void *search_base_type_c::visit(hex_integer_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
92 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
93 |
|
181 | 94 |
/***********************************/ |
95 |
/* B 1.3.1 - Elementary Data Types */ |
|
96 |
/***********************************/ |
|
97 |
void *search_base_type_c::visit(time_type_name_c *symbol) {return (void *)symbol;} |
|
98 |
void *search_base_type_c::visit(bool_type_name_c *symbol) {return (void *)symbol;} |
|
99 |
void *search_base_type_c::visit(sint_type_name_c *symbol) {return (void *)symbol;} |
|
100 |
void *search_base_type_c::visit(int_type_name_c *symbol) {return (void *)symbol;} |
|
101 |
void *search_base_type_c::visit(dint_type_name_c *symbol) {return (void *)symbol;} |
|
102 |
void *search_base_type_c::visit(lint_type_name_c *symbol) {return (void *)symbol;} |
|
103 |
void *search_base_type_c::visit(usint_type_name_c *symbol) {return (void *)symbol;} |
|
104 |
void *search_base_type_c::visit(uint_type_name_c *symbol) {return (void *)symbol;} |
|
105 |
void *search_base_type_c::visit(udint_type_name_c *symbol) {return (void *)symbol;} |
|
106 |
void *search_base_type_c::visit(ulint_type_name_c *symbol) {return (void *)symbol;} |
|
107 |
void *search_base_type_c::visit(real_type_name_c *symbol) {return (void *)symbol;} |
|
108 |
void *search_base_type_c::visit(lreal_type_name_c *symbol) {return (void *)symbol;} |
|
109 |
void *search_base_type_c::visit(date_type_name_c *symbol) {return (void *)symbol;} |
|
110 |
void *search_base_type_c::visit(tod_type_name_c *symbol) {return (void *)symbol;} |
|
111 |
void *search_base_type_c::visit(dt_type_name_c *symbol) {return (void *)symbol;} |
|
112 |
void *search_base_type_c::visit(byte_type_name_c *symbol) {return (void *)symbol;} |
|
113 |
void *search_base_type_c::visit(word_type_name_c *symbol) {return (void *)symbol;} |
|
114 |
void *search_base_type_c::visit(dword_type_name_c *symbol) {return (void *)symbol;} |
|
115 |
void *search_base_type_c::visit(lword_type_name_c *symbol) {return (void *)symbol;} |
|
116 |
void *search_base_type_c::visit(string_type_name_c *symbol) {return (void *)symbol;} |
|
117 |
void *search_base_type_c::visit(wstring_type_name_c *symbol) {return (void *)symbol;} |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
118 |
/* |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
119 |
void *search_base_type_c::visit(constant_int_type_name_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
120 |
void *search_base_type_c::visit(constant_real_type_name_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
121 |
*/ |
181 | 122 |
/******************************************************/ |
123 |
/* Extensions to the base standard as defined in */ |
|
124 |
/* "Safety Software Technical Specification, */ |
|
125 |
/* Part 1: Concepts and Function Blocks, */ |
|
126 |
/* Version 1.0 – Official Release" */ |
|
127 |
/* by PLCopen - Technical Committee 5 - 2006-01-31 */ |
|
128 |
/******************************************************/ |
|
129 |
void *search_base_type_c::visit(safebool_type_name_c *symbol) {return (void *)symbol;} |
|
130 |
||
131 |
/********************************/ |
|
132 |
/* B 1.3.3 - Derived data types */ |
|
133 |
/********************************/ |
|
134 |
/* simple_type_name ':' simple_spec_init */ |
|
135 |
void *search_base_type_c::visit(simple_type_declaration_c *symbol) { |
|
136 |
return symbol->simple_spec_init->accept(*this); |
|
137 |
} |
|
138 |
/* simple_specification ASSIGN constant */ |
|
139 |
void *search_base_type_c::visit(simple_spec_init_c *symbol) { |
|
140 |
return symbol->simple_specification->accept(*this); |
|
141 |
} |
|
142 |
||
143 |
/* subrange_type_name ':' subrange_spec_init */ |
|
144 |
void *search_base_type_c::visit(subrange_type_declaration_c *symbol) { |
|
145 |
return symbol->subrange_spec_init->accept(*this); |
|
146 |
} |
|
147 |
||
148 |
/* subrange_specification ASSIGN signed_integer */ |
|
149 |
void *search_base_type_c::visit(subrange_spec_init_c *symbol) { |
|
150 |
this->is_subrange = true; |
|
151 |
return symbol->subrange_specification->accept(*this); |
|
152 |
} |
|
153 |
||
154 |
/* integer_type_name '(' subrange')' */ |
|
155 |
void *search_base_type_c::visit(subrange_specification_c *symbol) { |
|
156 |
return symbol->integer_type_name->accept(*this); |
|
157 |
} |
|
158 |
||
159 |
/* signed_integer DOTDOT signed_integer */ |
|
160 |
void *search_base_type_c::visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
161 |
||
162 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
163 |
void *search_base_type_c::visit(enumerated_type_declaration_c *symbol) { |
|
164 |
this->current_type_name = symbol->enumerated_type_name; |
|
165 |
return symbol->enumerated_spec_init->accept(*this); |
|
166 |
} |
|
167 |
||
168 |
/* enumerated_specification ASSIGN enumerated_value */ |
|
169 |
void *search_base_type_c::visit(enumerated_spec_init_c *symbol) { |
|
170 |
this->is_enumerated = true; |
|
171 |
return symbol->enumerated_specification->accept(*this); |
|
172 |
} |
|
173 |
||
174 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
175 |
/* enumerated_value_list ',' enumerated_value */ |
|
176 |
void *search_base_type_c::visit(enumerated_value_list_c *symbol) { |
|
177 |
if (NULL == this->current_type_name) ERROR; |
|
178 |
return (void *)this->current_type_name; |
|
179 |
} |
|
180 |
||
181 |
/* enumerated_type_name '#' identifier */ |
|
182 |
// SYM_REF2(enumerated_value_c, type, value) |
|
183 |
void *search_base_type_c::visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
184 |
||
185 |
/* identifier ':' array_spec_init */ |
|
186 |
void *search_base_type_c::visit(array_type_declaration_c *symbol) { |
|
187 |
this->current_type_name = symbol->identifier; |
|
188 |
return symbol->array_spec_init->accept(*this); |
|
189 |
} |
|
190 |
||
191 |
/* array_specification [ASSIGN array_initialization} */ |
|
192 |
/* array_initialization may be NULL ! */ |
|
193 |
void *search_base_type_c::visit(array_spec_init_c *symbol) { |
|
194 |
return symbol->array_specification->accept(*this); |
|
195 |
} |
|
196 |
||
197 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
198 |
void *search_base_type_c::visit(array_specification_c *symbol) { |
|
199 |
if (NULL == this->current_type_name) ERROR; |
|
200 |
return symbol->non_generic_type_name->accept(*this); |
|
201 |
} |
|
202 |
||
203 |
/* helper symbol for array_specification */ |
|
204 |
/* array_subrange_list ',' subrange */ |
|
205 |
void *search_base_type_c::visit(array_subrange_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
206 |
||
207 |
/* array_initialization: '[' array_initial_elements_list ']' */ |
|
208 |
/* helper symbol for array_initialization */ |
|
209 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
210 |
void *search_base_type_c::visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
211 |
||
212 |
/* integer '(' [array_initial_element] ')' */ |
|
213 |
/* array_initial_element may be NULL ! */ |
|
214 |
void *search_base_type_c::visit(array_initial_elements_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
215 |
||
216 |
/* structure_type_name ':' structure_specification */ |
|
217 |
/* NOTE: structure_specification will point to either a |
|
218 |
* initialized_structure_c |
|
219 |
* OR A |
|
220 |
* structure_element_declaration_list_c |
|
221 |
*/ |
|
222 |
void *search_base_type_c::visit(structure_type_declaration_c *symbol) { |
|
223 |
this->current_type_name = symbol->structure_type_name; |
|
224 |
return symbol->structure_specification->accept(*this); |
|
225 |
} |
|
226 |
||
227 |
/* structure_type_name ASSIGN structure_initialization */ |
|
228 |
/* structure_initialization may be NULL ! */ |
|
229 |
void *search_base_type_c::visit(initialized_structure_c *symbol) { |
|
230 |
return symbol->structure_type_name->accept(*this); |
|
231 |
} |
|
232 |
||
233 |
/* helper symbol for structure_declaration */ |
|
234 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
235 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
236 |
void *search_base_type_c::visit(structure_element_declaration_list_c *symbol) { |
|
237 |
if (NULL == this->current_type_name) ERROR; |
|
238 |
return (void *)symbol; |
|
239 |
} |
|
240 |
||
241 |
/* structure_element_name ':' *_spec_init */ |
|
242 |
void *search_base_type_c::visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
243 |
||
244 |
/* helper symbol for structure_initialization */ |
|
245 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
246 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
247 |
void *search_base_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
248 |
||
249 |
/* structure_element_name ASSIGN value */ |
|
250 |
void *search_base_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
251 |
||
252 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
253 |
/* |
|
254 |
SYM_REF4(string_type_declaration_c, string_type_name, |
|
255 |
elementary_string_type_name, |
|
256 |
string_type_declaration_size, |
|
257 |
string_type_declaration_init) // may be == NULL! |
|
258 |
*/ |
|
259 |
void *search_base_type_c::visit(string_type_declaration_c *symbol) {return symbol;} |
|
260 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
261 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
262 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
263 |
/*****************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
264 |
/* B 1.5.2 - Function Blocks */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
265 |
/*****************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
266 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
267 |
// SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body) |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
268 |
void *search_base_type_c::visit(function_block_declaration_c *symbol) { |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
269 |
return symbol; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
270 |
} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
271 |