source: src/Parser/ExpressionNode.cc @ 5ba653c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 5ba653c was 984dce6, checked in by Peter A. Buhr <pabuhr@…>, 8 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
Line 
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 : Sun Mar 13 12:34:38 2016
13// Update Count     : 272
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
30using namespace std;
31
32ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {}
33
34ExpressionNode::ExpressionNode( const string *name ) : ParseNode( name ), argName( 0 ) {}
35
36ExpressionNode::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
44ExpressionNode * ExpressionNode::set_argName( const std::string *aName ) {
45        argName = new VarRefNode( aName );
46        return this;
47}
48
49ExpressionNode * ExpressionNode::set_argName( ExpressionNode *aDesignator ) {
50        argName = aDesignator;
51        return this;
52}
53
54void 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
64NullExprNode::NullExprNode() {}
65
66NullExprNode *NullExprNode::clone() const {
67        return new NullExprNode();
68}
69
70void NullExprNode::print( std::ostream & os, int indent ) const {
71        printDesignation( os );
72        os << "null expression";
73}
74
75void NullExprNode::printOneLine( std::ostream & os, int indent ) const {
76        printDesignation( os );
77        os << "null";
78}
79
80Expression *NullExprNode::build() const {
81        return 0;
82}
83
84CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ) {
85        return new CommaExprNode( this, exp );
86}
87
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'; }
93static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
94static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
95static 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
109ConstantNode::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
225ConstantNode *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
236void 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
256void ConstantNode::print( std::ostream &os, int indent ) const {
257        printOneLine( os, indent );
258        os << endl;
259}
260
261Expression *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
282VarRefNode::VarRefNode() : isLabel( false ) {}
283
284VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {}
285
286VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) {
287}
288
289Expression *VarRefNode::build() const {
290        return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) );
291}
292
293void VarRefNode::printOneLine( std::ostream &os, int indent ) const {
294        printDesignation( os );
295        os << get_name() << ' ';
296}
297
298void 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
307DesignatorNode::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
328DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) {
329}
330
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
343Expression *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
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
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
397OperatorNode::OperatorNode( Type t ) : type( t ) {}
398
399OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) {
400}
401
402OperatorNode::~OperatorNode() {}
403
404OperatorNode::Type OperatorNode::get_type( void ) const{
405        return type;
406}
407
408void OperatorNode::printOneLine( std::ostream &os, int indent ) const {
409        printDesignation( os );
410        os << opName[ type ] << ' ';
411}
412
413void OperatorNode::print( std::ostream &os, int indent ) const{
414        printDesignation( os );
415        os << string( indent, ' ' ) << "Operator: " << opName[type] << endl;
416        return;
417}
418
419const char *OperatorNode::get_typename( void ) const{
420        return opName[ type ];
421}
422
423//##############################################################################
424
425CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) {
426}
427
428CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) {
429}
430
431CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ):
432        function( f ), arguments( args ) {
433}
434
435CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2):
436        function( f ), arguments( arg1 ) {
437        arguments->set_link( arg2 );
438}
439
440CompositeExprNode::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
452CompositeExprNode::~CompositeExprNode() {
453        delete function;
454        delete arguments;
455}
456
457#include "Common/utility.h"
458
459Expression *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:              // do nothing
490                ;
491        } // switch
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:
533                return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args );
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 );
543
544                        DeclarationNode *decl_node = arg->get_decl();
545                        ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
546
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());
561
562                        if ( member != 0 ) {
563                                UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
564                                delete member;
565                                return ret;
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                }
589          case OperatorNode::SizeOf:
590                {
591                        if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
592                                return new SizeofExpr( arg->get_decl()->buildType());
593                        } else {
594                                return new SizeofExpr( args.front());
595                        } // if
596                }
597          case OperatorNode::AlignOf:
598                {
599                        if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
600                                return new AlignofExpr( arg->get_decl()->buildType());
601                        } else {
602                                return new AlignofExpr( args.front());
603                        } // if
604                }
605          case OperatorNode::OffsetOf:
606                {
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 );
615                }
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);
638                        std::list< Expression * >::const_iterator i = args.begin();
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                {
648                        assert( args.size() == 2);
649                        std::list< Expression * >::const_iterator i = args.begin();
650                        Expression *ret = *i++;
651                        while ( i != args.end() ) {
652                                ret = new CommaExpr( ret, *i++ );
653                        }
654                        return ret;
655                }
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
665                assert( false );
666                return 0;
667        } // switch
668}
669
670void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const {
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 );
676        } // for
677        os << ") ";
678}
679
680void CompositeExprNode::print( std::ostream &os, int indent ) const {
681        printDesignation( os );
682        os << string( indent, ' ' ) << "Application of: " << endl;
683        function->print( os, indent + ParseNode::indent_by );
684
685        os << string( indent, ' ' ) ;
686        if ( arguments ) {
687                os << "... on arguments: " << endl;
688                arguments->printList( os, indent + ParseNode::indent_by );
689        } else
690                os << "... on no arguments: " << endl;
691}
692
693void CompositeExprNode::set_function( ExpressionNode *f ) {
694        function = f;
695}
696
697void CompositeExprNode::set_args( ExpressionNode *args ) {
698        arguments = args;
699}
700
701ExpressionNode *CompositeExprNode::get_function( void ) const {
702        return function;
703}
704
705ExpressionNode *CompositeExprNode::get_args( void ) const {
706        return arguments;
707}
708
709void CompositeExprNode::add_arg( ExpressionNode *arg ) {
710        if ( arguments )
711                arguments->set_link( arg );
712        else
713                set_args( arg );
714}
715
716//##############################################################################
717
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
757CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {}
758
759CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) {
760}
761
762CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) {
763}
764
765CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ) {
766        add_arg( exp );
767
768        return this;
769}
770
771CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) {
772}
773
774//##############################################################################
775
776ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {}
777
778ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) {
779}
780
781ValofExprNode::~ValofExprNode() {
782        delete body;
783}
784
785void ValofExprNode::print( std::ostream &os, int indent ) const {
786        printDesignation( os );
787        os << string( indent, ' ' ) << "Valof Expression:" << std::endl;
788        get_body()->print( os, indent + 4);
789}
790
791void ValofExprNode::printOneLine( std::ostream &, int indent ) const {
792        assert( false );
793}
794
795Expression *ValofExprNode::build() const {
796        return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) );
797}
798
799//##############################################################################
800
801ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
802        if ( init_ == 0 )
803                init = 0;
804        else {
805                DeclarationNode *decl;
806                ExpressionNode *exp;
807
808                if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
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        }
815}
816
817ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
818        : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
819}
820
821ForCtlExprNode::~ForCtlExprNode() {
822        delete init;
823        delete condition;
824        delete change;
825}
826
827Expression *ForCtlExprNode::build() const {
828        // this shouldn't be used!
829        assert( false );
830        return 0;
831}
832
833void ForCtlExprNode::print( std::ostream &os, int indent ) const{
834        os << string( indent,' ' ) << "For Control Expression -- :" << endl;
835
836        os << string( indent + 2, ' ' ) << "initialization:" << endl;
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 );
843        os << string( indent + 2, ' ' ) << "increment: " << endl;
844        if ( change != 0 )
845                change->print( os, indent + 4 );
846}
847
848void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
849        assert( false );
850}
851
852//##############################################################################
853
854TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) {
855}
856
857TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
858}
859
860Expression *TypeValueNode::build() const {
861        return new TypeExpr( decl->buildType() );
862}
863
864void TypeValueNode::print( std::ostream &os, int indent ) const {
865        os << std::string( indent, ' ' ) << "Type:";
866        get_decl()->print( os, indent + 2);
867}
868
869void TypeValueNode::printOneLine( std::ostream &os, int indent ) const {
870        os << "Type:";
871        get_decl()->print( os, indent + 2);
872}
873
874ExpressionNode *flattenCommas( ExpressionNode *list ) {
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
883
884        if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
885                list->set_next( flattenCommas( next ) );
886
887        return list;
888}
889
890ExpressionNode *tupleContents( ExpressionNode *tuple ) {
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();
895        } // if
896        return tuple;
897}
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.