source: src/Parser/parser.yy @ 1717b12

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 1717b12 was 9fb1367, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

make keywords catch, catchResume, finally quasi-keywords, add quasi-keyword recover as synonym for catch and fixup for catchResume

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