source: src/Parser/parser.yy@ 4fd45bc

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 4fd45bc was 1b8f13f0, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Missed file in merge

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