Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/parser.yy

    r3d937e2 r9fa61f5  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Feb 25 13:23:16 2023
    13 // Update Count     : 5989
     12// Last Modified On : Tue Mar 14 09:37:58 2023
     13// Update Count     : 5990
    1414//
    1515
     
    206206#define MISSING_HIGH "Missing high value for down-to range so index is uninitialized."
    207207
     208static ForCtrl * makeForCtrl(
     209                DeclarationNode * init,
     210                enum OperKinds compop,
     211                ExpressionNode * comp,
     212                ExpressionNode * inc ) {
     213        // Wrap both comp/inc if they are non-null.
     214        if ( comp ) comp = new ExpressionNode( build_binary_val(
     215                compop,
     216                new ExpressionNode( build_varref( new string( *init->name ) ) ),
     217                comp ) );
     218        if ( inc ) inc = new ExpressionNode( build_binary_val(
     219                // choose += or -= for upto/downto
     220                compop == OperKinds::LThan || compop == OperKinds::LEThan ? OperKinds::PlusAssn : OperKinds::MinusAssn,
     221                new ExpressionNode( build_varref( new string( *init->name ) ) ),
     222                inc ) );
     223        // The StatementNode call frees init->name, it must happen later.
     224        return new ForCtrl( new StatementNode( init ), comp, inc );
     225}
     226
    208227ForCtrl * forCtrl( DeclarationNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
    209228        if ( index->initializer ) {
     
    213232                SemanticError( yylloc, "Multiple loop indexes disallowed in for-loop declaration." );
    214233        } // if
    215         return new ForCtrl( index->addInitializer( new InitializerNode( start ) ),
    216                 // NULL comp/inc => leave blank
    217                 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index->name ) ) ), comp ) ) : nullptr,
    218                 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
    219                                                         OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index->name ) ) ), inc ) ) : nullptr );
     234        DeclarationNode * initDecl = index->addInitializer( new InitializerNode( start ) );
     235        return makeForCtrl( initDecl, compop, comp, inc );
    220236} // forCtrl
    221237
     
    223239        ConstantExpr * constant = dynamic_cast<ConstantExpr *>(type->expr.get());
    224240        if ( constant && (constant->get_constant()->get_value() == "0" || constant->get_constant()->get_value() == "1") ) {
    225                 type = new ExpressionNode( new CastExpr( maybeMoveBuild<Expression>(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
     241                type = new ExpressionNode( new CastExpr( maybeMoveBuild( type ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
    226242        } // if
    227 //      type = new ExpressionNode( build_func( new ExpressionNode( build_varref( new string( "__for_control_index_constraints__" ) ) ), type ) );
    228         return new ForCtrl(
    229                 distAttr( DeclarationNode::newTypeof( type, true ), DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) ),
    230                 // NULL comp/inc => leave blank
    231                 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index ) ) ), comp ) ) : nullptr,
    232                 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
    233                                                         OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index ) ) ), inc ) ) : nullptr );
     243        DeclarationNode * initDecl = distAttr(
     244                DeclarationNode::newTypeof( type, true ),
     245                DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) )
     246        );
     247        return makeForCtrl( initDecl, compop, comp, inc );
    234248} // forCtrl
    235249
     
    649663                { $$ = $2; }
    650664        | '(' compound_statement ')'                                            // GCC, lambda expression
    651                 { $$ = new ExpressionNode( new StmtExpr( dynamic_cast<CompoundStmt *>(maybeMoveBuild<Statement>($2) ) ) ); }
     665                { $$ = new ExpressionNode( new StmtExpr( dynamic_cast<CompoundStmt *>(maybeMoveBuild( $2 ) ) ) ); }
    652666        | type_name '.' identifier                                                      // CFA, nested type
    653667                { $$ = new ExpressionNode( build_qualified_expr( $1, build_varref( $3 ) ) ); }
     
    657671                {
    658672                        // add the missing control expression to the GenericExpr and return it
    659                         $5->control = maybeMoveBuild<Expression>( $3 );
     673                        $5->control = maybeMoveBuild( $3 );
    660674                        $$ = new ExpressionNode( $5 );
    661675                }
     
    693707                {
    694708                        // create a GenericExpr wrapper with one association pair
    695                         $$ = new GenericExpr( nullptr, { { maybeMoveBuildType($1), maybeMoveBuild<Expression>( $3 ) } } );
     709                        $$ = new GenericExpr( nullptr, { { maybeMoveBuildType($1), maybeMoveBuild( $3 ) } } );
    696710                }
    697711        | DEFAULT ':' assignment_expression
    698                 { $$ = new GenericExpr( nullptr, { { maybeMoveBuild<Expression>( $3 ) } } ); }
     712                { $$ = new GenericExpr( nullptr, { { maybeMoveBuild( $3 ) } } ); }
    699713        ;
    700714
     
    751765                { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $4 ) ) ); }
    752766        | postfix_expression ICR
    753                 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::IncrPost, $1 ) ); }
     767                { $$ = new ExpressionNode( build_unary_val( OperKinds::IncrPost, $1 ) ); }
    754768        | postfix_expression DECR
    755                 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::DecrPost, $1 ) ); }
     769                { $$ = new ExpressionNode( build_unary_val( OperKinds::DecrPost, $1 ) ); }
    756770        | '(' type_no_function ')' '{' initializer_list_opt comma_opt '}' // C99, compound-literal
    757771                { $$ = new ExpressionNode( build_compoundLiteral( $2, new InitializerNode( $5, true ) ) ); }
     
    793807        field_name
    794808        | FLOATING_DECIMALconstant field
    795                 { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), maybeMoveBuild<Expression>( $2 ) ) ); }
     809                { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), maybeMoveBuild( $2 ) ) ); }
    796810        | FLOATING_DECIMALconstant '[' field_name_list ']'
    797811                { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), build_tuple( $3 ) ) ); }
    798812        | field_name '.' field
    799                 { $$ = new ExpressionNode( build_fieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
     813                { $$ = new ExpressionNode( build_fieldSel( $1, maybeMoveBuild( $3 ) ) ); }
    800814        | field_name '.' '[' field_name_list ']'
    801815                { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $4 ) ) ); }
    802816        | field_name ARROW field
    803                 { $$ = new ExpressionNode( build_pfieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
     817                { $$ = new ExpressionNode( build_pfieldSel( $1, maybeMoveBuild( $3 ) ) ); }
    804818        | field_name ARROW '[' field_name_list ']'
    805819                { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $4 ) ) ); }
     
    843857                        switch ( $1 ) {
    844858                          case OperKinds::AddressOf:
    845                                 $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) );
     859                                $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild( $2 ) ) );
    846860                                break;
    847861                          case OperKinds::PointTo:
     
    849863                                break;
    850864                          case OperKinds::And:
    851                                 $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) ) );
     865                                $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild( $2 ) ) ) );
    852866                                break;
    853867                          default:
     
    858872                { $$ = new ExpressionNode( build_unary_val( $1, $2 ) ); }
    859873        | ICR unary_expression
    860                 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Incr, $2 ) ); }
     874                { $$ = new ExpressionNode( build_unary_val( OperKinds::Incr, $2 ) ); }
    861875        | DECR unary_expression
    862                 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
     876                { $$ = new ExpressionNode( build_unary_val( OperKinds::Decr, $2 ) ); }
    863877        | SIZEOF unary_expression
    864                 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
     878                { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild( $2 ) ) ); }
    865879        | SIZEOF '(' type_no_function ')'
    866880                { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuildType( $3 ) ) ); }
    867881        | ALIGNOF unary_expression                                                      // GCC, variable alignment
    868                 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
     882                { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild( $2 ) ) ); }
    869883        | ALIGNOF '(' type_no_function ')'                                      // GCC, type alignment
    870884                { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuildType( $3 ) ) ); }
     
    901915                { $$ = new ExpressionNode( build_keyword_cast( $2, $5 ) ); }
    902916        | '(' VIRTUAL ')' cast_expression                                       // CFA
    903                 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $4 ), maybeMoveBuildType( nullptr ) ) ); }
     917                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild( $4 ), maybeMoveBuildType( nullptr ) ) ); }
    904918        | '(' VIRTUAL type_no_function ')' cast_expression      // CFA
    905                 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $5 ), maybeMoveBuildType( $3 ) ) ); }
     919                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild( $5 ), maybeMoveBuildType( $3 ) ) ); }
    906920        | '(' RETURN type_no_function ')' cast_expression       // CFA
    907921                { SemanticError( yylloc, "Return cast is currently unimplemented." ); $$ = nullptr; }
     
    10921106        assignment_expression
    10931107        | comma_expression ',' assignment_expression
    1094                 { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
     1108                { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
    10951109        ;
    10961110
     
    12301244        constant_expression                                                     { $$ = $1; }
    12311245        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
    1232                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
     1246                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
    12331247        | subrange                                                                                      // CFA, subrange
    12341248        ;
     
    12981312                { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ), $8 ) ); }
    12991313        | FOR '(' ')' statement                                                         %prec THEN // CFA => for ( ;; )
    1300                 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); }
     1314                { $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) ); }
    13011315        | FOR '(' ')' statement ELSE statement                          // CFA
    13021316                {
    1303                         $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) );
     1317                        $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) );
    13041318                        SemanticWarning( yylloc, Warning::SuperfluousElse );
    13051319                }
     
    13351349for_control_expression:
    13361350        ';' comma_expression_opt ';' comma_expression_opt
    1337                 { $$ = new ForCtrl( (ExpressionNode * )nullptr, $2, $4 ); }
     1351                { $$ = new ForCtrl( nullptr, $2, $4 ); }
    13381352        | comma_expression ';' comma_expression_opt ';' comma_expression_opt
    1339                 { $$ = new ForCtrl( $1, $3, $5 ); }
     1353                {
     1354                        StatementNode * init = $1 ? new StatementNode( new ExprStmt( maybeMoveBuild( $1 ) ) ) : nullptr;
     1355                        $$ = new ForCtrl( init, $3, $5 );
     1356                }
    13401357        | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';'
    1341                 { $$ = new ForCtrl( $1, $2, $4 ); }
     1358                { $$ = new ForCtrl( new StatementNode( $1 ), $2, $4 ); }
    13421359
    13431360        | '@' ';' comma_expression                                                      // CFA, empty loop-index
    1344                 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, nullptr ); }
     1361                { $$ = new ForCtrl( nullptr, $3, nullptr ); }
    13451362        | '@' ';' comma_expression ';' comma_expression         // CFA, empty loop-index
    1346                 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, $5 ); }
     1363                { $$ = new ForCtrl( nullptr, $3, $5 ); }
    13471364
    13481365        | comma_expression                                                                      // CFA, anonymous loop-index
     
    17321749asm_operand:                                                                                    // GCC
    17331750        string_literal '(' constant_expression ')'
    1734                 { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild<Expression>( $3 ) ) ); }
     1751                { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild( $3 ) ) ); }
    17351752        | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
    1736                 { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild<Expression>( $6 ) ) ); }
     1753                { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild( $6 ) ) ); }
    17371754        ;
    17381755
     
    20382055        basic_type_specifier
    20392056        | sue_type_specifier
    2040                 {
    2041                         // printf( "sue_type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
    2042                         // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
    2043                         //   printf( "\tattr %s\n", attr->name.c_str() );
    2044                         // } // for
    2045                 }
    20462057        | type_type_specifier
    20472058        ;
     
    23832394          '{' field_declaration_list_opt '}' type_parameters_opt
    23842395                {
    2385                         // printf( "aggregate_type1 %s\n", $3.str->c_str() );
    2386                         // if ( $2 )
    2387                         //      for ( Attribute * attr: reverseIterate( $2->attributes ) ) {
    2388                         //              printf( "copySpecifiers12 %s\n", attr->name.c_str() );
    2389                         //      } // for
    23902396                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
    2391                         // printf( "aggregate_type2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
    2392                         // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
    2393                         //      printf( "aggregate_type3 %s\n", attr->name.c_str() );
    2394                         // } // for
    23952397                }
    23962398        | aggregate_key attribute_list_opt TYPEDEFname          // unqualified type name
     
    24012403          '{' field_declaration_list_opt '}' type_parameters_opt
    24022404                {
    2403                         // printf( "AGG3\n" );
    24042405                        DeclarationNode::newFromTypedef( $3 );
    24052406                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
     
    24122413          '{' field_declaration_list_opt '}' type_parameters_opt
    24132414                {
    2414                         // printf( "AGG4\n" );
    24152415                        DeclarationNode::newFromTypeGen( $3, nullptr );
    24162416                        $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
     
    24392439                        // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
    24402440                        // delete newFromTypeGen.
    2441                         $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
    2442                         $3->type->symbolic.name = nullptr;
    2443                         $3->type->symbolic.actuals = nullptr;
    2444                         delete $3;
     2441                        if ( $3->type->kind == TypeData::SymbolicInst && ! $3->type->symbolic.isTypedef ) {
     2442                                $$ = $3->addQualifiers( $2 );
     2443                        } else {
     2444                                $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
     2445                                $3->type->symbolic.name = nullptr;                      // copied to $$
     2446                                $3->type->symbolic.actuals = nullptr;
     2447                                delete $3;
     2448                        }
    24452449                }
    24462450        ;
     
    27792783type_no_function:                                                                               // sizeof, alignof, cast (constructor)
    27802784        cfa_abstract_declarator_tuple                                           // CFA
    2781         | type_specifier
     2785        | type_specifier                                                                        // cannot be type_specifier_nobody, e.g., (struct S {}){} is a thing
    27822786        | type_specifier abstract_declarator
    27832787                { $$ = $2->addType( $1 ); }
     
    28432847                { $$ = $3; }
    28442848        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
    2845                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
     2849                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild( $3 ), maybeMoveBuild( $5 ) ) ); }
    28462850        | '.' '[' push field_name_list pop ']'                          // CFA, tuple field selector
    28472851                { $$ = $4; }
     
    32313235subrange:
    32323236        constant_expression '~' constant_expression                     // CFA, integer subrange
    3233                 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
     3237                { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
    32343238        ;
    32353239
Note: See TracChangeset for help on using the changeset viewer.