source: src/Parser/parser.yy@ beeff61e

ADT ast-experimental
Last change on this file since beeff61e was bb7422a, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Translated parser to the new ast. This incuded a small fix in the resolver so larger expressions can be used in with statements and some updated tests. errors/declaration just is a formatting update. attributes now actually preserves more attributes (unknown if all versions work).

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