source: src/SynTree/Expression.cc @ f487962

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

even more more refactoring of parser code

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