source: src/Parser/parser.yy @ 55c7577

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

add explicit C compound-literal

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