source: src/Parser/parser.yy@ fbb5bdd

Last change on this file since fbb5bdd was ecf3812, checked in by Andrew Beach <ajbeach@…>, 10 months ago

CastExpr reorganization and clean-up in Lvalue. I kept these from a fix that is not working.

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