source: src/Parser/parser.yy@ e0f3bd2

Last change on this file since e0f3bd2 was a16f2b6, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

update SuperfluousDecl warning, update field_declaring rules, comment casts, update CFA shorthand attribute to @[...]

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