source: src/Parser/parser.yy @ 3d2b7bc

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

formatting, documentation

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