source: src/Parser/parser.yy@ b10c621c

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 resolv-new with_gc
Last change on this file since b10c621c was bd3d9e4, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

move constructor call from primary to postfix expression

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