source: src/Parser/parser.yy@ a1b41e3

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

Split up ParseNode.h so that headers match implementation. May have a bit less to include total because of it.

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