Changes in / [907c545:a1b154d]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r907c545 ra1b154d  
    4444class ConverterNewToOld : public ast::Visitor {
    4545        BaseSyntaxNode * node = nullptr;
    46         using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
    47         Cache cache;
     46        std::unordered_map< ast::Node *, BaseSyntaxNode * > cache;
    4847
    4948        template<typename T>
     
    9594        Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
    9695
    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;
     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                }
    103105        }
    104106
     
    128130                decl->isDeleted = node->isDeleted;
    129131                // fs comes from constructor
    130                 cache.emplace( node, decl );
    131132                return nullptr;
    132133        }
    133134
    134135        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;
    151150                auto decl = new FunctionDecl(
    152151                        node->name,
     
    172171
    173172        const ast::Decl * visit( const ast::TypeDecl * node ) override final {
    174                 if ( inCache( node ) ) return nullptr;
     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                };
    175187                auto decl = new TypeDecl(
    176188                        node->name,
    177189                        Type::StorageClasses( node->storage.val ),
    178190                        get<Type>().accept1( node->base ),
    179                         (TypeDecl::Kind)(unsigned)node->kind,
     191                        kind,
    180192                        node->sized,
    181193                        get<Type>().accept1( node->init )
    182194                );
    183                 cache.emplace( node, decl );
    184195                return namedTypePostamble( decl, node );
    185196        }
     
    201212                decl->body = node->body;
    202213                // attributes come from constructor
    203                 decl->parent = get<AggregateDecl>().accept1( node->parent );
    204                 cache.emplace( node, decl );
     214                // TODO: Need caching for: decl->parent = node->parent;
    205215                return nullptr;
    206216        }
    207217
    208218        const ast::Decl * visit( const ast::StructDecl * node ) override final {
    209                 if ( inCache( node ) ) return nullptr;
    210219                auto decl = new StructDecl(
    211220                        node->name,
     
    218227
    219228        const ast::Decl * visit( const ast::UnionDecl * node ) override final {
    220                 if ( inCache( node ) ) return nullptr;
    221229                auto decl = new UnionDecl(
    222230                        node->name,
     
    228236
    229237        const ast::Decl * visit( const ast::EnumDecl * node ) override final {
    230                 if ( inCache( node ) ) return nullptr;
    231238                auto decl = new EnumDecl(
    232239                        node->name,
     
    238245
    239246        const ast::Decl * visit( const ast::TraitDecl * node ) override final {
    240                 if ( inCache( node ) ) return nullptr;
    241247                auto decl = new TraitDecl(
    242248                        node->name,
     
    798804        }
    799805
    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                 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;
     807                (void)node;
    823808                return nullptr;
    824809        }
    825810
    826811        const ast::Type * visit( const ast::UnionInstType * node ) override final {
    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;
     812                (void)node;
    843813                return nullptr;
    844814        }
    845815
    846816        const ast::Type * visit( const ast::EnumInstType * node ) override final {
    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;
     817                (void)node;
    863818                return nullptr;
    864819        }
    865820
    866821        const ast::Type * visit( const ast::TraitInstType * node ) override final {
    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;
     822                (void)node;
    883823                return nullptr;
    884824        }
    885825
    886826        const ast::Type * visit( const ast::TypeInstType * node ) override final {
    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;
     827                (void)node;
    905828                return nullptr;
    906829        }
     
    1038961        static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; }
    1039962
    1040         /// returns true and sets `node` if in cache
    1041         bool inCache( BaseSyntaxNode * old ) {
     963        template<typename NewT, typename OldT>
     964        NewT * cached( OldT * old ) {
    1042965                auto it = cache.find( old );
    1043                 if ( it == cache.end() ) return false;
    1044                 node = it->second;
    1045                 return true;
     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 );
    1046969        }
    1047970
     
    1049972
    1050973        virtual void visit( ObjectDecl * old ) override final {
    1051                 if ( inCache( old ) ) return;
    1052974                auto decl = new ast::ObjectDecl(
    1053975                        old->location,
     
    1066988                decl->uniqueId   = old->uniqueId;
    1067989                decl->extension  = old->extension;
    1068                 cache.emplace( old, decl );
    1069990
    1070991                this->node = decl;
    1071992        }
    1072993
    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 );
     994        virtual void visit( FunctionDecl * ) override final {
     995
    1078996        }
    1079997
    1080998        virtual void visit( StructDecl * old ) override final {
    1081                 if ( inCache( old ) ) return;
    1082999                auto decl = new ast::StructDecl(
    10831000                        old->location,
     
    10941011                decl->uniqueId   = old->uniqueId;
    10951012                decl->storage    = { old->storageClasses.val };
    1096                 cache.emplace( old, decl );
    10971013
    10981014                this->node = decl;
     
    11001016
    11011017        virtual void visit( UnionDecl * old ) override final {
    1102                 if ( inCache( old ) ) return;
    11031018                auto decl = new ast::UnionDecl(
    11041019                        old->location,
     
    11141029                decl->uniqueId   = old->uniqueId;
    11151030                decl->storage    = { old->storageClasses.val };
    1116                 cache.emplace( old, decl );
    11171031
    11181032                this->node = decl;
     
    11201034
    11211035        virtual void visit( EnumDecl * old ) override final {
    1122                 if ( inCache( old ) ) return;
    11231036                auto decl = new ast::UnionDecl(
    11241037                        old->location,
     
    11341047                decl->uniqueId   = old->uniqueId;
    11351048                decl->storage    = { old->storageClasses.val };
    1136                 cache.emplace( old, decl );
    11371049
    11381050                this->node = decl;
     
    11401052
    11411053        virtual void visit( TraitDecl * old ) override final {
    1142                 if ( inCache( old ) ) return;
    11431054                auto decl = new ast::UnionDecl(
    11441055                        old->location,
     
    11541065                decl->uniqueId   = old->uniqueId;
    11551066                decl->storage    = { old->storageClasses.val };
    1156                 cache.emplace( old, decl );
    11571067
    11581068                this->node = decl;
    11591069        }
    11601070
    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 );
     1071        virtual void visit( TypeDecl * ) override final {
     1072
    11661073        }
    11671074
     
    17351642
    17361643        virtual void visit( StructInstType * old ) override final {
    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                 }
     1644                auto ty = new ast::StructInstType{
     1645                        cached< ast::StructDecl >( old->baseStruct ),
     1646                        cv( old ),
     1647                        GET_ACCEPT_V( attributes, Attribute )
     1648                };
    17511649                postvisit( old, ty );
    17521650                this->node = ty;
     
    17541652
    17551653        virtual void visit( UnionInstType * old ) override final {
    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                 }
     1654                auto ty = new ast::UnionInstType{
     1655                        cached< ast::UnionDecl >( old->baseUnion ),
     1656                        cv( old ),
     1657                        GET_ACCEPT_V( attributes, Attribute )
     1658                };
    17701659                postvisit( old, ty );
    17711660                this->node = ty;
     
    17731662
    17741663        virtual void visit( EnumInstType * old ) override final {
    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                 }
     1664                auto ty = new ast::EnumInstType{
     1665                        cached< ast::EnumDecl >( old->baseEnum ),
     1666                        cv( old ),
     1667                        GET_ACCEPT_V( attributes, Attribute )
     1668                };
    17891669                postvisit( old, ty );
    17901670                this->node = ty;
     
    17921672
    17931673        virtual void visit( TraitInstType * old ) override final {
    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                 }
     1674                auto ty = new ast::TraitInstType{
     1675                        cached< ast::TraitDecl >( old->baseTrait ),
     1676                        cv( old ),
     1677                        GET_ACCEPT_V( attributes, Attribute )
     1678                };
    18081679                postvisit( old, ty );
    18091680                this->node = ty;
     
    18151686                        ty = new ast::TypeInstType{
    18161687                                old->name,
    1817                                 GET_ACCEPT_1( baseType, TypeDecl ),
     1688                                cached< ast::TypeDecl >( old->baseType ),
    18181689                                cv( old ),
    18191690                                GET_ACCEPT_V( attributes, Attribute )
Note: See TracChangeset for help on using the changeset viewer.