source: src/Parser/parser.yy@ ae1d151

ADT ast-experimental pthread-emulation
Last change on this file since ae1d151 was 09f34a84, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Remove some of the warnings on the new clang

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