source: src/Parser/parser.yy@ ae42f2a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 ae42f2a was d63eeb0, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Makefile.in
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/Validate.cc
src/SynTree/Initializer.h
src/SynTree/ObjectDecl.cc
src/SynTree/Visitor.h
src/main.cc

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