source: src/Parser/parser.yy @ 679e644

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 679e644 was 679e644, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

extend plan 9, anonymous declarations, change token for default argument

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