source: src/Parser/parser.yy@ 6de9f4a

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 6de9f4a was 15697ff, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change processing of concatenated (juxtaposed) string and string encodings

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