source: src/Parser/parser.yy@ e2f7bc3

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 e2f7bc3 was d48e529, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Begin to introduce support for yylloc in the parser and extend CodeLocation to include start column and end column/line number information

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