source: src/Parser/parser.yy@ f673c13c

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

add gcc auto_type to parsing side

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