Changeset 9e7236f4 for src/Parser


Ignore:
Timestamp:
May 2, 2022, 3:18:32 AM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
24ceace
Parents:
2686bc7
Message:

Resolution of struct enum. The codegen of struct enum will be in the next commit

Location:
src/Parser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r2686bc7 r9e7236f4  
    253253} // DeclarationNode::newAggregate
    254254
    255 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body) {
     255DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, DeclarationNode * base) {
    256256        DeclarationNode * newnode = new DeclarationNode;
    257257        newnode->type = new TypeData( TypeData::Enum );
     
    260260        newnode->type->enumeration.body = body;
    261261        newnode->type->enumeration.anon = name == nullptr;
     262        if ( base && base->type)  {
     263                newnode->type->base = base->type;       
     264        } // if
     265
     266        // Check: if base has TypeData
    262267        return newnode;
    263268} // DeclarationNode::newEnum
     
    290295                return newName( name ); // Not explicitly inited enum value;
    291296        } // if
    292 } // DeclarationNode::newEnumGeneric
     297} // DeclarationNode::newEnumValueGeneric
    293298
    294299DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
  • src/Parser/ParseNode.h

    r2686bc7 r9e7236f4  
    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 );
     237        static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, DeclarationNode * base = nullptr );
    238238        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
    239239        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
  • src/Parser/TypeData.cc

    r2686bc7 r9e7236f4  
    388388                if ( enumeration.body ) {
    389389                        os << string( indent + 2, ' ' ) << " with body" << endl;
     390                } // if
     391                if ( base ) {
     392                        os << "for ";
     393                        base->print( os, indent + 2 );
    390394                } // if
    391395                break;
     
    926930                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
    927931                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
    928                 } else {
     932                } else if ( !cur->initializer ) {
    929933                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
    930934                                SemanticError( td->location, "A non whole number enum value decl must be explicitly initialized." );
    931935                        }
    932                 } // if
     936                }
     937                // else cur is a List Initializer and has been set as init in buildList()
     938                // if
    933939        } // for
    934         ret->set_body( td->enumeration.body ); // Boolean; if it has body
     940        ret->set_body( td->enumeration.body );
    935941        return ret;
    936942} // buildEnum
  • src/Parser/parser.yy

    r2686bc7 r9e7236f4  
    23032303        ;
    23042304
    2305 enum_type: // static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed );                                                                                         // enum
     2305enum_type:
    23062306        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
    23072307                { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
     
    23182318                        { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    23192319
    2320                         $$ = DeclarationNode::newEnum( nullptr, $7, true ) ->addQualifiers( $5 )  -> addEnumBase( $3 );
    2321                 }
    2322         | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt // Question: why attributes/qualifier after identifier
     2320                        $$ = DeclarationNode::newEnum( nullptr, $7, true, $3 ) ->addQualifiers( $5 );
     2321                }
     2322        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
    23232323                {
    23242324                        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." ); }
     
    23272327          '{' enumerator_list comma_opt '}'
    23282328                {
    2329                         $$ = DeclarationNode::newEnum( $6, $10, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
     2329                        $$ = DeclarationNode::newEnum( $6, $10, true, $3 ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
    23302330                }
    23312331        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
     
    23332333                        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." ); }
    23342334                        typedefTable.makeTypedef( *$6->name );
    2335                         $$ = DeclarationNode::newEnum( $6->name, $9, true ) -> addQualifiers( $5 ) -> addQualifiers( $7 ) -> addEnumBase( $3 );
     2335                        $$ = DeclarationNode::newEnum( $6->name, $9, true, $3 ) -> addQualifiers( $5 ) -> addQualifiers( $7 );
    23362336                }
    23372337        | enum_type_nobody
Note: See TracChangeset for help on using the changeset viewer.