Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/parser.yy

    rc86b08d r561354f  
    4848using namespace std;
    4949
    50 #include "SynTree/Declaration.h"
    51 #include "ParseNode.h"
     50#include "SynTree/Type.h"                               // for Type
     51#include "DeclarationNode.h"                            // for DeclarationNode, ...
     52#include "ExpressionNode.h"                             // for ExpressionNode, ...
     53#include "InitializerNode.h"                            // for InitializerNode, ...
     54#include "ParserTypes.h"
     55#include "StatementNode.h"                              // for build_...
    5256#include "TypedefTable.h"
    5357#include "TypeData.h"
    54 #include "SynTree/LinkageSpec.h"
    5558#include "Common/SemanticError.h"                                               // error_str
    5659#include "Common/utility.h"                                                             // for maybeMoveBuild, maybeBuild, CodeLo...
     
    297300%union {
    298301        Token tok;
    299         ParseNode * pn;
    300         ExpressionNode * en;
     302        ExpressionNode * expr;
    301303        DeclarationNode * decl;
    302304        ast::AggregateDecl::Aggregate aggKey;
    303305        ast::TypeDecl::Kind tclass;
    304         StatementNode * sn;
     306        StatementNode * stmt;
     307        ClauseNode * clause;
    305308        ast::WaitForStmt * wfs;
    306     ast::WaitUntilStmt::ClauseNode * wuscn;
    307         ast::Expr * constant;
    308309        CondCtl * ifctl;
    309         ForCtrl * fctl;
    310         OperKinds compop;
    311         LabelNode * label;
    312         InitializerNode * in;
    313         OperKinds op;
     310        ForCtrl * forctl;
     311        LabelNode * labels;
     312        InitializerNode * init;
     313        OperKinds oper;
    314314        std::string * str;
    315         bool flag;
    316         EnumHiding hide;
    317         ast::ExceptionKind catch_kind;
     315        bool is_volatile;
     316        EnumHiding enum_hiding;
     317        ast::ExceptionKind except_kind;
    318318        ast::GenericExpr * genexpr;
    319319}
     
    339339%token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE                   // GCC
    340340%token OFFSETOF BASETYPEOF TYPEID                                               // CFA
    341 %token ENUM STRUCT UNION
     341%token ENUM STRUCT UNION DATA
    342342%token EXCEPTION                                                                                // CFA
    343343%token GENERATOR COROUTINE MONITOR THREAD                               // CFA
     
    381381%type<tok> identifier                                   identifier_at                           identifier_or_type_name         attr_name
    382382%type<tok> quasi_keyword
    383 %type<constant> string_literal
     383%type<expr> string_literal
    384384%type<str> string_literal_list
    385385
    386 %type<hide> hide_opt                                    visible_hide_opt
     386%type<enum_hiding> hide_opt                                     visible_hide_opt
    387387
    388388// expressions
    389 %type<en> constant
    390 %type<en> tuple                                                 tuple_expression_list
    391 %type<op> ptrref_operator                               unary_operator                          assignment_operator                     simple_assignment_operator      compound_assignment_operator
    392 %type<en> primary_expression                    postfix_expression                      unary_expression
    393 %type<en> cast_expression_list                  cast_expression                         exponential_expression          multiplicative_expression       additive_expression
    394 %type<en> shift_expression                              relational_expression           equality_expression
    395 %type<en> AND_expression                                exclusive_OR_expression         inclusive_OR_expression
    396 %type<en> logical_AND_expression                logical_OR_expression
    397 %type<en> conditional_expression                constant_expression                     assignment_expression           assignment_expression_opt
    398 %type<en> comma_expression                              comma_expression_opt
    399 %type<en> argument_expression_list_opt  argument_expression_list        argument_expression                     default_initializer_opt
     389%type<expr> constant
     390%type<expr> tuple                                                       tuple_expression_list
     391%type<oper> ptrref_operator                             unary_operator                          assignment_operator                     simple_assignment_operator      compound_assignment_operator
     392%type<expr> primary_expression                  postfix_expression                      unary_expression
     393%type<expr> cast_expression_list                        cast_expression                         exponential_expression          multiplicative_expression       additive_expression
     394%type<expr> shift_expression                            relational_expression           equality_expression
     395%type<expr> AND_expression                              exclusive_OR_expression         inclusive_OR_expression
     396%type<expr> logical_AND_expression              logical_OR_expression
     397%type<expr> conditional_expression              constant_expression                     assignment_expression           assignment_expression_opt
     398%type<expr> comma_expression                            comma_expression_opt
     399%type<expr> argument_expression_list_opt        argument_expression_list        argument_expression                     default_initializer_opt
    400400%type<ifctl> conditional_declaration
    401 %type<fctl> for_control_expression              for_control_expression_list
    402 %type<compop> upupeq updown updowneq downupdowneq
    403 %type<en> subrange
     401%type<forctl> for_control_expression            for_control_expression_list
     402%type<oper> upupeq updown updowneq downupdowneq
     403%type<expr> subrange
    404404%type<decl> asm_name_opt
    405 %type<en> asm_operands_opt                              asm_operands_list                       asm_operand
    406 %type<label> label_list
    407 %type<en> asm_clobbers_list_opt
    408 %type<flag> asm_volatile_opt
    409 %type<en> handler_predicate_opt
     405%type<expr> asm_operands_opt                            asm_operands_list                       asm_operand
     406%type<labels> label_list
     407%type<expr> asm_clobbers_list_opt
     408%type<is_volatile> asm_volatile_opt
     409%type<expr> handler_predicate_opt
    410410%type<genexpr> generic_association              generic_assoc_list
    411411
    412412// statements
    413 %type<sn> statement                                             labeled_statement                       compound_statement
    414 %type<sn> statement_decl                                statement_decl_list                     statement_list_nodecl
    415 %type<sn> selection_statement                   if_statement
    416 %type<sn> switch_clause_list_opt                switch_clause_list
    417 %type<en> case_value
    418 %type<sn> case_clause                                   case_value_list                         case_label                                      case_label_list
    419 %type<sn> iteration_statement                   jump_statement
    420 %type<sn> expression_statement                  asm_statement
    421 %type<sn> with_statement
    422 %type<en> with_clause_opt
    423 %type<sn> exception_statement                   handler_clause                          finally_clause
    424 %type<catch_kind> handler_key
    425 %type<sn> mutex_statement
    426 %type<en> when_clause                                   when_clause_opt                         waitfor         waituntil               timeout
    427 %type<sn> waitfor_statement                             waituntil_statement
    428 %type<wfs> wor_waitfor_clause
    429 %type<wuscn> waituntil_clause                   wand_waituntil_clause       wor_waituntil_clause
     413%type<stmt> statement                                           labeled_statement                       compound_statement
     414%type<stmt> statement_decl                              statement_decl_list                     statement_list_nodecl
     415%type<stmt> selection_statement                 if_statement
     416%type<clause> switch_clause_list_opt            switch_clause_list
     417%type<expr> case_value
     418%type<clause> case_clause                               case_value_list                         case_label                                      case_label_list
     419%type<stmt> iteration_statement                 jump_statement
     420%type<stmt> expression_statement                        asm_statement
     421%type<stmt> with_statement
     422%type<expr> with_clause_opt
     423%type<stmt> exception_statement
     424%type<clause> handler_clause                    finally_clause
     425%type<except_kind> handler_key
     426%type<stmt> mutex_statement
     427%type<expr> when_clause                                 when_clause_opt                         waitfor         waituntil               timeout
     428%type<stmt> waitfor_statement                           waituntil_statement
     429%type<wfs> wor_waitfor_clause                   waituntil_clause                        wand_waituntil_clause   wor_waituntil_clause
    430430
    431431// declarations
     
    439439%type<decl> assertion assertion_list assertion_list_opt
    440440
    441 %type<en> bit_subrange_size_opt bit_subrange_size
     441%type<expr> bit_subrange_size_opt bit_subrange_size
    442442
    443443%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
     
    452452
    453453%type<decl> enumerator_list enum_type enum_type_nobody
    454 %type<in> enumerator_value_opt
     454%type<init> enumerator_value_opt
     455
     456%type<decl> value_list
     457%type<decl> data_constructor type_specifier_list
    455458
    456459%type<decl> external_definition external_definition_list external_definition_list_opt
     
    459462
    460463%type<decl> field_declaration_list_opt field_declaration field_declaring_list_opt field_declarator field_abstract_list_opt field_abstract
    461 %type<en> field field_name_list field_name fraction_constants_opt
     464%type<expr> field field_name_list field_name fraction_constants_opt
    462465
    463466%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
     
    508511%type<decl> type_parameter type_parameter_list type_initializer_opt
    509512
    510 %type<en> type_parameters_opt type_list array_type_list
     513%type<expr> type_parameters_opt type_list array_type_list
    511514
    512515%type<decl> type_qualifier type_qualifier_name forall type_qualifier_list_opt type_qualifier_list
     
    519522
    520523// initializers
    521 %type<in>  initializer initializer_list_opt initializer_opt
     524%type<init>  initializer initializer_list_opt initializer_opt
    522525
    523526// designators
    524 %type<en>  designator designator_list designation
     527%type<expr>  designator designator_list designation
    525528
    526529
     
    644647
    645648string_literal:
    646         string_literal_list                                                     { $$ = build_constantStr( yylloc, *$1 ); }
     649        string_literal_list                                                     { $$ = new ExpressionNode( build_constantStr( yylloc, *$1 ) ); }
    647650        ;
    648651
     
    739742                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
    740743        | string_literal '[' assignment_expression ']'          // "abc"[3], 3["abc"]
    741                 { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, new ExpressionNode( $1 ), $3 ) ); }
     744                { $$ = new ExpressionNode( build_binary_val( yylloc, OperKinds::Index, $1, $3 ) ); }
    742745        | postfix_expression '{' argument_expression_list_opt '}' // CFA, constructor call
    743746                {
     
    757760                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
    758761        | string_literal '`' identifier                                         // CFA, postfix call
    759                 { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), new ExpressionNode( $1 ) ) ); }
     762                { $$ = new ExpressionNode( build_func( yylloc, new ExpressionNode( build_varref( yylloc, build_postfix_name( $3 ) ) ), $1 ) ); }
    760763        | postfix_expression '.' identifier
    761764                { $$ = new ExpressionNode( build_fieldSel( yylloc, $1, build_varref( yylloc, $3 ) ) ); }
     
    857860        | constant
    858861        | string_literal
    859                 { $$ = new ExpressionNode( $1 ); }
     862                { $$ = $1; }
    860863        | EXTENSION cast_expression                                                     // GCC
    861864                { $$ = $2->set_extension( true ); }
     
    12601263
    12611264case_value_list:                                                                                // CFA
    1262         case_value                                                                      { $$ = new StatementNode( build_case( $1 ) ); }
     1265        case_value                                                                      { $$ = new ClauseNode( build_case( yylloc, $1 ) ); }
    12631266                // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
    1264         | case_value_list ',' case_value                        { $$ = (StatementNode *)($1->set_last( new StatementNode( build_case( $3 ) ) ) ); }
     1267        | case_value_list ',' case_value                        { $$ = $1->set_last( new ClauseNode( build_case( yylloc, $3 ) ) ); }
    12651268        ;
    12661269
     
    12711274        | CASE case_value_list error                                            // syntax error
    12721275                { SemanticError( yylloc, "Missing colon after case list." ); $$ = nullptr; }
    1273         | DEFAULT ':'                                                           { $$ = new StatementNode( build_default( yylloc ) ); }
     1276        | DEFAULT ':'                                                           { $$ = new ClauseNode( build_default( yylloc ) ); }
    12741277                // A semantic check is required to ensure only one default clause per switch/choose statement.
    12751278        | DEFAULT error                                                                         //  syntax error
     
    12791282case_label_list:                                                                                // CFA
    12801283        case_label
    1281         | case_label_list case_label                            { $$ = (StatementNode *)( $1->set_last( $2 )); }
     1284        | case_label_list case_label                            { $$ = $1->set_last( $2 ); }
    12821285        ;
    12831286
     
    12961299                { $$ = $1->append_last_case( new StatementNode( build_compound( yylloc, $2 ) ) ); }
    12971300        | switch_clause_list case_label_list statement_list_nodecl
    1298                 { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( yylloc, $3 ) ) ) ) ); }
     1301                { $$ = $1->set_last( $2->append_last_case( new StatementNode( build_compound( yylloc, $3 ) ) ) ); }
    12991302        ;
    13001303
     
    16851688waituntil_clause:
    16861689        when_clause_opt waituntil statement
    1687                 { $$ = build_waituntil_clause( yylloc, $1, $2, maybe_build_compound( yylloc, $3 ) ); }
     1690                { printf( "waituntil_clause 1\n" ); $$ = nullptr; }
    16881691        | '(' wor_waituntil_clause ')'
    1689                 { $$ = $2; }
     1692                { printf( "waituntil_clause 2\n" ); $$ = nullptr; }
    16901693        ;
    16911694
    16921695wand_waituntil_clause:
    16931696        waituntil_clause                                                                        %prec THEN
    1694                 { $$ = $1; }
     1697                { printf( "wand_waituntil_clause 1\n" ); $$ = nullptr; }
    16951698        | waituntil_clause wand wand_waituntil_clause
    1696                 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::AND, $1, $3 ); }
     1699                { printf( "wand_waituntil_clause 2\n" ); $$ = nullptr; }
    16971700        ;
    16981701
    16991702wor_waituntil_clause:
    17001703        wand_waituntil_clause
    1701                 { $$ = $1; }
     1704                { printf( "wor_waituntil_clause 1\n" ); $$ = nullptr; }
    17021705        | wor_waituntil_clause wor wand_waituntil_clause
    1703                 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::OR, $1, $3 ); }
     1706                { printf( "wor_waituntil_clause 2\n" ); $$ = nullptr; }
    17041707        | wor_waituntil_clause wor when_clause_opt ELSE statement
    1705                 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::LEFT_OR, $1, build_waituntil_else( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); }
     1708                { printf( "wor_waituntil_clause 3\n" ); $$ = nullptr; }
    17061709        | wor_waituntil_clause wor when_clause_opt timeout statement    %prec THEN
    1707                 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::LEFT_OR, $1, build_waituntil_timeout( yylloc, $3, $4, maybe_build_compound( yylloc, $5 ) ) ); }
     1710                { printf( "wor_waituntil_clause 4\n" ); $$ = nullptr; }
    17081711        // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
    17091712        | wor_waituntil_clause wor when_clause_opt timeout statement wor ELSE statement // syntax error
    17101713                { SemanticError( yylloc, "else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; }
    17111714        | wor_waituntil_clause wor when_clause_opt timeout statement wor when_clause ELSE statement
    1712                 { $$ = new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::LEFT_OR, $1,
    1713                 new ast::WaitUntilStmt::ClauseNode( ast::WaitUntilStmt::ClauseNode::Op::OR,
    1714                     build_waituntil_timeout( yylloc, $3, $4, maybe_build_compound( yylloc, $5 ) ),
    1715                     build_waituntil_else( yylloc, $7, maybe_build_compound( yylloc, $9 ) ) ) ); }
     1715                { printf( "wor_waituntil_clause 6\n" ); $$ = nullptr; }
    17161716        ;
    17171717
     
    17191719        wor_waituntil_clause                                                            %prec THEN
    17201720                // SKULLDUGGERY: create an empty compound statement to test parsing of waituntil statement.
    1721                 {
    1722             $$ = new StatementNode( build_waituntil_stmt( yylloc, $1 ) );
    1723             // $$ = new StatementNode( build_compound( yylloc, nullptr ) );
    1724         }
     1721                { $$ = new StatementNode( build_compound( yylloc, nullptr ) ); }
    17251722        ;
    17261723
     
    17361733handler_clause:
    17371734        handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
    1738                 { $$ = new StatementNode( build_catch( yylloc, $1, $4, $6, $8 ) ); }
     1735                { $$ = new ClauseNode( build_catch( yylloc, $1, $4, $6, $8 ) ); }
    17391736        | handler_clause handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
    1740                 { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( yylloc, $2, $5, $7, $9 ) ) ); }
     1737                { $$ = $1->set_last( new ClauseNode( build_catch( yylloc, $2, $5, $7, $9 ) ) ); }
    17411738        ;
    17421739
     
    17551752
    17561753finally_clause:
    1757         FINALLY compound_statement                                      { $$ = new StatementNode( build_finally( yylloc, $2 ) ); }
     1754        FINALLY compound_statement                                      { $$ = new ClauseNode( build_finally( yylloc, $2 ) ); }
    17581755        ;
    17591756
     
    18131810asm_operand:                                                                                    // GCC
    18141811        string_literal '(' constant_expression ')'
    1815                 { $$ = new ExpressionNode( new ast::AsmExpr( yylloc, "", $1, maybeMoveBuild( $3 ) ) ); }
     1812                { $$ = new ExpressionNode( new ast::AsmExpr( yylloc, "", maybeMoveBuild( $1 ), maybeMoveBuild( $3 ) ) ); }
    18161813        | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
    18171814                {
    1818                         $$ = new ExpressionNode( new ast::AsmExpr( yylloc, *$2.str, $4, maybeMoveBuild( $6 ) ) );
     1815                        $$ = new ExpressionNode( new ast::AsmExpr( yylloc, *$2.str, maybeMoveBuild( $4 ), maybeMoveBuild( $6 ) ) );
    18191816                        delete $2.str;
    18201817                }
     
    18251822                { $$ = nullptr; }                                                               // use default argument
    18261823        | string_literal
    1827                 { $$ = new ExpressionNode( $1 ); }
     1824                { $$ = $1; }
    18281825        | asm_clobbers_list_opt ',' string_literal
    1829                 { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( $3 ) )); }
     1826                { $$ = (ExpressionNode *)( $1->set_last( $3 ) ); }
    18301827        ;
    18311828
     
    18991896static_assert:
    19001897        STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
    1901                 { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
     1898                { $$ = DeclarationNode::newStaticAssert( $3, maybeMoveBuild( $5 ) ); }
    19021899        | STATICASSERT '(' constant_expression ')' ';'          // CFA
    19031900                { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( yylloc, *new string( "\"\"" ) ) ); }
     
    24472444                }
    24482445        | enum_type
     2446        /* | algebric_data_type */
    24492447        ;
    24502448
     
    26852683                        $$ = DeclarationNode::newEnum( nullptr, $7, true, true, $3 )->addQualifiers( $5 );
    26862684                }
    2687         | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
    2688                 {
    2689                         $$ = DeclarationNode::newEnum( nullptr, $6, true, true )->addQualifiers( $4 );
    2690                 }
    26912685        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
    26922686                {
     
    26982692                        $$ = DeclarationNode::newEnum( $6, $11, true, true, $3, $9 )->addQualifiers( $5 )->addQualifiers( $7 );
    26992693                }
    2700         | ENUM '(' ')' attribute_list_opt identifier attribute_list_opt
    2701           hide_opt '{' enumerator_list comma_opt '}'
    2702                 {
    2703                         $$ = DeclarationNode::newEnum( $5, $9, true, true, nullptr, $7 )->addQualifiers( $4 )->addQualifiers( $6 );
    2704                 }
    27052694        | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt
    27062695          hide_opt '{' enumerator_list comma_opt '}'
     
    27082697                        $$ = DeclarationNode::newEnum( $6->name, $10, true, true, $3, $8 )->addQualifiers( $5 )->addQualifiers( $7 );
    27092698                }
    2710         | ENUM '(' ')' attribute_list_opt typedef_name attribute_list_opt
    2711           hide_opt '{' enumerator_list comma_opt '}'
    2712                 {
    2713                         $$ = DeclarationNode::newEnum( $5->name, $9, true, true, nullptr, $7 )->addQualifiers( $4 )->addQualifiers( $6 );
    2714                 }
    27152699        | enum_type_nobody
    2716         ;
     2700        | DATA identifier
     2701        { typedefTable.makeTypedef( *$2 ); }
     2702         '{' value_list '}'
     2703         {
     2704                $$ = DeclarationNode::newAdt( $2, $5 );
     2705         }
     2706        ;
     2707
     2708value_list:
     2709        data_constructor
     2710        {
     2711                $$ = $1;
     2712        }
     2713        /* | identifier_or_type_name '(' type_specifier ')'
     2714        {
     2715                $$ = DeclarationNode::newEnumValueGeneric( $1, nullptr );
     2716        } */
     2717        /* | data_constructor '|' value_list   */
     2718        | value_list '|' data_constructor
     2719        {
     2720                 { $$ = $1->appendList( $3 ); }
     2721        }
     2722        ;
     2723
     2724data_constructor:
     2725        identifier_or_type_name
     2726        {
     2727                typedefTable.makeTypedef( *$1 );
     2728                $$ =  DeclarationNode::newTypeDecl( $1, nullptr );;
     2729        }
     2730        | identifier_or_type_name '(' type_specifier_list ')'
     2731        {
     2732                typedefTable.makeTypedef( *$1 );
     2733                $$ = DeclarationNode::newTypeDecl( $1, $3 );
     2734        }
     2735
     2736type_specifier_list:
     2737        type_specifier
     2738        /* | type_specifier ',' type_specifier_list  */
     2739        | type_specifier_list ',' type_specifier
     2740        {
     2741                $$ = $1->appendList($3);
     2742        }
     2743        ;
     2744
    27172745
    27182746hide_opt:
     
    28242852
    28252853cfa_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
    2826         abstract_parameter_declaration
     2854        // empty
     2855                { $$ = nullptr; }
     2856        | abstract_parameter_declaration
    28272857        | cfa_identifier_parameter_declarator_no_tuple
    28282858        | cfa_abstract_tuple
     
    33293359                {
    33303360                        DeclarationNode * name = new DeclarationNode();
    3331                         name->asmName = $3;
     3361                        name->asmName = maybeMoveBuild( $3 );
    33323362                        $$ = name->addQualifiers( $5 );
    33333363                }
Note: See TracChangeset for help on using the changeset viewer.