author | Edouard Tisserant |
Wed, 24 Nov 2021 08:58:58 +0100 | |
changeset 1095 | 36bbd0b64816 |
parent 1041 | 56ebe2a31b5b |
permissions | -rw-r--r-- |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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 |
181 | 6 |
* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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:
181
diff
changeset
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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:
181
diff
changeset
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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 |
#include "absyntax_utils.hh" |
|
34 |
||
35 |
||
36 |
/* Returns the function block type declaration |
|
37 |
* of a specific function block instance. |
|
38 |
*/ |
|
39 |
||
40 |
||
41 |
||
42 |
/* Returns the type name of a specific function block |
|
43 |
* instance. This class will search the variable |
|
44 |
* declarations inside the scope given to it |
|
45 |
* searching for the declaration of the function |
|
46 |
* block instance. |
|
47 |
* |
|
48 |
* The class constructor must be given the search scope |
|
49 |
* (function, function block or program within which |
|
50 |
* the function block instance was declared). |
|
51 |
* |
|
52 |
* This class will search the tree from the root given to the |
|
53 |
* constructor. Another option would be to build a symbol table, |
|
54 |
* and search that instead. Building the symbol table would be done |
|
55 |
* while visiting the variable declaration objects in the parse |
|
56 |
* tree. Unfortuantely, generate_c_c does not visit these |
|
57 |
* objects, delegating it to another class. This means that |
|
58 |
* we would need another specialised class just to build the |
|
59 |
* symbol table. We might just as well have a specialised class |
|
60 |
* that searches the tree itself for the relevant info. This |
|
61 |
* class is exactly that...! |
|
62 |
*/ |
|
63 |
search_fb_instance_decl_c::search_fb_instance_decl_c(symbol_c *search_scope) { |
|
64 |
this->search_scope = search_scope; |
|
65 |
this->current_fb_type_name = NULL; |
|
66 |
} |
|
67 |
||
68 |
symbol_c *search_fb_instance_decl_c::get_type_name(symbol_c *fb_instance_name) { |
|
69 |
this->search_name = fb_instance_name; |
|
70 |
return (symbol_c *)search_scope->accept(*this); |
|
71 |
} |
|
72 |
||
73 |
/***************************/ |
|
74 |
/* B 0 - Programming Model */ |
|
75 |
/***************************/ |
|
76 |
void *search_fb_instance_decl_c::visit(library_c *symbol) { |
|
77 |
/* we do not want to search multiple declaration scopes, |
|
78 |
* so we do not visit all the functions, fucntion blocks, etc... |
|
79 |
*/ |
|
80 |
return NULL; |
|
81 |
} |
|
82 |
||
83 |
/******************************************/ |
|
84 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
85 |
/******************************************/ |
|
86 |
||
87 |
/* name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
88 |
/* structure_initialization -> may be NULL ! */ |
|
89 |
void *search_fb_instance_decl_c::visit(fb_name_decl_c *symbol) { |
|
810
d9c48ad646f1
Add a new node to the abstract symtax tree, which will let us do datatype checking of FB variable declarations using the standard algorithm, and no special cases.
Mario de Sousa <msousa@fe.up.pt>
parents:
706
diff
changeset
|
90 |
current_fb_type_name = spec_init_sperator_c::get_spec(symbol->fb_spec_init); |
181 | 91 |
return symbol->fb_name_list->accept(*this); |
92 |
} |
|
93 |
||
94 |
/* name_list ',' fb_name */ |
|
95 |
void *search_fb_instance_decl_c::visit(fb_name_list_c *symbol) { |
|
96 |
list_c *list = symbol; |
|
97 |
for(int i = 0; i < list->n; i++) { |
|
1041
56ebe2a31b5b
Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents:
814
diff
changeset
|
98 |
if (compare_identifiers(list->get_element(i), search_name) == 0) |
181 | 99 |
/* by now, current_fb_declaration should be != NULL */ |
100 |
return current_fb_type_name; |
|
101 |
} |
|
102 |
return NULL; |
|
103 |
} |
|
104 |
||
706
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
105 |
/* name_list ',' fb_name */ |
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
106 |
void *search_fb_instance_decl_c::visit(external_declaration_c *symbol) { |
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
107 |
if (compare_identifiers(symbol->global_var_name, search_name) == 0) |
814
a98a4dad904a
Fix bug related to FB invocation of FB instances declared as VAR_EXTERN.
Mario de Sousa <msousa@fe.up.pt>
parents:
810
diff
changeset
|
108 |
return spec_init_sperator_c::get_spec(symbol->specification); |
706
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
109 |
return NULL; |
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
110 |
} |
31553c22f318
Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents:
279
diff
changeset
|
111 |
|
181 | 112 |
/**************************************/ |
113 |
/* B.1.5 - Program organization units */ |
|
114 |
/**************************************/ |
|
115 |
/***********************/ |
|
116 |
/* B 1.5.1 - Functions */ |
|
117 |
/***********************/ |
|
118 |
void *search_fb_instance_decl_c::visit(function_declaration_c *symbol) { |
|
119 |
/* no need to search through all the body, so we only |
|
120 |
* visit the variable declarations...! |
|
121 |
*/ |
|
122 |
return symbol->var_declarations_list->accept(*this); |
|
123 |
} |
|
124 |
||
125 |
/*****************************/ |
|
126 |
/* B 1.5.2 - Function Blocks */ |
|
127 |
/*****************************/ |
|
128 |
void *search_fb_instance_decl_c::visit(function_block_declaration_c *symbol) { |
|
129 |
/* no need to search through all the body, so we only |
|
130 |
* visit the variable declarations...! |
|
131 |
*/ |
|
132 |
return symbol->var_declarations->accept(*this); |
|
133 |
} |
|
134 |
||
135 |
/**********************/ |
|
136 |
/* B 1.5.3 - Programs */ |
|
137 |
/**********************/ |
|
138 |
void *search_fb_instance_decl_c::visit(program_declaration_c *symbol) { |
|
139 |
/* no need to search through all the body, so we only |
|
140 |
* visit the variable declarations...! |
|
141 |
*/ |
|
142 |
return symbol->var_declarations->accept(*this); |
|
143 |
} |