Changes in src/SynTree/Expression.cc [0f79853:487845d7]
- File:
-
- 1 edited
-
src/SynTree/Expression.cc (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/Expression.cc
r0f79853 r487845d7 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 59 52 Expression::~Expression() { 60 53 delete env; … … 88 81 constant.print( os ); 89 82 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 " );103 83 } 104 84 … … 115 95 // assert( inst->baseEnum ); 116 96 // EnumDecl * decl = inst->baseEnum; 117 // long long int value; 118 // if ( decl->valueOf( var, value ) ) { 119 // type->set_lvalue( false ); 97 // for ( Declaration * member : decl->members ) { 98 // if ( member == _var ) { 99 // type->set_lvalue( false ); 100 // } 120 101 // } 121 102 // } … … 278 259 } 279 260 280 CastExpr::CastExpr( Expression *arg , Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated) {261 CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { 281 262 set_result(toType); 282 263 } 283 264 284 CastExpr::CastExpr( Expression *arg , bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated) {265 CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) { 285 266 set_result( new VoidType( Type::Qualifiers() ) ); 286 267 } 287 268 288 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) , isGenerated( other.isGenerated ){269 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { 289 270 } 290 271 … … 306 287 } 307 288 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 337 289 VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { 338 290 set_result(toType); … … 381 333 } 382 334 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 383 350 MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) : 384 351 Expression(), member(member), aggregate(aggregate) { 385 352 assert( member ); 386 353 assert( aggregate ); 387 assert( aggregate->result ); 388 389 TypeSubstitution sub = aggregate->result->genericSubstitution(); 354 355 TypeSubstitution sub( makeSub( aggregate->get_result() ) ); 390 356 Type * res = member->get_type()->clone(); 391 357 sub.apply( res ); … … 437 403 } else { 438 404 // references have been removed, in which case dereference returns an lvalue of the base type. 439 ret-> result->set_lvalue( true );405 ret->get_result()->set_lvalue( true ); 440 406 } 441 407 } … … 619 585 620 586 StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { 621 computeResult(); 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 } 622 598 } 623 599 StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { … … 629 605 deleteAll( dtors ); 630 606 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, then639 // the StmtExpr is currently in an intermediate state where the body will always640 // 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 type648 if ( ! result ) {649 result = new VoidType( Type::Qualifiers() );650 }651 607 } 652 608 void StmtExpr::print( std::ostream &os, Indenter indent ) const { … … 732 688 } 733 689 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 800 690 // Local Variables: // 801 691 // tab-width: 4 //
Note:
See TracChangeset
for help on using the changeset viewer.