source: src/Parser/parser.yy @ f1aeede

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since f1aeede was f1aeede, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

simplify for control parsing

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