source: src/Parser/parser.yy @ c0d00b6

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c0d00b6 was c38ae92, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix addQualifiers bug for multiple forall qualifiers, rebind forall from inline struct to routine

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