source: src/SynTree/Expression.cc @ 3e8fb3b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 3e8fb3b was 3da470c, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 14.9 KB
RevLine 
[0dd3a2f]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//
[3be261a]7// Expression.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[630a82a]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Apr  8 17:16:23 2016
13// Update Count     : 40
[0dd3a2f]14//
15
[51b7345]16#include <iostream>
17#include <cassert>
18#include <list>
19#include <algorithm>
20
21#include <iterator>
22
23#include "Type.h"
[630a82a]24#include "Initializer.h"
[51b7345]25#include "Expression.h"
26#include "Declaration.h"
27#include "Statement.h"
28#include "TypeSubstitution.h"
[d3b7937]29#include "Common/utility.h"
[51b7345]30
31
32Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
[0dd3a2f]33
[d0d9610]34Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ) {
[0dd3a2f]35        cloneAll( other.results, results );
36}
37
38Expression::~Expression() {
39        delete env;
[7f5566b]40        delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
[0dd3a2f]41        deleteAll( results );
42}
43
44void Expression::add_result( Type *t ) {
45        if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) {
46                std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) );
47        } else {
48                results.push_back(t);
49        } // if
[51b7345]50}
51
[59db689]52void Expression::print( std::ostream &os, int indent ) const {
[0dd3a2f]53        if ( env ) {
[cf0941d]54                os << std::string( indent, ' ' ) << "with environment:" << std::endl;
[0dd3a2f]55                env->print( os, indent+2 );
56        } // if
[51b7345]57
[0dd3a2f]58        if ( argName ) {
[cf0941d]59                os << std::string( indent, ' ' ) << "with designator:";
[0dd3a2f]60                argName->print( os, indent+2 );
61        } // if
[51b7345]62}
63
[0dd3a2f]64ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
65        add_result( constant.get_type()->clone() );
[51b7345]66}
67
[0dd3a2f]68ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
69}
[51b7345]70
71ConstantExpr::~ConstantExpr() {}
72
[0dd3a2f]73void ConstantExpr::print( std::ostream &os, int indent ) const {
[d58ebf3]74        os << std::string( indent, ' ' ) << "constant expression " ;
[cf0941d]75        constant.print( os );
[0dd3a2f]76        Expression::print( os, indent );
[d58ebf3]77        os << std::endl;
[51b7345]78}
79
[0dd3a2f]80VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
81        add_result( var->get_type()->clone() );
82        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
83                (*i)->set_isLvalue( true );
84        } // for
[51b7345]85}
86
[0dd3a2f]87VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
[51b7345]88}
89
[0dd3a2f]90VariableExpr::~VariableExpr() {
91        // don't delete the declaration, since it points somewhere else in the tree
[51b7345]92}
93
[0dd3a2f]94void VariableExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]95        os << std::string( indent, ' ' ) << "Variable Expression: ";
[51b7345]96
[0dd3a2f]97        Declaration *decl = get_var();
98        // if ( decl != 0) decl->print(os, indent + 2);
99        if ( decl != 0) decl->printShort(os, indent + 2);
100        os << std::endl;
101        Expression::print( os, indent );
[51b7345]102}
103
104SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
[0dd3a2f]105                Expression( _aname ), expr(expr_), type(0), isType(false) {
[bfebb6c5]106        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b7345]107}
108
109SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
[0dd3a2f]110                Expression( _aname ), expr(0), type(type_), isType(true) {
[bfebb6c5]111        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b7345]112}
113
[0dd3a2f]114SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
115        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b7345]116}
117
[0dd3a2f]118SizeofExpr::~SizeofExpr() {
119        delete expr;
120        delete type;
[51b7345]121}
122
123void SizeofExpr::print( std::ostream &os, int indent) const {
[44b5ca0]124        os << std::string( indent, ' ' ) << "Sizeof Expression on: ";
[51b7345]125
[47534159]126        if (isType)
127                type->print(os, indent + 2);
128        else
129                expr->print(os, indent + 2);
130
131        os << std::endl;
132        Expression::print( os, indent );
133}
134
135AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
136                Expression( _aname ), expr(expr_), type(0), isType(false) {
[bfebb6c5]137        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]138}
139
140AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
141                Expression( _aname ), expr(0), type(type_), isType(true) {
[bfebb6c5]142        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]143}
144
145AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
146        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
147}
148
149AlignofExpr::~AlignofExpr() {
150        delete expr;
151        delete type;
152}
153
154void AlignofExpr::print( std::ostream &os, int indent) const {
155        os << std::string( indent, ' ' ) << "Alignof Expression on: ";
156
[0dd3a2f]157        if (isType)
158                type->print(os, indent + 2);
159        else
160                expr->print(os, indent + 2);
[51b7345]161
[0dd3a2f]162        os << std::endl;
163        Expression::print( os, indent );
[51b7345]164}
165
[2a4b088]166UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
167                Expression( _aname ), type(type_), member(member_) {
168        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
169}
170
171UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
172        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
173
174UntypedOffsetofExpr::~UntypedOffsetofExpr() {
175        delete type;
176}
177
178void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
179        os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
180
181        if ( type ) {
182                type->print(os, indent + 2);
183        } else {
184                os << "<NULL>";
185        }
186
187        os << std::endl;
188        Expression::print( os, indent );
189}
190
[25a054f]191OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
192                Expression( _aname ), type(type_), member(member_) {
[bfebb6c5]193        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[25a054f]194}
195
196OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
197        Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
198
199OffsetofExpr::~OffsetofExpr() {
200        delete type;
201        delete member;
202}
203
204void OffsetofExpr::print( std::ostream &os, int indent) const {
205        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
206
207        if ( member ) {
208                os << member->get_name();
209        } else {
210                os << "<NULL>";
211        }
212
213        os << " of ";
214
215        if ( type ) {
216                type->print(os, indent + 2);
217        } else {
218                os << "<NULL>";
219        }
220
221        os << std::endl;
222        Expression::print( os, indent );
223}
224
[afc1045]225OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
226        add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
227}
228
229OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
230
231OffsetPackExpr::~OffsetPackExpr() { delete type; }
232
233void OffsetPackExpr::print( std::ostream &os, int indent ) const {
234        os << std::string( indent, ' ' ) << "Offset pack expression on ";
235
236        if ( type ) {
237                type->print(os, indent + 2);
238        } else {
239                os << "<NULL>";
240        }
241
[25a054f]242        os << std::endl;
243        Expression::print( os, indent );
244}
245
[51b7345]246AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
[0dd3a2f]247                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
[51b7345]248}
249
250AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
[0dd3a2f]251                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
[51b7345]252}
253
[0dd3a2f]254AttrExpr::AttrExpr( const AttrExpr &other ) :
255                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b7345]256}
257
[0dd3a2f]258AttrExpr::~AttrExpr() {
259        delete attr;
260        delete expr;
261        delete type;
[51b7345]262}
263
264void AttrExpr::print( std::ostream &os, int indent) const {
[44b5ca0]265        os << std::string( indent, ' ' ) << "Attr ";
[0dd3a2f]266        attr->print( os, indent + 2 );
267        if ( isType || expr ) {
268                os << "applied to: ";
[51b7345]269
[0dd3a2f]270                if (isType)
271                        type->print(os, indent + 2);
272                else
273                        expr->print(os, indent + 2);
274        } // if
[51b7345]275
[0dd3a2f]276        os << std::endl;
277        Expression::print( os, indent );
[51b7345]278}
279
[0dd3a2f]280CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
281        add_result(toType);
[51b7345]282}
283
284CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
285}
286
[0dd3a2f]287CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
[51b7345]288}
289
290CastExpr::~CastExpr() {
[0dd3a2f]291        delete arg;
[51b7345]292}
293
294// CastExpr *CastExpr::clone() const { return 0; }
295
296void CastExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]297        os << std::string( indent, ' ' ) << "Cast of:" << std::endl;
[0dd3a2f]298        arg->print(os, indent+2);
[44b5ca0]299        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
[0dd3a2f]300        if ( results.empty() ) {
[44b5ca0]301                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
[0dd3a2f]302        } else {
303                printAll(results, os, indent+2);
304        } // if
305        Expression::print( os, indent );
306}
307
[51b7345]308UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]309                Expression( _aname ), member(_member), aggregate(_aggregate) {}
[51b7345]310
[0dd3a2f]311UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
312                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]313}
314
315UntypedMemberExpr::~UntypedMemberExpr() {
[0dd3a2f]316        delete aggregate;
[51b7345]317}
318
319void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]320        os << std::string( indent, ' ' ) << "Member Expression, with field: " << get_member();
[51b7345]321
[0dd3a2f]322        Expression *agg = get_aggregate();
[44b5ca0]323        os << std::string( indent, ' ' ) << "from aggregate: ";
[0dd3a2f]324        if (agg != 0) agg->print(os, indent + 2);
325        Expression::print( os, indent );
[51b7345]326}
327
328
329MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]330                Expression( _aname ), member(_member), aggregate(_aggregate) {
331        add_result( member->get_type()->clone() );
332        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
333                (*i)->set_isLvalue( true );
334        } // for
[51b7345]335}
336
[0dd3a2f]337MemberExpr::MemberExpr( const MemberExpr &other ) :
338                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]339}
340
341MemberExpr::~MemberExpr() {
[0dd3a2f]342        delete member;
343        delete aggregate;
[51b7345]344}
345
346void MemberExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]347        os << std::string( indent, ' ' ) << "Member Expression, with field: " << std::endl;
[51b7345]348
[0dd3a2f]349        assert( member );
[44b5ca0]350        os << std::string( indent + 2, ' ' );
[0dd3a2f]351        member->print( os, indent + 2 );
352        os << std::endl;
[51b7345]353
[0dd3a2f]354        Expression *agg = get_aggregate();
[44b5ca0]355        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
[0dd3a2f]356        if (agg != 0) agg->print(os, indent + 2);
357        Expression::print( os, indent );
[51b7345]358}
359
360
361UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
362
[0dd3a2f]363UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
364                Expression( other ), function( maybeClone( other.function ) ) {
365        cloneAll( other.args, args );
[51b7345]366}
367
368UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
[0dd3a2f]369                Expression( _aname ), function(_function), args(_args) {}
[51b7345]370
371UntypedExpr::~UntypedExpr() {}
372
373void UntypedExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]374        os << std::string( indent, ' ' ) << "Applying untyped: " << std::endl;
[0dd3a2f]375        function->print(os, indent + 4);
[44b5ca0]376        os << std::string( indent, ' ' ) << "...to: " << std::endl;
[0dd3a2f]377        printArgs(os, indent + 4);
378        Expression::print( os, indent );
[51b7345]379}
380
[0dd3a2f]381void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
382        std::list<Expression *>::const_iterator i;
383        for (i = args.begin(); i != args.end(); i++)
384                (*i)->print(os, indent);
[51b7345]385}
386
387NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
388
[0dd3a2f]389NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
[51b7345]390}
391
392NameExpr::~NameExpr() {}
393
394void NameExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]395        os << std::string( indent, ' ' ) << "Name: " << get_name() << std::endl;
[0dd3a2f]396        Expression::print( os, indent );
[51b7345]397}
398
399LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
[0dd3a2f]400                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
401        add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b7345]402}
403
[0dd3a2f]404LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
405                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b7345]406}
407
[a08ba92]408LogicalExpr::~LogicalExpr() {
[0dd3a2f]409        delete arg1;
410        delete arg2;
[51b7345]411}
412
413void LogicalExpr::print( std::ostream &os, int indent )const {
[44b5ca0]414        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
[0dd3a2f]415        arg1->print(os);
416        os << " and ";
417        arg2->print(os);
418        os << std::endl;
419        Expression::print( os, indent );
[51b7345]420}
421
422ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
[0dd3a2f]423                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
[51b7345]424
[0dd3a2f]425ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
426                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
427}
[51b7345]428
429ConditionalExpr::~ConditionalExpr() {
[0dd3a2f]430        delete arg1;
431        delete arg2;
432        delete arg3;
[51b7345]433}
434
435void ConditionalExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]436        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
[0dd3a2f]437        arg1->print( os, indent+2 );
[44b5ca0]438        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
[0dd3a2f]439        arg2->print( os, indent+2 );
[44b5ca0]440        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
[0dd3a2f]441        arg3->print( os, indent+2 );
442        os << std::endl;
443        Expression::print( os, indent );
444}
445
[3be261a]446AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
447
448
[7f5566b]449void AsmExpr::print( std::ostream &os, int indent ) const {
450        os << "Asm Expression: " << std::endl;
451        if ( inout ) inout->print( os, indent + 2 );
452        if ( constraint ) constraint->print( os, indent + 2 );
453        if ( operand ) operand->print( os, indent + 2 );
454}
455
[3be261a]456UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
457
458UntypedValofExpr::~UntypedValofExpr() { delete body; }
459
[0dd3a2f]460void UntypedValofExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]461        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
[0dd3a2f]462        if ( get_body() != 0 )
463                get_body()->print( os, indent + 2 );
464}
465
[3be261a]466
[630a82a]467CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
468        add_result( type->clone() );
469}
470
471CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
472
473CompoundLiteralExpr::~CompoundLiteralExpr() {
474        delete initializer;
475        delete type;
476}
477
478void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
479        os << "Compound Literal Expression: " << std::endl;
480        if ( type ) type->print( os, indent + 2 );
481        if ( initializer ) initializer->print( os, indent + 2 );
482}
483
[3be261a]484
[baf7fee]485std::ostream & operator<<( std::ostream & out, Expression * expr ) {
486        expr->print( out );
487        return out;
488}
489
[0dd3a2f]490// Local Variables: //
491// tab-width: 4 //
492// mode: c++ //
493// compile-command: "make install" //
494// End: //
Note: See TracBrowser for help on using the repository browser.