source: src/Parser/parser.yy@ c0714bf

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 c0714bf was 513e165, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

generalize types for encoded strings, and fold simple ExpressionNode build routines into parser

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