source: src/Parser/parser.yy@ af75a87

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since af75a87 was 7991c7d, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

change enumerator_value_opt to only parse constant_expression

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