source: src/Parser/parser.yy@ 8da3cc4d

Last change on this file since 8da3cc4d was 4941716, checked in by Peter A. Buhr <pabuhr@…>, 14 months ago

preclude aggregate/enumeration type declaration in trait body

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