source: src/Parser/parser.yy@ 12df6fe

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 12df6fe was b0d9ff7, checked in by JiadaL <j82liang@…>, 3 years ago

Fix up the QualifiedNameExpr. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

  • Property mode set to 100644
File size: 153.1 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 { $$ = new ExpressionNode( build_qualified_expr( $1, build_varref( $3 ) ) ); }
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, false )->addQualifiers( $2 ); }
2390 | ENUM attribute_list_opt identifier
2391 { typedefTable.makeTypedef( *$3 ); }
2392 '{' enumerator_list comma_opt '}'
2393 { $$ = DeclarationNode::newEnum( $3, $6, true, false )->addQualifiers( $2 ); }
2394 | ENUM attribute_list_opt typedef_name // unqualified type name
2395 '{' enumerator_list comma_opt '}'
2396 { $$ = DeclarationNode::newEnum( $3->name, $5, true, false )->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, true, $3 )->addQualifiers( $5 );
2403 }
2404 | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2405 {
2406 $$ = DeclarationNode::newEnum( nullptr, $6, true, true )->addQualifiers( $4 );
2407 }
2408 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
2409 {
2410 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." ); }
2411 typedefTable.makeTypedef( *$6 );
2412 }
2413 '{' enumerator_list comma_opt '}'
2414 {
2415 $$ = DeclarationNode::newEnum( $6, $10, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2416 }
2417 | ENUM '(' ')' attribute_list_opt identifier attribute_list_opt
2418 '{' enumerator_list comma_opt '}'
2419 {
2420 $$ = DeclarationNode::newEnum( $5, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
2421 }
2422 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
2423 {
2424 $$ = DeclarationNode::newEnum( $6->name, $9, true, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2425 }
2426 | ENUM '(' ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
2427 {
2428 $$ = DeclarationNode::newEnum( $5->name, $8, true, true, nullptr )->addQualifiers( $4 )->addQualifiers( $6 );
2429 }
2430 | enum_type_nobody
2431 ;
2432
2433enum_type_nobody: // enum - {...}
2434 ENUM attribute_list_opt identifier
2435 { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false, false )->addQualifiers( $2 ); }
2436 | ENUM attribute_list_opt type_name
2437 { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false, false )->addQualifiers( $2 ); }
2438 ;
2439
2440enumerator_list:
2441 identifier_or_type_name enumerator_value_opt
2442 { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
2443 | INLINE type_name
2444 { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
2445 | enumerator_list ',' identifier_or_type_name enumerator_value_opt
2446 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
2447 | enumerator_list ',' INLINE type_name enumerator_value_opt
2448 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
2449 ;
2450
2451enumerator_value_opt:
2452 // empty
2453 { $$ = nullptr; }
2454 | '=' constant_expression { $$ = new InitializerNode( $2 ); }
2455 | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2456 // | simple_assignment_operator initializer
2457 // { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2458 ;
2459
2460cfa_parameter_ellipsis_list_opt: // CFA, abstract + real
2461 // empty
2462 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2463 | ELLIPSIS
2464 { $$ = nullptr; }
2465 | cfa_abstract_parameter_list
2466 | cfa_parameter_list
2467 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
2468 { $$ = $1->appendList( $5 ); }
2469 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
2470 { $$ = $1->addVarArgs(); }
2471 | cfa_parameter_list pop ',' push ELLIPSIS
2472 { $$ = $1->addVarArgs(); }
2473 ;
2474
2475cfa_parameter_list: // CFA
2476 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2477 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2478 cfa_parameter_declaration
2479 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2480 { $$ = $1->appendList( $5 ); }
2481 | cfa_parameter_list pop ',' push cfa_parameter_declaration
2482 { $$ = $1->appendList( $5 ); }
2483 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2484 { $$ = $1->appendList( $5 )->appendList( $9 ); }
2485 ;
2486
2487cfa_abstract_parameter_list: // CFA, new & old style abstract
2488 cfa_abstract_parameter_declaration
2489 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2490 { $$ = $1->appendList( $5 ); }
2491 ;
2492
2493parameter_type_list_opt:
2494 // empty
2495 { $$ = nullptr; }
2496 | ELLIPSIS
2497 { $$ = nullptr; }
2498 | parameter_list
2499 | parameter_list pop ',' push ELLIPSIS
2500 { $$ = $1->addVarArgs(); }
2501 ;
2502
2503parameter_list: // abstract + real
2504 abstract_parameter_declaration
2505 | parameter_declaration
2506 | parameter_list pop ',' push abstract_parameter_declaration
2507 { $$ = $1->appendList( $5 ); }
2508 | parameter_list pop ',' push parameter_declaration
2509 { $$ = $1->appendList( $5 ); }
2510 ;
2511
2512// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
2513// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
2514
2515cfa_parameter_declaration: // CFA, new & old style parameter declaration
2516 parameter_declaration
2517 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
2518 { $$ = $1->addName( $2 ); }
2519 | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2520 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2521 { $$ = $1->addName( $2 ); }
2522 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2523 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
2524 | cfa_function_specifier
2525 ;
2526
2527cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
2528 abstract_parameter_declaration
2529 | cfa_identifier_parameter_declarator_no_tuple
2530 | cfa_abstract_tuple
2531 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2532 | type_qualifier_list cfa_abstract_tuple
2533 { $$ = $2->addQualifiers( $1 ); }
2534 | cfa_abstract_function
2535 ;
2536
2537parameter_declaration:
2538 // No SUE declaration in parameter list.
2539 declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
2540 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2541 | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
2542 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2543 ;
2544
2545abstract_parameter_declaration:
2546 declaration_specifier_nobody default_initializer_opt
2547 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2548 | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
2549 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2550 ;
2551
2552// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2553// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2554// identifiers. The ANSI-style parameter-list can redefine a typedef name.
2555
2556identifier_list: // K&R-style parameter list => no types
2557 identifier
2558 { $$ = DeclarationNode::newName( $1 ); }
2559 | identifier_list ',' identifier
2560 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2561 ;
2562
2563identifier_or_type_name:
2564 identifier
2565 | TYPEDEFname
2566 | TYPEGENname
2567 ;
2568
2569type_no_function: // sizeof, alignof, cast (constructor)
2570 cfa_abstract_declarator_tuple // CFA
2571 | type_specifier
2572 | type_specifier abstract_declarator
2573 { $$ = $2->addType( $1 ); }
2574 ;
2575
2576type: // typeof, assertion
2577 type_no_function
2578 | cfa_abstract_function // CFA
2579 ;
2580
2581initializer_opt:
2582 // empty
2583 { $$ = nullptr; }
2584 | simple_assignment_operator initializer { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2585 | '=' VOID { $$ = new InitializerNode( true ); }
2586 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2587 ;
2588
2589initializer:
2590 assignment_expression { $$ = new InitializerNode( $1 ); }
2591 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2592 ;
2593
2594initializer_list_opt:
2595 // empty
2596 { $$ = nullptr; }
2597 | initializer
2598 | designation initializer { $$ = $2->set_designators( $1 ); }
2599 | initializer_list_opt ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2600 | initializer_list_opt ',' designation initializer { $$ = (InitializerNode *)($1->set_last( $4->set_designators( $3 ) )); }
2601 ;
2602
2603// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2604// '=' to separator the designator from the initializer value, as in:
2605//
2606// int x[10] = { [1] = 3 };
2607//
2608// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2609// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2610// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2611// shift/reduce conflicts
2612
2613designation:
2614 designator_list ':' // C99, CFA uses ":" instead of "="
2615 | identifier_at ':' // GCC, field name
2616 { $$ = new ExpressionNode( build_varref( $1 ) ); }
2617 ;
2618
2619designator_list: // C99
2620 designator
2621 | designator_list designator
2622 { $$ = (ExpressionNode *)($1->set_last( $2 )); }
2623 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
2624 ;
2625
2626designator:
2627 '.' identifier_at // C99, field name
2628 { $$ = new ExpressionNode( build_varref( $2 ) ); }
2629 | '[' push assignment_expression pop ']' // C99, single array element
2630 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2631 { $$ = $3; }
2632 | '[' push subrange pop ']' // CFA, multiple array elements
2633 { $$ = $3; }
2634 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2635 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
2636 | '.' '[' push field_name_list pop ']' // CFA, tuple field selector
2637 { $$ = $4; }
2638 ;
2639
2640// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2641// rather than an object-oriented type system. This required four groups of extensions:
2642//
2643// Overloading: function, data, and operator identifiers may be overloaded.
2644//
2645// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2646// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2647// definitions of new types. Type declarations with storage class "extern" provide opaque types.
2648//
2649// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2650// site. A polymorphic function is not a template; it is a function, with an address and a type.
2651//
2652// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2653// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2654// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2655// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2656// on type arguments of polymorphic functions.
2657
2658type_parameter_list: // CFA
2659 type_parameter
2660 | type_parameter_list ',' type_parameter
2661 { $$ = $1->appendList( $3 ); }
2662 ;
2663
2664type_initializer_opt: // CFA
2665 // empty
2666 { $$ = nullptr; }
2667 | '=' type
2668 { $$ = $2; }
2669 ;
2670
2671type_parameter: // CFA
2672 type_class identifier_or_type_name
2673 {
2674 typedefTable.addToScope( *$2, TYPEDEFname, "9" );
2675 if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
2676 if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
2677 if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
2678 }
2679 type_initializer_opt assertion_list_opt
2680 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2681 | identifier_or_type_name new_type_class
2682 { typedefTable.addToScope( *$1, TYPEDEFname, "9" ); }
2683 type_initializer_opt assertion_list_opt
2684 { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2685 | '[' identifier_or_type_name ']'
2686 {
2687 typedefTable.addToScope( *$2, TYPEDIMname, "9" );
2688 $$ = DeclarationNode::newTypeParam( TypeDecl::Dimension, $2 );
2689 }
2690 // | type_specifier identifier_parameter_declarator
2691 | assertion_list
2692 { $$ = DeclarationNode::newTypeParam( TypeDecl::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
2693 ;
2694
2695new_type_class: // CFA
2696 // empty
2697 { $$ = TypeDecl::Otype; }
2698 | '&'
2699 { $$ = TypeDecl::Dtype; }
2700 | '*'
2701 { $$ = TypeDecl::DStype; } // dtype + sized
2702 // | '(' '*' ')'
2703 // { $$ = TypeDecl::Ftype; }
2704 | ELLIPSIS
2705 { $$ = TypeDecl::Ttype; }
2706 ;
2707
2708type_class: // CFA
2709 OTYPE
2710 { $$ = TypeDecl::Otype; }
2711 | DTYPE
2712 { $$ = TypeDecl::Dtype; }
2713 | FTYPE
2714 { $$ = TypeDecl::Ftype; }
2715 | TTYPE
2716 { $$ = TypeDecl::Ttype; }
2717 ;
2718
2719assertion_list_opt: // CFA
2720 // empty
2721 { $$ = nullptr; }
2722 | assertion_list
2723 ;
2724
2725assertion_list: // CFA
2726 assertion
2727 | assertion_list assertion
2728 { $$ = $1->appendList( $2 ); }
2729 ;
2730
2731assertion: // CFA
2732 '|' identifier_or_type_name '(' type_list ')'
2733 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
2734 | '|' '{' push trait_declaration_list pop '}'
2735 { $$ = $4; }
2736 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2737 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
2738 ;
2739
2740type_list: // CFA
2741 type
2742 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
2743 | assignment_expression
2744 | type_list ',' type
2745 { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) )); }
2746 | type_list ',' assignment_expression
2747 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2748 ;
2749
2750type_declaring_list: // CFA
2751 OTYPE type_declarator
2752 { $$ = $2; }
2753 | storage_class_list OTYPE type_declarator
2754 { $$ = $3->addQualifiers( $1 ); }
2755 | type_declaring_list ',' type_declarator
2756 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2757 ;
2758
2759type_declarator: // CFA
2760 type_declarator_name assertion_list_opt
2761 { $$ = $1->addAssertions( $2 ); }
2762 | type_declarator_name assertion_list_opt '=' type
2763 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2764 ;
2765
2766type_declarator_name: // CFA
2767 identifier_or_type_name
2768 {
2769 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
2770 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2771 }
2772 | identifier_or_type_name '(' type_parameter_list ')'
2773 {
2774 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
2775 $$ = DeclarationNode::newTypeDecl( $1, $3 );
2776 }
2777 ;
2778
2779trait_specifier: // CFA
2780 TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
2781 { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
2782 | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
2783 { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
2784 ;
2785
2786trait_declaration_list: // CFA
2787 trait_declaration
2788 | trait_declaration_list pop push trait_declaration
2789 { $$ = $1->appendList( $4 ); }
2790 ;
2791
2792trait_declaration: // CFA
2793 cfa_trait_declaring_list ';'
2794 | trait_declaring_list ';'
2795 ;
2796
2797cfa_trait_declaring_list: // CFA
2798 cfa_variable_specifier
2799 | cfa_function_specifier
2800 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2801 { $$ = $1->appendList( $1->cloneType( $5 ) ); }
2802 ;
2803
2804trait_declaring_list: // CFA
2805 type_specifier declarator
2806 { $$ = $2->addType( $1 ); }
2807 | trait_declaring_list pop ',' push declarator
2808 { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
2809 ;
2810
2811//***************************** EXTERNAL DEFINITIONS *****************************
2812
2813translation_unit:
2814 // empty, input file
2815 | external_definition_list
2816 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
2817 ;
2818
2819external_definition_list:
2820 push external_definition pop
2821 { $$ = $2; }
2822 | external_definition_list push external_definition pop
2823 { $$ = $1 ? $1->appendList( $3 ) : $3; }
2824 ;
2825
2826external_definition_list_opt:
2827 // empty
2828 { $$ = nullptr; }
2829 | external_definition_list
2830 ;
2831
2832up:
2833 { typedefTable.up( forall ); forall = false; }
2834 ;
2835
2836down:
2837 { typedefTable.down(); }
2838 ;
2839
2840external_definition:
2841 DIRECTIVE
2842 { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( $1 ) ) ); }
2843 | declaration
2844 | external_function_definition
2845 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2846 {
2847 distExt( $2 ); // mark all fields in list
2848 $$ = $2;
2849 }
2850 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2851 { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) ); }
2852 | EXTERN STRINGliteral
2853 {
2854 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2855 linkage = LinkageSpec::update( yylloc, linkage, $2 );
2856 }
2857 up external_definition down
2858 {
2859 linkage = linkageStack.top();
2860 linkageStack.pop();
2861 $$ = $5;
2862 }
2863 | EXTERN STRINGliteral // C++-style linkage specifier
2864 {
2865 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2866 linkage = LinkageSpec::update( yylloc, linkage, $2 );
2867 }
2868 '{' up external_definition_list_opt down '}'
2869 {
2870 linkage = linkageStack.top();
2871 linkageStack.pop();
2872 $$ = $6;
2873 }
2874 | type_qualifier_list
2875 {
2876 if ( $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2877 if ( $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
2886 {
2887 if ( $1->type && $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2888 if ( $1->type && $1->type->forall ) forall = true; // remember generic type
2889 }
2890 '{' up external_definition_list_opt down '}' // CFA, namespace
2891 {
2892 distQual( $5, $1 );
2893 forall = false;
2894 $$ = $5;
2895 }
2896 | declaration_qualifier_list type_qualifier_list
2897 {
2898 if ( ($1->type && $1->type->qualifiers.val) || $2->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2899 if ( ($1->type && $1->type->forall) || $2->type->forall ) forall = true; // remember generic type
2900 }
2901 '{' up external_definition_list_opt down '}' // CFA, namespace
2902 {
2903 distQual( $6, $1->addQualifiers( $2 ) );
2904 forall = false;
2905 $$ = $6;
2906 }
2907 ;
2908
2909external_function_definition:
2910 function_definition
2911 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2912 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2913 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2914 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2915 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
2916 | function_declarator compound_statement
2917 { $$ = $1->addFunctionBody( $2 ); }
2918 | KR_function_declarator KR_parameter_list_opt compound_statement
2919 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
2920 ;
2921
2922with_clause_opt:
2923 // empty
2924 { $$ = nullptr; forall = false; }
2925 | WITH '(' tuple_expression_list ')' attribute_list_opt
2926 {
2927 $$ = $3; forall = false;
2928 if ( $5 ) {
2929 SemanticError( yylloc, "Attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
2930 $$ = nullptr;
2931 } // if
2932 }
2933 ;
2934
2935function_definition:
2936 cfa_function_declaration with_clause_opt compound_statement // CFA
2937 {
2938 // Add the function body to the last identifier in the function definition list, i.e., foo3:
2939 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
2940 $1->get_last()->addFunctionBody( $3, $2 );
2941 $$ = $1;
2942 }
2943 | declaration_specifier function_declarator with_clause_opt compound_statement
2944 {
2945 rebindForall( $1, $2 );
2946 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2947 }
2948 | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
2949 {
2950 rebindForall( $1, $2 );
2951 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
2952 }
2953 // handles default int return type, OBSOLESCENT (see 1)
2954 | type_qualifier_list function_declarator with_clause_opt compound_statement
2955 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
2956 // handles default int return type, OBSOLESCENT (see 1)
2957 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
2958 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
2959 // handles default int return type, OBSOLESCENT (see 1)
2960 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
2961 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
2962
2963 // Old-style K&R function definition, OBSOLESCENT (see 4)
2964 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2965 {
2966 rebindForall( $1, $2 );
2967 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
2968 }
2969 // handles default int return type, OBSOLESCENT (see 1)
2970 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2971 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
2972 // handles default int return type, OBSOLESCENT (see 1)
2973 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2974 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
2975 // handles default int return type, OBSOLESCENT (see 1)
2976 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
2977 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
2978 ;
2979
2980declarator:
2981 variable_declarator
2982 | variable_type_redeclarator
2983 | function_declarator
2984 ;
2985
2986subrange:
2987 constant_expression '~' constant_expression // CFA, integer subrange
2988 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
2989 ;
2990
2991asm_name_opt: // GCC
2992 // empty
2993 { $$ = nullptr; }
2994 | ASM '(' string_literal ')' attribute_list_opt
2995 {
2996 DeclarationNode * name = new DeclarationNode();
2997 name->asmName = $3;
2998 $$ = name->addQualifiers( $5 );
2999 }
3000 ;
3001
3002attribute_list_opt: // GCC
3003 // empty
3004 { $$ = nullptr; }
3005 | attribute_list
3006 ;
3007
3008attribute_list: // GCC
3009 attribute
3010 | attribute_list attribute
3011 { $$ = $2->addQualifiers( $1 ); }
3012 ;
3013
3014attribute: // GCC
3015 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3016 { $$ = $4; }
3017 ;
3018
3019attribute_name_list: // GCC
3020 attribute_name
3021 | attribute_name_list ',' attribute_name
3022 { $$ = $3->addQualifiers( $1 ); }
3023 ;
3024
3025attribute_name: // GCC
3026 // empty
3027 { $$ = nullptr; }
3028 | attr_name
3029 { $$ = DeclarationNode::newAttribute( $1 ); }
3030 | attr_name '(' argument_expression_list_opt ')'
3031 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
3032 ;
3033
3034attr_name: // GCC
3035 IDENTIFIER
3036 | quasi_keyword
3037 | TYPEDEFname
3038 | TYPEGENname
3039 | FALLTHROUGH
3040 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
3041 | CONST
3042 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
3043 ;
3044
3045// ============================================================================
3046// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3047// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3048// in:
3049//
3050// int (*f())[10] { ... };
3051// ... (*f())[3] += 1; // definition mimics usage
3052//
3053// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3054// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
3055// ============================================================================
3056
3057// ----------------------------------------------------------------------------
3058// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3059// to define a variable or function prototype, e.g.:
3060//
3061// valid declaration invalid definition
3062// ----------------- ------------------
3063// int f; int f {}
3064// int *f; int *f {}
3065// int f[10]; int f[10] {}
3066// int (*f)(int); int (*f)(int) {}
3067//
3068// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3069// variable_declarator and function_declarator.
3070// ----------------------------------------------------------------------------
3071
3072// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3073// declaring an array of functions versus a pointer to an array of functions.
3074
3075paren_identifier:
3076 identifier_at
3077 { $$ = DeclarationNode::newName( $1 ); }
3078 | '(' paren_identifier ')' // redundant parenthesis
3079 { $$ = $2; }
3080 ;
3081
3082variable_declarator:
3083 paren_identifier attribute_list_opt
3084 { $$ = $1->addQualifiers( $2 ); }
3085 | variable_ptr
3086 | variable_array attribute_list_opt
3087 { $$ = $1->addQualifiers( $2 ); }
3088 | variable_function attribute_list_opt
3089 { $$ = $1->addQualifiers( $2 ); }
3090 ;
3091
3092variable_ptr:
3093 ptrref_operator variable_declarator
3094 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3095 | ptrref_operator type_qualifier_list variable_declarator
3096 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3097 | '(' variable_ptr ')' attribute_list_opt // redundant parenthesis
3098 { $$ = $2->addQualifiers( $4 ); }
3099 | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3100 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3101 ;
3102
3103variable_array:
3104 paren_identifier array_dimension
3105 { $$ = $1->addArray( $2 ); }
3106 | '(' variable_ptr ')' array_dimension
3107 { $$ = $2->addArray( $4 ); }
3108 | '(' attribute_list variable_ptr ')' array_dimension
3109 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3110 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
3111 { $$ = $2->addArray( $4 ); }
3112 | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3113 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3114 | '(' variable_array ')' // redundant parenthesis
3115 { $$ = $2; }
3116 | '(' attribute_list variable_array ')' // redundant parenthesis
3117 { $$ = $3->addQualifiers( $2 ); }
3118 ;
3119
3120variable_function:
3121 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3122 { $$ = $2->addParamList( $6 ); }
3123 | '(' attribute_list variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3124 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3125 | '(' variable_function ')' // redundant parenthesis
3126 { $$ = $2; }
3127 | '(' attribute_list variable_function ')' // redundant parenthesis
3128 { $$ = $3->addQualifiers( $2 ); }
3129 ;
3130
3131// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3132// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3133// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
3134
3135function_declarator:
3136 function_no_ptr attribute_list_opt
3137 { $$ = $1->addQualifiers( $2 ); }
3138 | function_ptr
3139 | function_array attribute_list_opt
3140 { $$ = $1->addQualifiers( $2 ); }
3141 ;
3142
3143function_no_ptr:
3144 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3145 { $$ = $1->addParamList( $4 ); }
3146 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
3147 { $$ = $2->addParamList( $6 ); }
3148 | '(' attribute_list function_ptr ')' '(' push parameter_type_list_opt pop ')'
3149 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3150 | '(' function_no_ptr ')' // redundant parenthesis
3151 { $$ = $2; }
3152 | '(' attribute_list function_no_ptr ')' // redundant parenthesis
3153 { $$ = $3->addQualifiers( $2 ); }
3154 ;
3155
3156function_ptr:
3157 ptrref_operator function_declarator
3158 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3159 | ptrref_operator type_qualifier_list function_declarator
3160 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3161 | '(' function_ptr ')' attribute_list_opt
3162 { $$ = $2->addQualifiers( $4 ); }
3163 | '(' attribute_list function_ptr ')' attribute_list_opt
3164 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3165 ;
3166
3167function_array:
3168 '(' function_ptr ')' array_dimension
3169 { $$ = $2->addArray( $4 ); }
3170 | '(' attribute_list function_ptr ')' array_dimension
3171 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3172 | '(' function_array ')' multi_array_dimension // redundant parenthesis
3173 { $$ = $2->addArray( $4 ); }
3174 | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3175 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3176 | '(' function_array ')' // redundant parenthesis
3177 { $$ = $2; }
3178 | '(' attribute_list function_array ')' // redundant parenthesis
3179 { $$ = $3->addQualifiers( $2 ); }
3180 ;
3181
3182// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3183//
3184// f( a, b, c ) int a, *b, c[]; {}
3185//
3186// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3187// returning arrays and functions versus pointers to arrays and functions.
3188
3189KR_function_declarator:
3190 KR_function_no_ptr
3191 | KR_function_ptr
3192 | KR_function_array
3193 ;
3194
3195KR_function_no_ptr:
3196 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
3197 { $$ = $1->addIdList( $3 ); }
3198 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3199 { $$ = $2->addParamList( $6 ); }
3200 | '(' attribute_list KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3201 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3202 | '(' KR_function_no_ptr ')' // redundant parenthesis
3203 { $$ = $2; }
3204 | '(' attribute_list KR_function_no_ptr ')' // redundant parenthesis
3205 { $$ = $3->addQualifiers( $2 ); }
3206 ;
3207
3208KR_function_ptr:
3209 ptrref_operator KR_function_declarator
3210 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3211 | ptrref_operator type_qualifier_list KR_function_declarator
3212 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3213 | '(' KR_function_ptr ')'
3214 { $$ = $2; }
3215 | '(' attribute_list KR_function_ptr ')'
3216 { $$ = $3->addQualifiers( $2 ); }
3217 ;
3218
3219KR_function_array:
3220 '(' KR_function_ptr ')' array_dimension
3221 { $$ = $2->addArray( $4 ); }
3222 | '(' attribute_list KR_function_ptr ')' array_dimension
3223 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3224 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
3225 { $$ = $2->addArray( $4 ); }
3226 | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3227 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3228 | '(' KR_function_array ')' // redundant parenthesis
3229 { $$ = $2; }
3230 | '(' attribute_list KR_function_array ')' // redundant parenthesis
3231 { $$ = $3->addQualifiers( $2 ); }
3232 ;
3233
3234// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
3235//
3236// typedef int foo;
3237// {
3238// int foo; // redefine typedef name in new scope
3239// }
3240//
3241// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3242// and functions versus pointers to arrays and functions.
3243
3244paren_type:
3245 typedef_name
3246 {
3247 // hide type name in enclosing scope by variable name
3248 typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" );
3249 }
3250 | '(' paren_type ')'
3251 { $$ = $2; }
3252 ;
3253
3254variable_type_redeclarator:
3255 paren_type attribute_list_opt
3256 { $$ = $1->addQualifiers( $2 ); }
3257 | type_ptr
3258 | type_array attribute_list_opt
3259 { $$ = $1->addQualifiers( $2 ); }
3260 | type_function attribute_list_opt
3261 { $$ = $1->addQualifiers( $2 ); }
3262 ;
3263
3264type_ptr:
3265 ptrref_operator variable_type_redeclarator
3266 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3267 | ptrref_operator type_qualifier_list variable_type_redeclarator
3268 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3269 | '(' type_ptr ')' attribute_list_opt // redundant parenthesis
3270 { $$ = $2->addQualifiers( $4 ); }
3271 | '(' attribute_list type_ptr ')' attribute_list_opt // redundant parenthesis
3272 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3273 ;
3274
3275type_array:
3276 paren_type array_dimension
3277 { $$ = $1->addArray( $2 ); }
3278 | '(' type_ptr ')' array_dimension
3279 { $$ = $2->addArray( $4 ); }
3280 | '(' attribute_list type_ptr ')' array_dimension
3281 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3282 | '(' type_array ')' multi_array_dimension // redundant parenthesis
3283 { $$ = $2->addArray( $4 ); }
3284 | '(' attribute_list type_array ')' multi_array_dimension // redundant parenthesis
3285 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3286 | '(' type_array ')' // redundant parenthesis
3287 { $$ = $2; }
3288 | '(' attribute_list type_array ')' // redundant parenthesis
3289 { $$ = $3->addQualifiers( $2 ); }
3290 ;
3291
3292type_function:
3293 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3294 { $$ = $1->addParamList( $4 ); }
3295 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3296 { $$ = $2->addParamList( $6 ); }
3297 | '(' attribute_list type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3298 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3299 | '(' type_function ')' // redundant parenthesis
3300 { $$ = $2; }
3301 | '(' attribute_list type_function ')' // redundant parenthesis
3302 { $$ = $3->addQualifiers( $2 ); }
3303 ;
3304
3305// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3306// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
3307// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3308// pointers to arrays and functions.
3309
3310identifier_parameter_declarator:
3311 paren_identifier attribute_list_opt
3312 { $$ = $1->addQualifiers( $2 ); }
3313 | '&' MUTEX paren_identifier attribute_list_opt
3314 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3315 | identifier_parameter_ptr
3316 | identifier_parameter_array attribute_list_opt
3317 { $$ = $1->addQualifiers( $2 ); }
3318 | identifier_parameter_function attribute_list_opt
3319 { $$ = $1->addQualifiers( $2 ); }
3320 ;
3321
3322identifier_parameter_ptr:
3323 ptrref_operator identifier_parameter_declarator
3324 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3325 | ptrref_operator type_qualifier_list identifier_parameter_declarator
3326 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3327 | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3328 { $$ = $2->addQualifiers( $4 ); }
3329 ;
3330
3331identifier_parameter_array:
3332 paren_identifier array_parameter_dimension
3333 { $$ = $1->addArray( $2 ); }
3334 | '(' identifier_parameter_ptr ')' array_dimension
3335 { $$ = $2->addArray( $4 ); }
3336 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3337 { $$ = $2->addArray( $4 ); }
3338 | '(' identifier_parameter_array ')' // redundant parenthesis
3339 { $$ = $2; }
3340 ;
3341
3342identifier_parameter_function:
3343 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3344 { $$ = $1->addParamList( $4 ); }
3345 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3346 { $$ = $2->addParamList( $6 ); }
3347 | '(' identifier_parameter_function ')' // redundant parenthesis
3348 { $$ = $2; }
3349 ;
3350
3351// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3352// e.g.:
3353//
3354// typedef int foo;
3355// forall( otype T ) struct foo;
3356// int f( int foo ); // redefine typedef name in new scope
3357//
3358// and allows the C99 array options, which can only appear in a parameter list.
3359
3360type_parameter_redeclarator:
3361 typedef_name attribute_list_opt
3362 { $$ = $1->addQualifiers( $2 ); }
3363 | '&' MUTEX typedef_name attribute_list_opt
3364 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3365 | type_parameter_ptr
3366 | type_parameter_array attribute_list_opt
3367 { $$ = $1->addQualifiers( $2 ); }
3368 | type_parameter_function attribute_list_opt
3369 { $$ = $1->addQualifiers( $2 ); }
3370 ;
3371
3372typedef_name:
3373 TYPEDEFname
3374 { $$ = DeclarationNode::newName( $1 ); }
3375 | TYPEGENname
3376 { $$ = DeclarationNode::newName( $1 ); }
3377 ;
3378
3379type_parameter_ptr:
3380 ptrref_operator type_parameter_redeclarator
3381 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3382 | ptrref_operator type_qualifier_list type_parameter_redeclarator
3383 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3384 | '(' type_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3385 { $$ = $2->addQualifiers( $4 ); }
3386 ;
3387
3388type_parameter_array:
3389 typedef_name array_parameter_dimension
3390 { $$ = $1->addArray( $2 ); }
3391 | '(' type_parameter_ptr ')' array_parameter_dimension
3392 { $$ = $2->addArray( $4 ); }
3393 ;
3394
3395type_parameter_function:
3396 typedef_name '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3397 { $$ = $1->addParamList( $4 ); }
3398 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3399 { $$ = $2->addParamList( $6 ); }
3400 ;
3401
3402// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3403// which the type applies, e.g.:
3404//
3405// sizeof( int );
3406// sizeof( int * );
3407// sizeof( int [10] );
3408// sizeof( int (*)() );
3409// sizeof( int () );
3410//
3411// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3412// and functions versus pointers to arrays and functions.
3413
3414abstract_declarator:
3415 abstract_ptr
3416 | abstract_array attribute_list_opt
3417 { $$ = $1->addQualifiers( $2 ); }
3418 | abstract_function attribute_list_opt
3419 { $$ = $1->addQualifiers( $2 ); }
3420 ;
3421
3422abstract_ptr:
3423 ptrref_operator
3424 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3425 | ptrref_operator type_qualifier_list
3426 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3427 | ptrref_operator abstract_declarator
3428 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3429 | ptrref_operator type_qualifier_list abstract_declarator
3430 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3431 | '(' abstract_ptr ')' attribute_list_opt
3432 { $$ = $2->addQualifiers( $4 ); }
3433 ;
3434
3435abstract_array:
3436 array_dimension
3437 | '(' abstract_ptr ')' array_dimension
3438 { $$ = $2->addArray( $4 ); }
3439 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
3440 { $$ = $2->addArray( $4 ); }
3441 | '(' abstract_array ')' // redundant parenthesis
3442 { $$ = $2; }
3443 ;
3444
3445abstract_function:
3446 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3447 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3448 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3449 { $$ = $2->addParamList( $6 ); }
3450 | '(' abstract_function ')' // redundant parenthesis
3451 { $$ = $2; }
3452 ;
3453
3454array_dimension:
3455 // Only the first dimension can be empty.
3456 '[' ']'
3457 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3458 | '[' ']' multi_array_dimension
3459 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
3460 | '[' push assignment_expression pop ',' comma_expression ']'
3461 { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); }
3462 // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
3463 | multi_array_dimension
3464 ;
3465
3466multi_array_dimension:
3467 '[' push assignment_expression pop ']'
3468 { $$ = DeclarationNode::newArray( $3, 0, false ); }
3469 | '[' push '*' pop ']' // C99
3470 { $$ = DeclarationNode::newVarArray( 0 ); }
3471 | multi_array_dimension '[' push assignment_expression pop ']'
3472 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3473 | multi_array_dimension '[' push '*' pop ']' // C99
3474 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3475 ;
3476
3477// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3478// identifier to which the type applies, e.g.:
3479//
3480// int f( int ); // not handled here
3481// int f( int * ); // abstract function-prototype parameter; no parameter name specified
3482// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
3483// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
3484//
3485// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3486// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3487// special meaning of parenthesis around a typedef name:
3488//
3489// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3490// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3491// not as redundant parentheses around the identifier."
3492//
3493// For example:
3494//
3495// typedef float T;
3496// int f( int ( T [5] ) ); // see abstract_parameter_declarator
3497// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
3498// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
3499// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
3500//
3501// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3502// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3503// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3504// functions.
3505
3506abstract_parameter_declarator:
3507 abstract_parameter_ptr
3508 | '&' MUTEX attribute_list_opt
3509 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
3510 | abstract_parameter_array attribute_list_opt
3511 { $$ = $1->addQualifiers( $2 ); }
3512 | abstract_parameter_function attribute_list_opt
3513 { $$ = $1->addQualifiers( $2 ); }
3514 ;
3515
3516abstract_parameter_ptr:
3517 ptrref_operator
3518 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3519 | ptrref_operator type_qualifier_list
3520 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3521 | ptrref_operator abstract_parameter_declarator
3522 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3523 | ptrref_operator type_qualifier_list abstract_parameter_declarator
3524 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3525 | '(' abstract_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3526 { $$ = $2->addQualifiers( $4 ); }
3527 ;
3528
3529abstract_parameter_array:
3530 array_parameter_dimension
3531 | '(' abstract_parameter_ptr ')' array_parameter_dimension
3532 { $$ = $2->addArray( $4 ); }
3533 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3534 { $$ = $2->addArray( $4 ); }
3535 | '(' abstract_parameter_array ')' // redundant parenthesis
3536 { $$ = $2; }
3537 ;
3538
3539abstract_parameter_function:
3540 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3541 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3542 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3543 { $$ = $2->addParamList( $6 ); }
3544 | '(' abstract_parameter_function ')' // redundant parenthesis
3545 { $$ = $2; }
3546 ;
3547
3548array_parameter_dimension:
3549 // Only the first dimension can be empty or have qualifiers.
3550 array_parameter_1st_dimension
3551 | array_parameter_1st_dimension multi_array_dimension
3552 { $$ = $1->addArray( $2 ); }
3553 | multi_array_dimension
3554 ;
3555
3556// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3557//
3558// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3559// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3560
3561array_parameter_1st_dimension:
3562 '[' ']'
3563 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3564 // multi_array_dimension handles the '[' '*' ']' case
3565 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3566 { $$ = DeclarationNode::newVarArray( $3 ); }
3567 | '[' push type_qualifier_list pop ']'
3568 { $$ = DeclarationNode::newArray( 0, $3, false ); }
3569 // multi_array_dimension handles the '[' assignment_expression ']' case
3570 | '[' push type_qualifier_list assignment_expression pop ']'
3571 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3572 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3573 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3574 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3575 { $$ = DeclarationNode::newArray( $5, $3, true ); }
3576 ;
3577
3578// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
3579//
3580// struct S {
3581// int;
3582// int *;
3583// int [10];
3584// int (*)();
3585// };
3586
3587variable_abstract_declarator:
3588 variable_abstract_ptr
3589 | variable_abstract_array attribute_list_opt
3590 { $$ = $1->addQualifiers( $2 ); }
3591 | variable_abstract_function attribute_list_opt
3592 { $$ = $1->addQualifiers( $2 ); }
3593 ;
3594
3595variable_abstract_ptr:
3596 ptrref_operator
3597 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3598 | ptrref_operator type_qualifier_list
3599 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3600 | ptrref_operator variable_abstract_declarator
3601 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3602 | ptrref_operator type_qualifier_list variable_abstract_declarator
3603 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3604 | '(' variable_abstract_ptr ')' attribute_list_opt // redundant parenthesis
3605 { $$ = $2->addQualifiers( $4 ); }
3606 ;
3607
3608variable_abstract_array:
3609 array_dimension
3610 | '(' variable_abstract_ptr ')' array_dimension
3611 { $$ = $2->addArray( $4 ); }
3612 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3613 { $$ = $2->addArray( $4 ); }
3614 | '(' variable_abstract_array ')' // redundant parenthesis
3615 { $$ = $2; }
3616 ;
3617
3618variable_abstract_function:
3619 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3620 { $$ = $2->addParamList( $6 ); }
3621 | '(' variable_abstract_function ')' // redundant parenthesis
3622 { $$ = $2; }
3623 ;
3624
3625// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3626// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3627
3628cfa_identifier_parameter_declarator_tuple: // CFA
3629 cfa_identifier_parameter_declarator_no_tuple
3630 | cfa_abstract_tuple
3631 | type_qualifier_list cfa_abstract_tuple
3632 { $$ = $2->addQualifiers( $1 ); }
3633 ;
3634
3635cfa_identifier_parameter_declarator_no_tuple: // CFA
3636 cfa_identifier_parameter_ptr
3637 | cfa_identifier_parameter_array
3638 ;
3639
3640cfa_identifier_parameter_ptr: // CFA
3641 // No SUE declaration in parameter list.
3642 ptrref_operator type_specifier_nobody
3643 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3644 | type_qualifier_list ptrref_operator type_specifier_nobody
3645 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3646 | ptrref_operator cfa_abstract_function
3647 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3648 | type_qualifier_list ptrref_operator cfa_abstract_function
3649 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3650 | ptrref_operator cfa_identifier_parameter_declarator_tuple
3651 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3652 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3653 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3654 ;
3655
3656cfa_identifier_parameter_array: // CFA
3657 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3658 // shift/reduce conflict with new-style empty (void) function return type.
3659 '[' ']' type_specifier_nobody
3660 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3661 | cfa_array_parameter_1st_dimension type_specifier_nobody
3662 { $$ = $2->addNewArray( $1 ); }
3663 | '[' ']' multi_array_dimension type_specifier_nobody
3664 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3665 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3666 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3667 | multi_array_dimension type_specifier_nobody
3668 { $$ = $2->addNewArray( $1 ); }
3669
3670 | '[' ']' cfa_identifier_parameter_ptr
3671 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3672 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3673 { $$ = $2->addNewArray( $1 ); }
3674 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3675 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3676 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3677 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3678 | multi_array_dimension cfa_identifier_parameter_ptr
3679 { $$ = $2->addNewArray( $1 ); }
3680 ;
3681
3682cfa_array_parameter_1st_dimension:
3683 '[' push type_qualifier_list '*' pop ']' // remaining C99
3684 { $$ = DeclarationNode::newVarArray( $3 ); }
3685 | '[' push type_qualifier_list assignment_expression pop ']'
3686 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3687 | '[' push declaration_qualifier_list assignment_expression pop ']'
3688 // declaration_qualifier_list must be used because of shift/reduce conflict with
3689 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3690 // appear in this context.
3691 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3692 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3693 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3694 ;
3695
3696// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3697// identifier to which the type applies, e.g.:
3698//
3699// [int] f( int ); // abstract variable parameter; no parameter name specified
3700// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
3701//
3702// These rules need LR(3):
3703//
3704// cfa_abstract_tuple identifier_or_type_name
3705// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
3706//
3707// since a function return type can be syntactically identical to a tuple type:
3708//
3709// [int, int] t;
3710// [int, int] f( int );
3711//
3712// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3713// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3714// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3715// duplicated when appearing with cfa_function_specifier.
3716
3717cfa_abstract_declarator_tuple: // CFA
3718 cfa_abstract_tuple
3719 | type_qualifier_list cfa_abstract_tuple
3720 { $$ = $2->addQualifiers( $1 ); }
3721 | cfa_abstract_declarator_no_tuple
3722 ;
3723
3724cfa_abstract_declarator_no_tuple: // CFA
3725 cfa_abstract_ptr
3726 | cfa_abstract_array
3727 ;
3728
3729cfa_abstract_ptr: // CFA
3730 ptrref_operator type_specifier
3731 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3732 | type_qualifier_list ptrref_operator type_specifier
3733 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3734 | ptrref_operator cfa_abstract_function
3735 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3736 | type_qualifier_list ptrref_operator cfa_abstract_function
3737 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3738 | ptrref_operator cfa_abstract_declarator_tuple
3739 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3740 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3741 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3742 ;
3743
3744cfa_abstract_array: // CFA
3745 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3746 // empty (void) function return type.
3747 '[' ']' type_specifier
3748 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3749 | '[' ']' multi_array_dimension type_specifier
3750 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3751 | multi_array_dimension type_specifier
3752 { $$ = $2->addNewArray( $1 ); }
3753 | '[' ']' cfa_abstract_ptr
3754 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3755 | '[' ']' multi_array_dimension cfa_abstract_ptr
3756 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3757 | multi_array_dimension cfa_abstract_ptr
3758 { $$ = $2->addNewArray( $1 ); }
3759 ;
3760
3761cfa_abstract_tuple: // CFA
3762 '[' push cfa_abstract_parameter_list pop ']'
3763 { $$ = DeclarationNode::newTuple( $3 ); }
3764 | '[' push type_specifier_nobody ELLIPSIS pop ']'
3765 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3766 | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
3767 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3768 ;
3769
3770cfa_abstract_function: // CFA
3771// '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
3772// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3773 cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
3774 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3775 | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
3776 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3777 ;
3778
3779// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3780// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3781//
3782// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3783// the declaration specifiers in a declaration is an obsolescent feature."
3784//
3785// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3786// prototype-format parameter type declarators) is an obsolescent feature."
3787//
3788// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3789// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3790
3791//************************* MISCELLANEOUS ********************************
3792
3793comma_opt: // redundant comma
3794 // empty
3795 | ','
3796 ;
3797
3798default_initializer_opt:
3799 // empty
3800 { $$ = nullptr; }
3801 | '=' assignment_expression
3802 { $$ = $2; }
3803 ;
3804
3805%%
3806
3807// ----end of grammar----
3808
3809// Local Variables: //
3810// mode: c++ //
3811// tab-width: 4 //
3812// compile-command: "make install" //
3813// End: //
Note: See TracBrowser for help on using the repository browser.