source: src/Parser/parser.yy @ 7a780ad

Last change on this file since 7a780ad was 7a780ad, checked in by Andrew Beach <ajbeach@…>, 3 months ago

Moved ast::BasicType::Kind to ast::BasicKind? in its own hearder. This is more consistent with other utility enums (although we still use this as a enum class) and reduces what some files need to include. Also did a upgrade in a comment with MAX_INTEGER_TYPE, it is now part of the enum.

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