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 : Fri Apr 8 15:43:05 2016 |
---|
13 | // Update Count : 296 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | #include <cctype> |
---|
18 | #include <algorithm> |
---|
19 | #include <sstream> |
---|
20 | #include <cstdio> |
---|
21 | #include <climits> |
---|
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() : ParseNode(), argName( 0 ) {} |
---|
35 | |
---|
36 | ExpressionNode::ExpressionNode( const string *name ) : ParseNode( name ), argName( 0 ) {} |
---|
37 | |
---|
38 | ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) { |
---|
39 | if ( other.argName ) { |
---|
40 | argName = other.argName->clone(); |
---|
41 | } else { |
---|
42 | argName = 0; |
---|
43 | } // if |
---|
44 | } |
---|
45 | |
---|
46 | ExpressionNode * ExpressionNode::set_argName( const std::string *aName ) { |
---|
47 | argName = new VarRefNode( aName ); |
---|
48 | return this; |
---|
49 | } |
---|
50 | |
---|
51 | ExpressionNode * ExpressionNode::set_argName( ExpressionNode *aDesignator ) { |
---|
52 | argName = aDesignator; |
---|
53 | return this; |
---|
54 | } |
---|
55 | |
---|
56 | void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { |
---|
57 | if ( argName ) { |
---|
58 | os << string( indent, ' ' ) << "(designated by: "; |
---|
59 | argName->printOneLine( os, indent ); |
---|
60 | os << ")" << std::endl; |
---|
61 | } // if |
---|
62 | } |
---|
63 | |
---|
64 | //############################################################################## |
---|
65 | |
---|
66 | NullExprNode::NullExprNode() {} |
---|
67 | |
---|
68 | NullExprNode *NullExprNode::clone() const { |
---|
69 | return new NullExprNode(); |
---|
70 | } |
---|
71 | |
---|
72 | void NullExprNode::print( std::ostream & os, int indent ) const { |
---|
73 | printDesignation( os ); |
---|
74 | os << "null expression"; |
---|
75 | } |
---|
76 | |
---|
77 | void NullExprNode::printOneLine( std::ostream & os, int indent ) const { |
---|
78 | printDesignation( os ); |
---|
79 | os << "null"; |
---|
80 | } |
---|
81 | |
---|
82 | Expression *NullExprNode::build() const { |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ) { |
---|
87 | return new CommaExprNode( this, exp ); |
---|
88 | } |
---|
89 | |
---|
90 | //############################################################################## |
---|
91 | |
---|
92 | static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
93 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
94 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
95 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
96 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } |
---|
97 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } |
---|
98 | |
---|
99 | // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: |
---|
100 | // |
---|
101 | // prefix action constant action suffix |
---|
102 | // |
---|
103 | // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: |
---|
104 | // |
---|
105 | // constant BEGIN CONT ... |
---|
106 | // <CONT>(...)? BEGIN 0 ... // possible empty suffix |
---|
107 | // |
---|
108 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their |
---|
109 | // type. |
---|
110 | |
---|
111 | ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), value( *inVal ) { |
---|
112 | // lexing divides constants into 4 kinds |
---|
113 | switch ( type ) { |
---|
114 | case Integer: |
---|
115 | { |
---|
116 | static const BasicType::Kind kind[2][3] = { |
---|
117 | { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, |
---|
118 | { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt }, |
---|
119 | }; |
---|
120 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
121 | int size; // 0 => int, 1 => long, 2 => long long |
---|
122 | unsigned long long v; // converted integral value |
---|
123 | size_t last = value.length() - 1; // last character of constant |
---|
124 | |
---|
125 | if ( value[0] == '0' ) { // octal constant ? |
---|
126 | dec = false; |
---|
127 | if ( last != 0 && checkX( value[1] ) ) { // hex constant ? |
---|
128 | sscanf( (char *)value.c_str(), "%llx", &v ); |
---|
129 | //printf( "%llx %llu\n", v, v ); |
---|
130 | } else { |
---|
131 | sscanf( (char *)value.c_str(), "%llo", &v ); |
---|
132 | //printf( "%llo %llu\n", v, v ); |
---|
133 | } // if |
---|
134 | } else { // decimal constant ? |
---|
135 | sscanf( (char *)value.c_str(), "%llu", &v ); |
---|
136 | //printf( "%llu %llu\n", v, v ); |
---|
137 | } // if |
---|
138 | |
---|
139 | if ( v <= INT_MAX ) { // signed int |
---|
140 | size = 0; |
---|
141 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
142 | size = 0; |
---|
143 | Unsigned = true; // unsigned |
---|
144 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
145 | size = 1; |
---|
146 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
147 | size = 1; |
---|
148 | Unsigned = true; // unsigned long int |
---|
149 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
150 | size = 2; |
---|
151 | } else { // unsigned long long int |
---|
152 | size = 2; |
---|
153 | Unsigned = true; // unsigned long long int |
---|
154 | } // if |
---|
155 | |
---|
156 | if ( checkU( value[last] ) ) { // suffix 'u' ? |
---|
157 | Unsigned = true; |
---|
158 | if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'l' ? |
---|
159 | size = 1; |
---|
160 | if ( last > 1 && checkL( value[ last - 2 ] ) ) { // suffix 'll' ? |
---|
161 | size = 2; |
---|
162 | } // if |
---|
163 | } // if |
---|
164 | } else if ( checkL( value[ last ] ) ) { // suffix 'l' ? |
---|
165 | size = 1; |
---|
166 | if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'll' ? |
---|
167 | size = 2; |
---|
168 | if ( last > 1 && checkU( value[ last - 2 ] ) ) { // suffix 'u' ? |
---|
169 | Unsigned = true; |
---|
170 | } // if |
---|
171 | } else { |
---|
172 | if ( last > 0 && checkU( value[ last - 1 ] ) ) { // suffix 'u' ? |
---|
173 | Unsigned = true; |
---|
174 | } // if |
---|
175 | } // if |
---|
176 | } // if |
---|
177 | btype = kind[Unsigned][size]; // lookup constant type |
---|
178 | break; |
---|
179 | } |
---|
180 | case Float: |
---|
181 | { |
---|
182 | static const BasicType::Kind kind[2][3] = { |
---|
183 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
184 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
185 | }; |
---|
186 | bool complx = false; // real, complex |
---|
187 | int size = 1; // 0 => float, 1 => double (default), 2 => long double |
---|
188 | // floating-point constant has minimum of 2 characters: 1. or .1 |
---|
189 | size_t last = value.length() - 1; |
---|
190 | |
---|
191 | if ( checkI( value[last] ) ) { // imaginary ? |
---|
192 | complx = true; |
---|
193 | last -= 1; // backup one character |
---|
194 | } // if |
---|
195 | if ( checkF( value[last] ) ) { // float ? |
---|
196 | size = 0; |
---|
197 | } else if ( checkD( value[last] ) ) { // double ? |
---|
198 | size = 1; |
---|
199 | } else if ( checkL( value[last] ) ) { // long double ? |
---|
200 | size = 2; |
---|
201 | } // if |
---|
202 | if ( ! complx && checkI( value[last - 1] ) ) { // imaginary ? |
---|
203 | complx = true; |
---|
204 | } // if |
---|
205 | btype = kind[complx][size]; // lookup constant type |
---|
206 | break; |
---|
207 | } |
---|
208 | case Character: |
---|
209 | btype = BasicType::Char; // default |
---|
210 | if ( string( "LUu" ).find( value[0] ) != string::npos ) { |
---|
211 | // ??? |
---|
212 | } // if |
---|
213 | break; |
---|
214 | case String: |
---|
215 | // array of char |
---|
216 | if ( string( "LUu" ).find( value[0] ) != string::npos ) { |
---|
217 | if ( value[0] == 'u' && value[1] == '8' ) { |
---|
218 | // ??? |
---|
219 | } else { |
---|
220 | // ??? |
---|
221 | } // if |
---|
222 | } // if |
---|
223 | break; |
---|
224 | } // switch |
---|
225 | } // ConstantNode::ConstantNode |
---|
226 | |
---|
227 | ConstantNode *ConstantNode::appendstr( const std::string *newValue ) { |
---|
228 | assert( newValue != 0 ); |
---|
229 | assert( type == String ); |
---|
230 | |
---|
231 | // "abc" "def" "ghi" => "abcdefghi", remove new text from quotes and insert before last quote in old string. |
---|
232 | value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) ); |
---|
233 | |
---|
234 | delete newValue; // allocated by lexer |
---|
235 | return this; |
---|
236 | } |
---|
237 | |
---|
238 | void ConstantNode::printOneLine( std::ostream &os, int indent ) const { |
---|
239 | os << string( indent, ' ' ); |
---|
240 | printDesignation( os ); |
---|
241 | |
---|
242 | switch ( type ) { |
---|
243 | case Integer: |
---|
244 | case Float: |
---|
245 | os << value ; |
---|
246 | break; |
---|
247 | case Character: |
---|
248 | os << "'" << value << "'"; |
---|
249 | break; |
---|
250 | case String: |
---|
251 | os << '"' << value << '"'; |
---|
252 | break; |
---|
253 | } // switch |
---|
254 | |
---|
255 | os << ' '; |
---|
256 | } |
---|
257 | |
---|
258 | void ConstantNode::print( std::ostream &os, int indent ) const { |
---|
259 | printOneLine( os, indent ); |
---|
260 | os << endl; |
---|
261 | } |
---|
262 | |
---|
263 | Expression *ConstantNode::build() const { |
---|
264 | ::Type::Qualifiers q; // no qualifiers on constants |
---|
265 | |
---|
266 | switch ( get_type() ) { |
---|
267 | case String: |
---|
268 | { |
---|
269 | // string should probably be a primitive type |
---|
270 | ArrayType *at = new ArrayType( q, new BasicType( q, BasicType::Char ), |
---|
271 | new ConstantExpr( |
---|
272 | Constant( new BasicType( q, BasicType::UnsignedInt ), |
---|
273 | toString( value.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' |
---|
274 | false, false ); |
---|
275 | return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) ); |
---|
276 | } |
---|
277 | default: |
---|
278 | return new ConstantExpr( Constant( new BasicType( q, btype ), get_value() ), maybeBuild< Expression >( get_argName() ) ); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | //############################################################################## |
---|
283 | |
---|
284 | VarRefNode::VarRefNode() : isLabel( false ) {} |
---|
285 | |
---|
286 | VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {} |
---|
287 | |
---|
288 | VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { |
---|
289 | } |
---|
290 | |
---|
291 | Expression *VarRefNode::build() const { |
---|
292 | return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) ); |
---|
293 | } |
---|
294 | |
---|
295 | void VarRefNode::printOneLine( std::ostream &os, int indent ) const { |
---|
296 | printDesignation( os ); |
---|
297 | os << get_name() << ' '; |
---|
298 | } |
---|
299 | |
---|
300 | void VarRefNode::print( std::ostream &os, int indent ) const { |
---|
301 | printDesignation( os ); |
---|
302 | os << string( indent, ' ' ) << "Referencing: "; |
---|
303 | os << "Variable: " << get_name(); |
---|
304 | os << endl; |
---|
305 | } |
---|
306 | |
---|
307 | //############################################################################## |
---|
308 | |
---|
309 | DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex ) : isArrayIndex( isArrayIndex ) { |
---|
310 | set_argName( expr ); |
---|
311 | assert( get_argName() ); |
---|
312 | |
---|
313 | if ( ! isArrayIndex ) { |
---|
314 | if ( VarRefNode * var = dynamic_cast< VarRefNode * >( expr ) ) { |
---|
315 | |
---|
316 | stringstream ss( var->get_name() ); |
---|
317 | double value; |
---|
318 | if ( ss >> value ) { |
---|
319 | // this is a floating point constant. It MUST be |
---|
320 | // ".0" or ".1", otherwise the program is invalid |
---|
321 | if ( ! (var->get_name() == ".0" || var->get_name() == ".1") ) { |
---|
322 | throw SemanticError( "invalid designator name: " + var->get_name() ); |
---|
323 | } // if |
---|
324 | var->set_name( var->get_name().substr(1) ); |
---|
325 | } // if |
---|
326 | } // if |
---|
327 | } // if |
---|
328 | } |
---|
329 | |
---|
330 | DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) { |
---|
331 | } |
---|
332 | |
---|
333 | class DesignatorFixer : public Mutator { |
---|
334 | public: |
---|
335 | virtual Expression* mutate( NameExpr *nameExpr ) { |
---|
336 | if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) { |
---|
337 | Constant val( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nameExpr->get_name() ); |
---|
338 | delete nameExpr; |
---|
339 | return new ConstantExpr( val ); |
---|
340 | } |
---|
341 | return nameExpr; |
---|
342 | } |
---|
343 | }; |
---|
344 | |
---|
345 | Expression *DesignatorNode::build() const { |
---|
346 | Expression * ret = get_argName()->build(); |
---|
347 | |
---|
348 | if ( isArrayIndex ) { |
---|
349 | // need to traverse entire structure and change any instances of 0 or 1 to |
---|
350 | // ConstantExpr |
---|
351 | DesignatorFixer fixer; |
---|
352 | ret = ret->acceptMutator( fixer ); |
---|
353 | } // if |
---|
354 | |
---|
355 | return ret; |
---|
356 | } |
---|
357 | |
---|
358 | void DesignatorNode::printOneLine( std::ostream &os, int indent ) const { |
---|
359 | if ( get_argName() ) { |
---|
360 | if ( isArrayIndex ) { |
---|
361 | os << "["; |
---|
362 | get_argName()->printOneLine( os, indent ); |
---|
363 | os << "]"; |
---|
364 | } else { |
---|
365 | os << "."; |
---|
366 | get_argName()->printOneLine( os, indent ); |
---|
367 | } |
---|
368 | } // if |
---|
369 | } |
---|
370 | |
---|
371 | void DesignatorNode::print( std::ostream &os, int indent ) const { |
---|
372 | if ( get_argName() ) { |
---|
373 | if ( isArrayIndex ) { |
---|
374 | os << "["; |
---|
375 | get_argName()->print( os, indent ); |
---|
376 | os << "]"; |
---|
377 | } else { |
---|
378 | os << "."; |
---|
379 | get_argName()->print( os, indent ); |
---|
380 | } |
---|
381 | } // if |
---|
382 | } |
---|
383 | |
---|
384 | //############################################################################## |
---|
385 | |
---|
386 | static const char *opName[] = { |
---|
387 | "TupleC", "Comma", "TupleFieldSel", // "TuplePFieldSel", // n-adic |
---|
388 | // triadic |
---|
389 | "Cond", "NCond", |
---|
390 | // diadic |
---|
391 | "SizeOf", "AlignOf", "OffsetOf", "Attr", "CompLit", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", |
---|
392 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
393 | "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
394 | "?[?]", "FieldSel", "PFieldSel", "Range", |
---|
395 | // monadic |
---|
396 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" |
---|
397 | }; |
---|
398 | |
---|
399 | OperatorNode::OperatorNode( Type t ) : type( t ) {} |
---|
400 | |
---|
401 | OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) { |
---|
402 | } |
---|
403 | |
---|
404 | OperatorNode::~OperatorNode() {} |
---|
405 | |
---|
406 | OperatorNode::Type OperatorNode::get_type( void ) const{ |
---|
407 | return type; |
---|
408 | } |
---|
409 | |
---|
410 | void OperatorNode::printOneLine( std::ostream &os, int indent ) const { |
---|
411 | printDesignation( os ); |
---|
412 | os << opName[ type ] << ' '; |
---|
413 | } |
---|
414 | |
---|
415 | void OperatorNode::print( std::ostream &os, int indent ) const{ |
---|
416 | printDesignation( os ); |
---|
417 | os << string( indent, ' ' ) << "Operator: " << opName[type] << endl; |
---|
418 | return; |
---|
419 | } |
---|
420 | |
---|
421 | const char *OperatorNode::get_typename( void ) const{ |
---|
422 | return opName[ type ]; |
---|
423 | } |
---|
424 | |
---|
425 | //############################################################################## |
---|
426 | |
---|
427 | CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) { |
---|
428 | } |
---|
429 | |
---|
430 | CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) { |
---|
431 | } |
---|
432 | |
---|
433 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ): |
---|
434 | function( f ), arguments( args ) { |
---|
435 | } |
---|
436 | |
---|
437 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2): |
---|
438 | function( f ), arguments( arg1 ) { |
---|
439 | arguments->set_link( arg2 ); |
---|
440 | } |
---|
441 | |
---|
442 | CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ) { |
---|
443 | ParseNode *cur = other.arguments; |
---|
444 | while ( cur ) { |
---|
445 | if ( arguments ) { |
---|
446 | arguments->set_link( cur->clone() ); |
---|
447 | } else { |
---|
448 | arguments = ( ExpressionNode*)cur->clone(); |
---|
449 | } // if |
---|
450 | cur = cur->get_link(); |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | CompositeExprNode::~CompositeExprNode() { |
---|
455 | delete function; |
---|
456 | delete arguments; |
---|
457 | } |
---|
458 | |
---|
459 | #include "Common/utility.h" |
---|
460 | |
---|
461 | Expression *CompositeExprNode::build() const { |
---|
462 | OperatorNode *op; |
---|
463 | std::list<Expression *> args; |
---|
464 | |
---|
465 | buildList( get_args(), args ); |
---|
466 | |
---|
467 | if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator |
---|
468 | return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() )); |
---|
469 | } // if |
---|
470 | |
---|
471 | switch ( op->get_type()) { |
---|
472 | case OperatorNode::Incr: |
---|
473 | case OperatorNode::Decr: |
---|
474 | case OperatorNode::IncrPost: |
---|
475 | case OperatorNode::DecrPost: |
---|
476 | case OperatorNode::Assign: |
---|
477 | case OperatorNode::MulAssn: |
---|
478 | case OperatorNode::DivAssn: |
---|
479 | case OperatorNode::ModAssn: |
---|
480 | case OperatorNode::PlusAssn: |
---|
481 | case OperatorNode::MinusAssn: |
---|
482 | case OperatorNode::LSAssn: |
---|
483 | case OperatorNode::RSAssn: |
---|
484 | case OperatorNode::AndAssn: |
---|
485 | case OperatorNode::ERAssn: |
---|
486 | case OperatorNode::OrAssn: |
---|
487 | // the rewrite rules for these expressions specify that the first argument has its address taken |
---|
488 | assert( ! args.empty() ); |
---|
489 | args.front() = new AddressExpr( args.front() ); |
---|
490 | break; |
---|
491 | default: // do nothing |
---|
492 | ; |
---|
493 | } // switch |
---|
494 | |
---|
495 | switch ( op->get_type() ) { |
---|
496 | case OperatorNode::Incr: |
---|
497 | case OperatorNode::Decr: |
---|
498 | case OperatorNode::IncrPost: |
---|
499 | case OperatorNode::DecrPost: |
---|
500 | case OperatorNode::Assign: |
---|
501 | case OperatorNode::MulAssn: |
---|
502 | case OperatorNode::DivAssn: |
---|
503 | case OperatorNode::ModAssn: |
---|
504 | case OperatorNode::PlusAssn: |
---|
505 | case OperatorNode::MinusAssn: |
---|
506 | case OperatorNode::LSAssn: |
---|
507 | case OperatorNode::RSAssn: |
---|
508 | case OperatorNode::AndAssn: |
---|
509 | case OperatorNode::ERAssn: |
---|
510 | case OperatorNode::OrAssn: |
---|
511 | case OperatorNode::Plus: |
---|
512 | case OperatorNode::Minus: |
---|
513 | case OperatorNode::Mul: |
---|
514 | case OperatorNode::Div: |
---|
515 | case OperatorNode::Mod: |
---|
516 | case OperatorNode::BitOr: |
---|
517 | case OperatorNode::BitAnd: |
---|
518 | case OperatorNode::Xor: |
---|
519 | case OperatorNode::LShift: |
---|
520 | case OperatorNode::RShift: |
---|
521 | case OperatorNode::LThan: |
---|
522 | case OperatorNode::GThan: |
---|
523 | case OperatorNode::LEThan: |
---|
524 | case OperatorNode::GEThan: |
---|
525 | case OperatorNode::Eq: |
---|
526 | case OperatorNode::Neq: |
---|
527 | case OperatorNode::Index: |
---|
528 | case OperatorNode::Range: |
---|
529 | case OperatorNode::UnPlus: |
---|
530 | case OperatorNode::UnMinus: |
---|
531 | case OperatorNode::PointTo: |
---|
532 | case OperatorNode::Neg: |
---|
533 | case OperatorNode::BitNeg: |
---|
534 | case OperatorNode::LabelAddress: |
---|
535 | return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args ); |
---|
536 | case OperatorNode::AddressOf: |
---|
537 | assert( args.size() == 1 ); |
---|
538 | assert( args.front() ); |
---|
539 | |
---|
540 | return new AddressExpr( args.front() ); |
---|
541 | case OperatorNode::Cast: |
---|
542 | { |
---|
543 | TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()); |
---|
544 | assert( arg ); |
---|
545 | |
---|
546 | DeclarationNode *decl_node = arg->get_decl(); |
---|
547 | ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link()); |
---|
548 | |
---|
549 | Type *targetType = decl_node->buildType(); |
---|
550 | if ( dynamic_cast< VoidType* >( targetType ) ) { |
---|
551 | delete targetType; |
---|
552 | return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) ); |
---|
553 | } else { |
---|
554 | return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) ); |
---|
555 | } // if |
---|
556 | } |
---|
557 | case OperatorNode::FieldSel: |
---|
558 | { |
---|
559 | assert( args.size() == 2 ); |
---|
560 | |
---|
561 | NameExpr *member = dynamic_cast<NameExpr *>( args.back()); |
---|
562 | // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back()); |
---|
563 | |
---|
564 | if ( member != 0 ) { |
---|
565 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front()); |
---|
566 | delete member; |
---|
567 | return ret; |
---|
568 | /* else if ( memberTup != 0 ) |
---|
569 | { |
---|
570 | UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front()); |
---|
571 | delete member; |
---|
572 | return ret; |
---|
573 | } */ |
---|
574 | } else |
---|
575 | assert( false ); |
---|
576 | } |
---|
577 | case OperatorNode::PFieldSel: |
---|
578 | { |
---|
579 | assert( args.size() == 2 ); |
---|
580 | |
---|
581 | NameExpr *member = dynamic_cast<NameExpr *>( args.back()); // modify for Tuples xxx |
---|
582 | assert( member != 0 ); |
---|
583 | |
---|
584 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
585 | deref->get_args().push_back( args.front() ); |
---|
586 | |
---|
587 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); |
---|
588 | delete member; |
---|
589 | return ret; |
---|
590 | } |
---|
591 | case OperatorNode::SizeOf: |
---|
592 | { |
---|
593 | if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) { |
---|
594 | return new SizeofExpr( arg->get_decl()->buildType()); |
---|
595 | } else { |
---|
596 | return new SizeofExpr( args.front()); |
---|
597 | } // if |
---|
598 | } |
---|
599 | case OperatorNode::AlignOf: |
---|
600 | { |
---|
601 | if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) { |
---|
602 | return new AlignofExpr( arg->get_decl()->buildType()); |
---|
603 | } else { |
---|
604 | return new AlignofExpr( args.front()); |
---|
605 | } // if |
---|
606 | } |
---|
607 | case OperatorNode::OffsetOf: |
---|
608 | { |
---|
609 | assert( args.size() == 2 ); |
---|
610 | |
---|
611 | if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args() ) ) { |
---|
612 | NameExpr *member = dynamic_cast<NameExpr *>( args.back() ); |
---|
613 | assert( member != 0 ); |
---|
614 | |
---|
615 | return new UntypedOffsetofExpr( arg->get_decl()->buildType(), member->get_name() ); |
---|
616 | } else assert( false ); |
---|
617 | } |
---|
618 | case OperatorNode::Attr: |
---|
619 | { |
---|
620 | VarRefNode *var = dynamic_cast<VarRefNode *>( get_args()); |
---|
621 | assert( var ); |
---|
622 | if ( ! get_args()->get_link() ) { |
---|
623 | return new AttrExpr( var->build(), ( Expression*)0); |
---|
624 | } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) { |
---|
625 | return new AttrExpr( var->build(), arg->get_decl()->buildType()); |
---|
626 | } else { |
---|
627 | return new AttrExpr( var->build(), args.back()); |
---|
628 | } // if |
---|
629 | } |
---|
630 | case OperatorNode::CompLit: |
---|
631 | throw UnimplementedError( "C99 compound literals" ); |
---|
632 | // the short-circuited operators |
---|
633 | case OperatorNode::Or: |
---|
634 | case OperatorNode::And: |
---|
635 | assert( args.size() == 2); |
---|
636 | return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) ); |
---|
637 | case OperatorNode::Cond: |
---|
638 | { |
---|
639 | assert( args.size() == 3); |
---|
640 | std::list< Expression * >::const_iterator i = args.begin(); |
---|
641 | Expression *arg1 = notZeroExpr( *i++ ); |
---|
642 | Expression *arg2 = *i++; |
---|
643 | Expression *arg3 = *i++; |
---|
644 | return new ConditionalExpr( arg1, arg2, arg3 ); |
---|
645 | } |
---|
646 | case OperatorNode::NCond: |
---|
647 | throw UnimplementedError( "GNU 2-argument conditional expression" ); |
---|
648 | case OperatorNode::Comma: |
---|
649 | { |
---|
650 | assert( args.size() == 2); |
---|
651 | std::list< Expression * >::const_iterator i = args.begin(); |
---|
652 | Expression *ret = *i++; |
---|
653 | while ( i != args.end() ) { |
---|
654 | ret = new CommaExpr( ret, *i++ ); |
---|
655 | } |
---|
656 | return ret; |
---|
657 | } |
---|
658 | // Tuples |
---|
659 | case OperatorNode::TupleC: |
---|
660 | { |
---|
661 | TupleExpr *ret = new TupleExpr(); |
---|
662 | std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) ); |
---|
663 | return ret; |
---|
664 | } |
---|
665 | default: |
---|
666 | // shouldn't happen |
---|
667 | assert( false ); |
---|
668 | return 0; |
---|
669 | } // switch |
---|
670 | } |
---|
671 | |
---|
672 | void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const { |
---|
673 | printDesignation( os ); |
---|
674 | os << "( "; |
---|
675 | function->printOneLine( os, indent ); |
---|
676 | for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) { |
---|
677 | cur->printOneLine( os, indent ); |
---|
678 | } // for |
---|
679 | os << ") "; |
---|
680 | } |
---|
681 | |
---|
682 | void CompositeExprNode::print( std::ostream &os, int indent ) const { |
---|
683 | printDesignation( os ); |
---|
684 | os << string( indent, ' ' ) << "Application of: " << endl; |
---|
685 | function->print( os, indent + ParseNode::indent_by ); |
---|
686 | |
---|
687 | os << string( indent, ' ' ) ; |
---|
688 | if ( arguments ) { |
---|
689 | os << "... on arguments: " << endl; |
---|
690 | arguments->printList( os, indent + ParseNode::indent_by ); |
---|
691 | } else |
---|
692 | os << "... on no arguments: " << endl; |
---|
693 | } |
---|
694 | |
---|
695 | void CompositeExprNode::set_function( ExpressionNode *f ) { |
---|
696 | function = f; |
---|
697 | } |
---|
698 | |
---|
699 | void CompositeExprNode::set_args( ExpressionNode *args ) { |
---|
700 | arguments = args; |
---|
701 | } |
---|
702 | |
---|
703 | ExpressionNode *CompositeExprNode::get_function( void ) const { |
---|
704 | return function; |
---|
705 | } |
---|
706 | |
---|
707 | ExpressionNode *CompositeExprNode::get_args( void ) const { |
---|
708 | return arguments; |
---|
709 | } |
---|
710 | |
---|
711 | void CompositeExprNode::add_arg( ExpressionNode *arg ) { |
---|
712 | if ( arguments ) |
---|
713 | arguments->set_link( arg ); |
---|
714 | else |
---|
715 | set_args( arg ); |
---|
716 | } |
---|
717 | |
---|
718 | //############################################################################## |
---|
719 | |
---|
720 | Expression *AsmExprNode::build() const { |
---|
721 | return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)constraint->build(), operand->build() ); |
---|
722 | } |
---|
723 | |
---|
724 | void AsmExprNode::print( std::ostream &os, int indent ) const { |
---|
725 | os << string( indent, ' ' ) << "Assembler Expression:" << endl; |
---|
726 | if ( inout ) { |
---|
727 | os << string( indent, ' ' ) << "inout: " << std::endl; |
---|
728 | inout->print( os, indent + 2 ); |
---|
729 | } // if |
---|
730 | if ( constraint ) { |
---|
731 | os << string( indent, ' ' ) << "constraint: " << std::endl; |
---|
732 | constraint->print( os, indent + 2 ); |
---|
733 | } // if |
---|
734 | if ( operand ) { |
---|
735 | os << string( indent, ' ' ) << "operand: " << std::endl; |
---|
736 | operand->print( os, indent + 2 ); |
---|
737 | } // if |
---|
738 | } |
---|
739 | |
---|
740 | void AsmExprNode::printOneLine( std::ostream &os, int indent ) const { |
---|
741 | printDesignation( os ); |
---|
742 | os << "( "; |
---|
743 | if ( inout ) inout->printOneLine( os, indent + 2 ); |
---|
744 | os << ", "; |
---|
745 | if ( constraint ) constraint->printOneLine( os, indent + 2 ); |
---|
746 | os << ", "; |
---|
747 | if ( operand ) operand->printOneLine( os, indent + 2 ); |
---|
748 | os << ") "; |
---|
749 | } |
---|
750 | |
---|
751 | //############################################################################## |
---|
752 | |
---|
753 | void LabelNode::print( std::ostream &os, int indent ) const {} |
---|
754 | |
---|
755 | void LabelNode::printOneLine( std::ostream &os, int indent ) const {} |
---|
756 | |
---|
757 | //############################################################################## |
---|
758 | |
---|
759 | CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {} |
---|
760 | |
---|
761 | CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) { |
---|
762 | } |
---|
763 | |
---|
764 | CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) { |
---|
765 | } |
---|
766 | |
---|
767 | CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ) { |
---|
768 | add_arg( exp ); |
---|
769 | |
---|
770 | return this; |
---|
771 | } |
---|
772 | |
---|
773 | CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) { |
---|
774 | } |
---|
775 | |
---|
776 | //############################################################################## |
---|
777 | |
---|
778 | ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} |
---|
779 | |
---|
780 | ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) { |
---|
781 | } |
---|
782 | |
---|
783 | ValofExprNode::~ValofExprNode() { |
---|
784 | delete body; |
---|
785 | } |
---|
786 | |
---|
787 | void ValofExprNode::print( std::ostream &os, int indent ) const { |
---|
788 | printDesignation( os ); |
---|
789 | os << string( indent, ' ' ) << "Valof Expression:" << std::endl; |
---|
790 | get_body()->print( os, indent + 4); |
---|
791 | } |
---|
792 | |
---|
793 | void ValofExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
794 | assert( false ); |
---|
795 | } |
---|
796 | |
---|
797 | Expression *ValofExprNode::build() const { |
---|
798 | return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) ); |
---|
799 | } |
---|
800 | |
---|
801 | //############################################################################## |
---|
802 | |
---|
803 | ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { |
---|
804 | if ( init_ == 0 ) |
---|
805 | init = 0; |
---|
806 | else { |
---|
807 | DeclarationNode *decl; |
---|
808 | ExpressionNode *exp; |
---|
809 | |
---|
810 | if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) |
---|
811 | init = new StatementNode( decl ); |
---|
812 | else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) |
---|
813 | init = new StatementNode( StatementNode::Exp, exp ); |
---|
814 | else |
---|
815 | throw SemanticError("Error in for control expression"); |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other ) |
---|
820 | : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) { |
---|
821 | } |
---|
822 | |
---|
823 | ForCtlExprNode::~ForCtlExprNode() { |
---|
824 | delete init; |
---|
825 | delete condition; |
---|
826 | delete change; |
---|
827 | } |
---|
828 | |
---|
829 | Expression *ForCtlExprNode::build() const { |
---|
830 | // this shouldn't be used! |
---|
831 | assert( false ); |
---|
832 | return 0; |
---|
833 | } |
---|
834 | |
---|
835 | void ForCtlExprNode::print( std::ostream &os, int indent ) const{ |
---|
836 | os << string( indent,' ' ) << "For Control Expression -- :" << endl; |
---|
837 | |
---|
838 | os << string( indent + 2, ' ' ) << "initialization:" << endl; |
---|
839 | if ( init != 0 ) |
---|
840 | init->printList( os, indent + 4 ); |
---|
841 | |
---|
842 | os << string( indent + 2, ' ' ) << "condition: " << endl; |
---|
843 | if ( condition != 0 ) |
---|
844 | condition->print( os, indent + 4 ); |
---|
845 | os << string( indent + 2, ' ' ) << "increment: " << endl; |
---|
846 | if ( change != 0 ) |
---|
847 | change->print( os, indent + 4 ); |
---|
848 | } |
---|
849 | |
---|
850 | void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const { |
---|
851 | assert( false ); |
---|
852 | } |
---|
853 | |
---|
854 | //############################################################################## |
---|
855 | |
---|
856 | TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { |
---|
857 | } |
---|
858 | |
---|
859 | TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { |
---|
860 | } |
---|
861 | |
---|
862 | Expression *TypeValueNode::build() const { |
---|
863 | return new TypeExpr( decl->buildType() ); |
---|
864 | } |
---|
865 | |
---|
866 | void TypeValueNode::print( std::ostream &os, int indent ) const { |
---|
867 | os << std::string( indent, ' ' ) << "Type:"; |
---|
868 | get_decl()->print( os, indent + 2); |
---|
869 | } |
---|
870 | |
---|
871 | void TypeValueNode::printOneLine( std::ostream &os, int indent ) const { |
---|
872 | os << "Type:"; |
---|
873 | get_decl()->print( os, indent + 2); |
---|
874 | } |
---|
875 | |
---|
876 | |
---|
877 | CompoundLiteralNode::CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids ) : type( type ), kids( kids ) {} |
---|
878 | CompoundLiteralNode::CompoundLiteralNode( const CompoundLiteralNode &other ) : ExpressionNode( other ), type( other.type ), kids( other.kids ) {} |
---|
879 | |
---|
880 | CompoundLiteralNode::~CompoundLiteralNode() { |
---|
881 | delete kids; |
---|
882 | delete type; |
---|
883 | } |
---|
884 | |
---|
885 | CompoundLiteralNode *CompoundLiteralNode::clone() const { |
---|
886 | return new CompoundLiteralNode( *this ); |
---|
887 | } |
---|
888 | |
---|
889 | void CompoundLiteralNode::print( std::ostream &os, int indent ) const { |
---|
890 | os << string( indent,' ' ) << "CompoundLiteralNode:" << endl; |
---|
891 | |
---|
892 | os << string( indent + 2, ' ' ) << "type:" << endl; |
---|
893 | if ( type != 0 ) |
---|
894 | type->print( os, indent + 4 ); |
---|
895 | |
---|
896 | os << string( indent + 2, ' ' ) << "initialization:" << endl; |
---|
897 | if ( kids != 0 ) |
---|
898 | kids->printList( os, indent + 4 ); |
---|
899 | } |
---|
900 | |
---|
901 | void CompoundLiteralNode::printOneLine( std::ostream &os, int indent ) const { |
---|
902 | os << "( "; |
---|
903 | if ( type ) type->print( os ); |
---|
904 | os << ", "; |
---|
905 | if ( kids ) kids->printOneLine( os ); |
---|
906 | os << ") "; |
---|
907 | } |
---|
908 | |
---|
909 | Expression *CompoundLiteralNode::build() const { |
---|
910 | Declaration * newDecl = type->build(); // compound literal type |
---|
911 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
912 | return new CompoundLiteralExpr( newDeclWithType->get_type(), kids->build() ); |
---|
913 | // these types do not have associated type information |
---|
914 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
915 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), kids->build() ); |
---|
916 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
917 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), kids->build() ); |
---|
918 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
919 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), kids->build() ); |
---|
920 | } else { |
---|
921 | assert( false ); |
---|
922 | } // if |
---|
923 | } |
---|
924 | |
---|
925 | |
---|
926 | ExpressionNode *flattenCommas( ExpressionNode *list ) { |
---|
927 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) { |
---|
928 | OperatorNode *op; |
---|
929 | if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) { |
---|
930 | if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) |
---|
931 | composite->add_arg( next ); |
---|
932 | return flattenCommas( composite->get_args() ); |
---|
933 | } // if |
---|
934 | } // if |
---|
935 | |
---|
936 | if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) ) |
---|
937 | list->set_next( flattenCommas( next ) ); |
---|
938 | |
---|
939 | return list; |
---|
940 | } |
---|
941 | |
---|
942 | ExpressionNode *tupleContents( ExpressionNode *tuple ) { |
---|
943 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) { |
---|
944 | OperatorNode *op = 0; |
---|
945 | if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) ) |
---|
946 | return composite->get_args(); |
---|
947 | } // if |
---|
948 | return tuple; |
---|
949 | } |
---|
950 | |
---|
951 | // Local Variables: // |
---|
952 | // tab-width: 4 // |
---|
953 | // mode: c++ // |
---|
954 | // compile-command: "make install" // |
---|
955 | // End: // |
---|