source: src/Parser/parser.yy@ 235114f

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 stuck-waitfor-destruct with_gc
Last change on this file since 235114f was a9b657a, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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