source: src/Parser/parser.yy@ fbefc4d

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 fbefc4d was 9335ecc, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

update internal names for renamed files

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