source: src/Parser/parser.yy @ bcb14b5

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

second attempt to at extended for-ctrl

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