source: src/Parser/parser.yy @ 9997fee

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

start anonymous type-variables and qualifier-distribution block

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