Changeset 374cb117


Ignore:
Timestamp:
Apr 19, 2022, 3:53:53 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
2686bc7
Parents:
75cd27b
Message:

Replace the interface for EnumDecl? node construction to support generic enum types

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r75cd27b r374cb117  
    27752775        }
    27762776
    2777         virtual void visit( const EnumInstType * old ) override final { // Here is visiting the EnumInst Decl not the usage.
     2777        virtual void visit( const EnumInstType * old ) override final {
    27782778                ast::EnumInstType * ty;
    27792779                if ( old->baseEnum ) {
    2780                         ty = new ast::EnumInstType{ // Probably here: missing the specification of the base
     2780                        ty = new ast::EnumInstType{
    27812781                                GET_ACCEPT_1( baseEnum, EnumDecl ),
    27822782                                cv( old ),
  • src/Parser/DeclarationNode.cc

    r75cd27b r374cb117  
    253253} // DeclarationNode::newAggregate
    254254
    255 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed) {
     255DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body) {
    256256        DeclarationNode * newnode = new DeclarationNode;
    257257        newnode->type = new TypeData( TypeData::Enum );
     
    272272} // DeclarationNode::newName
    273273
    274 DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) { // Marker
     274DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
    275275        DeclarationNode * newnode = newName( name );
    276276        newnode->enumeratorValue.reset( constant );
    277277        return newnode;
    278278} // DeclarationNode::newEnumConstant
     279
     280DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
     281        if ( init ) { // list init {} or a singleInit
     282                if ( init->get_expression() ) { // singleInit
     283                        return newEnumConstant( name, init->get_expression() );
     284                } else { // TODO: listInit
     285                        DeclarationNode * newnode = newName( name );
     286                        newnode->initializer = init;
     287                        return newnode;
     288                } // if
     289        } else {
     290                return newName( name ); // Not explicitly inited enum value;
     291        } // if
     292} // DeclarationNode::newEnumGeneric
    279293
    280294DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
  • src/Parser/InitializerNode.cc

    r75cd27b r374cb117  
    101101        } else {
    102102                if ( get_expression() ) {
     103                        assertf( get_expression()->expr, "The expression of initializer must have value" );
    103104                        return new SingleInit( maybeBuild< Expression >( get_expression() ), maybeConstructed );
    104105                } // if
  • src/Parser/ParseNode.h

    r75cd27b r374cb117  
    235235        static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
    236236        static DeclarationNode * newAggregate( AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
    237         static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed );
     237        static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body );
    238238        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
     239        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
    239240        static DeclarationNode * newName( const std::string * );
    240241        static DeclarationNode * newFromTypeGen( const std::string *, ExpressionNode * params );
  • src/Parser/parser.yy

    r75cd27b r374cb117  
    380380
    381381%type<decl> enumerator_list enum_type enum_type_nobody
    382 %type<en> enumerator_value_opt
     382%type<in> enumerator_value_opt
    383383
    384384%type<decl> external_definition external_definition_list external_definition_list_opt
     
    23052305enum_type: // static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed );                                                                                         // enum
    23062306        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
    2307                 { $$ = DeclarationNode::newEnum( nullptr, $4, true, false )->addQualifiers( $2 ); }
     2307                { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
    23082308        | ENUM attribute_list_opt identifier
    23092309                { typedefTable.makeTypedef( *$3 ); }
    23102310          '{' enumerator_list comma_opt '}'
    2311                 { $$ = DeclarationNode::newEnum( $3, $6, true, false )->addQualifiers( $2 ); }
     2311                { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
    23122312        | ENUM attribute_list_opt typedef_name                          // unqualified type name
    23132313          '{' enumerator_list comma_opt '}'
    2314                 { $$ = DeclarationNode::newEnum( $3->name, $5, true, false )->addQualifiers( $2 ); }
     2314                { $$ = DeclarationNode::newEnum( $3->name, $5, true )->addQualifiers( $2 ); }
    23152315        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
    23162316                {
    23172317                        if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 )
    23182318                        { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    2319                         // SemanticError( yylloc, "Typed enumeration is currently unimplemented." ); $$ = nullptr;
    2320 
    2321                         $$ = DeclarationNode::newEnum( nullptr, $7, true, true ) ->addQualifiers( $5 )  -> addEnumBase( $3 );
    2322                         // $$ = DeclarationNode::newEnum( nullptr, $7, true, true ) ->addQualifiers( $5 );
     2319
     2320                        $$ = DeclarationNode::newEnum( nullptr, $7, true ) ->addQualifiers( $5 )  -> addEnumBase( $3 );
    23232321                }
    23242322        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt // Question: why attributes/qualifier after identifier
     
    23292327          '{' enumerator_list comma_opt '}'
    23302328                {
    2331                         $$ = DeclarationNode::newEnum( $6, $10, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
    2332                         // $$ = DeclarationNode::newEnum( $6, $10, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
     2329                        $$ = DeclarationNode::newEnum( $6, $10, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
    23332330                }
    23342331        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
     
    23362333                        if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 ) { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    23372334                        typedefTable.makeTypedef( *$6->name );
    2338                         $$ = DeclarationNode::newEnum( $6->name, $9, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
    2339                         // $$ = DeclarationNode::newEnum( $6->name, $9, true, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
     2335                        $$ = DeclarationNode::newEnum( $6->name, $9, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
    23402336                }
    23412337        | enum_type_nobody
     
    23442340enum_type_nobody:                                                                               // enum - {...}
    23452341        ENUM attribute_list_opt identifier
    2346                 { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false, false )->addQualifiers( $2 ); }
     2342                { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 ); }
    23472343        | ENUM attribute_list_opt type_name                                     // qualified type name
    2348                 { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false, false )->addQualifiers( $2 ); }
     2344                { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false )->addQualifiers( $2 ); }
    23492345        ;
    23502346
    23512347enumerator_list:
    23522348        identifier_or_type_name enumerator_value_opt
    2353                 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
     2349                { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
    23542350        | INLINE type_name
    2355                 { $$ = DeclarationNode::newEnumConstant( new string("inline"), nullptr ); }
     2351                { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
    23562352        | enumerator_list ',' identifier_or_type_name enumerator_value_opt
    2357                 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
     2353                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
    23582354        | enumerator_list ',' INLINE type_name enumerator_value_opt
    2359                 { $$ = $1->appendList( DeclarationNode::newEnumConstant( new string("inline"), nullptr ) ); }
     2355                { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
    23602356        ;
    23612357
     
    23662362        //      { $$ = $2; }
    23672363        | simple_assignment_operator initializer
    2368                 { $$ = $2->get_expression(); }                                  // FIX ME: enum only deals with constant_expression
     2364                { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
    23692365        ;
    23702366
  • src/SynTree/Visitor.h

    r75cd27b r374cb117  
    3535        virtual void visit( UnionDecl * node ) { visit( const_cast<const UnionDecl *>(node) ); }
    3636        virtual void visit( const UnionDecl * aggregateDecl ) = 0;
    37         virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); } // Marker 1
     37        virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); }
    3838        virtual void visit( const EnumDecl * aggregateDecl ) = 0;
    3939        virtual void visit( TraitDecl * node ) { visit( const_cast<const TraitDecl *>(node) ); }
     
    190190        virtual void visit( UnionInstType * node ) { visit( const_cast<const UnionInstType *>(node) ); }
    191191        virtual void visit( const UnionInstType * aggregateUseType ) = 0;
    192         virtual void visit( EnumInstType * node ) { visit( const_cast<const EnumInstType *>(node) ); } // Marker 2
     192        virtual void visit( EnumInstType * node ) { visit( const_cast<const EnumInstType *>(node) ); }
    193193        virtual void visit( const EnumInstType * aggregateUseType ) = 0;
    194194        virtual void visit( TraitInstType * node ) { visit( const_cast<const TraitInstType *>(node) ); }
Note: See TracChangeset for help on using the changeset viewer.