author | Lolitech |
Thu, 03 Jun 2010 12:59:32 +0200 | |
changeset 243 | bdfee1f5be9e |
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 of a specific constant or variable. |
|
26 |
* A reference to the relevant type definition is returned. |
|
27 |
* |
|
28 |
* For example: |
|
29 |
* 22 -> returns reference to a int_type_name_c object. |
|
30 |
* 22.2 -> returns reference to a real_type_name_c object. |
|
31 |
* LREAL#22.2 -> returns reference to a lreal_type_name_c object. |
|
32 |
* etc... |
|
33 |
*/ |
|
34 |
||
35 |
||
36 |
#include "search_constant_type.hh" |
|
37 |
||
38 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
39 |
/* function defined in main.cc */ |
|
40 |
extern void error_exit(const char *file_name, int line_no); |
|
41 |
||
42 |
symbol_c *search_constant_type_c::get_type(symbol_c *constant) { |
|
43 |
return (symbol_c *)constant->accept(*this); |
|
44 |
} |
|
45 |
||
46 |
||
47 |
/*********************/ |
|
48 |
/* B 1.2 - Constants */ |
|
49 |
/*********************/ |
|
50 |
||
51 |
/******************************/ |
|
52 |
/* B 1.2.1 - Numeric Literals */ |
|
53 |
/******************************/ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
54 |
/* 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
|
55 |
* 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
|
56 |
* 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
|
57 |
* 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
|
58 |
* 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
|
59 |
* 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
|
60 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
61 |
void *search_constant_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
|
62 |
void *search_constant_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
|
63 |
void *search_constant_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
|
64 |
void *search_constant_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
|
65 |
void *search_constant_type_c::visit(hex_integer_c *symbol) {return (void *)symbol;} |
181 | 66 |
|
67 |
void *search_constant_type_c::visit(integer_literal_c *symbol) |
|
68 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
69 |
void *search_constant_type_c::visit(real_literal_c *symbol) |
|
70 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
71 |
void *search_constant_type_c::visit(bit_string_literal_c *symbol) |
|
72 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
73 |
void *search_constant_type_c::visit(boolean_literal_c *symbol) |
|
74 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
75 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
76 |
void *search_constant_type_c::visit(boolean_true_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
77 |
void *search_constant_type_c::visit(boolean_false_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
78 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
79 |
|
181 | 80 |
/*******************************/ |
81 |
/* B.1.2.2 Character Strings */ |
|
82 |
/*******************************/ |
|
83 |
void *search_constant_type_c::visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;} |
|
84 |
void *search_constant_type_c::visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;} |
|
85 |
||
86 |
/***************************/ |
|
87 |
/* B 1.2.3 - Time Literals */ |
|
88 |
/***************************/ |
|
89 |
/************************/ |
|
90 |
/* B 1.2.3.1 - Duration */ |
|
91 |
/************************/ |
|
92 |
void *search_constant_type_c::visit(neg_time_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
93 |
void *search_constant_type_c::visit(duration_c *symbol) {return (void *)&time_type_name;} |
|
94 |
void *search_constant_type_c::visit(fixed_point_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
95 |
void *search_constant_type_c::visit(days_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
96 |
void *search_constant_type_c::visit(hours_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
97 |
void *search_constant_type_c::visit(minutes_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
98 |
void *search_constant_type_c::visit(seconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
99 |
void *search_constant_type_c::visit(milliseconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
100 |
||
101 |
/************************************/ |
|
102 |
/* B 1.2.3.2 - Time of day and Date */ |
|
103 |
/************************************/ |
|
104 |
void *search_constant_type_c::visit(time_of_day_c *symbol) {return (void *)&tod_type_name;} |
|
105 |
void *search_constant_type_c::visit(daytime_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
106 |
void *search_constant_type_c::visit(date_c *symbol) {return (void *)&date_type_name;} |
|
107 |
void *search_constant_type_c::visit(date_literal_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
108 |
void *search_constant_type_c::visit(date_and_time_c *symbol) {return (void *)&dt_type_name;} |
|
109 |
||
110 |
real_type_name_c search_constant_type_c::real_type_name; |
|
111 |
sint_type_name_c search_constant_type_c::sint_type_name; |
|
112 |
lint_type_name_c search_constant_type_c::lint_type_name; |
|
113 |
dint_type_name_c search_constant_type_c::dint_type_name; |
|
114 |
date_type_name_c search_constant_type_c::date_type_name; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
115 |
dword_type_name_c search_constant_type_c::dword_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
116 |
dt_type_name_c search_constant_type_c::dt_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
117 |
tod_type_name_c search_constant_type_c::tod_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
118 |
udint_type_name_c search_constant_type_c::udint_type_name; |
181 | 119 |
word_type_name_c search_constant_type_c::word_type_name; |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
120 |
wstring_type_name_c search_constant_type_c::wstring_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
121 |
string_type_name_c search_constant_type_c::string_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
122 |
lword_type_name_c search_constant_type_c::lword_type_name; |
181 | 123 |
uint_type_name_c search_constant_type_c::uint_type_name; |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
124 |
lreal_type_name_c search_constant_type_c::lreal_type_name; |
181 | 125 |
byte_type_name_c search_constant_type_c::byte_type_name; |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
126 |
usint_type_name_c search_constant_type_c::usint_type_name; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
127 |
ulint_type_name_c search_constant_type_c::ulint_type_name; |
181 | 128 |
bool_type_name_c search_constant_type_c::bool_type_name; |
129 |
time_type_name_c search_constant_type_c::time_type_name; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
130 |
int_type_name_c search_constant_type_c::int_type_name; |
181 | 131 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
132 |
/* |
181 | 133 |
constant_real_type_name_c search_constant_type_c::constant_real_type_name; |
134 |
constant_int_type_name_c search_constant_type_c::constant_int_type_name; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
135 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
136 |
/* temporarily here until we remove the st_code_gen.c and il_code_gen.c files... */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
137 |
/* It should then move to search_expression_type_c */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
138 |
integer_c search_constant_type_c::integer("1"); |