source: src/Parser/parser.yy@ 1d245ea

ADT ast-experimental
Last change on this file since 1d245ea was 1f771fc, checked in by Mugilan Ganesan <mganesan@…>, 3 years ago

Removed list initialization and simple assignment expression rules for functions. Split variable_type_redeclarator rule into variable_type_redeclarator and function_type_redeclarator rules

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