author | Mario de Sousa <msousa@fe.up.pt> |
Mon, 04 Apr 2011 10:48:30 +0100 | |
changeset 268 | 1eba01e3fb70 |
parent 265 | 4d222f46f8cc |
child 279 | c0453b7f99df |
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:
257
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
181 | 3 |
* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
181 | 5 |
* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
6 |
* 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:
257
diff
changeset
|
7 |
* 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:
257
diff
changeset
|
8 |
* 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:
257
diff
changeset
|
9 |
* (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:
257
diff
changeset
|
10 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
11 |
* 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:
257
diff
changeset
|
12 |
* 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:
257
diff
changeset
|
13 |
* 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:
257
diff
changeset
|
14 |
* 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:
257
diff
changeset
|
15 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
16 |
* 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:
257
diff
changeset
|
17 |
* 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:
257
diff
changeset
|
18 |
* |
181 | 19 |
* |
20 |
* This code is made available on the understanding that it will not be |
|
21 |
* used in safety-critical situations without a full and competent review. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
25 |
* An IEC 61131-3 compiler. |
181 | 26 |
* |
27 |
* Based on the |
|
28 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
29 |
* |
|
30 |
*/ |
|
31 |
||
32 |
/* Determine the data type of a specific constant or variable. |
|
33 |
* A reference to the relevant type definition is returned. |
|
34 |
* |
|
35 |
* For example: |
|
36 |
* 22 -> returns reference to a int_type_name_c object. |
|
37 |
* 22.2 -> returns reference to a real_type_name_c object. |
|
38 |
* LREAL#22.2 -> returns reference to a lreal_type_name_c object. |
|
39 |
* etc... |
|
40 |
*/ |
|
41 |
||
42 |
||
43 |
#include "search_constant_type.hh" |
|
44 |
||
45 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
46 |
/* function defined in main.cc */ |
|
47 |
extern void error_exit(const char *file_name, int line_no); |
|
48 |
||
49 |
symbol_c *search_constant_type_c::get_type(symbol_c *constant) { |
|
50 |
return (symbol_c *)constant->accept(*this); |
|
51 |
} |
|
52 |
||
53 |
||
54 |
/*********************/ |
|
55 |
/* B 1.2 - Constants */ |
|
56 |
/*********************/ |
|
57 |
||
58 |
/******************************/ |
|
59 |
/* B 1.2.1 - Numeric Literals */ |
|
60 |
/******************************/ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
61 |
/* 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
|
62 |
* 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
|
63 |
* 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
|
64 |
* 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
|
65 |
* 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
|
66 |
* 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
|
67 |
*/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
68 |
void *search_constant_type_c::visit(real_c *symbol) {return (void *)symbol;} |
257 | 69 |
void *search_constant_type_c::visit(neg_real_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
|
70 |
void *search_constant_type_c::visit(integer_c *symbol) {return (void *)symbol;} |
257 | 71 |
void *search_constant_type_c::visit(neg_integer_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
|
72 |
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
|
73 |
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
|
74 |
void *search_constant_type_c::visit(hex_integer_c *symbol) {return (void *)symbol;} |
181 | 75 |
|
76 |
void *search_constant_type_c::visit(integer_literal_c *symbol) |
|
77 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
78 |
void *search_constant_type_c::visit(real_literal_c *symbol) |
|
79 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
80 |
void *search_constant_type_c::visit(bit_string_literal_c *symbol) |
|
81 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
82 |
void *search_constant_type_c::visit(boolean_literal_c *symbol) |
|
83 |
{return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
84 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
88 |
|
181 | 89 |
/*******************************/ |
90 |
/* B.1.2.2 Character Strings */ |
|
91 |
/*******************************/ |
|
92 |
void *search_constant_type_c::visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;} |
|
93 |
void *search_constant_type_c::visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;} |
|
94 |
||
95 |
/***************************/ |
|
96 |
/* B 1.2.3 - Time Literals */ |
|
97 |
/***************************/ |
|
98 |
/************************/ |
|
99 |
/* B 1.2.3.1 - Duration */ |
|
100 |
/************************/ |
|
101 |
void *search_constant_type_c::visit(neg_time_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
257 | 102 |
void *search_constant_type_c::visit(duration_c *symbol) {return (void *)(symbol->type_name);} |
181 | 103 |
void *search_constant_type_c::visit(fixed_point_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
104 |
void *search_constant_type_c::visit(days_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
105 |
void *search_constant_type_c::visit(hours_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
106 |
void *search_constant_type_c::visit(minutes_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
107 |
void *search_constant_type_c::visit(seconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
108 |
void *search_constant_type_c::visit(milliseconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
109 |
||
110 |
/************************************/ |
|
111 |
/* B 1.2.3.2 - Time of day and Date */ |
|
112 |
/************************************/ |
|
257 | 113 |
void *search_constant_type_c::visit(time_of_day_c *symbol) {return (void *)(symbol->type_name);} |
181 | 114 |
void *search_constant_type_c::visit(daytime_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
257 | 115 |
void *search_constant_type_c::visit(date_c *symbol) {return (void *)(symbol->type_name);} |
181 | 116 |
void *search_constant_type_c::visit(date_literal_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
257 | 117 |
void *search_constant_type_c::visit(date_and_time_c *symbol) {return (void *)(symbol->type_name);} |
118 |
||
181 | 119 |
|
120 |
real_type_name_c search_constant_type_c::real_type_name; |
|
121 |
sint_type_name_c search_constant_type_c::sint_type_name; |
|
122 |
lint_type_name_c search_constant_type_c::lint_type_name; |
|
123 |
dint_type_name_c search_constant_type_c::dint_type_name; |
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
udint_type_name_c search_constant_type_c::udint_type_name; |
181 | 129 |
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
|
130 |
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
|
131 |
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
|
132 |
lword_type_name_c search_constant_type_c::lword_type_name; |
181 | 133 |
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
|
134 |
lreal_type_name_c search_constant_type_c::lreal_type_name; |
181 | 135 |
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
|
136 |
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
|
137 |
ulint_type_name_c search_constant_type_c::ulint_type_name; |
181 | 138 |
bool_type_name_c search_constant_type_c::bool_type_name; |
139 |
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
|
140 |
int_type_name_c search_constant_type_c::int_type_name; |
181 | 141 |
|
257 | 142 |
// safebool_type_name_c search_constant_type_c::safebool_type_name; |
143 |
/* The following is required because the expression (TOD_var - TOD_var) will result in a data type |
|
144 |
* (in this case, TIME) that is neither of the expression elements... |
|
145 |
*/ |
|
146 |
safetime_type_name_c search_constant_type_c::safetime_type_name; |
|
147 |
safetod_type_name_c search_constant_type_c::safetod_type_name; |
|
148 |
safedt_type_name_c search_constant_type_c::safedt_type_name; |
|
149 |
||
150 |
||
151 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
194
diff
changeset
|
152 |
/* 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
|
153 |
/* 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
|
154 |
integer_c search_constant_type_c::integer("1"); |