source: src/Parser/ExpressionNode.cc@ ae7014e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since ae7014e was 984dce6, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

only implicitly generate typedef for structures if name not in use and overwrite typedef name if explicit name appears, upate parser symbol table

  • Property mode set to 100644
File size: 26.8 KB
RevLine 
[b87a5ed]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
[097e2b0]11// Last Modified By : Peter A. Buhr
[984dce6]12// Last Modified On : Sun Mar 13 12:34:38 2016
13// Update Count : 272
[b87a5ed]14//
15
[51b73452]16#include <cassert>
17#include <cctype>
18#include <algorithm>
[59db689]19#include <sstream>
20#include <cstdio>
21#include <climits>
[51b73452]22
23#include "ParseNode.h"
24#include "SynTree/Constant.h"
25#include "SynTree/Expression.h"
[d3b7937]26#include "Common/UnimplementedError.h"
[51b73452]27#include "parseutility.h"
[d3b7937]28#include "Common/utility.h"
[51b73452]29
30using namespace std;
31
[3848e0e]32ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {}
[51b73452]33
[e869d663]34ExpressionNode::ExpressionNode( const string *name ) : ParseNode( name ), argName( 0 ) {}
[51b73452]35
[3848e0e]36ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) {
[b87a5ed]37 if ( other.argName ) {
38 argName = other.argName->clone();
39 } else {
40 argName = 0;
41 } // if
[51b73452]42}
43
[7f5566b]44ExpressionNode * ExpressionNode::set_argName( const std::string *aName ) {
[b87a5ed]45 argName = new VarRefNode( aName );
46 return this;
[51b73452]47}
48
[7f5566b]49ExpressionNode * ExpressionNode::set_argName( ExpressionNode *aDesignator ) {
[b87a5ed]50 argName = aDesignator;
51 return this;
[51b73452]52}
53
54void ExpressionNode::printDesignation( std::ostream &os, int indent ) const {
[b87a5ed]55 if ( argName ) {
[59db689]56 os << string( indent, ' ' ) << "(designated by: ";
[b87a5ed]57 argName->printOneLine( os, indent );
58 os << ")" << std::endl;
59 } // if
[51b73452]60}
61
[cd623a4]62//##############################################################################
63
[3848e0e]64NullExprNode::NullExprNode() {}
[51b73452]65
[3848e0e]66NullExprNode *NullExprNode::clone() const {
[b87a5ed]67 return new NullExprNode();
[51b73452]68}
69
[bdd516a]70void NullExprNode::print( std::ostream & os, int indent ) const {
[b87a5ed]71 printDesignation( os );
72 os << "null expression";
[51b73452]73}
74
[bdd516a]75void NullExprNode::printOneLine( std::ostream & os, int indent ) const {
[b87a5ed]76 printDesignation( os );
77 os << "null";
[51b73452]78}
79
[3848e0e]80Expression *NullExprNode::build() const {
[b87a5ed]81 return 0;
[51b73452]82}
83
[a08ba92]84CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ) {
[b87a5ed]85 return new CommaExprNode( this, exp );
[51b73452]86}
87
[cd623a4]88//##############################################################################
89
90static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
91static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
92static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
[5721a6d]93static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
94static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
[cd623a4]95static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
96
[5f2f2d7]97// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
[cd623a4]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.
[51b73452]108
[59db689]109ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), value( *inVal ) {
[cd623a4]110 // lexing divides constants into 4 kinds
[a08ba92]111 switch ( type ) {
[59db689]112 case Integer:
[b87a5ed]113 {
[59db689]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
[5721a6d]120 unsigned long long v; // converted integral value
121 size_t last = value.length() - 1; // last character of constant
[59db689]122
[cd623a4]123 if ( value[0] == '0' ) { // octal constant ?
[59db689]124 dec = false;
[cd623a4]125 if ( last != 0 && checkX( value[1] ) ) { // hex constant ?
[59db689]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
[cd623a4]132 } else { // decimal constant ?
[5f2f2d7]133 sscanf( (char *)value.c_str(), "%llu", &v );
[59db689]134 //printf( "%llu %llu\n", v, v );
135 } // if
136
137 if ( v <= INT_MAX ) { // signed int
138 size = 0;
[5f2f2d7]139 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
[59db689]140 size = 0;
[5f2f2d7]141 Unsigned = true; // unsigned
[59db689]142 } else if ( v <= LONG_MAX ) { // signed long int
143 size = 1;
[5f2f2d7]144 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
[59db689]145 size = 1;
[5f2f2d7]146 Unsigned = true; // unsigned long int
[59db689]147 } else if ( v <= LLONG_MAX ) { // signed long long int
148 size = 2;
[5f2f2d7]149 } else { // unsigned long long int
[59db689]150 size = 2;
[5f2f2d7]151 Unsigned = true; // unsigned long long int
[59db689]152 } // if
[51b73452]153
[cd623a4]154 if ( checkU( value[last] ) ) { // suffix 'u' ?
[59db689]155 Unsigned = true;
[5f2f2d7]156 if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'l' ?
[59db689]157 size = 1;
[5f2f2d7]158 if ( last > 1 && checkL( value[ last - 2 ] ) ) { // suffix 'll' ?
[59db689]159 size = 2;
160 } // if
161 } // if
[cd623a4]162 } else if ( checkL( value[ last ] ) ) { // suffix 'l' ?
[59db689]163 size = 1;
[5f2f2d7]164 if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'll' ?
[59db689]165 size = 2;
[5f2f2d7]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
[59db689]173 } // if
174 } // if
[cd623a4]175 btype = kind[Unsigned][size]; // lookup constant type
176 break;
177 }
178 case Float:
179 {
[5721a6d]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
[cd623a4]192 } // if
[5721a6d]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;
[cd623a4]199 } // if
[5721a6d]200 if ( ! complx && checkI( value[last - 1] ) ) { // imaginary ?
201 complx = true;
202 } // if
203 btype = kind[complx][size]; // lookup constant type
[b87a5ed]204 break;
205 }
[59db689]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
[51b73452]224
[59db689]225ConstantNode *ConstantNode::appendstr( const std::string *newValue ) {
226 assert( newValue != 0 );
227 assert( type == String );
[51b73452]228
[7f5566b]229 // "abc" "def" "ghi" => "abcdefghi", remove new text from quotes and insert before last quote in old string.
[59db689]230 value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) );
231
[cd623a4]232 delete newValue; // allocated by lexer
[b87a5ed]233 return this;
[51b73452]234}
235
[bdd516a]236void ConstantNode::printOneLine( std::ostream &os, int indent ) const {
[59db689]237 os << string( indent, ' ' );
[b87a5ed]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;
[c7ed6d0]251 } // switch
[51b73452]252
[b87a5ed]253 os << ' ';
[51b73452]254}
255
[bdd516a]256void ConstantNode::print( std::ostream &os, int indent ) const {
[b87a5ed]257 printOneLine( os, indent );
258 os << endl;
[51b73452]259}
260
261Expression *ConstantNode::build() const {
[cd623a4]262 ::Type::Qualifiers q; // no qualifiers on constants
[b87a5ed]263
[59db689]264 switch ( get_type() ) {
[b87a5ed]265 case String:
[59db689]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 ),
[ea9b9d3]271 toString( value.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"'
[59db689]272 false, false );
273 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
274 }
275 default:
[cd623a4]276 return new ConstantExpr( Constant( new BasicType( q, btype ), get_value() ), maybeBuild< Expression >( get_argName() ) );
[b87a5ed]277 }
[51b73452]278}
279
[cd623a4]280//##############################################################################
281
[bdd516a]282VarRefNode::VarRefNode() : isLabel( false ) {}
[51b73452]283
[59db689]284VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {}
[51b73452]285
[3848e0e]286VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) {
[51b73452]287}
288
289Expression *VarRefNode::build() const {
[b87a5ed]290 return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) );
[51b73452]291}
292
[bdd516a]293void VarRefNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]294 printDesignation( os );
295 os << get_name() << ' ';
[51b73452]296}
297
[bdd516a]298void VarRefNode::print( std::ostream &os, int indent ) const {
[b87a5ed]299 printDesignation( os );
[44b5ca0]300 os << string( indent, ' ' ) << "Referencing: ";
[b87a5ed]301 os << "Variable: " << get_name();
302 os << endl;
[51b73452]303}
304
[cd623a4]305//##############################################################################
306
[51b1202]307DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex ) : isArrayIndex( isArrayIndex ) {
308 set_argName( expr );
[e869d663]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
[51b1202]326}
327
328DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) {
329}
330
[e869d663]331class DesignatorFixer : public Mutator {
332public:
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
[51b1202]343Expression *DesignatorNode::build() const {
[e869d663]344 Expression * ret = get_argName()->build();
345
[51b1202]346 if ( isArrayIndex ) {
[e869d663]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 );
[51b1202]351 } // if
[e869d663]352
353 return ret;
[51b1202]354}
355
356void 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
369void 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
[5721a6d]384static 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
[bdd516a]397OperatorNode::OperatorNode( Type t ) : type( t ) {}
[51b73452]398
[3848e0e]399OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) {
[51b73452]400}
401
402OperatorNode::~OperatorNode() {}
403
[bdd516a]404OperatorNode::Type OperatorNode::get_type( void ) const{
[b87a5ed]405 return type;
[51b73452]406}
407
[3848e0e]408void OperatorNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]409 printDesignation( os );
[5721a6d]410 os << opName[ type ] << ' ';
[51b73452]411}
412
413void OperatorNode::print( std::ostream &os, int indent ) const{
[b87a5ed]414 printDesignation( os );
[5721a6d]415 os << string( indent, ' ' ) << "Operator: " << opName[type] << endl;
[b87a5ed]416 return;
[51b73452]417}
418
[59db689]419const char *OperatorNode::get_typename( void ) const{
[5721a6d]420 return opName[ type ];
[51b73452]421}
422
[cd623a4]423//##############################################################################
424
[7f5566b]425CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) {
[51b73452]426}
427
[59db689]428CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) {
[51b73452]429}
430
[bdd516a]431CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ):
[b87a5ed]432 function( f ), arguments( args ) {
[51b73452]433}
434
[bdd516a]435CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2):
[097e2b0]436 function( f ), arguments( arg1 ) {
437 arguments->set_link( arg2 );
[51b73452]438}
439
[3848e0e]440CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ) {
[b87a5ed]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 }
[51b73452]450}
451
[3848e0e]452CompositeExprNode::~CompositeExprNode() {
[b87a5ed]453 delete function;
454 delete arguments;
[51b73452]455}
456
[d3b7937]457#include "Common/utility.h"
[51b73452]458
[3848e0e]459Expression *CompositeExprNode::build() const {
[b87a5ed]460 OperatorNode *op;
461 std::list<Expression *> args;
[51b73452]462
[b87a5ed]463 buildList( get_args(), args );
464
[cd623a4]465 if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator
[b87a5ed]466 return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() ));
[cd623a4]467 } // if
[b87a5ed]468
[cd623a4]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;
[984dce6]489 default: // do nothing
[cd623a4]490 ;
[984dce6]491 } // switch
[cd623a4]492
493 switch ( op->get_type() ) {
494 case OperatorNode::Incr:
495 case OperatorNode::Decr:
496 case OperatorNode::IncrPost:
497 case OperatorNode::DecrPost:
498 case OperatorNode::Assign:
499 case OperatorNode::MulAssn:
500 case OperatorNode::DivAssn:
501 case OperatorNode::ModAssn:
502 case OperatorNode::PlusAssn:
503 case OperatorNode::MinusAssn:
504 case OperatorNode::LSAssn:
505 case OperatorNode::RSAssn:
506 case OperatorNode::AndAssn:
507 case OperatorNode::ERAssn:
508 case OperatorNode::OrAssn:
509 case OperatorNode::Plus:
510 case OperatorNode::Minus:
511 case OperatorNode::Mul:
512 case OperatorNode::Div:
513 case OperatorNode::Mod:
514 case OperatorNode::BitOr:
515 case OperatorNode::BitAnd:
516 case OperatorNode::Xor:
517 case OperatorNode::LShift:
518 case OperatorNode::RShift:
519 case OperatorNode::LThan:
520 case OperatorNode::GThan:
521 case OperatorNode::LEThan:
522 case OperatorNode::GEThan:
523 case OperatorNode::Eq:
524 case OperatorNode::Neq:
525 case OperatorNode::Index:
526 case OperatorNode::Range:
527 case OperatorNode::UnPlus:
528 case OperatorNode::UnMinus:
529 case OperatorNode::PointTo:
530 case OperatorNode::Neg:
531 case OperatorNode::BitNeg:
532 case OperatorNode::LabelAddress:
[5721a6d]533 return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args );
[cd623a4]534 case OperatorNode::AddressOf:
535 assert( args.size() == 1 );
536 assert( args.front() );
537
538 return new AddressExpr( args.front() );
539 case OperatorNode::Cast:
540 {
541 TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args());
542 assert( arg );
[b87a5ed]543
[cd623a4]544 DeclarationNode *decl_node = arg->get_decl();
545 ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
[b87a5ed]546
[cd623a4]547 Type *targetType = decl_node->buildType();
548 if ( dynamic_cast< VoidType* >( targetType ) ) {
549 delete targetType;
550 return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) );
551 } else {
552 return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) );
553 } // if
554 }
555 case OperatorNode::FieldSel:
556 {
557 assert( args.size() == 2 );
558
559 NameExpr *member = dynamic_cast<NameExpr *>( args.back());
560 // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back());
[b87a5ed]561
[cd623a4]562 if ( member != 0 ) {
563 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
[b87a5ed]564 delete member;
565 return ret;
[cd623a4]566 /* else if ( memberTup != 0 )
567 {
568 UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front());
569 delete member;
570 return ret;
571 } */
572 } else
573 assert( false );
574 }
575 case OperatorNode::PFieldSel:
576 {
577 assert( args.size() == 2 );
578
579 NameExpr *member = dynamic_cast<NameExpr *>( args.back()); // modify for Tuples xxx
580 assert( member != 0 );
581
582 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
583 deref->get_args().push_back( args.front() );
584
585 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
586 delete member;
587 return ret;
588 }
[5721a6d]589 case OperatorNode::SizeOf:
[47534159]590 {
591 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
[5721a6d]592 return new SizeofExpr( arg->get_decl()->buildType());
[47534159]593 } else {
[5721a6d]594 return new SizeofExpr( args.front());
[47534159]595 } // if
596 }
[5721a6d]597 case OperatorNode::AlignOf:
[cd623a4]598 {
599 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
[5721a6d]600 return new AlignofExpr( arg->get_decl()->buildType());
[cd623a4]601 } else {
[5721a6d]602 return new AlignofExpr( args.front());
[cd623a4]603 } // if
604 }
[5721a6d]605 case OperatorNode::OffsetOf:
606 {
[2a4b088]607 assert( args.size() == 2 );
608
609 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args() ) ) {
610 NameExpr *member = dynamic_cast<NameExpr *>( args.back() );
611 assert( member != 0 );
612
613 return new UntypedOffsetofExpr( arg->get_decl()->buildType(), member->get_name() );
614 } else assert( false );
[5721a6d]615 }
[cd623a4]616 case OperatorNode::Attr:
617 {
618 VarRefNode *var = dynamic_cast<VarRefNode *>( get_args());
619 assert( var );
620 if ( ! get_args()->get_link() ) {
621 return new AttrExpr( var->build(), ( Expression*)0);
622 } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) {
623 return new AttrExpr( var->build(), arg->get_decl()->buildType());
624 } else {
625 return new AttrExpr( var->build(), args.back());
626 } // if
627 }
628 case OperatorNode::CompLit:
629 throw UnimplementedError( "C99 compound literals" );
630 // the short-circuited operators
631 case OperatorNode::Or:
632 case OperatorNode::And:
633 assert( args.size() == 2);
634 return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) );
635 case OperatorNode::Cond:
636 {
637 assert( args.size() == 3);
[7f5566b]638 std::list< Expression * >::const_iterator i = args.begin();
[cd623a4]639 Expression *arg1 = notZeroExpr( *i++ );
640 Expression *arg2 = *i++;
641 Expression *arg3 = *i++;
642 return new ConditionalExpr( arg1, arg2, arg3 );
643 }
644 case OperatorNode::NCond:
645 throw UnimplementedError( "GNU 2-argument conditional expression" );
646 case OperatorNode::Comma:
647 {
[b87a5ed]648 assert( args.size() == 2);
[7f5566b]649 std::list< Expression * >::const_iterator i = args.begin();
[cd623a4]650 Expression *ret = *i++;
651 while ( i != args.end() ) {
652 ret = new CommaExpr( ret, *i++ );
[b87a5ed]653 }
[cd623a4]654 return ret;
[3848e0e]655 }
[cd623a4]656 // Tuples
657 case OperatorNode::TupleC:
658 {
659 TupleExpr *ret = new TupleExpr();
660 std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) );
661 return ret;
662 }
663 default:
664 // shouldn't happen
[5721a6d]665 assert( false );
[cd623a4]666 return 0;
667 } // switch
[51b73452]668}
669
[bdd516a]670void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]671 printDesignation( os );
672 os << "( ";
673 function->printOneLine( os, indent );
674 for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) {
675 cur->printOneLine( os, indent );
[5721a6d]676 } // for
[b87a5ed]677 os << ") ";
[51b73452]678}
679
[bdd516a]680void CompositeExprNode::print( std::ostream &os, int indent ) const {
[b87a5ed]681 printDesignation( os );
[44b5ca0]682 os << string( indent, ' ' ) << "Application of: " << endl;
[b87a5ed]683 function->print( os, indent + ParseNode::indent_by );
[51b73452]684
[44b5ca0]685 os << string( indent, ' ' ) ;
[b87a5ed]686 if ( arguments ) {
687 os << "... on arguments: " << endl;
688 arguments->printList( os, indent + ParseNode::indent_by );
689 } else
690 os << "... on no arguments: " << endl;
[51b73452]691}
692
[a08ba92]693void CompositeExprNode::set_function( ExpressionNode *f ) {
[b87a5ed]694 function = f;
[51b73452]695}
696
[a08ba92]697void CompositeExprNode::set_args( ExpressionNode *args ) {
[b87a5ed]698 arguments = args;
[51b73452]699}
700
[bdd516a]701ExpressionNode *CompositeExprNode::get_function( void ) const {
[b87a5ed]702 return function;
[51b73452]703}
704
[bdd516a]705ExpressionNode *CompositeExprNode::get_args( void ) const {
[b87a5ed]706 return arguments;
[51b73452]707}
708
[a08ba92]709void CompositeExprNode::add_arg( ExpressionNode *arg ) {
[b87a5ed]710 if ( arguments )
711 arguments->set_link( arg );
712 else
713 set_args( arg );
[51b73452]714}
715
[cd623a4]716//##############################################################################
717
[7f5566b]718Expression *AsmExprNode::build() const {
719 return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)constraint->build(), operand->build() );
720}
721
722void AsmExprNode::print( std::ostream &os, int indent ) const {
723 os << string( indent, ' ' ) << "Assembler Expression:" << endl;
724 if ( inout ) {
725 os << string( indent, ' ' ) << "inout: " << std::endl;
726 inout->print( os, indent + 2 );
727 } // if
728 if ( constraint ) {
729 os << string( indent, ' ' ) << "constraint: " << std::endl;
730 constraint->print( os, indent + 2 );
731 } // if
732 if ( operand ) {
733 os << string( indent, ' ' ) << "operand: " << std::endl;
734 operand->print( os, indent + 2 );
735 } // if
736}
737
738void AsmExprNode::printOneLine( std::ostream &os, int indent ) const {
739 printDesignation( os );
740 os << "( ";
741 if ( inout ) inout->printOneLine( os, indent + 2 );
742 os << ", ";
743 if ( constraint ) constraint->printOneLine( os, indent + 2 );
744 os << ", ";
745 if ( operand ) operand->printOneLine( os, indent + 2 );
746 os << ") ";
747}
748
749//##############################################################################
750
751void LabelNode::print( std::ostream &os, int indent ) const {}
752
753void LabelNode::printOneLine( std::ostream &os, int indent ) const {}
754
755//##############################################################################
756
[bdd516a]757CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {}
[51b73452]758
[bdd516a]759CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) {
[3848e0e]760}
[51b73452]761
[bdd516a]762CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) {
[51b73452]763}
764
[a08ba92]765CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ) {
[b87a5ed]766 add_arg( exp );
[51b73452]767
[b87a5ed]768 return this;
[51b73452]769}
770
[3848e0e]771CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) {
[51b73452]772}
773
[cd623a4]774//##############################################################################
775
[bdd516a]776ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {}
[51b73452]777
[3848e0e]778ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) {
[51b73452]779}
780
781ValofExprNode::~ValofExprNode() {
[b87a5ed]782 delete body;
[51b73452]783}
784
785void ValofExprNode::print( std::ostream &os, int indent ) const {
[b87a5ed]786 printDesignation( os );
[59db689]787 os << string( indent, ' ' ) << "Valof Expression:" << std::endl;
[b87a5ed]788 get_body()->print( os, indent + 4);
[51b73452]789}
790
[3848e0e]791void ValofExprNode::printOneLine( std::ostream &, int indent ) const {
[b87a5ed]792 assert( false );
[51b73452]793}
794
795Expression *ValofExprNode::build() const {
[b87a5ed]796 return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) );
[3848e0e]797}
798
[cd623a4]799//##############################################################################
800
[bdd516a]801ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
[b87a5ed]802 if ( init_ == 0 )
803 init = 0;
804 else {
805 DeclarationNode *decl;
806 ExpressionNode *exp;
807
[a61fea9a]808 if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
[b87a5ed]809 init = new StatementNode( decl );
810 else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0)
811 init = new StatementNode( StatementNode::Exp, exp );
812 else
813 throw SemanticError("Error in for control expression");
814 }
[51b73452]815}
816
817ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
[b87a5ed]818 : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
[51b73452]819}
820
[a08ba92]821ForCtlExprNode::~ForCtlExprNode() {
[b87a5ed]822 delete init;
823 delete condition;
824 delete change;
[51b73452]825}
826
827Expression *ForCtlExprNode::build() const {
[b87a5ed]828 // this shouldn't be used!
829 assert( false );
830 return 0;
[51b73452]831}
832
833void ForCtlExprNode::print( std::ostream &os, int indent ) const{
[59db689]834 os << string( indent,' ' ) << "For Control Expression -- :" << endl;
[a61fea9a]835
[59db689]836 os << string( indent + 2, ' ' ) << "initialization:" << endl;
[a61fea9a]837 if ( init != 0 )
838 init->printList( os, indent + 4 );
839
840 os << string( indent + 2, ' ' ) << "condition: " << endl;
841 if ( condition != 0 )
842 condition->print( os, indent + 4 );
[59db689]843 os << string( indent + 2, ' ' ) << "increment: " << endl;
[a61fea9a]844 if ( change != 0 )
845 change->print( os, indent + 4 );
[51b73452]846}
847
[3848e0e]848void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
[b87a5ed]849 assert( false );
[51b73452]850}
851
[cd623a4]852//##############################################################################
853
854TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) {
[51b73452]855}
856
[cd623a4]857TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
[51b73452]858}
859
[3848e0e]860Expression *TypeValueNode::build() const {
[b87a5ed]861 return new TypeExpr( decl->buildType() );
[51b73452]862}
863
[bdd516a]864void TypeValueNode::print( std::ostream &os, int indent ) const {
[b87a5ed]865 os << std::string( indent, ' ' ) << "Type:";
866 get_decl()->print( os, indent + 2);
[51b73452]867}
868
[bdd516a]869void TypeValueNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]870 os << "Type:";
871 get_decl()->print( os, indent + 2);
[51b73452]872}
873
[3848e0e]874ExpressionNode *flattenCommas( ExpressionNode *list ) {
[cd623a4]875 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) {
876 OperatorNode *op;
877 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) {
878 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
879 composite->add_arg( next );
880 return flattenCommas( composite->get_args() );
881 } // if
882 } // if
[51b73452]883
[b87a5ed]884 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
885 list->set_next( flattenCommas( next ) );
[51b73452]886
[b87a5ed]887 return list;
[51b73452]888}
889
[3848e0e]890ExpressionNode *tupleContents( ExpressionNode *tuple ) {
[b87a5ed]891 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) {
892 OperatorNode *op = 0;
893 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) )
894 return composite->get_args();
[cd623a4]895 } // if
[b87a5ed]896 return tuple;
[51b73452]897}
[b87a5ed]898
899// Local Variables: //
900// tab-width: 4 //
901// mode: c++ //
902// compile-command: "make install" //
903// End: //
Note: See TracBrowser for help on using the repository browser.