source: src/Parser/parser.yy@ 6e8bd43

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6e8bd43 was 738e304, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

merge qualifier types and use the one in Type

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