Changeset 90152a4 for src/SynTree
- 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. - Location:
- src/SynTree
- Files:
-
- 2 added
- 4 deleted
- 24 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/AggregateDecl.cc
rf9feab8 r90152a4 86 86 std::string TraitDecl::typeString() const { return "trait"; } 87 87 88 bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) { 89 if ( enumValues.empty() ) { 90 long long int currentValue = 0; 91 for ( Declaration * member : members ) { 92 ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member ); 93 if ( field->init ) { 94 SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init ); 95 auto result = eval( init->value ); 96 if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) ); 97 currentValue = result.first; 98 } 99 assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() ); 100 enumValues[ field->name ] = currentValue; 101 ++currentValue; 102 } 103 } 104 if ( enumValues.count( enumerator->name ) ) { 105 value = enumValues[ enumerator->name ]; 106 return true; 107 } 108 return false; 109 } 110 88 111 // Local Variables: // 89 112 // tab-width: 4 // -
src/SynTree/ApplicationExpr.cc
rf9feab8 r90152a4 49 49 } 50 50 51 ParamEntry::ParamEntry( ParamEntry && other ) : 52 decl( other.decl ), actualType( other.actualType ), formalType( other.formalType ), expr( other.expr ), inferParams( std::move( other.inferParams ) ) { 53 other.actualType = nullptr; 54 other.formalType = nullptr; 55 other.expr = nullptr; 56 } 57 58 ParamEntry & ParamEntry::operator=( ParamEntry && other ) { 59 if ( &other == this ) return *this; 60 delete actualType; 61 delete formalType; 62 delete expr; 63 decl = other.decl; 64 actualType = other.actualType; 65 formalType = other.formalType; 66 expr = other.expr; 67 other.actualType = nullptr; 68 other.formalType = nullptr; 69 other.expr = nullptr; 70 inferParams = std::move( other.inferParams ); 71 return *this; 72 } 73 51 74 ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list<Expression *> & args ) : function( funcExpr ), args( args ) { 52 75 PointerType *pointer = strict_dynamic_cast< PointerType* >( funcExpr->get_result() ); -
src/SynTree/Attribute.cc
rf9feab8 r90152a4 15 15 16 16 #include <ostream> // for operator<<, ostream, basic_ostream, endl 17 #include <set> 17 18 18 19 #include "Attribute.h" … … 21 22 22 23 Attribute::Attribute( const Attribute &other ) : name( other.name ) { 23 24 cloneAll( other.parameters, parameters ); 24 25 } 25 26 26 27 Attribute::~Attribute() { 27 deleteAll( parameters ); 28 deleteAll( parameters ); 29 } 30 31 bool Attribute::isValidOnFuncParam() const { 32 // attributes such as aligned, cleanup, etc. produce GCC errors when they appear 33 // on function parameters. Maintain here a whitelist of attribute names that are 34 // allowed to appear on parameters. 35 static std::set< std::string > valid = { 36 "noreturn", "unused" 37 }; 38 return valid.count( normalizedName() ); 39 } 40 41 std::string Attribute::normalizedName() const { 42 // trim beginning/ending _, convert to lowercase 43 auto begin = name.find_first_not_of('_'); 44 auto end = name.find_last_not_of('_'); 45 if (begin == std::string::npos || end == std::string::npos) return ""; 46 std::string ret; 47 ret.reserve( end-begin+1 ); 48 std::transform( &name[begin], &name[end+1], back_inserter( ret ), tolower ); 49 return ret; 28 50 } 29 51 30 52 void Attribute::print( std::ostream &os, Indenter indent ) const { 31 32 53 using std::endl; 54 using std::string; 33 55 34 35 36 37 38 39 40 56 if ( ! empty() ) { 57 os << "Attribute with name: " << name; 58 if ( ! parameters.empty() ) { 59 os << " with parameters: " << endl; 60 printAll( parameters, os, indent+1 ); 61 } 62 } 41 63 } 42 64 -
src/SynTree/Attribute.h
rf9feab8 r90152a4 43 43 bool empty() const { return name == ""; } 44 44 45 std::string normalizedName() const; 46 47 /// true if this attribute is allowed to appear attached to a function parameter 48 bool isValidOnFuncParam() const; 49 45 50 Attribute * clone() const override { return new Attribute( *this ); } 46 51 virtual void accept( Visitor & v ) override { v.visit( this ); } -
src/SynTree/BasicType.cc
rf9feab8 r90152a4 55 55 case DoubleImaginary: 56 56 case LongDoubleImaginary: 57 case Float80: 58 case Float128: 57 59 return false; 58 60 case NUMBER_OF_BASIC_TYPES: -
src/SynTree/CompoundStmt.cc
rf9feab8 r90152a4 23 23 #include "Statement.h" // for CompoundStmt, Statement, DeclStmt 24 24 #include "SynTree/Label.h" // for Label 25 #include "SynTree/ VarExprReplacer.h" // for VarExprReplacer, VarExprReplace...25 #include "SynTree/DeclReplacer.h" // for DeclReplacer 26 26 27 27 using std::string; … … 49 49 // recursively execute this routine. There may be more efficient ways of doing 50 50 // this. 51 VarExprReplacer::DeclMap declMap;51 DeclReplacer::DeclMap declMap; 52 52 std::list< Statement * >::const_iterator origit = other.kids.begin(); 53 53 for ( Statement * s : kids ) { … … 59 59 DeclarationWithType * origdwt = strict_dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ); 60 60 assert( dwt->get_name() == origdwt->get_name() ); 61 declMap[ origdwt ] = new VariableExpr( dwt );61 declMap[ origdwt ] = dwt; 62 62 } else assert( ! dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ) ); 63 63 } else assert( ! dynamic_cast< DeclStmt * > ( s ) ); 64 64 } 65 65 if ( ! declMap.empty() ) { 66 VarExprReplacer replacer( declMap ); 67 acceptMutator( replacer ); 66 DeclReplacer::replace( this, declMap ); 68 67 } 69 68 } -
src/SynTree/Declaration.cc
rf9feab8 r90152a4 81 81 82 82 83 StaticAssertDecl::StaticAssertDecl( Expression * condition, ConstantExpr * message ) : Declaration( "", Type::StorageClasses(), LinkageSpec::C ), condition( condition ), message( message ) { 84 } 85 86 StaticAssertDecl::StaticAssertDecl( const StaticAssertDecl & other ) : Declaration( other ), condition( maybeClone( other.condition ) ), message( maybeClone( other.message ) ) { 87 } 88 89 StaticAssertDecl::~StaticAssertDecl() { 90 delete condition; 91 delete message; 92 } 93 94 void StaticAssertDecl::print( std::ostream &os, Indenter indent ) const { 95 os << "Static Assert with condition: "; 96 condition->print( os, indent+1 ); 97 os << std::endl << indent << "and message: "; 98 message->print( os, indent+1 ); 99 os << std::endl; 100 } 101 102 void StaticAssertDecl::printShort( std::ostream &os, Indenter indent ) const { 103 print( os, indent ); 104 } 105 106 83 107 // Local Variables: // 84 108 // tab-width: 4 // -
src/SynTree/Declaration.h
rf9feab8 r90152a4 84 84 Expression *asmName; 85 85 std::list< Attribute * > attributes; 86 bool isDeleted = false; 86 87 87 88 DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs ); … … 151 152 FunctionType *type; 152 153 CompoundStmt *statements; 154 std::list< Expression * > withExprs; 153 155 154 156 FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, … … 200 202 typedef NamedTypeDecl Parent; 201 203 public: 202 enum Kind { Dtype, Ftype, Ttype };204 enum Kind { Dtype, Ftype, Ttype, NUMBER_OF_KINDS }; 203 205 204 206 Type * init; … … 244 246 typedef NamedTypeDecl Parent; 245 247 public: 246 TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); } 248 TypedefDecl( const std::string &name, CodeLocation location, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) 249 : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; } 250 247 251 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} 248 252 … … 262 266 bool body; 263 267 std::list< Attribute * > attributes; 268 AggregateDecl * parent = nullptr; 264 269 265 270 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ); … … 319 324 EnumDecl( const EnumDecl &other ) : Parent( other ) {} 320 325 326 bool valueOf( Declaration * enumerator, long long int & value ); 327 321 328 virtual EnumDecl *clone() const override { return new EnumDecl( *this ); } 322 329 virtual void accept( Visitor &v ) override { v.visit( this ); } 323 330 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } 324 331 private: 332 std::map< std::string, long long int > enumValues; 325 333 virtual std::string typeString() const override; 326 334 }; … … 355 363 virtual void accept( Visitor &v ) override { v.visit( this ); } 356 364 virtual AsmDecl *acceptMutator( Mutator &m ) override { return m.mutate( this ); } 365 virtual void print( std::ostream &os, Indenter indent = {} ) const override; 366 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; 367 }; 368 369 class StaticAssertDecl : public Declaration { 370 public: 371 Expression * condition; 372 ConstantExpr * message; // string literal 373 374 StaticAssertDecl( Expression * condition, ConstantExpr * message ); 375 StaticAssertDecl( const StaticAssertDecl & other ); 376 virtual ~StaticAssertDecl(); 377 378 virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); } 379 virtual void accept( Visitor &v ) override { v.visit( this ); } 380 virtual StaticAssertDecl * acceptMutator( Mutator &m ) override { return m.mutate( this ); } 357 381 virtual void print( std::ostream &os, Indenter indent = {} ) const override; 358 382 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; -
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 // -
src/SynTree/Expression.h
rf9feab8 r90152a4 41 41 ParamEntry( UniqueId decl, Type * actualType, Type * formalType, Expression* expr ): decl( decl ), actualType( actualType ), formalType( formalType ), expr( expr ), inferParams( new InferredParams ) {} 42 42 ParamEntry( const ParamEntry & other ); 43 ParamEntry( ParamEntry && other ); 43 44 ~ParamEntry(); 44 45 ParamEntry & operator=( const ParamEntry & other ); 46 ParamEntry & operator=( ParamEntry && other ); 45 47 46 48 UniqueId decl; … … 74 76 InferredParams & get_inferParams() { return inferParams; } 75 77 78 // move other's inferParams to this 79 void spliceInferParams( Expression * other ); 80 76 81 virtual Expression * clone() const override = 0; 77 82 virtual void accept( Visitor & v ) override = 0; … … 188 193 public: 189 194 Expression * arg; 190 191 CastExpr( Expression * arg ); 192 CastExpr( Expression * arg, Type * toType ); 195 bool isGenerated = true; // whether this cast appeared in the source program 196 197 CastExpr( Expression * arg, bool isGenerated = true ); 198 CastExpr( Expression * arg, Type * toType, bool isGenerated = true ); 199 CastExpr( Expression * arg, void * ) = delete; // prevent accidentally passing pointers for isGenerated in the first constructor 193 200 CastExpr( const CastExpr & other ); 194 201 virtual ~CastExpr(); … … 198 205 199 206 virtual CastExpr * clone() const { return new CastExpr( * this ); } 207 virtual void accept( Visitor & v ) { v.visit( this ); } 208 virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); } 209 virtual void print( std::ostream & os, Indenter indent = {} ) const; 210 }; 211 212 /// KeywordCastExpr represents a cast to 'keyword types', e.g. (thread &)t 213 class KeywordCastExpr : public Expression { 214 public: 215 Expression * arg; 216 enum Target { 217 Coroutine, Thread, Monitor, NUMBER_OF_TARGETS 218 } target; 219 220 KeywordCastExpr( Expression * arg, Target target ); 221 KeywordCastExpr( const KeywordCastExpr & other ); 222 virtual ~KeywordCastExpr(); 223 224 const std::string & targetString() const; 225 226 virtual KeywordCastExpr * clone() const { return new KeywordCastExpr( * this ); } 200 227 virtual void accept( Visitor & v ) { v.visit( this ); } 201 228 virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); } … … 295 322 296 323 Constant * get_constant() { return & constant; } 324 const Constant * get_constant() const { return & constant; } 297 325 void set_constant( const Constant & newValue ) { constant = newValue; } 326 327 long long int intValue() const; 298 328 299 329 virtual ConstantExpr * clone() const { return new ConstantExpr( * this ); } … … 725 755 StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; } 726 756 757 // call to set the result type of this StmtExpr based on its body 758 void computeResult(); 759 727 760 std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; } 728 761 std::list< Expression * > & get_dtors() { return dtors; } … … 816 849 }; 817 850 851 /// expression that contains a deleted identifier - should never make it past the resolver. 852 class DeletedExpr : public Expression { 853 public: 854 Expression * expr; 855 BaseSyntaxNode * deleteStmt; 856 857 DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ); 858 DeletedExpr( const DeletedExpr & other ); 859 ~DeletedExpr(); 860 861 virtual DeletedExpr * clone() const { return new DeletedExpr( * this ); } 862 virtual void accept( Visitor & v ) { v.visit( this ); } 863 virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); } 864 virtual void print( std::ostream & os, Indenter indent = {} ) const; 865 }; 866 867 /// expression wrapping the use of a default argument - should never make it past the resolver. 868 class DefaultArgExpr : public Expression { 869 public: 870 Expression * expr; 871 872 DefaultArgExpr( Expression * expr ); 873 DefaultArgExpr( const DefaultArgExpr & other ); 874 ~DefaultArgExpr(); 875 876 virtual DefaultArgExpr * clone() const { return new DefaultArgExpr( * this ); } 877 virtual void accept( Visitor & v ) { v.visit( this ); } 878 virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); } 879 virtual void print( std::ostream & os, Indenter indent = {} ) const; 880 }; 881 882 /// C11 _Generic expression 883 class GenericExpr : public Expression { 884 public: 885 struct Association { 886 Type * type = nullptr; 887 Expression * expr = nullptr; 888 bool isDefault = false; 889 890 Association( Type * type, Expression * expr ); 891 Association( Expression * expr ); 892 Association( const Association & other ); 893 Association & operator=( const Association & other ) = delete; // at the moment this isn't used, and I don't want to implement it 894 ~Association(); 895 }; 896 897 Expression * control; 898 std::list<Association> associations; 899 900 GenericExpr( Expression * control, const std::list<Association> & assoc ); 901 GenericExpr( const GenericExpr & other ); 902 virtual ~GenericExpr(); 903 904 virtual GenericExpr * clone() const { return new GenericExpr( * this ); } 905 virtual void accept( Visitor & v ) { v.visit( this ); } 906 virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); } 907 virtual void print( std::ostream & os, Indenter indent = {} ) const; 908 }; 909 818 910 // Local Variables: // 819 911 // tab-width: 4 // -
src/SynTree/FunctionDecl.cc
rf9feab8 r90152a4 26 26 #include "Statement.h" // for CompoundStmt 27 27 #include "Type.h" // for Type, FunctionType, Type::FuncSpecif... 28 #include " VarExprReplacer.h"28 #include "DeclReplacer.h" 29 29 30 30 extern bool translation_unit_nomain; … … 41 41 : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) { 42 42 43 VarExprReplacer::DeclMap declMap;43 DeclReplacer::DeclMap declMap; 44 44 for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) { 45 declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p));45 declMap[ std::get<0>(p) ] = std::get<1>(p); 46 46 } 47 47 for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) { 48 declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p));48 declMap[ std::get<0>(p) ] = std::get<1>(p); 49 49 } 50 50 if ( ! declMap.empty() ) { 51 VarExprReplacer replacer( declMap ); 52 acceptMutator( replacer ); 51 DeclReplacer::replace( this, declMap ); 53 52 } 53 cloneAll( other.withExprs, withExprs ); 54 54 } 55 55 … … 57 57 delete type; 58 58 delete statements; 59 deleteAll( withExprs ); 59 60 } 60 61 -
src/SynTree/Label.h
rf9feab8 r90152a4 33 33 std::list< Attribute * >& get_attributes() { return attributes; } 34 34 35 operator std::string() { return name; }35 operator std::string() const { return name; } 36 36 bool empty() { return name.empty(); } 37 37 private: -
src/SynTree/Mutator.h
rf9feab8 r90152a4 22 22 class Mutator { 23 23 protected: 24 Mutator() ;25 virtual ~Mutator() ;24 Mutator() = default; 25 virtual ~Mutator() = default; 26 26 public: 27 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ); 28 virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ); 29 virtual Declaration * mutate( StructDecl * aggregateDecl ); 30 virtual Declaration * mutate( UnionDecl * aggregateDecl ); 31 virtual Declaration * mutate( EnumDecl * aggregateDecl ); 32 virtual Declaration * mutate( TraitDecl * aggregateDecl ); 33 virtual Declaration * mutate( TypeDecl * typeDecl ); 34 virtual Declaration * mutate( TypedefDecl * typeDecl ); 35 virtual AsmDecl * mutate( AsmDecl * asmDecl ); 27 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) = 0; 28 virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ) = 0; 29 virtual Declaration * mutate( StructDecl * aggregateDecl ) = 0; 30 virtual Declaration * mutate( UnionDecl * aggregateDecl ) = 0; 31 virtual Declaration * mutate( EnumDecl * aggregateDecl ) = 0; 32 virtual Declaration * mutate( TraitDecl * aggregateDecl ) = 0; 33 virtual Declaration * mutate( TypeDecl * typeDecl ) = 0; 34 virtual Declaration * mutate( TypedefDecl * typeDecl ) = 0; 35 virtual AsmDecl * mutate( AsmDecl * asmDecl ) = 0; 36 virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) = 0; 36 37 37 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ); 38 virtual Statement * mutate( ExprStmt * exprStmt ); 39 virtual Statement * mutate( AsmStmt * asmStmt ); 40 virtual Statement * mutate( IfStmt * ifStmt ); 41 virtual Statement * mutate( WhileStmt * whileStmt ); 42 virtual Statement * mutate( ForStmt * forStmt ); 43 virtual Statement * mutate( SwitchStmt * switchStmt ); 44 virtual Statement * mutate( CaseStmt * caseStmt ); 45 virtual Statement * mutate( BranchStmt * branchStmt ); 46 virtual Statement * mutate( ReturnStmt * returnStmt ); 47 virtual Statement * mutate( ThrowStmt * throwStmt ); 48 virtual Statement * mutate( TryStmt * tryStmt ); 49 virtual Statement * mutate( CatchStmt * catchStmt ); 50 virtual Statement * mutate( FinallyStmt * catchStmt ); 51 virtual Statement * mutate( WaitForStmt * waitforStmt ); 52 virtual Statement * mutate( WithStmt * withStmt ); 53 virtual NullStmt * mutate( NullStmt * nullStmt ); 54 virtual Statement * mutate( DeclStmt * declStmt ); 55 virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ); 38 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) = 0; 39 virtual Statement * mutate( ExprStmt * exprStmt ) = 0; 40 virtual Statement * mutate( AsmStmt * asmStmt ) = 0; 41 virtual Statement * mutate( DirectiveStmt * dirStmt ) = 0; 42 virtual Statement * mutate( IfStmt * ifStmt ) = 0; 43 virtual Statement * mutate( WhileStmt * whileStmt ) = 0; 44 virtual Statement * mutate( ForStmt * forStmt ) = 0; 45 virtual Statement * mutate( SwitchStmt * switchStmt ) = 0; 46 virtual Statement * mutate( CaseStmt * caseStmt ) = 0; 47 virtual Statement * mutate( BranchStmt * branchStmt ) = 0; 48 virtual Statement * mutate( ReturnStmt * returnStmt ) = 0; 49 virtual Statement * mutate( ThrowStmt * throwStmt ) = 0; 50 virtual Statement * mutate( TryStmt * tryStmt ) = 0; 51 virtual Statement * mutate( CatchStmt * catchStmt ) = 0; 52 virtual Statement * mutate( FinallyStmt * catchStmt ) = 0; 53 virtual Statement * mutate( WaitForStmt * waitforStmt ) = 0; 54 virtual Statement * mutate( WithStmt * withStmt ) = 0; 55 virtual NullStmt * mutate( NullStmt * nullStmt ) = 0; 56 virtual Statement * mutate( DeclStmt * declStmt ) = 0; 57 virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ) = 0; 56 58 57 virtual Expression* mutate( ApplicationExpr * applicationExpr ); 58 virtual Expression* mutate( UntypedExpr * untypedExpr ); 59 virtual Expression* mutate( NameExpr * nameExpr ); 60 virtual Expression* mutate( AddressExpr * castExpr ); 61 virtual Expression* mutate( LabelAddressExpr * labAddressExpr ); 62 virtual Expression* mutate( CastExpr * castExpr ); 63 virtual Expression* mutate( VirtualCastExpr * castExpr ); 64 virtual Expression* mutate( UntypedMemberExpr * memberExpr ); 65 virtual Expression* mutate( MemberExpr * memberExpr ); 66 virtual Expression* mutate( VariableExpr * variableExpr ); 67 virtual Expression* mutate( ConstantExpr * constantExpr ); 68 virtual Expression* mutate( SizeofExpr * sizeofExpr ); 69 virtual Expression* mutate( AlignofExpr * alignofExpr ); 70 virtual Expression* mutate( UntypedOffsetofExpr * offsetofExpr ); 71 virtual Expression* mutate( OffsetofExpr * offsetofExpr ); 72 virtual Expression* mutate( OffsetPackExpr * offsetPackExpr ); 73 virtual Expression* mutate( AttrExpr * attrExpr ); 74 virtual Expression* mutate( LogicalExpr * logicalExpr ); 75 virtual Expression* mutate( ConditionalExpr * conditionalExpr ); 76 virtual Expression* mutate( CommaExpr * commaExpr ); 77 virtual Expression* mutate( TypeExpr * typeExpr ); 78 virtual Expression* mutate( AsmExpr * asmExpr ); 79 virtual Expression* mutate( ImplicitCopyCtorExpr * impCpCtorExpr ); 80 virtual Expression* mutate( ConstructorExpr * ctorExpr ); 81 virtual Expression* mutate( CompoundLiteralExpr * compLitExpr ); 82 virtual Expression* mutate( RangeExpr * rangeExpr ); 83 virtual Expression* mutate( UntypedTupleExpr * tupleExpr ); 84 virtual Expression* mutate( TupleExpr * tupleExpr ); 85 virtual Expression* mutate( TupleIndexExpr * tupleExpr ); 86 virtual Expression* mutate( TupleAssignExpr * assignExpr ); 87 virtual Expression* mutate( StmtExpr * stmtExpr ); 88 virtual Expression* mutate( UniqueExpr * uniqueExpr ); 89 virtual Expression* mutate( UntypedInitExpr * initExpr ); 90 virtual Expression* mutate( InitExpr * initExpr ); 59 virtual Expression * mutate( ApplicationExpr * applicationExpr ) = 0; 60 virtual Expression * mutate( UntypedExpr * untypedExpr ) = 0; 61 virtual Expression * mutate( NameExpr * nameExpr ) = 0; 62 virtual Expression * mutate( AddressExpr * addrExpr ) = 0; 63 virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) = 0; 64 virtual Expression * mutate( CastExpr * castExpr ) = 0; 65 virtual Expression * mutate( KeywordCastExpr * castExpr ) = 0; 66 virtual Expression * mutate( VirtualCastExpr * castExpr ) = 0; 67 virtual Expression * mutate( UntypedMemberExpr * memberExpr ) = 0; 68 virtual Expression * mutate( MemberExpr * memberExpr ) = 0; 69 virtual Expression * mutate( VariableExpr * variableExpr ) = 0; 70 virtual Expression * mutate( ConstantExpr * constantExpr ) = 0; 71 virtual Expression * mutate( SizeofExpr * sizeofExpr ) = 0; 72 virtual Expression * mutate( AlignofExpr * alignofExpr ) = 0; 73 virtual Expression * mutate( UntypedOffsetofExpr * offsetofExpr ) = 0; 74 virtual Expression * mutate( OffsetofExpr * offsetofExpr ) = 0; 75 virtual Expression * mutate( OffsetPackExpr * offsetPackExpr ) = 0; 76 virtual Expression * mutate( AttrExpr * attrExpr ) = 0; 77 virtual Expression * mutate( LogicalExpr * logicalExpr ) = 0; 78 virtual Expression * mutate( ConditionalExpr * conditionalExpr ) = 0; 79 virtual Expression * mutate( CommaExpr * commaExpr ) = 0; 80 virtual Expression * mutate( TypeExpr * typeExpr ) = 0; 81 virtual Expression * mutate( AsmExpr * asmExpr ) = 0; 82 virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) = 0; 83 virtual Expression * mutate( ConstructorExpr * ctorExpr ) = 0; 84 virtual Expression * mutate( CompoundLiteralExpr * compLitExpr ) = 0; 85 virtual Expression * mutate( RangeExpr * rangeExpr ) = 0; 86 virtual Expression * mutate( UntypedTupleExpr * tupleExpr ) = 0; 87 virtual Expression * mutate( TupleExpr * tupleExpr ) = 0; 88 virtual Expression * mutate( TupleIndexExpr * tupleExpr ) = 0; 89 virtual Expression * mutate( TupleAssignExpr * assignExpr ) = 0; 90 virtual Expression * mutate( StmtExpr * stmtExpr ) = 0; 91 virtual Expression * mutate( UniqueExpr * uniqueExpr ) = 0; 92 virtual Expression * mutate( UntypedInitExpr * initExpr ) = 0; 93 virtual Expression * mutate( InitExpr * initExpr ) = 0; 94 virtual Expression * mutate( DeletedExpr * delExpr ) = 0; 95 virtual Expression * mutate( DefaultArgExpr * argExpr ) = 0; 96 virtual Expression * mutate( GenericExpr * genExpr ) = 0; 91 97 92 virtual Type * mutate( VoidType * basicType ); 93 virtual Type * mutate( BasicType * basicType ); 94 virtual Type * mutate( PointerType * pointerType ); 95 virtual Type * mutate( ArrayType * arrayType ); 96 virtual Type * mutate( ReferenceType * refType ); 97 virtual Type * mutate( FunctionType * functionType ); 98 virtual Type * mutate( StructInstType * aggregateUseType ); 99 virtual Type * mutate( UnionInstType * aggregateUseType ); 100 virtual Type * mutate( EnumInstType * aggregateUseType ); 101 virtual Type * mutate( TraitInstType * aggregateUseType ); 102 virtual Type * mutate( TypeInstType * aggregateUseType ); 103 virtual Type * mutate( TupleType * tupleType ); 104 virtual Type * mutate( TypeofType * typeofType ); 105 virtual Type * mutate( AttrType * attrType ); 106 virtual Type * mutate( VarArgsType * varArgsType ); 107 virtual Type * mutate( ZeroType * zeroType ); 108 virtual Type * mutate( OneType * oneType ); 98 virtual Type * mutate( VoidType * basicType ) = 0; 99 virtual Type * mutate( BasicType * basicType ) = 0; 100 virtual Type * mutate( PointerType * pointerType ) = 0; 101 virtual Type * mutate( ArrayType * arrayType ) = 0; 102 virtual Type * mutate( ReferenceType * refType ) = 0; 103 virtual Type * mutate( QualifiedType * qualType ) = 0; 104 virtual Type * mutate( FunctionType * functionType ) = 0; 105 virtual Type * mutate( StructInstType * aggregateUseType ) = 0; 106 virtual Type * mutate( UnionInstType * aggregateUseType ) = 0; 107 virtual Type * mutate( EnumInstType * aggregateUseType ) = 0; 108 virtual Type * mutate( TraitInstType * aggregateUseType ) = 0; 109 virtual Type * mutate( TypeInstType * aggregateUseType ) = 0; 110 virtual Type * mutate( TupleType * tupleType ) = 0; 111 virtual Type * mutate( TypeofType * typeofType ) = 0; 112 virtual Type * mutate( AttrType * attrType ) = 0; 113 virtual Type * mutate( VarArgsType * varArgsType ) = 0; 114 virtual Type * mutate( ZeroType * zeroType ) = 0; 115 virtual Type * mutate( OneType * oneType ) = 0; 116 virtual Type * mutate( GlobalScopeType * globalType ) = 0; 109 117 110 virtual Designation * mutate( Designation * designation ) ;111 virtual Initializer * mutate( SingleInit * singleInit ) ;112 virtual Initializer * mutate( ListInit * listInit ) ;113 virtual Initializer * mutate( ConstructorInit * ctorInit ) ;118 virtual Designation * mutate( Designation * designation ) = 0 ; 119 virtual Initializer * mutate( SingleInit * singleInit ) = 0 ; 120 virtual Initializer * mutate( ListInit * listInit ) = 0 ; 121 virtual Initializer * mutate( ConstructorInit * ctorInit ) = 0 ; 114 122 115 virtual Subrange * mutate( Subrange * subrange ) ;123 virtual Subrange * mutate( Subrange * subrange ) = 0; 116 124 117 virtual Constant * mutate( Constant * constant ) ;125 virtual Constant * mutate( Constant * constant ) = 0; 118 126 119 virtual Attribute * mutate( Attribute * attribute ) ;127 virtual Attribute * mutate( Attribute * attribute ) = 0; 120 128 121 virtual TypeSubstitution * mutate( TypeSubstitution * sub ); 122 123 private: 124 virtual Declaration * handleAggregateDecl(AggregateDecl * aggregateDecl ); 125 virtual Declaration * handleNamedTypeDecl(NamedTypeDecl * typeDecl ); 126 virtual Type * handleReferenceToType(ReferenceToType * aggregateUseType ); 129 virtual TypeSubstitution * mutate( TypeSubstitution * sub ) = 0; 127 130 }; 128 131 … … 140 143 template< typename Container, typename MutatorType > 141 144 inline void mutateAll( Container &container, MutatorType &mutator ) { 142 SemanticError errors;145 SemanticErrorException errors; 143 146 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 144 147 try { … … 148 151 assert( *i ); 149 152 } // if 150 } catch( SemanticError &e ) { 151 e.set_location( (*i)->location ); 153 } catch( SemanticErrorException &e ) { 152 154 errors.append( e ); 153 155 } // try -
src/SynTree/ReferenceToType.cc
rf9feab8 r90152a4 14 14 // 15 15 16 #include <cassert> // for assert 17 #include <list> // for list, _List_const_iterator, list<>::cons... 18 #include <ostream> // for operator<<, basic_ostream, ostream, endl 19 #include <string> // for string, operator<<, char_traits, operator== 20 21 #include "Common/utility.h" // for printAll, cloneAll, deleteAll 22 #include "Declaration.h" // for StructDecl, UnionDecl, EnumDecl, Declara... 23 #include "Expression.h" // for Expression 24 #include "Type.h" // for TypeInstType, StructInstType, UnionInstType 16 #include <cassert> // for assert 17 #include <list> // for list, _List_const_iterator, list<>::cons... 18 #include <ostream> // for operator<<, basic_ostream, ostream, endl 19 #include <string> // for string, operator<<, char_traits, operator== 20 21 #include "Common/utility.h" // for printAll, cloneAll, deleteAll 22 #include "Declaration.h" // for StructDecl, UnionDecl, EnumDecl, Declara... 23 #include "Expression.h" // for Expression 24 #include "Type.h" // for TypeInstType, StructInstType, UnionInstType 25 #include "TypeSubstitution.h" // for TypeSubstitution 25 26 26 27 class Attribute; … … 49 50 50 51 namespace { 51 void doLookup( const std::list< Declaration * > &members, const std::string &name, std::list< Declaration* > &foundDecls ) {52 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i) {53 if ( (*i)->get_name()== name ) {54 foundDecls.push_back( *i);52 void doLookup( const std::list< Declaration * > & members, const std::string & name, std::list< Declaration* > & foundDecls ) { 53 for ( Declaration * decl : members ) { 54 if ( decl->name == name ) { 55 foundDecls.push_back( decl ); 55 56 } // if 56 57 } // for … … 59 60 60 61 StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) : 61 Parent( tq, baseStruct-> get_name(), attributes ), baseStruct( baseStruct ) {}62 Parent( tq, baseStruct->name, attributes ), baseStruct( baseStruct ) {} 62 63 63 64 std::string StructInstType::typeString() const { return "struct"; } 65 66 const std::list<TypeDecl*>* StructInstType::get_baseParameters() const { 67 if ( ! baseStruct ) return nullptr; 68 return &baseStruct->get_parameters(); 69 } 64 70 65 71 std::list<TypeDecl*>* StructInstType::get_baseParameters() { … … 70 76 bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; } 71 77 72 AggregateDecl * StructInstType::getAggr() { return baseStruct; } 78 AggregateDecl * StructInstType::getAggr() const { return baseStruct; } 79 80 TypeSubstitution StructInstType::genericSubstitution() const { 81 return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); 82 } 73 83 74 84 void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { 75 85 assert( baseStruct ); 76 doLookup( baseStruct-> get_members(), name, foundDecls );86 doLookup( baseStruct->members, name, foundDecls ); 77 87 } 78 88 … … 93 103 94 104 UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) : 95 Parent( tq, baseUnion-> get_name(), attributes ), baseUnion( baseUnion ) {}105 Parent( tq, baseUnion->name, attributes ), baseUnion( baseUnion ) {} 96 106 97 107 std::string UnionInstType::typeString() const { return "union"; } … … 102 112 } 103 113 114 const std::list< TypeDecl * > * UnionInstType::get_baseParameters() const { 115 if ( ! baseUnion ) return nullptr; 116 return &baseUnion->get_parameters(); 117 } 118 104 119 bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; } 105 120 106 AggregateDecl * UnionInstType::getAggr() { return baseUnion; } 121 AggregateDecl * UnionInstType::getAggr() const { return baseUnion; } 122 123 TypeSubstitution UnionInstType::genericSubstitution() const { 124 return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); 125 } 107 126 108 127 void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { 109 128 assert( baseUnion ); 110 doLookup( baseUnion-> get_members(), name, foundDecls );129 doLookup( baseUnion->members, name, foundDecls ); 111 130 } 112 131 … … 133 152 bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; } 134 153 154 AggregateDecl * EnumInstType::getAggr() const { return baseEnum; } 155 135 156 void EnumInstType::print( std::ostream &os, Indenter indent ) const { 136 157 using std::endl; -
src/SynTree/ReferenceType.cc
rf9feab8 r90152a4 16 16 #include "Type.h" 17 17 #include "Expression.h" 18 #include "TypeSubstitution.h" 18 19 #include "Common/utility.h" 19 20 … … 35 36 } 36 37 38 TypeSubstitution ReferenceType::genericSubstitution() const { return base->genericSubstitution(); } 39 37 40 void ReferenceType::print( std::ostream &os, Indenter indent ) const { 38 41 Type::print( os, indent ); -
src/SynTree/Statement.cc
rf9feab8 r90152a4 34 34 Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {} 35 35 36 void Statement::print( std::ostream & os, Indenter ) const {36 void Statement::print( std::ostream & os, Indenter indent ) const { 37 37 if ( ! labels.empty() ) { 38 os << "Labels: {";38 os << indent << "... Labels: {"; 39 39 for ( const Label & l : labels ) { 40 40 os << l << ","; … … 94 94 95 95 96 DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {} 97 98 void DirectiveStmt::print( std::ostream &os, Indenter ) const { 99 os << "GCC Directive:" << directive << endl; 100 } 101 102 96 103 const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" }; 97 104 98 BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticError ) :105 BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) : 99 106 Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) { 100 107 //actually this is a syntactic error signaled by the parser 101 108 if ( type == BranchStmt::Goto && target.empty() ) { 102 throw SemanticError("goto without target");103 } 104 } 105 106 BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticError ) :109 SemanticError( target.get_statement()->location, "goto without target"); 110 } 111 } 112 113 BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) : 107 114 Statement(), computedTarget( computedTarget ), type( type ) { 108 115 if ( type != BranchStmt::Goto || computedTarget == nullptr ) { 109 throw SemanticError("Computed target not valid in branch statement");116 SemanticError( computedTarget->location, "Computed target not valid in branch statement"); 110 117 } 111 118 } … … 201 208 } 202 209 203 CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :210 CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) : 204 211 Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) { 205 if ( isDefault() && condition != 0 ) throw SemanticError("default case with condition: ", condition);212 if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " ); 206 213 } 207 214 … … 223 230 224 231 void CaseStmt::print( std::ostream &os, Indenter indent ) const { 225 if ( isDefault() ) os << "Default ";232 if ( isDefault() ) os << indent << "Default "; 226 233 else { 227 os << "Case ";234 os << indent << "Case "; 228 235 condition->print( os, indent ); 229 236 } // if … … 231 238 232 239 for ( Statement * stmt : stmts ) { 240 os << indent+1; 233 241 stmt->print( os, indent+1 ); 234 242 } 235 243 } 236 244 237 WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):238 Statement(), condition( condition), body( body), i sDoWhile( isDoWhile) {245 WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ): 246 Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) { 239 247 } 240 248 … … 452 460 void WaitForStmt::print( std::ostream &os, Indenter indent ) const { 453 461 os << "Waitfor Statement" << endl; 454 os << indent << "... with block:" << endl << indent+1; 455 // block->print( os, indent + 4 ); 462 indent += 1; 463 for( auto & clause : clauses ) { 464 os << indent << "target function :"; 465 if(clause.target.function) { clause.target.function->print(os, indent + 1); } 466 os << endl << indent << "with arguments :" << endl; 467 for( auto & thing : clause.target.arguments) { 468 if(thing) { thing->print(os, indent + 1); } 469 } 470 os << indent << " with statment :" << endl; 471 if(clause.statement) { clause.statement->print(os, indent + 1); } 472 473 os << indent << " with condition :" << endl; 474 if(clause.condition) { clause.condition->print(os, indent + 1); } 475 } 476 477 os << indent << " timeout of :" << endl; 478 if(timeout.time) { timeout.time->print(os, indent + 1); } 479 480 os << indent << " with statment :" << endl; 481 if(timeout.statement) { timeout.statement->print(os, indent + 1); } 482 483 os << indent << " with condition :" << endl; 484 if(timeout.condition) { timeout.condition->print(os, indent + 1); } 485 486 487 os << indent << " else :" << endl; 488 if(orelse.statement) { orelse.statement->print(os, indent + 1); } 489 490 os << indent << " with condition :" << endl; 491 if(orelse.condition) { orelse.condition->print(os, indent + 1); } 456 492 } 457 493 … … 468 504 void WithStmt::print( std::ostream & os, Indenter indent ) const { 469 505 os << "With statement" << endl; 506 os << indent << "... with expressions: " << endl; 507 printAll( exprs, os, indent+1 ); 470 508 os << indent << "... with statement:" << endl << indent+1; 471 509 stmt->print( os, indent+1 ); … … 476 514 } 477 515 478 void NullStmt::print( std::ostream &os, Indenter ) const {516 void NullStmt::print( std::ostream &os, Indenter indent ) const { 479 517 os << "Null Statement" << endl; 518 Statement::print( os, indent ); 480 519 } 481 520 -
src/SynTree/Statement.h
rf9feab8 r90152a4 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Sep 3 20:46:46 201713 // Update Count : 7 712 // Last Modified On : Thu Mar 8 14:53:02 2018 13 // Update Count : 78 14 14 // 15 15 … … 126 126 }; 127 127 128 class DirectiveStmt : public Statement { 129 public: 130 std::string directive; 131 132 DirectiveStmt( const std::string & ); 133 virtual ~DirectiveStmt(){} 134 135 virtual DirectiveStmt * clone() const { return new DirectiveStmt( *this ); } 136 virtual void accept( Visitor & v ) { v.visit( this ); } 137 virtual Statement * acceptMutator( Mutator & m ) { return m.mutate( this ); } 138 virtual void print( std::ostream & os, Indenter indent = {} ) const; 139 }; 140 128 141 class IfStmt : public Statement { 129 142 public: … … 179 192 std::list<Statement *> stmts; 180 193 181 CaseStmt( Expression *conditions, const std::list<Statement *> &stmts, bool isdef = false ) throw (SemanticError);194 CaseStmt( Expression *conditions, const std::list<Statement *> &stmts, bool isdef = false ) throw (SemanticErrorException); 182 195 CaseStmt( const CaseStmt &other ); 183 196 virtual ~CaseStmt(); … … 207 220 Expression *condition; 208 221 Statement *body; 222 std::list<Statement *> initialization; 209 223 bool isDoWhile; 210 224 211 225 WhileStmt( Expression *condition, 212 Statement *body, bool isDoWhile = false );226 Statement *body, std::list<Statement *> & initialization, bool isDoWhile = false ); 213 227 WhileStmt( const WhileStmt &other ); 214 228 virtual ~WhileStmt(); … … 255 269 class BranchStmt : public Statement { 256 270 public: 257 enum Type { Goto = 0, Break, Continue };271 enum Type { Goto = 0, Break, Continue, FallThrough, FallThroughDefault }; 258 272 259 273 // originalTarget kept for error messages. … … 263 277 Type type; 264 278 265 BranchStmt( Label target, Type ) throw (SemanticError );266 BranchStmt( Expression *computedTarget, Type ) throw (SemanticError );279 BranchStmt( Label target, Type ) throw (SemanticErrorException); 280 BranchStmt( Expression *computedTarget, Type ) throw (SemanticErrorException); 267 281 268 282 Label get_originalTarget() { return originalTarget; } -
src/SynTree/SynTree.h
rf9feab8 r90152a4 38 38 class TypedefDecl; 39 39 class AsmDecl; 40 class StaticAssertDecl; 40 41 41 42 class Statement; … … 43 44 class ExprStmt; 44 45 class AsmStmt; 46 class DirectiveStmt; 45 47 class IfStmt; 46 48 class WhileStmt; … … 68 70 class LabelAddressExpr; 69 71 class CastExpr; 72 class KeywordCastExpr; 70 73 class VirtualCastExpr; 71 74 class MemberExpr; … … 97 100 class UntypedInitExpr; 98 101 class InitExpr; 102 class DeletedExpr; 103 class DefaultArgExpr; 104 class GenericExpr; 99 105 100 106 class Type; … … 104 110 class ArrayType; 105 111 class ReferenceType; 112 class QualifiedType; 106 113 class FunctionType; 107 114 class ReferenceToType; … … 117 124 class ZeroType; 118 125 class OneType; 126 class GlobalScopeType; 119 127 120 128 class Designation; -
src/SynTree/Type.cc
rf9feab8 r90152a4 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Sep 25 15:16:32 201713 // Update Count : 3 812 // Last Modified On : Fri Jun 22 10:17:19 2018 13 // Update Count : 39 14 14 // 15 15 #include "Type.h" 16 16 17 #include "Attribute.h" // for Attribute 18 #include "Common/utility.h" // for cloneAll, deleteAll, printAll 19 #include "InitTweak/InitTweak.h" // for getPointerBase 20 #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode 21 #include "SynTree/Declaration.h" // for TypeDecl 17 #include "Attribute.h" // for Attribute 18 #include "Common/utility.h" // for cloneAll, deleteAll, printAll 19 #include "InitTweak/InitTweak.h" // for getPointerBase 20 #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode 21 #include "SynTree/Declaration.h" // for TypeDecl 22 #include "SynTree/TypeSubstitution.h" // for TypeSubstitution 22 23 23 24 using namespace std; 24 25 25 const char *BasicType::typeNames[ BasicType::NUMBER_OF_BASIC_TYPES] = {26 const char *BasicType::typeNames[] = { 26 27 "_Bool", 27 28 "char", … … 47 48 "__int128", 48 49 "unsigned __int128", 50 "__float80", 51 "__float128" 49 52 }; 53 static_assert( 54 sizeof(BasicType::typeNames)/sizeof(BasicType::typeNames[0]) == BasicType::NUMBER_OF_BASIC_TYPES, 55 "Each basic type name should have a corresponding kind enum value" 56 ); 50 57 51 58 Type::Type( const Qualifiers &tq, const std::list< Attribute * > & attributes ) : tq( tq ), attributes( attributes ) {} … … 62 69 63 70 // These must remain in the same order as the corresponding bit fields. 64 const char * Type::FuncSpecifiersNames[] = { "inline", " fortran", "_Noreturn" };71 const char * Type::FuncSpecifiersNames[] = { "inline", "_Noreturn", "fortran" }; 65 72 const char * Type::StorageClassesNames[] = { "extern", "static", "auto", "register", "_Thread_local" }; 66 73 const char * Type::QualifiersNames[] = { "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic" }; … … 81 88 int Type::referenceDepth() const { return 0; } 82 89 90 TypeSubstitution Type::genericSubstitution() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); } 91 83 92 void Type::print( std::ostream &os, Indenter indent ) const { 84 93 if ( ! forall.empty() ) { … … 96 105 } 97 106 107 108 QualifiedType::QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ) : Type( tq, {} ), parent( parent ), child( child ) { 109 } 110 111 QualifiedType::QualifiedType( const QualifiedType & other ) : Type( other ), parent( maybeClone( other.parent ) ), child( maybeClone( other.child ) ) { 112 } 113 114 QualifiedType::~QualifiedType() { 115 delete parent; 116 delete child; 117 } 118 119 void QualifiedType::print( std::ostream & os, Indenter indent ) const { 120 os << "Qualified Type: " << endl; 121 os << indent+1; 122 parent->print( os, indent+1 ); 123 os << endl << indent+1; 124 child->print( os, indent+1 ); 125 os << endl; 126 Type::print( os, indent+1 ); 127 } 128 129 GlobalScopeType::GlobalScopeType() : Type( Type::Qualifiers(), {} ) {} 130 131 void GlobalScopeType::print( std::ostream & os, Indenter ) const { 132 os << "Global Scope Type" << endl; 133 } 134 135 98 136 // Empty Variable declarations: 99 137 const Type::FuncSpecifiers noFuncSpecifiers; -
src/SynTree/Type.h
rf9feab8 r90152a4 178 178 virtual bool isComplete() const { return true; } 179 179 180 virtual AggregateDecl * getAggr() { assertf( false, "Non-aggregate type: %s", toString( this ).c_str() ); } 180 virtual AggregateDecl * getAggr() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); } 181 182 virtual TypeSubstitution genericSubstitution() const; 181 183 182 184 virtual Type *clone() const = 0; … … 229 231 SignedInt128, 230 232 UnsignedInt128, 233 Float80, 234 Float128, 231 235 NUMBER_OF_BASIC_TYPES 232 236 } kind; … … 311 315 }; 312 316 317 class QualifiedType : public Type { 318 public: 319 Type * parent; 320 Type * child; 321 322 QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ); 323 QualifiedType( const QualifiedType & tq ); 324 virtual ~QualifiedType(); 325 326 virtual QualifiedType *clone() const override { return new QualifiedType( *this ); } 327 virtual void accept( Visitor & v ) override { v.visit( this ); } 328 virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } 329 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 330 }; 331 313 332 class ReferenceType : public Type { 314 333 public: … … 328 347 // the number of values are disallowed. 329 348 virtual unsigned size() const override { return base->size(); } 349 350 virtual TypeSubstitution genericSubstitution() const override; 330 351 331 352 virtual ReferenceType *clone() const override { return new ReferenceType( *this ); } … … 356 377 bool isTtype() const; 357 378 379 bool isUnprototyped() const { return isVarArgs && parameters.size() == 0; } 380 358 381 virtual FunctionType *clone() const override { return new FunctionType( *this ); } 359 382 virtual void accept( Visitor & v ) override { v.visit( this ); } … … 404 427 /// Accesses generic parameters of base struct (NULL if none such) 405 428 std::list<TypeDecl*> * get_baseParameters(); 429 const std::list<TypeDecl*> * get_baseParameters() const; 406 430 407 431 virtual bool isComplete() const override; 408 432 409 virtual AggregateDecl * getAggr() override; 433 virtual AggregateDecl * getAggr() const override; 434 435 virtual TypeSubstitution genericSubstitution() const override; 410 436 411 437 /// Looks up the members of this struct named "name" and places them into "foundDecls". … … 437 463 438 464 /// Accesses generic parameters of base union (NULL if none such) 439 std::list< TypeDecl * > * get_baseParameters(); 465 std::list<TypeDecl*> * get_baseParameters(); 466 const std::list<TypeDecl*> * get_baseParameters() const; 440 467 441 468 virtual bool isComplete() const override; 442 469 443 virtual AggregateDecl * getAggr() override; 470 virtual AggregateDecl * getAggr() const override; 471 472 virtual TypeSubstitution genericSubstitution() const override; 444 473 445 474 /// looks up the members of this union named "name" and places them into "foundDecls" … … 471 500 472 501 virtual bool isComplete() const override; 502 503 virtual AggregateDecl * getAggr() const override; 473 504 474 505 virtual EnumInstType *clone() const override { return new EnumInstType( *this ); } … … 651 682 }; 652 683 684 class GlobalScopeType : public Type { 685 public: 686 GlobalScopeType(); 687 688 virtual GlobalScopeType *clone() const override { return new GlobalScopeType( *this ); } 689 virtual void accept( Visitor & v ) override { v.visit( this ); } 690 virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } 691 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 692 }; 693 653 694 // Local Variables: // 654 695 // tab-width: 4 // -
src/SynTree/TypeSubstitution.cc
rf9feab8 r90152a4 106 106 } 107 107 108 namespace { 109 struct EnvTrimmer { 110 TypeSubstitution * env, * newEnv; 111 EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} 112 void previsit( TypeDecl * tyDecl ) { 113 // transfer known bindings for seen type variables 114 if ( Type * t = env->lookup( tyDecl->name ) ) { 115 newEnv->add( tyDecl->name, t ); 116 } 117 } 118 }; 119 } // namespace 120 121 /// reduce environment to just the parts that are referenced in a given expression 122 TypeSubstitution * TypeSubstitution::newFromExpr( Expression * expr, TypeSubstitution * env ) { 123 if ( env ) { 124 TypeSubstitution * newEnv = new TypeSubstitution(); 125 PassVisitor<EnvTrimmer> trimmer( env, newEnv ); 126 expr->accept( trimmer ); 127 return newEnv; 128 } 129 return nullptr; 130 } 131 108 132 void TypeSubstitution::normalize() { 133 PassVisitor<Substituter> sub( *this, true ); 109 134 do { 110 sub Count = 0;111 freeOnly = true;135 sub.pass.subCount = 0; 136 sub.pass.freeOnly = true; 112 137 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 113 i->second = i->second->acceptMutator( *this);138 i->second = i->second->acceptMutator( sub ); 114 139 } 115 } while ( sub Count );116 } 117 118 Type * TypeSubstitution:: mutate( TypeInstType *inst ) {119 BoundVarsType::const_iterator bound = boundVars.find( inst-> get_name());140 } while ( sub.pass.subCount ); 141 } 142 143 Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) { 144 BoundVarsType::const_iterator bound = boundVars.find( inst->name ); 120 145 if ( bound != boundVars.end() ) return inst; 121 146 122 TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );123 if ( i == typeEnv.end() ) {147 TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() ); 148 if ( i == sub.typeEnv.end() ) { 124 149 return inst; 125 150 } else { 126 /// std::cout << "found " << inst->get_name() << ", replacing with "; 127 /// i->second->print( std::cout ); 128 /// std::cout << std::endl; 151 // cut off infinite loop for the case where a type is bound to itself. 152 // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here. 153 // TODO: investigate preventing type variables from being bound to themselves in the first place. 154 if ( TypeInstType * replacement = dynamic_cast< TypeInstType * >( i->second ) ) { 155 if ( inst->name == replacement->name ) { 156 return inst; 157 } 158 } 159 // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl; 129 160 subCount++; 130 Type * newtype = i->second->clone();161 Type * newtype = i->second->clone(); 131 162 newtype->get_qualifiers() |= inst->get_qualifiers(); 132 163 delete inst; 133 return newtype; 134 } // if 135 } 136 137 Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) { 138 VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); 139 if ( i == varEnv.end() ) { 164 // Note: need to recursively apply substitution to the new type because normalize does not substitute bound vars, but bound vars must be substituted when not in freeOnly mode. 165 return newtype->acceptMutator( *visitor ); 166 } // if 167 } 168 169 Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) { 170 VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); 171 if ( i == sub.varEnv.end() ) { 140 172 return nameExpr; 141 173 } else { … … 146 178 } 147 179 148 template< typename TypeClass > 149 Type *TypeSubstitution::handleType( TypeClass *type ) { 150 ValueGuard<BoundVarsType> oldBoundVars( boundVars ); 180 void TypeSubstitution::Substituter::premutate( Type * type ) { 181 GuardValue( boundVars ); 151 182 // bind type variables from forall-qualifiers 152 183 if ( freeOnly ) { 153 for ( Type::ForallList::const_iterator tyvar = type-> get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {154 boundVars.insert( (*tyvar )->get_name());184 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { 185 boundVars.insert( (*tyvar)->name ); 155 186 } // for 156 187 } // if 157 Type *ret = Mutator::mutate( type );158 return ret;159 188 } 160 189 161 190 template< typename TypeClass > 162 Type *TypeSubstitution::handleAggregateType( TypeClass *type ) {163 ValueGuard<BoundVarsType> oldBoundVars( boundVars );191 void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) { 192 GuardValue( boundVars ); 164 193 // bind type variables from forall-qualifiers 165 194 if ( freeOnly ) { 166 for ( Type::ForallList::const_iterator tyvar = type-> get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {167 boundVars.insert( (*tyvar )->get_name());195 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { 196 boundVars.insert( (*tyvar)->name ); 168 197 } // for 169 198 // bind type variables from generic type instantiations 170 199 std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); 171 if ( baseParameters && ! type-> get_parameters().empty() ) {200 if ( baseParameters && ! type->parameters.empty() ) { 172 201 for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { 173 boundVars.insert( (*tyvar)-> get_name());202 boundVars.insert( (*tyvar)->name ); 174 203 } // for 175 204 } // if 176 205 } // if 177 Type *ret = Mutator::mutate( type ); 178 return ret; 179 } 180 181 Type * TypeSubstitution::mutate( VoidType *voidType ) { 182 return handleType( voidType ); 183 } 184 185 Type * TypeSubstitution::mutate( BasicType *basicType ) { 186 return handleType( basicType ); 187 } 188 189 Type * TypeSubstitution::mutate( PointerType *pointerType ) { 190 return handleType( pointerType ); 191 } 192 193 Type * TypeSubstitution::mutate( ArrayType *arrayType ) { 194 return handleType( arrayType ); 195 } 196 197 Type * TypeSubstitution::mutate( FunctionType *functionType ) { 198 return handleType( functionType ); 199 } 200 201 Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { 202 return handleAggregateType( aggregateUseType ); 203 } 204 205 Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { 206 return handleAggregateType( aggregateUseType ); 207 } 208 209 Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { 210 return handleType( aggregateUseType ); 211 } 212 213 Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) { 214 return handleType( aggregateUseType ); 215 } 216 217 Type * TypeSubstitution::mutate( TupleType *tupleType ) { 218 return handleType( tupleType ); 219 } 220 221 Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) { 222 return handleType( varArgsType ); 223 } 224 225 Type * TypeSubstitution::mutate( ZeroType *zeroType ) { 226 return handleType( zeroType ); 227 } 228 229 Type * TypeSubstitution::mutate( OneType *oneType ) { 230 return handleType( oneType ); 206 } 207 208 void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) { 209 handleAggregateType( aggregateUseType ); 210 } 211 212 void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) { 213 handleAggregateType( aggregateUseType ); 231 214 } 232 215 -
src/SynTree/TypeSubstitution.h
rf9feab8 r90152a4 27 27 #include "SynTree/Declaration.h" // for TypeDecl, Declaration (ptr only) 28 28 #include "SynTree/Expression.h" // for Expression (ptr only), NameExpr (p... 29 #include "SynTree/Mutator.h" // for Mutator30 29 #include "SynTree/Type.h" // for Type, ArrayType (ptr only), BasicT... 31 30 32 class TypeSubstitution : public Mutator { 33 typedef Mutator Parent; 31 class TypeSubstitution { 34 32 public: 35 33 TypeSubstitution(); … … 57 55 void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ); 58 56 57 /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr 58 static TypeSubstitution * newFromExpr( Expression * expr, TypeSubstitution * env ); 59 59 60 void normalize(); 60 61 … … 64 65 TypeSubstitution *clone() const { return new TypeSubstitution( *this ); } 65 66 private: 66 virtual Type* mutate(TypeInstType *aggregateUseType); 67 virtual Expression* mutate(NameExpr *nameExpr); 68 69 /// Records type variable bindings from forall-statements 70 template< typename TypeClass > Type *handleType( TypeClass *type ); 71 /// Records type variable bindings from forall-statements and instantiations of generic types 72 template< typename TypeClass > Type *handleAggregateType( TypeClass *type ); 73 74 virtual Type* mutate(VoidType *basicType); 75 virtual Type* mutate(BasicType *basicType); 76 virtual Type* mutate(PointerType *pointerType); 77 virtual Type* mutate(ArrayType *arrayType); 78 virtual Type* mutate(FunctionType *functionType); 79 virtual Type* mutate(StructInstType *aggregateUseType); 80 virtual Type* mutate(UnionInstType *aggregateUseType); 81 virtual Type* mutate(EnumInstType *aggregateUseType); 82 virtual Type* mutate(TraitInstType *aggregateUseType); 83 virtual Type* mutate(TupleType *tupleType); 84 virtual Type* mutate(VarArgsType *varArgsType); 85 virtual Type* mutate(ZeroType *zeroType); 86 virtual Type* mutate(OneType *oneType); 67 68 // Mutator that performs the substitution 69 struct Substituter; 87 70 88 71 // TODO: worry about traversing into a forall-qualified function type or type decl with assertions … … 97 80 typedef std::map< std::string, Type* > TypeEnvType; 98 81 typedef std::map< std::string, Expression* > VarEnvType; 99 typedef std::set< std::string > BoundVarsType;100 82 TypeEnvType typeEnv; 101 83 VarEnvType varEnv; 102 BoundVarsType boundVars; 103 int subCount; 104 bool freeOnly; 84 85 public: 86 // has to come after declaration of typeEnv 87 auto begin() -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } 88 auto end() -> decltype( typeEnv. end() ) { return typeEnv. end(); } 89 auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } 90 auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); } 105 91 }; 106 92 … … 122 108 } // if 123 109 } else { 124 throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal);110 SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) ); 125 111 } // if 126 112 } else { … … 134 120 135 121 template< typename FormalIterator, typename ActualIterator > 136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) 137 { 122 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { 138 123 add( formalBegin, formalEnd, actualBegin ); 139 124 } 125 126 // include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and 127 // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals. 128 #include "Common/PassVisitor.h" 129 130 // definitition must happen after PassVisitor is included so that WithGuards can be used 131 struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> { 132 Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} 133 134 Type * postmutate( TypeInstType * aggregateUseType ); 135 Expression * postmutate( NameExpr * nameExpr ); 136 137 /// Records type variable bindings from forall-statements 138 void premutate( Type * type ); 139 /// Records type variable bindings from forall-statements and instantiations of generic types 140 template< typename TypeClass > void handleAggregateType( TypeClass * type ); 141 142 void premutate( StructInstType * aggregateUseType ); 143 void premutate( UnionInstType * aggregateUseType ); 144 145 TypeSubstitution & sub; 146 int subCount = 0; 147 bool freeOnly; 148 typedef std::set< std::string > BoundVarsType; 149 BoundVarsType boundVars; 150 }; 140 151 141 152 template< typename SynTreeClass > 142 153 int TypeSubstitution::apply( SynTreeClass *&input ) { 143 154 assert( input ); 144 subCount = 0; 145 freeOnly = false; 146 input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); 147 assert( input ); 148 /// std::cout << "substitution result is: "; 149 /// newType->print( std::cout ); 150 /// std::cout << std::endl; 151 return subCount; 155 PassVisitor<Substituter> sub( *this, false ); 156 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); 157 assert( input ); 158 /// std::cerr << "substitution result is: "; 159 /// newType->print( std::cerr ); 160 /// std::cerr << std::endl; 161 return sub.pass.subCount; 152 162 } 153 163 … … 155 165 int TypeSubstitution::applyFree( SynTreeClass *&input ) { 156 166 assert( input ); 157 subCount = 0; 158 freeOnly = true; 159 input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); 160 assert( input ); 161 /// std::cout << "substitution result is: "; 162 /// newType->print( std::cout ); 163 /// std::cout << std::endl; 164 return subCount; 167 PassVisitor<Substituter> sub( *this, true ); 168 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); 169 assert( input ); 170 /// std::cerr << "substitution result is: "; 171 /// newType->print( std::cerr ); 172 /// std::cerr << std::endl; 173 return sub.pass.subCount; 165 174 } 166 175 -
src/SynTree/Visitor.h
rf9feab8 r90152a4 21 21 class Visitor { 22 22 protected: 23 Visitor() ;24 virtual ~Visitor() ;23 Visitor() = default; 24 virtual ~Visitor() = default; 25 25 public: 26 26 // visit: Default implementation of all functions visits the children 27 27 // of the given syntax node, but performs no other action. 28 28 29 virtual void visit( ObjectDecl * objectDecl ); 30 virtual void visit( FunctionDecl * functionDecl ); 31 virtual void visit( StructDecl * aggregateDecl ); 32 virtual void visit( UnionDecl * aggregateDecl ); 33 virtual void visit( EnumDecl * aggregateDecl ); 34 virtual void visit( TraitDecl * aggregateDecl ); 35 virtual void visit( TypeDecl * typeDecl ); 36 virtual void visit( TypedefDecl * typeDecl ); 37 virtual void visit( AsmDecl * asmDecl ); 29 virtual void visit( ObjectDecl * objectDecl ) = 0; 30 virtual void visit( FunctionDecl * functionDecl ) = 0; 31 virtual void visit( StructDecl * aggregateDecl ) = 0; 32 virtual void visit( UnionDecl * aggregateDecl ) = 0; 33 virtual void visit( EnumDecl * aggregateDecl ) = 0; 34 virtual void visit( TraitDecl * aggregateDecl ) = 0; 35 virtual void visit( TypeDecl * typeDecl ) = 0; 36 virtual void visit( TypedefDecl * typeDecl ) = 0; 37 virtual void visit( AsmDecl * asmDecl ) = 0; 38 virtual void visit( StaticAssertDecl * assertDecl ) = 0; 38 39 39 virtual void visit( CompoundStmt * compoundStmt ); 40 virtual void visit( ExprStmt * exprStmt ); 41 virtual void visit( AsmStmt * asmStmt ); 42 virtual void visit( IfStmt * ifStmt ); 43 virtual void visit( WhileStmt * whileStmt ); 44 virtual void visit( ForStmt * forStmt ); 45 virtual void visit( SwitchStmt * switchStmt ); 46 virtual void visit( CaseStmt * caseStmt ); 47 virtual void visit( BranchStmt * branchStmt ); 48 virtual void visit( ReturnStmt * returnStmt ); 49 virtual void visit( ThrowStmt * throwStmt ); 50 virtual void visit( TryStmt * tryStmt ); 51 virtual void visit( CatchStmt * catchStmt ); 52 virtual void visit( FinallyStmt * finallyStmt ); 53 virtual void visit( WaitForStmt * waitforStmt ); 54 virtual void visit( WithStmt * withStmt ); 55 virtual void visit( NullStmt * nullStmt ); 56 virtual void visit( DeclStmt * declStmt ); 57 virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ); 40 virtual void visit( CompoundStmt * compoundStmt ) = 0; 41 virtual void visit( ExprStmt * exprStmt ) = 0; 42 virtual void visit( AsmStmt * asmStmt ) = 0; 43 virtual void visit( DirectiveStmt * directiveStmt ) = 0; 44 virtual void visit( IfStmt * ifStmt ) = 0; 45 virtual void visit( WhileStmt * whileStmt ) = 0; 46 virtual void visit( ForStmt * forStmt ) = 0; 47 virtual void visit( SwitchStmt * switchStmt ) = 0; 48 virtual void visit( CaseStmt * caseStmt ) = 0; 49 virtual void visit( BranchStmt * branchStmt ) = 0; 50 virtual void visit( ReturnStmt * returnStmt ) = 0; 51 virtual void visit( ThrowStmt * throwStmt ) = 0; 52 virtual void visit( TryStmt * tryStmt ) = 0; 53 virtual void visit( CatchStmt * catchStmt ) = 0; 54 virtual void visit( FinallyStmt * finallyStmt ) = 0; 55 virtual void visit( WaitForStmt * waitforStmt ) = 0; 56 virtual void visit( WithStmt * withStmt ) = 0; 57 virtual void visit( NullStmt * nullStmt ) = 0; 58 virtual void visit( DeclStmt * declStmt ) = 0; 59 virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) = 0; 58 60 59 virtual void visit( ApplicationExpr * applicationExpr ); 60 virtual void visit( UntypedExpr * untypedExpr ); 61 virtual void visit( NameExpr * nameExpr ); 62 virtual void visit( CastExpr * castExpr ); 63 virtual void visit( VirtualCastExpr * castExpr ); 64 virtual void visit( AddressExpr * addressExpr ); 65 virtual void visit( LabelAddressExpr * labAddressExpr ); 66 virtual void visit( UntypedMemberExpr * memberExpr ); 67 virtual void visit( MemberExpr * memberExpr ); 68 virtual void visit( VariableExpr * variableExpr ); 69 virtual void visit( ConstantExpr * constantExpr ); 70 virtual void visit( SizeofExpr * sizeofExpr ); 71 virtual void visit( AlignofExpr * alignofExpr ); 72 virtual void visit( UntypedOffsetofExpr * offsetofExpr ); 73 virtual void visit( OffsetofExpr * offsetofExpr ); 74 virtual void visit( OffsetPackExpr * offsetPackExpr ); 75 virtual void visit( AttrExpr * attrExpr ); 76 virtual void visit( LogicalExpr * logicalExpr ); 77 virtual void visit( ConditionalExpr * conditionalExpr ); 78 virtual void visit( CommaExpr * commaExpr ); 79 virtual void visit( TypeExpr * typeExpr ); 80 virtual void visit( AsmExpr * asmExpr ); 81 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); 82 virtual void visit( ConstructorExpr * ctorExpr ); 83 virtual void visit( CompoundLiteralExpr * compLitExpr ); 84 virtual void visit( RangeExpr * rangeExpr ); 85 virtual void visit( UntypedTupleExpr * tupleExpr ); 86 virtual void visit( TupleExpr * tupleExpr ); 87 virtual void visit( TupleIndexExpr * tupleExpr ); 88 virtual void visit( TupleAssignExpr * assignExpr ); 89 virtual void visit( StmtExpr * stmtExpr ); 90 virtual void visit( UniqueExpr * uniqueExpr ); 91 virtual void visit( UntypedInitExpr * initExpr ); 92 virtual void visit( InitExpr * initExpr ); 61 virtual void visit( ApplicationExpr * applicationExpr ) = 0; 62 virtual void visit( UntypedExpr * untypedExpr ) = 0; 63 virtual void visit( NameExpr * nameExpr ) = 0; 64 virtual void visit( CastExpr * castExpr ) = 0; 65 virtual void visit( KeywordCastExpr * castExpr ) = 0; 66 virtual void visit( VirtualCastExpr * castExpr ) = 0; 67 virtual void visit( AddressExpr * addressExpr ) = 0; 68 virtual void visit( LabelAddressExpr * labAddressExpr ) = 0; 69 virtual void visit( UntypedMemberExpr * memberExpr ) = 0; 70 virtual void visit( MemberExpr * memberExpr ) = 0; 71 virtual void visit( VariableExpr * variableExpr ) = 0; 72 virtual void visit( ConstantExpr * constantExpr ) = 0; 73 virtual void visit( SizeofExpr * sizeofExpr ) = 0; 74 virtual void visit( AlignofExpr * alignofExpr ) = 0; 75 virtual void visit( UntypedOffsetofExpr * offsetofExpr ) = 0; 76 virtual void visit( OffsetofExpr * offsetofExpr ) = 0; 77 virtual void visit( OffsetPackExpr * offsetPackExpr ) = 0; 78 virtual void visit( AttrExpr * attrExpr ) = 0; 79 virtual void visit( LogicalExpr * logicalExpr ) = 0; 80 virtual void visit( ConditionalExpr * conditionalExpr ) = 0; 81 virtual void visit( CommaExpr * commaExpr ) = 0; 82 virtual void visit( TypeExpr * typeExpr ) = 0; 83 virtual void visit( AsmExpr * asmExpr ) = 0; 84 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) = 0; 85 virtual void visit( ConstructorExpr * ctorExpr ) = 0; 86 virtual void visit( CompoundLiteralExpr * compLitExpr ) = 0; 87 virtual void visit( RangeExpr * rangeExpr ) = 0; 88 virtual void visit( UntypedTupleExpr * tupleExpr ) = 0; 89 virtual void visit( TupleExpr * tupleExpr ) = 0; 90 virtual void visit( TupleIndexExpr * tupleExpr ) = 0; 91 virtual void visit( TupleAssignExpr * assignExpr ) = 0; 92 virtual void visit( StmtExpr * stmtExpr ) = 0; 93 virtual void visit( UniqueExpr * uniqueExpr ) = 0; 94 virtual void visit( UntypedInitExpr * initExpr ) = 0; 95 virtual void visit( InitExpr * initExpr ) = 0; 96 virtual void visit( DeletedExpr * delExpr ) = 0; 97 virtual void visit( DefaultArgExpr * argExpr ) = 0; 98 virtual void visit( GenericExpr * genExpr ) = 0; 93 99 94 virtual void visit( VoidType * basicType ); 95 virtual void visit( BasicType * basicType ); 96 virtual void visit( PointerType * pointerType ); 97 virtual void visit( ArrayType * arrayType ); 98 virtual void visit( ReferenceType * refType ); 99 virtual void visit( FunctionType * functionType ); 100 virtual void visit( StructInstType * aggregateUseType ); 101 virtual void visit( UnionInstType * aggregateUseType ); 102 virtual void visit( EnumInstType * aggregateUseType ); 103 virtual void visit( TraitInstType * aggregateUseType ); 104 virtual void visit( TypeInstType * aggregateUseType ); 105 virtual void visit( TupleType * tupleType ); 106 virtual void visit( TypeofType * typeofType ); 107 virtual void visit( AttrType * attrType ); 108 virtual void visit( VarArgsType * varArgsType ); 109 virtual void visit( ZeroType * zeroType ); 110 virtual void visit( OneType * oneType ); 100 virtual void visit( VoidType * basicType ) = 0; 101 virtual void visit( BasicType * basicType ) = 0; 102 virtual void visit( PointerType * pointerType ) = 0; 103 virtual void visit( ArrayType * arrayType ) = 0; 104 virtual void visit( ReferenceType * refType ) = 0; 105 virtual void visit( QualifiedType * qualType ) = 0; 106 virtual void visit( FunctionType * functionType ) = 0; 107 virtual void visit( StructInstType * aggregateUseType ) = 0; 108 virtual void visit( UnionInstType * aggregateUseType ) = 0; 109 virtual void visit( EnumInstType * aggregateUseType ) = 0; 110 virtual void visit( TraitInstType * aggregateUseType ) = 0; 111 virtual void visit( TypeInstType * aggregateUseType ) = 0; 112 virtual void visit( TupleType * tupleType ) = 0; 113 virtual void visit( TypeofType * typeofType ) = 0; 114 virtual void visit( AttrType * attrType ) = 0; 115 virtual void visit( VarArgsType * varArgsType ) = 0; 116 virtual void visit( ZeroType * zeroType ) = 0; 117 virtual void visit( OneType * oneType ) = 0; 118 virtual void visit( GlobalScopeType * globalType ) = 0; 111 119 112 virtual void visit( Designation * designation ) ;113 virtual void visit( SingleInit * singleInit ) ;114 virtual void visit( ListInit * listInit ) ;115 virtual void visit( ConstructorInit * ctorInit ) ;120 virtual void visit( Designation * designation ) = 0; 121 virtual void visit( SingleInit * singleInit ) = 0; 122 virtual void visit( ListInit * listInit ) = 0; 123 virtual void visit( ConstructorInit * ctorInit ) = 0; 116 124 117 virtual void visit( Subrange * subrange ) ;125 virtual void visit( Subrange * subrange ) = 0; 118 126 119 virtual void visit( Constant * constant ) ;127 virtual void visit( Constant * constant ) = 0; 120 128 121 virtual void visit( Attribute * attribute ); 122 private: 123 virtual void handleAggregateDecl( AggregateDecl *aggregateDecl ); 124 virtual void handleNamedTypeDecl( NamedTypeDecl *typeDecl ); 125 virtual void handleReferenceToType( ReferenceToType *aggregateUseType ); 129 virtual void visit( Attribute * attribute ) = 0; 126 130 }; 127 131 … … 135 139 template< typename Container, typename VisitorType > 136 140 inline void acceptAll( Container &container, VisitorType &visitor ) { 137 SemanticError errors;141 SemanticErrorException errors; 138 142 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 139 143 try { … … 141 145 (*i)->accept( visitor ); 142 146 } 143 } catch( SemanticError &e ) { 144 e.set_location( (*i)->location ); 147 } catch( SemanticErrorException &e ) { 145 148 errors.append( e ); 146 149 } -
src/SynTree/module.mk
rf9feab8 r90152a4 46 46 SynTree/TypeDecl.cc \ 47 47 SynTree/Initializer.cc \ 48 SynTree/Visitor.cc \49 SynTree/Mutator.cc \50 48 SynTree/TypeSubstitution.cc \ 51 49 SynTree/Attribute.cc \ 52 SynTree/ VarExprReplacer.cc50 SynTree/DeclReplacer.cc 53 51
Note:
See TracChangeset
for help on using the changeset viewer.