source: src/Parser/parser.yy@ 1395748

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

Fix handling of GCC label address and computed goto

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