source: src/Parser/parser.yy @ 7de22b28

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 7de22b28 was 7de22b28, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Call TypedefTable::makeTypedef with leaf type for nested aggregate definitions

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