Changeset fa2c005 for src/AST


Ignore:
Timestamp:
Jun 8, 2023, 3:19:43 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT
Parents:
044ae62
Message:

Finish Adt POC

Location:
src/AST
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r044ae62 rfa2c005  
    321321                        get<Type>().accept1(node->base)
    322322                );
    323                 // decl->data_constructors = get<StructDecl>().acceptL( node->data_constructors );
    324                 // decl->data_union = get<UnionDecl>().accept1( node->data_union );
    325                 // decl->tag = get<EnumDecl>().accept1( node->tag );
    326                 // decl->tag_union = get<StructDecl>().accept1( node->tag_union );
    327323                return aggregatePostamble( decl, node );
    328324        }
     
    334330                        get<Attribute>().acceptL( node->attributes ),
    335331                        LinkageSpec::Spec( node->linkage.val ),
    336                         get<StructDecl>().acceptL( node->data_constructors ),
    337332                        get<UnionDecl>().accept1( node->data_union ),
    338333                        get<EnumDecl>().accept1( node->tag ),
     
    13441339        }
    13451340
     1341        const ast::Type * visit( [[maybe_unused]]const ast::AdtInstType * node ) override final {
     1342                return nullptr;
     1343        }
     1344
    13461345        const ast::Type * visit( const ast::UnionInstType * node ) override final {
    13471346                UnionInstType * ty;
     
    18241823                decl->uniqueId = old->uniqueId;
    18251824                decl->storage = { old->storageClasses.val };
    1826                 decl->data_constructors = GET_ACCEPT_V( data_constructors, StructDecl );
    18271825                decl->data_union = GET_ACCEPT_1( data_union, UnionDecl );
    18281826                decl->tag = GET_ACCEPT_1( tag, EnumDecl );
     
    28762874                postvisit( old, ty );
    28772875        }
     2876       
     2877        virtual void visit( const AdtInstType * old ) override final {
     2878                ast::AdtInstType * ty;
     2879                if ( old->baseAdt ) {
     2880                        ty = new ast::AdtInstType{
     2881                                GET_ACCEPT_1( baseAdt, AdtDecl ),
     2882                                cv( old ),
     2883                                GET_ACCEPT_V( attributes, Attribute )
     2884                        };
     2885                } else {
     2886                        ty = new ast::AdtInstType{
     2887                                old->name,
     2888                                cv( old ),
     2889                                GET_ACCEPT_V( attributes, Attribute )
     2890                        };
     2891                }
     2892                postvisit( old, ty );
     2893        }
    28782894
    28792895        virtual void visit( const UnionInstType * old ) override final {
  • src/AST/Decl.hpp

    r044ae62 rfa2c005  
    248248class AggregateDecl : public Decl {
    249249public:
    250         enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, Adt };
     250        enum Aggregate { Undecided, Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, Adt };
    251251        static const char * aggrString( Aggregate aggr );
    252252
     
    346346class AdtDecl final : public AggregateDecl {
    347347public:
    348         std::vector<ptr<StructDecl>> data_constructors; // Todo: members?
    349348        ptr<UnionDecl> data_union;
    350349        ptr<EnumDecl> tag;
  • src/AST/Fwd.hpp

    r044ae62 rfa2c005  
    123123using UnionInstType = SueInstType<UnionDecl>;
    124124using EnumInstType = SueInstType<EnumDecl>;
     125using AdtInstType = SueInstType<AdtDecl>;
    125126class TraitInstType;
    126127class TypeInstType;
  • src/AST/Pass.hpp

    r044ae62 rfa2c005  
    219219        const ast::Type *             visit( const ast::FunctionType         * ) override final;
    220220        const ast::Type *             visit( const ast::StructInstType       * ) override final;
     221        const ast::Type *             visit( const ast::AdtInstType          * ) override final;
    221222        const ast::Type *             visit( const ast::UnionInstType        * ) override final;
    222223        const ast::Type *             visit( const ast::EnumInstType         * ) override final;
  • src/AST/Pass.impl.hpp

    r044ae62 rfa2c005  
    585585
    586586//--------------------------------------------------------------------------
     587// AdtDecl
     588template< typename core_t >
     589const ast::Decl * ast::Pass< core_t >::visit( const ast::AdtDecl * node ) {
     590        VISIT_START( node );
     591
     592        __pass::symtab::addAdtFwd( core, 0, node );
     593
     594        if ( __visit_children() ) {
     595                guard_symtab guard { *this };
     596                maybe_accept( node, &AdtDecl::params );
     597                maybe_accept( node, &AdtDecl::members );
     598                maybe_accept( node, &AdtDecl::attributes );
     599
     600                maybe_accept( node, &AdtDecl::data_union );
     601                maybe_accept( node, &AdtDecl::tag );
     602                maybe_accept( node, &AdtDecl::tag_union );
     603        }
     604
     605        __pass::symtab::addAdt( core, 0, node );
     606        VISIT_END( Decl, node );
     607}
     608
     609//--------------------------------------------------------------------------
    587610// UnionDecl
    588611template< typename core_t >
     
    620643                        maybe_accept( node, &EnumDecl::members    );
    621644                        maybe_accept( node, &EnumDecl::attributes );
    622 
    623                         // maybe_accept( node, &EnumDecl::data_constructors );
    624                         // maybe_accept( node, &EnumDecl::data_union );
    625                         // maybe_accept( node, &EnumDecl::tag );
    626                         // maybe_accept( node, &EnumDecl::tag_union );
    627645                } else {
    628646                        maybe_accept( node, &EnumDecl::base );
     
    630648                        maybe_accept( node, &EnumDecl::members    );
    631649                        maybe_accept( node, &EnumDecl::attributes );
    632 
    633                         // maybe_accept( node, &EnumDecl::data_constructors );
    634                         // maybe_accept( node, &EnumDecl::data_union );
    635                         // maybe_accept( node, &EnumDecl::tag );
    636                         // maybe_accept( node, &EnumDecl::tag_union );
    637                 }
    638         }
    639 
    640         VISIT_END( Decl, node );
    641 }
    642 
    643 template< typename core_t >
    644 const ast::Decl * ast::Pass< core_t >::visit( const ast::AdtDecl * node ) {
    645         VISIT_START( node );
    646 
    647         __pass::symtab::addAdt( core, 0, node );
    648 
    649         if ( __visit_children() ) {
    650                 guard_symtab guard { *this };
    651                 maybe_accept( node, &AdtDecl::params );
    652                 maybe_accept( node, &AdtDecl::members );
    653                 maybe_accept( node, &AdtDecl::attributes );
    654 
    655                 maybe_accept( node, &AdtDecl::data_constructors );
    656                 maybe_accept( node, &AdtDecl::data_union );
    657                 maybe_accept( node, &AdtDecl::tag );
    658                 maybe_accept( node, &AdtDecl::tag_union );
     650                }
    659651        }
    660652
     
    19671959
    19681960//--------------------------------------------------------------------------
     1961// AdtInstType
     1962template< typename core_t >
     1963const ast::Type * ast::Pass< core_t >::visit( const ast::AdtInstType * node ) {
     1964        VISIT_START( node );
     1965
     1966        __pass::symtab::addAdt( core, 0, node->name );
     1967
     1968        if ( __visit_children() ) {
     1969                guard_symtab guard { *this };
     1970                maybe_accept( node, &AdtInstType::params );
     1971        }
     1972
     1973        VISIT_END( Type, node );
     1974}
     1975
     1976//--------------------------------------------------------------------------
    19691977// UnionInstType
    19701978template< typename core_t >
  • src/AST/Pass.proto.hpp

    r044ae62 rfa2c005  
    477477
    478478        template<typename core_t>
     479        static inline auto addAdtFwd( core_t & core, int, const ast::AdtDecl * decl ) -> decltype( core.symtab.addAdt( decl ), void() ) {
     480                ast::AdtDecl * fwd = new ast::AdtDecl( decl->location, decl->name );
     481                for ( const auto & param : decl->params ) {
     482                        fwd->params.push_back( deepCopy( param.get() ) );
     483                }
     484                core.symtab.addAdt( fwd );
     485        }
     486
     487        template<typename core_t>
     488        static inline auto addAdtFwd( core_t &, long, const ast::AdtDecl * ) {}
     489
     490        template<typename core_t>
    479491        static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) {
    480492                ast::UnionDecl * fwd = new ast::UnionDecl( decl->location, decl->name );
     
    489501
    490502        template<typename core_t>
    491         static inline auto addAdtFwd( core_t & core, int, const ast::AdtDecl * decl ) -> decltype( core.symtab.addAdt( decl ), void() ) {
    492                 ast::AdtDecl * fwd = new ast::AdtDecl( decl->location, decl->name );
    493                 for ( const auto & param : decl->params ) {
    494                         fwd->params.push_back( deepCopy( param.get() ) );
    495                 }
    496                 // Experimental
    497                 for ( const auto & ctor : decl->data_constructors ) {
    498                         addStructFwd( core, 0, ctor  );
    499                 }
    500                 core.symtab.addAdt( fwd );
    501         }
    502 
    503         template<typename core_t>
    504         static inline auto addAdtFwd( core_t &, long, const ast::AdtDecl) {}
    505 
    506         template<typename core_t>
    507503        static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) {
    508504                if ( ! core.symtab.lookupStruct( str ) ) {
     
    513509        template<typename core_t>
    514510        static inline void addStruct( core_t &, long, const std::string & ) {}
     511
     512                template<typename core_t>
     513        static inline auto addAdt( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addAdt( str ), void() ) {
     514                if ( ! core.symtab.lookupAdt( str ) ) {
     515                        core.symtab.addAdt( str );
     516                }
     517        }
     518
     519        template<typename core_t>
     520        static inline void addAdt( core_t &, long, const std::string & ) {}
    515521
    516522        template<typename core_t>
  • src/AST/Print.cpp

    r044ae62 rfa2c005  
    15011501        }
    15021502
     1503        virtual const ast::Type * visit( const ast::AdtInstType * node ) override final {
     1504                preprint( node );
     1505                os << "instance of Adt " << node->name;
     1506                print( node->params );
     1507                return node;
     1508        }
     1509
    15031510        virtual const ast::Type * visit( const ast::UnionInstType * node ) override final {
    15041511                preprint( node );
  • src/AST/SymbolTable.cpp

    r044ae62 rfa2c005  
    218218}
    219219
     220const AdtDecl * SymbolTable::lookupAdt( const std::string &id ) const {
     221        ++*stats().lookup_calls;
     222        if ( ! adtTable ) return nullptr;
     223        auto it = adtTable->find( id );
     224        return it == adtTable->end() ? nullptr : it->second.decl;
     225}
     226
    220227const EnumDecl * SymbolTable::lookupEnum( const std::string &id ) const {
    221228        ++*stats().lookup_calls;
     
    339346}
    340347
    341 void SymbolTable::addEnum( const EnumDecl *decl ) {
    342         ++*stats().add_calls;
    343         const std::string &id = decl->name;
    344 
    345         if ( ! enumTable ) {
    346                 enumTable = EnumTable::new_ptr();
    347         } else {
    348                 ++*stats().map_lookups;
    349                 auto existing = enumTable->find( id );
    350                 if ( existing != enumTable->end()
    351                         && existing->second.scope == scope
    352                         && addedDeclConflicts( existing->second.decl, decl ) ) return;
    353         }
    354 
    355         lazyInitScope();
    356         ++*stats().map_mutations;
    357         enumTable = enumTable->set( id, scoped<EnumDecl>{ decl, scope } );
     348void SymbolTable::addAdt( const std::string &id ) {
     349        addAdt( new AdtDecl( CodeLocation(), id ) );
    358350}
    359351
     
    377369        adtTable = adtTable->set( id, scoped<AdtDecl>{ decl, scope });
    378370}
     371
     372void SymbolTable::addEnum( const EnumDecl *decl ) {
     373        ++*stats().add_calls;
     374        const std::string &id = decl->name;
     375
     376        if ( ! enumTable ) {
     377                enumTable = EnumTable::new_ptr();
     378        } else {
     379                ++*stats().map_lookups;
     380                auto existing = enumTable->find( id );
     381                if ( existing != enumTable->end()
     382                        && existing->second.scope == scope
     383                        && addedDeclConflicts( existing->second.decl, decl ) ) return;
     384        }
     385
     386        lazyInitScope();
     387        ++*stats().map_mutations;
     388        enumTable = enumTable->set( id, scoped<EnumDecl>{ decl, scope } );
     389}
     390
    379391
    380392void SymbolTable::addUnion( const std::string &id ) {
  • src/AST/SymbolTable.hpp

    r044ae62 rfa2c005  
    111111        /// Gets the top-most struct declaration with the given ID
    112112        const StructDecl * lookupStruct( const std::string &id ) const;
     113
     114        const AdtDecl * lookupAdt( const std::string &id ) const;
    113115        /// Gets the top-most enum declaration with the given ID
    114116        const EnumDecl * lookupEnum( const std::string &id ) const;
     
    141143        void addEnum( const EnumDecl * decl );
    142144        /// Adds an adt declaration to the symbol table
     145        void addAdt( const std::string & id );
    143146        void addAdt( const AdtDecl * decl );
    144147        /// Adds a union declaration to the symbol table by name
  • src/AST/Type.cpp

    r044ae62 rfa2c005  
    138138template class SueInstType<UnionDecl>;
    139139template class SueInstType<EnumDecl>;
     140template class SueInstType<AdtDecl>;
    140141
    141142// --- TraitInstType
  • src/AST/Visitor.hpp

    r044ae62 rfa2c005  
    111111    virtual const ast::Type *             visit( const ast::TraitInstType        * ) = 0;
    112112    virtual const ast::Type *             visit( const ast::TypeInstType         * ) = 0;
     113    virtual const ast::Type *             visit( const ast::AdtInstType          * ) = 0;
    113114    virtual const ast::Type *             visit( const ast::TupleType            * ) = 0;
    114115    virtual const ast::Type *             visit( const ast::TypeofType           * ) = 0;
Note: See TracChangeset for help on using the changeset viewer.