source: src/Parser/parser.yy @ e149f77

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e149f77 was 513e165, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

generalize types for encoded strings, and fold simple ExpressionNode? build routines into parser

  • Property mode set to 100644
File size: 115.0 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[9335ecc]7// parser.yy --
[974906e2]8//
[c11e31c]9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
[936e9f4]11// Last Modified By : Peter A. Buhr
[513e165]12// Last Modified On : Wed Sep 13 11:01:20 2017
13// Update Count     : 2803
[974906e2]14//
[c11e31c]15
[de62360d]16// This grammar is based on the ANSI99/11 C grammar, specifically parts of EXPRESSION and STATEMENTS, and on the C
17// grammar by James A. Roskind, specifically parts of DECLARATIONS and EXTERNAL DEFINITIONS.  While parts have been
18// copied, important changes have been made in all sections; these changes are sufficient to constitute a new grammar.
19// In particular, this grammar attempts to be more syntactically precise, i.e., it parses less incorrect language syntax
20// that must be subsequently rejected by semantic checks.  Nevertheless, there are still several semantic checks
21// required and many are noted in the grammar. Finally, the grammar is extended with GCC and CFA language extensions.
[c11e31c]22
[de62360d]23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
[c11e31c]25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28// 1. designation with '=' (use ':' instead)
29//
[de62360d]30// Most of the syntactic extensions from ANSI90 to ANSI11 C are marked with the comment "C99/C11". This grammar also has
31// two levels of extensions. The first extensions cover most of the GCC C extensions, except for:
[c11e31c]32//
[e7aed49]33// 1. designation with and without '=' (use ':' instead)
[c0aa336]34// 2. attributes not allowed in parenthesis of declarator
[c11e31c]35//
[de62360d]36// All of the syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall
37// (CFA), which fixes several of C's outstanding problems and extends C with many modern language concepts. All of the
38// syntactic extensions for CFA C are marked with the comment "CFA". As noted above, there is one unreconcileable
39// parsing problem between C99 and CFA with respect to designators; this is discussed in detail before the "designation"
40// grammar rule.
[51b7345]41
42%{
[b87a5ed]43#define YYDEBUG_LEXER_TEXT (yylval)                                             // lexer loads this up each time
44#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
[15697ff]45#define YYERROR_VERBOSE
[51b7345]46
47#undef __GNUC_MINOR__
48
49#include <cstdio>
50#include <stack>
[9ed4f94]51using namespace std;
52
[51b7345]53#include "ParseNode.h"
[984dce6]54#include "TypedefTable.h"
[1db21619]55#include "TypeData.h"
[51b7345]56#include "LinkageSpec.h"
[9ed4f94]57#include "Common/SemanticError.h"                                               // error_str
[513e165]58#include "Common/utility.h"                                                             // for maybeMoveBuild, maybeBuild, CodeLo...
[51b7345]59
[cbaee0d]60extern DeclarationNode * parseTree;
[8b7ee09]61extern LinkageSpec::Spec linkage;
[0da3e2c]62extern TypedefTable typedefTable;
63
[2298f728]64stack< LinkageSpec::Spec > linkageStack;
[7bf7fb9]65
[15697ff]66bool appendStr( string & to, string & from ) {
67        // 1. Multiple strings are concatenated into a single string but not combined internally. The reason is that
[ea0c5e3]68        //    "\x12" "3" is treated as 2 characters versus 1 because "escape sequences are converted into single members of
[15697ff]69        //    the execution character set just prior to adjacent string literal concatenation" (C11, Section 6.4.5-8). It is
70        //    easier to let the C compiler handle this case.
71        //
72        // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found
73        //    without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match,
74        //    i.e., u"a" U"b" L"c" is disallowed.
75
76        if ( from[0] != '"' ) {                                                         // encoding ?
77                if ( to[0] != '"' ) {                                                   // encoding ?
[ea0c5e3]78                        if ( to[0] != from[0] || to[1] != from[1] ) { // different encodings ?
[15697ff]79                                yyerror( "non-matching string encodings for string-literal concatenation" );
80                                return false;                                                   // parse error, must call YYERROR in action
[ea0c5e3]81                        } else if ( from[1] == '8' ) {
82                                from.erase( 0, 1 );                                             // remove 2nd encoding
[15697ff]83                        } // if
84                } else {
[ea0c5e3]85                        if ( from[1] == '8' ) {                                         // move encoding to start
86                                to = "u8" + to;
87                                from.erase( 0, 1 );                                             // remove 2nd encoding
88                        } else {
89                                to = from[0] + to;
90                        } // if
[15697ff]91                } // if
92                from.erase( 0, 1 );                                                             // remove 2nd encoding
93        } // if
94        to += " " + from;                                                                       // concatenated into single string
95        return true;
[7bf7fb9]96} // appendStr
[c0aa336]97
98DeclarationNode * distAttr( DeclarationNode * specifier, DeclarationNode * declList ) {
99        // distribute declaration_specifier across all declared variables, e.g., static, const, __attribute__.
100        DeclarationNode * cur = declList, * cl = (new DeclarationNode)->addType( specifier );
101        //cur->addType( specifier );
102        for ( cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
103                cl->cloneBaseType( cur );
104        } // for
105        declList->addType( cl );
106//      delete cl;
107        return declList;
108} // distAttr
109
110void distExt( DeclarationNode * declaration ) {
111        // distribute EXTENSION across all declarations
112        for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
113                iter->set_extension( true );
114        } // for
115} // distExt
[fdca7c6]116
117bool forall = false;                                                                    // aggregate have one or more forall qualifiers ?
[51b7345]118%}
119
[15697ff]120%define parse.error verbose
121
[a67b60e]122// Types declaration
123%union
124{
125        Token tok;
126        ParseNode * pn;
127        ExpressionNode * en;
128        DeclarationNode * decl;
129        DeclarationNode::Aggregate aggKey;
130        DeclarationNode::TypeClass tclass;
131        StatementNode * sn;
[135b431]132        WaitForStmt * wfs;
[e612146c]133        Expression * constant;
[936e9f4]134        IfCtl * ifctl;
[a67b60e]135        ForCtl * fctl;
136        LabelNode * label;
137        InitializerNode * in;
138        OperKinds op;
139        std::string * str;
140        bool flag;
[307a732]141        CatchStmt::Kind catch_kind;
[a67b60e]142}
143
[c11e31c]144//************************* TERMINAL TOKENS ********************************
[51b7345]145
[c11e31c]146// keywords
[51b7345]147%token TYPEDEF
[a7c90d4]148%token EXTERN STATIC AUTO REGISTER
149%token THREADLOCAL                                                                              // C11
150%token INLINE FORTRAN                                                                   // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
151%token NORETURN                                                                                 // C11
[51b7345]152%token CONST VOLATILE
[b87a5ed]153%token RESTRICT                                                                                 // C99
[a7c90d4]154%token ATOMIC                                                                                   // C11
[51d6d6a]155%token FORALL MUTEX VIRTUAL                                                             // CFA
[72457b6]156%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
[b87a5ed]157%token BOOL COMPLEX IMAGINARY                                                   // C99
[72457b6]158%token ZERO_T ONE_T                                                                             // CFA
159%token VALIST                                                                                   // GCC
[b87a5ed]160%token TYPEOF LABEL                                                                             // GCC
[51b7345]161%token ENUM STRUCT UNION
[d3bc0ad]162%token COROUTINE MONITOR THREAD                                                 // CFA
[a7c90d4]163%token OTYPE FTYPE DTYPE TTYPE TRAIT                                    // CFA
[5721a6d]164%token SIZEOF OFFSETOF
[b87a5ed]165%token ATTRIBUTE EXTENSION                                                              // GCC
[51b7345]166%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
[5b2edbc]167%token CHOOSE DISABLE ENABLE FALLTHRU TRY CATCH CATCHRESUME FINALLY THROW THROWRESUME AT WITH WHEN WAITFOR // CFA
[b87a5ed]168%token ASM                                                                                              // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
[a7c90d4]169%token ALIGNAS ALIGNOF GENERIC STATICASSERT                             // C11
[51b7345]170
[c11e31c]171// names and constants: lexer differentiates between identifier and typedef names
[b87a5ed]172%token<tok> IDENTIFIER                  QUOTED_IDENTIFIER               TYPEDEFname                             TYPEGENname
[5b2edbc]173%token<tok> TIMEOUT                             WOR
[b87a5ed]174%token<tok> ATTR_IDENTIFIER             ATTR_TYPEDEFname                ATTR_TYPEGENname
[1b29996]175%token<tok> INTEGERconstant             CHARACTERconstant               STRINGliteral
176// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
177// overloading constants 0/1, e.g., x.1 is lexed as (x)(.1), where (.1) is a factional constant, but is semantically
178// converted into the tuple index (.)(1). e.g., 3.x
[930f69e]179%token<tok>     FLOATING_DECIMALconstant        FLOATING_FRACTIONconstant       FLOATINGconstant
[51b7345]180
[c11e31c]181// multi-character operators
[b87a5ed]182%token ARROW                                                                                    // ->
183%token ICR DECR                                                                                 // ++   --
184%token LS RS                                                                                    // <<   >>
185%token LE GE EQ NE                                                                              // <=   >=      ==      !=
186%token ANDAND OROR                                                                              // &&   ||
187%token ELLIPSIS                                                                                 // ...
188
[e5f2a67]189%token EXPassign        MULTassign      DIVassign       MODassign       // \=   *=      /=      %=
[b87a5ed]190%token PLUSassign       MINUSassign                                                     // +=   -=
191%token LSassign         RSassign                                                        // <<=  >>=
192%token ANDassign        ERassign        ORassign                                // &=   ^=      |=
[51b7345]193
[e7aed49]194%token ATassign                                                                                 // @=
[097e2b0]195
[6165ce7]196%type<tok> identifier  no_attr_identifier
[84d58c5]197%type<tok> identifier_or_type_name  no_attr_identifier_or_type_name  attr_name
[5b2edbc]198%type<tok> quasi_keyword
[ab57786]199%type<constant> string_literal
200%type<str> string_literal_list
[51b7345]201
[c11e31c]202// expressions
[d1625f8]203%type<en> constant
[b87a5ed]204%type<en> tuple                                                 tuple_expression_list
[9706554]205%type<op> ptrref_operator                               unary_operator                          assignment_operator
[b87a5ed]206%type<en> primary_expression                    postfix_expression                      unary_expression
[994d080]207%type<en> cast_expression                               exponential_expression          multiplicative_expression       additive_expression
208%type<en> shift_expression                              relational_expression           equality_expression
209%type<en> AND_expression                                exclusive_OR_expression         inclusive_OR_expression
210%type<en> logical_AND_expression                logical_OR_expression
211%type<en> conditional_expression                constant_expression                     assignment_expression           assignment_expression_opt
[b87a5ed]212%type<en> comma_expression                              comma_expression_opt
[994d080]213%type<en> argument_expression_list              argument_expression                     default_initialize_opt
[936e9f4]214%type<ifctl> if_control_expression
[2f22cc4]215%type<fctl> for_control_expression
[51b7345]216%type<en> subrange
[c0aa336]217%type<decl> asm_name_opt
[7f5566b]218%type<en> asm_operands_opt asm_operands_list asm_operand
219%type<label> label_list
[d1625f8]220%type<en> asm_clobbers_list_opt
[7f5566b]221%type<flag> asm_volatile_opt
[cbce272]222%type<en> handler_predicate_opt
[51b7345]223
[c11e31c]224// statements
[9bd6105]225%type<sn> statement                                             labeled_statement                       compound_statement
226%type<sn> statement_decl                                statement_decl_list                     statement_list_nodecl
227%type<sn> selection_statement
228%type<sn> switch_clause_list_opt                switch_clause_list                      choose_clause_list_opt          choose_clause_list
229%type<en> case_value
230%type<sn> case_clause                                   case_value_list                         case_label                                      case_label_list
231%type<sn> fall_through                                  fall_through_opt
[8b47e50]232%type<sn> iteration_statement                   jump_statement
[9bd6105]233%type<sn> expression_statement                  asm_statement
234%type<sn> with_statement                                with_clause_opt
[51d6d6a]235%type<sn> exception_statement                   handler_clause                          finally_clause
236%type<catch_kind> handler_key
[9bd6105]237%type<en> when_clause                                   when_clause_opt                         waitfor                                         timeout
[135b431]238%type<sn> waitfor_statement
[51d6d6a]239%type<wfs> waitfor_clause
[51b7345]240
[c11e31c]241// declarations
[c0aa336]242%type<decl> abstract_declarator abstract_ptr abstract_array abstract_function array_dimension multi_array_dimension
243%type<decl> abstract_parameter_declarator abstract_parameter_ptr abstract_parameter_array abstract_parameter_function array_parameter_dimension array_parameter_1st_dimension
244%type<decl> abstract_parameter_declaration
[51b7345]245
246%type<aggKey> aggregate_key
[d0ffed1]247%type<decl> aggregate_type aggregate_type_nobody
[51b7345]248
249%type<decl> assertion assertion_list_opt
250
251%type<en>   bit_subrange_size_opt bit_subrange_size
252
[84d58c5]253%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
[51b7345]254
[4040425]255%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
[51b7345]256
257%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
[d0ffed1]258%type<decl> declaration_specifier declaration_specifier_nobody declarator declaring_list
[51b7345]259
[d0ffed1]260%type<decl> elaborated_type elaborated_type_nobody
[51b7345]261
[d0ffed1]262%type<decl> enumerator_list enum_type enum_type_nobody
[51b7345]263%type<en> enumerator_value_opt
264
265%type<decl> exception_declaration external_definition external_definition_list external_definition_list_opt
266
267%type<decl> field_declaration field_declaration_list field_declarator field_declaring_list
[8780e30]268%type<en> field field_list field_name fraction_constants
[51b7345]269
[4d51835]270%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
[51b7345]271
[d3bc0ad]272%type<decl> identifier_parameter_declarator identifier_parameter_ptr identifier_parameter_array identifier_parameter_function
273%type<decl> identifier_list
[51b7345]274
[c0aa336]275%type<decl> cfa_abstract_array cfa_abstract_declarator_no_tuple cfa_abstract_declarator_tuple
276%type<decl> cfa_abstract_function cfa_abstract_parameter_declaration cfa_abstract_parameter_list
277%type<decl> cfa_abstract_ptr cfa_abstract_tuple
[51b7345]278
[c0aa336]279%type<decl> cfa_array_parameter_1st_dimension
[51b7345]280
[c0aa336]281%type<decl> cfa_trait_declaring_list cfa_declaration cfa_field_declaring_list
282%type<decl> cfa_function_declaration cfa_function_return cfa_function_specifier
[51b7345]283
[c0aa336]284%type<decl> cfa_identifier_parameter_array cfa_identifier_parameter_declarator_no_tuple
285%type<decl> cfa_identifier_parameter_declarator_tuple cfa_identifier_parameter_ptr
[51b7345]286
[c0aa336]287%type<decl> cfa_parameter_declaration cfa_parameter_list cfa_parameter_type_list cfa_parameter_type_list_opt
[51b7345]288
[c0aa336]289%type<decl> cfa_typedef_declaration cfa_variable_declaration cfa_variable_specifier
[51b7345]290
[c0aa336]291%type<decl> c_declaration
292%type<decl> KR_function_declarator KR_function_no_ptr KR_function_ptr KR_function_array
293%type<decl> KR_declaration_list KR_declaration_list_opt
[51b7345]294
295%type<decl> parameter_declaration parameter_list parameter_type_list
296%type<decl> parameter_type_list_opt
297
[2871210]298%type<decl> paren_identifier paren_type
[51b7345]299
[0da3e2c]300%type<decl> storage_class storage_class_list
[51b7345]301
[d0ffed1]302%type<decl> sue_declaration_specifier sue_declaration_specifier_nobody sue_type_specifier sue_type_specifier_nobody
[51b7345]303
304%type<tclass> type_class
305%type<decl> type_declarator type_declarator_name type_declaring_list
306
[84d58c5]307%type<decl> type_declaration_specifier type_type_specifier type_name typegen_name
308%type<decl> typedef typedef_declaration typedef_expression
[c0aa336]309
310%type<decl> variable_type_redeclarator type_ptr type_array type_function
311
312%type<decl> type_parameter_redeclarator type_parameter_ptr type_parameter_array type_parameter_function
[51b7345]313
[84d58c5]314%type<decl> type type_no_function
315%type<decl> type_parameter type_parameter_list type_initializer_opt
[51b7345]316
[84d58c5]317%type<en> type_list
[51b7345]318
[d3bc0ad]319%type<decl> type_qualifier type_qualifier_name type_qualifier_list_opt type_qualifier_list
320%type<decl> type_specifier type_specifier_nobody
[51b7345]321
[c0aa336]322%type<decl> variable_declarator variable_ptr variable_array variable_function
323%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
[51b7345]324
[44a81853]325%type<decl> attribute_list_opt attribute_list attribute_name_list attribute attribute_name
[1db21619]326
[c11e31c]327// initializers
[51b7345]328%type<in>  initializer initializer_list initializer_opt
329
[c11e31c]330// designators
[51b7345]331%type<en>  designator designator_list designation
332
333
[b87a5ed]334// Handle single shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string
335// is ambiguous:
[5b2edbc]336// .---------.                          matches IF '(' comma_expression ')' statement . (reduce)
[c11e31c]337// if ( C ) S1 else S2
[5b2edbc]338// `-----------------'          matches IF '(' comma_expression ')' statement . (shift) ELSE statement */
339// Similar issues exit with the waitfor statement.
[51b7345]340
[1efa1e1]341// Order of these lines matters (low-to-high precedence). THEN is left associative over WOR/TIMEOUT/ELSE, WOR is left
342// associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
[5b2edbc]343%precedence THEN        // rule precedence for IF/WAITFOR statement
344%precedence WOR         // token precedence for start of WOR in WAITFOR statement
345%precedence TIMEOUT     // token precedence for start of TIMEOUT in WAITFOR statement
346%precedence ELSE        // token precedence for start of else clause in IF/WAITFOR statement
[51b7345]347
[930f69e]348
[b87a5ed]349%start translation_unit                                                                 // parse-tree root
[51b7345]350
351%%
[c11e31c]352//************************* Namespace Management ********************************
353
[de62360d]354// The grammar in the ANSI C standard is not strictly context-free, since it relies upon the distinct terminal symbols
[930f69e]355// "identifier", "TYPEDEFname", and "TYPEGENname" that are lexically identical.  While it is possible to write a purely
356// context-free grammar, such a grammar would obscure the relationship between syntactic and semantic constructs.
357// Hence, this grammar uses the ANSI style.
[c11e31c]358//
[de62360d]359// Cforall compounds this problem by introducing type names local to the scope of a declaration (for instance, those
[9059213]360// introduced through "forall" qualifiers), and by introducing "type generators" -- parameterized types.  This latter
[de62360d]361// type name creates a third class of identifiers that must be distinguished by the scanner.
[c11e31c]362//
[de62360d]363// Since the scanner cannot distinguish among the different classes of identifiers without some context information, it
[9059213]364// accesses a data structure (TypedefTable) to allow classification of an identifier that it has just read.  Semantic
365// actions during the parser update this data structure when the class of identifiers change.
[c11e31c]366//
[de62360d]367// Because the Cforall language is block-scoped, there is the possibility that an identifier can change its class in a
368// local scope; it must revert to its original class at the end of the block.  Since type names can be local to a
369// particular declaration, each declaration is itself a scope.  This requires distinguishing between type names that are
370// local to the current declaration scope and those that persist past the end of the declaration (i.e., names defined in
[9059213]371// "typedef" or "otype" declarations).
[c11e31c]372//
[de62360d]373// The non-terminals "push" and "pop" derive the empty string; their only use is to denote the opening and closing of
374// scopes.  Every push must have a matching pop, although it is regrettable the matching pairs do not always occur
375// within the same rule.  These non-terminals may appear in more contexts than strictly necessary from a semantic point
376// of view.  Unfortunately, these extra rules are necessary to prevent parsing conflicts -- the parser may not have
377// enough context and look-ahead information to decide whether a new scope is necessary, so the effect of these extra
378// rules is to open a new scope unconditionally.  As the grammar evolves, it may be neccesary to add or move around
379// "push" and "pop" nonterminals to resolve conflicts of this sort.
[51b7345]380
381push:
[ab57786]382                { typedefTable.enterScope(); }
[4d51835]383        ;
[51b7345]384
385pop:
[ab57786]386                { typedefTable.leaveScope(); }
[4d51835]387        ;
[51b7345]388
[c11e31c]389//************************* CONSTANTS ********************************
[51b7345]390
391constant:
[de62360d]392                // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
[ab57786]393        INTEGERconstant                                                         { $$ = new ExpressionNode( build_constantInteger( *$1 ) ); }
[930f69e]394        | FLOATING_DECIMALconstant                                      { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
395        | FLOATING_FRACTIONconstant                                     { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
[ab57786]396        | FLOATINGconstant                                                      { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
397        | CHARACTERconstant                                                     { $$ = new ExpressionNode( build_constantChar( *$1 ) ); }
[4d51835]398        ;
[51b7345]399
[5b2edbc]400quasi_keyword:                                                                                  // CFA
401        TIMEOUT
402        | WOR
403        ;
404
[51b7345]405identifier:
[4d51835]406        IDENTIFIER
407        | ATTR_IDENTIFIER                                                                       // CFA
[5b2edbc]408        | quasi_keyword
[4d51835]409        ;
[51b7345]410
411no_attr_identifier:
[4d51835]412        IDENTIFIER
[5b2edbc]413        | quasi_keyword
[4d51835]414        ;
[51b7345]415
[ab57786]416string_literal:
417        string_literal_list                                                     { $$ = build_constantStr( *$1 ); }
418        ;
419
[b87a5ed]420string_literal_list:                                                                    // juxtaposed strings are concatenated
[ab57786]421        STRINGliteral                                                           { $$ = $1; } // conversion from tok to str
[7bf7fb9]422        | string_literal_list STRINGliteral
423                {
[15697ff]424                        if ( ! appendStr( *$1, *$2 ) ) YYERROR;         // append 2nd juxtaposed string to 1st
[7bf7fb9]425                        delete $2;                                                                      // allocated by lexer
[ab57786]426                        $$ = $1;                                                                        // conversion from tok to str
[7bf7fb9]427                }
[4d51835]428        ;
[51b7345]429
[c11e31c]430//************************* EXPRESSIONS ********************************
[51b7345]431
432primary_expression:
[4d51835]433        IDENTIFIER                                                                                      // typedef name cannot be used as a variable name
[d1625f8]434                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[5b2edbc]435        | quasi_keyword
436                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[1b29996]437        | tuple
[4d51835]438        | '(' comma_expression ')'
439                { $$ = $2; }
440        | '(' compound_statement ')'                                            // GCC, lambda expression
[513e165]441                { $$ = new ExpressionNode( new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >($2) ) ) ); }
[84d58c5]442        | primary_expression '{' argument_expression_list '}' // CFA, constructor call
[ecb27a7]443                {
444                        Token fn;
445                        fn.str = new std::string( "?{}" );                      // location undefined - use location of '{'?
446                        $$ = new ExpressionNode( new ConstructorExpr( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $1 )->set_last( $3 ) ) ) );
447                }
[84d58c5]448        | type_name '.' no_attr_identifier                                      // CFA, nested type
449                { $$ = nullptr; }                                                               // FIX ME
450        | type_name '.' '[' push field_list pop ']'                     // CFA, nested type / tuple field selector
451                { $$ = nullptr; }                                                               // FIX ME
[4d51835]452        ;
[51b7345]453
454postfix_expression:
[4d51835]455        primary_expression
456        | postfix_expression '[' push assignment_expression pop ']'
[c6b1105]457                // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
[de62360d]458                // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
459                // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
460                // equivalent to the old x[i,j].
[d1625f8]461                { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, $4 ) ); }
[4d51835]462        | postfix_expression '(' argument_expression_list ')'
[d1625f8]463                { $$ = new ExpressionNode( build_func( $1, $3 ) ); }
[4d51835]464        | postfix_expression '.' no_attr_identifier
[d1625f8]465                { $$ = new ExpressionNode( build_fieldSel( $1, build_varref( $3 ) ) ); }
[4d51835]466        | postfix_expression '.' '[' push field_list pop ']' // CFA, tuple field selector
[3b58d91]467                { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $5 ) ) ); }
[930f69e]468        | postfix_expression FLOATING_FRACTIONconstant          // CFA, tuple index
469                { $$ = new ExpressionNode( build_fieldSel( $1, build_field_name_FLOATING_FRACTIONconstant( *$2 ) ) ); }
[4d51835]470        | postfix_expression ARROW no_attr_identifier
[861799c]471                {
472                        $$ = new ExpressionNode( build_pfieldSel( $1, *$3 == "0" || *$3 == "1" ? build_constantInteger( *$3 ) : build_varref( $3 ) ) );
473                }
[4d51835]474        | postfix_expression ARROW '[' push field_list pop ']' // CFA, tuple field selector
[3b58d91]475                        { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $5 ) ) ); }
[861799c]476        | postfix_expression ARROW INTEGERconstant                      // CFA, tuple index
477                { $$ = new ExpressionNode( build_pfieldSel( $1, build_constantInteger( *$3 ) ) ); }
[4d51835]478        | postfix_expression ICR
[d1625f8]479                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::IncrPost, $1 ) ); }
[4d51835]480        | postfix_expression DECR
[d1625f8]481                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::DecrPost, $1 ) ); }
[84d58c5]482        | '(' type_no_function ')' '{' initializer_list comma_opt '}' // C99, compound-literal
[d1625f8]483                { $$ = new ExpressionNode( build_compoundLiteral( $2, new InitializerNode( $5, true ) ) ); }
[ecb27a7]484        | '^' primary_expression '{' argument_expression_list '}' // CFA
[097e2b0]485                {
[9706554]486                        Token fn;
[ecb27a7]487                        fn.str = new string( "^?{}" );                          // location undefined
488                        $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $2 )->set_last( $4 ) ) );
[097e2b0]489                }
[4d51835]490        ;
[51b7345]491
492argument_expression_list:
[4d51835]493        argument_expression
494        | argument_expression_list ',' argument_expression
[1d4580a]495                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]496        ;
[51b7345]497
498argument_expression:
[4d51835]499        // empty
[738e304]500                { $$ = nullptr; }
501        // | '@'                                                                                                // use default argument
502        //      { $$ = new ExpressionNode( build_constantInteger( *new string( "2" ) ) ); }
[4d51835]503        | assignment_expression
504        ;
[b87a5ed]505
506field_list:                                                                                             // CFA, tuple field selector
[4d51835]507        field
[1d4580a]508        | field_list ',' field                                          { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]509        ;
[b87a5ed]510
511field:                                                                                                  // CFA, tuple field selector
[faddbd8]512        field_name
[930f69e]513        | FLOATING_DECIMALconstant field
514                { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), maybeMoveBuild<Expression>( $2 ) ) ); }
515        | FLOATING_DECIMALconstant '[' push field_list pop ']'
516                { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), build_tuple( $4 ) ) ); }
[faddbd8]517        | field_name '.' field
[bf32bb8]518                { $$ = new ExpressionNode( build_fieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
[faddbd8]519        | field_name '.' '[' push field_list pop ']'
[bf32bb8]520                { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $5 ) ) ); }
[faddbd8]521        | field_name ARROW field
[bf32bb8]522                { $$ = new ExpressionNode( build_pfieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
[faddbd8]523        | field_name ARROW '[' push field_list pop ']'
[bf32bb8]524                { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $5 ) ) ); }
[4d51835]525        ;
[51b7345]526
[faddbd8]527field_name:
[1b29996]528        INTEGERconstant fraction_constants
[8780e30]529                { $$ = new ExpressionNode( build_field_name_fraction_constants( build_constantInteger( *$1 ), $2 ) ); }
[1b29996]530        | FLOATINGconstant fraction_constants
[8780e30]531                { $$ = new ExpressionNode( build_field_name_fraction_constants( build_field_name_FLOATINGconstant( *$1 ), $2 ) ); }
[1b29996]532        | no_attr_identifier fraction_constants
[4cb935e]533                {
[84d58c5]534                        $$ = new ExpressionNode( build_field_name_fraction_constants( build_varref( $1 ), $2 ) );
535                }
[1b29996]536        ;
537
538fraction_constants:
539        // empty
[8780e30]540                { $$ = nullptr; }
[930f69e]541        | fraction_constants FLOATING_FRACTIONconstant
[8780e30]542                {
[930f69e]543                        Expression * constant = build_field_name_FLOATING_FRACTIONconstant( *$2 );
[8780e30]544                        $$ = $1 != nullptr ? new ExpressionNode( build_fieldSel( $1,  constant ) ) : new ExpressionNode( constant );
545                }
[faddbd8]546        ;
547
[51b7345]548unary_expression:
[4d51835]549        postfix_expression
[c6b1105]550                // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
551                // semantics checks, e.g., ++3, 3--, *3, &&3
[51b1202]552        | constant
553                { $$ = $1; }
[ab57786]554        | string_literal
[d1625f8]555                { $$ = new ExpressionNode( $1 ); }
[4d51835]556        | EXTENSION cast_expression                                                     // GCC
[e04ef3a]557                { $$ = $2->set_extension( true ); }
[c6b1105]558                // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
559                //              { * X; }         // dereference X
560                //              { * int X; } // CFA declaration of pointer to int
[51e076e]561        | ptrref_operator cast_expression                                       // CFA
[9706554]562                {
563                        switch ( $1 ) {
[d9e2280]564                          case OperKinds::AddressOf:
[d1625f8]565                                $$ = new ExpressionNode( build_addressOf( $2 ) );
[9706554]566                                break;
[d9e2280]567                          case OperKinds::PointTo:
[d1625f8]568                                $$ = new ExpressionNode( build_unary_val( $1, $2 ) );
[9706554]569                                break;
[5809461]570                          case OperKinds::And:
571                                $$ = new ExpressionNode( new AddressExpr( build_addressOf( $2 ) ) );
572                                break;
[9706554]573                          default:
574                                assert( false );
575                        }
576                }
[4d51835]577        | unary_operator cast_expression
[d1625f8]578                { $$ = new ExpressionNode( build_unary_val( $1, $2 ) ); }
[dd51906]579        | ICR unary_expression
[d1625f8]580                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Incr, $2 ) ); }
[dd51906]581        | DECR unary_expression
[d1625f8]582                { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
[4d51835]583        | SIZEOF unary_expression
[d1625f8]584                { $$ = new ExpressionNode( build_sizeOfexpr( $2 ) ); }
[84d58c5]585        | SIZEOF '(' type_no_function ')'
[d1625f8]586                { $$ = new ExpressionNode( build_sizeOftype( $3 ) ); }
587        | ALIGNOF unary_expression                                                      // GCC, variable alignment
588                { $$ = new ExpressionNode( build_alignOfexpr( $2 ) ); }
[a2e0687]589        | ALIGNOF '(' type_no_function ')'                                      // GCC, type alignment
[d1625f8]590                { $$ = new ExpressionNode( build_alignOftype( $3 ) ); }
[84d58c5]591        | OFFSETOF '(' type_no_function ',' no_attr_identifier ')'
[d1625f8]592                { $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) ); }
[4d51835]593        | ATTR_IDENTIFIER
[d1625f8]594                { $$ = new ExpressionNode( build_attrexpr( build_varref( $1 ), nullptr ) ); }
[4d51835]595        | ATTR_IDENTIFIER '(' argument_expression ')'
[d1625f8]596                { $$ = new ExpressionNode( build_attrexpr( build_varref( $1 ), $3 ) ); }
[84d58c5]597        | ATTR_IDENTIFIER '(' type ')'
[d1625f8]598                { $$ = new ExpressionNode( build_attrtype( build_varref( $1 ), $3 ) ); }
[4d51835]599        ;
[51b7345]600
[dd51906]601ptrref_operator:
[d9e2280]602        '*'                                                                                     { $$ = OperKinds::PointTo; }
603        | '&'                                                                           { $$ = OperKinds::AddressOf; }
[c6b1105]604                // GCC, address of label must be handled by semantic check for ref,ref,label
[9f07232]605        | ANDAND                                                                        { $$ = OperKinds::And; }
[dd51906]606        ;
607
[51b7345]608unary_operator:
[d9e2280]609        '+'                                                                                     { $$ = OperKinds::UnPlus; }
610        | '-'                                                                           { $$ = OperKinds::UnMinus; }
611        | '!'                                                                           { $$ = OperKinds::Neg; }
612        | '~'                                                                           { $$ = OperKinds::BitNeg; }
[4d51835]613        ;
[51b7345]614
615cast_expression:
[4d51835]616        unary_expression
[84d58c5]617        | '(' type_no_function ')' cast_expression
[d1625f8]618                { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[72457b6]619                // VIRTUAL cannot be opt because of look ahead issues
620        | '(' VIRTUAL ')' cast_expression
[513e165]621                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $4 ), maybeMoveBuildType( nullptr ) ) ); }
[72457b6]622        | '(' VIRTUAL type_no_function ')' cast_expression
[513e165]623                { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild< Expression >( $5 ), maybeMoveBuildType( $3 ) ) ); }
[84d58c5]624//      | '(' type_no_function ')' tuple
[1b29996]625//              { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
[4d51835]626        ;
[51b7345]627
[994d080]628exponential_expression:
[4d51835]629        cast_expression
[994d080]630        | exponential_expression '\\' cast_expression
[e5f2a67]631                { $$ = new ExpressionNode( build_binary_val( OperKinds::Exp, $1, $3 ) ); }
[994d080]632        ;
633
634multiplicative_expression:
635        exponential_expression
636        | multiplicative_expression '*' exponential_expression
[d1625f8]637                { $$ = new ExpressionNode( build_binary_val( OperKinds::Mul, $1, $3 ) ); }
[994d080]638        | multiplicative_expression '/' exponential_expression
[d1625f8]639                { $$ = new ExpressionNode( build_binary_val( OperKinds::Div, $1, $3 ) ); }
[994d080]640        | multiplicative_expression '%' exponential_expression
[d1625f8]641                { $$ = new ExpressionNode( build_binary_val( OperKinds::Mod, $1, $3 ) ); }
[4d51835]642        ;
[51b7345]643
644additive_expression:
[4d51835]645        multiplicative_expression
646        | additive_expression '+' multiplicative_expression
[d1625f8]647                { $$ = new ExpressionNode( build_binary_val( OperKinds::Plus, $1, $3 ) ); }
[4d51835]648        | additive_expression '-' multiplicative_expression
[d1625f8]649                { $$ = new ExpressionNode( build_binary_val( OperKinds::Minus, $1, $3 ) ); }
[4d51835]650        ;
[51b7345]651
652shift_expression:
[4d51835]653        additive_expression
654        | shift_expression LS additive_expression
[d1625f8]655                { $$ = new ExpressionNode( build_binary_val( OperKinds::LShift, $1, $3 ) ); }
[4d51835]656        | shift_expression RS additive_expression
[d1625f8]657                { $$ = new ExpressionNode( build_binary_val( OperKinds::RShift, $1, $3 ) ); }
[4d51835]658        ;
[51b7345]659
660relational_expression:
[4d51835]661        shift_expression
662        | relational_expression '<' shift_expression
[d1625f8]663                { $$ = new ExpressionNode( build_binary_val( OperKinds::LThan, $1, $3 ) ); }
[4d51835]664        | relational_expression '>' shift_expression
[d1625f8]665                { $$ = new ExpressionNode( build_binary_val( OperKinds::GThan, $1, $3 ) ); }
[4d51835]666        | relational_expression LE shift_expression
[d1625f8]667                { $$ = new ExpressionNode( build_binary_val( OperKinds::LEThan, $1, $3 ) ); }
[4d51835]668        | relational_expression GE shift_expression
[d1625f8]669                { $$ = new ExpressionNode( build_binary_val( OperKinds::GEThan, $1, $3 ) ); }
[4d51835]670        ;
[51b7345]671
672equality_expression:
[4d51835]673        relational_expression
674        | equality_expression EQ relational_expression
[d1625f8]675                { $$ = new ExpressionNode( build_binary_val( OperKinds::Eq, $1, $3 ) ); }
[4d51835]676        | equality_expression NE relational_expression
[d1625f8]677                { $$ = new ExpressionNode( build_binary_val( OperKinds::Neq, $1, $3 ) ); }
[4d51835]678        ;
[51b7345]679
680AND_expression:
[4d51835]681        equality_expression
682        | AND_expression '&' equality_expression
[d1625f8]683                { $$ = new ExpressionNode( build_binary_val( OperKinds::BitAnd, $1, $3 ) ); }
[4d51835]684        ;
[51b7345]685
686exclusive_OR_expression:
[4d51835]687        AND_expression
688        | exclusive_OR_expression '^' AND_expression
[d1625f8]689                { $$ = new ExpressionNode( build_binary_val( OperKinds::Xor, $1, $3 ) ); }
[4d51835]690        ;
[51b7345]691
692inclusive_OR_expression:
[4d51835]693        exclusive_OR_expression
694        | inclusive_OR_expression '|' exclusive_OR_expression
[d1625f8]695                { $$ = new ExpressionNode( build_binary_val( OperKinds::BitOr, $1, $3 ) ); }
[4d51835]696        ;
[51b7345]697
698logical_AND_expression:
[4d51835]699        inclusive_OR_expression
700        | logical_AND_expression ANDAND inclusive_OR_expression
[d1625f8]701                { $$ = new ExpressionNode( build_and_or( $1, $3, true ) ); }
[4d51835]702        ;
[51b7345]703
704logical_OR_expression:
[4d51835]705        logical_AND_expression
706        | logical_OR_expression OROR logical_AND_expression
[d1625f8]707                { $$ = new ExpressionNode( build_and_or( $1, $3, false ) ); }
[4d51835]708        ;
[51b7345]709
710conditional_expression:
[4d51835]711        logical_OR_expression
712        | logical_OR_expression '?' comma_expression ':' conditional_expression
[d1625f8]713                { $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
[d9e2280]714                // FIX ME: this hack computes $1 twice
[4d51835]715        | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
[d1625f8]716                { $$ = new ExpressionNode( build_cond( $1, $1, $4 ) ); }
[4d51835]717        ;
[51b7345]718
719constant_expression:
[4d51835]720        conditional_expression
721        ;
[51b7345]722
723assignment_expression:
[4d51835]724                // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
725        conditional_expression
726        | unary_expression assignment_operator assignment_expression
[cda7889]727                { $$ = new ExpressionNode( build_binary_val( $2, $1, $3 ) ); }
[4d51835]728        ;
[51b7345]729
730assignment_expression_opt:
[4d51835]731        // empty
[d1625f8]732                { $$ = nullptr; }
[4d51835]733        | assignment_expression
734        ;
[b87a5ed]735
[9706554]736assignment_operator:
[d9e2280]737        '='                                                                                     { $$ = OperKinds::Assign; }
[a839867]738        | ATassign                                                                      { $$ = OperKinds::AtAssn; }
[e5f2a67]739        | EXPassign                                                                     { $$ = OperKinds::ExpAssn; }
[d9e2280]740        | MULTassign                                                            { $$ = OperKinds::MulAssn; }
741        | DIVassign                                                                     { $$ = OperKinds::DivAssn; }
742        | MODassign                                                                     { $$ = OperKinds::ModAssn; }
743        | PLUSassign                                                            { $$ = OperKinds::PlusAssn; }
744        | MINUSassign                                                           { $$ = OperKinds::MinusAssn; }
745        | LSassign                                                                      { $$ = OperKinds::LSAssn; }
746        | RSassign                                                                      { $$ = OperKinds::RSAssn; }
747        | ANDassign                                                                     { $$ = OperKinds::AndAssn; }
748        | ERassign                                                                      { $$ = OperKinds::ERAssn; }
749        | ORassign                                                                      { $$ = OperKinds::OrAssn; }
[413ad05]750        ;
[9706554]751
[b87a5ed]752tuple:                                                                                                  // CFA, tuple
[de62360d]753                // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
[c0aa336]754                // comma_expression in cfa_identifier_parameter_array and cfa_abstract_array
[1b29996]755//      '[' ']'
756//              { $$ = new ExpressionNode( build_tuple() ); }
757//      '[' push assignment_expression pop ']'
758//              { $$ = new ExpressionNode( build_tuple( $3 ) ); }
759        '[' push ',' tuple_expression_list pop ']'
[1d4580a]760                { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)(new ExpressionNode( nullptr ) )->set_last( $4 ) ) ); }
[4d51835]761        | '[' push assignment_expression ',' tuple_expression_list pop ']'
[1d4580a]762                { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)$3->set_last( $5 ) ) ); }
[4d51835]763        ;
[51b7345]764
765tuple_expression_list:
[4d51835]766        assignment_expression_opt
767        | tuple_expression_list ',' assignment_expression_opt
[1d4580a]768                { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]769        ;
[51b7345]770
771comma_expression:
[4d51835]772        assignment_expression
[9706554]773        | comma_expression ',' assignment_expression
[513e165]774                { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[4d51835]775        ;
[51b7345]776
777comma_expression_opt:
[4d51835]778        // empty
[58dd019]779                { $$ = nullptr; }
[4d51835]780        | comma_expression
781        ;
[51b7345]782
[c11e31c]783//*************************** STATEMENTS *******************************
[51b7345]784
785statement:
[4d51835]786        labeled_statement
787        | compound_statement
788        | expression_statement                                          { $$ = $1; }
789        | selection_statement
790        | iteration_statement
791        | jump_statement
[8b47e50]792        | with_statement
[5b2edbc]793        | waitfor_statement
[4d51835]794        | exception_statement
795        | asm_statement
[51b7345]796
797labeled_statement:
[44a81853]798                // labels cannot be identifiers 0 or 1 or ATTR_IDENTIFIER
799        identifier_or_type_name ':' attribute_list_opt statement
[1db21619]800                {
[44a81853]801                        $$ = $4->add_label( $1, $3 );
[1db21619]802                }
[4d51835]803        ;
[51b7345]804
805compound_statement:
[4d51835]806        '{' '}'
[e82aa9df]807                { $$ = new StatementNode( build_compound( (StatementNode *)0 ) ); }
[4d51835]808        | '{'
[de62360d]809                // Two scopes are necessary because the block itself has a scope, but every declaration within the block also
[3a2128f]810                // requires its own scope.
[4d51835]811          push push
[51b1202]812          local_label_declaration_opt                                           // GCC, local labels
[9bd6105]813          statement_decl_list                                                           // C99, intermix declarations and statements
[c0aa336]814          pop '}'
[e82aa9df]815                { $$ = new StatementNode( build_compound( $5 ) ); }
[4d51835]816        ;
[b87a5ed]817
[9bd6105]818statement_decl_list:                                                                    // C99
819        statement_decl
820        | statement_decl_list push statement_decl
[1d4580a]821                { if ( $1 != 0 ) { $1->set_last( $3 ); $$ = $1; } }
[4d51835]822        ;
[51b7345]823
[9bd6105]824statement_decl:
[4d51835]825        declaration                                                                                     // CFA, new & old style declarations
[e82aa9df]826                { $$ = new StatementNode( $1 ); }
[4d51835]827        | EXTENSION declaration                                                         // GCC
[c0aa336]828                {
829                        distExt( $2 );
[e82aa9df]830                        $$ = new StatementNode( $2 );
[8e9cbb2]831                }
[4d51835]832        | function_definition
[e82aa9df]833                { $$ = new StatementNode( $1 ); }
[c0aa336]834        | EXTENSION function_definition                                         // GCC
835                {
836                        distExt( $2 );
837                        $$ = new StatementNode( $2 );
838                }
[4d51835]839        | statement pop
840        ;
[51b7345]841
[9bd6105]842statement_list_nodecl:
[4d51835]843        statement
[9bd6105]844        | statement_list_nodecl statement
[1d4580a]845                { if ( $1 != 0 ) { $1->set_last( $2 ); $$ = $1; } }
[4d51835]846        ;
[51b7345]847
848expression_statement:
[4d51835]849        comma_expression_opt ';'
[e82aa9df]850                { $$ = new StatementNode( build_expr( $1 ) ); }
[4d51835]851        ;
[51b7345]852
853selection_statement:
[51d6d6a]854        IF '(' push if_control_expression ')' statement         %prec THEN
[4d51835]855                // explicitly deal with the shift/reduce conflict on if/else
[6d49ea3]856                { $$ = new StatementNode( build_if( $4, $6, nullptr ) ); }
857        | IF '(' push if_control_expression ')' statement ELSE statement
858                { $$ = new StatementNode( build_if( $4, $6, $8 ) ); }
[4cc585b]859        | SWITCH '(' comma_expression ')' case_clause
[e82aa9df]860                { $$ = new StatementNode( build_switch( $3, $5 ) ); }
[4d51835]861        | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
[4e06c1e]862                {
[e82aa9df]863                        StatementNode *sw = new StatementNode( build_switch( $3, $8 ) );
[4e06c1e]864                        // The semantics of the declaration list is changed to include associated initialization, which is performed
865                        // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
866                        // statement around the switch.  Statements after the initial declaration list can never be executed, and
[8688ce1]867                        // therefore, are removed from the grammar even though C allows it. The change also applies to choose
868                        // statement.
[a7741435]869                        $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]870                }
[4d51835]871        | CHOOSE '(' comma_expression ')' case_clause           // CFA
[e82aa9df]872                { $$ = new StatementNode( build_switch( $3, $5 ) ); }
[4d51835]873        | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt choose_clause_list_opt '}' // CFA
[4e06c1e]874                {
[e82aa9df]875                        StatementNode *sw = new StatementNode( build_switch( $3, $8 ) );
[a7741435]876                        $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
[4e06c1e]877                }
[4d51835]878        ;
[b87a5ed]879
[936e9f4]880if_control_expression:
[6d49ea3]881        comma_expression pop
[936e9f4]882                { $$ = new IfCtl( nullptr, $1 ); }
[4cc585b]883        | c_declaration pop                                                                     // no semi-colon
[936e9f4]884                { $$ = new IfCtl( $1, nullptr ); }
[4cc585b]885        | cfa_declaration pop                                                           // no semi-colon
[936e9f4]886                { $$ = new IfCtl( $1, nullptr ); }
[6d49ea3]887        | declaration comma_expression                                          // semi-colon separated
[936e9f4]888                { $$ = new IfCtl( $1, $2 ); }
889        ;
890
[de62360d]891// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
892// clause allows a list of values and subranges.
[b87a5ed]893
894case_value:                                                                                             // CFA
[4d51835]895        constant_expression                                                     { $$ = $1; }
896        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
[d1625f8]897                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
[4d51835]898        | subrange                                                                                      // CFA, subrange
899        ;
[b87a5ed]900
901case_value_list:                                                                                // CFA
[e82aa9df]902        case_value                                                                      { $$ = new StatementNode( build_case( $1 ) ); }
[064e3ff]903                // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
[e82aa9df]904        | case_value_list ',' case_value                        { $$ = (StatementNode *)($1->set_last( new StatementNode( build_case( $3 ) ) ) ); }
[4d51835]905        ;
[b87a5ed]906
907case_label:                                                                                             // CFA
[8688ce1]908        CASE case_value_list ':'                                        { $$ = $2; }
[e82aa9df]909        | DEFAULT ':'                                                           { $$ = new StatementNode( build_default() ); }
[4d51835]910                // A semantic check is required to ensure only one default clause per switch/choose statement.
911        ;
[b87a5ed]912
913case_label_list:                                                                                // CFA
[4d51835]914        case_label
[1d4580a]915        | case_label_list case_label                            { $$ = (StatementNode *)( $1->set_last( $2 )); }
[4d51835]916        ;
[b87a5ed]917
918case_clause:                                                                                    // CFA
[e82aa9df]919        case_label_list statement                                       { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[4d51835]920        ;
[b87a5ed]921
922switch_clause_list_opt:                                                                 // CFA
[4d51835]923        // empty
[58dd019]924                { $$ = nullptr; }
[4d51835]925        | switch_clause_list
926        ;
[b87a5ed]927
928switch_clause_list:                                                                             // CFA
[9bd6105]929        case_label_list statement_list_nodecl
[e82aa9df]930                { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
[9bd6105]931        | switch_clause_list case_label_list statement_list_nodecl
[e82aa9df]932                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( $3 ) ) ) ) ); }
[4d51835]933        ;
[b87a5ed]934
935choose_clause_list_opt:                                                                 // CFA
[4d51835]936        // empty
[58dd019]937                { $$ = nullptr; }
[4d51835]938        | choose_clause_list
939        ;
[b87a5ed]940
941choose_clause_list:                                                                             // CFA
[4d51835]942        case_label_list fall_through
[de62360d]943                { $$ = $1->append_last_case( $2 ); }
[9bd6105]944        | case_label_list statement_list_nodecl fall_through_opt
[e82aa9df]945                { $$ = $1->append_last_case( new StatementNode( build_compound( (StatementNode *)$2->set_last( $3 ) ) ) ); }
[4d51835]946        | choose_clause_list case_label_list fall_through
[1d4580a]947                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( $3 ))); }
[9bd6105]948        | choose_clause_list case_label_list statement_list_nodecl fall_through_opt
[e82aa9df]949                { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( (StatementNode *)$3->set_last( $4 ) ) ) ) ) ); }
[4d51835]950        ;
[b87a5ed]951
952fall_through_opt:                                                                               // CFA
[4d51835]953        // empty
[ab57786]954                { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); } // insert implicit break
[4d51835]955        | fall_through
956        ;
[b87a5ed]957
958fall_through:                                                                                   // CFA
[4e06c1e]959        FALLTHRU
[58dd019]960                { $$ = nullptr; }
[4e06c1e]961        | FALLTHRU ';'
[58dd019]962                { $$ = nullptr; }
[4d51835]963        ;
[51b7345]964
965iteration_statement:
[4d51835]966        WHILE '(' comma_expression ')' statement
[e82aa9df]967                { $$ = new StatementNode( build_while( $3, $5 ) ); }
[4d51835]968        | DO statement WHILE '(' comma_expression ')' ';'
[5b639ee]969                { $$ = new StatementNode( build_while( $5, $2, true ) ); }
[4d51835]970        | FOR '(' push for_control_expression ')' statement
[e82aa9df]971                { $$ = new StatementNode( build_for( $4, $6 ) ); }
[4d51835]972        ;
[51b7345]973
974for_control_expression:
[4d51835]975        comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
[2f22cc4]976                { $$ = new ForCtl( $1, $4, $6 ); }
[4d51835]977        | declaration comma_expression_opt ';' comma_expression_opt // C99
[2f22cc4]978                { $$ = new ForCtl( $1, $2, $4 ); }
[d1625f8]979        ;
[51b7345]980
981jump_statement:
[44a81853]982        GOTO identifier_or_type_name ';'
[ab57786]983                { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
[4d51835]984        | GOTO '*' comma_expression ';'                                         // GCC, computed goto
[4e06c1e]985                // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
[de62360d]986                // whereas normal operator precedence yields goto (*i)+3;
[e82aa9df]987                { $$ = new StatementNode( build_computedgoto( $3 ) ); }
[4d51835]988        | CONTINUE ';'
[de62360d]989                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]990                { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
[44a81853]991        | CONTINUE identifier_or_type_name ';'                          // CFA, multi-level continue
[de62360d]992                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
993                // the target of the transfer appears only at the start of an iteration statement.
[ab57786]994                { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
[4d51835]995        | BREAK ';'
[de62360d]996                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
[ab57786]997                { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
[44a81853]998        | BREAK identifier_or_type_name ';'                                     // CFA, multi-level exit
[de62360d]999                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1000                // the target of the transfer appears only at the start of an iteration statement.
[ab57786]1001                { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
[4d51835]1002        | RETURN comma_expression_opt ';'
[e82aa9df]1003                { $$ = new StatementNode( build_return( $2 ) ); }
[8cc5cb0]1004        | THROW assignment_expression_opt ';'                           // handles rethrow
[e82aa9df]1005                { $$ = new StatementNode( build_throw( $2 ) ); }
[8cc5cb0]1006        | THROWRESUME assignment_expression_opt ';'                     // handles reresume
[daf1af8]1007                { $$ = new StatementNode( build_resume( $2 ) ); }
[8cc5cb0]1008        | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
[daf1af8]1009                { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
[4d51835]1010        ;
[51b7345]1011
[8b47e50]1012with_statement:
[5b2edbc]1013        WITH '(' tuple_expression_list ')' statement
1014                { $$ = nullptr; }                                                               // FIX ME
1015        ;
1016
[51d6d6a]1017when_clause:
1018        WHEN '(' comma_expression ')'
1019                { $$ = $3; }
1020        ;
1021
[5b2edbc]1022when_clause_opt:
1023        // empty
[135b431]1024                { $$ = nullptr; }
[51d6d6a]1025        | when_clause
[5b2edbc]1026        ;
1027
1028waitfor:
1029        WAITFOR '(' identifier ')'
[135b431]1030                {
1031                        $$ = new ExpressionNode( new NameExpr( *$3 ) );
1032                        delete $3;
1033                }
[5b2edbc]1034        | WAITFOR '(' identifier ',' argument_expression_list ')'
[135b431]1035                {
1036                        $$ = new ExpressionNode( new NameExpr( *$3 ) );
1037                        $$->set_last( $5 );
1038                        delete $3;
1039                }
[5b2edbc]1040        ;
1041
1042timeout:
1043        TIMEOUT '(' comma_expression ')'
[135b431]1044                { $$ = $3; }
[5b2edbc]1045        ;
1046
1047waitfor_clause:
[51d6d6a]1048        when_clause_opt waitfor statement                                       %prec THEN
[135b431]1049                { $$ = build_waitfor( $2, $3, $1 ); }
[5b2edbc]1050        | when_clause_opt waitfor statement WOR waitfor_clause
[135b431]1051                { $$ = build_waitfor( $2, $3, $1, $5 ); }
[51d6d6a]1052        | when_clause_opt timeout statement                                     %prec THEN
[135b431]1053                { $$ = build_waitfor_timeout( $2, $3, $1 ); }
[5b2edbc]1054        | when_clause_opt ELSE statement
[135b431]1055                { $$ = build_waitfor_timeout( nullptr, $3, $1 ); }
[51d6d6a]1056                // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1057        | when_clause_opt timeout statement WOR when_clause ELSE statement
[135b431]1058                { $$ = build_waitfor_timeout( $2, $3, $1, $7, $5 ); }
[5b2edbc]1059        ;
1060
1061waitfor_statement:
[51d6d6a]1062        when_clause_opt waitfor statement                                       %prec THEN
[135b431]1063                { $$ = new StatementNode( build_waitfor( $2, $3, $1 ) ); }
[5b2edbc]1064        | when_clause_opt waitfor statement WOR waitfor_clause
[135b431]1065                { $$ = new StatementNode( build_waitfor( $2, $3, $1, $5 ) ); }
[8b47e50]1066        ;
1067
[51b7345]1068exception_statement:
[cfaabe2c]1069        TRY compound_statement handler_clause
[e82aa9df]1070                { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
[4d51835]1071        | TRY compound_statement finally_clause
[e82aa9df]1072                { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
[cfaabe2c]1073        | TRY compound_statement handler_clause finally_clause
[e82aa9df]1074                { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
[4d51835]1075        ;
[51b7345]1076
1077handler_clause:
[cbce272]1078        handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
1079                { $$ = new StatementNode( build_catch( $1, $5, $7, $9 ) ); }
[dc2b4d6]1080        | handler_clause handler_key '(' push push exception_declaration pop handler_predicate_opt ')' compound_statement pop
[cbce272]1081                { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $2, $6, $8, $10 ) ) ); }
[994d080]1082        ;
1083
1084handler_predicate_opt:
1085        //empty
[cbce272]1086                { $$ = nullptr; }
[dc2b4d6]1087        | ';' conditional_expression
[cbce272]1088                { $$ = $2; }
[307a732]1089        ;
1090
1091handler_key:
1092        CATCH
1093                { $$ = CatchStmt::Terminate; }
1094        | CATCHRESUME
1095                { $$ = CatchStmt::Resume; }
[4d51835]1096        ;
[51b7345]1097
1098finally_clause:
[4d51835]1099        FINALLY compound_statement
1100                {
[e82aa9df]1101                        $$ = new StatementNode( build_finally( $2 ) );
[4d51835]1102                }
1103        ;
[51b7345]1104
1105exception_declaration:
[d0ffed1]1106                // No SUE declaration in parameter list.
1107        type_specifier_nobody
1108        | type_specifier_nobody declarator
[4d51835]1109                {
1110                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1111                        $$ = $2->addType( $1 );
1112                }
[d0ffed1]1113        | type_specifier_nobody variable_abstract_declarator
[4d51835]1114                { $$ = $2->addType( $1 ); }
[c0aa336]1115        | cfa_abstract_declarator_tuple no_attr_identifier      // CFA
[4d51835]1116                {
1117                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1118                        $$ = $1->addName( $2 );
1119                }
[c0aa336]1120        | cfa_abstract_declarator_tuple                                         // CFA
[4d51835]1121        ;
[51b7345]1122
1123asm_statement:
[ab57786]1124        ASM asm_volatile_opt '(' string_literal ')' ';'
[e82aa9df]1125                { $$ = new StatementNode( build_asmstmt( $2, $4, 0 ) ); }
[ab57786]1126        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
[e82aa9df]1127                { $$ = new StatementNode( build_asmstmt( $2, $4, $6 ) ); }
[ab57786]1128        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
[e82aa9df]1129                { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8 ) ); }
[ab57786]1130        | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
[e82aa9df]1131                { $$ = new StatementNode( build_asmstmt( $2, $4, $6, $8, $10 ) ); }
[ab57786]1132        | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
[e82aa9df]1133                { $$ = new StatementNode( build_asmstmt( $2, $5, 0, $8, $10, $12 ) ); }
[7f5566b]1134        ;
1135
1136asm_volatile_opt:                                                                               // GCC
1137        // empty
1138                { $$ = false; }
1139        | VOLATILE
1140                { $$ = true; }
[4d51835]1141        ;
[b87a5ed]1142
1143asm_operands_opt:                                                                               // GCC
[4d51835]1144        // empty
[58dd019]1145                { $$ = nullptr; }                                                               // use default argument
[4d51835]1146        | asm_operands_list
1147        ;
[b87a5ed]1148
1149asm_operands_list:                                                                              // GCC
[4d51835]1150        asm_operand
1151        | asm_operands_list ',' asm_operand
[1d4580a]1152                { $$ = (ExpressionNode *)$1->set_last( $3 ); }
[4d51835]1153        ;
[b87a5ed]1154
1155asm_operand:                                                                                    // GCC
[ab57786]1156        string_literal '(' constant_expression ')'
[513e165]1157                { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( (ExpressionNode *)nullptr ), $1, maybeMoveBuild< Expression >( $3 ) ) ); }
[ab57786]1158        | '[' constant_expression ']' string_literal '(' constant_expression ')'
[513e165]1159                { $$ = new ExpressionNode( new AsmExpr( maybeMoveBuild< Expression >( $2 ), $4, maybeMoveBuild< Expression >( $6 ) ) ); }
[7f5566b]1160        ;
1161
[4e06c1e]1162asm_clobbers_list_opt:                                                                  // GCC
[7f5566b]1163        // empty
[58dd019]1164                { $$ = nullptr; }                                                               // use default argument
[ab57786]1165        | string_literal
[d1625f8]1166                { $$ = new ExpressionNode( $1 ); }
[ab57786]1167        | asm_clobbers_list_opt ',' string_literal
[513e165]1168                // set_last returns ParseNode *
[e82aa9df]1169                { $$ = (ExpressionNode *)$1->set_last( new ExpressionNode( $3 ) ); }
[4d51835]1170        ;
[b87a5ed]1171
[7f5566b]1172label_list:
1173        no_attr_identifier
[ab57786]1174                {
1175                        $$ = new LabelNode(); $$->labels.push_back( *$1 );
1176                        delete $1;                                                                      // allocated by lexer
1177                }
[7f5566b]1178        | label_list ',' no_attr_identifier
[ab57786]1179                {
1180                        $$ = $1; $1->labels.push_back( *$3 );
1181                        delete $3;                                                                      // allocated by lexer
1182                }
[4d51835]1183        ;
[51b7345]1184
[c11e31c]1185//******************************* DECLARATIONS *********************************
[51b7345]1186
[b87a5ed]1187declaration_list_opt:                                                                   // used at beginning of switch statement
[4d51835]1188        pop
[58dd019]1189                { $$ = nullptr; }
[4d51835]1190        | declaration_list
1191        ;
[51b7345]1192
1193declaration_list:
[4d51835]1194        declaration
1195        | declaration_list push declaration
1196                { $$ = $1->appendList( $3 ); }
1197        ;
[51b7345]1198
[c0aa336]1199KR_declaration_list_opt:                                                                // used to declare parameter types in K&R style functions
[4cc585b]1200        // empty
[58dd019]1201                { $$ = nullptr; }
[c0aa336]1202        | KR_declaration_list
[4d51835]1203        ;
[51b7345]1204
[c0aa336]1205KR_declaration_list:
[4cc585b]1206        push c_declaration pop ';'
1207                { $$ = $2; }
1208        | KR_declaration_list push c_declaration pop ';'
[4d51835]1209                { $$ = $1->appendList( $3 ); }
1210        ;
[b87a5ed]1211
[51b1202]1212local_label_declaration_opt:                                                    // GCC, local label
[4d51835]1213        // empty
[51b1202]1214        | local_label_declaration_list
[4d51835]1215        ;
[b87a5ed]1216
[51b1202]1217local_label_declaration_list:                                                   // GCC, local label
1218        LABEL local_label_list ';'
1219        | local_label_declaration_list LABEL local_label_list ';'
[4d51835]1220        ;
[b87a5ed]1221
[51b1202]1222local_label_list:                                                                               // GCC, local label
[7f5566b]1223        no_attr_identifier_or_type_name                         {}
[51b1202]1224        | local_label_list ',' no_attr_identifier_or_type_name {}
[4d51835]1225        ;
[b87a5ed]1226
[936e9f4]1227declaration:                                                                                    // old & new style declarations
[4cc585b]1228        c_declaration pop ';'
1229        | cfa_declaration pop ';'                                                       // CFA
[4d51835]1230        ;
[b87a5ed]1231
[de62360d]1232// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1233// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1234// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1235// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1236// the declaration list (as in Pascal).  ANSI C and the new CFA declarations may appear together in the same program
1237// block, but cannot be mixed within a specific declaration.
[c11e31c]1238//
[b87a5ed]1239//                      CFA                                     C
1240//              [10] int x;                     int x[10];              // array of 10 integers
1241//              [10] * char y;          char *y[10];    // array of 10 pointers to char
1242
[c0aa336]1243cfa_declaration:                                                                                // CFA
[4cc585b]1244        cfa_variable_declaration
1245        | cfa_typedef_declaration
1246        | cfa_function_declaration
1247        | type_declaring_list
1248        | trait_specifier
[4d51835]1249        ;
[b87a5ed]1250
[c0aa336]1251cfa_variable_declaration:                                                               // CFA
1252        cfa_variable_specifier initializer_opt
[4d51835]1253                {
1254                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1255                        $$ = $1->addInitializer( $2 );
[4d51835]1256                }
[c0aa336]1257        | declaration_qualifier_list cfa_variable_specifier initializer_opt
[de62360d]1258                // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1259                // them as a type_qualifier cannot appear in that context.
[4d51835]1260                {
1261                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[3cfe27f]1262                        $$ = $2->addQualifiers( $1 )->addInitializer( $3 );;
[4d51835]1263                }
[c0aa336]1264        | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
[4d51835]1265                {
1266                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
[3cfe27f]1267                        $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) );
[4d51835]1268                }
1269        ;
[b87a5ed]1270
[c0aa336]1271cfa_variable_specifier:                                                                 // CFA
[de62360d]1272                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1273                // storage-class
[c0aa336]1274        cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
[4d51835]1275                {
1276                        typedefTable.setNextIdentifier( *$2 );
[58dd019]1277                        $$ = $1->addName( $2 )->addAsmName( $3 );
[4d51835]1278                }
[c0aa336]1279        | cfa_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1280                {
1281                        typedefTable.setNextIdentifier( *$2 );
[58dd019]1282                        $$ = $1->addName( $2 )->addAsmName( $3 );
[4d51835]1283                }
[c0aa336]1284        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
[4d51835]1285                {
1286                        typedefTable.setNextIdentifier( *$3 );
[58dd019]1287                        $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 );
[4d51835]1288                }
1289        ;
[b87a5ed]1290
[c0aa336]1291cfa_function_declaration:                                                               // CFA
1292        cfa_function_specifier
[4d51835]1293                {
1294                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1295                        $$ = $1;
1296                }
[c0aa336]1297        | type_qualifier_list cfa_function_specifier
[4d51835]1298                {
1299                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1300                        $$ = $2->addQualifiers( $1 );
1301                }
[c0aa336]1302        | declaration_qualifier_list cfa_function_specifier
[4d51835]1303                {
1304                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1305                        $$ = $2->addQualifiers( $1 );
1306                }
[c0aa336]1307        | declaration_qualifier_list type_qualifier_list cfa_function_specifier
[4d51835]1308                {
1309                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1310                        $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
1311                }
[c0aa336]1312        | cfa_function_declaration pop ',' push identifier_or_type_name
[4d51835]1313                {
1314                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1315                        $$ = $1->appendList( $1->cloneType( $5 ) );
1316                }
1317        ;
[b87a5ed]1318
[c0aa336]1319cfa_function_specifier:                                                                 // CFA
1320//      '[' ']' identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')' // S/R conflict
[1b29996]1321//              {
1322//                      $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1323//              }
[c0aa336]1324//      '[' ']' identifier '(' push cfa_parameter_type_list_opt pop ')'
[2871210]1325//              {
1326//                      typedefTable.setNextIdentifier( *$5 );
1327//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1328//              }
[c0aa336]1329//      | '[' ']' TYPEDEFname '(' push cfa_parameter_type_list_opt pop ')'
[2871210]1330//              {
1331//                      typedefTable.setNextIdentifier( *$5 );
1332//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1333//              }
1334//      | '[' ']' typegen_name
1335                // identifier_or_type_name must be broken apart because of the sequence:
[4d51835]1336                //
[c0aa336]1337                //   '[' ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
[4d51835]1338                //   '[' ']' type_specifier
1339                //
[2871210]1340                // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1341                // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
[c0aa336]1342        cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
1343                // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
[4d51835]1344                {
1345                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1346                }
[c0aa336]1347        | cfa_function_return identifier_or_type_name '(' push cfa_parameter_type_list_opt pop ')'
[4d51835]1348                {
1349                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1350                }
1351        ;
[b87a5ed]1352
[c0aa336]1353cfa_function_return:                                                                    // CFA
1354        '[' push cfa_parameter_list pop ']'
[4d51835]1355                { $$ = DeclarationNode::newTuple( $3 ); }
[c0aa336]1356        | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1357                // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the
[de62360d]1358                // ']'.
[4d51835]1359                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1360        ;
[b87a5ed]1361
[c0aa336]1362cfa_typedef_declaration:                                                                // CFA
1363        TYPEDEF cfa_variable_specifier
[4d51835]1364                {
[de62360d]1365                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1366                        $$ = $2->addTypedef();
1367                }
[c0aa336]1368        | TYPEDEF cfa_function_specifier
[4d51835]1369                {
[de62360d]1370                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1371                        $$ = $2->addTypedef();
1372                }
[c0aa336]1373        | cfa_typedef_declaration pop ',' push no_attr_identifier
[4d51835]1374                {
[de62360d]1375                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1376                        $$ = $1->appendList( $1->cloneType( $5 ) );
1377                }
1378        ;
[b87a5ed]1379
[de62360d]1380// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1381// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b7345]1382
1383typedef_declaration:
[4d51835]1384        TYPEDEF type_specifier declarator
1385                {
[de62360d]1386                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1387                        $$ = $3->addType( $2 )->addTypedef();
1388                }
1389        | typedef_declaration pop ',' push declarator
1390                {
[de62360d]1391                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1392                        $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1393                }
[de62360d]1394        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[4d51835]1395                {
[de62360d]1396                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1397                        $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1398                }
1399        | type_specifier TYPEDEF declarator
1400                {
[de62360d]1401                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1402                        $$ = $3->addType( $1 )->addTypedef();
1403                }
1404        | type_specifier TYPEDEF type_qualifier_list declarator
1405                {
[de62360d]1406                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1407                        $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
[4d51835]1408                }
1409        ;
[b87a5ed]1410
[721f17a]1411typedef_expression:
1412                // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
[4d51835]1413        TYPEDEF no_attr_identifier '=' assignment_expression
1414                {
[de62360d]1415                        typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
[c0aa336]1416                        $$ = DeclarationNode::newName( 0 );                     // unimplemented
[4d51835]1417                }
1418        | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1419                {
[de62360d]1420                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[c0aa336]1421                        $$ = DeclarationNode::newName( 0 );                     // unimplemented
[4d51835]1422                }
1423        ;
[51b7345]1424
[c0aa336]1425//c_declaration:
1426//      declaring_list pop ';'
1427//      | typedef_declaration pop ';'
1428//      | typedef_expression pop ';'                                            // GCC, naming expression type
1429//      | sue_declaration_specifier pop ';'
1430//      ;
1431//
1432//declaring_list:
1433//              // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1434//              // storage-class
1435//       declarator asm_name_opt initializer_opt
1436//              {
1437//                      typedefTable.addToEnclosingScope( TypedefTable::ID );
1438//                      $$ = ( $2->addType( $1 ))->addAsmName( $3 )->addInitializer( $4 );
1439//              }
1440//      | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1441//              {
1442//                      typedefTable.addToEnclosingScope( TypedefTable::ID );
1443//                      $$ = $1->appendList( $1->cloneBaseType( $4->addAsmName( $5 )->addInitializer( $6 ) ) );
1444//              }
1445//      ;
1446
1447c_declaration:
[4cc585b]1448        declaration_specifier declaring_list
[c0aa336]1449                {
1450                        $$ = distAttr( $1, $2 );
1451                }
[4cc585b]1452        | typedef_declaration
1453        | typedef_expression                                                            // GCC, naming expression type
1454        | sue_declaration_specifier
[4d51835]1455        ;
[51b7345]1456
1457declaring_list:
[de62360d]1458                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1459                // storage-class
[c0aa336]1460        declarator asm_name_opt initializer_opt
[4d51835]1461                {
1462                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[c0aa336]1463                        $$ = $1->addAsmName( $2 )->addInitializer( $3 );
[4d51835]1464                }
1465        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1466                {
1467                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[c0aa336]1468                        $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) );
[4d51835]1469                }
1470        ;
[b87a5ed]1471
1472declaration_specifier:                                                                  // type specifier + storage class
[4d51835]1473        basic_declaration_specifier
1474        | sue_declaration_specifier
[84d58c5]1475        | type_declaration_specifier
[4d51835]1476        ;
[b87a5ed]1477
[d0ffed1]1478declaration_specifier_nobody:                                                   // type specifier + storage class - {...}
1479                // Preclude SUE declarations in restricted scopes:
1480                //
1481                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1482                //
1483                // because it is impossible to call f due to name equivalence.
1484        basic_declaration_specifier
1485        | sue_declaration_specifier_nobody
[84d58c5]1486        | type_declaration_specifier
[d0ffed1]1487        ;
1488
1489type_specifier:                                                                                 // type specifier
[4d51835]1490        basic_type_specifier
1491        | sue_type_specifier
[84d58c5]1492        | type_type_specifier
[4d51835]1493        ;
[b87a5ed]1494
[d0ffed1]1495type_specifier_nobody:                                                                  // type specifier - {...}
1496                // Preclude SUE declarations in restricted scopes:
1497                //
1498                //    int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1499                //
1500                // because it is impossible to call f due to name equivalence.
1501        basic_type_specifier
1502        | sue_type_specifier_nobody
[84d58c5]1503        | type_type_specifier
[d0ffed1]1504        ;
1505
[b87a5ed]1506type_qualifier_list_opt:                                                                // GCC, used in asm_statement
[4d51835]1507        // empty
[58dd019]1508                { $$ = nullptr; }
[4d51835]1509        | type_qualifier_list
1510        ;
[51b7345]1511
1512type_qualifier_list:
[de62360d]1513                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]1514                //
[de62360d]1515                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1516                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1517                // appeared only once.
[4d51835]1518        type_qualifier
1519        | type_qualifier_list type_qualifier
1520                { $$ = $1->addQualifiers( $2 ); }
1521        ;
[51b7345]1522
1523type_qualifier:
[4d51835]1524        type_qualifier_name
1525        | attribute
1526        ;
[51b7345]1527
1528type_qualifier_name:
[4d51835]1529        CONST
[738e304]1530                { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
[4d51835]1531        | RESTRICT
[738e304]1532                { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
[4d51835]1533        | VOLATILE
[738e304]1534                { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
[a7c90d4]1535        | MUTEX
[738e304]1536                { $$ = DeclarationNode::newTypeQualifier( Type::Mutex ); }
[4d51835]1537        | ATOMIC
[738e304]1538                { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
[4d51835]1539        | FORALL '('
1540                {
1541                        typedefTable.enterScope();
1542                }
1543          type_parameter_list ')'                                                       // CFA
1544                {
1545                        typedefTable.leaveScope();
1546                        $$ = DeclarationNode::newForall( $4 );
1547                }
1548        ;
[51b7345]1549
1550declaration_qualifier_list:
[4d51835]1551        storage_class_list
[de62360d]1552        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
[4d51835]1553                { $$ = $1->addQualifiers( $2 ); }
1554        | declaration_qualifier_list type_qualifier_list storage_class_list
1555                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1556        ;
[51b7345]1557
1558storage_class_list:
[de62360d]1559                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1560                // only one of each is specified, except for inline, which can appear with the others.
[4d51835]1561                //
[de62360d]1562                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1563                // specifiers in a declaration.
[4d51835]1564        storage_class
1565        | storage_class_list storage_class
1566                { $$ = $1->addQualifiers( $2 ); }
1567        ;
[51b7345]1568
1569storage_class:
[4d51835]1570        EXTERN
[68fe077a]1571                { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
[4d51835]1572        | STATIC
[68fe077a]1573                { $$ = DeclarationNode::newStorageClass( Type::Static ); }
[4d51835]1574        | AUTO
[68fe077a]1575                { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
[4d51835]1576        | REGISTER
[68fe077a]1577                { $$ = DeclarationNode::newStorageClass( Type::Register ); }
[dd020c0]1578        | THREADLOCAL                                                                           // C11
[68fe077a]1579                { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
[dd020c0]1580                // Put function specifiers here to simplify parsing rules, but separate them semantically.
[4d51835]1581        | INLINE                                                                                        // C99
[ddfd945]1582                { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
[4d51835]1583        | FORTRAN                                                                                       // C99
[ddfd945]1584                { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
[68cd1ce]1585        | NORETURN                                                                                      // C11
[ddfd945]1586                { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
[4d51835]1587        ;
[51b7345]1588
1589basic_type_name:
[4d51835]1590        CHAR
1591                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1592        | DOUBLE
1593                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1594        | FLOAT
1595                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1596        | INT
1597                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1598        | LONG
[5b639ee]1599                { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
[4d51835]1600        | SHORT
[5b639ee]1601                { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
[4d51835]1602        | SIGNED
[5b639ee]1603                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
[4d51835]1604        | UNSIGNED
[5b639ee]1605                { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
[4d51835]1606        | VOID
1607                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1608        | BOOL                                                                                          // C99
1609                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1610        | COMPLEX                                                                                       // C99
[5b639ee]1611                { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
[4d51835]1612        | IMAGINARY                                                                                     // C99
[5b639ee]1613                { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
[3a2128f]1614        | ZERO_T
[148f7290]1615                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
[3a2128f]1616        | ONE_T
[148f7290]1617                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
[72457b6]1618        | VALIST                                                                                        // GCC, __builtin_va_list
1619                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
[4d51835]1620        ;
[51b7345]1621
1622basic_declaration_specifier:
[4d51835]1623                // A semantic check is necessary for conflicting storage classes.
1624        basic_type_specifier
1625        | declaration_qualifier_list basic_type_specifier
1626                { $$ = $2->addQualifiers( $1 ); }
1627        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
1628                { $$ = $1->addQualifiers( $2 ); }
1629        | basic_declaration_specifier storage_class type_qualifier_list
1630                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1631        | basic_declaration_specifier storage_class basic_type_specifier
1632                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1633        ;
[51b7345]1634
1635basic_type_specifier:
[84d58c5]1636        direct_type
1637        | type_qualifier_list_opt indirect_type type_qualifier_list_opt
[4d51835]1638                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1639        ;
[51b7345]1640
[84d58c5]1641direct_type:
[4d51835]1642                // A semantic check is necessary for conflicting type qualifiers.
1643        basic_type_name
1644        | type_qualifier_list basic_type_name
1645                { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1646        | direct_type type_qualifier
[4d51835]1647                { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1648        | direct_type basic_type_name
[4d51835]1649                { $$ = $1->addType( $2 ); }
1650        ;
[51b7345]1651
[84d58c5]1652indirect_type:
1653        TYPEOF '(' type ')'                                                                     // GCC: typeof(x) y;
[4d51835]1654                { $$ = $3; }
1655        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof(a+b) y;
1656                { $$ = DeclarationNode::newTypeof( $3 ); }
[84d58c5]1657        | ATTR_TYPEGENname '(' type ')'                                         // CFA: e.g., @type(x) y;
[4d51835]1658                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1659        | ATTR_TYPEGENname '(' comma_expression ')'                     // CFA: e.g., @type(a+b) y;
1660                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1661        ;
[51b7345]1662
[d0ffed1]1663sue_declaration_specifier:                                                              // struct, union, enum + storage class + type specifier
[4d51835]1664        sue_type_specifier
1665        | declaration_qualifier_list sue_type_specifier
1666                { $$ = $2->addQualifiers( $1 ); }
1667        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
1668                { $$ = $1->addQualifiers( $2 ); }
1669        | sue_declaration_specifier storage_class type_qualifier_list
1670                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1671        ;
[51b7345]1672
[d0ffed1]1673sue_type_specifier:                                                                             // struct, union, enum + type specifier
1674        elaborated_type
[fdca7c6]1675        | type_qualifier_list
1676                { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
1677          elaborated_type
1678                { $$ = $3->addQualifiers( $1 ); }
[4d51835]1679        | sue_type_specifier type_qualifier
1680                { $$ = $1->addQualifiers( $2 ); }
1681        ;
[51b7345]1682
[d0ffed1]1683sue_declaration_specifier_nobody:                                               // struct, union, enum - {...} + storage class + type specifier
1684        sue_type_specifier_nobody
1685        | declaration_qualifier_list sue_type_specifier_nobody
1686                { $$ = $2->addQualifiers( $1 ); }
1687        | sue_declaration_specifier_nobody storage_class        // remaining OBSOLESCENT (see 2)
1688                { $$ = $1->addQualifiers( $2 ); }
1689        | sue_declaration_specifier_nobody storage_class type_qualifier_list
1690                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1691        ;
1692
1693sue_type_specifier_nobody:                                                              // struct, union, enum - {...} + type specifier
1694        elaborated_type_nobody
1695        | type_qualifier_list elaborated_type_nobody
1696                { $$ = $2->addQualifiers( $1 ); }
1697        | sue_type_specifier_nobody type_qualifier
1698                { $$ = $1->addQualifiers( $2 ); }
1699        ;
1700
[84d58c5]1701type_declaration_specifier:
1702        type_type_specifier
1703        | declaration_qualifier_list type_type_specifier
[4d51835]1704                { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1705        | type_declaration_specifier storage_class                      // remaining OBSOLESCENT (see 2)
[4d51835]1706                { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1707        | type_declaration_specifier storage_class type_qualifier_list
[4d51835]1708                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1709        ;
[b87a5ed]1710
[84d58c5]1711type_type_specifier:                                                                    // typedef types
1712        type_name
1713        | type_qualifier_list type_name
1714                { $$ = $2->addQualifiers( $1 ); }
1715        | type_type_specifier type_qualifier
1716                { $$ = $1->addQualifiers( $2 ); }
1717        ;
1718
1719type_name:
[4d51835]1720        TYPEDEFname
1721                { $$ = DeclarationNode::newFromTypedef( $1 ); }
[84d58c5]1722        | '.' TYPEDEFname
1723                { $$ = DeclarationNode::newFromTypedef( $2 ); } // FIX ME
1724        | type_name '.' TYPEDEFname
1725                { $$ = DeclarationNode::newFromTypedef( $3 ); } // FIX ME
1726        | typegen_name
1727        | '.' typegen_name
1728                { $$ = $2; }                                                                    // FIX ME
1729        | type_name '.' typegen_name
1730                { $$ = $3; }                                                                    // FIX ME
1731        ;
1732
1733typegen_name:                                                                                   // CFA
[67cf18c]1734        TYPEGENname '(' ')'
1735                { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1736        | TYPEGENname '(' type_list ')'
[84d58c5]1737                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
[4d51835]1738        ;
[51b7345]1739
[d0ffed1]1740elaborated_type:                                                                                // struct, union, enum
[c0aa336]1741        aggregate_type
1742        | enum_type
[4d51835]1743        ;
[51b7345]1744
[d0ffed1]1745elaborated_type_nobody:                                                                 // struct, union, enum - {...}
1746        aggregate_type_nobody
1747        | enum_type_nobody
1748        ;
1749
1750aggregate_type:                                                                                 // struct, union
[c0aa336]1751        aggregate_key attribute_list_opt '{' field_declaration_list '}'
[6f95000]1752                { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
[c0aa336]1753        | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
[fdca7c6]1754                {
1755                        typedefTable.makeTypedef( *$3 );                        // create typedef
1756                        if ( forall ) typedefTable.changeKind( *$3, TypedefTable::TG ); // possibly update
1757                        forall = false;                                                         // reset
1758                }
[c0aa336]1759          '{' field_declaration_list '}'
1760                { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
[84d58c5]1761        | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list '}' // CFA
[6f95000]1762                { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
[d0ffed1]1763        | aggregate_type_nobody
1764        ;
1765
1766aggregate_type_nobody:                                                                  // struct, union - {...}
[84d58c5]1767        aggregate_key attribute_list_opt no_attr_identifier
1768                {
1769                        typedefTable.makeTypedef( *$3 );
1770                        $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1771                }
1772        | aggregate_key attribute_list_opt TYPEDEFname
[d0ffed1]1773                {
1774                        typedefTable.makeTypedef( *$3 );
1775                        $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1776                }
[84d58c5]1777        | aggregate_key attribute_list_opt typegen_name         // CFA
[c0aa336]1778                { $$ = $3->addQualifiers( $2 ); }
[4d51835]1779        ;
[51b7345]1780
1781aggregate_key:
[c0aa336]1782        STRUCT
[4d51835]1783                { $$ = DeclarationNode::Struct; }
[c0aa336]1784        | UNION
[4d51835]1785                { $$ = DeclarationNode::Union; }
[d3bc0ad]1786        | COROUTINE
[409433da]1787                { $$ = DeclarationNode::Coroutine; }
[d3bc0ad]1788        | MONITOR
[409433da]1789                { $$ = DeclarationNode::Monitor; }
[d3bc0ad]1790        | THREAD
[409433da]1791                { $$ = DeclarationNode::Thread; }
[4d51835]1792        ;
[51b7345]1793
1794field_declaration_list:
[5d125e4]1795        // empty
[58dd019]1796                { $$ = nullptr; }
[4d51835]1797        | field_declaration_list field_declaration
[a7741435]1798                { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]1799        ;
[51b7345]1800
1801field_declaration:
[c0aa336]1802        cfa_field_declaring_list ';'                                            // CFA, new style field declaration
1803        | EXTENSION cfa_field_declaring_list ';'                        // GCC
1804                {
1805                        distExt( $2 );                                                          // mark all fields in list
[8e9cbb2]1806                        $$ = $2;
1807                }
[c0aa336]1808        | type_specifier field_declaring_list ';'
1809                {
1810                        $$ = distAttr( $1, $2 ); }
1811        | EXTENSION type_specifier field_declaring_list ';'     // GCC
1812                {
1813                        distExt( $3 );                                                          // mark all fields in list
1814                        $$ = distAttr( $2, $3 );
1815                }
[4d51835]1816        ;
[b87a5ed]1817
[c0aa336]1818cfa_field_declaring_list:                                                               // CFA, new style field declaration
1819        cfa_abstract_declarator_tuple                                           // CFA, no field name
1820        | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
[4d51835]1821                { $$ = $1->addName( $2 ); }
[c0aa336]1822        | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
[4d51835]1823                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
[c0aa336]1824        | cfa_field_declaring_list ','                                          // CFA, no field name
[4d51835]1825                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1826        ;
[51b7345]1827
1828field_declaring_list:
[c0aa336]1829        field_declarator
[4d51835]1830        | field_declaring_list ',' attribute_list_opt field_declarator
[c0aa336]1831                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
[4d51835]1832        ;
[51b7345]1833
1834field_declarator:
[4d51835]1835        // empty
1836                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
[6f95000]1837        // '@'
1838        //      { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
[4d51835]1839        | bit_subrange_size                                                                     // no field name
1840                { $$ = DeclarationNode::newBitfield( $1 ); }
1841        | variable_declarator bit_subrange_size_opt
1842                // A semantic check is required to ensure bit_subrange only appears on base type int.
1843                { $$ = $1->addBitfield( $2 ); }
[c6b1105]1844        | variable_type_redeclarator bit_subrange_size_opt
[4d51835]1845                // A semantic check is required to ensure bit_subrange only appears on base type int.
1846                { $$ = $1->addBitfield( $2 ); }
1847        | variable_abstract_declarator                                          // CFA, no field name
1848        ;
[51b7345]1849
1850bit_subrange_size_opt:
[4d51835]1851        // empty
[58dd019]1852                { $$ = nullptr; }
[4d51835]1853        | bit_subrange_size
1854                { $$ = $1; }
1855        ;
[51b7345]1856
1857bit_subrange_size:
[4d51835]1858        ':' constant_expression
1859                { $$ = $2; }
1860        ;
[51b7345]1861
[d0ffed1]1862enum_type:                                                                                              // enum
[c0aa336]1863        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
[6f95000]1864                { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
[c0aa336]1865        | ENUM attribute_list_opt no_attr_identifier_or_type_name
[d0ffed1]1866                { typedefTable.makeTypedef( *$3 ); }
1867          '{' enumerator_list comma_opt '}'
1868                { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1869        | enum_type_nobody
1870        ;
1871
1872enum_type_nobody:                                                                               // enum - {...}
1873        ENUM attribute_list_opt no_attr_identifier_or_type_name
[45161b4d]1874                {
[c0aa336]1875                        typedefTable.makeTypedef( *$3 );
[ca1a547]1876                        $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
[45161b4d]1877                }
[4d51835]1878        ;
[51b7345]1879
1880enumerator_list:
[2871210]1881        no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1882                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
[2871210]1883        | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1884                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1885        ;
[51b7345]1886
1887enumerator_value_opt:
[4d51835]1888        // empty
[58dd019]1889                { $$ = nullptr; }
[4d51835]1890        | '=' constant_expression
1891                { $$ = $2; }
1892        ;
[51b7345]1893
[c11e31c]1894// Minimum of one parameter after which ellipsis is allowed only at the end.
[51b7345]1895
[c0aa336]1896cfa_parameter_type_list_opt:                                                    // CFA
[4d51835]1897        // empty
[58dd019]1898                { $$ = nullptr; }
[c0aa336]1899        | cfa_parameter_type_list
[4d51835]1900        ;
[b87a5ed]1901
[c0aa336]1902cfa_parameter_type_list:                                                                // CFA, abstract + real
1903        cfa_abstract_parameter_list
1904        | cfa_parameter_list
1905        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
[4d51835]1906                { $$ = $1->appendList( $5 ); }
[c0aa336]1907        | cfa_abstract_parameter_list pop ',' push ELLIPSIS
[4d51835]1908                { $$ = $1->addVarArgs(); }
[c0aa336]1909        | cfa_parameter_list pop ',' push ELLIPSIS
[4d51835]1910                { $$ = $1->addVarArgs(); }
1911        ;
[b87a5ed]1912
[c0aa336]1913cfa_parameter_list:                                                                             // CFA
1914                // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1915                // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1916        cfa_parameter_declaration
1917        | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1918                { $$ = $1->appendList( $5 ); }
[c0aa336]1919        | cfa_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1920                { $$ = $1->appendList( $5 ); }
[c0aa336]1921        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
[4d51835]1922                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1923        ;
[b87a5ed]1924
[c0aa336]1925cfa_abstract_parameter_list:                                                    // CFA, new & old style abstract
1926        cfa_abstract_parameter_declaration
1927        | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
[4d51835]1928                { $$ = $1->appendList( $5 ); }
1929        ;
[51b7345]1930
1931parameter_type_list_opt:
[4d51835]1932        // empty
[58dd019]1933                { $$ = nullptr; }
[4d51835]1934        | parameter_type_list
1935        ;
[51b7345]1936
1937parameter_type_list:
[4d51835]1938        parameter_list
1939        | parameter_list pop ',' push ELLIPSIS
1940                { $$ = $1->addVarArgs(); }
1941        ;
[b87a5ed]1942
1943parameter_list:                                                                                 // abstract + real
[4d51835]1944        abstract_parameter_declaration
1945        | parameter_declaration
1946        | parameter_list pop ',' push abstract_parameter_declaration
1947                { $$ = $1->appendList( $5 ); }
1948        | parameter_list pop ',' push parameter_declaration
1949                { $$ = $1->appendList( $5 ); }
1950        ;
[51b7345]1951
[de62360d]1952// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]1953// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b7345]1954
[c0aa336]1955cfa_parameter_declaration:                                                              // CFA, new & old style parameter declaration
[4d51835]1956        parameter_declaration
[994d080]1957        | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initialize_opt
[4d51835]1958                { $$ = $1->addName( $2 ); }
[994d080]1959        | cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[c0aa336]1960                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]1961                { $$ = $1->addName( $2 ); }
[994d080]1962        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[4d51835]1963                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[c0aa336]1964        | cfa_function_specifier
[4d51835]1965        ;
[b87a5ed]1966
[c0aa336]1967cfa_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
[4d51835]1968        abstract_parameter_declaration
[c0aa336]1969        | cfa_identifier_parameter_declarator_no_tuple
1970        | cfa_abstract_tuple
1971                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
1972        | type_qualifier_list cfa_abstract_tuple
[4d51835]1973                { $$ = $2->addQualifiers( $1 ); }
[c0aa336]1974        | cfa_abstract_function
[4d51835]1975        ;
[51b7345]1976
1977parameter_declaration:
[d0ffed1]1978                // No SUE declaration in parameter list.
[994d080]1979        declaration_specifier_nobody identifier_parameter_declarator default_initialize_opt
[4d51835]1980                {
1981                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]1982                        $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]1983                }
[994d080]1984        | declaration_specifier_nobody type_parameter_redeclarator default_initialize_opt
[4d51835]1985                {
1986                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[a7741435]1987                        $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
[4d51835]1988                }
1989        ;
[51b7345]1990
1991abstract_parameter_declaration:
[994d080]1992        declaration_specifier_nobody default_initialize_opt
[e7cc8cb]1993                { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
[994d080]1994        | declaration_specifier_nobody abstract_parameter_declarator default_initialize_opt
[e7cc8cb]1995                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]1996        ;
[51b7345]1997
[c11e31c]1998// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]1999// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2000// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
[51b7345]2001
[b87a5ed]2002identifier_list:                                                                                // K&R-style parameter list => no types
[4d51835]2003        no_attr_identifier
2004                { $$ = DeclarationNode::newName( $1 ); }
2005        | identifier_list ',' no_attr_identifier
2006                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2007        ;
[51b7345]2008
[2871210]2009identifier_or_type_name:
[4d51835]2010        identifier
2011        | TYPEDEFname
2012        | TYPEGENname
2013        ;
[51b7345]2014
[2871210]2015no_attr_identifier_or_type_name:
[4d51835]2016        no_attr_identifier
2017        | TYPEDEFname
[721f17a]2018        | TYPEGENname
[4d51835]2019        ;
[b87a5ed]2020
[84d58c5]2021type_no_function:                                                                               // sizeof, alignof, cast (constructor)
[c0aa336]2022        cfa_abstract_declarator_tuple                                           // CFA
[4d51835]2023        | type_specifier
[c0aa336]2024        | type_specifier abstract_declarator
[4d51835]2025                { $$ = $2->addType( $1 ); }
2026        ;
[b87a5ed]2027
[84d58c5]2028type:                                                                                                   // typeof, assertion
2029        type_no_function
[c0aa336]2030        | cfa_abstract_function                                                         // CFA
[4d51835]2031        ;
[51b7345]2032
2033initializer_opt:
[4d51835]2034        // empty
[58dd019]2035                { $$ = nullptr; }
[2871210]2036        | '=' initializer
2037                { $$ = $2; }
[641c3d0]2038        | '=' VOID
2039                { $$ = nullptr; }
[097e2b0]2040        | ATassign initializer
[974906e2]2041                { $$ = $2->set_maybeConstructed( false ); }
[4d51835]2042        ;
[51b7345]2043
2044initializer:
[de62360d]2045        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
2046        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
[4d51835]2047        ;
[51b7345]2048
2049initializer_list:
[097e2b0]2050        // empty
[58dd019]2051                { $$ = nullptr; }
[097e2b0]2052        | initializer
[4d51835]2053        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
[1d4580a]2054        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
[4d51835]2055        | initializer_list ',' designation initializer
[1d4580a]2056                { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
[4d51835]2057        ;
[b87a5ed]2058
[de62360d]2059// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2060// '=' to separator the designator from the initializer value, as in:
[c11e31c]2061//
[b87a5ed]2062//              int x[10] = { [1] = 3 };
[c11e31c]2063//
[de62360d]2064// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
2065// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2066// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2067// shift/reduce conflicts
[51b7345]2068
2069designation:
[4d51835]2070        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
[84d58c5]2071        | no_attr_identifier ':'                                                        // GCC, field name
[d1625f8]2072                { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]2073        ;
[51b7345]2074
[b87a5ed]2075designator_list:                                                                                // C99
[4d51835]2076        designator
[2871210]2077        | designator_list designator
[1d4580a]2078                { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
[d1625f8]2079        //| designator_list designator                                          { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]2080        ;
[51b7345]2081
2082designator:
[84d58c5]2083        '.' no_attr_identifier                                                          // C99, field name
[d1625f8]2084                { $$ = new ExpressionNode( build_varref( $2 ) ); }
[4d51835]2085        | '[' push assignment_expression pop ']'                        // C99, single array element
[de62360d]2086                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]2087                { $$ = $3; }
[4d51835]2088        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
[d1625f8]2089                { $$ = $3; }
[4d51835]2090        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[d1625f8]2091                { $$ = new ExpressionNode( build_range( $3, $5 ) ); }
[4d51835]2092        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
[d1625f8]2093                { $$ = $4; }
[4d51835]2094        ;
[51b7345]2095
[de62360d]2096// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2097// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]2098//
2099// Overloading: function, data, and operator identifiers may be overloaded.
2100//
[de62360d]2101// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2102//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2103//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]2104//
[de62360d]2105// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2106//     site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]2107//
2108// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]2109//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2110//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
2111//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
2112//     on type arguments of polymorphic functions.
[c11e31c]2113
[b87a5ed]2114type_parameter_list:                                                                    // CFA
[67cf18c]2115        type_parameter
2116                { $$ = $1; }
2117        | type_parameter_list ',' type_parameter
[4d51835]2118                { $$ = $1->appendList( $3 ); }
2119        ;
[b87a5ed]2120
[84d58c5]2121type_initializer_opt:                                                                   // CFA
2122        // empty
2123                { $$ = nullptr; }
2124        | '=' type
2125                { $$ = $2; }
2126        ;
2127
[b87a5ed]2128type_parameter:                                                                                 // CFA
[2871210]2129        type_class no_attr_identifier_or_type_name
2130                { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
[67cf18c]2131          type_initializer_opt assertion_list_opt
2132                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[4d51835]2133        | type_specifier identifier_parameter_declarator
2134        ;
[b87a5ed]2135
2136type_class:                                                                                             // CFA
[4040425]2137        OTYPE
[5b639ee]2138                { $$ = DeclarationNode::Otype; }
[4d51835]2139        | DTYPE
2140                { $$ = DeclarationNode::Dtype; }
[8f60f0b]2141        | FTYPE
2142                { $$ = DeclarationNode::Ftype; }
2143        | TTYPE
2144                { $$ = DeclarationNode::Ttype; }
[4d51835]2145        ;
[b87a5ed]2146
2147assertion_list_opt:                                                                             // CFA
[4d51835]2148        // empty
[58dd019]2149                { $$ = nullptr; }
[4d51835]2150        | assertion_list_opt assertion
[a7741435]2151                { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]2152        ;
[b87a5ed]2153
2154assertion:                                                                                              // CFA
[84d58c5]2155        '|' no_attr_identifier_or_type_name '(' type_list ')'
[4d51835]2156                {
[4040425]2157                        typedefTable.openTrait( *$2 );
2158                        $$ = DeclarationNode::newTraitUse( $2, $4 );
[4d51835]2159                }
[4040425]2160        | '|' '{' push trait_declaration_list '}'
[4d51835]2161                { $$ = $4; }
[84d58c5]2162        | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_list ')'
[58dd019]2163                { $$ = nullptr; }
[4d51835]2164        ;
[b87a5ed]2165
[84d58c5]2166type_list:                                                                                              // CFA
2167        type
[513e165]2168                { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
[4d51835]2169        | assignment_expression
[84d58c5]2170        | type_list ',' type
[513e165]2171                { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) ) ); }
[84d58c5]2172        | type_list ',' assignment_expression
[1d4580a]2173                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]2174        ;
[b87a5ed]2175
2176type_declaring_list:                                                                    // CFA
[4040425]2177        OTYPE type_declarator
[4d51835]2178                { $$ = $2; }
[4040425]2179        | storage_class_list OTYPE type_declarator
[4d51835]2180                { $$ = $3->addQualifiers( $1 ); }
2181        | type_declaring_list ',' type_declarator
[a7c90d4]2182                { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
[4d51835]2183        ;
[b87a5ed]2184
2185type_declarator:                                                                                // CFA
[4d51835]2186        type_declarator_name assertion_list_opt
2187                { $$ = $1->addAssertions( $2 ); }
[84d58c5]2188        | type_declarator_name assertion_list_opt '=' type
[4d51835]2189                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2190        ;
[b87a5ed]2191
2192type_declarator_name:                                                                   // CFA
[2871210]2193        no_attr_identifier_or_type_name
[4d51835]2194                {
[de62360d]2195                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
[4d51835]2196                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
2197                }
[84d58c5]2198        | no_attr_identifier_or_type_name '(' push type_parameter_list pop ')'
[4d51835]2199                {
[de62360d]2200                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
[4d51835]2201                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
2202                }
2203        ;
[b87a5ed]2204
[4040425]2205trait_specifier:                                                                                // CFA
2206        TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
[4d51835]2207                {
[de62360d]2208                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2209                        $$ = DeclarationNode::newTrait( $2, $5, 0 );
[4d51835]2210                }
[4040425]2211        | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
[4d51835]2212                {
[4040425]2213                        typedefTable.enterTrait( *$2 );
[4d51835]2214                        typedefTable.enterScope();
2215                }
[4040425]2216          trait_declaration_list '}'
[4d51835]2217                {
[4040425]2218                        typedefTable.leaveTrait();
[de62360d]2219                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4040425]2220                        $$ = DeclarationNode::newTrait( $2, $5, $10 );
[4d51835]2221                }
2222        ;
[b87a5ed]2223
[84d58c5]2224trait_declaration_list:                                                                 // CFA
[4040425]2225        trait_declaration
2226        | trait_declaration_list push trait_declaration
[4d51835]2227                { $$ = $1->appendList( $3 ); }
2228        ;
[b87a5ed]2229
[84d58c5]2230trait_declaration:                                                                              // CFA
[c0aa336]2231        cfa_trait_declaring_list pop ';'
[4040425]2232        | trait_declaring_list pop ';'
[4d51835]2233        ;
[b87a5ed]2234
[c0aa336]2235cfa_trait_declaring_list:                                                               // CFA
2236        cfa_variable_specifier
[4d51835]2237                {
2238                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2239                        $$ = $1;
2240                }
[c0aa336]2241        | cfa_function_specifier
[4d51835]2242                {
2243                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2244                        $$ = $1;
2245                }
[c0aa336]2246        | cfa_trait_declaring_list pop ',' push identifier_or_type_name
[4d51835]2247                {
[de62360d]2248                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
[4d51835]2249                        $$ = $1->appendList( $1->cloneType( $5 ) );
2250                }
2251        ;
[b87a5ed]2252
[4040425]2253trait_declaring_list:                                                                   // CFA
[4d51835]2254        type_specifier declarator
2255                {
2256                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2257                        $$ = $2->addType( $1 );
2258                }
[4040425]2259        | trait_declaring_list pop ',' push declarator
[4d51835]2260                {
2261                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2262                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2263                }
2264        ;
[51b7345]2265
[c11e31c]2266//***************************** EXTERNAL DEFINITIONS *****************************
[51b7345]2267
2268translation_unit:
[4d51835]2269        // empty
2270                {}                                                                                              // empty input file
2271        | external_definition_list
[a7741435]2272                { parseTree = parseTree ? parseTree->appendList( $1 ) : $1;     }
[4d51835]2273        ;
[51b7345]2274
2275external_definition_list:
[4d51835]2276        external_definition
2277        | external_definition_list push external_definition
[a7741435]2278                { $$ = $1 ? $1->appendList( $3 ) : $3; }
[4d51835]2279        ;
[51b7345]2280
2281external_definition_list_opt:
[4d51835]2282        // empty
[58dd019]2283                { $$ = nullptr; }
[4d51835]2284        | external_definition_list
2285        ;
[51b7345]2286
2287external_definition:
[4d51835]2288        declaration
2289        | external_function_definition
[e994912]2290        | ASM '(' string_literal ')' ';'                                        // GCC, global assembler statement
2291                {
2292                        $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asmstmt( false, $3, 0 ) ) );
2293                }
[c0aa336]2294        | EXTERN STRINGliteral                                                          // C++-style linkage specifier
[4d51835]2295                {
[3b8e52c]2296                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
[0fc9756b]2297                        linkage = LinkageSpec::linkageUpdate( linkage, $2 );
[4d51835]2298                }
[c0aa336]2299          '{' external_definition_list_opt '}'
[4d51835]2300                {
2301                        linkage = linkageStack.top();
2302                        linkageStack.pop();
2303                        $$ = $5;
2304                }
[c0aa336]2305        | EXTENSION external_definition                                         // GCC, multiple __extension__ allowed, meaning unknown
2306                {
2307                        distExt( $2 );                                                          // mark all fields in list
[8e9cbb2]2308                        $$ = $2;
2309                }
[4d51835]2310        ;
2311
2312external_function_definition:
2313        function_definition
[de62360d]2314                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]2315                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2316                // Parsing is possible because function_definition does not appear in the context of an expression (nested
2317                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2318                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
[4d51835]2319        | function_declarator compound_statement
2320                {
2321                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2322                        typedefTable.leaveScope();
2323                        $$ = $1->addFunctionBody( $2 );
2324                }
[4cc585b]2325        | KR_function_declarator KR_declaration_list_opt compound_statement
[4d51835]2326                {
2327                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2328                        typedefTable.leaveScope();
[4cc585b]2329                        $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 );
[4d51835]2330                }
2331        ;
[51b7345]2332
[8b47e50]2333with_clause_opt:
2334        // empty
[5b2edbc]2335                { $$ = nullptr; }                                                               // FIX ME
[578e6037]2336        | WITH '(' tuple_expression_list ')'
[5b2edbc]2337                { $$ = nullptr; }                                                               // FIX ME
[8b47e50]2338        ;
2339
[51b7345]2340function_definition:
[8b47e50]2341        cfa_function_declaration with_clause_opt compound_statement     // CFA
[4d51835]2342                {
2343                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2344                        typedefTable.leaveScope();
[8b47e50]2345                        $$ = $1->addFunctionBody( $3 );
[4d51835]2346                }
[8b47e50]2347        | declaration_specifier function_declarator with_clause_opt compound_statement
[4d51835]2348                {
2349                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2350                        typedefTable.leaveScope();
[8b47e50]2351                        $$ = $2->addFunctionBody( $4 )->addType( $1 );
[4d51835]2352                }
[8b47e50]2353        | type_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2354                {
2355                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2356                        typedefTable.leaveScope();
[8b47e50]2357                        $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
[4d51835]2358                }
[8b47e50]2359        | declaration_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2360                {
2361                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2362                        typedefTable.leaveScope();
[8b47e50]2363                        $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
[4d51835]2364                }
[8b47e50]2365        | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
[4d51835]2366                {
2367                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2368                        typedefTable.leaveScope();
[8b47e50]2369                        $$ = $3->addFunctionBody( $5 )->addQualifiers( $2 )->addQualifiers( $1 );
[4d51835]2370                }
2371
2372                // Old-style K&R function definition, OBSOLESCENT (see 4)
[4cc585b]2373        | declaration_specifier KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2374                {
2375                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2376                        typedefTable.leaveScope();
[4cc585b]2377                        $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addType( $1 );
[4d51835]2378                }
[4cc585b]2379        | type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2380                {
2381                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2382                        typedefTable.leaveScope();
[4cc585b]2383                        $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addQualifiers( $1 );
[4d51835]2384                }
2385
2386                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
[4cc585b]2387        | declaration_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2388                {
2389                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2390                        typedefTable.leaveScope();
[4cc585b]2391                        $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5 )->addQualifiers( $1 );
[4d51835]2392                }
[4cc585b]2393        | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement
[4d51835]2394                {
2395                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2396                        typedefTable.leaveScope();
[4cc585b]2397                        $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
[4d51835]2398                }
2399        ;
[51b7345]2400
2401declarator:
[4d51835]2402        variable_declarator
[c6b1105]2403        | variable_type_redeclarator
[4d51835]2404        | function_declarator
2405        ;
[51b7345]2406
2407subrange:
[4d51835]2408        constant_expression '~' constant_expression                     // CFA, integer subrange
[d1625f8]2409                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
[4d51835]2410        ;
[b87a5ed]2411
2412asm_name_opt:                                                                                   // GCC
[4d51835]2413        // empty
[58dd019]2414                { $$ = nullptr; }
2415        | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]2416                {
2417                        DeclarationNode * name = new DeclarationNode();
2418                        name->asmName = $3;
2419                        $$ = name->addQualifiers( $5 );
2420                }
[4d51835]2421        ;
[b87a5ed]2422
2423attribute_list_opt:                                                                             // GCC
[4d51835]2424        // empty
[58dd019]2425                { $$ = nullptr; }
[4d51835]2426        | attribute_list
2427        ;
[b87a5ed]2428
2429attribute_list:                                                                                 // GCC
[4d51835]2430        attribute
2431        | attribute_list attribute
[1db21619]2432                { $$ = $2->addQualifiers( $1 ); }
[4d51835]2433        ;
[b87a5ed]2434
2435attribute:                                                                                              // GCC
[44a81853]2436        ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2437                { $$ = $4; }
[4d51835]2438        ;
[b87a5ed]2439
[44a81853]2440attribute_name_list:                                                                    // GCC
2441        attribute_name
2442        | attribute_name_list ',' attribute_name
[c0aa336]2443                { $$ = $3->addQualifiers( $1 ); }
[4d51835]2444        ;
[b87a5ed]2445
[44a81853]2446attribute_name:                                                                                 // GCC
[4d51835]2447        // empty
[44a81853]2448                { $$ = nullptr; }
2449        | attr_name
2450                { $$ = DeclarationNode::newAttribute( $1 ); }
2451        | attr_name '(' argument_expression_list ')'
2452                { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]2453        ;
[b87a5ed]2454
[44a81853]2455attr_name:                                                                                              // GCC
2456        IDENTIFIER
[5b2edbc]2457        | quasi_keyword
[44a81853]2458        | TYPEDEFname
2459        | TYPEGENname
2460        | CONST
[9ff56e7]2461                { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
[4d51835]2462        ;
[51b7345]2463
[c11e31c]2464// ============================================================================
[de62360d]2465// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2466// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2467// in:
[c11e31c]2468//
[b87a5ed]2469//              int (*f())[10] { ... };
2470//              ... (*f())[3] += 1;             // definition mimics usage
[c11e31c]2471//
[de62360d]2472// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2473// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]2474// ============================================================================
2475
2476// ----------------------------------------------------------------------------
[de62360d]2477// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2478// to define a variable or function prototype, e.g.:
[c11e31c]2479//
[b87a5ed]2480//              valid declaration               invalid definition
2481//              -----------------               ------------------
[4d51835]2482//              int f;                                  int f {}
2483//              int *f;                                 int *f {}
[b87a5ed]2484//              int f[10];                              int f[10] {}
[4d51835]2485//              int (*f)(int);                  int (*f)(int) {}
[c11e31c]2486//
[de62360d]2487// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2488// variable_declarator and function_declarator.
[c11e31c]2489// ----------------------------------------------------------------------------
2490
[de62360d]2491// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2492// declaring an array of functions versus a pointer to an array of functions.
[51b7345]2493
2494variable_declarator:
[4d51835]2495        paren_identifier attribute_list_opt
[1db21619]2496                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2497        | variable_ptr
2498        | variable_array attribute_list_opt
[1db21619]2499                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2500        | variable_function attribute_list_opt
[1db21619]2501                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2502        ;
[51b7345]2503
2504paren_identifier:
[4d51835]2505        identifier
2506                {
[de62360d]2507                        typedefTable.setNextIdentifier( *$1 );
[4d51835]2508                        $$ = DeclarationNode::newName( $1 );
2509                }
2510        | '(' paren_identifier ')'                                                      // redundant parenthesis
2511                { $$ = $2; }
2512        ;
[51b7345]2513
2514variable_ptr:
[dd51906]2515        ptrref_operator variable_declarator
[ce8c12f]2516                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2517        | ptrref_operator type_qualifier_list variable_declarator
[ce8c12f]2518                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2519        | '(' variable_ptr ')' attribute_list_opt
2520                { $$ = $2->addQualifiers( $4 ); }                               // redundant parenthesis
[4d51835]2521        ;
[51b7345]2522
2523variable_array:
[4d51835]2524        paren_identifier array_dimension
2525                { $$ = $1->addArray( $2 ); }
2526        | '(' variable_ptr ')' array_dimension
2527                { $$ = $2->addArray( $4 ); }
2528        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2529                { $$ = $2->addArray( $4 ); }
2530        | '(' variable_array ')'                                                        // redundant parenthesis
2531                { $$ = $2; }
2532        ;
[51b7345]2533
2534variable_function:
[4d51835]2535        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2536                { $$ = $2->addParamList( $6 ); }
2537        | '(' variable_function ')'                                                     // redundant parenthesis
2538                { $$ = $2; }
2539        ;
[51b7345]2540
[c6b1105]2541// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2542// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2543// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b7345]2544
2545function_declarator:
[4d51835]2546        function_no_ptr attribute_list_opt
[1db21619]2547                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2548        | function_ptr
2549        | function_array attribute_list_opt
[1db21619]2550                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2551        ;
[51b7345]2552
2553function_no_ptr:
[4d51835]2554        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2555                { $$ = $1->addParamList( $4 ); }
2556        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2557                { $$ = $2->addParamList( $6 ); }
2558        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2559                { $$ = $2; }
2560        ;
[51b7345]2561
2562function_ptr:
[dd51906]2563        ptrref_operator function_declarator
[ce8c12f]2564                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2565        | ptrref_operator type_qualifier_list function_declarator
[ce8c12f]2566                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[4d51835]2567        | '(' function_ptr ')'
2568                { $$ = $2; }
2569        ;
[51b7345]2570
2571function_array:
[4d51835]2572        '(' function_ptr ')' array_dimension
2573                { $$ = $2->addArray( $4 ); }
2574        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2575                { $$ = $2->addArray( $4 ); }
2576        | '(' function_array ')'                                                        // redundant parenthesis
2577                { $$ = $2; }
2578        ;
[51b7345]2579
[c0aa336]2580// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2581//
2582//   f( a, b, c ) int a, *b, c[]; {}
2583//
2584// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2585// returning arrays and functions versus pointers to arrays and functions.
[51b7345]2586
[c0aa336]2587KR_function_declarator:
2588        KR_function_no_ptr
2589        | KR_function_ptr
2590        | KR_function_array
[4d51835]2591        ;
[51b7345]2592
[c0aa336]2593KR_function_no_ptr:
[4d51835]2594        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2595                { $$ = $1->addIdList( $3 ); }
[c0aa336]2596        | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2597                { $$ = $2->addParamList( $6 ); }
2598        | '(' KR_function_no_ptr ')'                                            // redundant parenthesis
[4d51835]2599                { $$ = $2; }
2600        ;
[51b7345]2601
[c0aa336]2602KR_function_ptr:
2603        ptrref_operator KR_function_declarator
[ce8c12f]2604                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]2605        | ptrref_operator type_qualifier_list KR_function_declarator
[ce8c12f]2606                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2607        | '(' KR_function_ptr ')'
[4d51835]2608                { $$ = $2; }
2609        ;
[51b7345]2610
[c0aa336]2611KR_function_array:
2612        '(' KR_function_ptr ')' array_dimension
[4d51835]2613                { $$ = $2->addArray( $4 ); }
[c0aa336]2614        | '(' KR_function_array ')' multi_array_dimension       // redundant parenthesis
[4d51835]2615                { $$ = $2->addArray( $4 ); }
[c0aa336]2616        | '(' KR_function_array ')'                                                     // redundant parenthesis
[4d51835]2617                { $$ = $2; }
2618        ;
[51b7345]2619
[2871210]2620// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]2621//
[b87a5ed]2622//              typedef int foo;
2623//              {
2624//                 int foo; // redefine typedef name in new scope
2625//              }
[c11e31c]2626//
[de62360d]2627// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2628// and functions versus pointers to arrays and functions.
[51b7345]2629
[c6b1105]2630variable_type_redeclarator:
[2871210]2631        paren_type attribute_list_opt
[1db21619]2632                { $$ = $1->addQualifiers( $2 ); }
[2871210]2633        | type_ptr
2634        | type_array attribute_list_opt
[1db21619]2635                { $$ = $1->addQualifiers( $2 ); }
[2871210]2636        | type_function attribute_list_opt
[1db21619]2637                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2638        ;
[51b7345]2639
[2871210]2640paren_type:
2641        typedef
2642        | '(' paren_type ')'
[4d51835]2643                { $$ = $2; }
2644        ;
[51b7345]2645
[2871210]2646type_ptr:
[c6b1105]2647        ptrref_operator variable_type_redeclarator
[ce8c12f]2648                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c6b1105]2649        | ptrref_operator type_qualifier_list variable_type_redeclarator
[ce8c12f]2650                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2651        | '(' type_ptr ')' attribute_list_opt
2652                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2653        ;
[51b7345]2654
[2871210]2655type_array:
2656        paren_type array_dimension
[4d51835]2657                { $$ = $1->addArray( $2 ); }
[2871210]2658        | '(' type_ptr ')' array_dimension
[4d51835]2659                { $$ = $2->addArray( $4 ); }
[2871210]2660        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
[4d51835]2661                { $$ = $2->addArray( $4 ); }
[2871210]2662        | '(' type_array ')'                                                            // redundant parenthesis
[4d51835]2663                { $$ = $2; }
2664        ;
[51b7345]2665
[2871210]2666type_function:
2667        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2668                { $$ = $1->addParamList( $4 ); }
[2871210]2669        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2670                { $$ = $2->addParamList( $6 ); }
[2871210]2671        | '(' type_function ')'                                                         // redundant parenthesis
[4d51835]2672                { $$ = $2; }
2673        ;
[51b7345]2674
[c0aa336]2675// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2676// typedef name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes
2677// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2678// pointers to arrays and functions.
[51b7345]2679
2680identifier_parameter_declarator:
[4d51835]2681        paren_identifier attribute_list_opt
[1db21619]2682                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2683        | identifier_parameter_ptr
2684        | identifier_parameter_array attribute_list_opt
[1db21619]2685                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2686        | identifier_parameter_function attribute_list_opt
[1db21619]2687                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2688        ;
[51b7345]2689
2690identifier_parameter_ptr:
[dd51906]2691        ptrref_operator identifier_parameter_declarator
[ce8c12f]2692                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2693        | ptrref_operator type_qualifier_list identifier_parameter_declarator
[ce8c12f]2694                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2695        | '(' identifier_parameter_ptr ')' attribute_list_opt
2696                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2697        ;
[51b7345]2698
2699identifier_parameter_array:
[4d51835]2700        paren_identifier array_parameter_dimension
2701                { $$ = $1->addArray( $2 ); }
2702        | '(' identifier_parameter_ptr ')' array_dimension
2703                { $$ = $2->addArray( $4 ); }
2704        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2705                { $$ = $2->addArray( $4 ); }
2706        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2707                { $$ = $2; }
2708        ;
[51b7345]2709
2710identifier_parameter_function:
[4d51835]2711        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2712                { $$ = $1->addParamList( $4 ); }
2713        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2714                { $$ = $2->addParamList( $6 ); }
2715        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2716                { $$ = $2; }
2717        ;
[b87a5ed]2718
[de62360d]2719// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2720// e.g.:
[c11e31c]2721//
[b87a5ed]2722//              typedef int foo;
2723//              int f( int foo ); // redefine typedef name in new scope
[c11e31c]2724//
[c0aa336]2725// and allows the C99 array options, which can only appear in a parameter list.
[51b7345]2726
[2871210]2727type_parameter_redeclarator:
[4d51835]2728        typedef attribute_list_opt
[1db21619]2729                { $$ = $1->addQualifiers( $2 ); }
[2871210]2730        | type_parameter_ptr
2731        | type_parameter_array attribute_list_opt
[1db21619]2732                { $$ = $1->addQualifiers( $2 ); }
[2871210]2733        | type_parameter_function attribute_list_opt
[1db21619]2734                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2735        ;
[51b7345]2736
2737typedef:
[4d51835]2738        TYPEDEFname
2739                {
[de62360d]2740                        typedefTable.setNextIdentifier( *$1 );
[4d51835]2741                        $$ = DeclarationNode::newName( $1 );
2742                }
[2871210]2743        | TYPEGENname
2744                {
2745                        typedefTable.setNextIdentifier( *$1 );
2746                        $$ = DeclarationNode::newName( $1 );
2747                }
[4d51835]2748        ;
[51b7345]2749
[2871210]2750type_parameter_ptr:
[dd51906]2751        ptrref_operator type_parameter_redeclarator
[ce8c12f]2752                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2753        | ptrref_operator type_qualifier_list type_parameter_redeclarator
[ce8c12f]2754                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2755        | '(' type_parameter_ptr ')' attribute_list_opt
2756                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2757        ;
[51b7345]2758
[2871210]2759type_parameter_array:
[4d51835]2760        typedef array_parameter_dimension
2761                { $$ = $1->addArray( $2 ); }
[2871210]2762        | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]2763                { $$ = $2->addArray( $4 ); }
2764        ;
[51b7345]2765
[2871210]2766type_parameter_function:
[4d51835]2767        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2768                { $$ = $1->addParamList( $4 ); }
[2871210]2769        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2770                { $$ = $2->addParamList( $6 ); }
2771        ;
[b87a5ed]2772
[de62360d]2773// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2774// which the type applies, e.g.:
[c11e31c]2775//
[b87a5ed]2776//              sizeof( int );
[c0aa336]2777//              sizeof( int * );
[b87a5ed]2778//              sizeof( int [10] );
[c0aa336]2779//              sizeof( int (*)() );
2780//              sizeof( int () );
[c11e31c]2781//
[de62360d]2782// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2783// and functions versus pointers to arrays and functions.
[51b7345]2784
2785abstract_declarator:
[4d51835]2786        abstract_ptr
2787        | abstract_array attribute_list_opt
[1db21619]2788                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2789        | abstract_function attribute_list_opt
[1db21619]2790                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2791        ;
[51b7345]2792
2793abstract_ptr:
[dd51906]2794        ptrref_operator
[ce8c12f]2795                { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]2796        | ptrref_operator type_qualifier_list
[ce8c12f]2797                { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2798        | ptrref_operator abstract_declarator
[ce8c12f]2799                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2800        | ptrref_operator type_qualifier_list abstract_declarator
[ce8c12f]2801                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2802        | '(' abstract_ptr ')' attribute_list_opt
2803                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2804        ;
[51b7345]2805
2806abstract_array:
[4d51835]2807        array_dimension
2808        | '(' abstract_ptr ')' array_dimension
2809                { $$ = $2->addArray( $4 ); }
2810        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2811                { $$ = $2->addArray( $4 ); }
2812        | '(' abstract_array ')'                                                        // redundant parenthesis
2813                { $$ = $2; }
2814        ;
[51b7345]2815
2816abstract_function:
[4d51835]2817        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
[2298f728]2818                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]2819        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2820                { $$ = $2->addParamList( $6 ); }
2821        | '(' abstract_function ')'                                                     // redundant parenthesis
2822                { $$ = $2; }
2823        ;
[51b7345]2824
2825array_dimension:
[4d51835]2826                // Only the first dimension can be empty.
[2871210]2827        '[' ']'
[4d51835]2828                { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]2829        | '[' ']' multi_array_dimension
2830                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[4d51835]2831        | multi_array_dimension
2832        ;
[51b7345]2833
2834multi_array_dimension:
[4d51835]2835        '[' push assignment_expression pop ']'
2836                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2837        | '[' push '*' pop ']'                                                          // C99
2838                { $$ = DeclarationNode::newVarArray( 0 ); }
2839        | multi_array_dimension '[' push assignment_expression pop ']'
2840                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2841        | multi_array_dimension '[' push '*' pop ']'            // C99
2842                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2843        ;
[51b7345]2844
[c11e31c]2845// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2846// identifier to which the type applies, e.g.:
2847//
[c0aa336]2848//              int f( int );                   // not handled here
2849//              int f( int * );                 // abstract function-prototype parameter; no parameter name specified
2850//              int f( int (*)() );             // abstract function-prototype parameter; no parameter name specified
[b87a5ed]2851//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
[c11e31c]2852//
[de62360d]2853// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[c0aa336]2854// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2855// special meaning of parenthesis around a typedef name:
2856//
2857//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2858//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2859//              not as redundant parentheses around the identifier."
2860//
2861// For example:
2862//
2863//              typedef float T;
2864//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
2865//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2866//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2867//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
2868//
2869// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2870// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2871// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2872// functions.
[51b7345]2873
2874abstract_parameter_declarator:
[4d51835]2875        abstract_parameter_ptr
2876        | abstract_parameter_array attribute_list_opt
[1db21619]2877                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2878        | abstract_parameter_function attribute_list_opt
[1db21619]2879                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2880        ;
[51b7345]2881
2882abstract_parameter_ptr:
[dd51906]2883        ptrref_operator
[ce8c12f]2884                { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
[dd51906]2885        | ptrref_operator type_qualifier_list
[ce8c12f]2886                { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2887        | ptrref_operator abstract_parameter_declarator
[ce8c12f]2888                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]2889        | ptrref_operator type_qualifier_list abstract_parameter_declarator
[ce8c12f]2890                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2891        | '(' abstract_parameter_ptr ')' attribute_list_opt
2892                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2893        ;
[51b7345]2894
2895abstract_parameter_array:
[4d51835]2896        array_parameter_dimension
2897        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2898                { $$ = $2->addArray( $4 ); }
2899        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2900                { $$ = $2->addArray( $4 ); }
2901        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2902                { $$ = $2; }
2903        ;
[51b7345]2904
2905abstract_parameter_function:
[4d51835]2906        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
[2298f728]2907                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
[4d51835]2908        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2909                { $$ = $2->addParamList( $6 ); }
2910        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2911                { $$ = $2; }
2912        ;
[51b7345]2913
2914array_parameter_dimension:
[4d51835]2915                // Only the first dimension can be empty or have qualifiers.
2916        array_parameter_1st_dimension
2917        | array_parameter_1st_dimension multi_array_dimension
2918                { $$ = $1->addArray( $2 ); }
2919        | multi_array_dimension
2920        ;
[51b7345]2921
[c11e31c]2922// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2923//
[de62360d]2924//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2925//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b7345]2926
2927array_parameter_1st_dimension:
[2871210]2928        '[' ']'
[4d51835]2929                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2930        // multi_array_dimension handles the '[' '*' ']' case
2931        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2932                { $$ = DeclarationNode::newVarArray( $3 ); }
2933        | '[' push type_qualifier_list pop ']'
2934                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2935        // multi_array_dimension handles the '[' assignment_expression ']' case
2936        | '[' push type_qualifier_list assignment_expression pop ']'
2937                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2938        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2939                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2940        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2941                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2942        ;
[b87a5ed]2943
[c0aa336]2944// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]2945//
[c0aa336]2946//              struct S {
2947//          int;
2948//          int *;
2949//          int [10];
2950//          int (*)();
2951//      };
[51b7345]2952
2953variable_abstract_declarator:
[4d51835]2954        variable_abstract_ptr
2955        | variable_abstract_array attribute_list_opt
[1db21619]2956                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2957        | variable_abstract_function attribute_list_opt
[1db21619]2958                { $$ = $1->addQualifiers( $2 ); }
[4d51835]2959        ;
[51b7345]2960
2961variable_abstract_ptr:
[dd51906]2962        ptrref_operator
[ce8c12f]2963                { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]2964        | ptrref_operator type_qualifier_list
[ce8c12f]2965                { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2966        | ptrref_operator variable_abstract_declarator
[ce8c12f]2967                { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2968        | ptrref_operator type_qualifier_list variable_abstract_declarator
[ce8c12f]2969                { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2970        | '(' variable_abstract_ptr ')' attribute_list_opt
2971                { $$ = $2->addQualifiers( $4 ); }
[4d51835]2972        ;
[51b7345]2973
2974variable_abstract_array:
[4d51835]2975        array_dimension
2976        | '(' variable_abstract_ptr ')' array_dimension
2977                { $$ = $2->addArray( $4 ); }
2978        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2979                { $$ = $2->addArray( $4 ); }
2980        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2981                { $$ = $2; }
2982        ;
[51b7345]2983
2984variable_abstract_function:
[4d51835]2985        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2986                { $$ = $2->addParamList( $6 ); }
2987        | '(' variable_abstract_function ')'                            // redundant parenthesis
2988                { $$ = $2; }
2989        ;
[b87a5ed]2990
[de62360d]2991// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2992// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]2993
[c0aa336]2994cfa_identifier_parameter_declarator_tuple:                              // CFA
2995        cfa_identifier_parameter_declarator_no_tuple
2996        | cfa_abstract_tuple
2997        | type_qualifier_list cfa_abstract_tuple
[4d51835]2998                { $$ = $2->addQualifiers( $1 ); }
2999        ;
[b87a5ed]3000
[c0aa336]3001cfa_identifier_parameter_declarator_no_tuple:                   // CFA
3002        cfa_identifier_parameter_ptr
3003        | cfa_identifier_parameter_array
[4d51835]3004        ;
[b87a5ed]3005
[c0aa336]3006cfa_identifier_parameter_ptr:                                                   // CFA
[d0ffed1]3007                // No SUE declaration in parameter list.
3008        ptrref_operator type_specifier_nobody
[ce8c12f]3009                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[d0ffed1]3010        | type_qualifier_list ptrref_operator type_specifier_nobody
[ce8c12f]3011                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3012        | ptrref_operator cfa_abstract_function
[ce8c12f]3013                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3014        | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3015                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3016        | ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3017                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3018        | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3019                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3020        ;
[b87a5ed]3021
[c0aa336]3022cfa_identifier_parameter_array:                                                 // CFA
[de62360d]3023                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3024                // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]3025        '[' ']' type_specifier_nobody
[2871210]3026                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3027        | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]3028                { $$ = $2->addNewArray( $1 ); }
[d0ffed1]3029        | '[' ']' multi_array_dimension type_specifier_nobody
[2871210]3030                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3031        | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]3032                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]3033        | multi_array_dimension type_specifier_nobody
[4d51835]3034                { $$ = $2->addNewArray( $1 ); }
[9059213]3035
[c0aa336]3036        | '[' ']' cfa_identifier_parameter_ptr
[2871210]3037                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3038        | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]3039                { $$ = $2->addNewArray( $1 ); }
[c0aa336]3040        | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[2871210]3041                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3042        | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3043                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]3044        | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3045                { $$ = $2->addNewArray( $1 ); }
3046        ;
[51b7345]3047
[c0aa336]3048cfa_array_parameter_1st_dimension:
[4d51835]3049        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
3050                { $$ = DeclarationNode::newVarArray( $3 ); }
3051        | '[' push type_qualifier_list assignment_expression pop ']'
3052                { $$ = DeclarationNode::newArray( $4, $3, false ); }
3053        | '[' push declaration_qualifier_list assignment_expression pop ']'
3054                // declaration_qualifier_list must be used because of shift/reduce conflict with
3055                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3056                // appear in this context.
3057                { $$ = DeclarationNode::newArray( $4, $3, true ); }
3058        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3059                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3060        ;
[b87a5ed]3061
[de62360d]3062// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3063// identifier to which the type applies, e.g.:
[c11e31c]3064//
[b87a5ed]3065//              [int] f( int );                         // abstract variable parameter; no parameter name specified
3066//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
[c11e31c]3067//
3068// These rules need LR(3):
3069//
[c0aa336]3070//              cfa_abstract_tuple identifier_or_type_name
3071//              '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
[c11e31c]3072//
3073// since a function return type can be syntactically identical to a tuple type:
3074//
[b87a5ed]3075//              [int, int] t;
3076//              [int, int] f( int );
[c11e31c]3077//
[2871210]3078// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]3079// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3080// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3081// duplicated when appearing with cfa_function_specifier.
[b87a5ed]3082
[c0aa336]3083cfa_abstract_declarator_tuple:                                                  // CFA
3084        cfa_abstract_tuple
3085        | type_qualifier_list cfa_abstract_tuple
[4d51835]3086                { $$ = $2->addQualifiers( $1 ); }
[c0aa336]3087        | cfa_abstract_declarator_no_tuple
[4d51835]3088        ;
[b87a5ed]3089
[c0aa336]3090cfa_abstract_declarator_no_tuple:                                               // CFA
3091        cfa_abstract_ptr
3092        | cfa_abstract_array
[4d51835]3093        ;
[b87a5ed]3094
[c0aa336]3095cfa_abstract_ptr:                                                                               // CFA
[dd51906]3096        ptrref_operator type_specifier
[ce8c12f]3097                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3098        | type_qualifier_list ptrref_operator type_specifier
[ce8c12f]3099                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3100        | ptrref_operator cfa_abstract_function
[ce8c12f]3101                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3102        | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3103                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3104        | ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3105                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3106        | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3107                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3108        ;
[b87a5ed]3109
[c0aa336]3110cfa_abstract_array:                                                                             // CFA
[de62360d]3111                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3112                // empty (void) function return type.
[2871210]3113        '[' ']' type_specifier
[2298f728]3114                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]3115        | '[' ']' multi_array_dimension type_specifier
[2298f728]3116                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]3117        | multi_array_dimension type_specifier
3118                { $$ = $2->addNewArray( $1 ); }
[c0aa336]3119        | '[' ']' cfa_abstract_ptr
[2298f728]3120                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3121        | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]3122                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3123        | multi_array_dimension cfa_abstract_ptr
[4d51835]3124                { $$ = $2->addNewArray( $1 ); }
3125        ;
[b87a5ed]3126
[c0aa336]3127cfa_abstract_tuple:                                                                             // CFA
3128        '[' push cfa_abstract_parameter_list pop ']'
[4d51835]3129                { $$ = DeclarationNode::newTuple( $3 ); }
3130        ;
[b87a5ed]3131
[c0aa336]3132cfa_abstract_function:                                                                  // CFA
3133//      '[' ']' '(' cfa_parameter_type_list_opt ')'
[1b29996]3134//              { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
[c0aa336]3135        cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3136                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[c0aa336]3137        | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
[2298f728]3138                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]3139        ;
[b87a5ed]3140
[de62360d]3141// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3142//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]3143//
[de62360d]3144// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3145//    the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]3146//
3147// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3148//    prototype-format parameter type declarators) is an obsolescent feature."
3149//
[de62360d]3150// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3151//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b7345]3152
[c11e31c]3153//************************* MISCELLANEOUS ********************************
[51b7345]3154
[b87a5ed]3155comma_opt:                                                                                              // redundant comma
[4d51835]3156        // empty
3157        | ','
3158        ;
[51b7345]3159
[994d080]3160default_initialize_opt:
[4d51835]3161        // empty
[58dd019]3162                { $$ = nullptr; }
[4d51835]3163        | '=' assignment_expression
3164                { $$ = $2; }
3165        ;
[51b7345]3166
3167%%
[c11e31c]3168// ----end of grammar----
[51b7345]3169
[c11e31c]3170// Local Variables: //
[b87a5ed]3171// mode: c++ //
[de62360d]3172// tab-width: 4 //
[c11e31c]3173// compile-command: "make install" //
3174// End: //
Note: See TracBrowser for help on using the repository browser.