source: src/Parser/parser.yy @ cd28605

Last change on this file since cd28605 was cd28605, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

first attempt at generalizing attributes to statements

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