source: src/Parser/parser.yy@ 98337569

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

add syntax for array-type for-control, update enum type error message

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