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