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

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

documentations, support CFA declaration syntax in sizeof/alignof

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