source: src/Parser/parser.yy @ 70056ed

ADTast-experimental
Last change on this file since 70056ed was 70056ed, checked in by Peter A. Buhr <pabuhr@…>, 13 months ago

clean up waituntil grammar

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