source: src/Parser/parser.yy@ 507d48d

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

fix typo in last push

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