source: src/Parser/parser.yy@ d5ccbe9

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 with_gc
Last change on this file since d5ccbe9 was 6a276a0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update parser for new fallthrough semantics

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