source: src/Parser/parser.yy @ ecae5860

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

more push/pop updates

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