source: src/Parser/parser.yy@ 515a037

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn 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 515a037 was 515a037, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Merge branch 'master' into shared_library

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