source: src/Parser/parser.yy@ 5b993e0

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

parse new casts

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