source: src/Parser/parser.yy@ 478b1d0

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 478b1d0 was bac5158, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix conflict

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