source: src/Parser/parser.yy@ 77de429

ADT ast-experimental
Last change on this file since 77de429 was c2b3243, checked in by JiadaL <j82liang@…>, 3 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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