Changeset 93c10de
- Timestamp:
- Nov 24, 2022, 11:01:37 AM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 82a90d4
- Parents:
- 78de1e5
- Location:
- src
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.cpp
r78de1e5 r93c10de 125 125 } 126 126 127 std::ostream & operator<< ( std::ostream & out, const TypeD ecl::Data & data ) {127 std::ostream & operator<< ( std::ostream & out, const TypeData & data ) { 128 128 return out << data.kind << ", " << data.isComplete; 129 129 } -
src/AST/Decl.hpp
r78de1e5 r93c10de 10 10 // Created On : Thu May 9 10:00:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Thu May 5 12:09:00 202213 // Update Count : 3 312 // Last Modified On : Thu Nov 24 9:44:00 2022 13 // Update Count : 34 14 14 // 15 15 … … 191 191 ptr<Type> init; 192 192 193 /// Data extracted from a type decl194 struct Data {195 Kind kind;196 bool isComplete;197 198 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}199 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}200 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}201 Data( const Data & d1, const Data & d2 )202 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}203 204 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }205 bool operator!=( const Data & o ) const { return !(*this == o); }206 };207 208 193 TypeDecl( 209 194 const CodeLocation & loc, const std::string & name, Storage::Classes storage, … … 225 210 }; 226 211 227 std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & ); 212 /// Data extracted from a TypeDecl. 213 struct TypeData { 214 TypeDecl::Kind kind; 215 bool isComplete; 216 217 TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {} 218 TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} 219 TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {} 220 TypeData( const TypeData & d1, const TypeData & d2 ) 221 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} 222 223 bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; } 224 bool operator!=( const TypeData & o ) const { return !(*this == o); } 225 }; 226 227 std::ostream & operator<< ( std::ostream &, const TypeData & ); 228 228 229 229 /// C-style typedef `typedef Foo Bar` -
src/AST/Type.cpp
r78de1e5 r93c10de 10 10 // Created On : Mon May 13 15:00:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Thu Jul 23 14:16:00 202013 // Update Count : 512 // Last Modified On : Thu Nov 24 9:49:00 2022 13 // Update Count : 6 14 14 // 15 15 … … 147 147 // --- TypeInstType 148 148 149 TypeInstType::TypeInstType( const TypeEnvKey & key ) 150 : BaseInstType(key.base->name), base(key.base), kind(key.base->kind), formal_usage(key.formal_usage), expr_id(key.expr_id) {} 151 149 152 bool TypeInstType::operator==( const TypeInstType & other ) const { 150 153 return base == other.base … … 164 167 bool TypeInstType::isComplete() const { return base->sized; } 165 168 166 std::string Type InstType::TypeEnvKey::typeString() const {169 std::string TypeEnvKey::typeString() const { 167 170 return std::string("_") + std::to_string(formal_usage) 168 171 + "_" + std::to_string(expr_id) + "_" + base->name; 169 172 } 170 173 171 bool Type InstType::TypeEnvKey::operator==(172 const Type InstType::TypeEnvKey & other ) const {174 bool TypeEnvKey::operator==( 175 const TypeEnvKey & other ) const { 173 176 return base == other.base 174 177 && formal_usage == other.formal_usage … … 176 179 } 177 180 178 bool Type InstType::TypeEnvKey::operator<(179 const Type InstType::TypeEnvKey & other ) const {181 bool TypeEnvKey::operator<( 182 const TypeEnvKey & other ) const { 180 183 // TypeEnvKey ordering is an arbitrary total ordering. 181 184 // It doesn't mean anything but allows for a sorting. -
src/AST/Type.hpp
r78de1e5 r93c10de 10 10 // Created On : Thu May 9 10:00:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Jul 14 15:54:00 202113 // Update Count : 712 // Last Modified On : Thu Nov 24 9:47:00 2022 13 // Update Count : 8 14 14 // 15 15 … … 390 390 }; 391 391 392 struct TypeEnvKey; 393 392 394 /// instance of named type alias (typedef or variable) 393 395 class TypeInstType final : public BaseInstType { … … 401 403 int expr_id = 0; 402 404 403 // compact representation used for map lookups.404 struct TypeEnvKey {405 const TypeDecl * base = nullptr;406 int formal_usage = 0;407 int expr_id = 0;408 409 TypeEnvKey() = default;410 TypeEnvKey(const TypeDecl * base, int formal_usage = 0, int expr_id = 0)411 : base(base), formal_usage(formal_usage), expr_id(expr_id) {}412 TypeEnvKey(const TypeInstType & inst)413 : base(inst.base), formal_usage(inst.formal_usage), expr_id(inst.expr_id) {}414 std::string typeString() const;415 bool operator==(const TypeEnvKey & other) const;416 bool operator<(const TypeEnvKey & other) const;417 };418 419 405 bool operator==(const TypeInstType & other) const; 420 406 … … 433 419 TypeInstType( const TypeInstType & o ) = default; 434 420 435 TypeInstType( const TypeEnvKey & key ) 436 : BaseInstType(key.base->name), base(key.base), kind(key.base->kind), formal_usage(key.formal_usage), expr_id(key.expr_id) {} 421 TypeInstType( const TypeEnvKey & key ); 437 422 438 423 /// sets `base`, updating `kind` correctly … … 453 438 TypeInstType * clone() const override { return new TypeInstType{ *this }; } 454 439 MUTATE_FRIEND 440 }; 441 442 /// Compact representation of TypeInstType used for map lookups. 443 struct TypeEnvKey { 444 const TypeDecl * base = nullptr; 445 int formal_usage = 0; 446 int expr_id = 0; 447 448 TypeEnvKey() = default; 449 TypeEnvKey(const TypeDecl * base, int formal_usage = 0, int expr_id = 0) 450 : base(base), formal_usage(formal_usage), expr_id(expr_id) {} 451 TypeEnvKey(const TypeInstType & inst) 452 : base(inst.base), formal_usage(inst.formal_usage), expr_id(inst.expr_id) {} 453 std::string typeString() const; 454 bool operator==(const TypeEnvKey & other) const; 455 bool operator<(const TypeEnvKey & other) const; 455 456 }; 456 457 … … 560 561 namespace std { 561 562 template<> 562 struct hash<typename ast::Type InstType::TypeEnvKey> {563 size_t operator() (const ast::Type InstType::TypeEnvKey & x) const {563 struct hash<typename ast::TypeEnvKey> { 564 size_t operator() (const ast::TypeEnvKey & x) const { 564 565 const size_t p = 1000007; 565 566 size_t res = reinterpret_cast<size_t>(x.base); -
src/AST/TypeEnvironment.cpp
r78de1e5 r93c10de 82 82 } 83 83 84 const EqvClass * TypeEnvironment::lookup( const Type InstType::TypeEnvKey & var ) const {84 const EqvClass * TypeEnvironment::lookup( const TypeEnvKey & var ) const { 85 85 for ( ClassList::const_iterator i = env.begin(); i != env.end(); ++i ) { 86 86 if ( i->vars.find( var ) != i->vars.end() ) return &*i; … … 122 122 void TypeEnvironment::writeToSubstitution( TypeSubstitution & sub ) const { 123 123 for ( const auto & clz : env ) { 124 Type InstType::TypeEnvKey clzRep;124 TypeEnvKey clzRep; 125 125 bool first = true; 126 126 for ( const auto & var : clz.vars ) { … … 146 146 struct Occurs : public ast::WithVisitorRef<Occurs> { 147 147 bool result; 148 std::unordered_set< Type InstType::TypeEnvKey > vars;148 std::unordered_set< TypeEnvKey > vars; 149 149 const TypeEnvironment & tenv; 150 150 151 Occurs( const Type InstType::TypeEnvKey & var, const TypeEnvironment & env )151 Occurs( const TypeEnvKey & var, const TypeEnvironment & env ) 152 152 : result( false ), vars(), tenv( env ) { 153 153 if ( const EqvClass * clz = tenv.lookup( var ) ) { … … 170 170 171 171 /// true if `var` occurs in `ty` under `env` 172 bool occurs( const Type * ty, const Type InstType::TypeEnvKey & var, const TypeEnvironment & env ) {172 bool occurs( const Type * ty, const TypeEnvKey & var, const TypeEnvironment & env ) { 173 173 Pass<Occurs> occur{ var, env }; 174 174 maybe_accept( ty, occur ); … … 258 258 namespace { 259 259 /// true if the given type can be bound to the given type variable 260 bool tyVarCompatible( const TypeD ecl::Data & data, const Type * type ) {260 bool tyVarCompatible( const TypeData & data, const Type * type ) { 261 261 switch ( data.kind ) { 262 262 case TypeDecl::Dtype: … … 279 279 280 280 bool TypeEnvironment::bindVar( 281 const TypeInstType * typeInst, const Type * bindTo, const TypeD ecl::Data & data,281 const TypeInstType * typeInst, const Type * bindTo, const TypeData & data, 282 282 AssertionSet & need, AssertionSet & have, const OpenVarSet & open, WidenMode widen, 283 283 const SymbolTable & symtab … … 319 319 320 320 bool TypeEnvironment::bindVarToVar( 321 const TypeInstType * var1, const TypeInstType * var2, TypeD ecl::Data && data,321 const TypeInstType * var1, const TypeInstType * var2, TypeData && data, 322 322 AssertionSet & need, AssertionSet & have, const OpenVarSet & open, 323 323 WidenMode widen, const SymbolTable & symtab … … 457 457 } 458 458 459 TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const Type InstType::TypeEnvKey & var ) {459 TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const TypeEnvKey & var ) { 460 460 for ( ClassList::iterator i = env.begin(); i != env.end(); ++i ) { 461 461 if ( i->vars.count( var ) ) return i; -
src/AST/TypeEnvironment.hpp
r78de1e5 r93c10de 79 79 80 80 /// Set of open variables 81 using OpenVarSet = std::unordered_map< Type InstType::TypeEnvKey, TypeDecl::Data >;81 using OpenVarSet = std::unordered_map< TypeEnvKey, TypeData >; 82 82 83 83 /// Merges one set of open vars into another … … 95 95 /// they bind to. 96 96 struct EqvClass { 97 std::unordered_set< Type InstType::TypeEnvKey > vars;97 std::unordered_set< TypeEnvKey > vars; 98 98 ptr<Type> bound; 99 99 bool allowWidening; 100 TypeD ecl::Data data;100 TypeData data; 101 101 102 102 EqvClass() : vars(), bound(), allowWidening( true ), data() {} … … 111 111 112 112 /// Singleton class constructor from substitution 113 EqvClass( const Type InstType::TypeEnvKey & v, const Type * b )113 EqvClass( const TypeEnvKey & v, const Type * b ) 114 114 : vars{ v }, bound( b ), allowWidening( false ), data( TypeDecl::Dtype, false ) {} 115 115 116 116 /// Single-var constructor (strips qualifiers from bound type) 117 EqvClass( const Type InstType::TypeEnvKey & v, const Type * b, bool w, const TypeDecl::Data & d )117 EqvClass( const TypeEnvKey & v, const Type * b, bool w, const TypeData & d ) 118 118 : vars{ v }, bound( b ), allowWidening( w ), data( d ) { 119 119 reset_qualifiers( bound ); … … 121 121 122 122 /// Double-var constructor 123 EqvClass( const Type InstType::TypeEnvKey & v, const TypeInstType::TypeEnvKey & u, bool w, const TypeDecl::Data & d )123 EqvClass( const TypeEnvKey & v, const TypeEnvKey & u, bool w, const TypeData & d ) 124 124 : vars{ v, u }, bound(), allowWidening( w ), data( d ) {} 125 125 … … 137 137 public: 138 138 /// Finds the equivalence class containing a variable; nullptr for none such 139 const EqvClass * lookup( const Type InstType::TypeEnvKey & var ) const;139 const EqvClass * lookup( const TypeEnvKey & var ) const; 140 140 141 141 /// Add a new equivalence class for each type variable … … 181 181 /// needed. Returns false on failure. 182 182 bool bindVar( 183 const TypeInstType * typeInst, const Type * bindTo, const TypeD ecl::Data & data,183 const TypeInstType * typeInst, const Type * bindTo, const TypeData & data, 184 184 AssertionSet & need, AssertionSet & have, const OpenVarSet & openVars, 185 185 ResolvExpr::WidenMode widen, const SymbolTable & symtab ); … … 188 188 /// classes if needed. Returns false on failure. 189 189 bool bindVarToVar( 190 const TypeInstType * var1, const TypeInstType * var2, TypeD ecl::Data && data,190 const TypeInstType * var1, const TypeInstType * var2, TypeData && data, 191 191 AssertionSet & need, AssertionSet & have, const OpenVarSet & openVars, 192 192 ResolvExpr::WidenMode widen, const SymbolTable & symtab ); … … 213 213 214 214 /// Private lookup API; returns array index of string, or env.size() for not found 215 ClassList::iterator internal_lookup( const Type InstType::TypeEnvKey & );215 ClassList::iterator internal_lookup( const TypeEnvKey & ); 216 216 }; 217 217 -
src/AST/TypeSubstitution.cpp
r78de1e5 r93c10de 52 52 } 53 53 54 void TypeSubstitution::add( const Type InstType::TypeEnvKey & key, const Type * actualType) {54 void TypeSubstitution::add( const TypeEnvKey & key, const Type * actualType) { 55 55 typeMap[ key ] = actualType; 56 56 } … … 64 64 65 65 const Type *TypeSubstitution::lookup( 66 const Type InstType::TypeEnvKey & formalType ) const {66 const TypeEnvKey & formalType ) const { 67 67 TypeMap::const_iterator i = typeMap.find( formalType ); 68 68 … … 85 85 86 86 const Type *TypeSubstitution::lookup( const TypeInstType * formalType ) const { 87 return lookup( ast::Type InstType::TypeEnvKey( *formalType ) );87 return lookup( ast::TypeEnvKey( *formalType ) ); 88 88 } 89 89 -
src/AST/TypeSubstitution.hpp
r78de1e5 r93c10de 72 72 73 73 void add( const TypeInstType * formalType, const Type *actualType ); 74 void add( const Type InstType::TypeEnvKey & key, const Type *actualType );74 void add( const TypeEnvKey & key, const Type *actualType ); 75 75 void add( const TypeSubstitution &other ); 76 76 void remove( const TypeInstType * formalType ); 77 const Type *lookup( const Type InstType::TypeEnvKey & formalType ) const;77 const Type *lookup( const TypeEnvKey & formalType ) const; 78 78 const Type *lookup( const TypeInstType * formalType ) const; 79 79 bool empty() const; … … 105 105 friend class Pass; 106 106 107 typedef std::unordered_map< Type InstType::TypeEnvKey, ptr<Type> > TypeMap;107 typedef std::unordered_map< TypeEnvKey, ptr<Type> > TypeMap; 108 108 TypeMap typeMap; 109 109 … … 184 184 int subCount = 0; 185 185 bool freeOnly; 186 typedef std::unordered_set< Type InstType::TypeEnvKey > BoundVarsType;186 typedef std::unordered_set< TypeEnvKey > BoundVarsType; 187 187 BoundVarsType boundVars; 188 188 -
src/GenPoly/GenPoly.cc
r78de1e5 r93c10de 783 783 const ast::FunctionType * function = getFunctionType( expr->func->result ); 784 784 assertf( function, "ApplicationExpr has non-function type: %s", toString( expr->func->result ).c_str() ); 785 TypeVarMap exprTyVars = { ast::TypeD ecl::Data() };785 TypeVarMap exprTyVars = { ast::TypeData() }; 786 786 makeTypeVarMap( function, exprTyVars ); 787 787 return needsBoxing( param, arg, exprTyVars, subst ); … … 793 793 794 794 void addToTypeVarMap( const ast::TypeInstType * type, TypeVarMap & typeVars ) { 795 typeVars.insert( *type, ast::TypeD ecl::Data( type->base ) );795 typeVars.insert( *type, ast::TypeData( type->base ) ); 796 796 } 797 797 -
src/GenPoly/GenPoly.h
r78de1e5 r93c10de 30 30 31 31 typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap; 32 using TypeVarMap = ErasableScopedMap< ast::Type InstType::TypeEnvKey, ast::TypeDecl::Data >;32 using TypeVarMap = ErasableScopedMap< ast::TypeEnvKey, ast::TypeData >; 33 33 34 34 /// Replaces a TypeInstType by its referrent in the environment, if applicable -
src/ResolvExpr/CandidateFinder.cpp
r78de1e5 r93c10de 221 221 ) { 222 222 for ( auto & tyvar : type->forall ) { 223 unifiableVars[ *tyvar ] = ast::TypeD ecl::Data{ tyvar->base };223 unifiableVars[ *tyvar ] = ast::TypeData{ tyvar->base }; 224 224 } 225 225 for ( auto & assn : type->assertions ) { -
src/ResolvExpr/FindOpenVars.cc
r78de1e5 r93c10de 113 113 if ( nextIsOpen ) { 114 114 for ( auto & decl : type->forall ) { 115 open[ *decl ] = ast::TypeD ecl::Data{ decl->base };115 open[ *decl ] = ast::TypeData{ decl->base }; 116 116 } 117 117 for ( auto & assert : type->assertions ) { … … 120 120 } else { 121 121 for ( auto & decl : type->forall ) { 122 closed[ *decl ] = ast::TypeD ecl::Data{ decl->base };122 closed[ *decl ] = ast::TypeData{ decl->base }; 123 123 } 124 124 for ( auto & assert : type->assertions ) { -
src/ResolvExpr/RenameVars.cc
r78de1e5 r93c10de 42 42 int next_usage_id = 1; 43 43 ScopedMap< std::string, std::string > nameMap; 44 ScopedMap< std::string, ast::Type InstType::TypeEnvKey > idMap;44 ScopedMap< std::string, ast::TypeEnvKey > idMap; 45 45 public: 46 46 void reset() { … … 121 121 assert(false); 122 122 } 123 idMap[ td->name ] = ast::Type InstType::TypeEnvKey(*mut);124 123 idMap[ td->name ] = ast::TypeEnvKey( *mut ); 124 125 125 td = mut; 126 126 } -
src/ResolvExpr/Unify.cc
r78de1e5 r93c10de 1166 1166 if ( entry1->second.kind != entry2->second.kind ) return false; 1167 1167 return env.bindVarToVar( 1168 var1, var2, ast::TypeD ecl::Data{ entry1->second, entry2->second }, need, have,1168 var1, var2, ast::TypeData{ entry1->second, entry2->second }, need, have, 1169 1169 open, widen, symtab ); 1170 1170 } else if ( isopen1 ) {
Note: See TracChangeset
for help on using the changeset viewer.