source: src/Parser/parser.yy @ d2887f7

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 d2887f7 was 24c3b67, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

parse _Generic

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