source: src/Parser/parser.yy@ 6825167

ADT ast-experimental pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 6825167 was 6825167, checked in by caparsons <caparson@…>, 4 years ago

fixed loop else parse bug

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