Changeset b0d9ff7 for src/Parser


Ignore:
Timestamp:
Sep 1, 2022, 1:27:52 PM (3 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
12df6fe
Parents:
def751f
Message:

Fix up the QualifiedNameExpr?. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

Location:
src/Parser
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/Parser/DeclarationNode.cc

    rdef751f rb0d9ff7  
    253253} // DeclarationNode::newAggregate
    254254
    255 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, DeclarationNode * base) {
     255DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, 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        newnode->type->enumeration.typed = typed;
    262263        if ( base && base->type)  {
    263264                newnode->type->base = base->type;       
    264265        } // if
    265266
    266         // Check: if base has TypeData
    267267        return newnode;
    268268} // DeclarationNode::newEnum
     
    284284
    285285DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
    286         if ( init ) { // list init {} or a singleInit
    287                 if ( init->get_expression() ) { // singleInit
     286        if ( init ) {
     287                if ( init->get_expression() ) {
    288288                        return newEnumConstant( name, init->get_expression() );
    289                 } else { // TODO: listInit
     289                } else {
    290290                        DeclarationNode * newnode = newName( name );
    291291                        newnode->initializer = init;
     
    293293                } // if
    294294        } else {
    295                 return newName( name ); // Not explicitly inited enum value;
     295                return newName( name );
    296296        } // if
    297297} // DeclarationNode::newEnumValueGeneric
  • TabularUnified src/Parser/ExpressionNode.cc

    rdef751f rb0d9ff7  
    510510
    511511QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) {
    512         Type * targetType = maybeMoveBuildType( decl_node );
    513         return new QualifiedNameExpr( targetType, name->name );
    514         return nullptr;
     512        Declaration * newDecl = maybeBuild< Declaration >(decl_node);
     513        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) {
     514                const Type * t = newDeclWithType->get_type();
     515                if ( t ) {
     516                        if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) {
     517                                newDecl= new EnumDecl( typeInst->name );
     518                        }
     519                }
     520        }
     521        auto ret =  new QualifiedNameExpr( newDecl, name->name );
     522        if ( auto e = dynamic_cast<EnumDecl*>(newDecl) ) {
     523                auto enumInst = new EnumInstType( Type::Qualifiers(), e );
     524                auto obj = new ObjectDecl( name->name, Type::StorageClasses(), LinkageSpec::Cforall, nullptr, enumInst, nullptr );
     525                ret->set_var( obj );
     526        }
     527        return ret;
     528}
     529
     530QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) {
     531        EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node );
     532        return new QualifiedNameExpr( newDecl, name->name );
    515533}
    516534
  • TabularUnified src/Parser/ParseNode.h

    rdef751f rb0d9ff7  
    184184NameExpr * build_varref( const std::string * name );
    185185QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name );
     186QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl, const NameExpr * name );
    186187DimensionExpr * build_dimensionref( const std::string * name );
    187188
     
    236237        static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
    237238        static DeclarationNode * newAggregate( AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
    238         static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, DeclarationNode * base = nullptr );
     239        static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base = nullptr );
    239240        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
    240241        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
  • TabularUnified src/Parser/TypeData.cc

    rdef751f rb0d9ff7  
    548548                return buildAggInst( td );
    549549          case TypeData::EnumConstant:
    550                 // the name gets filled in later -- by SymTab::Validate
    551550                return new EnumInstType( buildQualifiers( td ), "" );
    552551          case TypeData::SymbolicInst:
     
    923922        assert( td->kind == TypeData::Enum );
    924923        Type * baseType = td->base ? typebuild(td->base) : nullptr;
    925         EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage, baseType );
     924        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, td->enumeration.typed, linkage, baseType );
    926925        buildList( td->enumeration.constants, ret->get_members() );
    927926        list< Declaration * >::iterator members = ret->get_members().begin();
    928927        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
    929                 if ( cur->has_enumeratorValue() ) {
     928                if ( ret->isTyped && cur->has_enumeratorValue() ) {
     929                        SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." );
     930                } else if ( cur->has_enumeratorValue() ) {
    930931                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
    931932                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
    932933                } else if ( !cur->initializer ) {
    933934                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
    934                                 SemanticError( td->location, "A non whole number enum value decl must be explicitly initialized." );
     935                                SemanticError( td->location, "Enumerators of an non-integer typed enum must be explicitly initialized." );
    935936                        }
    936937                }
  • TabularUnified src/Parser/TypeData.h

    rdef751f rb0d9ff7  
    5959                bool body;
    6060                bool anon;
     61                bool typed;
    6162        };
    6263
  • TabularUnified src/Parser/parser.yy

    rdef751f rb0d9ff7  
    614614                { $$ = new ExpressionNode( new StmtExpr( dynamic_cast<CompoundStmt *>(maybeMoveBuild<Statement>($2) ) ) ); }
    615615        | type_name '.' identifier                                                      // CFA, nested type
    616                 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     616                { $$ = new ExpressionNode( build_qualified_expr( $1, build_varref( $3 ) ) ); }
    617617        | type_name '.' '[' field_name_list ']'                         // CFA, nested type / tuple field selector
    618618                { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
     
    23872387enum_type:
    23882388        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
    2389                 { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
     2389                { $$ = DeclarationNode::newEnum( nullptr, $4, true, false )->addQualifiers( $2 ); }
    23902390        | ENUM attribute_list_opt identifier
    23912391                { typedefTable.makeTypedef( *$3 ); }
    23922392          '{' enumerator_list comma_opt '}'
    2393                 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
     2393                { $$ = DeclarationNode::newEnum( $3, $6, true, false )->addQualifiers( $2 ); }
    23942394        | ENUM attribute_list_opt typedef_name                          // unqualified type name
    23952395          '{' enumerator_list comma_opt '}'
    2396                 { $$ = DeclarationNode::newEnum( $3->name, $5, true )->addQualifiers( $2 ); }
     2396                { $$ = DeclarationNode::newEnum( $3->name, $5, true, false )->addQualifiers( $2 ); }
    23972397        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
    23982398                {
     
    24002400                        { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
    24012401
    2402                         $$ = DeclarationNode::newEnum( nullptr, $7, true, $3 )->addQualifiers( $5 );
     2402                        $$ = DeclarationNode::newEnum( nullptr, $7, true, true, $3 )->addQualifiers( $5 );
     2403                }
     2404        | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
     2405                {
     2406                        $$ = DeclarationNode::newEnum( nullptr, $6, true, true )->addQualifiers( $4 );
    24032407                }
    24042408        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
     
    24092413          '{' enumerator_list comma_opt '}'
    24102414                {
    2411                         $$ = DeclarationNode::newEnum( $6, $10, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
    2412                 }
     2415                        $$ = DeclarationNode::newEnum( $6, $10, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
     2416                }
     2417        | ENUM '(' ')' attribute_list_opt identifier attribute_list_opt
     2418          '{' enumerator_list comma_opt '}'
     2419                {
     2420                        $$ = DeclarationNode::newEnum( $5, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
     2421                }       
    24132422        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
    24142423                {
    2415                         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." ); }
    2416                         typedefTable.makeTypedef( *$6->name );
    2417                         $$ = DeclarationNode::newEnum( $6->name, $9, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
     2424                        $$ = DeclarationNode::newEnum( $6->name, $9, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
     2425                }
     2426        | ENUM '(' ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
     2427                {
     2428                        $$ = DeclarationNode::newEnum( $5->name, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
    24182429                }
    24192430        | enum_type_nobody
     
    24222433enum_type_nobody:                                                                               // enum - {...}
    24232434        ENUM attribute_list_opt identifier
    2424                 { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 ); }
    2425         | ENUM attribute_list_opt type_name                                     // qualified type name
    2426                 { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false )->addQualifiers( $2 ); }
     2435                { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false, false )->addQualifiers( $2 ); }
     2436        | ENUM attribute_list_opt type_name     
     2437                { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false, false )->addQualifiers( $2 ); }
    24272438        ;
    24282439
Note: See TracChangeset for help on using the changeset viewer.