|
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 |
|
26 /* |
|
27 * Function call parameter iterator. |
|
28 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 |
|
35 //#include <stdio.h> /* required for NULL */ |
|
36 //#include <string> |
|
37 //#include <iostream> |
|
38 |
|
39 //#include "../../util/symtable.hh" |
|
40 |
|
41 //#include "generate_c.hh" |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 /* given a function_body_c, iterate through each |
|
47 * function in/out/inout parameter, returning the name |
|
48 * of each parameter... |
|
49 */ |
|
50 class function_call_iterator_c : public iterator_visitor_c { |
|
51 private: |
|
52 symbol_c *start_symbol; |
|
53 int next_fcall, fcall_count; |
|
54 //identifier_c *current_fcall_name; |
|
55 symbol_c *current_fcall_name; |
|
56 //function_invocation_c *current_finvocation; |
|
57 symbol_c *current_finvocation; |
|
58 |
|
59 public: |
|
60 /* initialise the iterator object. |
|
61 * We must be given a reference to the function declaration |
|
62 * that will be analysed... |
|
63 */ |
|
64 function_call_iterator_c(symbol_c *symbol) { |
|
65 this->start_symbol = symbol; |
|
66 next_fcall = fcall_count = 0; |
|
67 current_finvocation = NULL; |
|
68 current_fcall_name = NULL; |
|
69 } |
|
70 |
|
71 /* Skip to the next function call. After object creation, |
|
72 * the object references _before_ the first, so |
|
73 * this function must be called once to get the object to |
|
74 * reference the first function call... |
|
75 * |
|
76 * Returns the function_invocation_c! |
|
77 */ |
|
78 //function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called "); |
|
79 symbol_c *next(void) {TRACE("function_call_iterator_c::next(): called "); |
|
80 fcall_count = 0; |
|
81 next_fcall++; |
|
82 current_finvocation = NULL; |
|
83 current_fcall_name = NULL; |
|
84 |
|
85 start_symbol->accept(*this); |
|
86 return current_finvocation; |
|
87 } |
|
88 |
|
89 /* Returns the name of the currently referenced function invocation */ |
|
90 identifier_c *fname(void) { |
|
91 identifier_c *identifier = dynamic_cast<identifier_c *>(current_fcall_name); |
|
92 if (identifier == NULL) ERROR; |
|
93 return identifier; |
|
94 } |
|
95 |
|
96 |
|
97 /***************************************/ |
|
98 /* B.3 - Language ST (Structured Text) */ |
|
99 /***************************************/ |
|
100 /***********************/ |
|
101 /* B 3.1 - Expressions */ |
|
102 /***********************/ |
|
103 void *visit(function_invocation_c *symbol) { |
|
104 fcall_count++; |
|
105 if (next_fcall == fcall_count) { |
|
106 current_finvocation = symbol; |
|
107 current_fcall_name = symbol->function_name; |
|
108 } |
|
109 return NULL; |
|
110 } |
|
111 |
|
112 |
|
113 |
|
114 /****************************************/ |
|
115 /* B.2 - Language IL (Instruction List) */ |
|
116 /****************************************/ |
|
117 /***********************************/ |
|
118 /* B 2.1 Instructions and Operands */ |
|
119 /***********************************/ |
|
120 |
|
121 /* | function_name [il_operand_list] */ |
|
122 // SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
123 void *visit(il_function_call_c *symbol) { |
|
124 fcall_count++; |
|
125 if (next_fcall == fcall_count) { |
|
126 current_finvocation = symbol; |
|
127 current_fcall_name = symbol->function_name; |
|
128 } |
|
129 return NULL; |
|
130 } |
|
131 |
|
132 |
|
133 |
|
134 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
135 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) |
|
136 void *visit(il_formal_funct_call_c *symbol) { |
|
137 fcall_count++; |
|
138 if (next_fcall == fcall_count) { |
|
139 current_finvocation = symbol; |
|
140 current_fcall_name = symbol->function_name; |
|
141 } |
|
142 return NULL; |
|
143 } |
|
144 |
|
145 |
|
146 }; |
|
147 |
|
148 |
|
149 |
|
150 |