source: src/Parser/parser.yy@ 77328d0

Last change on this file since 77328d0 was 12f1156, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

simplify grammar in a few places

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