source: src/Parser/parser.yy @ 089b39e1

Last change on this file since 089b39e1 was 089b39e1, checked in by Peter A. Buhr <pabuhr@…>, 5 days ago

clean up, prepare for downto enumerating

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