|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2003-2011 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 /* |
|
26 * An IEC 61131-3 compiler. |
|
27 * |
|
28 * Based on the |
|
29 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 * |
|
31 */ |
|
32 |
|
33 |
|
34 /* Declaration sequence is a source code part needed to declare variables. |
|
35 * There are some checks we need to do before start with other analysis: |
|
36 * |
|
37 * - Check external option redefinition. |
|
38 * - Check external data type redefinition. |
|
39 * - Check initial values consistently with the data types of the variables/data types being declared. |
|
40 * - Check whether a function block uses a CONSTANT qualifier as described in 2.5.2.1. |
|
41 * |
|
42 */ |
|
43 |
|
44 |
|
45 #include "declaration_check.hh" |
|
46 #include "datatype_functions.hh" |
|
47 |
|
48 #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) |
|
49 #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) |
|
50 |
|
51 #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ |
|
52 if (current_display_error_level >= error_level) { \ |
|
53 fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ |
|
54 FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ |
|
55 LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
|
56 fprintf(stderr, __VA_ARGS__); \ |
|
57 fprintf(stderr, "\n"); \ |
|
58 error_count++; \ |
|
59 } \ |
|
60 } |
|
61 |
|
62 |
|
63 #define STAGE3_WARNING(symbol1, symbol2, ...) { \ |
|
64 fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ |
|
65 FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ |
|
66 LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
|
67 fprintf(stderr, __VA_ARGS__); \ |
|
68 fprintf(stderr, "\n"); \ |
|
69 warning_found = true; \ |
|
70 } |
|
71 |
|
72 |
|
73 declaration_check_c::declaration_check_c(symbol_c *ignore) { |
|
74 error_count = 0; |
|
75 } |
|
76 |
|
77 declaration_check_c::~declaration_check_c(void) { |
|
78 |
|
79 } |
|
80 |
|
81 int declaration_check_c::get_error_count() { |
|
82 return error_count; |
|
83 } |
|
84 |
|
85 void declaration_check_c::check_global_decl(symbol_c *p_decl) { |
|
86 symbol_c *var_name; |
|
87 search_base_type_c search_base_type; |
|
88 |
|
89 search_var_instance_decl_c search_var_instance_glo_decl(global_var_decls); |
|
90 search_var_instance_decl_c search_var_instance_ext_decl(p_decl); |
|
91 function_param_iterator_c fpi(p_decl); |
|
92 while((var_name = fpi.next()) != NULL) { |
|
93 if (fpi.param_direction() == function_param_iterator_c::direction_extref) { |
|
94 /* found an external reference parameter. */ |
|
95 symbol_c *glo_decl = search_var_instance_glo_decl.get_decl(var_name); |
|
96 symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name); |
|
97 if (search_var_instance_glo_decl.get_option(var_name) != search_var_instance_ext_decl.get_option(var_name)) |
|
98 STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error an external redefinition option."); |
|
99 |
|
100 /* TODO: Check redefinition data type. |
|
101 * We need a new class (like search_base_type class) to get type id by variable declaration. |
|
102 * symbol_c *glo_type = ????; |
|
103 * symbol_c *ext_type = fpi.param_type(); |
|
104 * if (! is_type_equal(glo_type, ext_type)) |
|
105 * STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error an external redefinition data type."); |
|
106 */ |
|
107 } |
|
108 } |
|
109 |
|
110 } |
|
111 |
|
112 /******************************************/ |
|
113 /* B 1.4.3 - Declaration & Initialisation */ |
|
114 /******************************************/ |
|
115 void *declaration_check_c::visit(global_var_declarations_c *symbol) { |
|
116 global_var_decls = dynamic_cast<list_c *>(symbol->global_var_decl_list); |
|
117 if (NULL == global_var_decls) |
|
118 ERROR; |
|
119 return NULL; |
|
120 } |
|
121 |
|
122 /********************************/ |
|
123 /* B 1.7 Configuration elements */ |
|
124 /********************************/ |
|
125 void *declaration_check_c::visit(program_configuration_c *symbol) { |
|
126 symbol_c *p_decl = program_type_symtable.find_value(symbol->program_type_name); |
|
127 if (p_decl == program_type_symtable.end_value()) |
|
128 p_decl = function_block_type_symtable.find_value(symbol->program_type_name); |
|
129 /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */ |
|
130 if (p_decl == function_block_type_symtable.end_value()) |
|
131 ERROR; |
|
132 check_global_decl(p_decl); |
|
133 return NULL; |
|
134 } |
|
135 |