source: src/Parser/parser.yy@ f083335

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 stuck-waitfor-destruct with_gc
Last change on this file since f083335 was 401e61f, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add if control to while loop

  • Property mode set to 100644
File size: 124.0 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
[401e61f]12// Last Modified On : Mon Jun 4 22:22:04 2018
13// Update Count : 3492
[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:
[401e61f]1052 WHILE '(' push if_control_expression ')' statement pop
1053 { $$ = new StatementNode( build_while( $4, $6 ) ); }
[4d51835]1054 | DO statement WHILE '(' comma_expression ')' ';'
[401e61f]1055 { $$ = new StatementNode( build_do_while( $5, $2 ) ); }
[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
[b47b827]1333 | static_assert // C11
[4d51835]1334 ;
[b87a5ed]1335
[b9be000b]1336static_assert:
1337 STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
[f6e3e34]1338 { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
[b47b827]1339 | STATICASSERT '(' constant_expression ')' ';' // CFA
[6e3eaa57]1340 { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( *new string( "\"\"" ) ) ); }
[b9be000b]1341
[de62360d]1342// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1343// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1344// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1345// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1346// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
1347// block, but cannot be mixed within a specific declaration.
[c11e31c]1348//
[b87a5ed]1349// CFA C
1350// [10] int x; int x[10]; // array of 10 integers
1351// [10] * char y; char *y[10]; // array of 10 pointers to char
1352
[c0aa336]1353cfa_declaration: // CFA
[4cc585b]1354 cfa_variable_declaration
1355 | cfa_typedef_declaration
1356 | cfa_function_declaration
1357 | type_declaring_list
1358 | trait_specifier
[4d51835]1359 ;
[b87a5ed]1360
[c0aa336]1361cfa_variable_declaration: // CFA
1362 cfa_variable_specifier initializer_opt
[7fdb94e1]1363 { $$ = $1->addInitializer( $2 ); }
[c0aa336]1364 | declaration_qualifier_list cfa_variable_specifier initializer_opt
[de62360d]1365 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1366 // them as a type_qualifier cannot appear in that context.
[7fdb94e1]1367 { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
[c0aa336]1368 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
[7fdb94e1]1369 { $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) ); }
[4d51835]1370 ;
[b87a5ed]1371
[c0aa336]1372cfa_variable_specifier: // CFA
[de62360d]1373 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1374 // storage-class
[c0aa336]1375 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
[7fdb94e1]1376 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
[c0aa336]1377 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
[7fdb94e1]1378 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
[c0aa336]1379 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
[7fdb94e1]1380 { $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 ); }
[4d51835]1381 ;
[b87a5ed]1382
[c0aa336]1383cfa_function_declaration: // CFA
1384 cfa_function_specifier
1385 | type_qualifier_list cfa_function_specifier
[7fdb94e1]1386 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]1387 | declaration_qualifier_list cfa_function_specifier
[7fdb94e1]1388 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]1389 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
[7fdb94e1]1390 { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
[40de461]1391 | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
[4d51835]1392 {
[481115f]1393 // Append the return type at the start (left-hand-side) to each identifier in the list.
1394 DeclarationNode * ret = new DeclarationNode;
1395 ret->type = maybeClone( $1->type->base );
[40de461]1396 $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
[4d51835]1397 }
1398 ;
[b87a5ed]1399
[c0aa336]1400cfa_function_specifier: // CFA
[40de461]1401// '[' ']' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' // S/R conflict
[1b29996]1402// {
1403// $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1404// }
[40de461]1405// '[' ']' identifier '(' push cfa_parameter_ellipsis_list_opt pop ')'
[2871210]1406// {
1407// typedefTable.setNextIdentifier( *$5 );
1408// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1409// }
[40de461]1410// | '[' ']' TYPEDEFname '(' push cfa_parameter_ellipsis_list_opt pop ')'
[2871210]1411// {
1412// typedefTable.setNextIdentifier( *$5 );
1413// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1414// }
1415// | '[' ']' typegen_name
1416 // identifier_or_type_name must be broken apart because of the sequence:
[4d51835]1417 //
[40de461]1418 // '[' ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
[4d51835]1419 // '[' ']' type_specifier
1420 //
[2871210]1421 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1422 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
[40de461]1423 cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0aa336]1424 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
[c0a33d2]1425 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 ); }
[40de461]1426 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0a33d2]1427 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 ); }
[4d51835]1428 ;
[b87a5ed]1429
[c0aa336]1430cfa_function_return: // CFA
[c0a33d2]1431 '[' push cfa_parameter_list pop ']'
1432 { $$ = DeclarationNode::newTuple( $3 ); }
1433 | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
[b048dc3]1434 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
[c0a33d2]1435 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
[4d51835]1436 ;
[b87a5ed]1437
[c0aa336]1438cfa_typedef_declaration: // CFA
1439 TYPEDEF cfa_variable_specifier
[4d51835]1440 {
[ecae5860]1441 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "1" );
[4d51835]1442 $$ = $2->addTypedef();
1443 }
[c0aa336]1444 | TYPEDEF cfa_function_specifier
[4d51835]1445 {
[ecae5860]1446 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "2" );
[4d51835]1447 $$ = $2->addTypedef();
1448 }
[c0aa336]1449 | cfa_typedef_declaration pop ',' push no_attr_identifier
[4d51835]1450 {
[ecae5860]1451 typedefTable.addToEnclosingScope( *$5, TYPEDEFname, "3" );
[4d51835]1452 $$ = $1->appendList( $1->cloneType( $5 ) );
1453 }
1454 ;
[b87a5ed]1455
[de62360d]1456// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1457// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b73452]1458
1459typedef_declaration:
[4d51835]1460 TYPEDEF type_specifier declarator
1461 {
[ecae5860]1462 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "4" );
[4d51835]1463 $$ = $3->addType( $2 )->addTypedef();
1464 }
1465 | typedef_declaration pop ',' push declarator
1466 {
[ecae5860]1467 typedefTable.addToEnclosingScope( *$5->name, TYPEDEFname, "5" );
[4d51835]1468 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1469 }
[de62360d]1470 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[4d51835]1471 {
[ecae5860]1472 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "6" );
[4d51835]1473 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1474 }
1475 | type_specifier TYPEDEF declarator
1476 {
[ecae5860]1477 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "7" );
[4d51835]1478 $$ = $3->addType( $1 )->addTypedef();
1479 }
1480 | type_specifier TYPEDEF type_qualifier_list declarator
1481 {
[ecae5860]1482 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "8" );
[de62360d]1483 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
[4d51835]1484 }
1485 ;
[b87a5ed]1486
[721f17a]1487typedef_expression:
1488 // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
[4d51835]1489 TYPEDEF no_attr_identifier '=' assignment_expression
1490 {
[7fdb94e1]1491 // $$ = DeclarationNode::newName( 0 ); // unimplemented
1492 SemanticError( yylloc, "Typedef expression is currently unimplemented." ); $$ = nullptr;
[4d51835]1493 }
1494 | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1495 {
[7fdb94e1]1496 // $$ = DeclarationNode::newName( 0 ); // unimplemented
1497 SemanticError( yylloc, "Typedef expression is currently unimplemented." ); $$ = nullptr;
[4d51835]1498 }
1499 ;
[51b73452]1500
[c0aa336]1501//c_declaration:
1502// declaring_list pop ';'
1503// | typedef_declaration pop ';'
1504// | typedef_expression pop ';' // GCC, naming expression type
1505// | sue_declaration_specifier pop ';'
1506// ;
1507//
1508//declaring_list:
1509// // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1510// // storage-class
1511// declarator asm_name_opt initializer_opt
1512// {
[2f0a0678]1513// typedefTable.addToEnclosingScope( IDENTIFIER );
[c0aa336]1514// $$ = ( $2->addType( $1 ))->addAsmName( $3 )->addInitializer( $4 );
1515// }
1516// | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1517// {
[2f0a0678]1518// typedefTable.addToEnclosingScope( IDENTIFIER );
[c0aa336]1519// $$ = $1->appendList( $1->cloneBaseType( $4->addAsmName( $5 )->addInitializer( $6 ) ) );
1520// }
1521// ;
1522
1523c_declaration:
[4cc585b]1524 declaration_specifier declaring_list
[7fdb94e1]1525 { $$ = distAttr( $1, $2 ); }
[4cc585b]1526 | typedef_declaration
1527 | typedef_expression // GCC, naming expression type
1528 | sue_declaration_specifier
[4d51835]1529 ;
[51b73452]1530
1531declaring_list:
[de62360d]1532 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1533 // storage-class
[c0aa336]1534 declarator asm_name_opt initializer_opt
[7fdb94e1]1535 { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
[4d51835]1536 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
[7fdb94e1]1537 { $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
[4d51835]1538 ;
[b87a5ed]1539
1540declaration_specifier: // type specifier + storage class
[4d51835]1541 basic_declaration_specifier
1542 | sue_declaration_specifier
[84d58c5]1543 | type_declaration_specifier
[4d51835]1544 ;
[b87a5ed]1545
[d0ffed1]1546declaration_specifier_nobody: // type specifier + storage class - {...}
1547 // Preclude SUE declarations in restricted scopes:
1548 //
1549 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1550 //
1551 // because it is impossible to call f due to name equivalence.
1552 basic_declaration_specifier
1553 | sue_declaration_specifier_nobody
[84d58c5]1554 | type_declaration_specifier
[d0ffed1]1555 ;
1556
1557type_specifier: // type specifier
[4d51835]1558 basic_type_specifier
1559 | sue_type_specifier
[84d58c5]1560 | type_type_specifier
[4d51835]1561 ;
[b87a5ed]1562
[d0ffed1]1563type_specifier_nobody: // type specifier - {...}
1564 // Preclude SUE declarations in restricted scopes:
1565 //
1566 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1567 //
1568 // because it is impossible to call f due to name equivalence.
1569 basic_type_specifier
1570 | sue_type_specifier_nobody
[84d58c5]1571 | type_type_specifier
[d0ffed1]1572 ;
1573
[b87a5ed]1574type_qualifier_list_opt: // GCC, used in asm_statement
[4d51835]1575 // empty
[58dd019]1576 { $$ = nullptr; }
[4d51835]1577 | type_qualifier_list
1578 ;
[51b73452]1579
1580type_qualifier_list:
[de62360d]1581 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]1582 //
[de62360d]1583 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1584 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1585 // appeared only once.
[4d51835]1586 type_qualifier
1587 | type_qualifier_list type_qualifier
1588 { $$ = $1->addQualifiers( $2 ); }
1589 ;
[51b73452]1590
1591type_qualifier:
[4d51835]1592 type_qualifier_name
1593 | attribute
1594 ;
[51b73452]1595
1596type_qualifier_name:
[4d51835]1597 CONST
[738e304]1598 { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
[4d51835]1599 | RESTRICT
[738e304]1600 { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
[4d51835]1601 | VOLATILE
[738e304]1602 { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
[4d51835]1603 | ATOMIC
[738e304]1604 { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
[a16a7ec]1605 | forall
1606 ;
1607
1608forall:
[35718a9]1609 FORALL '(' type_parameter_list ')' // CFA
1610 { $$ = DeclarationNode::newForall( $3 ); }
[4d51835]1611 ;
[51b73452]1612
1613declaration_qualifier_list:
[4d51835]1614 storage_class_list
[de62360d]1615 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
[4d51835]1616 { $$ = $1->addQualifiers( $2 ); }
1617 | declaration_qualifier_list type_qualifier_list storage_class_list
1618 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1619 ;
[51b73452]1620
1621storage_class_list:
[de62360d]1622 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1623 // only one of each is specified, except for inline, which can appear with the others.
[4d51835]1624 //
[de62360d]1625 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1626 // specifiers in a declaration.
[4d51835]1627 storage_class
1628 | storage_class_list storage_class
1629 { $$ = $1->addQualifiers( $2 ); }
1630 ;
[51b73452]1631
1632storage_class:
[4d51835]1633 EXTERN
[68fe077a]1634 { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
[4d51835]1635 | STATIC
[68fe077a]1636 { $$ = DeclarationNode::newStorageClass( Type::Static ); }
[4d51835]1637 | AUTO
[68fe077a]1638 { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
[4d51835]1639 | REGISTER
[68fe077a]1640 { $$ = DeclarationNode::newStorageClass( Type::Register ); }
[dd020c0]1641 | THREADLOCAL // C11
[68fe077a]1642 { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
[dd020c0]1643 // Put function specifiers here to simplify parsing rules, but separate them semantically.
[4d51835]1644 | INLINE // C99
[ddfd945]1645 { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
[4d51835]1646 | FORTRAN // C99
[ddfd945]1647 { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
[68cd1ce]1648 | NORETURN // C11
[ddfd945]1649 { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
[4d51835]1650 ;
[51b73452]1651
1652basic_type_name:
[201aeb9]1653 VOID
[4d51835]1654 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1655 | BOOL // C99
1656 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
[201aeb9]1657 | CHAR
1658 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1659 | INT
1660 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1661 | INT128
1662 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
1663 | FLOAT
1664 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1665 | FLOAT80
1666 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float80 ); }
1667 | FLOAT128
1668 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float128 ); }
1669 | DOUBLE
1670 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
[4d51835]1671 | COMPLEX // C99
[5b639ee]1672 { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
[4d51835]1673 | IMAGINARY // C99
[5b639ee]1674 { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
[201aeb9]1675 | SIGNED
1676 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
1677 | UNSIGNED
1678 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
1679 | SHORT
1680 { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
1681 | LONG
1682 { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
[72457b6]1683 | VALIST // GCC, __builtin_va_list
1684 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
[4d51835]1685 ;
[51b73452]1686
1687basic_declaration_specifier:
[4d51835]1688 // A semantic check is necessary for conflicting storage classes.
1689 basic_type_specifier
1690 | declaration_qualifier_list basic_type_specifier
1691 { $$ = $2->addQualifiers( $1 ); }
1692 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1693 { $$ = $1->addQualifiers( $2 ); }
1694 | basic_declaration_specifier storage_class type_qualifier_list
1695 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1696 | basic_declaration_specifier storage_class basic_type_specifier
1697 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1698 ;
[51b73452]1699
1700basic_type_specifier:
[84d58c5]1701 direct_type
[f38e7d7]1702 // Cannot have type modifiers, e.g., short, long, etc.
[84d58c5]1703 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
[4d51835]1704 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1705 ;
[51b73452]1706
[84d58c5]1707direct_type:
[4d51835]1708 basic_type_name
1709 | type_qualifier_list basic_type_name
1710 { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1711 | direct_type type_qualifier
[4d51835]1712 { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1713 | direct_type basic_type_name
[4d51835]1714 { $$ = $1->addType( $2 ); }
1715 ;
[51b73452]1716
[84d58c5]1717indirect_type:
1718 TYPEOF '(' type ')' // GCC: typeof(x) y;
[4d51835]1719 { $$ = $3; }
1720 | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
1721 { $$ = DeclarationNode::newTypeof( $3 ); }
[84d58c5]1722 | ATTR_TYPEGENname '(' type ')' // CFA: e.g., @type(x) y;
[4d51835]1723 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1724 | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
1725 { $$ = DeclarationNode::newAttr( $1, $3 ); }
[f38e7d7]1726 | ZERO_T // CFA
1727 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
1728 | ONE_T // CFA
1729 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
[4d51835]1730 ;
[51b73452]1731
[d0ffed1]1732sue_declaration_specifier: // struct, union, enum + storage class + type specifier
[4d51835]1733 sue_type_specifier
1734 | declaration_qualifier_list sue_type_specifier
1735 { $$ = $2->addQualifiers( $1 ); }
1736 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1737 { $$ = $1->addQualifiers( $2 ); }
1738 | sue_declaration_specifier storage_class type_qualifier_list
1739 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1740 ;
[51b73452]1741
[d0ffed1]1742sue_type_specifier: // struct, union, enum + type specifier
1743 elaborated_type
[fdca7c6]1744 | type_qualifier_list
1745 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
1746 elaborated_type
1747 { $$ = $3->addQualifiers( $1 ); }
[4d51835]1748 | sue_type_specifier type_qualifier
1749 { $$ = $1->addQualifiers( $2 ); }
1750 ;
[51b73452]1751
[d0ffed1]1752sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
1753 sue_type_specifier_nobody
1754 | declaration_qualifier_list sue_type_specifier_nobody
1755 { $$ = $2->addQualifiers( $1 ); }
1756 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
1757 { $$ = $1->addQualifiers( $2 ); }
1758 | sue_declaration_specifier_nobody storage_class type_qualifier_list
1759 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1760 ;
1761
1762sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
1763 elaborated_type_nobody
1764 | type_qualifier_list elaborated_type_nobody
1765 { $$ = $2->addQualifiers( $1 ); }
1766 | sue_type_specifier_nobody type_qualifier
1767 { $$ = $1->addQualifiers( $2 ); }
1768 ;
1769
[84d58c5]1770type_declaration_specifier:
1771 type_type_specifier
1772 | declaration_qualifier_list type_type_specifier
[4d51835]1773 { $$ = $2->addQualifiers( $1 ); }
[84d58c5]1774 | type_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
[4d51835]1775 { $$ = $1->addQualifiers( $2 ); }
[84d58c5]1776 | type_declaration_specifier storage_class type_qualifier_list
[4d51835]1777 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1778 ;
[b87a5ed]1779
[84d58c5]1780type_type_specifier: // typedef types
1781 type_name
1782 | type_qualifier_list type_name
1783 { $$ = $2->addQualifiers( $1 ); }
1784 | type_type_specifier type_qualifier
1785 { $$ = $1->addQualifiers( $2 ); }
1786 ;
1787
1788type_name:
[4d51835]1789 TYPEDEFname
1790 { $$ = DeclarationNode::newFromTypedef( $1 ); }
[84d58c5]1791 | '.' TYPEDEFname
1792 { $$ = DeclarationNode::newFromTypedef( $2 ); } // FIX ME
1793 | type_name '.' TYPEDEFname
1794 { $$ = DeclarationNode::newFromTypedef( $3 ); } // FIX ME
1795 | typegen_name
1796 | '.' typegen_name
1797 { $$ = $2; } // FIX ME
1798 | type_name '.' typegen_name
1799 { $$ = $3; } // FIX ME
1800 ;
1801
1802typegen_name: // CFA
[65d6de4]1803 TYPEGENname
1804 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1805 | TYPEGENname '(' ')'
[67cf18c]1806 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
1807 | TYPEGENname '(' type_list ')'
[84d58c5]1808 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
[4d51835]1809 ;
[51b73452]1810
[d0ffed1]1811elaborated_type: // struct, union, enum
[c0aa336]1812 aggregate_type
1813 | enum_type
[4d51835]1814 ;
[51b73452]1815
[d0ffed1]1816elaborated_type_nobody: // struct, union, enum - {...}
1817 aggregate_type_nobody
1818 | enum_type_nobody
1819 ;
1820
1821aggregate_type: // struct, union
[7fdb94e1]1822 aggregate_key attribute_list_opt '{' field_declaration_list_opt '}'
[6f95000]1823 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), nullptr, $4, true )->addQualifiers( $2 ); }
[c0aa336]1824 | aggregate_key attribute_list_opt no_attr_identifier_or_type_name
[fdca7c6]1825 {
1826 typedefTable.makeTypedef( *$3 ); // create typedef
[2f0a0678]1827 if ( forall ) typedefTable.changeKind( *$3, TYPEGENname ); // possibly update
[fdca7c6]1828 forall = false; // reset
1829 }
[7fdb94e1]1830 '{' field_declaration_list_opt '}'
[c0aa336]1831 { $$ = DeclarationNode::newAggregate( $1, $3, nullptr, $6, true )->addQualifiers( $2 ); }
[7fdb94e1]1832 | aggregate_key attribute_list_opt '(' type_list ')' '{' field_declaration_list_opt '}' // CFA
[6f95000]1833 { $$ = DeclarationNode::newAggregate( $1, new string( DeclarationNode::anonymous.newName() ), $4, $7, false )->addQualifiers( $2 ); }
[d0ffed1]1834 | aggregate_type_nobody
1835 ;
1836
1837aggregate_type_nobody: // struct, union - {...}
[84d58c5]1838 aggregate_key attribute_list_opt no_attr_identifier
1839 {
1840 typedefTable.makeTypedef( *$3 );
[2f0a0678]1841 if ( forall ) typedefTable.changeKind( *$3, TYPEGENname ); // possibly update
[9997fee]1842 forall = false; // reset
[84d58c5]1843 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1844 }
1845 | aggregate_key attribute_list_opt TYPEDEFname
[d0ffed1]1846 {
1847 typedefTable.makeTypedef( *$3 );
1848 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
1849 }
[84d58c5]1850 | aggregate_key attribute_list_opt typegen_name // CFA
[65d6de4]1851 {
1852 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
1853 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
1854 // delete newFromTypeGen.
1855 $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
1856 $3->type->symbolic.name = nullptr;
1857 $3->type->symbolic.actuals = nullptr;
1858 delete $3;
1859 }
[4d51835]1860 ;
[51b73452]1861
1862aggregate_key:
[c0aa336]1863 STRUCT
[4d51835]1864 { $$ = DeclarationNode::Struct; }
[c0aa336]1865 | UNION
[4d51835]1866 { $$ = DeclarationNode::Union; }
[c27fb59]1867 | EXCEPTION
1868 { $$ = DeclarationNode::Exception; }
[d3bc0ad]1869 | COROUTINE
[409433da]1870 { $$ = DeclarationNode::Coroutine; }
[d3bc0ad]1871 | MONITOR
[409433da]1872 { $$ = DeclarationNode::Monitor; }
[d3bc0ad]1873 | THREAD
[409433da]1874 { $$ = DeclarationNode::Thread; }
[4d51835]1875 ;
[51b73452]1876
[7fdb94e1]1877field_declaration_list_opt:
[5d125e4]1878 // empty
[58dd019]1879 { $$ = nullptr; }
[7fdb94e1]1880 | field_declaration_list_opt field_declaration
[a7741435]1881 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]1882 ;
[51b73452]1883
1884field_declaration:
[46fa473]1885 type_specifier field_declaring_list ';'
1886 { $$ = distAttr( $1, $2 ); }
[c0aa336]1887 | EXTENSION type_specifier field_declaring_list ';' // GCC
[46fa473]1888 { distExt( $3 ); $$ = distAttr( $2, $3 ); } // mark all fields in list
1889 | typedef_declaration ';' // CFA
1890 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
1891 | cfa_field_declaring_list ';' // CFA, new style field declaration
1892 | EXTENSION cfa_field_declaring_list ';' // GCC
1893 { distExt( $2 ); $$ = $2; } // mark all fields in list
1894 | cfa_typedef_declaration ';' // CFA
1895 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }
[b47b827]1896 | static_assert // C11
[4d51835]1897 ;
[b87a5ed]1898
[c0aa336]1899cfa_field_declaring_list: // CFA, new style field declaration
1900 cfa_abstract_declarator_tuple // CFA, no field name
1901 | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
[4d51835]1902 { $$ = $1->addName( $2 ); }
[c0aa336]1903 | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
[4d51835]1904 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
[c0aa336]1905 | cfa_field_declaring_list ',' // CFA, no field name
[4d51835]1906 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1907 ;
[51b73452]1908
1909field_declaring_list:
[7fdb94e1]1910 field_declarator_opt
1911 | field_declaring_list ',' attribute_list_opt field_declarator_opt
[c0aa336]1912 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
[4d51835]1913 ;
[51b73452]1914
[7fdb94e1]1915field_declarator_opt:
[4d51835]1916 // empty
1917 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
[6f95000]1918 // '@'
1919 // { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
[4d51835]1920 | bit_subrange_size // no field name
1921 { $$ = DeclarationNode::newBitfield( $1 ); }
1922 | variable_declarator bit_subrange_size_opt
1923 // A semantic check is required to ensure bit_subrange only appears on base type int.
1924 { $$ = $1->addBitfield( $2 ); }
[c6b1105]1925 | variable_type_redeclarator bit_subrange_size_opt
[4d51835]1926 // A semantic check is required to ensure bit_subrange only appears on base type int.
1927 { $$ = $1->addBitfield( $2 ); }
1928 | variable_abstract_declarator // CFA, no field name
1929 ;
[51b73452]1930
1931bit_subrange_size_opt:
[4d51835]1932 // empty
[58dd019]1933 { $$ = nullptr; }
[4d51835]1934 | bit_subrange_size
1935 ;
[51b73452]1936
1937bit_subrange_size:
[4d51835]1938 ':' constant_expression
1939 { $$ = $2; }
1940 ;
[51b73452]1941
[d0ffed1]1942enum_type: // enum
[c0aa336]1943 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
[6f95000]1944 { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
[c0aa336]1945 | ENUM attribute_list_opt no_attr_identifier_or_type_name
[d0ffed1]1946 { typedefTable.makeTypedef( *$3 ); }
1947 '{' enumerator_list comma_opt '}'
1948 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1949 | enum_type_nobody
1950 ;
1951
1952enum_type_nobody: // enum - {...}
1953 ENUM attribute_list_opt no_attr_identifier_or_type_name
[45161b4d]1954 {
[c0aa336]1955 typedefTable.makeTypedef( *$3 );
[ca1a547]1956 $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
[45161b4d]1957 }
[4d51835]1958 ;
[51b73452]1959
1960enumerator_list:
[2871210]1961 no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1962 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
[2871210]1963 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
[4d51835]1964 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1965 ;
[51b73452]1966
1967enumerator_value_opt:
[4d51835]1968 // empty
[58dd019]1969 { $$ = nullptr; }
[4d51835]1970 | '=' constant_expression
1971 { $$ = $2; }
1972 ;
[51b73452]1973
[40de461]1974cfa_parameter_ellipsis_list_opt: // CFA, abstract + real
[4d51835]1975 // empty
[2a8427c6]1976 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1977 | ELLIPSIS
[58dd019]1978 { $$ = nullptr; }
[2a8427c6]1979 | cfa_abstract_parameter_list
[c0aa336]1980 | cfa_parameter_list
[c0a33d2]1981 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
1982 { $$ = $1->appendList( $5 ); }
1983 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
[4d51835]1984 { $$ = $1->addVarArgs(); }
[c0a33d2]1985 | cfa_parameter_list pop ',' push ELLIPSIS
[4d51835]1986 { $$ = $1->addVarArgs(); }
1987 ;
[b87a5ed]1988
[c0aa336]1989cfa_parameter_list: // CFA
1990 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1991 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1992 cfa_parameter_declaration
[c0a33d2]1993 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1994 { $$ = $1->appendList( $5 ); }
1995 | cfa_parameter_list pop ',' push cfa_parameter_declaration
1996 { $$ = $1->appendList( $5 ); }
1997 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1998 { $$ = $1->appendList( $5 )->appendList( $9 ); }
[4d51835]1999 ;
[b87a5ed]2000
[c0aa336]2001cfa_abstract_parameter_list: // CFA, new & old style abstract
2002 cfa_abstract_parameter_declaration
[c0a33d2]2003 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2004 { $$ = $1->appendList( $5 ); }
[4d51835]2005 ;
[51b73452]2006
2007parameter_type_list_opt:
[4d51835]2008 // empty
[58dd019]2009 { $$ = nullptr; }
[2a8427c6]2010 | ELLIPSIS
2011 { $$ = nullptr; }
2012 | parameter_list
[4d51835]2013 | parameter_list pop ',' push ELLIPSIS
2014 { $$ = $1->addVarArgs(); }
2015 ;
[b87a5ed]2016
2017parameter_list: // abstract + real
[4d51835]2018 abstract_parameter_declaration
2019 | parameter_declaration
2020 | parameter_list pop ',' push abstract_parameter_declaration
2021 { $$ = $1->appendList( $5 ); }
2022 | parameter_list pop ',' push parameter_declaration
2023 { $$ = $1->appendList( $5 ); }
2024 ;
[51b73452]2025
[de62360d]2026// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
[2871210]2027// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b73452]2028
[c0aa336]2029cfa_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2030 parameter_declaration
[994d080]2031 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initialize_opt
[4d51835]2032 { $$ = $1->addName( $2 ); }
[994d080]2033 | cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[c0aa336]2034 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
[4d51835]2035 { $$ = $1->addName( $2 ); }
[994d080]2036 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initialize_opt
[4d51835]2037 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
[c0aa336]2038 | cfa_function_specifier
[4d51835]2039 ;
[b87a5ed]2040
[c0aa336]2041cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
[4d51835]2042 abstract_parameter_declaration
[c0aa336]2043 | cfa_identifier_parameter_declarator_no_tuple
2044 | cfa_abstract_tuple
2045 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2046 | type_qualifier_list cfa_abstract_tuple
[4d51835]2047 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]2048 | cfa_abstract_function
[4d51835]2049 ;
[51b73452]2050
2051parameter_declaration:
[d0ffed1]2052 // No SUE declaration in parameter list.
[994d080]2053 declaration_specifier_nobody identifier_parameter_declarator default_initialize_opt
[7fdb94e1]2054 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[994d080]2055 | declaration_specifier_nobody type_parameter_redeclarator default_initialize_opt
[7fdb94e1]2056 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]2057 ;
[51b73452]2058
2059abstract_parameter_declaration:
[994d080]2060 declaration_specifier_nobody default_initialize_opt
[e7cc8cb]2061 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
[994d080]2062 | declaration_specifier_nobody abstract_parameter_declarator default_initialize_opt
[e7cc8cb]2063 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
[4d51835]2064 ;
[51b73452]2065
[c11e31c]2066// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]2067// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2068// identifiers. The ANSI-style parameter-list can redefine a typedef name.
[51b73452]2069
[b87a5ed]2070identifier_list: // K&R-style parameter list => no types
[4d51835]2071 no_attr_identifier
2072 { $$ = DeclarationNode::newName( $1 ); }
2073 | identifier_list ',' no_attr_identifier
2074 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2075 ;
[51b73452]2076
[2871210]2077identifier_or_type_name:
[4d51835]2078 identifier
2079 | TYPEDEFname
2080 | TYPEGENname
2081 ;
[51b73452]2082
[2871210]2083no_attr_identifier_or_type_name:
[4d51835]2084 no_attr_identifier
2085 | TYPEDEFname
[721f17a]2086 | TYPEGENname
[4d51835]2087 ;
[b87a5ed]2088
[84d58c5]2089type_no_function: // sizeof, alignof, cast (constructor)
[c0aa336]2090 cfa_abstract_declarator_tuple // CFA
[4d51835]2091 | type_specifier
[c0aa336]2092 | type_specifier abstract_declarator
[4d51835]2093 { $$ = $2->addType( $1 ); }
2094 ;
[b87a5ed]2095
[84d58c5]2096type: // typeof, assertion
2097 type_no_function
[c0aa336]2098 | cfa_abstract_function // CFA
[4d51835]2099 ;
[51b73452]2100
2101initializer_opt:
[4d51835]2102 // empty
[58dd019]2103 { $$ = nullptr; }
[2871210]2104 | '=' initializer
2105 { $$ = $2; }
[641c3d0]2106 | '=' VOID
[3ed994e]2107 { $$ = new InitializerNode( true ); }
[097e2b0]2108 | ATassign initializer
[974906e2]2109 { $$ = $2->set_maybeConstructed( false ); }
[4d51835]2110 ;
[51b73452]2111
2112initializer:
[de62360d]2113 assignment_expression { $$ = new InitializerNode( $1 ); }
[7fdb94e1]2114 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
[4d51835]2115 ;
[51b73452]2116
[7fdb94e1]2117initializer_list_opt:
[097e2b0]2118 // empty
[58dd019]2119 { $$ = nullptr; }
[097e2b0]2120 | initializer
[4d51835]2121 | designation initializer { $$ = $2->set_designators( $1 ); }
[7fdb94e1]2122 | initializer_list_opt ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2123 | initializer_list_opt ',' designation initializer
[1d4580a]2124 { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
[4d51835]2125 ;
[b87a5ed]2126
[de62360d]2127// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2128// '=' to separator the designator from the initializer value, as in:
[c11e31c]2129//
[b87a5ed]2130// int x[10] = { [1] = 3 };
[c11e31c]2131//
[de62360d]2132// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2133// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2134// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2135// shift/reduce conflicts
[51b73452]2136
2137designation:
[4d51835]2138 designator_list ':' // C99, CFA uses ":" instead of "="
[84d58c5]2139 | no_attr_identifier ':' // GCC, field name
[d1625f8]2140 { $$ = new ExpressionNode( build_varref( $1 ) ); }
[4d51835]2141 ;
[51b73452]2142
[b87a5ed]2143designator_list: // C99
[4d51835]2144 designator
[2871210]2145 | designator_list designator
[1d4580a]2146 { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
[d1625f8]2147 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
[4d51835]2148 ;
[51b73452]2149
2150designator:
[84d58c5]2151 '.' no_attr_identifier // C99, field name
[d1625f8]2152 { $$ = new ExpressionNode( build_varref( $2 ) ); }
[c0a33d2]2153 | '[' push assignment_expression pop ']' // C99, single array element
[de62360d]2154 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[d1625f8]2155 { $$ = $3; }
[c0a33d2]2156 | '[' push subrange pop ']' // CFA, multiple array elements
2157 { $$ = $3; }
2158 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2159 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $3 ), maybeMoveBuild< Expression >( $5 ) ) ); }
2160 | '.' '[' push field_list pop ']' // CFA, tuple field selector
2161 { $$ = $4; }
[4d51835]2162 ;
[51b73452]2163
[de62360d]2164// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2165// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]2166//
2167// Overloading: function, data, and operator identifiers may be overloaded.
2168//
[de62360d]2169// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2170// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2171// definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]2172//
[de62360d]2173// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2174// site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]2175//
2176// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]2177// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2178// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2179// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2180// on type arguments of polymorphic functions.
[c11e31c]2181
[b87a5ed]2182type_parameter_list: // CFA
[67cf18c]2183 type_parameter
2184 | type_parameter_list ',' type_parameter
[4d51835]2185 { $$ = $1->appendList( $3 ); }
2186 ;
[b87a5ed]2187
[84d58c5]2188type_initializer_opt: // CFA
2189 // empty
2190 { $$ = nullptr; }
2191 | '=' type
2192 { $$ = $2; }
2193 ;
2194
[b87a5ed]2195type_parameter: // CFA
[2871210]2196 type_class no_attr_identifier_or_type_name
[ecae5860]2197 { typedefTable.addToScope( *$2, TYPEDEFname, "9" ); }
[67cf18c]2198 type_initializer_opt assertion_list_opt
2199 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
[4d51835]2200 | type_specifier identifier_parameter_declarator
[9997fee]2201 | assertion_list
2202 { $$ = DeclarationNode::newTypeParam( DeclarationNode::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
[4d51835]2203 ;
[b87a5ed]2204
2205type_class: // CFA
[4040425]2206 OTYPE
[5b639ee]2207 { $$ = DeclarationNode::Otype; }
[4d51835]2208 | DTYPE
2209 { $$ = DeclarationNode::Dtype; }
[8f60f0b]2210 | FTYPE
2211 { $$ = DeclarationNode::Ftype; }
2212 | TTYPE
2213 { $$ = DeclarationNode::Ttype; }
[4d51835]2214 ;
[b87a5ed]2215
2216assertion_list_opt: // CFA
[4d51835]2217 // empty
[58dd019]2218 { $$ = nullptr; }
[9997fee]2219 | assertion_list
2220 ;
2221
2222assertion_list: // CFA
2223 assertion
2224 | assertion_list assertion
[a7741435]2225 { $$ = $1 ? $1->appendList( $2 ) : $2; }
[4d51835]2226 ;
[b87a5ed]2227
2228assertion: // CFA
[84d58c5]2229 '|' no_attr_identifier_or_type_name '(' type_list ')'
[7fdb94e1]2230 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
[13e8427]2231 | '|' '{' push trait_declaration_list pop '}'
[4d51835]2232 { $$ = $4; }
[35718a9]2233 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2234 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
[4d51835]2235 ;
[b87a5ed]2236
[84d58c5]2237type_list: // CFA
2238 type
[513e165]2239 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
[4d51835]2240 | assignment_expression
[84d58c5]2241 | type_list ',' type
[513e165]2242 { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) ) ); }
[84d58c5]2243 | type_list ',' assignment_expression
[1d4580a]2244 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
[4d51835]2245 ;
[b87a5ed]2246
2247type_declaring_list: // CFA
[4040425]2248 OTYPE type_declarator
[4d51835]2249 { $$ = $2; }
[4040425]2250 | storage_class_list OTYPE type_declarator
[4d51835]2251 { $$ = $3->addQualifiers( $1 ); }
2252 | type_declaring_list ',' type_declarator
[a7c90d4]2253 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
[4d51835]2254 ;
[b87a5ed]2255
2256type_declarator: // CFA
[4d51835]2257 type_declarator_name assertion_list_opt
2258 { $$ = $1->addAssertions( $2 ); }
[84d58c5]2259 | type_declarator_name assertion_list_opt '=' type
[4d51835]2260 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2261 ;
[b87a5ed]2262
2263type_declarator_name: // CFA
[2871210]2264 no_attr_identifier_or_type_name
[4d51835]2265 {
[ecae5860]2266 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
[4d51835]2267 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2268 }
[35718a9]2269 | no_attr_identifier_or_type_name '(' type_parameter_list ')'
[4d51835]2270 {
[ecae5860]2271 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
[35718a9]2272 $$ = DeclarationNode::newTypeDecl( $1, $3 );
[4d51835]2273 }
2274 ;
[b87a5ed]2275
[4040425]2276trait_specifier: // CFA
[35718a9]2277 TRAIT no_attr_identifier_or_type_name '(' type_parameter_list ')' '{' '}'
2278 { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
2279 | TRAIT no_attr_identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
2280 { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
[4d51835]2281 ;
[b87a5ed]2282
[84d58c5]2283trait_declaration_list: // CFA
[4040425]2284 trait_declaration
[13e8427]2285 | trait_declaration_list pop push trait_declaration
2286 { $$ = $1->appendList( $4 ); }
[4d51835]2287 ;
[b87a5ed]2288
[84d58c5]2289trait_declaration: // CFA
[13e8427]2290 cfa_trait_declaring_list ';'
2291 | trait_declaring_list ';'
[4d51835]2292 ;
[b87a5ed]2293
[c0aa336]2294cfa_trait_declaring_list: // CFA
2295 cfa_variable_specifier
2296 | cfa_function_specifier
2297 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
[7fdb94e1]2298 { $$ = $1->appendList( $1->cloneType( $5 ) ); }
[4d51835]2299 ;
[b87a5ed]2300
[4040425]2301trait_declaring_list: // CFA
[4d51835]2302 type_specifier declarator
[7fdb94e1]2303 { $$ = $2->addType( $1 ); }
[4040425]2304 | trait_declaring_list pop ',' push declarator
[7fdb94e1]2305 { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
[4d51835]2306 ;
[51b73452]2307
[c11e31c]2308//***************************** EXTERNAL DEFINITIONS *****************************
[51b73452]2309
2310translation_unit:
[4d51835]2311 // empty
2312 {} // empty input file
2313 | external_definition_list
[a7741435]2314 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
[4d51835]2315 ;
[51b73452]2316
2317external_definition_list:
[35718a9]2318 push external_definition pop
2319 { $$ = $2; }
[df22130]2320 | external_definition_list
2321 { forall = xxx; }
[35718a9]2322 push external_definition pop
[9997fee]2323 { $$ = $1 ? $1->appendList( $4 ) : $4; }
[4d51835]2324 ;
[51b73452]2325
[ecae5860]2326 // SKULLDUGGERY: Declarations in extern "X" and distribution need to be added to the current lexical scope.
2327 // However, external_definition_list creates a new scope around each external_definition, but the pop loses all the
2328 // types in the extern "X" and distribution at the end of the block. This version of external_definition_list does
2329
2330 // not do push/pop for declarations at the level of the extern "X" and distribution block. Any recursive uses of
2331 // external_definition_list within the extern "X" and distribution block correctly pushes/pops for that scope level.
2332external_definition_list_no_pop_push:
2333 external_definition
2334 | external_definition_list_no_pop_push
2335 { forall = xxx; }
2336 external_definition
2337 { $$ = $1 ? $1->appendList( $3 ) : $3; }
2338 ;
2339
[51b73452]2340external_definition_list_opt:
[4d51835]2341 // empty
[58dd019]2342 { $$ = nullptr; }
[ecae5860]2343 | external_definition_list_no_pop_push
[4d51835]2344 ;
[51b73452]2345
2346external_definition:
[4d51835]2347 declaration
2348 | external_function_definition
[ecae5860]2349 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2350 {
2351 distExt( $2 ); // mark all fields in list
2352 $$ = $2;
2353 }
[e994912]2354 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2355 {
[6d539f83]2356 $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) );
[e994912]2357 }
[c0aa336]2358 | EXTERN STRINGliteral // C++-style linkage specifier
[4d51835]2359 {
[3b8e52c]2360 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
[d55d7a6]2361 linkage = LinkageSpec::linkageUpdate( yylloc, linkage, $2 );
[4d51835]2362 }
[ecae5860]2363 '{' external_definition_list_opt '}'
[4d51835]2364 {
2365 linkage = linkageStack.top();
2366 linkageStack.pop();
[ecae5860]2367 $$ = $5;
[8e9cbb2]2368 }
[9997fee]2369 | type_qualifier_list
[c0a33d2]2370 { if ( $1->type->forall ) xxx = forall = true; } // remember generic type
[ecae5860]2371 '{' external_definition_list_opt '}' // CFA, namespace
[9997fee]2372 {
[35718a9]2373 for ( DeclarationNode * iter = $4; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
[1dbc8590]2374 if ( isMangled( iter->linkage ) ) { // ignore extern "C"
2375 iter->addQualifiers( $1->clone() );
2376 } // if
[9997fee]2377 } // for
2378 xxx = false;
2379 delete $1;
[35718a9]2380 $$ = $4;
[9997fee]2381 }
2382 | declaration_qualifier_list
[c0a33d2]2383 { if ( $1->type->forall ) xxx = forall = true; } // remember generic type
[ecae5860]2384 '{' external_definition_list_opt '}' // CFA, namespace
[9997fee]2385 {
[35718a9]2386 for ( DeclarationNode * iter = $4; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
[1dbc8590]2387 if ( isMangled( iter->linkage ) ) { // ignore extern "C"
2388 iter->addQualifiers( $1->clone() );
2389 } // if
[9997fee]2390 } // for
2391 xxx = false;
2392 delete $1;
[35718a9]2393 $$ = $4;
[9997fee]2394 }
2395 | declaration_qualifier_list type_qualifier_list
2396 {
[637dd9c]2397 // forall must be in the type_qualifier_list
2398 if ( $2->type->forall ) xxx = forall = true; // remember generic type
[9997fee]2399 }
[ecae5860]2400 '{' external_definition_list_opt '}' // CFA, namespace
[9997fee]2401 {
[35718a9]2402 for ( DeclarationNode * iter = $5; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
[1dbc8590]2403 if ( isMangled( iter->linkage ) && isMangled( $2->linkage ) ) { // ignore extern "C"
2404 iter->addQualifiers( $1->clone() );
2405 iter->addQualifiers( $2->clone() );
2406 } // if
[9997fee]2407 } // for
2408 xxx = false;
2409 delete $1;
2410 delete $2;
[35718a9]2411 $$ = $5;
[9997fee]2412 }
[4d51835]2413 ;
2414
2415external_function_definition:
2416 function_definition
[de62360d]2417 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
[c6b1105]2418 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2419 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2420 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2421 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
[4d51835]2422 | function_declarator compound_statement
[c0a33d2]2423 { $$ = $1->addFunctionBody( $2 ); }
[35718a9]2424 | KR_function_declarator KR_parameter_list_opt compound_statement
[c0a33d2]2425 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
[4d51835]2426 ;
[51b73452]2427
[8b47e50]2428with_clause_opt:
2429 // empty
[9997fee]2430 { $$ = nullptr; forall = false; }
[578e6037]2431 | WITH '(' tuple_expression_list ')'
[9997fee]2432 { $$ = $3; forall = false; }
[8b47e50]2433 ;
2434
[51b73452]2435function_definition:
[8b47e50]2436 cfa_function_declaration with_clause_opt compound_statement // CFA
[4d51835]2437 {
[481115f]2438 // Add the function body to the last identifier in the function definition list, i.e., foo3:
2439 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
[5fcba14]2440 $1->get_last()->addFunctionBody( $3, $2 );
[481115f]2441 $$ = $1;
[4d51835]2442 }
[8b47e50]2443 | declaration_specifier function_declarator with_clause_opt compound_statement
[4d51835]2444 {
[c38ae92]2445 rebindForall( $1, $2 );
[7fdb94e1]2446 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2447 }
2448 | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
2449 {
2450 rebindForall( $1, $2 );
[5fcba14]2451 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
[4d51835]2452 }
[a16a7ec]2453 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2454 | type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]2455 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]2456 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2457 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]2458 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
[a16a7ec]2459 // handles default int return type, OBSOLESCENT (see 1)
[8b47e50]2460 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
[c0a33d2]2461 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]2462
2463 // Old-style K&R function definition, OBSOLESCENT (see 4)
[35718a9]2464 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[4d51835]2465 {
[c38ae92]2466 rebindForall( $1, $2 );
[5fcba14]2467 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
[4d51835]2468 }
[a16a7ec]2469 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]2470 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]2471 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]2472 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]2473 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]2474 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
[a16a7ec]2475 // handles default int return type, OBSOLESCENT (see 1)
[35718a9]2476 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
[c0a33d2]2477 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
[4d51835]2478 ;
[51b73452]2479
2480declarator:
[4d51835]2481 variable_declarator
[c6b1105]2482 | variable_type_redeclarator
[4d51835]2483 | function_declarator
2484 ;
[51b73452]2485
2486subrange:
[4d51835]2487 constant_expression '~' constant_expression // CFA, integer subrange
[db70fe4]2488 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild< Expression >( $1 ), maybeMoveBuild< Expression >( $3 ) ) ); }
[4d51835]2489 ;
[b87a5ed]2490
2491asm_name_opt: // GCC
[4d51835]2492 // empty
[58dd019]2493 { $$ = nullptr; }
2494 | ASM '(' string_literal ')' attribute_list_opt
[c0aa336]2495 {
2496 DeclarationNode * name = new DeclarationNode();
2497 name->asmName = $3;
2498 $$ = name->addQualifiers( $5 );
2499 }
[4d51835]2500 ;
[b87a5ed]2501
2502attribute_list_opt: // GCC
[4d51835]2503 // empty
[58dd019]2504 { $$ = nullptr; }
[4d51835]2505 | attribute_list
2506 ;
[b87a5ed]2507
2508attribute_list: // GCC
[4d51835]2509 attribute
2510 | attribute_list attribute
[1db21619]2511 { $$ = $2->addQualifiers( $1 ); }
[4d51835]2512 ;
[b87a5ed]2513
2514attribute: // GCC
[44a81853]2515 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2516 { $$ = $4; }
[4d51835]2517 ;
[b87a5ed]2518
[44a81853]2519attribute_name_list: // GCC
2520 attribute_name
2521 | attribute_name_list ',' attribute_name
[c0aa336]2522 { $$ = $3->addQualifiers( $1 ); }
[4d51835]2523 ;
[b87a5ed]2524
[44a81853]2525attribute_name: // GCC
[4d51835]2526 // empty
[44a81853]2527 { $$ = nullptr; }
2528 | attr_name
2529 { $$ = DeclarationNode::newAttribute( $1 ); }
2530 | attr_name '(' argument_expression_list ')'
2531 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
[4d51835]2532 ;
[b87a5ed]2533
[44a81853]2534attr_name: // GCC
2535 IDENTIFIER
[5b2edbc]2536 | quasi_keyword
[44a81853]2537 | TYPEDEFname
2538 | TYPEGENname
[114014c]2539 | FALLTHROUGH
2540 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
[44a81853]2541 | CONST
[9ff56e7]2542 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
[4d51835]2543 ;
[51b73452]2544
[c11e31c]2545// ============================================================================
[de62360d]2546// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2547// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2548// in:
[c11e31c]2549//
[b87a5ed]2550// int (*f())[10] { ... };
2551// ... (*f())[3] += 1; // definition mimics usage
[c11e31c]2552//
[de62360d]2553// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2554// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
[c11e31c]2555// ============================================================================
2556
2557// ----------------------------------------------------------------------------
[de62360d]2558// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2559// to define a variable or function prototype, e.g.:
[c11e31c]2560//
[b87a5ed]2561// valid declaration invalid definition
2562// ----------------- ------------------
[4d51835]2563// int f; int f {}
2564// int *f; int *f {}
[b87a5ed]2565// int f[10]; int f[10] {}
[4d51835]2566// int (*f)(int); int (*f)(int) {}
[c11e31c]2567//
[de62360d]2568// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2569// variable_declarator and function_declarator.
[c11e31c]2570// ----------------------------------------------------------------------------
2571
[de62360d]2572// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2573// declaring an array of functions versus a pointer to an array of functions.
[51b73452]2574
2575variable_declarator:
[4d51835]2576 paren_identifier attribute_list_opt
[1db21619]2577 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2578 | variable_ptr
2579 | variable_array attribute_list_opt
[1db21619]2580 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2581 | variable_function attribute_list_opt
[1db21619]2582 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2583 ;
[51b73452]2584
2585paren_identifier:
[4d51835]2586 identifier
[7fdb94e1]2587 { $$ = DeclarationNode::newName( $1 ); }
[4d51835]2588 | '(' paren_identifier ')' // redundant parenthesis
2589 { $$ = $2; }
2590 ;
[51b73452]2591
2592variable_ptr:
[dd51906]2593 ptrref_operator variable_declarator
[ce8c12f]2594 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2595 | ptrref_operator type_qualifier_list variable_declarator
[ce8c12f]2596 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2597 | '(' variable_ptr ')' attribute_list_opt
2598 { $$ = $2->addQualifiers( $4 ); } // redundant parenthesis
[4d51835]2599 ;
[51b73452]2600
2601variable_array:
[4d51835]2602 paren_identifier array_dimension
2603 { $$ = $1->addArray( $2 ); }
2604 | '(' variable_ptr ')' array_dimension
2605 { $$ = $2->addArray( $4 ); }
2606 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
2607 { $$ = $2->addArray( $4 ); }
2608 | '(' variable_array ')' // redundant parenthesis
2609 { $$ = $2; }
2610 ;
[51b73452]2611
2612variable_function:
[4d51835]2613 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2614 { $$ = $2->addParamList( $6 ); }
2615 | '(' variable_function ')' // redundant parenthesis
2616 { $$ = $2; }
2617 ;
[51b73452]2618
[c6b1105]2619// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2620// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2621// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
[51b73452]2622
2623function_declarator:
[4d51835]2624 function_no_ptr attribute_list_opt
[1db21619]2625 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2626 | function_ptr
2627 | function_array attribute_list_opt
[1db21619]2628 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2629 ;
[51b73452]2630
2631function_no_ptr:
[4d51835]2632 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2633 { $$ = $1->addParamList( $4 ); }
2634 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2635 { $$ = $2->addParamList( $6 ); }
2636 | '(' function_no_ptr ')' // redundant parenthesis
2637 { $$ = $2; }
2638 ;
[51b73452]2639
2640function_ptr:
[dd51906]2641 ptrref_operator function_declarator
[ce8c12f]2642 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2643 | ptrref_operator type_qualifier_list function_declarator
[ce8c12f]2644 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[4d51835]2645 | '(' function_ptr ')'
2646 { $$ = $2; }
2647 ;
[51b73452]2648
2649function_array:
[4d51835]2650 '(' function_ptr ')' array_dimension
2651 { $$ = $2->addArray( $4 ); }
2652 | '(' function_array ')' multi_array_dimension // redundant parenthesis
2653 { $$ = $2->addArray( $4 ); }
2654 | '(' function_array ')' // redundant parenthesis
2655 { $$ = $2; }
2656 ;
[51b73452]2657
[c0aa336]2658// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2659//
2660// f( a, b, c ) int a, *b, c[]; {}
2661//
2662// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2663// returning arrays and functions versus pointers to arrays and functions.
[51b73452]2664
[c0aa336]2665KR_function_declarator:
2666 KR_function_no_ptr
2667 | KR_function_ptr
2668 | KR_function_array
[4d51835]2669 ;
[51b73452]2670
[c0aa336]2671KR_function_no_ptr:
[4d51835]2672 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
2673 { $$ = $1->addIdList( $3 ); }
[c0a33d2]2674 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2675 { $$ = $2->addParamList( $6 ); }
[c0aa336]2676 | '(' KR_function_no_ptr ')' // redundant parenthesis
[4d51835]2677 { $$ = $2; }
2678 ;
[51b73452]2679
[c0aa336]2680KR_function_ptr:
2681 ptrref_operator KR_function_declarator
[ce8c12f]2682 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]2683 | ptrref_operator type_qualifier_list KR_function_declarator
[ce8c12f]2684 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2685 | '(' KR_function_ptr ')'
[4d51835]2686 { $$ = $2; }
2687 ;
[51b73452]2688
[c0aa336]2689KR_function_array:
2690 '(' KR_function_ptr ')' array_dimension
[4d51835]2691 { $$ = $2->addArray( $4 ); }
[c0aa336]2692 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2693 { $$ = $2->addArray( $4 ); }
[c0aa336]2694 | '(' KR_function_array ')' // redundant parenthesis
[4d51835]2695 { $$ = $2; }
2696 ;
[51b73452]2697
[2871210]2698// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
[c11e31c]2699//
[b87a5ed]2700// typedef int foo;
2701// {
2702// int foo; // redefine typedef name in new scope
2703// }
[c11e31c]2704//
[de62360d]2705// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2706// and functions versus pointers to arrays and functions.
[51b73452]2707
[c6b1105]2708variable_type_redeclarator:
[2871210]2709 paren_type attribute_list_opt
[1db21619]2710 { $$ = $1->addQualifiers( $2 ); }
[2871210]2711 | type_ptr
2712 | type_array attribute_list_opt
[1db21619]2713 { $$ = $1->addQualifiers( $2 ); }
[2871210]2714 | type_function attribute_list_opt
[1db21619]2715 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2716 ;
[51b73452]2717
[2871210]2718paren_type:
2719 typedef
[2f0a0678]2720 // hide type name in enclosing scope by variable name
[ecae5860]2721 { typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" ); }
[2871210]2722 | '(' paren_type ')'
[4d51835]2723 { $$ = $2; }
2724 ;
[51b73452]2725
[2871210]2726type_ptr:
[c6b1105]2727 ptrref_operator variable_type_redeclarator
[ce8c12f]2728 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c6b1105]2729 | ptrref_operator type_qualifier_list variable_type_redeclarator
[ce8c12f]2730 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2731 | '(' type_ptr ')' attribute_list_opt
2732 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2733 ;
[51b73452]2734
[2871210]2735type_array:
2736 paren_type array_dimension
[4d51835]2737 { $$ = $1->addArray( $2 ); }
[2871210]2738 | '(' type_ptr ')' array_dimension
[4d51835]2739 { $$ = $2->addArray( $4 ); }
[2871210]2740 | '(' type_array ')' multi_array_dimension // redundant parenthesis
[4d51835]2741 { $$ = $2->addArray( $4 ); }
[2871210]2742 | '(' type_array ')' // redundant parenthesis
[4d51835]2743 { $$ = $2; }
2744 ;
[51b73452]2745
[2871210]2746type_function:
2747 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2748 { $$ = $1->addParamList( $4 ); }
[2871210]2749 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
[4d51835]2750 { $$ = $2->addParamList( $6 ); }
[2871210]2751 | '(' type_function ')' // redundant parenthesis
[4d51835]2752 { $$ = $2; }
2753 ;
[51b73452]2754
[c0aa336]2755// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2756// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
2757// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2758// pointers to arrays and functions.
[51b73452]2759
2760identifier_parameter_declarator:
[4d51835]2761 paren_identifier attribute_list_opt
[1db21619]2762 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]2763 | '&' MUTEX paren_identifier attribute_list_opt
2764 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[4d51835]2765 | identifier_parameter_ptr
2766 | identifier_parameter_array attribute_list_opt
[1db21619]2767 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2768 | identifier_parameter_function attribute_list_opt
[1db21619]2769 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2770 ;
[51b73452]2771
2772identifier_parameter_ptr:
[dd51906]2773 ptrref_operator identifier_parameter_declarator
[ce8c12f]2774 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2775 | ptrref_operator type_qualifier_list identifier_parameter_declarator
[ce8c12f]2776 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2777 | '(' identifier_parameter_ptr ')' attribute_list_opt
2778 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2779 ;
[51b73452]2780
2781identifier_parameter_array:
[4d51835]2782 paren_identifier array_parameter_dimension
2783 { $$ = $1->addArray( $2 ); }
2784 | '(' identifier_parameter_ptr ')' array_dimension
2785 { $$ = $2->addArray( $4 ); }
2786 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2787 { $$ = $2->addArray( $4 ); }
2788 | '(' identifier_parameter_array ')' // redundant parenthesis
2789 { $$ = $2; }
2790 ;
[51b73452]2791
2792identifier_parameter_function:
[c0a33d2]2793 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2794 { $$ = $1->addParamList( $4 ); }
2795 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2796 { $$ = $2->addParamList( $6 ); }
[4d51835]2797 | '(' identifier_parameter_function ')' // redundant parenthesis
2798 { $$ = $2; }
2799 ;
[b87a5ed]2800
[de62360d]2801// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2802// e.g.:
[c11e31c]2803//
[b87a5ed]2804// typedef int foo;
[114014c]2805// forall( otype T ) struct foo;
[b87a5ed]2806// int f( int foo ); // redefine typedef name in new scope
[c11e31c]2807//
[c0aa336]2808// and allows the C99 array options, which can only appear in a parameter list.
[51b73452]2809
[2871210]2810type_parameter_redeclarator:
[4d51835]2811 typedef attribute_list_opt
[1db21619]2812 { $$ = $1->addQualifiers( $2 ); }
[b6b3c42]2813 | '&' MUTEX typedef attribute_list_opt
2814 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
[2871210]2815 | type_parameter_ptr
2816 | type_parameter_array attribute_list_opt
[1db21619]2817 { $$ = $1->addQualifiers( $2 ); }
[2871210]2818 | type_parameter_function attribute_list_opt
[1db21619]2819 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2820 ;
[51b73452]2821
2822typedef:
[4d51835]2823 TYPEDEFname
[7fdb94e1]2824 { $$ = DeclarationNode::newName( $1 ); }
[2871210]2825 | TYPEGENname
[7fdb94e1]2826 { $$ = DeclarationNode::newName( $1 ); }
[4d51835]2827 ;
[51b73452]2828
[2871210]2829type_parameter_ptr:
[dd51906]2830 ptrref_operator type_parameter_redeclarator
[ce8c12f]2831 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2832 | ptrref_operator type_qualifier_list type_parameter_redeclarator
[ce8c12f]2833 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2834 | '(' type_parameter_ptr ')' attribute_list_opt
2835 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2836 ;
[51b73452]2837
[2871210]2838type_parameter_array:
[4d51835]2839 typedef array_parameter_dimension
2840 { $$ = $1->addArray( $2 ); }
[2871210]2841 | '(' type_parameter_ptr ')' array_parameter_dimension
[4d51835]2842 { $$ = $2->addArray( $4 ); }
2843 ;
[51b73452]2844
[2871210]2845type_parameter_function:
[c0a33d2]2846 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2847 { $$ = $1->addParamList( $4 ); }
2848 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2849 { $$ = $2->addParamList( $6 ); }
[4d51835]2850 ;
[b87a5ed]2851
[de62360d]2852// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2853// which the type applies, e.g.:
[c11e31c]2854//
[b87a5ed]2855// sizeof( int );
[c0aa336]2856// sizeof( int * );
[b87a5ed]2857// sizeof( int [10] );
[c0aa336]2858// sizeof( int (*)() );
2859// sizeof( int () );
[c11e31c]2860//
[de62360d]2861// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2862// and functions versus pointers to arrays and functions.
[51b73452]2863
2864abstract_declarator:
[4d51835]2865 abstract_ptr
2866 | abstract_array attribute_list_opt
[1db21619]2867 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2868 | abstract_function attribute_list_opt
[1db21619]2869 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2870 ;
[51b73452]2871
2872abstract_ptr:
[dd51906]2873 ptrref_operator
[ce8c12f]2874 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]2875 | ptrref_operator type_qualifier_list
[ce8c12f]2876 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2877 | ptrref_operator abstract_declarator
[ce8c12f]2878 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]2879 | ptrref_operator type_qualifier_list abstract_declarator
[ce8c12f]2880 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2881 | '(' abstract_ptr ')' attribute_list_opt
2882 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2883 ;
[51b73452]2884
2885abstract_array:
[4d51835]2886 array_dimension
2887 | '(' abstract_ptr ')' array_dimension
2888 { $$ = $2->addArray( $4 ); }
2889 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2890 { $$ = $2->addArray( $4 ); }
2891 | '(' abstract_array ')' // redundant parenthesis
2892 { $$ = $2; }
2893 ;
[51b73452]2894
2895abstract_function:
[c0a33d2]2896 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2897 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2898 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2899 { $$ = $2->addParamList( $6 ); }
[4d51835]2900 | '(' abstract_function ')' // redundant parenthesis
2901 { $$ = $2; }
2902 ;
[51b73452]2903
2904array_dimension:
[4d51835]2905 // Only the first dimension can be empty.
[2871210]2906 '[' ']'
[4d51835]2907 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[2871210]2908 | '[' ']' multi_array_dimension
2909 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
[4d51835]2910 | multi_array_dimension
2911 ;
[51b73452]2912
2913multi_array_dimension:
[c0a33d2]2914 '[' push assignment_expression pop ']'
2915 { $$ = DeclarationNode::newArray( $3, 0, false ); }
2916 | '[' push '*' pop ']' // C99
[4d51835]2917 { $$ = DeclarationNode::newVarArray( 0 ); }
[c0a33d2]2918 | multi_array_dimension '[' push assignment_expression pop ']'
2919 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2920 | multi_array_dimension '[' push '*' pop ']' // C99
[4d51835]2921 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2922 ;
[51b73452]2923
[c11e31c]2924// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2925// identifier to which the type applies, e.g.:
2926//
[c0aa336]2927// int f( int ); // not handled here
2928// int f( int * ); // abstract function-prototype parameter; no parameter name specified
2929// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
[b87a5ed]2930// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]2931//
[de62360d]2932// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
[c0aa336]2933// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2934// special meaning of parenthesis around a typedef name:
2935//
2936// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2937// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2938// not as redundant parentheses around the identifier."
2939//
2940// For example:
2941//
2942// typedef float T;
2943// int f( int ( T [5] ) ); // see abstract_parameter_declarator
2944// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
2945// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
2946// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
2947//
2948// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2949// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2950// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2951// functions.
[51b73452]2952
2953abstract_parameter_declarator:
[4d51835]2954 abstract_parameter_ptr
[b6b3c42]2955 | '&' MUTEX attribute_list_opt
2956 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
[4d51835]2957 | abstract_parameter_array attribute_list_opt
[1db21619]2958 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2959 | abstract_parameter_function attribute_list_opt
[1db21619]2960 { $$ = $1->addQualifiers( $2 ); }
[4d51835]2961 ;
[51b73452]2962
2963abstract_parameter_ptr:
[dd51906]2964 ptrref_operator
[ce8c12f]2965 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
[dd51906]2966 | ptrref_operator type_qualifier_list
[ce8c12f]2967 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]2968 | ptrref_operator abstract_parameter_declarator
[ce8c12f]2969 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
[dd51906]2970 | ptrref_operator type_qualifier_list abstract_parameter_declarator
[ce8c12f]2971 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]2972 | '(' abstract_parameter_ptr ')' attribute_list_opt
2973 { $$ = $2->addQualifiers( $4 ); }
[4d51835]2974 ;
[51b73452]2975
2976abstract_parameter_array:
[4d51835]2977 array_parameter_dimension
2978 | '(' abstract_parameter_ptr ')' array_parameter_dimension
2979 { $$ = $2->addArray( $4 ); }
2980 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2981 { $$ = $2->addArray( $4 ); }
2982 | '(' abstract_parameter_array ')' // redundant parenthesis
2983 { $$ = $2; }
2984 ;
[51b73452]2985
2986abstract_parameter_function:
[c0a33d2]2987 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2988 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2989 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2990 { $$ = $2->addParamList( $6 ); }
[4d51835]2991 | '(' abstract_parameter_function ')' // redundant parenthesis
2992 { $$ = $2; }
2993 ;
[51b73452]2994
2995array_parameter_dimension:
[4d51835]2996 // Only the first dimension can be empty or have qualifiers.
2997 array_parameter_1st_dimension
2998 | array_parameter_1st_dimension multi_array_dimension
2999 { $$ = $1->addArray( $2 ); }
3000 | multi_array_dimension
3001 ;
[51b73452]3002
[c11e31c]3003// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3004//
[de62360d]3005// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3006// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b73452]3007
3008array_parameter_1st_dimension:
[2871210]3009 '[' ']'
[4d51835]3010 { $$ = DeclarationNode::newArray( 0, 0, false ); }
[13e8427]3011 // multi_array_dimension handles the '[' '*' ']' case
[c0a33d2]3012 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3013 { $$ = DeclarationNode::newVarArray( $3 ); }
3014 | '[' push type_qualifier_list pop ']'
3015 { $$ = DeclarationNode::newArray( 0, $3, false ); }
[13e8427]3016 // multi_array_dimension handles the '[' assignment_expression ']' case
[c0a33d2]3017 | '[' push type_qualifier_list assignment_expression pop ']'
3018 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3019 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3020 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3021 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3022 { $$ = DeclarationNode::newArray( $5, $3, true ); }
[4d51835]3023 ;
[b87a5ed]3024
[c0aa336]3025// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
[c11e31c]3026//
[c0aa336]3027// struct S {
3028// int;
3029// int *;
3030// int [10];
3031// int (*)();
3032// };
[51b73452]3033
3034variable_abstract_declarator:
[4d51835]3035 variable_abstract_ptr
3036 | variable_abstract_array attribute_list_opt
[1db21619]3037 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3038 | variable_abstract_function attribute_list_opt
[1db21619]3039 { $$ = $1->addQualifiers( $2 ); }
[4d51835]3040 ;
[51b73452]3041
3042variable_abstract_ptr:
[dd51906]3043 ptrref_operator
[ce8c12f]3044 { $$ = DeclarationNode::newPointer( 0, $1 ); }
[dd51906]3045 | ptrref_operator type_qualifier_list
[ce8c12f]3046 { $$ = DeclarationNode::newPointer( $2, $1 ); }
[dd51906]3047 | ptrref_operator variable_abstract_declarator
[ce8c12f]3048 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3049 | ptrref_operator type_qualifier_list variable_abstract_declarator
[ce8c12f]3050 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
[c0aa336]3051 | '(' variable_abstract_ptr ')' attribute_list_opt
3052 { $$ = $2->addQualifiers( $4 ); }
[4d51835]3053 ;
[51b73452]3054
3055variable_abstract_array:
[4d51835]3056 array_dimension
3057 | '(' variable_abstract_ptr ')' array_dimension
3058 { $$ = $2->addArray( $4 ); }
3059 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3060 { $$ = $2->addArray( $4 ); }
3061 | '(' variable_abstract_array ')' // redundant parenthesis
3062 { $$ = $2; }
3063 ;
[51b73452]3064
3065variable_abstract_function:
[c0a33d2]3066 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3067 { $$ = $2->addParamList( $6 ); }
[4d51835]3068 | '(' variable_abstract_function ')' // redundant parenthesis
3069 { $$ = $2; }
3070 ;
[b87a5ed]3071
[de62360d]3072// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3073// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]3074
[c0aa336]3075cfa_identifier_parameter_declarator_tuple: // CFA
3076 cfa_identifier_parameter_declarator_no_tuple
3077 | cfa_abstract_tuple
3078 | type_qualifier_list cfa_abstract_tuple
[4d51835]3079 { $$ = $2->addQualifiers( $1 ); }
3080 ;
[b87a5ed]3081
[c0aa336]3082cfa_identifier_parameter_declarator_no_tuple: // CFA
3083 cfa_identifier_parameter_ptr
3084 | cfa_identifier_parameter_array
[4d51835]3085 ;
[b87a5ed]3086
[c0aa336]3087cfa_identifier_parameter_ptr: // CFA
[d0ffed1]3088 // No SUE declaration in parameter list.
3089 ptrref_operator type_specifier_nobody
[ce8c12f]3090 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[d0ffed1]3091 | type_qualifier_list ptrref_operator type_specifier_nobody
[ce8c12f]3092 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3093 | ptrref_operator cfa_abstract_function
[ce8c12f]3094 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3095 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3096 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3097 | ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3098 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3099 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
[ce8c12f]3100 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3101 ;
[b87a5ed]3102
[c0aa336]3103cfa_identifier_parameter_array: // CFA
[de62360d]3104 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3105 // shift/reduce conflict with new-style empty (void) function return type.
[d0ffed1]3106 '[' ']' type_specifier_nobody
[2871210]3107 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3108 | cfa_array_parameter_1st_dimension type_specifier_nobody
[4d51835]3109 { $$ = $2->addNewArray( $1 ); }
[d0ffed1]3110 | '[' ']' multi_array_dimension type_specifier_nobody
[2871210]3111 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[d0ffed1]3112 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
[4d51835]3113 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[d0ffed1]3114 | multi_array_dimension type_specifier_nobody
[4d51835]3115 { $$ = $2->addNewArray( $1 ); }
[9059213]3116
[c0aa336]3117 | '[' ']' cfa_identifier_parameter_ptr
[2871210]3118 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3119 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
[4d51835]3120 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3121 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
[2871210]3122 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
[c0aa336]3123 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3124 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
[c0aa336]3125 | multi_array_dimension cfa_identifier_parameter_ptr
[4d51835]3126 { $$ = $2->addNewArray( $1 ); }
3127 ;
[51b73452]3128
[c0aa336]3129cfa_array_parameter_1st_dimension:
[c0a33d2]3130 '[' push type_qualifier_list '*' pop ']' // remaining C99
3131 { $$ = DeclarationNode::newVarArray( $3 ); }
3132 | '[' push type_qualifier_list assignment_expression pop ']'
3133 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3134 | '[' push declaration_qualifier_list assignment_expression pop ']'
[4d51835]3135 // declaration_qualifier_list must be used because of shift/reduce conflict with
3136 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3137 // appear in this context.
[c0a33d2]3138 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3139 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3140 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
[4d51835]3141 ;
[b87a5ed]3142
[de62360d]3143// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3144// identifier to which the type applies, e.g.:
[c11e31c]3145//
[b87a5ed]3146// [int] f( int ); // abstract variable parameter; no parameter name specified
3147// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
[c11e31c]3148//
3149// These rules need LR(3):
3150//
[c0aa336]3151// cfa_abstract_tuple identifier_or_type_name
[40de461]3152// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
[c11e31c]3153//
3154// since a function return type can be syntactically identical to a tuple type:
3155//
[b87a5ed]3156// [int, int] t;
3157// [int, int] f( int );
[c11e31c]3158//
[2871210]3159// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
[c0aa336]3160// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3161// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3162// duplicated when appearing with cfa_function_specifier.
[b87a5ed]3163
[c0aa336]3164cfa_abstract_declarator_tuple: // CFA
3165 cfa_abstract_tuple
3166 | type_qualifier_list cfa_abstract_tuple
[4d51835]3167 { $$ = $2->addQualifiers( $1 ); }
[c0aa336]3168 | cfa_abstract_declarator_no_tuple
[4d51835]3169 ;
[b87a5ed]3170
[c0aa336]3171cfa_abstract_declarator_no_tuple: // CFA
3172 cfa_abstract_ptr
3173 | cfa_abstract_array
[4d51835]3174 ;
[b87a5ed]3175
[c0aa336]3176cfa_abstract_ptr: // CFA
[dd51906]3177 ptrref_operator type_specifier
[ce8c12f]3178 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[dd51906]3179 | type_qualifier_list ptrref_operator type_specifier
[ce8c12f]3180 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3181 | ptrref_operator cfa_abstract_function
[ce8c12f]3182 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3183 | type_qualifier_list ptrref_operator cfa_abstract_function
[ce8c12f]3184 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[c0aa336]3185 | ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3186 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
[c0aa336]3187 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
[ce8c12f]3188 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
[4d51835]3189 ;
[b87a5ed]3190
[c0aa336]3191cfa_abstract_array: // CFA
[de62360d]3192 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3193 // empty (void) function return type.
[2871210]3194 '[' ']' type_specifier
[2298f728]3195 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[2871210]3196 | '[' ']' multi_array_dimension type_specifier
[2298f728]3197 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[4d51835]3198 | multi_array_dimension type_specifier
3199 { $$ = $2->addNewArray( $1 ); }
[c0aa336]3200 | '[' ']' cfa_abstract_ptr
[2298f728]3201 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3202 | '[' ']' multi_array_dimension cfa_abstract_ptr
[2298f728]3203 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
[c0aa336]3204 | multi_array_dimension cfa_abstract_ptr
[4d51835]3205 { $$ = $2->addNewArray( $1 ); }
3206 ;
[b87a5ed]3207
[c0aa336]3208cfa_abstract_tuple: // CFA
[c0a33d2]3209 '[' push cfa_abstract_parameter_list pop ']'
3210 { $$ = DeclarationNode::newTuple( $3 ); }
[35718a9]3211 | '[' push type_specifier_nobody ELLIPSIS pop ']'
[13e8427]3212 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[35718a9]3213 | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
[13e8427]3214 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
[4d51835]3215 ;
[b87a5ed]3216
[c0aa336]3217cfa_abstract_function: // CFA
[40de461]3218// '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
[1b29996]3219// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
[40de461]3220 cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0a33d2]3221 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[40de461]3222 | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
[c0a33d2]3223 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
[4d51835]3224 ;
[b87a5ed]3225
[de62360d]3226// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3227// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]3228//
[de62360d]3229// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3230// the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]3231//
3232// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3233// prototype-format parameter type declarators) is an obsolescent feature."
3234//
[de62360d]3235// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3236// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b73452]3237
[c11e31c]3238//************************* MISCELLANEOUS ********************************
[51b73452]3239
[b87a5ed]3240comma_opt: // redundant comma
[4d51835]3241 // empty
3242 | ','
3243 ;
[51b73452]3244
[994d080]3245default_initialize_opt:
[4d51835]3246 // empty
[58dd019]3247 { $$ = nullptr; }
[4d51835]3248 | '=' assignment_expression
3249 { $$ = $2; }
3250 ;
[51b73452]3251
3252%%
[c11e31c]3253// ----end of grammar----
[51b73452]3254
[c11e31c]3255// Local Variables: //
[b87a5ed]3256// mode: c++ //
[de62360d]3257// tab-width: 4 //
[c11e31c]3258// compile-command: "make install" //
3259// End: //
Note: See TracBrowser for help on using the repository browser.