source: src/Parser/parser.yy@ 0c88135

Last change on this file since 0c88135 was c5f69fd, checked in by Peter A. Buhr <pabuhr@…>, 13 months ago

clean up naming of float-point types, and start to add new ARM floating-point types

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