source: src/Parser/parser.yy@ 857a1c6

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

add keywords typeid and vtable, and parse new syntax for virtual tables

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