author | Matteo Facchinetti |
Thu, 25 Aug 2011 17:22:44 +0200 | |
changeset 370 | f3cec8694f65 |
parent 367 | 6d94128ba5ad |
child 371 | 926490780952 |
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:
238
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:
238
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
265
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
6 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
7 |
* 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:
238
diff
changeset
|
8 |
* 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:
238
diff
changeset
|
9 |
* 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:
238
diff
changeset
|
10 |
* (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:
238
diff
changeset
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
12 |
* 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:
238
diff
changeset
|
13 |
* 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:
238
diff
changeset
|
14 |
* 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:
238
diff
changeset
|
15 |
* 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:
238
diff
changeset
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
17 |
* 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:
238
diff
changeset
|
18 |
* 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:
238
diff
changeset
|
19 |
* |
181 | 20 |
* |
21 |
* This code is made available on the understanding that it will not be |
|
22 |
* used in safety-critical situations without a full and competent review. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
26 |
* An IEC 61131-3 compiler. |
181 | 27 |
* |
28 |
* Based on the |
|
29 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 |
* |
|
31 |
*/ |
|
32 |
||
33 |
/* Determine the data type of a variable. |
|
34 |
* The variable may be a simple variable, a function block instance, a |
|
35 |
* struture element within a data structured type (a struct or a fb), or |
|
36 |
* an array element. |
|
37 |
* A mixture of array element of a structure element of a structure element |
|
38 |
* of a .... is also suported! |
|
39 |
* |
|
40 |
* example: |
|
41 |
* window.points[1].coordinate.x |
|
42 |
* window.points[1].colour |
|
43 |
* etc... ARE ALLOWED! |
|
44 |
* |
|
45 |
* This class must be passed the scope within which the |
|
46 |
* variable was declared, and the variable name... |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
47 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
48 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
49 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
50 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
51 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
52 |
* This class has several members, depending on the exact data the caller |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
53 |
* is looking for... |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
54 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
55 |
* - item i: we can get either the name of the data type(A), |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
56 |
* or it's declaration (B) |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
57 |
* (notice however that some variables belong to a data type that does |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
58 |
* not have a name, only a declaration as in |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
59 |
* VAR a: ARRAY [1..3] of INT; END_VAR |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
60 |
* ) |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
61 |
* - item ii: we can get either the direct data type (1), |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
62 |
* or the base type (2) |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
63 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
64 |
* By direct type, I mean the data type of the variable. By base type, I |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
65 |
* mean the data type on which the direct type is based on. For example, in |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
66 |
* a subrange on INT, the direct type is the subrange itself, while the |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
67 |
* base type is INT. |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
68 |
* e.g. |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
69 |
* This means that if we find that the variable is of type MY_INT, |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
70 |
* which was previously declared to be |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
71 |
* TYPE MY_INT: INT := 9; |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
72 |
* option (1) will return MY_INT |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
73 |
* option (2) will return INT |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
74 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
75 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
76 |
* Member functions: |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
77 |
* ================ |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
78 |
* get_basetype_decl() ---> returns 2B |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
79 |
* get_type_id() ---> returns 1A |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
80 |
* |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
81 |
* Since we haven't yet needed them, we don't yet implement |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
82 |
* get_basetype_id() ----> would return 2A |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
83 |
* get_type_decl() ----> would return 1B |
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
84 |
*/ |
181 | 85 |
|
86 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
87 |
/* |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
88 |
* TODO: this code has a memory leak... |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
89 |
* We call 'new' in several locations, but bever get to 'delete' the object instances... |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
90 |
*/ |
181 | 91 |
#include "absyntax_utils.hh" |
92 |
||
93 |
||
94 |
search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { |
|
95 |
this->decompose_var_instance_name = NULL; |
|
96 |
this->current_structelement_name = NULL; |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
97 |
this->current_rawtype = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
98 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
99 |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
100 |
symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) { |
181 | 101 |
this->current_structelement_name = NULL; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
102 |
this->current_rawtype = NULL; |
181 | 103 |
this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
104 |
if (NULL == decompose_var_instance_name) ERROR; |
|
105 |
||
106 |
/* find the part of the variable name that will appear in the |
|
107 |
* variable declaration, for e.g., in window.point.x, this would be |
|
108 |
* window! |
|
109 |
*/ |
|
110 |
symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
111 |
if (NULL == var_name_part) ERROR; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
112 |
|
181 | 113 |
/* Now we try to find the variable instance declaration, to determine its type... */ |
114 |
symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
115 |
if (NULL == var_decl) { |
|
116 |
/* variable instance declaration not found! */ |
|
117 |
ERROR; |
|
118 |
} |
|
119 |
||
120 |
/* if it is a struct or function block, we must search the type |
|
121 |
* of the struct or function block member. |
|
122 |
* This is done by this class visiting the var_decl. |
|
123 |
* This class, while visiting, will recursively call |
|
124 |
* decompose_var_instance_name->get_next() when and if required... |
|
125 |
*/ |
|
126 |
symbol_c *res = (symbol_c *)var_decl->accept(*this); |
|
367
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
127 |
/* NOTE: A Null result is not really an internal compiler error, but rather an error in |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
128 |
* the IEC 61131-3 source code being compiled. This means we cannot just abort the compiler with ERROR. |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
129 |
* // if (NULL == res) ERROR; |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
130 |
*/ |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
131 |
if (NULL == res) return NULL; |
181 | 132 |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
133 |
/* make sure that we have decomposed all structure elements of the variable name */ |
181 | 134 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
367
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
135 |
/* NOTE: A non-NULL result is not really an internal compiler error, but rather an error in |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
136 |
* the IEC 61131-3 source code being compiled. |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
137 |
* (for example, 'int_var.struct_elem' in the source code, when 'int_var' is a simple integer, |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
138 |
* and not a structure, will result in this result being non-NULL!) |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
139 |
* This means we cannot just abort the compiler with ERROR. |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
140 |
* // if (NULL != var_name) ERROR; |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
141 |
*/ |
6d94128ba5ad
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
326
diff
changeset
|
142 |
if (NULL != var_name) return NULL; |
181 | 143 |
|
144 |
return res; |
|
145 |
} |
|
146 |
||
147 |
unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) { |
|
148 |
this->current_structelement_name = NULL; |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
149 |
this->current_rawtype = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
150 |
this->is_complex = false; |
181 | 151 |
this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
152 |
if (NULL == decompose_var_instance_name) ERROR; |
|
153 |
||
154 |
/* find the part of the variable name that will appear in the |
|
155 |
* variable declaration, for e.g., in window.point.x, this would be |
|
156 |
* window! |
|
157 |
*/ |
|
158 |
symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
159 |
if (NULL == var_name_part) ERROR; |
|
160 |
||
161 |
/* Now we try to find the variable instance declaration, to determine its type... */ |
|
162 |
symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
163 |
if (NULL == var_decl) { |
|
164 |
/* variable instance declaration not found! */ |
|
165 |
return 0; |
|
166 |
} |
|
167 |
||
168 |
/* if it is a struct or function block, we must search the type |
|
169 |
* of the struct or function block member. |
|
170 |
* This is done by this class visiting the var_decl. |
|
171 |
* This class, while visiting, will recursively call |
|
172 |
* decompose_var_instance_name->get_next() when and if required... |
|
173 |
*/ |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
174 |
var_decl->accept(*this); |
181 | 175 |
unsigned int res = search_var_instance_decl.get_vartype(); |
176 |
||
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
177 |
/* make sure that we have decomposed all structure elements of the variable name */ |
181 | 178 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
179 |
if (NULL != var_name) ERROR; |
|
180 |
||
181 |
return res; |
|
182 |
} |
|
183 |
||
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
184 |
symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) { |
326
e3fbf97d2021
Fix what I think is a bug (has not yet manifested itself).
Mario de Sousa <msousa@fe.up.pt>
parents:
321
diff
changeset
|
185 |
this->current_rawtype = NULL; |
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
186 |
symbol_c *rawtype = this->get_basetype_decl(variable_name); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
187 |
if (this->current_rawtype != NULL) |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
188 |
return this->current_rawtype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
189 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
190 |
return rawtype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
191 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
192 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
193 |
bool search_varfb_instance_type_c::type_is_complex(void) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
194 |
return this->is_complex; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
195 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
196 |
|
181 | 197 |
/* a helper function... */ |
198 |
void *search_varfb_instance_type_c::visit_list(list_c *list) { |
|
199 |
if (NULL == current_structelement_name) ERROR; |
|
200 |
||
201 |
for(int i = 0; i < list->n; i++) { |
|
202 |
void *res = list->elements[i]->accept(*this); |
|
203 |
if (res != NULL) |
|
204 |
return res; |
|
205 |
} |
|
206 |
/* not found! */ |
|
207 |
return NULL; |
|
208 |
} |
|
209 |
||
210 |
/* a helper function... */ |
|
211 |
void *search_varfb_instance_type_c::base_type(symbol_c *symbol) { |
|
212 |
search_base_type_c search_base_type; |
|
213 |
return symbol->accept(search_base_type); |
|
214 |
} |
|
215 |
||
216 |
/* We override the base class' visitor to identifier_c. |
|
217 |
* This is so because the base class does not consider a function block |
|
218 |
* to be a type, unlike this class that allows a variable instance |
|
219 |
* of a function block type... |
|
220 |
*/ |
|
221 |
void *search_varfb_instance_type_c::visit(identifier_c *type_name) { |
|
222 |
/* look up the type declaration... */ |
|
223 |
symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); |
|
224 |
if (fb_decl != function_block_type_symtable.end_value()) |
|
225 |
/* Type declaration found!! */ |
|
226 |
return fb_decl->accept(*this); |
|
227 |
||
235 | 228 |
this->current_rawtype = type_name; |
229 |
||
181 | 230 |
/* No. It is not a function block, so we let |
231 |
* the base class take care of it... |
|
232 |
*/ |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
233 |
if (NULL == decompose_var_instance_name->next_part(false)) { |
235 | 234 |
return base_type(type_name); |
235 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
236 |
else { |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
237 |
return search_base_type_c::visit(type_name); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
238 |
} |
181 | 239 |
} |
240 |
||
241 |
/********************************/ |
|
242 |
/* B 1.3.3 - Derived data types */ |
|
243 |
/********************************/ |
|
244 |
||
245 |
/* identifier ':' array_spec_init */ |
|
246 |
void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) { |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
247 |
this->is_complex = true; |
181 | 248 |
return symbol->array_spec_init->accept(*this); |
249 |
} |
|
250 |
||
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
251 |
/* array_specification [ASSIGN array_initialization] */ |
181 | 252 |
/* array_initialization may be NULL ! */ |
253 |
void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
254 |
this->is_complex = true; |
181 | 255 |
return symbol->array_specification->accept(*this); |
256 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
257 |
|
181 | 258 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
259 |
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) { |
|
235 | 260 |
this->is_complex = true; |
181 | 261 |
return symbol->non_generic_type_name->accept(*this); |
262 |
} |
|
263 |
||
264 |
/* structure_type_name ':' structure_specification */ |
|
265 |
void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) { |
|
235 | 266 |
this->is_complex = true; |
181 | 267 |
return symbol->structure_specification->accept(*this); |
268 |
/* NOTE: structure_specification will point to either a |
|
269 |
* initialized_structure_c |
|
270 |
* OR A |
|
271 |
* structure_element_declaration_list_c |
|
272 |
*/ |
|
273 |
} |
|
274 |
||
275 |
/* structure_type_name ASSIGN structure_initialization */ |
|
276 |
/* structure_initialization may be NULL ! */ |
|
277 |
// SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
278 |
void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
279 |
this->is_complex = true; |
181 | 280 |
/* recursively find out the data type of var_name... */ |
281 |
return symbol->structure_type_name->accept(*this); |
|
282 |
} |
|
283 |
||
284 |
/* helper symbol for structure_declaration */ |
|
285 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
286 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
287 |
void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol) { |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
288 |
/* make sure that we have decomposed all structure elements of the variable name */ |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
289 |
current_structelement_name = decompose_var_instance_name->next_part(); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
290 |
/* now search the structure declaration */ |
181 | 291 |
return visit_list(symbol); |
292 |
} |
|
293 |
||
294 |
/* structure_element_name ':' spec_init */ |
|
295 |
void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) { |
|
296 |
if (NULL == current_structelement_name) ERROR; |
|
297 |
||
298 |
if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) |
|
299 |
return symbol->spec_init->accept(*this); |
|
300 |
||
301 |
return NULL; |
|
302 |
} |
|
303 |
||
304 |
/* helper symbol for structure_initialization */ |
|
305 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
306 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
307 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
308 |
/* structure_element_name ASSIGN value */ |
|
309 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
310 |
||
311 |
||
312 |
||
313 |
/**************************************/ |
|
314 |
/* B.1.5 - Program organization units */ |
|
315 |
/**************************************/ |
|
316 |
/*****************************/ |
|
317 |
/* B 1.5.2 - Function Blocks */ |
|
318 |
/*****************************/ |
|
319 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
320 |
// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
321 |
void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) { |
|
322 |
/* make sure that we have decomposed all strcuture elements of the variable name */ |
|
323 |
||
324 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
325 |
if (NULL == var_name) { |
|
326 |
/* this is it... ! |
|
327 |
* No need to look any further... |
|
328 |
* Note also that, unlike for the struct types, a function block may |
|
329 |
* not be defined based on another (i.e. no inheritance is allowed), |
|
330 |
* so this function block is already the most base type. |
|
331 |
* We simply return it. |
|
332 |
*/ |
|
333 |
return (void *)symbol; |
|
334 |
} |
|
335 |
||
336 |
/* now search the function block declaration for the variable... */ |
|
337 |
search_var_instance_decl_c search_decl(symbol); |
|
338 |
symbol_c *var_decl = search_decl.get_decl(var_name); |
|
339 |
if (NULL == var_decl) { |
|
340 |
/* variable instance declaration not found! */ |
|
341 |
return NULL; |
|
342 |
} |
|
343 |
||
344 |
/* We have found the declaration. |
|
345 |
* Should we look any further? |
|
346 |
*/ |
|
347 |
var_name = decompose_var_instance_name->next_part(); |
|
348 |
if (NULL == var_name) { |
|
349 |
/* this is it... ! */ |
|
350 |
return base_type(var_decl); |
|
351 |
} |
|
352 |
||
353 |
current_structelement_name = var_name; |
|
354 |
/* recursively find out the data type of var_name... */ |
|
355 |
return symbol->var_declarations->accept(*this); |
|
356 |
} |