source: src/Parser/parser.yy @ 1571e4d

Last change on this file since 1571e4d was daa4cc1, checked in by Peter A. Buhr <pabuhr@…>, 10 days ago

temporary hack to allow parsing of default/named parameters/calls

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