source: src/Parser/parser.yy@ f8de4e4

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

fix conflict

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