|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) |
|
6 * |
|
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 |
|
9 * the Free Software Foundation, either version 3 of the License, or |
|
10 * (at your option) any later version. |
|
11 * |
|
12 * This program is distributed in the hope that it will be useful, |
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 * GNU General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License |
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
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 #ifndef _HELPER_FUNCTIONS_HH_ |
|
26 #define _HELPER_FUNCTIONS_HH_ |
|
27 |
|
28 #include "../absyntax/visitor.hh" |
|
29 #include <typeinfo> |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 typedef struct { |
|
36 symbol_c *function_name; |
|
37 symbol_c *nonformal_operand_list; |
|
38 symbol_c * formal_operand_list; |
|
39 |
|
40 enum {POU_FB, POU_function} POU_type; |
|
41 //symbol_c &*datatype; |
|
42 //std::vector <symbol_c *> &candidate_datatypes; |
|
43 std::vector <symbol_c *> &candidate_functions; |
|
44 symbol_c *&called_function_declaration; |
|
45 int &extensible_param_count; |
|
46 } generic_function_call_t; |
|
47 |
|
48 |
|
49 |
|
50 /* A small helper class, to transform elementary data type to string. |
|
51 * this allows us to generate more relevant error messages... |
|
52 */ |
|
53 |
|
54 class elementary_type_c: public null_visitor_c { |
|
55 protected: |
|
56 elementary_type_c(void) {}; |
|
57 ~elementary_type_c(void) {}; |
|
58 |
|
59 private: |
|
60 /* singleton class! */ |
|
61 static elementary_type_c *singleton; |
|
62 |
|
63 public: |
|
64 static const char * to_string(symbol_c *symbol); |
|
65 |
|
66 |
|
67 /***********************************/ |
|
68 /* B 1.3.1 - Elementary Data Types */ |
|
69 /***********************************/ |
|
70 void *visit(time_type_name_c *symbol) {return (void *)"TIME"; }; |
|
71 void *visit(bool_type_name_c *symbol) {return (void *)"BOOL"; }; |
|
72 void *visit(sint_type_name_c *symbol) {return (void *)"SINT"; }; |
|
73 void *visit(int_type_name_c *symbol) {return (void *)"INT"; }; |
|
74 void *visit(dint_type_name_c *symbol) {return (void *)"DINT"; }; |
|
75 void *visit(lint_type_name_c *symbol) {return (void *)"LINT"; }; |
|
76 void *visit(usint_type_name_c *symbol) {return (void *)"USINT"; }; |
|
77 void *visit(uint_type_name_c *symbol) {return (void *)"UINT"; }; |
|
78 void *visit(udint_type_name_c *symbol) {return (void *)"UDINT"; }; |
|
79 void *visit(ulint_type_name_c *symbol) {return (void *)"ULINT"; }; |
|
80 void *visit(real_type_name_c *symbol) {return (void *)"REAL"; }; |
|
81 void *visit(lreal_type_name_c *symbol) {return (void *)"LREAL"; }; |
|
82 void *visit(date_type_name_c *symbol) {return (void *)"DATE"; }; |
|
83 void *visit(tod_type_name_c *symbol) {return (void *)"TOD"; }; |
|
84 void *visit(dt_type_name_c *symbol) {return (void *)"DT"; }; |
|
85 void *visit(byte_type_name_c *symbol) {return (void *)"BYTE"; }; |
|
86 void *visit(word_type_name_c *symbol) {return (void *)"WORD"; }; |
|
87 void *visit(lword_type_name_c *symbol) {return (void *)"LWORD"; }; |
|
88 void *visit(dword_type_name_c *symbol) {return (void *)"DWORD"; }; |
|
89 void *visit(string_type_name_c *symbol) {return (void *)"STRING"; }; |
|
90 void *visit(wstring_type_name_c *symbol) {return (void *)"WSTRING"; }; |
|
91 |
|
92 void *visit(safetime_type_name_c *symbol) {return (void *)"SAFETIME"; }; |
|
93 void *visit(safebool_type_name_c *symbol) {return (void *)"SAFEBOOL"; }; |
|
94 void *visit(safesint_type_name_c *symbol) {return (void *)"SAFESINT"; }; |
|
95 void *visit(safeint_type_name_c *symbol) {return (void *)"SAFEINT"; }; |
|
96 void *visit(safedint_type_name_c *symbol) {return (void *)"SAFEDINT"; }; |
|
97 void *visit(safelint_type_name_c *symbol) {return (void *)"SAFELINT"; }; |
|
98 void *visit(safeusint_type_name_c *symbol) {return (void *)"SAFEUSINT"; }; |
|
99 void *visit(safeuint_type_name_c *symbol) {return (void *)"SAFEUINT"; }; |
|
100 void *visit(safeudint_type_name_c *symbol) {return (void *)"SAFEUDINT"; }; |
|
101 void *visit(safeulint_type_name_c *symbol) {return (void *)"SAFEULINT"; }; |
|
102 void *visit(safereal_type_name_c *symbol) {return (void *)"SAFEREAL"; }; |
|
103 void *visit(safelreal_type_name_c *symbol) {return (void *)"SAFELREAL"; }; |
|
104 void *visit(safedate_type_name_c *symbol) {return (void *)"SAFEDATE"; }; |
|
105 void *visit(safetod_type_name_c *symbol) {return (void *)"SAFETOD"; }; |
|
106 void *visit(safedt_type_name_c *symbol) {return (void *)"SAFEDT"; }; |
|
107 void *visit(safebyte_type_name_c *symbol) {return (void *)"SAFEBYTE"; }; |
|
108 void *visit(safeword_type_name_c *symbol) {return (void *)"SAFEWORD"; }; |
|
109 void *visit(safelword_type_name_c *symbol) {return (void *)"SAFELWORD"; }; |
|
110 void *visit(safedword_type_name_c *symbol) {return (void *)"SAFEDWORD"; }; |
|
111 void *visit(safestring_type_name_c *symbol) {return (void *)"SAFESTRING"; }; |
|
112 void *visit(safewstring_type_name_c *symbol) {return (void *)"SAFEWSTRING"; }; |
|
113 }; |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 /* Widening Primitive Conversion */ |
|
121 struct widen_entry { |
|
122 symbol_c *left; |
|
123 symbol_c *right; |
|
124 symbol_c *result; |
|
125 enum {ok, deprecated} status; |
|
126 }; |
|
127 /* |
|
128 * 2.5.1.5.6 Functions of time data types |
|
129 * Table 30 - page 64 |
|
130 */ |
|
131 extern const struct widen_entry widen_ADD_table[]; |
|
132 extern const struct widen_entry widen_SUB_table[]; |
|
133 extern const struct widen_entry widen_MUL_table[]; |
|
134 extern const struct widen_entry widen_DIV_table[]; |
|
135 extern const struct widen_entry widen_MOD_table[]; |
|
136 extern const struct widen_entry widen_EXPT_table[]; |
|
137 extern const struct widen_entry widen_AND_table[]; |
|
138 extern const struct widen_entry widen_OR_table[]; |
|
139 extern const struct widen_entry widen_XOR_table[]; |
|
140 extern const struct widen_entry widen_CMP_table[]; |
|
141 |
|
142 /* Search for a datatype inside a candidate_datatypes list. |
|
143 * Returns: position of datatype in the list, or -1 if not found. |
|
144 */ |
|
145 int search_in_candidate_datatype_list(symbol_c *datatype, const std::vector <symbol_c *> &candidate_datatypes); |
|
146 |
|
147 /* Remove a datatype inside a candidate_datatypes list. |
|
148 * Returns: If successful it returns true, false otherwise. |
|
149 */ |
|
150 bool remove_from_candidate_datatype_list(symbol_c *datatype, std::vector <symbol_c *> &candidate_datatypes); |
|
151 |
|
152 /* Intersect two candidate_datatype_lists. |
|
153 * Remove from list1 (origin, dest.) all elements that are not found in list2 (with). |
|
154 * In essence, list1 will contain the result of the intersection of list1 with list2. |
|
155 * In other words, modify list1 so it only contains the elelements that are simultaneously in list1 and list2! |
|
156 */ |
|
157 void intersect_candidate_datatype_list(symbol_c *list1 /*origin, dest.*/, symbol_c *list2 /*with*/); |
|
158 |
|
159 /* intersect the candidate_datatype lists of all prev_il_intructions, and set the local candidate_datatype list to the result! */ |
|
160 void intersect_prev_candidate_datatype_lists(il_instruction_c *symbol); |
|
161 |
|
162 |
|
163 |
|
164 /* A helper function... */ |
|
165 bool is_ANY_ELEMENTARY_type (symbol_c *type_symbol); |
|
166 bool is_ANY_SAFEELEMENTARY_type (symbol_c *type_symbol); |
|
167 bool is_ANY_ELEMENTARY_compatible (symbol_c *type_symbol); |
|
168 |
|
169 bool is_ANY_MAGNITUDE_type (symbol_c *type_symbol); |
|
170 bool is_ANY_SAFEMAGNITUDE_type (symbol_c *type_symbol); |
|
171 bool is_ANY_MAGNITUDE_compatible (symbol_c *type_symbol); |
|
172 |
|
173 bool is_ANY_signed_MAGNITUDE_type (symbol_c *type_symbol); |
|
174 bool is_ANY_signed_SAFEMAGNITUDE_type (symbol_c *type_symbol); |
|
175 bool is_ANY_signed_MAGNITUDE_compatible (symbol_c *type_symbol); |
|
176 |
|
177 bool is_ANY_DATE_type (symbol_c *type_symbol); |
|
178 bool is_ANY_SAFEDATE_type (symbol_c *type_symbol); |
|
179 bool is_ANY_DATE_compatible (symbol_c *type_symbol); |
|
180 |
|
181 bool is_ANY_STRING_type (symbol_c *type_symbol); |
|
182 bool is_ANY_SAFESTRING_type (symbol_c *type_symbol); |
|
183 bool is_ANY_STRING_compatible (symbol_c *type_symbol); |
|
184 |
|
185 bool is_ANY_INT_type (symbol_c *type_symbol); |
|
186 bool is_ANY_SAFEINT_type (symbol_c *type_symbol); |
|
187 bool is_ANY_INT_compatible (symbol_c *type_symbol); |
|
188 |
|
189 bool is_ANY_signed_INT_type (symbol_c *type_symbol); |
|
190 bool is_ANY_signed_SAFEINT_type (symbol_c *type_symbol); |
|
191 bool is_ANY_signed_INT_compatible (symbol_c *type_symbol); |
|
192 |
|
193 bool is_ANY_REAL_type (symbol_c *type_symbol); |
|
194 bool is_ANY_SAFEREAL_type (symbol_c *type_symbol); |
|
195 bool is_ANY_REAL_compatible (symbol_c *type_symbol); |
|
196 |
|
197 bool is_ANY_NUM_type (symbol_c *type_symbol); |
|
198 bool is_ANY_SAFENUM_type (symbol_c *type_symbol); |
|
199 bool is_ANY_NUM_compatible (symbol_c *type_symbol); |
|
200 |
|
201 bool is_ANY_signed_NUM_type (symbol_c *type_symbol); |
|
202 bool is_ANY_signed_SAFENUM_type (symbol_c *type_symbol); |
|
203 bool is_ANY_signed_NUM_compatible (symbol_c *type_symbol); |
|
204 |
|
205 bool is_ANY_BIT_type (symbol_c *type_symbol); |
|
206 bool is_ANY_SAFEBIT_type (symbol_c *type_symbol); |
|
207 bool is_ANY_BIT_compatible (symbol_c *type_symbol); |
|
208 |
|
209 bool is_BOOL_type (symbol_c *type_symbol); |
|
210 bool is_SAFEBOOL_type (symbol_c *type_symbol); |
|
211 bool is_ANY_BOOL_compatible (symbol_c *type_symbol); |
|
212 |
|
213 |
|
214 bool is_type_equal(symbol_c *first_type, symbol_c *second_type); |
|
215 bool is_type_valid(symbol_c *type); |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 #endif /* _HELPER_FUNCTIONS_HH_ */ |