source: src/Parser/parser.yy@ e5aba4a

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since e5aba4a was 8f6f3729, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

change typedef_name to type_name for non-terminal vtable

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