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!!
|
|
36 |
*/
|
|
37 |
#include "absyntax_utils.hh"
|
|
38 |
|
|
39 |
#define ERROR error_exit(__FILE__,__LINE__)
|
|
40 |
/* function defined in main.cc */
|
|
41 |
extern void error_exit(const char *file_name, int line_no);
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
search_base_type_c::search_base_type_c(void) {current_type_name = NULL;}
|
|
46 |
|
|
47 |
void *search_base_type_c::visit(identifier_c *type_name) {
|
|
48 |
this->current_type_name = type_name;
|
|
49 |
/* look up the type declaration... */
|
|
50 |
symbol_c *type_decl = type_symtable.find_value(type_name);
|
|
51 |
if (type_decl == type_symtable.end_value())
|
|
52 |
/* Type declaration not found!! */
|
|
53 |
ERROR;
|
|
54 |
|
|
55 |
return type_decl->accept(*this);
|
|
56 |
}
|
|
57 |
|
|
58 |
bool search_base_type_c::type_is_subrange(symbol_c* type_decl) {
|
|
59 |
this->is_subrange = false;
|
|
60 |
type_decl->accept(*this);
|
|
61 |
return this->is_subrange;
|
|
62 |
}
|
|
63 |
|
|
64 |
bool search_base_type_c::type_is_enumerated(symbol_c* type_decl) {
|
|
65 |
this->is_enumerated = false;
|
|
66 |
type_decl->accept(*this);
|
|
67 |
return this->is_enumerated;
|
|
68 |
}
|
|
69 |
|
|
70 |
/***********************************/
|
|
71 |
/* B 1.3.1 - Elementary Data Types */
|
|
72 |
/***********************************/
|
|
73 |
void *search_base_type_c::visit(time_type_name_c *symbol) {return (void *)symbol;}
|
|
74 |
void *search_base_type_c::visit(bool_type_name_c *symbol) {return (void *)symbol;}
|
|
75 |
void *search_base_type_c::visit(sint_type_name_c *symbol) {return (void *)symbol;}
|
|
76 |
void *search_base_type_c::visit(int_type_name_c *symbol) {return (void *)symbol;}
|
|
77 |
void *search_base_type_c::visit(dint_type_name_c *symbol) {return (void *)symbol;}
|
|
78 |
void *search_base_type_c::visit(lint_type_name_c *symbol) {return (void *)symbol;}
|
|
79 |
void *search_base_type_c::visit(usint_type_name_c *symbol) {return (void *)symbol;}
|
|
80 |
void *search_base_type_c::visit(uint_type_name_c *symbol) {return (void *)symbol;}
|
|
81 |
void *search_base_type_c::visit(udint_type_name_c *symbol) {return (void *)symbol;}
|
|
82 |
void *search_base_type_c::visit(ulint_type_name_c *symbol) {return (void *)symbol;}
|
|
83 |
void *search_base_type_c::visit(real_type_name_c *symbol) {return (void *)symbol;}
|
|
84 |
void *search_base_type_c::visit(lreal_type_name_c *symbol) {return (void *)symbol;}
|
|
85 |
void *search_base_type_c::visit(date_type_name_c *symbol) {return (void *)symbol;}
|
|
86 |
void *search_base_type_c::visit(tod_type_name_c *symbol) {return (void *)symbol;}
|
|
87 |
void *search_base_type_c::visit(dt_type_name_c *symbol) {return (void *)symbol;}
|
|
88 |
void *search_base_type_c::visit(byte_type_name_c *symbol) {return (void *)symbol;}
|
|
89 |
void *search_base_type_c::visit(word_type_name_c *symbol) {return (void *)symbol;}
|
|
90 |
void *search_base_type_c::visit(dword_type_name_c *symbol) {return (void *)symbol;}
|
|
91 |
void *search_base_type_c::visit(lword_type_name_c *symbol) {return (void *)symbol;}
|
|
92 |
void *search_base_type_c::visit(string_type_name_c *symbol) {return (void *)symbol;}
|
|
93 |
void *search_base_type_c::visit(wstring_type_name_c *symbol) {return (void *)symbol;}
|
|
94 |
void *search_base_type_c::visit(constant_int_type_name_c *symbol) {return (void *)symbol;}
|
|
95 |
void *search_base_type_c::visit(constant_real_type_name_c *symbol) {return (void *)symbol;}
|
|
96 |
void *search_base_type_c::visit(direct_variable_type_name_c *symbol) {return (void *)symbol;}
|
|
97 |
/******************************************************/
|
|
98 |
/* Extensions to the base standard as defined in */
|
|
99 |
/* "Safety Software Technical Specification, */
|
|
100 |
/* Part 1: Concepts and Function Blocks, */
|
|
101 |
/* Version 1.0 – Official Release" */
|
|
102 |
/* by PLCopen - Technical Committee 5 - 2006-01-31 */
|
|
103 |
/******************************************************/
|
|
104 |
void *search_base_type_c::visit(safebool_type_name_c *symbol) {return (void *)symbol;}
|
|
105 |
|
|
106 |
/********************************/
|
|
107 |
/* B 1.3.3 - Derived data types */
|
|
108 |
/********************************/
|
|
109 |
/* simple_type_name ':' simple_spec_init */
|
|
110 |
void *search_base_type_c::visit(simple_type_declaration_c *symbol) {
|
|
111 |
return symbol->simple_spec_init->accept(*this);
|
|
112 |
}
|
|
113 |
/* simple_specification ASSIGN constant */
|
|
114 |
void *search_base_type_c::visit(simple_spec_init_c *symbol) {
|
|
115 |
return symbol->simple_specification->accept(*this);
|
|
116 |
}
|
|
117 |
|
|
118 |
/* subrange_type_name ':' subrange_spec_init */
|
|
119 |
void *search_base_type_c::visit(subrange_type_declaration_c *symbol) {
|
|
120 |
return symbol->subrange_spec_init->accept(*this);
|
|
121 |
}
|
|
122 |
|
|
123 |
/* subrange_specification ASSIGN signed_integer */
|
|
124 |
void *search_base_type_c::visit(subrange_spec_init_c *symbol) {
|
|
125 |
this->is_subrange = true;
|
|
126 |
return symbol->subrange_specification->accept(*this);
|
|
127 |
}
|
|
128 |
|
|
129 |
/* integer_type_name '(' subrange')' */
|
|
130 |
void *search_base_type_c::visit(subrange_specification_c *symbol) {
|
|
131 |
return symbol->integer_type_name->accept(*this);
|
|
132 |
}
|
|
133 |
|
|
134 |
/* signed_integer DOTDOT signed_integer */
|
|
135 |
void *search_base_type_c::visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
136 |
|
|
137 |
/* enumerated_type_name ':' enumerated_spec_init */
|
|
138 |
void *search_base_type_c::visit(enumerated_type_declaration_c *symbol) {
|
|
139 |
this->current_type_name = symbol->enumerated_type_name;
|
|
140 |
return symbol->enumerated_spec_init->accept(*this);
|
|
141 |
}
|
|
142 |
|
|
143 |
/* enumerated_specification ASSIGN enumerated_value */
|
|
144 |
void *search_base_type_c::visit(enumerated_spec_init_c *symbol) {
|
|
145 |
this->is_enumerated = true;
|
|
146 |
return symbol->enumerated_specification->accept(*this);
|
|
147 |
}
|
|
148 |
|
|
149 |
/* helper symbol for enumerated_specification->enumerated_spec_init */
|
|
150 |
/* enumerated_value_list ',' enumerated_value */
|
|
151 |
void *search_base_type_c::visit(enumerated_value_list_c *symbol) {
|
|
152 |
if (NULL == this->current_type_name) ERROR;
|
|
153 |
return (void *)this->current_type_name;
|
|
154 |
}
|
|
155 |
|
|
156 |
/* enumerated_type_name '#' identifier */
|
|
157 |
// SYM_REF2(enumerated_value_c, type, value)
|
|
158 |
void *search_base_type_c::visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
159 |
|
|
160 |
/* identifier ':' array_spec_init */
|
|
161 |
void *search_base_type_c::visit(array_type_declaration_c *symbol) {
|
|
162 |
this->current_type_name = symbol->identifier;
|
|
163 |
return symbol->array_spec_init->accept(*this);
|
|
164 |
}
|
|
165 |
|
|
166 |
/* array_specification [ASSIGN array_initialization} */
|
|
167 |
/* array_initialization may be NULL ! */
|
|
168 |
void *search_base_type_c::visit(array_spec_init_c *symbol) {
|
|
169 |
return symbol->array_specification->accept(*this);
|
|
170 |
}
|
|
171 |
|
|
172 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
|
|
173 |
void *search_base_type_c::visit(array_specification_c *symbol) {
|
|
174 |
if (NULL == this->current_type_name) ERROR;
|
|
175 |
return symbol->non_generic_type_name->accept(*this);
|
|
176 |
}
|
|
177 |
|
|
178 |
/* helper symbol for array_specification */
|
|
179 |
/* array_subrange_list ',' subrange */
|
|
180 |
void *search_base_type_c::visit(array_subrange_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
181 |
|
|
182 |
/* array_initialization: '[' array_initial_elements_list ']' */
|
|
183 |
/* helper symbol for array_initialization */
|
|
184 |
/* array_initial_elements_list ',' array_initial_elements */
|
|
185 |
void *search_base_type_c::visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
186 |
|
|
187 |
/* integer '(' [array_initial_element] ')' */
|
|
188 |
/* array_initial_element may be NULL ! */
|
|
189 |
void *search_base_type_c::visit(array_initial_elements_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
190 |
|
|
191 |
/* structure_type_name ':' structure_specification */
|
|
192 |
/* NOTE: structure_specification will point to either a
|
|
193 |
* initialized_structure_c
|
|
194 |
* OR A
|
|
195 |
* structure_element_declaration_list_c
|
|
196 |
*/
|
|
197 |
void *search_base_type_c::visit(structure_type_declaration_c *symbol) {
|
|
198 |
this->current_type_name = symbol->structure_type_name;
|
|
199 |
return symbol->structure_specification->accept(*this);
|
|
200 |
}
|
|
201 |
|
|
202 |
/* structure_type_name ASSIGN structure_initialization */
|
|
203 |
/* structure_initialization may be NULL ! */
|
|
204 |
void *search_base_type_c::visit(initialized_structure_c *symbol) {
|
|
205 |
return symbol->structure_type_name->accept(*this);
|
|
206 |
}
|
|
207 |
|
|
208 |
/* helper symbol for structure_declaration */
|
|
209 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */
|
|
210 |
/* structure_element_declaration_list structure_element_declaration ';' */
|
|
211 |
void *search_base_type_c::visit(structure_element_declaration_list_c *symbol) {
|
|
212 |
if (NULL == this->current_type_name) ERROR;
|
|
213 |
return (void *)symbol;
|
|
214 |
}
|
|
215 |
|
|
216 |
/* structure_element_name ':' *_spec_init */
|
|
217 |
void *search_base_type_c::visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
218 |
|
|
219 |
/* helper symbol for structure_initialization */
|
|
220 |
/* structure_initialization: '(' structure_element_initialization_list ')' */
|
|
221 |
/* structure_element_initialization_list ',' structure_element_initialization */
|
|
222 |
void *search_base_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
223 |
|
|
224 |
/* structure_element_name ASSIGN value */
|
|
225 |
void *search_base_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
|
|
226 |
|
|
227 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
|
|
228 |
/*
|
|
229 |
SYM_REF4(string_type_declaration_c, string_type_name,
|
|
230 |
elementary_string_type_name,
|
|
231 |
string_type_declaration_size,
|
|
232 |
string_type_declaration_init) // may be == NULL!
|
|
233 |
*/
|
|
234 |
void *search_base_type_c::visit(string_type_declaration_c *symbol) {return symbol;}
|
|
235 |
|