Changeset fa2c005


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

Finish Adt POC

Location:
src
Files:
34 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;
  • src/CodeGen/CodeGenerator.cc

    r044ae62 rfa2c005  
    296296
    297297        void CodeGenerator::handleData( EnumDecl * ) {
    298                 // output << " /** data type */" << endl;
    299                 // for ( StructDecl * decl : dataDecl->data_constructors ) {
    300                 //      postvisit(decl);
    301                 //      output << ";" << endl;
    302                 // }
    303                 // postvisit( dataDecl->data_union );
    304                 // output << ";" << endl;
    305                 // postvisit( dataDecl->tag );
    306                 // output << ";" << endl;
    307                 // postvisit( dataDecl->tag_union );
    308                 // output << ";" << endl;
    309298                assert(false);
    310299        }
    311300
    312301        void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
    313                 // if ( enumDecl->data_constructors.size() > 0 ) return handleData( enumDecl );
    314302                extension( enumDecl );
    315303                std::list< Declaration* > &memb = enumDecl->get_members();
     
    350338        }
    351339
    352         void CodeGenerator::postvisit( AdtDecl * ) {
    353                 // TODO
     340        // Temporary fix, But visitor should be able to handle this
     341        void CodeGenerator::postvisit( AdtDecl * adtDecl ) {
     342                output << endl << "/* AdtDecl Code Section */" << endl;
     343                postvisit( adtDecl->tag );
     344                output << ";" << endl;
     345                postvisit( adtDecl->data_union );
     346                output << ";" << endl;
     347                postvisit( adtDecl->tag_union );
    354348        }
    355349
  • src/Common/CodeLocationTools.cpp

    r044ae62 rfa2c005  
    185185    macro(FunctionType, Type) \
    186186    macro(StructInstType, Type) \
     187    macro(AdtInstType, Type) \
    187188    macro(UnionInstType, Type) \
    188189    macro(EnumInstType, Type) \
  • src/Common/PassVisitor.h

    r044ae62 rfa2c005  
    226226        virtual void visit( StructInstType * aggregateUseType ) override final;
    227227        virtual void visit( const StructInstType * aggregateUseType ) override final;
     228        virtual void visit( AdtInstType * aggregateUseType ) override final;
     229        virtual void visit( const AdtInstType * aggregateUseType ) override final;
    228230        virtual void visit( UnionInstType * aggregateUseType ) override final;
    229231        virtual void visit( const UnionInstType * aggregateUseType ) override final;
     
    351353        virtual Type * mutate( FunctionType * functionType ) override final;
    352354        virtual Type * mutate( StructInstType * aggregateUseType ) override final;
     355        virtual Type * mutate( AdtInstType * aggregateUseType ) override final;
    353356        virtual Type * mutate( UnionInstType * aggregateUseType ) override final;
    354357        virtual Type * mutate( EnumInstType * aggregateUseType ) override final;
     
    442445        void indexerAddStructFwd( const StructDecl          * node  ) { indexer_impl_addStructFwd( pass, 0, node ); }
    443446        void indexerAddEnum     ( const EnumDecl            * node  ) { indexer_impl_addEnum     ( pass, 0, node ); }
     447        void indexerAddAdt              ( const std::string                     & id    ) { indexer_impl_addAdt      ( pass, 0, id   ); }
    444448        void indexerAddAdt              ( const AdtDecl                         * node  ) { indexer_impl_addAdt          ( pass, 0, node ); }
    445449        void indexerAddAdtFwd   ( const AdtDecl                         * node  ) { indexer_impl_addAdtFwd   ( pass, 0, node  ); }
  • src/Common/PassVisitor.impl.h

    r044ae62 rfa2c005  
    688688
    689689//--------------------------------------------------------------------------
     690// AdtDecl
     691template< typename pass_type >
     692void PassVisitor< pass_type >::visit( AdtDecl * node ) {
     693        VISIT_START( node );
     694
     695        indexerAddAdtFwd( node );
     696
     697        maybeAccept_impl( node->data_union, *this );
     698        maybeAccept_impl( node->tag, *this );
     699
     700        maybeAccept_impl( node->parameters, *this );
     701        maybeAccept_impl( node->members   , *this );
     702        maybeAccept_impl( node->attributes, *this );
     703
     704        indexerAddAdt( node );
     705
     706        VISIT_END( node );
     707}
     708
     709template< typename pass_type >
     710void PassVisitor< pass_type >::visit( const AdtDecl * node ) {
     711        VISIT_START( node );
     712
     713        indexerAddAdtFwd( node );
     714
     715        maybeAccept_impl( node->data_union, *this );
     716        maybeAccept_impl( node->tag, *this );
     717
     718        maybeAccept_impl( node->parameters, *this );
     719        maybeAccept_impl( node->members   , *this );
     720        maybeAccept_impl( node->attributes, *this );
     721
     722        indexerAddAdt( node );
     723
     724        VISIT_END( node );
     725
     726
     727template< typename pass_type >
     728Declaration * PassVisitor< pass_type >::mutate( AdtDecl * node ) {
     729        MUTATE_START( node );
     730       
     731        indexerAddAdtFwd( node );
     732
     733        maybeMutate_impl( node->data_union, *this );
     734        maybeMutate_impl( node->tag, *this );
     735
     736        maybeMutate_impl( node->parameters, *this );
     737        maybeMutate_impl( node->members   , *this );
     738        maybeMutate_impl( node->attributes, *this );
     739
     740        indexerAddAdt( node );
     741
     742        MUTATE_END( Declaration, node );
     743}
     744
     745
     746//--------------------------------------------------------------------------
    690747// UnionDecl
    691748template< typename pass_type >
     
    789846}
    790847
    791 template< typename pass_type >
    792 void PassVisitor< pass_type >::visit( AdtDecl * node ) {
    793         VISIT_START( node );
    794 
    795         indexerAddAdtFwd( node );
    796 
    797         // unlike structs, traits, and unions, enums inject their members into the global scope
    798         maybeAccept_impl( node->data_constructors, *this );
    799         maybeAccept_impl( node->data_union, *this );
    800         maybeAccept_impl( node->tag, *this );
    801 
    802         maybeAccept_impl( node->parameters, *this );
    803         maybeAccept_impl( node->members   , *this );
    804         maybeAccept_impl( node->attributes, *this );
    805 
    806         VISIT_END( node );
    807 }
    808 
    809 template< typename pass_type >
    810 void PassVisitor< pass_type >::visit( const AdtDecl * node ) {
    811         VISIT_START( node );
    812 
    813         indexerAddAdtFwd( node );
    814 
    815         maybeAccept_impl( node->data_constructors, *this );
    816         maybeAccept_impl( node->data_union, *this );
    817         maybeAccept_impl( node->tag, *this );
    818 
    819         maybeAccept_impl( node->parameters, *this );
    820         maybeAccept_impl( node->members   , *this );
    821         maybeAccept_impl( node->attributes, *this );
    822 
    823 
    824         VISIT_END( node );
    825 
    826 
    827 template< typename pass_type >
    828 Declaration * PassVisitor< pass_type >::mutate( AdtDecl * node ) {
    829         MUTATE_START( node );
    830        
    831         indexerAddAdtFwd( node );
    832 
    833         maybeMutate_impl( node->data_constructors, *this );
    834         maybeMutate_impl( node->data_union, *this );
    835         maybeMutate_impl( node->tag, *this );
    836 
    837         maybeMutate_impl( node->parameters, *this );
    838         maybeMutate_impl( node->members   , *this );
    839         maybeMutate_impl( node->attributes, *this );
    840 
    841         MUTATE_END( Declaration, node );
    842 }
    843848
    844849//--------------------------------------------------------------------------
     
    35403545
    35413546        indexerAddStruct( node->name );
     3547
     3548        {
     3549                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     3550                maybeMutate_impl( node->forall    , *this );
     3551                maybeMutate_impl( node->parameters, *this );
     3552        }
     3553
     3554        MUTATE_END( Type, node );
     3555}
     3556
     3557//--------------------------------------------------------------------------
     3558// AdtInstType
     3559template< typename pass_type >
     3560void PassVisitor< pass_type >::visit( AdtInstType * node ) {
     3561        VISIT_START( node );
     3562
     3563        indexerAddAdt( node->name );
     3564
     3565        {
     3566                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     3567                maybeAccept_impl( node->forall    , *this );
     3568                maybeAccept_impl( node->parameters, *this );
     3569        }
     3570
     3571        VISIT_END( node );
     3572}
     3573
     3574template< typename pass_type >
     3575void PassVisitor< pass_type >::visit( const AdtInstType * node ) {
     3576        VISIT_START( node );
     3577
     3578        indexerAddAdt( node->name );
     3579
     3580        {
     3581                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     3582                maybeAccept_impl( node->forall    , *this );
     3583                maybeAccept_impl( node->parameters, *this );
     3584        }
     3585
     3586        VISIT_END( node );
     3587}
     3588
     3589template< typename pass_type >
     3590Type * PassVisitor< pass_type >::mutate( AdtInstType * node ) {
     3591        MUTATE_START( node );
     3592
     3593        indexerAddAdt( node->name );
    35423594
    35433595        {
  • src/Common/PassVisitor.proto.h

    r044ae62 rfa2c005  
    256256        cloneAll( decl->parameters, fwd->parameters );
    257257
    258         // Experimental
    259         for ( const StructDecl * ctor : fwd->data_constructors ) {
    260                 indexer_impl_addStructFwd( pass, 0, ctor );
    261         }
    262 
    263258        pass.indexer.addAdt( fwd );
    264259}
     
    288283
    289284template<typename pass_type>
     285static inline auto indexer_impl_addAdt( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addAdt( str ), void() ) {
     286        assert( false );
     287        if ( ! pass.indexer.lookupAdt( str )) {
     288                pass.indexer.addAdt( str );
     289        }
     290}
     291
     292template<typename pass_type>
     293static inline auto indexer_impl_addAdt( pass_type &, long, const std::string & ) {
     294        assert( false );
     295}
     296
     297template<typename pass_type>
    290298static inline auto indexer_impl_addUnion( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
    291299        if ( ! pass.indexer.lookupUnion( str ) ) {
  • src/GenPoly/Box.cc

    r044ae62 rfa2c005  
    185185                        void premutate( StructDecl * );
    186186                        void premutate( UnionDecl * );
     187                        void premutate( AdtDecl * );
    187188
    188189                        void beginScope();
     
    15131514                }
    15141515
     1516                void PolyGenericCalculator::premutate( AdtDecl * adtDecl ) {
     1517                        mutateMembers( adtDecl );
     1518                }
     1519
    15151520                void PolyGenericCalculator::premutate( DeclStmt *declStmt ) {
    15161521                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
  • src/Parser/DeclarationNode.cc

    r044ae62 rfa2c005  
    11041104        std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList );
    11051105        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
    1106                 // td->kind == TypeData::Symbolic
     1106
    11071107                assert( cur->type->kind == TypeData::Symbolic );
    11081108                const std::string * name = cur->name;
     
    11251125                        member->name = "field_" + std::to_string(i);
    11261126                }
     1127
    11271128                *out++ = ctor;         
    11281129        }
     
    11301131}
    11311132
    1132 ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1133void buildDataConstructorsAsMember( DeclarationNode * firstNode, ast::AdtDecl * adtDecl ) {
     1134        std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( adtDecl->members );
     1135        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
     1136
     1137                assert( cur->type->kind == TypeData::Symbolic );
     1138                const std::string * name = cur->name;
     1139                auto ctor = new ast::StructDecl( cur->location,
     1140                        std::string(*name),
     1141                        ast::AggregateDecl::Aggregate::Struct
     1142                );
     1143                ctor->set_body(true);
     1144                TypeData * td = cur->type;
     1145                TypeData::Symbolic_t st = td->symbolic;
     1146                DeclarationNode * params = st.params;
     1147               
     1148                if ( params ) {
     1149                        buildList( params, ctor->members );
     1150                }
     1151
     1152                for ( std::size_t i = 0; i < ctor->members.size(); ++i ) {
     1153                        assert(ctor->members[i]->name == "");
     1154                        ast::Decl * member = ctor->members[i].get_and_mutate();
     1155                        member->name = "field_" + std::to_string(i);
     1156                }
     1157
     1158                *out++ = ctor;         
     1159        }
     1160}
     1161
     1162ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::Decl>> & typeList ) {
    11331163        ast::UnionDecl * out = new ast::UnionDecl( loc, "temp_data_union" );
    11341164        // size_t index = 0;
    11351165        if ( typeList.size() > 0 ) out->set_body( true );
    11361166        size_t i = 0;
    1137         for (const ast::ptr<ast::StructDecl> structDecl : typeList ) {
    1138                 ast::StructInstType * inst = new ast::StructInstType(structDecl);
     1167        for (const ast::ptr<ast::Decl> structDecl : typeList ) {
     1168                ast::StructInstType * inst = new ast::StructInstType( structDecl.as< ast::StructDecl >() );
    11391169                ast::ObjectDecl * instObj = new ast::ObjectDecl(
    11401170                        structDecl->location,
     
    11491179}
    11501180
    1151 ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1181ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::Decl>> & typeList ) {
    11521182        ast::EnumDecl * out = new ast::EnumDecl( loc, "temp_data_tag" );
    11531183        if ( typeList.size() > 0 ) out->set_body( true );
    1154         for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     1184        for ( const ast::ptr<ast::Decl> structDecl : typeList ) {
    11551185                ast::EnumInstType * inst = new ast::EnumInstType( out );
    11561186                assert( inst->base != nullptr );
     
    11671197ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
    11681198        assert( tags->members.size() == data_union->members.size() );
    1169         ast::StructDecl * out = new ast::StructDecl( data->location, *(data->adt.name) );
     1199        ast::StructDecl * out = new ast::StructDecl( data->location, (*(data->adt.name)) + "_tag_union" );
    11701200        out->kind = ast::AggregateDecl::Adt;
    11711201
  • src/Parser/DeclarationNode.h

    r044ae62 rfa2c005  
    218218
    219219std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode );
    220 ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
    221 ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList );
     220void buildDataConstructorsAsMember( DeclarationNode * firstNode, ast::AdtDecl * adtDecl );
     221ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::Decl>> & typeList );
     222ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::Decl>> & typeList );
    222223ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tag, const ast::UnionDecl * data_union );
    223224
  • src/Parser/TypeData.cc

    r044ae62 rfa2c005  
    8181        case Symbolic:
    8282        case SymbolicInst:
     83                aggregate.kind =  ast::AggregateDecl::Aggregate::Undecided;
     84               
    8385                symbolic.name = nullptr;
    8486                symbolic.params = nullptr;
     
    225227                newtype->enumeration.body = enumeration.body;
    226228                newtype->enumeration.anon = enumeration.anon;
    227                 newtype->enumeration.data_constructors = maybeClone( enumeration.data_constructors );
    228229                break;
    229230        case Adt:
    230                 newtype->adt.data_constructors = maybeClone( enumeration.data_constructors );
     231                newtype->adt.data_constructors = maybeClone( adt.data_constructors );
    231232                newtype->adt.name = new string ( *adt.name );
    232233                break;
     
    12761277        );
    12771278        buildList( td->enumeration.constants, ret->members );
    1278         if ( td->enumeration.data_constructors != nullptr ) {
    1279                 assert( false );
    1280                 // ret->data_constructors = buildDataConstructors( td->enumeration.data_constructors );
    1281                 // ret->data_union = buildDataUnion( td->location, ret->data_constructors );
    1282                 // ret->tag = buildTag( td->location, ret->data_constructors );
    1283                 // ret->tag_union = buildTaggedUnions( td, ret->tag.get(), ret->data_union.get() );
    1284         }
    1285 
    1286         // if ( ret->data_constructors.size() > 0 ) ret->isData = true;
     1279
    12871280        auto members = ret->members.begin();
    12881281        ret->hide = td->enumeration.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible;
     
    13171310        assert( td->kind == TypeData::Adt );
    13181311        ast::AdtDecl * ret = new ast::AdtDecl( td->location, *(td->adt.name) );
    1319         ret->data_constructors = buildDataConstructors( td->adt.data_constructors );
    1320         ret->data_union = buildDataUnion( td->location, ret->data_constructors );
    1321         ret->tag = buildTag( td->location, ret->data_constructors );
     1312
     1313        buildDataConstructorsAsMember( td->adt.data_constructors, ret );
     1314
     1315        ret->data_union = buildDataUnion( td->location, ret->members );
     1316        ret->tag = buildTag( td->location, ret->members );
    13221317        ret->tag_union = buildTaggedUnions( td, ret->tag.get(), ret->data_union.get() );
    13231318        return ret;
  • src/Parser/TypeData.h

    r044ae62 rfa2c005  
    2525struct TypeData {
    2626        enum Kind { Basic, Pointer, Reference, Array, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic,
    27                                 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Adt, Ctor, Unknown };
     27                                SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Adt, Unknown };
    2828
    2929        struct Aggregate_t {
     
    5959                EnumHiding hiding;
    6060                bool isData = false;
    61 
    62                 DeclarationNode * data_constructors = nullptr;
    6361        };
    6462
  • src/SymTab/Validate.cc

    r044ae62 rfa2c005  
    210210                void previsit( StructDecl * aggregateDecl );
    211211                void previsit( UnionDecl * aggregateDecl );
     212                void previsit( AdtDecl * AggregateDecl );
    212213                void previsit( CompoundStmt * compoundStmt );
    213214        };
     
    569570
    570571        void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) {
     572                handleAggregate( aggregateDecl );
     573        }
     574
     575        void EliminateTypedef::previsit( AdtDecl * aggregateDecl ) {
    571576                handleAggregate( aggregateDecl );
    572577        }
  • src/SynTree/Declaration.h

    r044ae62 rfa2c005  
    365365        typedef AggregateDecl Parent;
    366366  public:
    367         std::list<StructDecl*> data_constructors;
    368367        UnionDecl * data_union;
    369368        EnumDecl * tag;
     
    373372         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
    374373         LinkageSpec::Spec linkage = LinkageSpec::Cforall,
    375          const std::list< StructDecl* > data_constructors = std::list< StructDecl * >(),
    376374         UnionDecl * data_union = nullptr, EnumDecl * tag = nullptr, StructDecl * tag_union = nullptr )
    377          : Parent( name, attributes, linkage ), data_constructors(data_constructors),
     375         : Parent( name, attributes, linkage ),
    378376         data_union( data_union ), tag( tag ), tag_union( tag_union ) {}
    379377
  • src/SynTree/Mutator.h

    r044ae62 rfa2c005  
    110110        virtual Type * mutate( FunctionType * functionType ) = 0;
    111111        virtual Type * mutate( StructInstType * aggregateUseType ) = 0;
     112        virtual Type * mutate( AdtInstType * aggregateUseType ) = 0;
    112113        virtual Type * mutate( UnionInstType * aggregateUseType ) = 0;
    113114        virtual Type * mutate( EnumInstType * aggregateUseType ) = 0;
  • src/SynTree/SynTree.h

    r044ae62 rfa2c005  
    119119class UnionInstType;
    120120class EnumInstType;
     121class AdtInstType;
    121122class TraitInstType;
    122123class TypeInstType;
  • src/SynTree/Type.h

    r044ae62 rfa2c005  
    476476};
    477477
     478class AdtInstType : public ReferenceToType {
     479        typedef ReferenceToType  Parent;
     480  public:
     481        AdtDecl * baseAdt;
     482
     483        AdtInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >()  ) : Parent( tq, name, attributes ), baseAdt( 0 ) {}
     484        AdtInstType( const Type::Qualifiers & tq, AdtDecl * baseAdt, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
     485        AdtInstType( const AdtInstType & other ) : Parent( other ), baseAdt( other.baseAdt ) {}
     486
     487        AdtDecl * get_baseAdt() const { return baseAdt; }
     488        void set_baseAdt( AdtDecl * newValue ) { baseAdt = newValue; }
     489       
     490        std::list<TypeDecl*> * get_baseParameters();
     491        const std::list<TypeDecl*> * get_baseParameters() const;
     492
     493        virtual bool isComplete() const override;
     494
     495        virtual AggregateDecl * getAggr() const override;
     496       
     497        virtual TypeSubstitution genericSubstitution() const override;
     498
     499        void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const override;
     500
     501        virtual AdtInstType * clone() const override { return new AdtInstType( * this ); }
     502        virtual void accept( Visitor & v ) override { v.visit( this ); }
     503        virtual void accept( Visitor & v ) const override { v.visit( this ); }
     504        virtual Type * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
     505
     506        virtual void print( std::ostream & os, Indenter indenter = {} ) const override;
     507  private:
     508        virtual std::string typeString() const override;
     509};
     510
    478511class UnionInstType : public ReferenceToType {
    479512        typedef ReferenceToType Parent;
  • src/SynTree/Visitor.h

    r044ae62 rfa2c005  
    196196        virtual void visit( UnionInstType * node ) { visit( const_cast<const UnionInstType *>(node) ); }
    197197        virtual void visit( const UnionInstType * aggregateUseType ) = 0;
     198        virtual void visit( AdtInstType * node ) { visit( const_cast<const AdtInstType *>(node) ); }
     199        virtual void visit( const AdtInstType * node ) = 0;
    198200        virtual void visit( EnumInstType * node ) { visit( const_cast<const EnumInstType *>(node) ); }
    199201        virtual void visit( const EnumInstType * aggregateUseType ) = 0;
  • src/Validate/EliminateTypedef.cpp

    r044ae62 rfa2c005  
    3131        ast::StructDecl const * previsit( ast::StructDecl const * decl );
    3232        ast::UnionDecl const * previsit( ast::UnionDecl const * decl );
     33        ast::AdtDecl const * previsit( ast::AdtDecl const * decl );
    3334        // Remove typedefs from statement lists.
    3435        ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt );
     
    6566}
    6667
     68ast::AdtDecl const * EliminateTypedefCore::previsit( ast::AdtDecl const * decl ) {
     69        return field_erase_if( decl, &ast::AdtDecl::members, isTypedef );
     70}
     71
    6772ast::CompoundStmt const * EliminateTypedefCore::previsit( ast::CompoundStmt const * stmt ) {
    6873        return field_erase_if( stmt, &ast::CompoundStmt::kids, isTypedefStmt );
  • src/Validate/FixQualifiedTypes.cpp

    r044ae62 rfa2c005  
    5959                                instp = inst;
    6060                        } else if ( auto inst = parent.as<ast::UnionInstType>() ) {
     61                                aggr = inst->base;
     62                                instp = inst;
     63                        } else if ( auto inst = parent.as<ast::AdtInstType>() ) {
    6164                                aggr = inst->base;
    6265                                instp = inst;
  • src/Validate/HoistStruct.cpp

    r044ae62 rfa2c005  
    2828        return dynamic_cast< ast::StructDecl const * >( decl )
    2929                || dynamic_cast< ast::UnionDecl const * >( decl )
    30                 || dynamic_cast< ast::StaticAssertDecl const * >( decl );
     30                || dynamic_cast< ast::StaticAssertDecl const * >( decl )
     31                || dynamic_cast< ast::AdtDecl const * >( decl );
    3132}
    3233
     
    4546        ast::UnionInstType const * previsit( ast::UnionInstType const * type );
    4647        ast::EnumInstType const * previsit( ast::EnumInstType const * type );
     48        ast::AdtDecl const * previsit( ast::AdtDecl const * decl );
     49        ast::AdtDecl const * postvisit( ast::AdtDecl const * decl );
    4750
    4851private:
     
    100103}
    101104
     105ast::AdtDecl const * HoistStructCore::previsit( ast::AdtDecl const * decl ) {
     106        return preAggregate( decl );
     107}
     108
     109ast::AdtDecl const * HoistStructCore::postvisit( ast::AdtDecl const * decl ) {
     110        return postAggregate( decl );
     111}
     112
    102113ast::StructDecl const * HoistStructCore::postvisit( ast::StructDecl const * decl ) {
    103114        return postAggregate( decl );
     
    138149}
    139150
    140 void hoistAdt( [[maybe_unused]] ast::TranslationUnit & trnaslationUnit ) {
    141        
    142 }
    143 
    144151} // namespace Validate
    145152
  • src/Validate/HoistStruct.hpp

    r044ae62 rfa2c005  
    2424/// Flattens nested type declarations. (Run right after Fix Qualified Types.)
    2525void hoistStruct( ast::TranslationUnit & translationUnit );
    26 void hoistAdt( ast::TranslationUnit & trnaslationUnit );
    2726
    2827}
  • src/Validate/LinkReferenceToTypes.cpp

    r044ae62 rfa2c005  
    2020#include "Validate/ForallPointerDecay.hpp"
    2121#include "Validate/NoIdSymbolTable.hpp"
     22#include <assert.h>
    2223
    2324namespace Validate {
     
    3536        ast::UnionInstType const * postvisit( ast::UnionInstType const * type );
    3637        ast::TraitInstType const * postvisit( ast::TraitInstType const * type );
     38        ast::AdtInstType const * postvisit( ast::AdtInstType const * type );
    3739        void previsit( ast::QualifiedType const * type );
    3840        void postvisit( ast::QualifiedType const * type );
     
    107109                auto mut = ast::mutate( type );
    108110                forwardStructs[ mut->name ].push_back( mut );
     111                type = mut;
     112        }
     113        return type;
     114}
     115
     116ast::AdtInstType const * LinkTypesCore::postvisit( ast::AdtInstType const * type ) {
     117        ast::AdtDecl const * decl = symtab.lookupAdt( type->name );
     118        assert( decl != nullptr );
     119        if ( decl ) {
     120                auto mut = ast::mutate( type );
     121                mut->base = const_cast<ast::AdtDecl *>( decl );
    109122                type = mut;
    110123        }
  • src/Validate/NoIdSymbolTable.hpp

    r044ae62 rfa2c005  
    4141        FORWARD_1( lookupUnion , const std::string & )
    4242        FORWARD_1( lookupTrait , const std::string & )
     43        FORWARD_1( lookupAdt   , const std::string & )
    4344        FORWARD_1( addType  , const ast::NamedTypeDecl * )
    4445        FORWARD_1( addStruct, const ast::StructDecl *    )
     
    4950        FORWARD_1( addStruct, const std::string &        )
    5051        FORWARD_1( addUnion , const std::string &        )
     52        FORWARD_1( addAdt   , const std::string &                )
    5153        FORWARD_2( addWith  , const std::vector< ast::ptr<ast::Expr> > &, const ast::Decl * )
    5254
  • src/Validate/ReplaceTypedef.cpp

    r044ae62 rfa2c005  
    6060        ast::StructDecl const * previsit( ast::StructDecl const * );
    6161        ast::UnionDecl const * previsit( ast::UnionDecl const * );
     62        ast::AdtDecl const * previsit( ast::AdtDecl const * );
    6263        void previsit( ast::EnumDecl const * );
    6364        void previsit( ast::TraitDecl const * );
     
    9091}
    9192
     93// Here, 5/30
    9294ast::Type const * ReplaceTypedefCore::postvisit(
    9395                ast::TypeInstType const * type ) {
    9496        // Instances of typedef types will come here. If it is an instance
    9597        // of a typedef type, link the instance to its actual type.
    96         TypedefMap::const_iterator def = typedefNames.find( type->name );
     98        TypedefMap::const_iterator def = typedefNames.find( type->name ); // because of this
    9799        if ( def != typedefNames.end() ) {
    98100                ast::Type * ret = ast::deepCopy( def->second.first->base );
     
    260262}
    261263
     264ast::AdtDecl const * ReplaceTypedefCore::previsit( ast::AdtDecl const * decl ) {
     265        visit_children = false;
     266        addImplicitTypedef( decl );
     267        return handleAggregate( decl );
     268}
     269
    262270ast::UnionDecl const * ReplaceTypedefCore::previsit( ast::UnionDecl const * decl ) {
    263271        visit_children = false;
     
    287295        } else if ( auto enumDecl = dynamic_cast<const ast::EnumDecl *>( aggrDecl ) ) {
    288296                type = new ast::EnumInstType( enumDecl->name );
     297        } else if ( auto adtDecl = dynamic_cast<const ast::AdtDecl *>( aggrDecl )) {
     298                type = new ast::AdtInstType( adtDecl->name );
    289299        }
    290300        assert( type );
Note: See TracChangeset for help on using the changeset viewer.