source: src/Parser/parser.yy@ affb51b

ADT ast-experimental
Last change on this file since affb51b was b2ddaf3, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

fix parsing bug for attribute at the end of a distribution list

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