source: src/Parser/parser.yy@ c3acf0aa

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 c3acf0aa was cbce272, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Structure based exception handling.

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