source: src/Parser/parser.yy @ d55d7a6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d55d7a6 was d55d7a6, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Massive change to errors to enable warnings

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