source: src/Parser/parser.yy @ c29c342

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

improve error messages for useless declarations

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