1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ExpressionNode.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 13:17:07 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Aug 9 12:28:40 2016 |
---|
13 | // Update Count : 482 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | #include <cctype> |
---|
18 | #include <climits> |
---|
19 | #include <cstdio> |
---|
20 | #include <algorithm> |
---|
21 | #include <sstream> |
---|
22 | |
---|
23 | #include "ParseNode.h" |
---|
24 | #include "TypeData.h" |
---|
25 | #include "SynTree/Constant.h" |
---|
26 | #include "SynTree/Expression.h" |
---|
27 | #include "SynTree/Declaration.h" |
---|
28 | #include "Common/UnimplementedError.h" |
---|
29 | #include "parseutility.h" |
---|
30 | #include "Common/utility.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ), extension( other.extension ) {} |
---|
35 | |
---|
36 | //############################################################################## |
---|
37 | |
---|
38 | // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: |
---|
39 | // |
---|
40 | // prefix action constant action suffix |
---|
41 | // |
---|
42 | // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: |
---|
43 | // |
---|
44 | // constant BEGIN CONT ... |
---|
45 | // <CONT>(...)? BEGIN 0 ... // possible empty suffix |
---|
46 | // |
---|
47 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their |
---|
48 | // type. |
---|
49 | |
---|
50 | static Type::Qualifiers emptyQualifiers; // no qualifiers on constants |
---|
51 | |
---|
52 | static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
53 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
54 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
55 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
56 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } |
---|
57 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } |
---|
58 | |
---|
59 | Expression *build_constantInteger( std::string & str ) { |
---|
60 | static const BasicType::Kind kind[2][3] = { |
---|
61 | { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, |
---|
62 | { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt }, |
---|
63 | }; |
---|
64 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
65 | int size; // 0 => int, 1 => long, 2 => long long |
---|
66 | unsigned long long v; // converted integral value |
---|
67 | size_t last = str.length() - 1; // last character of constant |
---|
68 | |
---|
69 | if ( str[0] == '0' ) { // octal/hex constant ? |
---|
70 | dec = false; |
---|
71 | if ( last != 0 && checkX( str[1] ) ) { // hex constant ? |
---|
72 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
73 | //printf( "%llx %llu\n", v, v ); |
---|
74 | } else { // octal constant |
---|
75 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
76 | //printf( "%llo %llu\n", v, v ); |
---|
77 | } // if |
---|
78 | } else { // decimal constant ? |
---|
79 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
80 | //printf( "%llu %llu\n", v, v ); |
---|
81 | } // if |
---|
82 | |
---|
83 | if ( v <= INT_MAX ) { // signed int |
---|
84 | size = 0; |
---|
85 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
86 | size = 0; |
---|
87 | Unsigned = true; // unsigned |
---|
88 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
89 | size = 1; |
---|
90 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
91 | size = 1; |
---|
92 | Unsigned = true; // unsigned long int |
---|
93 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
94 | size = 2; |
---|
95 | } else { // unsigned long long int |
---|
96 | size = 2; |
---|
97 | Unsigned = true; // unsigned long long int |
---|
98 | } // if |
---|
99 | |
---|
100 | if ( checkU( str[last] ) ) { // suffix 'u' ? |
---|
101 | Unsigned = true; |
---|
102 | if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'l' ? |
---|
103 | size = 1; |
---|
104 | if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ? |
---|
105 | size = 2; |
---|
106 | } // if |
---|
107 | } // if |
---|
108 | } else if ( checkL( str[ last ] ) ) { // suffix 'l' ? |
---|
109 | size = 1; |
---|
110 | if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ? |
---|
111 | size = 2; |
---|
112 | if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
113 | Unsigned = true; |
---|
114 | } // if |
---|
115 | } else { |
---|
116 | if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
117 | Unsigned = true; |
---|
118 | } // if |
---|
119 | } // if |
---|
120 | } // if |
---|
121 | |
---|
122 | return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) ); |
---|
123 | } // build_constantInteger |
---|
124 | |
---|
125 | Expression *build_constantFloat( std::string & str ) { |
---|
126 | static const BasicType::Kind kind[2][3] = { |
---|
127 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
128 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
129 | }; |
---|
130 | |
---|
131 | bool complx = false; // real, complex |
---|
132 | int size = 1; // 0 => float, 1 => double (default), 2 => long double |
---|
133 | // floating-point constant has minimum of 2 characters: 1. or .1 |
---|
134 | size_t last = str.length() - 1; |
---|
135 | |
---|
136 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
137 | complx = true; |
---|
138 | last -= 1; // backup one character |
---|
139 | } // if |
---|
140 | |
---|
141 | if ( checkF( str[last] ) ) { // float ? |
---|
142 | size = 0; |
---|
143 | } else if ( checkD( str[last] ) ) { // double ? |
---|
144 | size = 1; |
---|
145 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
146 | size = 2; |
---|
147 | } // if |
---|
148 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
149 | complx = true; |
---|
150 | } // if |
---|
151 | |
---|
152 | return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) ); |
---|
153 | } // build_constantFloat |
---|
154 | |
---|
155 | Expression *build_constantChar( std::string & str ) { |
---|
156 | return new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) ); |
---|
157 | } // build_constantChar |
---|
158 | |
---|
159 | ConstantExpr *build_constantStr( std::string & str ) { |
---|
160 | // string should probably be a primitive type |
---|
161 | ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ), |
---|
162 | new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ), |
---|
163 | toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' |
---|
164 | false, false ); |
---|
165 | return new ConstantExpr( Constant( at, str ) ); |
---|
166 | } // build_constantStr |
---|
167 | |
---|
168 | //############################################################################## |
---|
169 | |
---|
170 | //Expression *build_varref( ExpressionNode expr ) { |
---|
171 | // return new NameExpr( get_name(), maybeBuild<Expression>( get_argName() ) ); |
---|
172 | //} |
---|
173 | |
---|
174 | // VarRefNode::VarRefNode( const string *name, bool labelp ) : ExpressionNode( nullptr, name ), isLabel( labelp ) {} |
---|
175 | |
---|
176 | // VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) {} |
---|
177 | |
---|
178 | // Expression *VarRefNode::build() const { |
---|
179 | // return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) ); |
---|
180 | // } |
---|
181 | |
---|
182 | // void VarRefNode::printOneLine( std::ostream &os, int indent ) const { |
---|
183 | // printDesignation( os ); |
---|
184 | // os << get_name() << ' '; |
---|
185 | // } |
---|
186 | |
---|
187 | // void VarRefNode::print( std::ostream &os, int indent ) const { |
---|
188 | // printDesignation( os ); |
---|
189 | // os << string( indent, ' ' ) << "Referencing: "; |
---|
190 | // os << "Variable: " << get_name(); |
---|
191 | // os << endl; |
---|
192 | // } |
---|
193 | |
---|
194 | NameExpr * build_varref( const string *name, bool labelp ) { |
---|
195 | return new NameExpr( *name, nullptr ); |
---|
196 | } |
---|
197 | |
---|
198 | //############################################################################## |
---|
199 | |
---|
200 | // DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex, bool isVarRef ) : isArrayIndex( isArrayIndex ) { |
---|
201 | // set_argName( expr ); |
---|
202 | // } |
---|
203 | |
---|
204 | // DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) {} |
---|
205 | |
---|
206 | // class DesignatorFixer : public Mutator { |
---|
207 | // public: |
---|
208 | // virtual Expression* mutate( NameExpr *nameExpr ) { |
---|
209 | // if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) { |
---|
210 | // Constant val( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nameExpr->get_name() ); |
---|
211 | // delete nameExpr; |
---|
212 | // return new ConstantExpr( val ); |
---|
213 | // } |
---|
214 | // return nameExpr; |
---|
215 | // } |
---|
216 | // }; |
---|
217 | |
---|
218 | // Expression *DesignatorNode::build() const { |
---|
219 | // return maybeBuild<Expression>(get_argName()); |
---|
220 | // } |
---|
221 | |
---|
222 | // void DesignatorNode::printOneLine( std::ostream &os, int indent ) const { |
---|
223 | // if ( get_argName() ) { |
---|
224 | // if ( isArrayIndex ) { |
---|
225 | // os << "["; |
---|
226 | // get_argName()->printOneLine( os, indent ); |
---|
227 | // os << "]"; |
---|
228 | // } else { |
---|
229 | // os << "."; |
---|
230 | // get_argName()->printOneLine( os, indent ); |
---|
231 | // } |
---|
232 | // } // if |
---|
233 | // } |
---|
234 | |
---|
235 | // void DesignatorNode::print( std::ostream &os, int indent ) const { |
---|
236 | // if ( get_argName() ) { |
---|
237 | // if ( isArrayIndex ) { |
---|
238 | // os << "["; |
---|
239 | // get_argName()->print( os, indent ); |
---|
240 | // os << "]"; |
---|
241 | // } else { |
---|
242 | // os << "."; |
---|
243 | // get_argName()->print( os, indent ); |
---|
244 | // } |
---|
245 | // } // if |
---|
246 | // } |
---|
247 | |
---|
248 | //############################################################################## |
---|
249 | |
---|
250 | static const char *OperName[] = { |
---|
251 | // diadic |
---|
252 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", |
---|
253 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
254 | "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
255 | "?[?]", "...", |
---|
256 | // monadic |
---|
257 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" |
---|
258 | }; |
---|
259 | |
---|
260 | //############################################################################## |
---|
261 | |
---|
262 | Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { |
---|
263 | Type *targetType = decl_node->buildType(); |
---|
264 | if ( dynamic_cast< VoidType * >( targetType ) ) { |
---|
265 | delete targetType; |
---|
266 | return new CastExpr( maybeBuild<Expression>(expr_node) ); |
---|
267 | } else { |
---|
268 | return new CastExpr( maybeBuild<Expression>(expr_node), targetType ); |
---|
269 | } // if |
---|
270 | } |
---|
271 | |
---|
272 | Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) { |
---|
273 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeBuild<Expression>(expr_node) ); |
---|
274 | delete member; |
---|
275 | return ret; |
---|
276 | } |
---|
277 | |
---|
278 | Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) { |
---|
279 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
280 | deref->get_args().push_back( maybeBuild<Expression>(expr_node) ); |
---|
281 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); |
---|
282 | delete member; |
---|
283 | return ret; |
---|
284 | } |
---|
285 | |
---|
286 | Expression *build_addressOf( ExpressionNode *expr_node ) { |
---|
287 | return new AddressExpr( maybeBuild<Expression>(expr_node) ); |
---|
288 | } |
---|
289 | Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { |
---|
290 | return new SizeofExpr( maybeBuild<Expression>(expr_node) ); |
---|
291 | } |
---|
292 | Expression *build_sizeOftype( DeclarationNode *decl_node ) { |
---|
293 | return new SizeofExpr( decl_node->buildType() ); |
---|
294 | } |
---|
295 | Expression *build_alignOfexpr( ExpressionNode *expr_node ) { |
---|
296 | return new AlignofExpr( maybeBuild<Expression>(expr_node) ); |
---|
297 | } |
---|
298 | Expression *build_alignOftype( DeclarationNode *decl_node ) { |
---|
299 | return new AlignofExpr( decl_node->buildType() ); |
---|
300 | } |
---|
301 | Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { |
---|
302 | return new UntypedOffsetofExpr( decl_node->buildType(), member->get_name() ); |
---|
303 | } |
---|
304 | |
---|
305 | Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) { |
---|
306 | return new LogicalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), notZeroExpr( maybeBuild<Expression>(expr_node2) ), kind ); |
---|
307 | } |
---|
308 | |
---|
309 | Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { |
---|
310 | std::list<Expression *> args; |
---|
311 | args.push_back( maybeBuild<Expression>(expr_node) ); |
---|
312 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
313 | } |
---|
314 | Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { |
---|
315 | std::list<Expression *> args; |
---|
316 | args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node) ) ); |
---|
317 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
318 | } |
---|
319 | Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
320 | std::list<Expression *> args; |
---|
321 | args.push_back( maybeBuild<Expression>(expr_node1) ); |
---|
322 | args.push_back( maybeBuild<Expression>(expr_node2) ); |
---|
323 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
324 | } |
---|
325 | Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
326 | std::list<Expression *> args; |
---|
327 | args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node1) ) ); |
---|
328 | args.push_back( maybeBuild<Expression>(expr_node2) ); |
---|
329 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
330 | } |
---|
331 | |
---|
332 | Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) { |
---|
333 | return new ConditionalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), maybeBuild<Expression>(expr_node2), maybeBuild<Expression>(expr_node3) ); |
---|
334 | } |
---|
335 | |
---|
336 | Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
337 | return new CommaExpr( maybeBuild<Expression>(expr_node1), maybeBuild<Expression>(expr_node2) ); |
---|
338 | } |
---|
339 | |
---|
340 | Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { |
---|
341 | return new AttrExpr( var, maybeBuild<Expression>(expr_node) ); |
---|
342 | } |
---|
343 | Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { |
---|
344 | return new AttrExpr( var, decl_node->buildType() ); |
---|
345 | } |
---|
346 | |
---|
347 | Expression *build_tuple( ExpressionNode * expr_node ) { |
---|
348 | TupleExpr *ret = new TupleExpr(); |
---|
349 | buildList( expr_node, ret->get_exprs() ); |
---|
350 | return ret; |
---|
351 | } |
---|
352 | |
---|
353 | Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
354 | std::list<Expression *> args; |
---|
355 | |
---|
356 | buildList( expr_node, args ); |
---|
357 | return new UntypedExpr( maybeBuild<Expression>(function), args, nullptr ); |
---|
358 | } |
---|
359 | |
---|
360 | Expression *build_range( ExpressionNode * low, ExpressionNode *high ) { |
---|
361 | Expression *low_cexpr = maybeBuild<Expression>( low ); |
---|
362 | Expression *high_cexpr = maybeBuild<Expression>( high ); |
---|
363 | return new RangeExpr( low_cexpr, high_cexpr ); |
---|
364 | } |
---|
365 | |
---|
366 | //############################################################################## |
---|
367 | |
---|
368 | // Expression *AsmExprNode::build() const { |
---|
369 | // return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)maybeBuild<Expression>(constraint), maybeBuild<Expression>(operand) ); |
---|
370 | // } |
---|
371 | |
---|
372 | // void AsmExprNode::print( std::ostream &os, int indent ) const { |
---|
373 | // os << string( indent, ' ' ) << "Assembler Expression:" << endl; |
---|
374 | // if ( inout ) { |
---|
375 | // os << string( indent, ' ' ) << "inout: " << std::endl; |
---|
376 | // inout->print( os, indent + 2 ); |
---|
377 | // } // if |
---|
378 | // if ( constraint ) { |
---|
379 | // os << string( indent, ' ' ) << "constraint: " << std::endl; |
---|
380 | // constraint->print( os, indent + 2 ); |
---|
381 | // } // if |
---|
382 | // if ( operand ) { |
---|
383 | // os << string( indent, ' ' ) << "operand: " << std::endl; |
---|
384 | // operand->print( os, indent + 2 ); |
---|
385 | // } // if |
---|
386 | // } |
---|
387 | |
---|
388 | // void AsmExprNode::printOneLine( std::ostream &os, int indent ) const { |
---|
389 | // printDesignation( os ); |
---|
390 | // os << "( "; |
---|
391 | // if ( inout ) inout->printOneLine( os, indent + 2 ); |
---|
392 | // os << ", "; |
---|
393 | // if ( constraint ) constraint->printOneLine( os, indent + 2 ); |
---|
394 | // os << ", "; |
---|
395 | // if ( operand ) operand->printOneLine( os, indent + 2 ); |
---|
396 | // os << ") "; |
---|
397 | // } |
---|
398 | |
---|
399 | Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { |
---|
400 | return new AsmExpr( maybeBuild< Expression >( inout ), constraint, maybeBuild<Expression>(operand) ); |
---|
401 | } |
---|
402 | |
---|
403 | //############################################################################## |
---|
404 | |
---|
405 | void LabelNode::print( std::ostream &os, int indent ) const {} |
---|
406 | |
---|
407 | void LabelNode::printOneLine( std::ostream &os, int indent ) const {} |
---|
408 | |
---|
409 | //############################################################################## |
---|
410 | |
---|
411 | // ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} |
---|
412 | |
---|
413 | // ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) { |
---|
414 | // } |
---|
415 | |
---|
416 | // ValofExprNode::~ValofExprNode() { |
---|
417 | // delete body; |
---|
418 | // } |
---|
419 | |
---|
420 | // void ValofExprNode::print( std::ostream &os, int indent ) const { |
---|
421 | // os << string( indent, ' ' ) << "Valof Expression:" << std::endl; |
---|
422 | // get_body()->print( os, indent + 4); |
---|
423 | // } |
---|
424 | |
---|
425 | // void ValofExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
426 | // assert( false ); |
---|
427 | // } |
---|
428 | |
---|
429 | // Expression *ValofExprNode::build() const { |
---|
430 | // return new UntypedValofExpr( maybeBuild<Statement>(get_body()), nullptr ); |
---|
431 | // } |
---|
432 | |
---|
433 | Expression *build_valexpr( StatementNode *s ) { |
---|
434 | return new UntypedValofExpr( maybeBuild<Statement>(s), nullptr ); |
---|
435 | } |
---|
436 | |
---|
437 | //############################################################################## |
---|
438 | |
---|
439 | ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { |
---|
440 | if ( init_ == 0 ) |
---|
441 | init = 0; |
---|
442 | else { |
---|
443 | DeclarationNode *decl; |
---|
444 | ExpressionNode *exp; |
---|
445 | |
---|
446 | if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) |
---|
447 | init = new StatementNode( decl ); |
---|
448 | else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) |
---|
449 | init = new StatementNode( StatementNode::Exp, exp ); |
---|
450 | else |
---|
451 | throw SemanticError("Error in for control expression"); |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other ) |
---|
456 | : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) { |
---|
457 | } |
---|
458 | |
---|
459 | ForCtlExprNode::~ForCtlExprNode() { |
---|
460 | delete init; |
---|
461 | delete condition; |
---|
462 | delete change; |
---|
463 | } |
---|
464 | |
---|
465 | Expression *ForCtlExprNode::build() const { |
---|
466 | // this shouldn't be used! |
---|
467 | assert( false ); |
---|
468 | return 0; |
---|
469 | } |
---|
470 | |
---|
471 | void ForCtlExprNode::print( std::ostream &os, int indent ) const{ |
---|
472 | os << string( indent,' ' ) << "For Control Expression -- :" << endl; |
---|
473 | |
---|
474 | os << string( indent + 2, ' ' ) << "initialization:" << endl; |
---|
475 | if ( init != 0 ) |
---|
476 | init->printList( os, indent + 4 ); |
---|
477 | |
---|
478 | os << string( indent + 2, ' ' ) << "condition: " << endl; |
---|
479 | if ( condition != 0 ) |
---|
480 | condition->print( os, indent + 4 ); |
---|
481 | os << string( indent + 2, ' ' ) << "increment: " << endl; |
---|
482 | if ( change != 0 ) |
---|
483 | change->print( os, indent + 4 ); |
---|
484 | } |
---|
485 | |
---|
486 | void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
487 | assert( false ); |
---|
488 | } |
---|
489 | |
---|
490 | //############################################################################## |
---|
491 | |
---|
492 | // TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { |
---|
493 | // } |
---|
494 | |
---|
495 | // TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { |
---|
496 | // } |
---|
497 | |
---|
498 | // Expression *TypeValueNode::build() const { |
---|
499 | // return new TypeExpr( decl->buildType() ); |
---|
500 | // } |
---|
501 | |
---|
502 | // void TypeValueNode::print( std::ostream &os, int indent ) const { |
---|
503 | // os << std::string( indent, ' ' ) << "Type:"; |
---|
504 | // get_decl()->print( os, indent + 2); |
---|
505 | // } |
---|
506 | |
---|
507 | // void TypeValueNode::printOneLine( std::ostream &os, int indent ) const { |
---|
508 | // os << "Type:"; |
---|
509 | // get_decl()->print( os, indent + 2); |
---|
510 | // } |
---|
511 | |
---|
512 | Expression *build_typevalue( DeclarationNode *decl ) { |
---|
513 | return new TypeExpr( decl->buildType() ); |
---|
514 | } |
---|
515 | |
---|
516 | //############################################################################## |
---|
517 | |
---|
518 | // CompoundLiteralNode::CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids ) : type( type ), kids( kids ) {} |
---|
519 | // CompoundLiteralNode::CompoundLiteralNode( const CompoundLiteralNode &other ) : ExpressionNode( other ), type( other.type ), kids( other.kids ) {} |
---|
520 | |
---|
521 | // CompoundLiteralNode::~CompoundLiteralNode() { |
---|
522 | // delete kids; |
---|
523 | // delete type; |
---|
524 | // } |
---|
525 | |
---|
526 | // CompoundLiteralNode *CompoundLiteralNode::clone() const { |
---|
527 | // return new CompoundLiteralNode( *this ); |
---|
528 | // } |
---|
529 | |
---|
530 | // void CompoundLiteralNode::print( std::ostream &os, int indent ) const { |
---|
531 | // os << string( indent,' ' ) << "CompoundLiteralNode:" << endl; |
---|
532 | |
---|
533 | // os << string( indent + 2, ' ' ) << "type:" << endl; |
---|
534 | // if ( type != 0 ) |
---|
535 | // type->print( os, indent + 4 ); |
---|
536 | |
---|
537 | // os << string( indent + 2, ' ' ) << "initialization:" << endl; |
---|
538 | // if ( kids != 0 ) |
---|
539 | // kids->printList( os, indent + 4 ); |
---|
540 | // } |
---|
541 | |
---|
542 | // void CompoundLiteralNode::printOneLine( std::ostream &os, int indent ) const { |
---|
543 | // os << "( "; |
---|
544 | // if ( type ) type->print( os ); |
---|
545 | // os << ", "; |
---|
546 | // if ( kids ) kids->printOneLine( os ); |
---|
547 | // os << ") "; |
---|
548 | // } |
---|
549 | |
---|
550 | // Expression *CompoundLiteralNode::build() const { |
---|
551 | // Declaration * newDecl = maybeBuild<Declaration>(type); // compound literal type |
---|
552 | // if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
553 | // return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeBuild<Initializer>(kids) ); |
---|
554 | // // these types do not have associated type information |
---|
555 | // } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
556 | // return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
557 | // } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
558 | // return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
559 | // } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
560 | // return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
561 | // } else { |
---|
562 | // assert( false ); |
---|
563 | // } // if |
---|
564 | // } |
---|
565 | |
---|
566 | Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { |
---|
567 | Declaration * newDecl = maybeBuild<Declaration>(decl_node); // compound literal type |
---|
568 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
569 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeBuild<Initializer>(kids) ); |
---|
570 | // these types do not have associated type information |
---|
571 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
572 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
573 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
574 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
575 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
576 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeBuild<Initializer>(kids) ); |
---|
577 | } else { |
---|
578 | assert( false ); |
---|
579 | } // if |
---|
580 | } |
---|
581 | |
---|
582 | // Local Variables: // |
---|
583 | // tab-width: 4 // |
---|
584 | // mode: c++ // |
---|
585 | // compile-command: "make install" // |
---|
586 | // End: // |
---|