source: src/Parser/parser.yy @ 9ca94af

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 9ca94af was ca37445, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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