source: src/Parser/parser.yy@ d46ed6e

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since d46ed6e was c4f68dc, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

potential changes for ID/TD/TG problem

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