source: src/Parser/parser.yy@ a1f7064

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

Merge branch 'master' of plg2:software/cfa/cfa-cc

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