source: src/Parser/parser.yy@ 044ae62

ADT
Last change on this file since 044ae62 was 044ae62, checked in by JiadaL <j82liang@…>, 2 years ago

Merge branch 'master' into ADT

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