source: src/Parser/parser.yy @ c453ac4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c453ac4 was c453ac4, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Change parser to build FunctionDecl? withExpr rather than convert to a WithStmt?

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