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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 8c97ee7 was 307a732, checked in by Andrew Beach <ajbeach@…>, 8 years ago

The exception handling code compilers and translates, but the translation crashes.

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