source: src/Parser/parser.yy @ bd946e4

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 bd946e4 was 94b1022a, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

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