source: src/Parser/parser.yy@ b86d14c0

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

remove warnings for incorrect usage of SuperfluousElse and DeprecTraitSyntax in parser

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