source: src/Parser/ExpressionNode.cc@ d63eeb0

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 with_gc
Last change on this file since d63eeb0 was 2a4b088, checked in by Aaron Moss <a3moss@…>, 10 years ago

Make offsetof expressions work

  • 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
[5721a6d]12// Last Modified On : Mon Feb 1 13:32:30 2016
13// Update Count : 271
[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;
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:
[5721a6d]534 return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args );
[cd623a4]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 );
[b87a5ed]544
[cd623a4]545 DeclarationNode *decl_node = arg->get_decl();
546 ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
[b87a5ed]547
[cd623a4]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());
[b87a5ed]562
[cd623a4]563 if ( member != 0 ) {
564 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
[b87a5ed]565 delete member;
566 return ret;
[cd623a4]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 }
[5721a6d]590 case OperatorNode::SizeOf:
[47534159]591 {
592 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
[5721a6d]593 return new SizeofExpr( arg->get_decl()->buildType());
[47534159]594 } else {
[5721a6d]595 return new SizeofExpr( args.front());
[47534159]596 } // if
597 }
[5721a6d]598 case OperatorNode::AlignOf:
[cd623a4]599 {
600 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
[5721a6d]601 return new AlignofExpr( arg->get_decl()->buildType());
[cd623a4]602 } else {
[5721a6d]603 return new AlignofExpr( args.front());
[cd623a4]604 } // if
605 }
[5721a6d]606 case OperatorNode::OffsetOf:
607 {
[2a4b088]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 );
[5721a6d]616 }
[cd623a4]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);
[7f5566b]639 std::list< Expression * >::const_iterator i = args.begin();
[cd623a4]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 {
[b87a5ed]649 assert( args.size() == 2);
[7f5566b]650 std::list< Expression * >::const_iterator i = args.begin();
[cd623a4]651 Expression *ret = *i++;
652 while ( i != args.end() ) {
653 ret = new CommaExpr( ret, *i++ );
[b87a5ed]654 }
[cd623a4]655 return ret;
[3848e0e]656 }
[cd623a4]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
[5721a6d]666 assert( false );
[cd623a4]667 return 0;
668 } // switch
[51b73452]669}
670
[bdd516a]671void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]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 );
[5721a6d]677 } // for
[b87a5ed]678 os << ") ";
[51b73452]679}
680
[bdd516a]681void CompositeExprNode::print( std::ostream &os, int indent ) const {
[b87a5ed]682 printDesignation( os );
[44b5ca0]683 os << string( indent, ' ' ) << "Application of: " << endl;
[b87a5ed]684 function->print( os, indent + ParseNode::indent_by );
[51b73452]685
[44b5ca0]686 os << string( indent, ' ' ) ;
[b87a5ed]687 if ( arguments ) {
688 os << "... on arguments: " << endl;
689 arguments->printList( os, indent + ParseNode::indent_by );
690 } else
691 os << "... on no arguments: " << endl;
[51b73452]692}
693
[a08ba92]694void CompositeExprNode::set_function( ExpressionNode *f ) {
[b87a5ed]695 function = f;
[51b73452]696}
697
[a08ba92]698void CompositeExprNode::set_args( ExpressionNode *args ) {
[b87a5ed]699 arguments = args;
[51b73452]700}
701
[bdd516a]702ExpressionNode *CompositeExprNode::get_function( void ) const {
[b87a5ed]703 return function;
[51b73452]704}
705
[bdd516a]706ExpressionNode *CompositeExprNode::get_args( void ) const {
[b87a5ed]707 return arguments;
[51b73452]708}
709
[a08ba92]710void CompositeExprNode::add_arg( ExpressionNode *arg ) {
[b87a5ed]711 if ( arguments )
712 arguments->set_link( arg );
713 else
714 set_args( arg );
[51b73452]715}
716
[cd623a4]717//##############################################################################
718
[7f5566b]719Expression *AsmExprNode::build() const {
720 return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)constraint->build(), operand->build() );
721}
722
723void 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
739void 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
752void LabelNode::print( std::ostream &os, int indent ) const {}
753
754void LabelNode::printOneLine( std::ostream &os, int indent ) const {}
755
756//##############################################################################
757
[bdd516a]758CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {}
[51b73452]759
[bdd516a]760CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) {
[3848e0e]761}
[51b73452]762
[bdd516a]763CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) {
[51b73452]764}
765
[a08ba92]766CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ) {
[b87a5ed]767 add_arg( exp );
[51b73452]768
[b87a5ed]769 return this;
[51b73452]770}
771
[3848e0e]772CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) {
[51b73452]773}
774
[cd623a4]775//##############################################################################
776
[bdd516a]777ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {}
[51b73452]778
[3848e0e]779ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) {
[51b73452]780}
781
782ValofExprNode::~ValofExprNode() {
[b87a5ed]783 delete body;
[51b73452]784}
785
786void ValofExprNode::print( std::ostream &os, int indent ) const {
[b87a5ed]787 printDesignation( os );
[59db689]788 os << string( indent, ' ' ) << "Valof Expression:" << std::endl;
[b87a5ed]789 get_body()->print( os, indent + 4);
[51b73452]790}
791
[3848e0e]792void ValofExprNode::printOneLine( std::ostream &, int indent ) const {
[b87a5ed]793 assert( false );
[51b73452]794}
795
796Expression *ValofExprNode::build() const {
[b87a5ed]797 return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) );
[3848e0e]798}
799
[cd623a4]800//##############################################################################
801
[bdd516a]802ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
[b87a5ed]803 if ( init_ == 0 )
804 init = 0;
805 else {
806 DeclarationNode *decl;
807 ExpressionNode *exp;
808
[a61fea9a]809 if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
[b87a5ed]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 }
[51b73452]816}
817
818ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
[b87a5ed]819 : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
[51b73452]820}
821
[a08ba92]822ForCtlExprNode::~ForCtlExprNode() {
[b87a5ed]823 delete init;
824 delete condition;
825 delete change;
[51b73452]826}
827
828Expression *ForCtlExprNode::build() const {
[b87a5ed]829 // this shouldn't be used!
830 assert( false );
831 return 0;
[51b73452]832}
833
834void ForCtlExprNode::print( std::ostream &os, int indent ) const{
[59db689]835 os << string( indent,' ' ) << "For Control Expression -- :" << endl;
[a61fea9a]836
[59db689]837 os << string( indent + 2, ' ' ) << "initialization:" << endl;
[a61fea9a]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 );
[59db689]844 os << string( indent + 2, ' ' ) << "increment: " << endl;
[a61fea9a]845 if ( change != 0 )
846 change->print( os, indent + 4 );
[51b73452]847}
848
[3848e0e]849void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
[b87a5ed]850 assert( false );
[51b73452]851}
852
[cd623a4]853//##############################################################################
854
855TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) {
[51b73452]856}
857
[cd623a4]858TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
[51b73452]859}
860
[3848e0e]861Expression *TypeValueNode::build() const {
[b87a5ed]862 return new TypeExpr( decl->buildType() );
[51b73452]863}
864
[bdd516a]865void TypeValueNode::print( std::ostream &os, int indent ) const {
[b87a5ed]866 os << std::string( indent, ' ' ) << "Type:";
867 get_decl()->print( os, indent + 2);
[51b73452]868}
869
[bdd516a]870void TypeValueNode::printOneLine( std::ostream &os, int indent ) const {
[b87a5ed]871 os << "Type:";
872 get_decl()->print( os, indent + 2);
[51b73452]873}
874
[3848e0e]875ExpressionNode *flattenCommas( ExpressionNode *list ) {
[cd623a4]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
[51b73452]884
[b87a5ed]885 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
886 list->set_next( flattenCommas( next ) );
[51b73452]887
[b87a5ed]888 return list;
[51b73452]889}
890
[3848e0e]891ExpressionNode *tupleContents( ExpressionNode *tuple ) {
[b87a5ed]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();
[cd623a4]896 } // if
[b87a5ed]897 return tuple;
[51b73452]898}
[b87a5ed]899
900// Local Variables: //
901// tab-width: 4 //
902// mode: c++ //
903// compile-command: "make install" //
904// End: //
Note: See TracBrowser for help on using the repository browser.