source: src/Parser/parser.yy @ daacf82

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 daacf82 was d48e529, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Begin to introduce support for yylloc in the parser and extend CodeLocation? to include start column and end column/line number information

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