source: src/Parser/parser.yy@ 4daf79f

ADT ast-experimental
Last change on this file since 4daf79f was 04c78215, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

change waituntil expression from cast_expression to comma_expression

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