source: src/Parser/parser.yy @ 1d71208

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 1d71208 was 1d71208, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Implementing new-array subscripting syntax, in which a[x,y,z] now means the same as ax,y,z?.

This behaviour immediately replaces a syntax error that prohibits the -[-,-,-] syntax. The prior state showed that the C programs we compile don't use the C-compatible meaning of commas in subscripts.

This behaviour ultimately replaces the C-compatible interpretation in which a[x,y,z] means a[(x,y,z)] or, roughly, ({ x; y; a[z]; }).

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