Changeset 90152a4 for src/SynTree/Expression.cc
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/Expression.cc
rf9feab8 r90152a4 50 50 } 51 51 52 void Expression::spliceInferParams( Expression * other ) { 53 if ( ! other ) return; 54 for ( auto p : other->inferParams ) { 55 inferParams[p.first] = std::move( p.second ); 56 } 57 } 58 52 59 Expression::~Expression() { 53 60 delete env; … … 81 88 constant.print( os ); 82 89 Expression::print( os, indent ); 90 } 91 92 long long int ConstantExpr::intValue() const { 93 if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) { 94 if ( basicType->isInteger() ) { 95 return get_constant()->get_ival(); 96 } 97 } else if ( dynamic_cast< OneType * >( result ) ) { 98 return 1; 99 } else if ( dynamic_cast< ZeroType * >( result ) ) { 100 return 0; 101 } 102 SemanticError( this, "Constant expression of non-integral type " ); 83 103 } 84 104 … … 95 115 // assert( inst->baseEnum ); 96 116 // EnumDecl * decl = inst->baseEnum; 97 // for ( Declaration * member : decl->members ) { 98 // if ( member == _var ) { 99 // type->set_lvalue( false ); 100 // } 117 // long long int value; 118 // if ( decl->valueOf( var, value ) ) { 119 // type->set_lvalue( false ); 101 120 // } 102 121 // } … … 259 278 } 260 279 261 CastExpr::CastExpr( Expression *arg _, Type *toType ) : Expression(), arg(arg_) {280 CastExpr::CastExpr( Expression *arg, Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) { 262 281 set_result(toType); 263 282 } 264 283 265 CastExpr::CastExpr( Expression *arg _ ) : Expression(), arg(arg_) {284 CastExpr::CastExpr( Expression *arg, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) { 266 285 set_result( new VoidType( Type::Qualifiers() ) ); 267 286 } 268 287 269 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {288 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) { 270 289 } 271 290 … … 287 306 } 288 307 308 KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) { 309 } 310 311 KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) { 312 } 313 314 KeywordCastExpr::~KeywordCastExpr() { 315 delete arg; 316 } 317 318 const std::string & KeywordCastExpr::targetString() const { 319 static const std::string targetStrs[] = { 320 "coroutine", "thread", "monitor" 321 }; 322 static_assert( 323 (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS), 324 "Each KeywordCastExpr::Target should have a corresponding string representation" 325 ); 326 return targetStrs[(unsigned long)target]; 327 } 328 329 void KeywordCastExpr::print( std::ostream &os, Indenter indent ) const { 330 os << "Keyword Cast of:" << std::endl << indent+1; 331 arg->print(os, indent+1); 332 os << std::endl << indent << "... to: "; 333 os << targetString(); 334 Expression::print( os, indent ); 335 } 336 289 337 VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { 290 338 set_result(toType); … … 333 381 } 334 382 335 namespace {336 TypeSubstitution makeSub( Type * t ) {337 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {338 return makeSub( refType->get_base() );339 } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {340 return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );341 } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {342 return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );343 } else {344 assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() );345 }346 }347 }348 349 350 383 MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) : 351 384 Expression(), member(member), aggregate(aggregate) { 352 385 assert( member ); 353 386 assert( aggregate ); 354 355 TypeSubstitution sub( makeSub( aggregate->get_result() ) ); 387 assert( aggregate->result ); 388 389 TypeSubstitution sub = aggregate->result->genericSubstitution(); 356 390 Type * res = member->get_type()->clone(); 357 391 sub.apply( res ); … … 403 437 } else { 404 438 // references have been removed, in which case dereference returns an lvalue of the base type. 405 ret-> get_result()->set_lvalue( true );439 ret->result->set_lvalue( true ); 406 440 } 407 441 } … … 585 619 586 620 StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { 587 assert( statements ); 588 std::list< Statement * > & body = statements->get_kids(); 589 if ( ! body.empty() ) { 590 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { 591 set_result( maybeClone( exprStmt->get_expr()->get_result() ) ); 592 } 593 } 594 // ensure that StmtExpr has a result type 595 if ( ! result ) { 596 set_result( new VoidType( Type::Qualifiers() ) ); 597 } 621 computeResult(); 598 622 } 599 623 StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { … … 605 629 deleteAll( dtors ); 606 630 deleteAll( returnDecls ); 631 } 632 void StmtExpr::computeResult() { 633 assert( statements ); 634 std::list< Statement * > & body = statements->kids; 635 delete result; 636 result = nullptr; 637 if ( ! returnDecls.empty() ) { 638 // prioritize return decl for result type, since if a return decl exists, then 639 // the StmtExpr is currently in an intermediate state where the body will always 640 // give a void result type. 641 result = returnDecls.front()->get_type()->clone(); 642 } else if ( ! body.empty() ) { 643 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { 644 result = maybeClone( exprStmt->expr->result ); 645 } 646 } 647 // ensure that StmtExpr has a result type 648 if ( ! result ) { 649 result = new VoidType( Type::Qualifiers() ); 650 } 607 651 } 608 652 void StmtExpr::print( std::ostream &os, Indenter indent ) const { … … 688 732 } 689 733 734 DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) { 735 assert( expr->result ); 736 result = expr->result->clone(); 737 } 738 DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {} 739 DeletedExpr::~DeletedExpr() { 740 delete expr; 741 } 742 743 void DeletedExpr::print( std::ostream & os, Indenter indent ) const { 744 os << "Deleted Expression" << std::endl << indent+1; 745 expr->print( os, indent+1 ); 746 os << std::endl << indent+1 << "... deleted by: "; 747 deleteStmt->print( os, indent+1 ); 748 } 749 750 751 DefaultArgExpr::DefaultArgExpr( Expression * expr ) : expr( expr ) { 752 assert( expr->result ); 753 result = expr->result->clone(); 754 } 755 DefaultArgExpr::DefaultArgExpr( const DefaultArgExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ) {} 756 DefaultArgExpr::~DefaultArgExpr() { 757 delete expr; 758 } 759 760 void DefaultArgExpr::print( std::ostream & os, Indenter indent ) const { 761 os << "Default Argument Expression" << std::endl << indent+1; 762 expr->print( os, indent+1 ); 763 } 764 765 GenericExpr::Association::Association( Type * type, Expression * expr ) : type( type ), expr( expr ), isDefault( false ) {} 766 GenericExpr::Association::Association( Expression * expr ) : type( nullptr ), expr( expr ), isDefault( true ) {} 767 GenericExpr::Association::Association( const Association & other ) : type( maybeClone( other.type ) ), expr( maybeClone( other.expr ) ), isDefault( other.isDefault ) {} 768 GenericExpr::Association::~Association() { 769 delete type; 770 delete expr; 771 } 772 773 GenericExpr::GenericExpr( Expression * control, const std::list<Association> & assoc ) : Expression(), control( control ), associations( assoc ) {} 774 GenericExpr::GenericExpr( const GenericExpr & other ) : Expression(other), control( maybeClone( other.control ) ), associations( other.associations ) { 775 } 776 GenericExpr::~GenericExpr() { 777 delete control; 778 } 779 780 void GenericExpr::print( std::ostream & os, Indenter indent ) const { 781 os << "C11 _Generic Expression" << std::endl << indent+1; 782 control->print( os, indent+1 ); 783 os << std::endl << indent+1 << "... with associations: " << std::endl; 784 for ( const Association & assoc : associations ) { 785 os << indent+1; 786 if (assoc.isDefault) { 787 os << "... default: "; 788 assoc.expr->print( os, indent+1 ); 789 } else { 790 os << "... type: "; 791 assoc.type->print( os, indent+1 ); 792 os << std::endl << indent+1 << "... expression: "; 793 assoc.expr->print( os, indent+1 ); 794 os << std::endl; 795 } 796 os << std::endl; 797 } 798 } 799 690 800 // Local Variables: // 691 801 // tab-width: 4 //
Note:
See TracChangeset
for help on using the changeset viewer.