Changeset 9cdfb4d0 for src/SynTree
- Timestamp:
- Jan 22, 2018, 3:09:01 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, stuck-waitfor-destruct, with_gc
- Children:
- e23d20b
- Parents:
- 326cd2b (diff), 4bf3b2b (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:
-
- 8 edited
-
AggregateDecl.cc (modified) (1 diff)
-
Declaration.h (modified) (1 diff)
-
Expression.cc (modified) (4 diffs)
-
Expression.h (modified) (1 diff)
-
Label.h (modified) (1 diff)
-
Type.h (modified) (1 diff)
-
TypeSubstitution.cc (modified) (3 diffs)
-
TypeSubstitution.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/AggregateDecl.cc
r326cd2b r9cdfb4d0 86 86 std::string TraitDecl::typeString() const { return "trait"; } 87 87 88 namespace { 89 long long int getConstValue( Expression * expr ) { 90 if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( expr ) ) { 91 return getConstValue( castExpr->arg ); 92 } else if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { 93 return constExpr->intValue(); 94 // can be -1, +1, etc. 95 // } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { 96 // if ( untypedExpr-> ) 97 } else { 98 assertf( false, "Unhandled expression type in getConstValue for enumerators: %s", toString( expr ).c_str() ); 99 } 100 } 101 } 102 103 bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) { 104 if ( enumValues.empty() ) { 105 long long int currentValue = 0; 106 for ( Declaration * member : members ) { 107 ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member ); 108 if ( field->init ) { 109 SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init ); 110 currentValue = getConstValue( init->value ); 111 } 112 assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() ); 113 enumValues[ field->name ] = currentValue; 114 ++currentValue; 115 } 116 } 117 if ( enumValues.count( enumerator->name ) ) { 118 value = enumValues[ enumerator->name ]; 119 return true; 120 } 121 return false; 122 } 123 88 124 // Local Variables: // 89 125 // tab-width: 4 // -
src/SynTree/Declaration.h
r326cd2b r9cdfb4d0 319 319 EnumDecl( const EnumDecl &other ) : Parent( other ) {} 320 320 321 bool valueOf( Declaration * enumerator, long long int & value ); 322 321 323 virtual EnumDecl *clone() const override { return new EnumDecl( *this ); } 322 324 virtual void accept( Visitor &v ) override { v.visit( this ); } 323 325 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } 324 326 private: 327 std::map< std::string, long long int > enumValues; 325 328 virtual std::string typeString() const override; 326 329 }; -
src/SynTree/Expression.cc
r326cd2b r9cdfb4d0 83 83 } 84 84 85 long long int ConstantExpr::intValue() const { 86 if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) { 87 if ( basicType->isInteger() ) { 88 return get_constant()->get_ival(); 89 } 90 } else if ( dynamic_cast< OneType * >( result ) ) { 91 return 1; 92 } else if ( dynamic_cast< ZeroType * >( result ) ) { 93 return 0; 94 } 95 throw SemanticError( "Constant expression of non-integral type ", this ); 96 } 97 85 98 VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) { 86 99 assert( var ); … … 95 108 // assert( inst->baseEnum ); 96 109 // EnumDecl * decl = inst->baseEnum; 97 // for ( Declaration * member : decl->members ) { 98 // if ( member == _var ) { 99 // type->set_lvalue( false ); 100 // } 110 // long long int value; 111 // if ( decl->valueOf( var, value ) ) { 112 // type->set_lvalue( false ); 101 113 // } 102 114 // } … … 403 415 } else { 404 416 // references have been removed, in which case dereference returns an lvalue of the base type. 405 ret-> get_result()->set_lvalue( true );417 ret->result->set_lvalue( true ); 406 418 } 407 419 } … … 589 601 if ( ! body.empty() ) { 590 602 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { 591 set_result( maybeClone( exprStmt->get_expr()->get_result() ));603 result = maybeClone( exprStmt->expr->result ); 592 604 } 593 605 } 594 606 // ensure that StmtExpr has a result type 595 607 if ( ! result ) { 596 set_result( new VoidType( Type::Qualifiers()) );608 result = new VoidType( Type::Qualifiers() ); 597 609 } 598 610 } -
src/SynTree/Expression.h
r326cd2b r9cdfb4d0 295 295 296 296 Constant * get_constant() { return & constant; } 297 const Constant * get_constant() const { return & constant; } 297 298 void set_constant( const Constant & newValue ) { constant = newValue; } 299 300 long long int intValue() const; 298 301 299 302 virtual ConstantExpr * clone() const { return new ConstantExpr( * this ); } -
src/SynTree/Label.h
r326cd2b r9cdfb4d0 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/Type.h
r326cd2b r9cdfb4d0 356 356 bool isTtype() const; 357 357 358 bool isUnprototyped() const { return isVarArgs && parameters.size() == 0; } 359 358 360 virtual FunctionType *clone() const override { return new FunctionType( *this ); } 359 361 virtual void accept( Visitor & v ) override { v.visit( this ); } -
src/SynTree/TypeSubstitution.cc
r326cd2b r9cdfb4d0 107 107 108 108 void TypeSubstitution::normalize() { 109 PassVisitor<Substituter> sub( *this, true ); 109 110 do { 110 sub Count = 0;111 freeOnly = true;111 sub.pass.subCount = 0; 112 sub.pass.freeOnly = true; 112 113 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 113 i->second = i->second->acceptMutator( *this);114 i->second = i->second->acceptMutator( sub ); 114 115 } 115 } while ( sub Count );116 } 117 118 Type * TypeSubstitution:: mutate( TypeInstType *inst ) {119 BoundVarsType::const_iterator bound = boundVars.find( inst-> get_name());116 } while ( sub.pass.subCount ); 117 } 118 119 Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) { 120 BoundVarsType::const_iterator bound = boundVars.find( inst->name ); 120 121 if ( bound != boundVars.end() ) return inst; 121 122 122 TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );123 if ( i == typeEnv.end() ) {123 TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() ); 124 if ( i == sub.typeEnv.end() ) { 124 125 return inst; 125 126 } else { 126 /// std::c out<< "found " << inst->get_name() << ", replacing with ";127 /// i->second->print( std::c out);128 /// std::c out<< std::endl;127 /// std::cerr << "found " << inst->get_name() << ", replacing with "; 128 /// i->second->print( std::cerr ); 129 /// std::cerr << std::endl; 129 130 subCount++; 130 Type * newtype = i->second->clone();131 Type * newtype = i->second->clone(); 131 132 newtype->get_qualifiers() |= inst->get_qualifiers(); 132 133 delete inst; … … 135 136 } 136 137 137 Expression * TypeSubstitution:: mutate( NameExpr *nameExpr ) {138 VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name());139 if ( i == varEnv.end() ) {138 Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) { 139 VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); 140 if ( i == sub.varEnv.end() ) { 140 141 return nameExpr; 141 142 } else { … … 146 147 } 147 148 148 template< typename TypeClass > 149 Type *TypeSubstitution::handleType( TypeClass *type ) { 150 ValueGuard<BoundVarsType> oldBoundVars( boundVars ); 149 void TypeSubstitution::Substituter::premutate( Type * type ) { 150 GuardValue( boundVars ); 151 151 // bind type variables from forall-qualifiers 152 152 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());153 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { 154 boundVars.insert( (*tyvar)->name ); 155 155 } // for 156 156 } // if 157 Type *ret = Mutator::mutate( type );158 return ret;159 157 } 160 158 161 159 template< typename TypeClass > 162 Type *TypeSubstitution::handleAggregateType( TypeClass *type ) {163 ValueGuard<BoundVarsType> oldBoundVars( boundVars );160 void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) { 161 GuardValue( boundVars ); 164 162 // bind type variables from forall-qualifiers 165 163 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());164 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { 165 boundVars.insert( (*tyvar)->name ); 168 166 } // for 169 167 // bind type variables from generic type instantiations 170 168 std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); 171 if ( baseParameters && ! type-> get_parameters().empty() ) {169 if ( baseParameters && ! type->parameters.empty() ) { 172 170 for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { 173 boundVars.insert( (*tyvar)-> get_name());171 boundVars.insert( (*tyvar)->name ); 174 172 } // for 175 173 } // if 176 174 } // 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 ); 175 } 176 177 void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) { 178 handleAggregateType( aggregateUseType ); 179 } 180 181 void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) { 182 handleAggregateType( aggregateUseType ); 231 183 } 232 184 -
src/SynTree/TypeSubstitution.h
r326cd2b r9cdfb4d0 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(); … … 64 62 TypeSubstitution *clone() const { return new TypeSubstitution( *this ); } 65 63 private: 66 virtual Type* mutate(TypeInstType *aggregateUseType);67 virtual Expression* mutate(NameExpr *nameExpr);68 64 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); 65 // Mutator that performs the substitution 66 struct Substituter; 87 67 88 68 // TODO: worry about traversing into a forall-qualified function type or type decl with assertions … … 97 77 typedef std::map< std::string, Type* > TypeEnvType; 98 78 typedef std::map< std::string, Expression* > VarEnvType; 99 typedef std::set< std::string > BoundVarsType;100 79 TypeEnvType typeEnv; 101 80 VarEnvType varEnv; 102 BoundVarsType boundVars;103 int subCount;104 bool freeOnly;105 81 }; 106 82 … … 134 110 135 111 template< typename FormalIterator, typename ActualIterator > 136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) 137 { 112 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { 138 113 add( formalBegin, formalEnd, actualBegin ); 139 114 } 115 116 // include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and 117 // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals. 118 #include "Common/PassVisitor.h" 119 120 // definitition must happen after PassVisitor is included so that WithGuards can be used 121 struct TypeSubstitution::Substituter : public WithGuards { 122 Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} 123 124 Type * postmutate( TypeInstType * aggregateUseType ); 125 Expression * postmutate( NameExpr * nameExpr ); 126 127 /// Records type variable bindings from forall-statements 128 void premutate( Type * type ); 129 /// Records type variable bindings from forall-statements and instantiations of generic types 130 template< typename TypeClass > void handleAggregateType( TypeClass * type ); 131 132 void premutate( StructInstType * aggregateUseType ); 133 void premutate( UnionInstType * aggregateUseType ); 134 135 TypeSubstitution & sub; 136 int subCount = 0; 137 bool freeOnly; 138 typedef std::set< std::string > BoundVarsType; 139 BoundVarsType boundVars; 140 }; 140 141 141 142 template< typename SynTreeClass > 142 143 int TypeSubstitution::apply( SynTreeClass *&input ) { 143 144 assert( input ); 144 subCount = 0; 145 freeOnly = false; 146 input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); 145 PassVisitor<Substituter> sub( *this, false ); 146 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); 147 147 assert( input ); 148 /// std::c out<< "substitution result is: ";149 /// newType->print( std::c out);150 /// std::c out<< std::endl;151 return sub Count;148 /// std::cerr << "substitution result is: "; 149 /// newType->print( std::cerr ); 150 /// std::cerr << std::endl; 151 return sub.pass.subCount; 152 152 } 153 153 … … 155 155 int TypeSubstitution::applyFree( SynTreeClass *&input ) { 156 156 assert( input ); 157 subCount = 0; 158 freeOnly = true; 159 input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); 157 PassVisitor<Substituter> sub( *this, true ); 158 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); 160 159 assert( input ); 161 /// std::c out<< "substitution result is: ";162 /// newType->print( std::c out);163 /// std::c out<< std::endl;164 return sub Count;160 /// std::cerr << "substitution result is: "; 161 /// newType->print( std::cerr ); 162 /// std::cerr << std::endl; 163 return sub.pass.subCount; 165 164 } 166 165
Note:
See TracChangeset
for help on using the changeset viewer.