source: src/Parser/parser.yy@ 1633e04

ADT ast-experimental
Last change on this file since 1633e04 was d63aeba, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

print unimplemented error for forall in typedef

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