Changeset b869ec5


Ignore:
Timestamp:
May 21, 2019, 6:11:24 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
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:
907c545
Parents:
514a791
Message:

Conversion caching for AggregateDecl?, DeclWithType?, TypeDecl?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r514a791 rb869ec5  
    4444class ConverterNewToOld : public ast::Visitor {
    4545        BaseSyntaxNode * node = nullptr;
    46         std::unordered_map< ast::Node *, BaseSyntaxNode * > cache;
     46        using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
     47        Cache cache;
    4748
    4849        template<typename T>
     
    9495        Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
    9596
    96         template<typename NewT, typename OldT>
    97         NewT * cached( const OldT & old ) {
    98                 auto it = cache.find( old.get() );
    99                 if ( it == cache.end() ) {
    100                         // doesn't update cache, that should be handled by the accept function
    101                         return get< NewT >().accept1( old );
    102                 } else {
    103                         return strict_dynamic_cast< NewT * >( it->second );
    104                 }
     97        /// returns true and sets `node` if in cache
     98        bool inCache( const ast::Node * node ) {
     99                auto it = cache.find( node );
     100                if ( it == cache.end() ) return false;
     101                this->node = it->second;
     102                return true;
    105103        }
    106104
     
    130128                decl->isDeleted = node->isDeleted;
    131129                // fs comes from constructor
     130                cache.emplace( node, decl );
    132131                return nullptr;
    133132        }
    134133
    135134        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
     135                if ( inCache( node ) ) return nullptr;
    136136                auto decl = new ObjectDecl(
    137137                        node->name,
     
    148148
    149149        const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
     150                if ( inCache( node ) ) return nullptr;
    150151                auto decl = new FunctionDecl(
    151152                        node->name,
     
    171172
    172173        const ast::Decl * visit( const ast::TypeDecl * node ) override final {
    173                 TypeDecl::Kind kind;
    174                 switch (node->kind) {
    175                 case ast::TypeVar::Dtype:
    176                         kind = TypeDecl::Dtype;
    177                         break;
    178                 case ast::TypeVar::Ftype:
    179                         kind = TypeDecl::Ftype;
    180                         break;
    181                 case ast::TypeVar::Ttype:
    182                         kind = TypeDecl::Ttype;
    183                         break;
    184                 default:
    185                         assertf(false, "Invalid ast::TypeVar::Kind: %d\n", node->kind);
    186                 };
     174                if ( inCache( node ) ) return nullptr;
    187175                auto decl = new TypeDecl(
    188176                        node->name,
    189177                        Type::StorageClasses( node->storage.val ),
    190178                        get<Type>().accept1( node->base ),
    191                         kind,
     179                        (TypeDecl::Kind)(unsigned)node->kind,
    192180                        node->sized,
    193181                        get<Type>().accept1( node->init )
    194182                );
     183                cache.emplace( node, decl );
    195184                return namedTypePostamble( decl, node );
    196185        }
     
    212201                decl->body = node->body;
    213202                // attributes come from constructor
    214                 // TODO: Need caching for: decl->parent = node->parent;
     203                decl->parent = get<AggregateDecl>().accept1( node->parent );
     204                cache.emplace( node, decl );
    215205                return nullptr;
    216206        }
    217207
    218208        const ast::Decl * visit( const ast::StructDecl * node ) override final {
     209                if ( inCache( node ) ) return nullptr;
    219210                auto decl = new StructDecl(
    220211                        node->name,
     
    227218
    228219        const ast::Decl * visit( const ast::UnionDecl * node ) override final {
     220                if ( inCache( node ) ) return nullptr;
    229221                auto decl = new UnionDecl(
    230222                        node->name,
     
    236228
    237229        const ast::Decl * visit( const ast::EnumDecl * node ) override final {
     230                if ( inCache( node ) ) return nullptr;
    238231                auto decl = new EnumDecl(
    239232                        node->name,
     
    245238
    246239        const ast::Decl * visit( const ast::TraitDecl * node ) override final {
     240                if ( inCache( node ) ) return nullptr;
    247241                auto decl = new TraitDecl(
    248242                        node->name,
     
    804798        }
    805799
     800        void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {
     801                ty->forall = get<TypeDecl>().acceptL( old->forall );
     802                ty->parameters = get<Expression>().acceptL( old->params );
     803                ty->hoistType = old->hoistType;
     804        }
     805
    806806        const ast::Type * visit( const ast::StructInstType * node ) override final {
    807                 (void)node;
     807                StructInstType * ty;
     808                if ( node->base ) {
     809                        ty = new StructInstType{
     810                                cv( node ),
     811                                get<StructDecl>().accept1( node->base ),
     812                                get<Attribute>().acceptL( node->attributes )
     813                        };
     814                } else {
     815                        ty = new StructInstType{
     816                                cv( node ),
     817                                node->name,
     818                                get<Attribute>().acceptL( node->attributes )
     819                        };
     820                }
     821                postvisit( node, ty );
     822                this->node = ty;
    808823                return nullptr;
    809824        }
    810825
    811826        const ast::Type * visit( const ast::UnionInstType * node ) override final {
    812                 (void)node;
     827                UnionInstType * ty;
     828                if ( node->base ) {
     829                        ty = new UnionInstType{
     830                                cv( node ),
     831                                get<UnionDecl>().accept1( node->base ),
     832                                get<Attribute>().acceptL( node->attributes )
     833                        };
     834                } else {
     835                        ty = new UnionInstType{
     836                                cv( node ),
     837                                node->name,
     838                                get<Attribute>().acceptL( node->attributes )
     839                        };
     840                }
     841                postvisit( node, ty );
     842                this->node = ty;
    813843                return nullptr;
    814844        }
    815845
    816846        const ast::Type * visit( const ast::EnumInstType * node ) override final {
    817                 (void)node;
     847                EnumInstType * ty;
     848                if ( node->base ) {
     849                        ty = new EnumInstType{
     850                                cv( node ),
     851                                get<EnumDecl>().accept1( node->base ),
     852                                get<Attribute>().acceptL( node->attributes )
     853                        };
     854                } else {
     855                        ty = new EnumInstType{
     856                                cv( node ),
     857                                node->name,
     858                                get<Attribute>().acceptL( node->attributes )
     859                        };
     860                }
     861                postvisit( node, ty );
     862                this->node = ty;
    818863                return nullptr;
    819864        }
    820865
    821866        const ast::Type * visit( const ast::TraitInstType * node ) override final {
    822                 (void)node;
     867                TraitInstType * ty;
     868                if ( node->base ) {
     869                        ty = new TraitInstType{
     870                                cv( node ),
     871                                get<TraitDecl>().accept1( node->base ),
     872                                get<Attribute>().acceptL( node->attributes )
     873                        };
     874                } else {
     875                        ty = new TraitInstType{
     876                                cv( node ),
     877                                node->name,
     878                                get<Attribute>().acceptL( node->attributes )
     879                        };
     880                }
     881                postvisit( node, ty );
     882                this->node = ty;
    823883                return nullptr;
    824884        }
    825885
    826886        const ast::Type * visit( const ast::TypeInstType * node ) override final {
    827                 (void)node;
     887                TypeInstType * ty;
     888                if ( node->base ) {
     889                        ty = new TypeInstType{
     890                                cv( node ),
     891                                node->name,
     892                                get<TypeDecl>().accept1( node->base ),
     893                                get<Attribute>().acceptL( node->attributes )
     894                        };
     895                } else {
     896                        ty = new TypeInstType{
     897                                cv( node ),
     898                                node->name,
     899                                node->kind == ast::TypeVar::Ftype,
     900                                get<Attribute>().acceptL( node->attributes )
     901                        };
     902                }
     903                postvisit( node, ty );
     904                this->node = ty;
    828905                return nullptr;
    829906        }
     
    9611038        static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; }
    9621039
    963         template<typename NewT, typename OldT>
    964         NewT * cached( OldT * old ) {
     1040        /// returns true and sets `node` if in cache
     1041        bool inCache( BaseSyntaxNode * old ) {
    9651042                auto it = cache.find( old );
    966                 // doesn't update cache, that should be handled by the accept function
    967                 ast::Node * nw = it == cache.end() ? getAccept1< NewT >( old ) : it->second;
    968                 return strict_dynamic_cast< NewT * >( nw );
     1043                if ( it == cache.end() ) return false;
     1044                node = it->second;
     1045                return true;
    9691046        }
    9701047
     
    9721049
    9731050        virtual void visit( ObjectDecl * old ) override final {
     1051                if ( inCache( old ) ) return;
    9741052                auto decl = new ast::ObjectDecl(
    9751053                        old->location,
     
    9881066                decl->uniqueId   = old->uniqueId;
    9891067                decl->extension  = old->extension;
     1068                cache.emplace( old, decl );
    9901069
    9911070                this->node = decl;
    9921071        }
    9931072
    994         virtual void visit( FunctionDecl * ) override final {
    995 
     1073        virtual void visit( FunctionDecl * old ) override final {
     1074                if ( inCache( old ) ) return;
     1075                // TODO
     1076                auto decl = (ast::FunctionDecl *)nullptr;
     1077                cache.emplace( old, decl );
    9961078        }
    9971079
    9981080        virtual void visit( StructDecl * old ) override final {
     1081                if ( inCache( old ) ) return;
    9991082                auto decl = new ast::StructDecl(
    10001083                        old->location,
     
    10111094                decl->uniqueId   = old->uniqueId;
    10121095                decl->storage    = { old->storageClasses.val };
     1096                cache.emplace( old, decl );
    10131097
    10141098                this->node = decl;
     
    10161100
    10171101        virtual void visit( UnionDecl * old ) override final {
     1102                if ( inCache( old ) ) return;
    10181103                auto decl = new ast::UnionDecl(
    10191104                        old->location,
     
    10291114                decl->uniqueId   = old->uniqueId;
    10301115                decl->storage    = { old->storageClasses.val };
     1116                cache.emplace( old, decl );
    10311117
    10321118                this->node = decl;
     
    10341120
    10351121        virtual void visit( EnumDecl * old ) override final {
     1122                if ( inCache( old ) ) return;
    10361123                auto decl = new ast::UnionDecl(
    10371124                        old->location,
     
    10471134                decl->uniqueId   = old->uniqueId;
    10481135                decl->storage    = { old->storageClasses.val };
     1136                cache.emplace( old, decl );
    10491137
    10501138                this->node = decl;
     
    10521140
    10531141        virtual void visit( TraitDecl * old ) override final {
     1142                if ( inCache( old ) ) return;
    10541143                auto decl = new ast::UnionDecl(
    10551144                        old->location,
     
    10651154                decl->uniqueId   = old->uniqueId;
    10661155                decl->storage    = { old->storageClasses.val };
     1156                cache.emplace( old, decl );
    10671157
    10681158                this->node = decl;
    10691159        }
    10701160
    1071         virtual void visit( TypeDecl * ) override final {
    1072 
     1161        virtual void visit( TypeDecl * old ) override final {
     1162                if ( inCache( old ) ) return;
     1163                // TODO
     1164                auto decl = (ast::TypeDecl *)nullptr;
     1165                cache.emplace( old, decl );
    10731166        }
    10741167
     
    16421735
    16431736        virtual void visit( StructInstType * old ) override final {
    1644                 auto ty = new ast::StructInstType{
    1645                         cached< ast::StructDecl >( old->baseStruct ),
    1646                         cv( old ),
    1647                         GET_ACCEPT_V( attributes, Attribute )
    1648                 };
     1737                ast::StructInstType * ty;
     1738                if ( old->baseStruct ) {
     1739                        ty = new ast::StructInstType{
     1740                                GET_ACCEPT_1( baseStruct, StructDecl ),
     1741                                cv( old ),
     1742                                GET_ACCEPT_V( attributes, Attribute )
     1743                        };
     1744                } else {
     1745                        ty = new ast::StructInstType{
     1746                                old->name,
     1747                                cv( old ),
     1748                                GET_ACCEPT_V( attributes, Attribute )
     1749                        };
     1750                }
    16491751                postvisit( old, ty );
    16501752                this->node = ty;
     
    16521754
    16531755        virtual void visit( UnionInstType * old ) override final {
    1654                 auto ty = new ast::UnionInstType{
    1655                         cached< ast::UnionDecl >( old->baseUnion ),
    1656                         cv( old ),
    1657                         GET_ACCEPT_V( attributes, Attribute )
    1658                 };
     1756                ast::UnionInstType * ty;
     1757                if ( old->baseUnion ) {
     1758                        ty = new ast::UnionInstType{
     1759                                GET_ACCEPT_1( baseUnion, UnionDecl ),
     1760                                cv( old ),
     1761                                GET_ACCEPT_V( attributes, Attribute )
     1762                        };
     1763                } else {
     1764                        ty = new ast::UnionInstType{
     1765                                old->name,
     1766                                cv( old ),
     1767                                GET_ACCEPT_V( attributes, Attribute )
     1768                        };
     1769                }
    16591770                postvisit( old, ty );
    16601771                this->node = ty;
     
    16621773
    16631774        virtual void visit( EnumInstType * old ) override final {
    1664                 auto ty = new ast::EnumInstType{
    1665                         cached< ast::EnumDecl >( old->baseEnum ),
    1666                         cv( old ),
    1667                         GET_ACCEPT_V( attributes, Attribute )
    1668                 };
     1775                ast::EnumInstType * ty;
     1776                if ( old->baseEnum ) {
     1777                        ty = new ast::EnumInstType{
     1778                                GET_ACCEPT_1( baseEnum, EnumDecl ),
     1779                                cv( old ),
     1780                                GET_ACCEPT_V( attributes, Attribute )
     1781                        };
     1782                } else {
     1783                        ty = new ast::EnumInstType{
     1784                                old->name,
     1785                                cv( old ),
     1786                                GET_ACCEPT_V( attributes, Attribute )
     1787                        };
     1788                }
    16691789                postvisit( old, ty );
    16701790                this->node = ty;
     
    16721792
    16731793        virtual void visit( TraitInstType * old ) override final {
    1674                 auto ty = new ast::TraitInstType{
    1675                         cached< ast::TraitDecl >( old->baseTrait ),
    1676                         cv( old ),
    1677                         GET_ACCEPT_V( attributes, Attribute )
    1678                 };
     1794                ast::TraitInstType * ty;
     1795                if ( old->baseTrait ) {
     1796                        ty = new ast::TraitInstType{
     1797                                GET_ACCEPT_1( baseTrait, TraitDecl ),
     1798                                cv( old ),
     1799                                GET_ACCEPT_V( attributes, Attribute )
     1800                        };
     1801                } else {
     1802                        ty = new ast::TraitInstType{
     1803                                old->name,
     1804                                cv( old ),
     1805                                GET_ACCEPT_V( attributes, Attribute )
     1806                        };
     1807                }
    16791808                postvisit( old, ty );
    16801809                this->node = ty;
     
    16861815                        ty = new ast::TypeInstType{
    16871816                                old->name,
    1688                                 cached< ast::TypeDecl >( old->baseType ),
     1817                                GET_ACCEPT_1( baseType, TypeDecl ),
    16891818                                cv( old ),
    16901819                                GET_ACCEPT_V( attributes, Attribute )
Note: See TracChangeset for help on using the changeset viewer.