source: src/Parser/parser.yy@ 058f549

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

update error message printing and add column to parsing errors

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