source: src/Parser/parser.yy@ 366f5cd

Last change on this file since 366f5cd was 366f5cd, checked in by Peter A. Buhr <pabuhr@…>, 21 hours ago

differentiate between C _Alignof and gcc alignof

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