source: src/Parser/parser.yy @ af9da5f

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 af9da5f was 1dbc8590, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

ignore extern "C" declarations in distribution block

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