source: src/Parser/parser.yy@ 8ef9c5e7

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 8ef9c5e7 was d3bc0ad, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add _Coroutine, _Monitor, _Thread keywords

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