source: src/Parser/parser.yy@ b05beaa

ADT ast-experimental
Last change on this file since b05beaa was 6611177, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Clean-up in parser. ClauseNode rework, plus internal adjustments to reduce extra code and unchecked casts.

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