source: src/Parser/parser.yy@ 2a84d994

ADT ast-experimental
Last change on this file since 2a84d994 was c86b08d, checked in by caparsons <caparson@…>, 2 years ago

added support for the waituntil statement in the compiler

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