source: src/Parser/parser.yy @ 1dbc8590

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

ignore extern "C" declarations in distribution block

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