source: src/Parser/parser.yy @ 3b69398

Last change on this file since 3b69398 was a8ced63, checked in by Peter A. Buhr <pabuhr@…>, 12 days ago

parse countof pseduo-function, update for-loop for enumeration

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