source: src/Parser/parser.yy@ 35718a9

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum stuck-waitfor-destruct with_gc
Last change on this file since 35718a9 was 35718a9, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more push/pop updates

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