Changeset fa2c005 for src/Parser


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/Parser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.