source: src/Parser/parser.yy @ 04bdc26

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

fix 0 and 1 for new for-control

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