source: src/Parser/parser.yy @ 4520b77e

ADTast-experimentalpthread-emulation
Last change on this file since 4520b77e was 4520b77e, checked in by JiadaL <j82liang@…>, 19 months ago

Merge to Master Sept 19

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