Changeset 561354f


Ignore:
Timestamp:
May 17, 2023, 1:33:39 AM (12 months ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT
Children:
d6c464d
Parents:
28f8f15
Message:

Save progress

Location:
src
Files:
30 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r28f8f15 r561354f  
    283283                decl->parent = get<AggregateDecl>().accept1( node->parent );
    284284                declPostamble( decl, node );
    285                 return nullptr; // ??
     285                return nullptr;
    286286        }
    287287
     
    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->tags = get<EnumDecl>().accept1( node->tag );
    326                 decl->tag_union = get<StructDecl>().accept1( node->tag_union );
     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 );
     327                return aggregatePostamble( decl, node );
     328        }
     329
     330        const ast::Decl * visit( const ast::AdtDecl * node ) override final {
     331                if ( inCache(node) ) return nullptr;
     332                auto decl = new AdtDecl(
     333                        node->name,
     334                        get<Attribute>().acceptL( node->attributes ),
     335                        LinkageSpec::Spec( node->linkage.val ),
     336                        get<StructDecl>().acceptL( node->data_constructors ),
     337                        get<UnionDecl>().accept1( node->data_union ),
     338                        get<EnumDecl>().accept1( node->tag ),
     339                        get<StructDecl>().accept1( node->tag_union )
     340                );
    327341                return aggregatePostamble( decl, node );
    328342        }
     
    17821796        }
    17831797
     1798        virtual void visit( const AdtDecl * old ) override final {
     1799                if ( inCache( old ) ) return;
     1800                auto decl = new ast::AdtDecl(
     1801                        old->location,
     1802                        old->name,
     1803                        GET_ACCEPT_V(attributes, Attribute),
     1804                        { old->linkage.val }
     1805                );
     1806                cache.emplace( old, decl );
     1807                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
     1808                decl->body = old->body;
     1809                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
     1810                decl->members = GET_ACCEPT_V(members, Decl);
     1811                decl->extension = old->extension;
     1812                decl->uniqueId = old->uniqueId;
     1813                decl->storage = { old->storageClasses.val };
     1814                decl->data_constructors = GET_ACCEPT_V( data_constructors, StructDecl );
     1815                decl->data_union = GET_ACCEPT_1( data_union, UnionDecl );
     1816                decl->tag = GET_ACCEPT_1( tag, EnumDecl );
     1817                decl->tag_union = GET_ACCEPT_1( tag_union, StructDecl );
     1818                this->node = decl;
     1819        }
     1820
    17841821        virtual void visit( const TraitDecl * old ) override final {
    17851822                if ( inCache( old ) ) return;
  • src/AST/Decl.cpp

    r28f8f15 r561354f  
    132132
    133133// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
    134 static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName", "data" };
     134static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName", "adt" };
    135135
    136136const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
  • src/AST/Decl.hpp

    r28f8f15 r561354f  
    248248class AggregateDecl : public Decl {
    249249public:
    250         enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, ADT };
     250        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, Adt };
    251251        static const char * aggrString( Aggregate aggr );
    252252
     
    286286        bool is_monitor  () const { return kind == Monitor  ; }
    287287        bool is_thread   () const { return kind == Thread   ; }
    288         bool is_adt              () const { return kind == ADT          ; }
     288        bool is_adt              () const { return kind == Adt          ; }
    289289
    290290        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     
    320320        ptr<Type> base; // if isTyped == true && base.get() == nullptr, it is a "void" type enum
    321321        enum class EnumHiding { Visible, Hide } hide;
    322 
    323         std::vector<ptr<StructDecl>> data_constructors;
    324         bool isData = false;
    325         ptr<UnionDecl> data_union;
    326         ptr<EnumDecl> tag;
    327         ptr<StructDecl> tag_union;
    328322
    329323        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
     
    350344};
    351345
     346class AdtDecl final : public AggregateDecl {
     347public:
     348        std::vector<ptr<StructDecl>> data_constructors; // Todo: members?
     349        ptr<UnionDecl> data_union;
     350        ptr<EnumDecl> tag;
     351        ptr<StructDecl> tag_union;
     352
     353        AdtDecl( const CodeLocation& loc, const std::string& name,
     354                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     355        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
     356
     357        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     358
     359        const char * typeString() const override { return aggrString( Adt ); }
     360
     361private:
     362        AdtDecl * clone() const override { return new AdtDecl{ *this }; }
     363        MUTATE_FRIEND
     364};
     365
    352366/// trait declaration `trait Foo( ... ) { ... };`
    353367class TraitDecl final : public AggregateDecl {
     
    358372
    359373        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    360 
    361374        const char * typeString() const override { return "trait"; }
    362375
  • src/AST/Fwd.hpp

    r28f8f15 r561354f  
    3232class UnionDecl;
    3333class EnumDecl;
     34class AdtDecl;
    3435class TraitDecl;
    3536class NamedTypeDecl;
  • src/AST/Node.cpp

    r28f8f15 r561354f  
    128128template class ast::ptr_base< ast::EnumDecl, ast::Node::ref_type::weak >;
    129129template class ast::ptr_base< ast::EnumDecl, ast::Node::ref_type::strong >;
     130template class ast::ptr_base< ast::AdtDecl, ast::Node::ref_type::weak >;
     131template class ast::ptr_base< ast::AdtDecl, ast::Node::ref_type::strong >;
    130132template class ast::ptr_base< ast::TraitDecl, ast::Node::ref_type::weak >;
    131133template class ast::ptr_base< ast::TraitDecl, ast::Node::ref_type::strong >;
  • src/AST/Pass.hpp

    r28f8f15 r561354f  
    139139        const ast::Decl *             visit( const ast::UnionDecl            * ) override final;
    140140        const ast::Decl *             visit( const ast::EnumDecl             * ) override final;
     141        const ast::Decl *                         visit( const ast::AdtDecl                              * ) override final;
    141142        const ast::Decl *             visit( const ast::TraitDecl            * ) override final;
    142143        const ast::Decl *             visit( const ast::TypeDecl             * ) override final;
  • src/AST/Pass.impl.hpp

    r28f8f15 r561354f  
    693693                        maybe_accept( node, &EnumDecl::members    );
    694694                        maybe_accept( node, &EnumDecl::attributes );
    695                         maybe_accept( node, &EnumDecl::data_constructors );
    696                         maybe_accept( node, &EnumDecl::data_union );
    697                         maybe_accept( node, &EnumDecl::tag );
    698                         maybe_accept( node, &EnumDecl::tag_union );
     695
     696                        // maybe_accept( node, &EnumDecl::data_constructors );
     697                        // maybe_accept( node, &EnumDecl::data_union );
     698                        // maybe_accept( node, &EnumDecl::tag );
     699                        // maybe_accept( node, &EnumDecl::tag_union );
    699700                } else {
    700701                        maybe_accept( node, &EnumDecl::base );
     
    702703                        maybe_accept( node, &EnumDecl::members    );
    703704                        maybe_accept( node, &EnumDecl::attributes );
    704                         maybe_accept( node, &EnumDecl::data_constructors );
    705                         maybe_accept( node, &EnumDecl::data_union );
    706                         maybe_accept( node, &EnumDecl::tag );
    707                         maybe_accept( node, &EnumDecl::tag_union );
    708                 }
     705
     706                        // maybe_accept( node, &EnumDecl::data_constructors );
     707                        // maybe_accept( node, &EnumDecl::data_union );
     708                        // maybe_accept( node, &EnumDecl::tag );
     709                        // maybe_accept( node, &EnumDecl::tag_union );
     710                }
     711        }
     712
     713        VISIT_END( Decl, node );
     714}
     715
     716template< typename core_t >
     717const ast::Decl * ast::Pass< core_t >::visit( const ast::AdtDecl * node ) {
     718        VISIT_START( node );
     719
     720        __pass::symtab::addAdt( core, 0, node );
     721
     722        if ( __visit_children() ) {
     723                guard_symtab guard { *this };
     724                maybe_accept( node, &AdtDecl::params );
     725                maybe_accept( node, &AdtDecl::members );
     726                maybe_accept( node, &AdtDecl::attributes );
     727
     728                maybe_accept( node, &AdtDecl::data_constructors );
     729                maybe_accept( node, &AdtDecl::data_union );
     730                maybe_accept( node, &AdtDecl::tag );
     731                maybe_accept( node, &AdtDecl::tag_union );
    709732        }
    710733
  • src/AST/Pass.proto.hpp

    r28f8f15 r561354f  
    399399        SYMTAB_FUNC1( addStruct , const StructDecl *    );
    400400        SYMTAB_FUNC1( addEnum   , const EnumDecl *      );
     401        SYMTAB_FUNC1( addAdt    , const AdtDecl *           );
    401402        SYMTAB_FUNC1( addUnion  , const UnionDecl *     );
    402403        SYMTAB_FUNC1( addTrait  , const TraitDecl *     );
  • src/AST/Print.cpp

    r28f8f15 r561354f  
    407407
    408408        virtual const ast::Decl * visit( const ast::EnumDecl * node ) override final {
     409                print(node);
     410                return node;
     411        }
     412
     413        virtual const ast::Decl * visit( const ast::AdtDecl * node ) override final {
    409414                print(node);
    410415                return node;
  • src/AST/SymbolTable.cpp

    r28f8f15 r561354f  
    358358}
    359359
     360void SymbolTable::addAdt( const AdtDecl *decl ) {
     361        ++*stats().add_calls;
     362        const std::string &id = decl->name;
     363
     364        if ( ! adtTable ) {
     365                adtTable = AdtTable::new_ptr();
     366        } else {
     367                ++*stats().map_lookups;
     368                auto existing = adtTable->find( id );
     369                if ( existing != adtTable->end()
     370                        && existing->second.scope == scope
     371                        && addedDeclConflicts( existing->second.decl, decl ) ) return;
     372       
     373        }
     374
     375        lazyInitScope();
     376        ++*stats().map_mutations;
     377        adtTable = adtTable->set( id, scoped<AdtDecl>{ decl, scope });
     378}
     379
    360380void SymbolTable::addUnion( const std::string &id ) {
    361381        addUnion( new UnionDecl( CodeLocation(), id ) );
  • src/AST/SymbolTable.hpp

    r28f8f15 r561354f  
    7272        using StructTable = PersistentMap< std::string, scoped<StructDecl> >;
    7373        using EnumTable = PersistentMap< std::string, scoped<EnumDecl> >;
     74        using AdtTable = PersistentMap< std::string, scoped<AdtDecl> >;
    7475        using UnionTable = PersistentMap< std::string, scoped<UnionDecl> >;
    7576        using TraitTable = PersistentMap< std::string, scoped<TraitDecl> >;
     
    8081        EnumTable::Ptr enumTable;      ///< enum namespace
    8182        UnionTable::Ptr unionTable;    ///< union namespace
     83        AdtTable::Ptr adtTable;            ///< adt namespace
    8284        TraitTable::Ptr traitTable;    ///< trait namespace
    8385        IdTable::Ptr specialFunctionTable[NUMBER_OF_KINDS];
     
    138140        /// Adds an enum declaration to the symbol table
    139141        void addEnum( const EnumDecl * decl );
     142        /// Adds an adt declaration to the symbol table
     143        void addAdt( const AdtDecl * decl );
    140144        /// Adds a union declaration to the symbol table by name
    141145        void addUnion( const std::string & id );
  • src/AST/Visitor.hpp

    r28f8f15 r561354f  
    2727    virtual const ast::Decl *             visit( const ast::UnionDecl            * ) = 0;
    2828    virtual const ast::Decl *             visit( const ast::EnumDecl             * ) = 0;
     29    virtual const ast::Decl *             visit( const ast::AdtDecl              * ) = 0;
    2930    virtual const ast::Decl *             visit( const ast::TraitDecl            * ) = 0;
    3031    virtual const ast::Decl *             visit( const ast::TypeDecl             * ) = 0;
  • src/CodeGen/CodeGenerator.cc

    r28f8f15 r561354f  
    296296
    297297        void CodeGenerator::handleData( EnumDecl * dataDecl ) {
    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->tags );
    306                 output << ";" << endl;
    307                 postvisit( dataDecl->tag_union );
    308                 output << ";" << endl;
     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;
     309                assert(false);
    309310        }
    310311
    311312        void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
    312                 if ( enumDecl->data_constructors.size() > 0 ) return handleData( enumDecl );
     313                // if ( enumDecl->data_constructors.size() > 0 ) return handleData( enumDecl );
    313314                extension( enumDecl );
    314315                std::list< Declaration* > &memb = enumDecl->get_members();
  • src/Common/CodeLocationTools.cpp

    r28f8f15 r561354f  
    105105    macro(UnionDecl, Decl) \
    106106    macro(EnumDecl, Decl) \
     107        macro(AdtDecl, Decl) \
    107108    macro(TraitDecl, Decl) \
    108109    macro(TypeDecl, Decl) \
  • src/Common/PassVisitor.h

    r28f8f15 r561354f  
    6969        virtual void visit( EnumDecl * aggregateDecl ) override final;
    7070        virtual void visit( const EnumDecl * aggregateDecl ) override final;
     71        virtual void visit( AdtDecl * aggregateDecl ) override final;
     72        virtual void visit( const AdtDecl * AggregateDecl ) override final;
    7173        virtual void visit( TraitDecl * aggregateDecl ) override final;
    7274        virtual void visit( const TraitDecl * aggregateDecl ) override final;
     
    269271        virtual Declaration * mutate( UnionDecl * aggregateDecl ) override final;
    270272        virtual Declaration * mutate( EnumDecl * aggregateDecl ) override final;
     273        virtual Declaration * mutate( AdtDecl * aggregateDecl ) override final;
    271274        virtual Declaration * mutate( TraitDecl * aggregateDecl ) override final;
    272275        virtual Declaration * mutate( TypeDecl * typeDecl ) override final;
     
    439442        void indexerAddStructFwd( const StructDecl          * node  ) { indexer_impl_addStructFwd( pass, 0, node ); }
    440443        void indexerAddEnum     ( const EnumDecl            * node  ) { indexer_impl_addEnum     ( pass, 0, node ); }
     444        void indexerAddAdt              ( const AdtDecl                         * node  ) { indexer_impl_addAdt          ( pass, 0, node ); }
    441445        void indexerAddUnion    ( const std::string         & id    ) { indexer_impl_addUnion    ( pass, 0, id   ); }
    442446        void indexerAddUnion    ( const UnionDecl           * node  ) { indexer_impl_addUnion    ( pass, 0, node ); }
  • src/Common/PassVisitor.impl.h

    r28f8f15 r561354f  
    754754
    755755        // unlike structs, traits, and unions, enums inject their members into the global scope
    756         maybeAccept_impl( node->data_constructors, *this );
    757         maybeAccept_impl( node->data_union, *this );
    758         maybeAccept_impl( node->tags, *this );
    759756        maybeAccept_impl( node->parameters, *this );
    760757        maybeAccept_impl( node->members   , *this );
     
    785782
    786783        // unlike structs, traits, and unions, enums inject their members into the global scope
     784        maybeMutate_impl( node->parameters, *this );
     785        maybeMutate_impl( node->members   , *this );
     786        maybeMutate_impl( node->attributes, *this );
     787
     788        MUTATE_END( Declaration, node );
     789}
     790
     791template< typename pass_type >
     792void PassVisitor< pass_type >::visit( AdtDecl * node ) {
     793        VISIT_START( node );
     794
     795        indexerAddAdt( 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
     809template< typename pass_type >
     810void PassVisitor< pass_type >::visit( const AdtDecl * node ) {
     811        VISIT_START( node );
     812
     813        maybeAccept_impl( node->data_constructors, *this );
     814        maybeAccept_impl( node->data_union, *this );
     815        maybeAccept_impl( node->tag, *this );
     816
     817        maybeAccept_impl( node->parameters, *this );
     818        maybeAccept_impl( node->members   , *this );
     819        maybeAccept_impl( node->attributes, *this );
     820
     821
     822        VISIT_END( node );
     823
     824
     825template< typename pass_type >
     826Declaration * PassVisitor< pass_type >::mutate( AdtDecl * node ) {
     827        MUTATE_START( node );
     828
     829        maybeMutate_impl( node->data_constructors, *this );
     830        maybeMutate_impl( node->data_union, *this );
     831        maybeMutate_impl( node->tag, *this );
     832
    787833        maybeMutate_impl( node->parameters, *this );
    788834        maybeMutate_impl( node->members   , *this );
  • src/Common/PassVisitor.proto.h

    r28f8f15 r561354f  
    233233INDEXER_FUNC1( addStruct , const StructDecl *                );
    234234INDEXER_FUNC1( addEnum   , const EnumDecl *                  );
     235INDEXER_FUNC1( addAdt    , const AdtDecl *                                       );
    235236INDEXER_FUNC1( addUnion  , const UnionDecl *                 );
    236237INDEXER_FUNC1( addTrait  , const TraitDecl *                 );
  • src/Parser/DeclarationNode.cc

    r28f8f15 r561354f  
    279279} // DeclarationNode::newEnum
    280280
    281 DeclarationNode * DeclarationNode::newADT( const string * name, DeclarationNode * constructors ) {
    282         DeclarationNode * newnode = newEnum( name, nullptr, true, false );
    283         newnode->type->enumeration.isData = true;
    284         newnode->type->enumeration.data_constructors = constructors;
    285         return newnode;
    286 }
     281DeclarationNode * DeclarationNode::newAdt( const string * name, DeclarationNode * constructors ) {
     282        assert( name );
     283        DeclarationNode * newnode = new DeclarationNode;
     284        newnode->type = new TypeData( TypeData::Adt );
     285        newnode->type->adt.name = name;
     286        newnode->type->adt.data_constructors = constructors;
     287        return newnode;
     288} // DeclarationNode::newAdt
    287289
    288290
     
    10981100}
    10991101
    1100 void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList ) {
     1102std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode ) {
     1103        std::vector<ast::ptr<ast::StructDecl>> outputList;
    11011104        std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList );
    11021105        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
     
    11241127                *out++ = ctor;         
    11251128        }
    1126 }
    1127 
    1128 ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
    1129         ast::UnionDecl * out = new ast::UnionDecl( data->location, "temp_data_union" );
     1129        return outputList;
     1130}
     1131
     1132ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1133        ast::UnionDecl * out = new ast::UnionDecl( loc, "temp_data_union" );
    11301134        // size_t index = 0;
    11311135        if ( typeList.size() > 0 ) out->set_body( true );
     
    11451149}
    11461150
    1147 ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
    1148         ast::EnumDecl * out = new ast::EnumDecl( data->location, "temp_data_tag" );
     1151ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1152        ast::EnumDecl * out = new ast::EnumDecl( loc, "temp_data_tag" );
    11491153        if ( typeList.size() > 0 ) out->set_body( true );
    11501154        for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     
    11611165}
    11621166
    1163 ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
     1167ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
    11641168        assert( tags->members.size() == data_union->members.size() );
    1165         ast::StructDecl * out = new ast::StructDecl( data->location, data->name );
    1166         out->kind = ast::AggregateDecl::ADT;
     1169        ast::StructDecl * out = new ast::StructDecl( data->location, *(data->adt.name) );
     1170        out->kind = ast::AggregateDecl::Adt;
    11671171
    11681172        out->set_body( true );
  • src/Parser/DeclarationNode.h

    r28f8f15 r561354f  
    7777
    7878        // Experimental algebric data type
    79         static DeclarationNode * newADT( const std::string * name, DeclarationNode * constructors );
     79        static DeclarationNode * newAdt( const std::string * name, DeclarationNode * constructors );
    8080        static DeclarationNode * newDataConstructor( const std::string * name );
    8181        // static DeclarationNode * newDataConstructor( const std::string * name, DeclarationNode * typeSpecifiers );
     
    216216void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
    217217void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
    218 void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList );
    219 ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
    220 ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
    221 ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union );
     218
     219std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode );
     220ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
     221ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList );
     222ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tag, const ast::UnionDecl * data_union );
    222223
    223224template<typename AstType, typename NodeType,
  • src/Parser/TypeData.cc

    r28f8f15 r561354f  
    5959                enumeration.anon = false;
    6060                break;
     61        case Adt:
     62                adt.name = nullptr;
     63                adt.data_constructors = nullptr;
     64                break;
    6165        case Aggregate:
    6266                aggregate.kind = ast::AggregateDecl::NoAggregate;
     
    160164                delete qualified.child;
    161165                break;
     166        case Adt:
     167                delete adt.data_constructors;
     168                delete adt.name;
     169                break;
    162170        } // switch
    163171} // TypeData::~TypeData
     
    217225                newtype->enumeration.body = enumeration.body;
    218226                newtype->enumeration.anon = enumeration.anon;
     227                newtype->enumeration.data_constructors = maybeClone( enumeration.data_constructors );
     228                break;
     229        case Adt:
     230                newtype->adt.data_constructors = maybeClone( enumeration.data_constructors );
     231                newtype->adt.name = new string ( *adt.name );
    219232                break;
    220233        case Symbolic:
     
    459472        case Enum:
    460473                return enumeration.name;
     474        case Adt:
     475                return adt.name;
    461476        case Symbolic:
    462477        case SymbolicInst:
     
    822837        case TypeData::Symbolic:
    823838        case TypeData::Enum:
     839        case TypeData::Adt:
    824840        case TypeData::Aggregate:
    825841                assert( false );
     
    12611277        buildList( td->enumeration.constants, ret->members );
    12621278        if ( td->enumeration.data_constructors != nullptr ) {
    1263                 buildDataConstructors( td->enumeration.data_constructors, ret->data_constructors );
    1264                 ret->data_union = buildDataUnion( ret, ret->data_constructors );
    1265                 ret->tag = buildTag( ret, ret->data_constructors );
    1266                 ret->tag_union = buildTaggedUnions( ret, ret->tag.get(), ret->data_union.get() );
     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() );
    12671284        }
    12681285
    1269         if ( ret->data_constructors.size() > 0 ) ret->isData = true;
     1286        // if ( ret->data_constructors.size() > 0 ) ret->isData = true;
    12701287        auto members = ret->members.begin();
    12711288        ret->hide = td->enumeration.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible;
     
    12941311        return ret;
    12951312} // buildEnum
     1313
     1314ast::AdtDecl * buildAdt(const TypeData * td,
     1315        std::vector<ast::ptr<ast::Attribute>> && attributes,
     1316        ast::Linkage::Spec linkage ) {
     1317        assert( td->kind == TypeData::Adt );
     1318        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 );
     1322        ret->tag_union = buildTaggedUnions( td, ret->tag.get(), ret->data_union.get() );
     1323        return ret;
     1324}
    12961325
    12971326
     
    14371466        } else if ( td->kind == TypeData::Enum ) {
    14381467                return buildEnum( td, std::move( attributes ), linkage );
     1468        } else if ( td->kind == TypeData::Adt) {
     1469                return buildAdt( td, std::move( attributes), linkage );
    14391470        } else if ( td->kind == TypeData::Symbolic ) {
    14401471                return buildSymbolic( td, std::move( attributes ), name, scs, linkage );
  • src/Parser/TypeData.h

    r28f8f15 r561354f  
    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, Ctor, Unknown };
    2828
    2929        struct Aggregate_t {
     
    6565        struct ADT_t {
    6666                const std::string * name = nullptr;
    67                 DeclarationNode * constructors;
    68         };
    69 
    70         struct Constructor_t {
    71                 const std::string * name;
    72                 DeclarationNode * type; // types?
     67                DeclarationNode * data_constructors;
    7368        };
    7469
     
    112107        Enumeration_t enumeration;
    113108        ADT_t adt;
    114         Constructor_t data_constructor;
    115109
    116110        Function_t function;
     
    140134ast::TypeDecl * buildVariable( const TypeData * );
    141135ast::EnumDecl * buildEnum( const TypeData *, std::vector<ast::ptr<ast::Attribute>> &&, ast::Linkage::Spec );
     136ast::EnumDecl * buildAst( const TypeData *, std::vector<ast::ptr<ast::Attribute>> &&, ast::Linkage::Spec );
    142137ast::TypeInstType * buildSymbolicInst( const TypeData * );
    143138ast::TupleType * buildTuple( const TypeData * );
  • src/Parser/parser.yy

    r28f8f15 r561354f  
    27022702         '{' value_list '}'
    27032703         {
    2704                 $$ = DeclarationNode::newADT( $2, $5 );
     2704                $$ = DeclarationNode::newAdt( $2, $5 );
    27052705         }
    27062706        ;
  • src/SymTab/Indexer.cc

    r28f8f15 r561354f  
    606606        }
    607607
     608        void Indexer::addAdt( const AdtDecl * decl ) {
     609                ++*stats().add_calls;
     610                const std::string & id = decl->name;
     611
     612                if ( ! adtTable ) {
     613                        adtTable = AdtTable::new_ptr();
     614                } else {
     615                        ++* stats().map_lookups;
     616                        auto existing = adtTable->find( id );
     617                        if ( existing != adtTable->end()
     618                                && existing->second.scope == scope
     619                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
     620
     621                }
     622
     623                lazyInitScope();
     624                ++* stats().map_mutations;
     625                adtTable = adtTable->set( id, Scoped<AdtDecl>{ decl, scope} );
     626        }
     627
    608628        void Indexer::addUnion( const std::string & id ) {
    609629                addUnion( new UnionDecl( id ) );
  • src/SymTab/Indexer.h

    r28f8f15 r561354f  
    8989                void addStruct( const StructDecl * decl );
    9090                void addEnum( const EnumDecl * decl );
     91                void addAdt( const AdtDecl * decl );
    9192                void addUnion( const std::string & id );
    9293                void addUnion( const UnionDecl * decl );
     
    124125                using UnionTable = PersistentMap< std::string, Scoped<UnionDecl> >;
    125126                using TraitTable = PersistentMap< std::string, Scoped<TraitDecl> >;
     127                using AdtTable = PersistentMap< std::string, Scoped<AdtDecl> >;
    126128
    127129                IdTable::Ptr idTable;          ///< identifier namespace
     
    131133                UnionTable::Ptr unionTable;    ///< union namespace
    132134                TraitTable::Ptr traitTable;    ///< trait namespace
     135                AdtTable::Ptr adtTable;            ///< adt namespace
    133136
    134137                Ptr prevScope;                 ///< reference to indexer for parent scope
  • src/SynTree/Declaration.h

    r28f8f15 r561354f  
    342342        enum EnumHiding { Visible, Hide } hide;
    343343
    344         std::list<StructDecl*> data_constructors;
    345         UnionDecl * data_union;
    346         EnumDecl * tags;
    347         StructDecl * tag_union;
    348 
    349344        EnumDecl( const std::string & name,
    350345         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
     
    367362};
    368363
     364class AdtDecl : public AggregateDecl {
     365        typedef AggregateDecl Parent;
     366  public:
     367        std::list<StructDecl*> data_constructors;
     368        UnionDecl * data_union;
     369        EnumDecl * tag;
     370        StructDecl * tag_union;
     371
     372        AdtDecl( const std::string & name,
     373         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
     374         LinkageSpec::Spec linkage = LinkageSpec::Cforall,
     375         const std::list< StructDecl* > data_constructors = std::list< StructDecl * >(),
     376         UnionDecl * data_union = nullptr, EnumDecl * tag = nullptr, StructDecl * tag_union = nullptr )
     377         : Parent( name, attributes, linkage ), data_constructors(data_constructors),
     378         data_union( data_union ), tag( tag ), tag_union( tag_union ) {}
     379
     380        AdtDecl( const AdtDecl & other )
     381         : Parent( other ) {}
     382
     383        virtual AdtDecl * clone() const override { return new AdtDecl( *this ); }
     384        virtual void accept( Visitor & v ) override { v.visit( this ); }
     385        virtual void accept( Visitor & v ) const override { v.visit( this ); }
     386
     387        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
     388        virtual void print( std::ostream & os, Indenter indent = {} ) const override final {
     389                os << "AdtDecl ... " << indent;
     390        }
     391       
     392private:
     393        virtual const char * typeString() const override {
     394                return "AdtDecl";
     395        }
     396};
     397
    369398class TraitDecl : public AggregateDecl {
    370399        typedef AggregateDecl Parent;
  • src/SynTree/Mutator.h

    r28f8f15 r561354f  
    3030        virtual Declaration * mutate( UnionDecl * aggregateDecl ) = 0;
    3131        virtual Declaration * mutate( EnumDecl * aggregateDecl ) = 0;
     32        virtual Declaration * mutate( AdtDecl * aggregateDecl ) = 0;
    3233        virtual Declaration * mutate( TraitDecl * aggregateDecl ) = 0;
    3334        virtual Declaration * mutate( TypeDecl * typeDecl ) = 0;
  • src/SynTree/SynTree.h

    r28f8f15 r561354f  
    3131class UnionDecl;
    3232class EnumDecl;
     33class AdtDecl;
    3334class TraitDecl;
    3435class NamedTypeDecl;
  • src/SynTree/Visitor.h

    r28f8f15 r561354f  
    3737        virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); }
    3838        virtual void visit( const EnumDecl * aggregateDecl ) = 0;
     39        virtual void visit( AdtDecl * node ) { visit( const_cast<const AdtDecl *>(node) ); }
     40        virtual void visit( const AdtDecl * node ) = 0;
    3941        virtual void visit( TraitDecl * node ) { visit( const_cast<const TraitDecl *>(node) ); }
    4042        virtual void visit( const TraitDecl * aggregateDecl ) = 0;
  • src/Validate/Autogen.cpp

    r28f8f15 r561354f  
    522522
    523523void StructFuncGenerator::genADTFuncs() {
    524         if ( decl->kind != ast::AggregateDecl::ADT ) return;
     524        if ( decl->kind != ast::AggregateDecl::Adt ) return;
    525525        assert( decl->members.size() == 2 );
    526526        auto first = (decl->members[0]).as<ast::ObjectDecl>();
  • src/Validate/NoIdSymbolTable.hpp

    r28f8f15 r561354f  
    4444        FORWARD_1( addStruct, const ast::StructDecl *    )
    4545        FORWARD_1( addEnum  , const ast::EnumDecl *      )
     46        FORWARD_1( addAdt,    const ast::AdtDecl *               )
    4647        FORWARD_1( addUnion , const ast::UnionDecl *     )
    4748        FORWARD_1( addTrait , const ast::TraitDecl *     )
Note: See TracChangeset for help on using the changeset viewer.