source: src/Parser/parser.yy@ e307e12

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since e307e12 was e307e12, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

generalize aggregate data and control in grammar, add aggregate-control field-name, add inline aggregate-control

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