Changeset 93c10de


Ignore:
Timestamp:
Nov 24, 2022, 11:01:37 AM (17 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
82a90d4
Parents:
78de1e5
Message:

Minimal changes to pull out nested types, TypeInstType::TypeEnvKey? and TypeDecl::Data (now TypeData?) from there parent types. Although they do connect to the parent types they were nested in they are used on their own most of the time.

Location:
src
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.cpp

    r78de1e5 r93c10de  
    125125}
    126126
    127 std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
     127std::ostream & operator<< ( std::ostream & out, const TypeData & data ) {
    128128        return out << data.kind << ", " << data.isComplete;
    129129}
  • src/AST/Decl.hpp

    r78de1e5 r93c10de  
    1010// Created On       : Thu May 9 10:00:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Thu May  5 12:09:00 2022
    13 // Update Count     : 33
     12// Last Modified On : Thu Nov 24  9:44:00 2022
     13// Update Count     : 34
    1414//
    1515
     
    191191        ptr<Type> init;
    192192
    193         /// Data extracted from a type decl
    194         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 
    208193        TypeDecl(
    209194                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
     
    225210};
    226211
    227 std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
     212/// Data extracted from a TypeDecl.
     213struct 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
     227std::ostream & operator<< ( std::ostream &, const TypeData & );
    228228
    229229/// C-style typedef `typedef Foo Bar`
  • src/AST/Type.cpp

    r78de1e5 r93c10de  
    1010// Created On       : Mon May 13 15:00:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Thu Jul 23 14:16:00 2020
    13 // Update Count     : 5
     12// Last Modified On : Thu Nov 24  9:49:00 2022
     13// Update Count     : 6
    1414//
    1515
     
    147147// --- TypeInstType
    148148
     149TypeInstType::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
    149152bool TypeInstType::operator==( const TypeInstType & other ) const {
    150153        return base == other.base
     
    164167bool TypeInstType::isComplete() const { return base->sized; }
    165168
    166 std::string TypeInstType::TypeEnvKey::typeString() const {
     169std::string TypeEnvKey::typeString() const {
    167170        return std::string("_") + std::to_string(formal_usage)
    168171                + "_" + std::to_string(expr_id) + "_" + base->name;
    169172}
    170173
    171 bool TypeInstType::TypeEnvKey::operator==(
    172                 const TypeInstType::TypeEnvKey & other ) const {
     174bool TypeEnvKey::operator==(
     175                const TypeEnvKey & other ) const {
    173176        return base == other.base
    174177                && formal_usage == other.formal_usage
     
    176179}
    177180
    178 bool TypeInstType::TypeEnvKey::operator<(
    179                 const TypeInstType::TypeEnvKey & other ) const {
     181bool TypeEnvKey::operator<(
     182                const TypeEnvKey & other ) const {
    180183        // TypeEnvKey ordering is an arbitrary total ordering.
    181184        // It doesn't mean anything but allows for a sorting.
  • src/AST/Type.hpp

    r78de1e5 r93c10de  
    1010// Created On       : Thu May 9 10:00:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jul 14 15:54:00 2021
    13 // Update Count     : 7
     12// Last Modified On : Thu Nov 24  9:47:00 2022
     13// Update Count     : 8
    1414//
    1515
     
    390390};
    391391
     392struct TypeEnvKey;
     393
    392394/// instance of named type alias (typedef or variable)
    393395class TypeInstType final : public BaseInstType {
     
    401403        int expr_id = 0;
    402404
    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 
    419405        bool operator==(const TypeInstType & other) const;
    420406
     
    433419        TypeInstType( const TypeInstType & o ) = default;
    434420
    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 );
    437422
    438423        /// sets `base`, updating `kind` correctly
     
    453438        TypeInstType * clone() const override { return new TypeInstType{ *this }; }
    454439        MUTATE_FRIEND
     440};
     441
     442/// Compact representation of TypeInstType used for map lookups.
     443struct 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;
    455456};
    456457
     
    560561namespace std {
    561562        template<>
    562         struct hash<typename ast::TypeInstType::TypeEnvKey> {
    563                 size_t operator() (const ast::TypeInstType::TypeEnvKey & x) const {
     563        struct hash<typename ast::TypeEnvKey> {
     564                size_t operator() (const ast::TypeEnvKey & x) const {
    564565                        const size_t p = 1000007;
    565566                        size_t res = reinterpret_cast<size_t>(x.base);
  • src/AST/TypeEnvironment.cpp

    r78de1e5 r93c10de  
    8282}
    8383
    84 const EqvClass * TypeEnvironment::lookup( const TypeInstType::TypeEnvKey & var ) const {
     84const EqvClass * TypeEnvironment::lookup( const TypeEnvKey & var ) const {
    8585        for ( ClassList::const_iterator i = env.begin(); i != env.end(); ++i ) {
    8686                if ( i->vars.find( var ) != i->vars.end() ) return &*i;
     
    122122void TypeEnvironment::writeToSubstitution( TypeSubstitution & sub ) const {
    123123        for ( const auto & clz : env ) {
    124                 TypeInstType::TypeEnvKey clzRep;
     124                TypeEnvKey clzRep;
    125125                bool first = true;
    126126                for ( const auto & var : clz.vars ) {
     
    146146        struct Occurs : public ast::WithVisitorRef<Occurs> {
    147147                bool result;
    148                 std::unordered_set< TypeInstType::TypeEnvKey > vars;
     148                std::unordered_set< TypeEnvKey > vars;
    149149                const TypeEnvironment & tenv;
    150150
    151                 Occurs( const TypeInstType::TypeEnvKey & var, const TypeEnvironment & env )
     151                Occurs( const TypeEnvKey & var, const TypeEnvironment & env )
    152152                : result( false ), vars(), tenv( env ) {
    153153                        if ( const EqvClass * clz = tenv.lookup( var ) ) {
     
    170170
    171171        /// true if `var` occurs in `ty` under `env`
    172         bool occurs( const Type * ty, const TypeInstType::TypeEnvKey & var, const TypeEnvironment & env ) {
     172        bool occurs( const Type * ty, const TypeEnvKey & var, const TypeEnvironment & env ) {
    173173                Pass<Occurs> occur{ var, env };
    174174                maybe_accept( ty, occur );
     
    258258namespace {
    259259        /// true if the given type can be bound to the given type variable
    260         bool tyVarCompatible( const TypeDecl::Data & data, const Type * type ) {
     260        bool tyVarCompatible( const TypeData & data, const Type * type ) {
    261261                switch ( data.kind ) {
    262262                  case TypeDecl::Dtype:
     
    279279
    280280bool TypeEnvironment::bindVar(
    281                 const TypeInstType * typeInst, const Type * bindTo, const TypeDecl::Data & data,
     281                const TypeInstType * typeInst, const Type * bindTo, const TypeData & data,
    282282                AssertionSet & need, AssertionSet & have, const OpenVarSet & open, WidenMode widen,
    283283                const SymbolTable & symtab
     
    319319
    320320bool TypeEnvironment::bindVarToVar(
    321                 const TypeInstType * var1, const TypeInstType * var2, TypeDecl::Data && data,
     321                const TypeInstType * var1, const TypeInstType * var2, TypeData && data,
    322322                AssertionSet & need, AssertionSet & have, const OpenVarSet & open,
    323323                WidenMode widen, const SymbolTable & symtab
     
    457457}
    458458
    459 TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const TypeInstType::TypeEnvKey & var ) {
     459TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const TypeEnvKey & var ) {
    460460        for ( ClassList::iterator i = env.begin(); i != env.end(); ++i ) {
    461461                if ( i->vars.count( var ) ) return i;
  • src/AST/TypeEnvironment.hpp

    r78de1e5 r93c10de  
    7979
    8080/// Set of open variables
    81 using OpenVarSet = std::unordered_map< TypeInstType::TypeEnvKey, TypeDecl::Data >;
     81using OpenVarSet = std::unordered_map< TypeEnvKey, TypeData >;
    8282
    8383/// Merges one set of open vars into another
     
    9595/// they bind to.
    9696struct EqvClass {
    97         std::unordered_set< TypeInstType::TypeEnvKey > vars;
     97        std::unordered_set< TypeEnvKey > vars;
    9898        ptr<Type> bound;
    9999        bool allowWidening;
    100         TypeDecl::Data data;
     100        TypeData data;
    101101
    102102        EqvClass() : vars(), bound(), allowWidening( true ), data() {}
     
    111111
    112112        /// Singleton class constructor from substitution
    113         EqvClass( const TypeInstType::TypeEnvKey & v, const Type * b )
     113        EqvClass( const TypeEnvKey & v, const Type * b )
    114114        : vars{ v }, bound( b ), allowWidening( false ), data( TypeDecl::Dtype, false ) {}
    115115
    116116        /// Single-var constructor (strips qualifiers from bound type)
    117         EqvClass( const TypeInstType::TypeEnvKey & v, const Type * b, bool w, const TypeDecl::Data & d )
     117        EqvClass( const TypeEnvKey & v, const Type * b, bool w, const TypeData & d )
    118118        : vars{ v }, bound( b ), allowWidening( w ), data( d ) {
    119119                reset_qualifiers( bound );
     
    121121
    122122        /// Double-var constructor
    123         EqvClass( const TypeInstType::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 )
    124124        : vars{ v, u }, bound(), allowWidening( w ), data( d ) {}
    125125
     
    137137public:
    138138        /// Finds the equivalence class containing a variable; nullptr for none such
    139         const EqvClass * lookup( const TypeInstType::TypeEnvKey & var ) const;
     139        const EqvClass * lookup( const TypeEnvKey & var ) const;
    140140
    141141        /// Add a new equivalence class for each type variable
     
    181181        /// needed. Returns false on failure.
    182182        bool bindVar(
    183                 const TypeInstType * typeInst, const Type * bindTo, const TypeDecl::Data & data,
     183                const TypeInstType * typeInst, const Type * bindTo, const TypeData & data,
    184184                AssertionSet & need, AssertionSet & have, const OpenVarSet & openVars,
    185185                ResolvExpr::WidenMode widen, const SymbolTable & symtab );
     
    188188        /// classes if needed. Returns false on failure.
    189189        bool bindVarToVar(
    190                 const TypeInstType * var1, const TypeInstType * var2, TypeDecl::Data && data,
     190                const TypeInstType * var1, const TypeInstType * var2, TypeData && data,
    191191                AssertionSet & need, AssertionSet & have, const OpenVarSet & openVars,
    192192                ResolvExpr::WidenMode widen, const SymbolTable & symtab );
     
    213213
    214214        /// Private lookup API; returns array index of string, or env.size() for not found
    215         ClassList::iterator internal_lookup( const TypeInstType::TypeEnvKey & );
     215        ClassList::iterator internal_lookup( const TypeEnvKey & );
    216216};
    217217
  • src/AST/TypeSubstitution.cpp

    r78de1e5 r93c10de  
    5252}
    5353
    54 void TypeSubstitution::add( const TypeInstType::TypeEnvKey & key, const Type * actualType) {
     54void TypeSubstitution::add( const TypeEnvKey & key, const Type * actualType) {
    5555        typeMap[ key ] = actualType;
    5656}
     
    6464
    6565const Type *TypeSubstitution::lookup(
    66                 const TypeInstType::TypeEnvKey & formalType ) const {
     66                const TypeEnvKey & formalType ) const {
    6767        TypeMap::const_iterator i = typeMap.find( formalType );
    6868
     
    8585
    8686const Type *TypeSubstitution::lookup( const TypeInstType * formalType ) const {
    87         return lookup( ast::TypeInstType::TypeEnvKey( *formalType ) );
     87        return lookup( ast::TypeEnvKey( *formalType ) );
    8888}
    8989
  • src/AST/TypeSubstitution.hpp

    r78de1e5 r93c10de  
    7272
    7373        void add( const TypeInstType * formalType, const Type *actualType );
    74         void add( const TypeInstType::TypeEnvKey & key, const Type *actualType );
     74        void add( const TypeEnvKey & key, const Type *actualType );
    7575        void add( const TypeSubstitution &other );
    7676        void remove( const TypeInstType * formalType );
    77         const Type *lookup( const TypeInstType::TypeEnvKey & formalType ) const;
     77        const Type *lookup( const TypeEnvKey & formalType ) const;
    7878        const Type *lookup( const TypeInstType * formalType ) const;
    7979        bool empty() const;
     
    105105        friend class Pass;
    106106
    107         typedef std::unordered_map< TypeInstType::TypeEnvKey, ptr<Type> > TypeMap;
     107        typedef std::unordered_map< TypeEnvKey, ptr<Type> > TypeMap;
    108108        TypeMap typeMap;
    109109
     
    184184                int subCount = 0;
    185185                bool freeOnly;
    186                 typedef std::unordered_set< TypeInstType::TypeEnvKey > BoundVarsType;
     186                typedef std::unordered_set< TypeEnvKey > BoundVarsType;
    187187                BoundVarsType boundVars;
    188188
  • src/GenPoly/GenPoly.cc

    r78de1e5 r93c10de  
    783783        const ast::FunctionType * function = getFunctionType( expr->func->result );
    784784        assertf( function, "ApplicationExpr has non-function type: %s", toString( expr->func->result ).c_str() );
    785         TypeVarMap exprTyVars = { ast::TypeDecl::Data() };
     785        TypeVarMap exprTyVars = { ast::TypeData() };
    786786        makeTypeVarMap( function, exprTyVars );
    787787        return needsBoxing( param, arg, exprTyVars, subst );
     
    793793
    794794void addToTypeVarMap( const ast::TypeInstType * type, TypeVarMap & typeVars ) {
    795         typeVars.insert( *type, ast::TypeDecl::Data( type->base ) );
     795        typeVars.insert( *type, ast::TypeData( type->base ) );
    796796}
    797797
  • src/GenPoly/GenPoly.h

    r78de1e5 r93c10de  
    3030
    3131        typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap;
    32         using TypeVarMap = ErasableScopedMap< ast::TypeInstType::TypeEnvKey, ast::TypeDecl::Data >;
     32        using TypeVarMap = ErasableScopedMap< ast::TypeEnvKey, ast::TypeData >;
    3333
    3434        /// Replaces a TypeInstType by its referrent in the environment, if applicable
  • src/ResolvExpr/CandidateFinder.cpp

    r78de1e5 r93c10de  
    221221        ) {
    222222                for ( auto & tyvar : type->forall ) {
    223                         unifiableVars[ *tyvar ] = ast::TypeDecl::Data{ tyvar->base };
     223                        unifiableVars[ *tyvar ] = ast::TypeData{ tyvar->base };
    224224                }
    225225                for ( auto & assn : type->assertions ) {
  • src/ResolvExpr/FindOpenVars.cc

    r78de1e5 r93c10de  
    113113                                if ( nextIsOpen ) {
    114114                                        for ( auto & decl : type->forall ) {
    115                                                 open[ *decl ] = ast::TypeDecl::Data{ decl->base };
     115                                                open[ *decl ] = ast::TypeData{ decl->base };
    116116                                        }
    117117                                        for ( auto & assert : type->assertions ) {
     
    120120                                } else {
    121121                                        for ( auto & decl : type->forall ) {
    122                                                 closed[ *decl ] = ast::TypeDecl::Data{ decl->base };   
     122                                                closed[ *decl ] = ast::TypeData{ decl->base };
    123123                                        }
    124124                                        for ( auto & assert : type->assertions ) {
  • src/ResolvExpr/RenameVars.cc

    r78de1e5 r93c10de  
    4242                int next_usage_id = 1;
    4343                ScopedMap< std::string, std::string > nameMap;
    44                 ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap;
     44                ScopedMap< std::string, ast::TypeEnvKey > idMap;
    4545        public:
    4646                void reset() {
     
    121121                                        assert(false);
    122122                                }
    123                                 idMap[ td->name ] = ast::TypeInstType::TypeEnvKey(*mut);
    124                                
     123                                idMap[ td->name ] = ast::TypeEnvKey( *mut );
     124
    125125                                td = mut;
    126126                        }
  • src/ResolvExpr/Unify.cc

    r78de1e5 r93c10de  
    11661166                        if ( entry1->second.kind != entry2->second.kind ) return false;
    11671167                        return env.bindVarToVar(
    1168                                 var1, var2, ast::TypeDecl::Data{ entry1->second, entry2->second }, need, have,
     1168                                var1, var2, ast::TypeData{ entry1->second, entry2->second }, need, have,
    11691169                                open, widen, symtab );
    11701170                } else if ( isopen1 ) {
Note: See TracChangeset for help on using the changeset viewer.