source: src/Parser/parser.yy @ 151c8db

Last change on this file since 151c8db was 151c8db, checked in by Peter A. Buhr <pabuhr@…>, 13 hours ago

parse keyword parameter and argument using '@' syntax

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