source: src/Parser/parser.yy@ d56cc219

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since d56cc219 was ecae5860, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

more push/pop updates

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