source: src/Parser/parser.yy@ 80c72a7

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 80c72a7 was 4cc585b, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt to normalize push/pop, and fix K&R parsing bug

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