source: src/Parser/parser.yy @ e07caa2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since e07caa2 was e07caa2, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix extend plan 9, anonymous declarations

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