source: src/Parser/parser.yy@ 5c98a25

ADT ast-experimental pthread-emulation
Last change on this file since 5c98a25 was d78c238, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

update for-control with explicit type declarations

  • Property mode set to 100644
File size: 156.2 KB
Line 
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//
7// parser.yy --
8//
9// Author : Peter A. Buhr
10// Created On : Sat Sep 1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Aug 8 15:45:04 2022
13// Update Count : 5574
14//
15
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.
22
23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28// designation with '=' (use ':' instead)
29//
30// This incompatibility is discussed in detail before the "designation" grammar rule. Most of the syntactic extensions
31// from ANSI90 to ANSI11 C are marked with the comment "C99/C11".
32
33// This grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions. All of the
34// syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall (CFA), which
35// fixes several of C's outstanding problems and extends C with many modern language concepts. All of the syntactic
36// extensions for CFA C are marked with the comment "CFA".
37
38%{
39#define YYDEBUG_LEXER_TEXT( yylval ) // lexer loads this up each time
40#define YYDEBUG 1 // get the pretty debugging code to compile
41#define YYERROR_VERBOSE // more information in syntax errors
42
43#undef __GNUC_MINOR__
44
45#include <cstdio>
46#include <stack>
47using namespace std;
48
49#include "SynTree/Declaration.h"
50#include "ParseNode.h"
51#include "TypedefTable.h"
52#include "TypeData.h"
53#include "SynTree/LinkageSpec.h"
54#include "Common/SemanticError.h" // error_str
55#include "Common/utility.h" // for maybeMoveBuild, maybeBuild, CodeLo...
56
57#include "SynTree/Attribute.h" // for Attribute
58
59// lex uses __null in a boolean context, it's fine.
60//#pragma GCC diagnostic ignored "-Wparentheses-equality"
61
62extern DeclarationNode * parseTree;
63extern LinkageSpec::Spec linkage;
64extern TypedefTable typedefTable;
65
66stack<LinkageSpec::Spec> linkageStack;
67
68bool appendStr( string & to, string & from ) {
69 // 1. Multiple strings are concatenated into a single string but not combined internally. The reason is that
70 // "\x12" "3" is treated as 2 characters versus 1 because "escape sequences are converted into single members of
71 // the execution character set just prior to adjacent string literal concatenation" (C11, Section 6.4.5-8). It is
72 // easier to let the C compiler handle this case.
73 //
74 // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found
75 // without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match,
76 // e.g., u"a" U"b" L"c" is disallowed.
77
78 if ( from[0] != '"' ) { // encoding ?
79 if ( to[0] != '"' ) { // encoding ?
80 if ( to[0] != from[0] || to[1] != from[1] ) { // different encodings ?
81 yyerror( "non-matching string encodings for string-literal concatenation" );
82 return false; // parse error, must call YYERROR in action
83 } else if ( from[1] == '8' ) {
84 from.erase( 0, 1 ); // remove 2nd encoding
85 } // if
86 } else {
87 if ( from[1] == '8' ) { // move encoding to start
88 to = "u8" + to;
89 from.erase( 0, 1 ); // remove 2nd encoding
90 } else {
91 to = from[0] + to;
92 } // if
93 } // if
94 from.erase( 0, 1 ); // remove 2nd encoding
95 } // if
96 to += " " + from; // concatenated into single string
97 return true;
98} // appendStr
99
100DeclarationNode * distAttr( DeclarationNode * typeSpec, DeclarationNode * declList ) {
101 // distribute declaration_specifier across all declared variables, e.g., static, const, but not __attribute__.
102 assert( declList );
103// printf( "distAttr1 typeSpec %p\n", typeSpec ); typeSpec->print( std::cout );
104 DeclarationNode * cur = declList, * cl = (new DeclarationNode)->addType( typeSpec );
105// printf( "distAttr2 cl %p\n", cl ); cl->type->print( std::cout );
106// cl->type->aggregate.name = cl->type->aggInst.aggregate->aggregate.name;
107
108 for ( cur = dynamic_cast<DeclarationNode *>( cur->get_next() ); cur != nullptr; cur = dynamic_cast<DeclarationNode *>( cur->get_next() ) ) {
109 cl->cloneBaseType( cur );
110 } // for
111 declList->addType( cl );
112// printf( "distAttr3 declList %p\n", declList ); declList->print( std::cout, 0 );
113 return declList;
114} // distAttr
115
116void distExt( DeclarationNode * declaration ) {
117 // distribute EXTENSION across all declarations
118 for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
119 iter->set_extension( true );
120 } // for
121} // distExt
122
123void distInl( DeclarationNode * declaration ) {
124 // distribute INLINE across all declarations
125 for ( DeclarationNode *iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
126 iter->set_inLine( true );
127 } // for
128} // distInl
129
130void distQual( DeclarationNode * declaration, DeclarationNode * qualifiers ) {
131 // distribute qualifiers across all non-variable declarations in a distribution statemement
132 for ( DeclarationNode * iter = declaration; iter != nullptr; iter = (DeclarationNode *)iter->get_next() ) {
133 // SKULLDUGGERY: Distributions are parsed inside out, so qualifiers are added to declarations inside out. Since
134 // addQualifiers appends to the back of the list, the forall clauses are in the wrong order (right to left). To
135 // get the qualifiers in the correct order and still use addQualifiers (otherwise, 90% of addQualifiers has to
136 // be copied to add to front), the appropriate forall pointers are interchanged before calling addQualifiers.
137 DeclarationNode * clone = qualifiers->clone();
138 if ( qualifiers->type ) { // forall clause ? (handles SC)
139 if ( iter->type->kind == TypeData::Aggregate ) { // struct/union ?
140 swap( clone->type->forall, iter->type->aggregate.params );
141 iter->addQualifiers( clone );
142 } else if ( iter->type->kind == TypeData::AggregateInst && iter->type->aggInst.aggregate->aggregate.body ) { // struct/union ?
143 // Create temporary node to hold aggregate, call addQualifiers as above, then put nodes back together.
144 DeclarationNode newnode;
145 swap( newnode.type, iter->type->aggInst.aggregate );
146 swap( clone->type->forall, newnode.type->aggregate.params );
147 newnode.addQualifiers( clone );
148 swap( newnode.type, iter->type->aggInst.aggregate );
149 } else if ( iter->type->kind == TypeData::Function ) { // routines ?
150 swap( clone->type->forall, iter->type->forall );
151 iter->addQualifiers( clone );
152 } // if
153 } else { // just SC qualifiers
154 iter->addQualifiers( clone );
155 } // if
156 } // for
157 delete qualifiers;
158} // distQual
159
160// There is an ambiguity for inline generic-routine return-types and generic routines.
161// forall( otype T ) struct S { int i; } bar( T ) {}
162// Does the forall bind to the struct or the routine, and how would it be possible to explicitly specify the binding.
163// forall( otype T ) struct S { int T; } forall( otype W ) bar( W ) {}
164// Currently, the forall is associated with the routine, and the generic type has to be separately defined:
165// forall( otype T ) struct S { int T; };
166// forall( otype W ) bar( W ) {}
167
168void rebindForall( DeclarationNode * declSpec, DeclarationNode * funcDecl ) {
169 if ( declSpec->type->kind == TypeData::Aggregate ) { // ignore aggregate definition
170 funcDecl->type->forall = declSpec->type->aggregate.params; // move forall from aggregate to function type
171 declSpec->type->aggregate.params = nullptr;
172 } // if
173} // rebindForall
174
175string * build_postfix_name( string * name ) {
176 *name = string("__postfix_func_") + *name;
177 return name;
178} // build_postfix_name
179
180DeclarationNode * fieldDecl( DeclarationNode * typeSpec, DeclarationNode * fieldList ) {
181 if ( ! fieldList ) { // field declarator ?
182 if ( ! ( typeSpec->type && (typeSpec->type->kind == TypeData::Aggregate || typeSpec->type->kind == TypeData::Enum) ) ) {
183 stringstream ss;
184 // printf( "fieldDecl1 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
185 SemanticWarning( yylloc, Warning::SuperfluousDecl, ss.str().c_str() );
186 return nullptr;
187 } // if
188 // printf( "fieldDecl2 typeSpec %p\n", typeSpec ); typeSpec->type->print( std::cout );
189 fieldList = DeclarationNode::newName( nullptr );
190 } // if
191// return distAttr( typeSpec, fieldList ); // mark all fields in list
192
193 // printf( "fieldDecl3 typeSpec %p\n", typeSpec ); typeSpec->print( std::cout, 0 );
194 DeclarationNode * temp = distAttr( typeSpec, fieldList ); // mark all fields in list
195 // printf( "fieldDecl4 temp %p\n", temp ); temp->print( std::cout, 0 );
196 return temp;
197} // fieldDecl
198
199#define NEW_ZERO new ExpressionNode( build_constantInteger( *new string( "0" ) ) )
200#define NEW_ONE new ExpressionNode( build_constantInteger( *new string( "1" ) ) )
201
202ForCtrl * forCtrl( DeclarationNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
203 if ( index->initializer ) {
204 SemanticError( yylloc, "Direct initialization disallowed. Use instead: type var; initialization ~ comparison ~ increment." );
205 } // if
206 if ( index->next ) {
207 SemanticError( yylloc, "Multiple loop indexes disallowed in for-loop declaration." );
208 } // if
209 return new ForCtrl( index->addInitializer( new InitializerNode( start ) ),
210 // NULL comp/inc => leave blank
211 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index->name ) ) ), comp ) ) : nullptr,
212 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
213 OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index->name ) ) ), inc ) ) : nullptr );
214} // forCtrl
215
216ForCtrl * forCtrl( ExpressionNode * type, string * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
217 ConstantExpr * constant = dynamic_cast<ConstantExpr *>(type->expr.get());
218 if ( constant && (constant->get_constant()->get_value() == "0" || constant->get_constant()->get_value() == "1") ) {
219 type = new ExpressionNode( new CastExpr( maybeMoveBuild<Expression>(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
220 } // if
221// type = new ExpressionNode( build_func( new ExpressionNode( build_varref( new string( "__for_control_index_constraints__" ) ) ), type ) );
222 return new ForCtrl(
223 distAttr( DeclarationNode::newTypeof( type, true ), DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) ),
224 // NULL comp/inc => leave blank
225 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index ) ) ), comp ) ) : nullptr,
226 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
227 OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index ) ) ), inc ) ) : nullptr );
228} // forCtrl
229
230ForCtrl * forCtrl( ExpressionNode * type, ExpressionNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
231 if ( NameExpr * identifier = dynamic_cast<NameExpr *>(index->expr.get()) ) {
232 return forCtrl( type, new string( identifier->name ), start, compop, comp, inc );
233 } else if ( CommaExpr * commaExpr = dynamic_cast<CommaExpr *>(index->expr.get()) ) {
234 if ( NameExpr * identifier = dynamic_cast<NameExpr *>(commaExpr->arg1 ) ) {
235 return forCtrl( type, new string( identifier->name ), start, compop, comp, inc );
236 } else {
237 SemanticError( yylloc, "Expression disallowed. Only loop-index name allowed." ); return nullptr;
238 } // if
239 } else {
240 SemanticError( yylloc, "Expression disallowed. Only loop-index name allowed." ); return nullptr;
241 } // if
242} // forCtrl
243
244bool forall = false; // aggregate have one or more forall qualifiers ?
245
246// https://www.gnu.org/software/bison/manual/bison.html#Location-Type
247#define YYLLOC_DEFAULT(Cur, Rhs, N) \
248if ( N ) { \
249 (Cur).first_line = YYRHSLOC( Rhs, 1 ).first_line; \
250 (Cur).first_column = YYRHSLOC( Rhs, 1 ).first_column; \
251 (Cur).last_line = YYRHSLOC( Rhs, N ).last_line; \
252 (Cur).last_column = YYRHSLOC( Rhs, N ).last_column; \
253 (Cur).filename = YYRHSLOC( Rhs, 1 ).filename; \
254} else { \
255 (Cur).first_line = (Cur).last_line = YYRHSLOC( Rhs, 0 ).last_line; \
256 (Cur).first_column = (Cur).last_column = YYRHSLOC( Rhs, 0 ).last_column; \
257 (Cur).filename = YYRHSLOC( Rhs, 0 ).filename; \
258}
259%}
260
261%define parse.error verbose
262
263// Types declaration for productions
264%union {
265 Token tok;
266 ParseNode * pn;
267 ExpressionNode * en;
268 DeclarationNode * decl;
269 AggregateDecl::Aggregate aggKey;
270 TypeDecl::Kind tclass;
271 StatementNode * sn;
272 WaitForStmt * wfs;
273 Expression * constant;
274 CondCtl * ifctl;
275 ForCtrl * fctl;
276 enum OperKinds compop;
277 LabelNode * label;
278 InitializerNode * in;
279 OperKinds op;
280 std::string * str;
281 bool flag;
282 CatchStmt::Kind catch_kind;
283 GenericExpr * genexpr;
284}
285
286//************************* TERMINAL TOKENS ********************************
287
288// keywords
289%token TYPEDEF
290%token EXTERN STATIC AUTO REGISTER
291%token THREADLOCAL // C11
292%token INLINE FORTRAN // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
293%token NORETURN // C11
294%token CONST VOLATILE
295%token RESTRICT // C99
296%token ATOMIC // C11
297%token FORALL MUTEX VIRTUAL VTABLE COERCE // CFA
298%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
299%token BOOL COMPLEX IMAGINARY // C99
300%token INT128 UINT128 uuFLOAT80 uuFLOAT128 // GCC
301%token uFLOAT16 uFLOAT32 uFLOAT32X uFLOAT64 uFLOAT64X uFLOAT128 // GCC
302%token DECIMAL32 DECIMAL64 DECIMAL128 // GCC
303%token ZERO_T ONE_T // CFA
304%token SIZEOF TYPEOF VALIST AUTO_TYPE // GCC
305%token OFFSETOF BASETYPEOF TYPEID // CFA
306%token ENUM STRUCT UNION
307%token EXCEPTION // CFA
308%token GENERATOR COROUTINE MONITOR THREAD // CFA
309%token OTYPE FTYPE DTYPE TTYPE TRAIT // CFA
310// %token RESUME // CFA
311%token LABEL // GCC
312%token SUSPEND // CFA
313%token ATTRIBUTE EXTENSION // GCC
314%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
315%token CHOOSE FALLTHRU FALLTHROUGH WITH WHEN WAITFOR // CFA
316%token DISABLE ENABLE TRY THROW THROWRESUME AT // CFA
317%token ASM // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
318%token ALIGNAS ALIGNOF GENERIC STATICASSERT // C11
319
320// names and constants: lexer differentiates between identifier and typedef names
321%token<tok> IDENTIFIER QUOTED_IDENTIFIER TYPEDIMname TYPEDEFname TYPEGENname
322%token<tok> TIMEOUT WOR CATCH RECOVER CATCHRESUME FIXUP FINALLY // CFA
323%token<tok> INTEGERconstant CHARACTERconstant STRINGliteral
324%token<tok> DIRECTIVE
325// Floating point constant is broken into three kinds of tokens because of the ambiguity with tuple indexing and
326// overloading constants 0/1, e.g., x.1 is lexed as (x)(.1), where (.1) is a factional constant, but is semantically
327// converted into the tuple index (.)(1). e.g., 3.x
328%token<tok> FLOATING_DECIMALconstant FLOATING_FRACTIONconstant FLOATINGconstant
329
330// multi-character operators
331%token ARROW // ->
332%token ICR DECR // ++ --
333%token LS RS // << >>
334%token LE GE EQ NE // <= >= == !=
335%token ANDAND OROR // && ||
336%token ELLIPSIS // ...
337
338%token EXPassign MULTassign DIVassign MODassign // \= *= /= %=
339%token PLUSassign MINUSassign // += -=
340%token LSassign RSassign // <<= >>=
341%token ANDassign ERassign ORassign // &= ^= |=
342
343%token ErangeUpEq ErangeDown ErangeDownEq // ~= -~ -~=
344%token ATassign // @=
345
346%type<tok> identifier identifier_at identifier_or_type_name attr_name
347%type<tok> quasi_keyword
348%type<constant> string_literal
349%type<str> string_literal_list
350
351// expressions
352%type<en> constant
353%type<en> tuple tuple_expression_list
354%type<op> ptrref_operator unary_operator assignment_operator simple_assignment_operator compound_assignment_operator
355%type<en> primary_expression postfix_expression unary_expression
356%type<en> cast_expression_list cast_expression exponential_expression multiplicative_expression additive_expression
357%type<en> shift_expression relational_expression equality_expression
358%type<en> AND_expression exclusive_OR_expression inclusive_OR_expression
359%type<en> logical_AND_expression logical_OR_expression
360%type<en> conditional_expression constant_expression assignment_expression assignment_expression_opt
361%type<en> comma_expression comma_expression_opt
362%type<en> argument_expression_list_opt argument_expression_list argument_expression default_initializer_opt
363%type<ifctl> conditional_declaration
364%type<fctl> for_control_expression for_control_expression_list
365%type<compop> updown updowneq downupdowneq
366%type<en> subrange
367%type<decl> asm_name_opt
368%type<en> asm_operands_opt asm_operands_list asm_operand
369%type<label> label_list
370%type<en> asm_clobbers_list_opt
371%type<flag> asm_volatile_opt
372%type<en> handler_predicate_opt
373%type<genexpr> generic_association generic_assoc_list
374
375// statements
376%type<sn> statement labeled_statement compound_statement
377%type<sn> statement_decl statement_decl_list statement_list_nodecl
378%type<sn> selection_statement if_statement
379%type<sn> switch_clause_list_opt switch_clause_list
380%type<en> case_value
381%type<sn> case_clause case_value_list case_label case_label_list
382%type<sn> iteration_statement jump_statement
383%type<sn> expression_statement asm_statement
384%type<sn> with_statement
385%type<en> with_clause_opt
386%type<sn> exception_statement handler_clause finally_clause
387%type<catch_kind> handler_key
388%type<sn> mutex_statement
389%type<en> when_clause when_clause_opt waitfor timeout
390%type<sn> waitfor_statement
391%type<wfs> waitfor_clause
392
393// declarations
394%type<decl> abstract_declarator abstract_ptr abstract_array abstract_function array_dimension multi_array_dimension
395%type<decl> abstract_parameter_declarator abstract_parameter_ptr abstract_parameter_array abstract_parameter_function array_parameter_dimension array_parameter_1st_dimension
396%type<decl> abstract_parameter_declaration
397
398%type<aggKey> aggregate_key aggregate_data aggregate_control
399%type<decl> aggregate_type aggregate_type_nobody
400
401%type<decl> assertion assertion_list assertion_list_opt
402
403%type<en> bit_subrange_size_opt bit_subrange_size
404
405%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type indirect_type
406%type<decl> vtable vtable_opt default_opt
407
408%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
409
410%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
411%type<decl> declaration_specifier declaration_specifier_nobody declarator declaring_list
412
413%type<decl> elaborated_type elaborated_type_nobody
414
415%type<decl> enumerator_list enum_type enum_type_nobody
416%type<in> enumerator_value_opt
417
418%type<decl> external_definition external_definition_list external_definition_list_opt
419
420%type<decl> exception_declaration
421
422%type<decl> field_declaration_list_opt field_declaration field_declaring_list_opt field_declarator field_abstract_list_opt field_abstract
423%type<en> field field_name_list field_name fraction_constants_opt
424
425%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
426
427%type<decl> identifier_parameter_declarator identifier_parameter_ptr identifier_parameter_array identifier_parameter_function
428%type<decl> identifier_list
429
430%type<decl> cfa_abstract_array cfa_abstract_declarator_no_tuple cfa_abstract_declarator_tuple
431%type<decl> cfa_abstract_function cfa_abstract_parameter_declaration cfa_abstract_parameter_list
432%type<decl> cfa_abstract_ptr cfa_abstract_tuple
433
434%type<decl> cfa_array_parameter_1st_dimension
435
436%type<decl> cfa_trait_declaring_list cfa_declaration cfa_field_declaring_list cfa_field_abstract_list
437%type<decl> cfa_function_declaration cfa_function_return cfa_function_specifier
438
439%type<decl> cfa_identifier_parameter_array cfa_identifier_parameter_declarator_no_tuple
440%type<decl> cfa_identifier_parameter_declarator_tuple cfa_identifier_parameter_ptr
441
442%type<decl> cfa_parameter_declaration cfa_parameter_list cfa_parameter_ellipsis_list_opt
443
444%type<decl> cfa_typedef_declaration cfa_variable_declaration cfa_variable_specifier
445
446%type<decl> c_declaration static_assert
447%type<decl> KR_function_declarator KR_function_no_ptr KR_function_ptr KR_function_array
448%type<decl> KR_parameter_list KR_parameter_list_opt
449
450%type<decl> parameter_declaration parameter_list parameter_type_list_opt
451
452%type<decl> paren_identifier paren_type
453
454%type<decl> storage_class storage_class_list
455
456%type<decl> sue_declaration_specifier sue_declaration_specifier_nobody sue_type_specifier sue_type_specifier_nobody
457
458%type<tclass> type_class new_type_class
459%type<decl> type_declarator type_declarator_name type_declaring_list
460
461%type<decl> type_declaration_specifier type_type_specifier type_name typegen_name
462%type<decl> typedef_name typedef_declaration typedef_expression
463
464%type<decl> variable_type_redeclarator type_ptr type_array type_function
465
466%type<decl> type_parameter_redeclarator type_parameter_ptr type_parameter_array type_parameter_function
467
468%type<decl> type type_no_function
469%type<decl> type_parameter type_parameter_list type_initializer_opt
470
471%type<en> type_parameters_opt type_list
472
473%type<decl> type_qualifier type_qualifier_name forall type_qualifier_list_opt type_qualifier_list
474%type<decl> type_specifier type_specifier_nobody
475
476%type<decl> variable_declarator variable_ptr variable_array variable_function
477%type<decl> variable_abstract_declarator variable_abstract_ptr variable_abstract_array variable_abstract_function
478
479%type<decl> attribute_list_opt attribute_list attribute attribute_name_list attribute_name
480
481// initializers
482%type<in> initializer initializer_list_opt initializer_opt
483
484// designators
485%type<en> designator designator_list designation
486
487
488// Handle shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string is ambiguous:
489// .---------. matches IF '(' comma_expression ')' statement . (reduce)
490// if ( C ) S1 else S2
491// `-----------------' matches IF '(' comma_expression ')' statement . (shift) ELSE statement */
492// Similar issues exit with the waitfor statement.
493
494// Order of these lines matters (low-to-high precedence). THEN is left associative over WOR/TIMEOUT/ELSE, WOR is left
495// associative over TIMEOUT/ELSE, and TIMEOUT is left associative over ELSE.
496%precedence THEN // rule precedence for IF/WAITFOR statement
497%precedence WOR // token precedence for start of WOR in WAITFOR statement
498%precedence TIMEOUT // token precedence for start of TIMEOUT in WAITFOR statement
499%precedence CATCH // token precedence for start of TIMEOUT in WAITFOR statement
500%precedence RECOVER // token precedence for start of TIMEOUT in WAITFOR statement
501%precedence CATCHRESUME // token precedence for start of TIMEOUT in WAITFOR statement
502%precedence FIXUP // token precedence for start of TIMEOUT in WAITFOR statement
503%precedence FINALLY // token precedence for start of TIMEOUT in WAITFOR statement
504%precedence ELSE // token precedence for start of else clause in IF/WAITFOR statement
505
506
507// Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous:
508// forall( otype T ) struct Foo { T v; };
509// .-----. matches pointer to function returning a generic (which is impossible without a type)
510// Foo ( *fp )( int );
511// `---' matches start of TYPEGENname '('
512// must be:
513// Foo( int ) ( *fp )( int );
514// The same problem occurs here:
515// forall( otype T ) struct Foo { T v; } ( *fp )( int );
516// must be:
517// forall( otype T ) struct Foo { T v; } ( int ) ( *fp )( int );
518
519// Order of these lines matters (low-to-high precedence).
520%precedence TYPEGENname
521%precedence '}'
522%precedence '('
523
524// %precedence RESUME
525// %precedence '{'
526// %precedence ')'
527
528%locations // support location tracking for error messages
529
530%start translation_unit // parse-tree root
531
532%%
533//************************* Namespace Management ********************************
534
535// The C grammar is not context free because it relies on the distinct terminal symbols "identifier" and "TYPEDEFname",
536// which are lexically identical.
537//
538// typedef int foo; // identifier foo must now be scanned as TYPEDEFname
539// foo f; // to allow it to appear in this context
540//
541// While it may be possible to write a purely context-free grammar, such a grammar would obscure the relationship
542// between syntactic and semantic constructs. Cforall compounds this problem by introducing type names local to the
543// scope of a declaration (for instance, those introduced through "forall" qualifiers), and by introducing "type
544// generators" -- parameterized types. This latter type name creates a third class of identifiers, "TYPEGENname", which
545// must be distinguished by the lexical scanner.
546//
547// Since the scanner cannot distinguish among the different classes of identifiers without some context information,
548// there is a type table (typedefTable), which holds type names and identifiers that override type names, for each named
549// scope. During parsing, semantic actions update the type table by adding new identifiers in the current scope. For
550// each context that introduces a name scope, a new level is created in the type table and that level is popped on
551// exiting the scope. Since type names can be local to a particular declaration, each declaration is itself a scope.
552// This requires distinguishing between type names that are local to the current declaration scope and those that
553// persist past the end of the declaration (i.e., names defined in "typedef" or "otype" declarations).
554//
555// The non-terminals "push" and "pop" denote the opening and closing of named scopes. Every push has a matching pop in
556// the production rule. There are multiple lists of declarations, where each declaration is a named scope, so pop/push
557// around the list separator.
558//
559// int f( forall(T) T (*f1) T , forall( S ) S (*f2)( S ) );
560// push pop push pop
561
562push:
563 { typedefTable.enterScope(); }
564 ;
565
566pop:
567 { typedefTable.leaveScope(); }
568 ;
569
570//************************* CONSTANTS ********************************
571
572constant:
573 // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
574 INTEGERconstant { $$ = new ExpressionNode( build_constantInteger( *$1 ) ); }
575 | FLOATING_DECIMALconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
576 | FLOATING_FRACTIONconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
577 | FLOATINGconstant { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
578 | CHARACTERconstant { $$ = new ExpressionNode( build_constantChar( *$1 ) ); }
579 ;
580
581quasi_keyword: // CFA
582 TIMEOUT
583 | WOR
584 | CATCH
585 | RECOVER
586 | CATCHRESUME
587 | FIXUP
588 | FINALLY
589 ;
590
591identifier:
592 IDENTIFIER
593 | quasi_keyword
594 ;
595
596identifier_at:
597 identifier
598 | '@' // CFA
599 { Token tok = { new string( DeclarationNode::anonymous.newName() ), yylval.tok.loc }; $$ = tok; }
600 ;
601
602string_literal:
603 string_literal_list { $$ = build_constantStr( *$1 ); }
604 ;
605
606string_literal_list: // juxtaposed strings are concatenated
607 STRINGliteral { $$ = $1; } // conversion from tok to str
608 | string_literal_list STRINGliteral
609 {
610 if ( ! appendStr( *$1, *$2 ) ) YYERROR; // append 2nd juxtaposed string to 1st
611 delete $2; // allocated by lexer
612 $$ = $1; // conversion from tok to str
613 }
614 ;
615
616//************************* EXPRESSIONS ********************************
617
618primary_expression:
619 IDENTIFIER // typedef name cannot be used as a variable name
620 { $$ = new ExpressionNode( build_varref( $1 ) ); }
621 | quasi_keyword
622 { $$ = new ExpressionNode( build_varref( $1 ) ); }
623 | TYPEDIMname // CFA, generic length argument
624 // { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( DeclarationNode::newFromTypedef( $1 ) ) ) ); }
625 // { $$ = new ExpressionNode( build_varref( $1 ) ); }
626 { $$ = new ExpressionNode( build_dimensionref( $1 ) ); }
627 | tuple
628 | '(' comma_expression ')'
629 { $$ = $2; }
630 | '(' compound_statement ')' // GCC, lambda expression
631 { $$ = new ExpressionNode( new StmtExpr( dynamic_cast<CompoundStmt *>(maybeMoveBuild<Statement>($2) ) ) ); }
632 | type_name '.' identifier // CFA, nested type
633 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
634 | type_name '.' '[' field_name_list ']' // CFA, nested type / tuple field selector
635 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }
636 | GENERIC '(' assignment_expression ',' generic_assoc_list ')' // C11
637 {
638 // add the missing control expression to the GenericExpr and return it
639 $5->control = maybeMoveBuild<Expression>( $3 );
640 $$ = new ExpressionNode( $5 );
641 }
642 // | RESUME '(' comma_expression ')'
643 // { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
644 // | RESUME '(' comma_expression ')' compound_statement
645 // { SemanticError( yylloc, "Resume expression is currently unimplemented." ); $$ = nullptr; }
646 | IDENTIFIER IDENTIFIER // syntax error
647 {
648 SemanticError( yylloc, ::toString( "Adjacent identifiers are not meaningful in an expression. "
649 "Possible problem is identifier \"", *$1.str,
650 "\" is a misspelled typename or an incorrectly specified type name, "
651 "e.g., missing generic parameter or missing struct/union/enum before typename." ) );
652 $$ = nullptr;
653 }
654 | IDENTIFIER direct_type // syntax error
655 {
656 SemanticError( yylloc, ::toString( "Identifier \"", *$1.str, "\" cannot appear before a type. "
657 "Possible problem is misspelled storage or CV qualifier." ) );
658 $$ = nullptr;
659 }
660 ;
661
662generic_assoc_list: // C11
663 generic_association
664 | generic_assoc_list ',' generic_association
665 {
666 // steal the association node from the singleton and delete the wrapper
667 $1->associations.splice($1->associations.end(), $3->associations);
668 delete $3;
669 $$ = $1;
670 }
671 ;
672
673generic_association: // C11
674 type_no_function ':' assignment_expression
675 {
676 // create a GenericExpr wrapper with one association pair
677 $$ = new GenericExpr( nullptr, { { maybeMoveBuildType($1), maybeMoveBuild<Expression>( $3 ) } } );
678 }
679 | DEFAULT ':' assignment_expression
680 { $$ = new GenericExpr( nullptr, { { maybeMoveBuild<Expression>( $3 ) } } ); }
681 ;
682
683postfix_expression:
684 primary_expression
685 | postfix_expression '[' assignment_expression ',' tuple_expression_list ']'
686 // Historic, transitional: Disallow commas in subscripts.
687 // Switching to this behaviour may help check if a C compatibilty case uses comma-exprs in subscripts.
688 // Current: Commas in subscripts make tuples.
689 { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, new ExpressionNode( build_tuple( (ExpressionNode *)($3->set_last( $5 ) ) )) ) ); }
690 | postfix_expression '[' assignment_expression ']'
691 // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
692 // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
693 // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
694 // equivalent to the old x[i,j].
695 { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, $3 ) ); }
696 | constant '[' assignment_expression ']' // 3[a], 'a'[a], 3.5[a]
697 { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, $1, $3 ) ); }
698 | string_literal '[' assignment_expression ']' // "abc"[3], 3["abc"]
699 { $$ = new ExpressionNode( build_binary_val( OperKinds::Index, new ExpressionNode( $1 ), $3 ) ); }
700 | postfix_expression '{' argument_expression_list_opt '}' // CFA, constructor call
701 {
702 Token fn;
703 fn.str = new std::string( "?{}" ); // location undefined - use location of '{'?
704 $$ = new ExpressionNode( new ConstructorExpr( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $1 )->set_last( $3 ) ) ) );
705 }
706 | postfix_expression '(' argument_expression_list_opt ')'
707 { $$ = new ExpressionNode( build_func( $1, $3 ) ); }
708 | postfix_expression '`' identifier // CFA, postfix call
709 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( build_postfix_name( $3 ) ) ), $1 ) ); }
710 | constant '`' identifier // CFA, postfix call
711 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( build_postfix_name( $3 ) ) ), $1 ) ); }
712 | string_literal '`' identifier // CFA, postfix call
713 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( build_postfix_name( $3 ) ) ), new ExpressionNode( $1 ) ) ); }
714 | postfix_expression '.' identifier
715 { $$ = new ExpressionNode( build_fieldSel( $1, build_varref( $3 ) ) ); }
716 | postfix_expression '.' INTEGERconstant // CFA, tuple index
717 { $$ = new ExpressionNode( build_fieldSel( $1, build_constantInteger( *$3 ) ) ); }
718 | postfix_expression FLOATING_FRACTIONconstant // CFA, tuple index
719 { $$ = new ExpressionNode( build_fieldSel( $1, build_field_name_FLOATING_FRACTIONconstant( *$2 ) ) ); }
720 | postfix_expression '.' '[' field_name_list ']' // CFA, tuple field selector
721 { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $4 ) ) ); }
722 | postfix_expression '.' aggregate_control
723 { $$ = new ExpressionNode( build_keyword_cast( $3, $1 ) ); }
724 | postfix_expression ARROW identifier
725 { $$ = new ExpressionNode( build_pfieldSel( $1, build_varref( $3 ) ) ); }
726 | postfix_expression ARROW INTEGERconstant // CFA, tuple index
727 { $$ = new ExpressionNode( build_pfieldSel( $1, build_constantInteger( *$3 ) ) ); }
728 | postfix_expression ARROW '[' field_name_list ']' // CFA, tuple field selector
729 { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $4 ) ) ); }
730 | postfix_expression ICR
731 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::IncrPost, $1 ) ); }
732 | postfix_expression DECR
733 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::DecrPost, $1 ) ); }
734 | '(' type_no_function ')' '{' initializer_list_opt comma_opt '}' // C99, compound-literal
735 { $$ = new ExpressionNode( build_compoundLiteral( $2, new InitializerNode( $5, true ) ) ); }
736 | '(' type_no_function ')' '@' '{' initializer_list_opt comma_opt '}' // CFA, explicit C compound-literal
737 { $$ = new ExpressionNode( build_compoundLiteral( $2, (new InitializerNode( $6, true ))->set_maybeConstructed( false ) ) ); }
738 | '^' primary_expression '{' argument_expression_list_opt '}' // CFA, destructor call
739 {
740 Token fn;
741 fn.str = new string( "^?{}" ); // location undefined
742 $$ = new ExpressionNode( build_func( new ExpressionNode( build_varref( fn ) ), (ExpressionNode *)( $2 )->set_last( $4 ) ) );
743 }
744 ;
745
746argument_expression_list_opt:
747 // empty
748 { $$ = nullptr; }
749 | argument_expression_list
750 ;
751
752argument_expression_list:
753 argument_expression
754 | argument_expression_list_opt ',' argument_expression
755 { $$ = (ExpressionNode *)($1->set_last( $3 )); }
756 ;
757
758argument_expression:
759 '@' // CFA, default parameter
760 { SemanticError( yylloc, "Default parameter for argument is currently unimplemented." ); $$ = nullptr; }
761 // { $$ = new ExpressionNode( build_constantInteger( *new string( "2" ) ) ); }
762 | assignment_expression
763 ;
764
765field_name_list: // CFA, tuple field selector
766 field
767 | field_name_list ',' field { $$ = (ExpressionNode *)($1->set_last( $3 )); }
768 ;
769
770field: // CFA, tuple field selector
771 field_name
772 | FLOATING_DECIMALconstant field
773 { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), maybeMoveBuild<Expression>( $2 ) ) ); }
774 | FLOATING_DECIMALconstant '[' field_name_list ']'
775 { $$ = new ExpressionNode( build_fieldSel( new ExpressionNode( build_field_name_FLOATING_DECIMALconstant( *$1 ) ), build_tuple( $3 ) ) ); }
776 | field_name '.' field
777 { $$ = new ExpressionNode( build_fieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
778 | field_name '.' '[' field_name_list ']'
779 { $$ = new ExpressionNode( build_fieldSel( $1, build_tuple( $4 ) ) ); }
780 | field_name ARROW field
781 { $$ = new ExpressionNode( build_pfieldSel( $1, maybeMoveBuild<Expression>( $3 ) ) ); }
782 | field_name ARROW '[' field_name_list ']'
783 { $$ = new ExpressionNode( build_pfieldSel( $1, build_tuple( $4 ) ) ); }
784 ;
785
786field_name:
787 INTEGERconstant fraction_constants_opt
788 { $$ = new ExpressionNode( build_field_name_fraction_constants( build_constantInteger( *$1 ), $2 ) ); }
789 | FLOATINGconstant fraction_constants_opt
790 { $$ = new ExpressionNode( build_field_name_fraction_constants( build_field_name_FLOATINGconstant( *$1 ), $2 ) ); }
791 | identifier_at fraction_constants_opt // CFA, allow anonymous fields
792 {
793 $$ = new ExpressionNode( build_field_name_fraction_constants( build_varref( $1 ), $2 ) );
794 }
795 ;
796
797fraction_constants_opt:
798 // empty
799 { $$ = nullptr; }
800 | fraction_constants_opt FLOATING_FRACTIONconstant
801 {
802 Expression * constant = build_field_name_FLOATING_FRACTIONconstant( *$2 );
803 $$ = $1 != nullptr ? new ExpressionNode( build_fieldSel( $1, constant ) ) : new ExpressionNode( constant );
804 }
805 ;
806
807unary_expression:
808 postfix_expression
809 // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
810 // semantics checks, e.g., ++3, 3--, *3, &&3
811 | constant
812 | string_literal
813 { $$ = new ExpressionNode( $1 ); }
814 | EXTENSION cast_expression // GCC
815 { $$ = $2->set_extension( true ); }
816 // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
817 // { * X; } // dereference X
818 // { * int X; } // CFA declaration of pointer to int
819 | ptrref_operator cast_expression // CFA
820 {
821 switch ( $1 ) {
822 case OperKinds::AddressOf:
823 $$ = new ExpressionNode( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) );
824 break;
825 case OperKinds::PointTo:
826 $$ = new ExpressionNode( build_unary_val( $1, $2 ) );
827 break;
828 case OperKinds::And:
829 $$ = new ExpressionNode( new AddressExpr( new AddressExpr( maybeMoveBuild<Expression>( $2 ) ) ) );
830 break;
831 default:
832 assert( false );
833 }
834 }
835 | unary_operator cast_expression
836 { $$ = new ExpressionNode( build_unary_val( $1, $2 ) ); }
837 | ICR unary_expression
838 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Incr, $2 ) ); }
839 | DECR unary_expression
840 { $$ = new ExpressionNode( build_unary_ptr( OperKinds::Decr, $2 ) ); }
841 | SIZEOF unary_expression
842 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
843 | SIZEOF '(' type_no_function ')'
844 { $$ = new ExpressionNode( new SizeofExpr( maybeMoveBuildType( $3 ) ) ); }
845 | ALIGNOF unary_expression // GCC, variable alignment
846 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuild<Expression>( $2 ) ) ); }
847 | ALIGNOF '(' type_no_function ')' // GCC, type alignment
848 { $$ = new ExpressionNode( new AlignofExpr( maybeMoveBuildType( $3 ) ) ); }
849 | OFFSETOF '(' type_no_function ',' identifier ')'
850 { $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) ); }
851 | TYPEID '(' type_no_function ')'
852 {
853 SemanticError( yylloc, "typeid name is currently unimplemented." ); $$ = nullptr;
854 // $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) );
855 }
856 ;
857
858ptrref_operator:
859 '*' { $$ = OperKinds::PointTo; }
860 | '&' { $$ = OperKinds::AddressOf; }
861 // GCC, address of label must be handled by semantic check for ref,ref,label
862 | ANDAND { $$ = OperKinds::And; }
863 ;
864
865unary_operator:
866 '+' { $$ = OperKinds::UnPlus; }
867 | '-' { $$ = OperKinds::UnMinus; }
868 | '!' { $$ = OperKinds::Neg; }
869 | '~' { $$ = OperKinds::BitNeg; }
870 ;
871
872cast_expression:
873 unary_expression
874 | '(' type_no_function ')' cast_expression
875 { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
876 | '(' aggregate_control '&' ')' cast_expression // CFA
877 { $$ = new ExpressionNode( build_keyword_cast( $2, $5 ) ); }
878 | '(' aggregate_control '*' ')' cast_expression // CFA
879 { $$ = new ExpressionNode( build_keyword_cast( $2, $5 ) ); }
880 | '(' VIRTUAL ')' cast_expression // CFA
881 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $4 ), maybeMoveBuildType( nullptr ) ) ); }
882 | '(' VIRTUAL type_no_function ')' cast_expression // CFA
883 { $$ = new ExpressionNode( new VirtualCastExpr( maybeMoveBuild<Expression>( $5 ), maybeMoveBuildType( $3 ) ) ); }
884 | '(' RETURN type_no_function ')' cast_expression // CFA
885 { SemanticError( yylloc, "Return cast is currently unimplemented." ); $$ = nullptr; }
886 | '(' COERCE type_no_function ')' cast_expression // CFA
887 { SemanticError( yylloc, "Coerce cast is currently unimplemented." ); $$ = nullptr; }
888 | '(' qualifier_cast_list ')' cast_expression // CFA
889 { SemanticError( yylloc, "Qualifier cast is currently unimplemented." ); $$ = nullptr; }
890// | '(' type_no_function ')' tuple
891// { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
892 ;
893
894qualifier_cast_list:
895 cast_modifier type_qualifier_name
896 | cast_modifier MUTEX
897 | qualifier_cast_list cast_modifier type_qualifier_name
898 | qualifier_cast_list cast_modifier MUTEX
899 ;
900
901cast_modifier:
902 '-'
903 | '+'
904 ;
905
906exponential_expression:
907 cast_expression
908 | exponential_expression '\\' cast_expression
909 { $$ = new ExpressionNode( build_binary_val( OperKinds::Exp, $1, $3 ) ); }
910 ;
911
912multiplicative_expression:
913 exponential_expression
914 | multiplicative_expression '*' exponential_expression
915 { $$ = new ExpressionNode( build_binary_val( OperKinds::Mul, $1, $3 ) ); }
916 | multiplicative_expression '/' exponential_expression
917 { $$ = new ExpressionNode( build_binary_val( OperKinds::Div, $1, $3 ) ); }
918 | multiplicative_expression '%' exponential_expression
919 { $$ = new ExpressionNode( build_binary_val( OperKinds::Mod, $1, $3 ) ); }
920 ;
921
922additive_expression:
923 multiplicative_expression
924 | additive_expression '+' multiplicative_expression
925 { $$ = new ExpressionNode( build_binary_val( OperKinds::Plus, $1, $3 ) ); }
926 | additive_expression '-' multiplicative_expression
927 { $$ = new ExpressionNode( build_binary_val( OperKinds::Minus, $1, $3 ) ); }
928 ;
929
930shift_expression:
931 additive_expression
932 | shift_expression LS additive_expression
933 { $$ = new ExpressionNode( build_binary_val( OperKinds::LShift, $1, $3 ) ); }
934 | shift_expression RS additive_expression
935 { $$ = new ExpressionNode( build_binary_val( OperKinds::RShift, $1, $3 ) ); }
936 ;
937
938relational_expression:
939 shift_expression
940 | relational_expression '<' shift_expression
941 { $$ = new ExpressionNode( build_binary_val( OperKinds::LThan, $1, $3 ) ); }
942 | relational_expression '>' shift_expression
943 { $$ = new ExpressionNode( build_binary_val( OperKinds::GThan, $1, $3 ) ); }
944 | relational_expression LE shift_expression
945 { $$ = new ExpressionNode( build_binary_val( OperKinds::LEThan, $1, $3 ) ); }
946 | relational_expression GE shift_expression
947 { $$ = new ExpressionNode( build_binary_val( OperKinds::GEThan, $1, $3 ) ); }
948 ;
949
950equality_expression:
951 relational_expression
952 | equality_expression EQ relational_expression
953 { $$ = new ExpressionNode( build_binary_val( OperKinds::Eq, $1, $3 ) ); }
954 | equality_expression NE relational_expression
955 { $$ = new ExpressionNode( build_binary_val( OperKinds::Neq, $1, $3 ) ); }
956 ;
957
958AND_expression:
959 equality_expression
960 | AND_expression '&' equality_expression
961 { $$ = new ExpressionNode( build_binary_val( OperKinds::BitAnd, $1, $3 ) ); }
962 ;
963
964exclusive_OR_expression:
965 AND_expression
966 | exclusive_OR_expression '^' AND_expression
967 { $$ = new ExpressionNode( build_binary_val( OperKinds::Xor, $1, $3 ) ); }
968 ;
969
970inclusive_OR_expression:
971 exclusive_OR_expression
972 | inclusive_OR_expression '|' exclusive_OR_expression
973 { $$ = new ExpressionNode( build_binary_val( OperKinds::BitOr, $1, $3 ) ); }
974 ;
975
976logical_AND_expression:
977 inclusive_OR_expression
978 | logical_AND_expression ANDAND inclusive_OR_expression
979 { $$ = new ExpressionNode( build_and_or( $1, $3, true ) ); }
980 ;
981
982logical_OR_expression:
983 logical_AND_expression
984 | logical_OR_expression OROR logical_AND_expression
985 { $$ = new ExpressionNode( build_and_or( $1, $3, false ) ); }
986 ;
987
988conditional_expression:
989 logical_OR_expression
990 | logical_OR_expression '?' comma_expression ':' conditional_expression
991 { $$ = new ExpressionNode( build_cond( $1, $3, $5 ) ); }
992 // FIX ME: computes $1 twice
993 | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
994 { $$ = new ExpressionNode( build_cond( $1, $1, $4 ) ); }
995 ;
996
997constant_expression:
998 conditional_expression
999 ;
1000
1001assignment_expression:
1002 // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
1003 conditional_expression
1004 | unary_expression assignment_operator assignment_expression
1005 {
1006// if ( $2 == OperKinds::AtAssn ) {
1007// SemanticError( yylloc, "C @= assignment is currently unimplemented." ); $$ = nullptr;
1008// } else {
1009 $$ = new ExpressionNode( build_binary_val( $2, $1, $3 ) );
1010// } // if
1011 }
1012 | unary_expression '=' '{' initializer_list_opt comma_opt '}'
1013 { SemanticError( yylloc, "Initializer assignment is currently unimplemented." ); $$ = nullptr; }
1014 ;
1015
1016assignment_expression_opt:
1017 // empty
1018 { $$ = nullptr; }
1019 | assignment_expression
1020 ;
1021
1022assignment_operator:
1023 simple_assignment_operator
1024 | compound_assignment_operator
1025 ;
1026
1027simple_assignment_operator:
1028 '=' { $$ = OperKinds::Assign; }
1029 | ATassign { $$ = OperKinds::AtAssn; } // CFA
1030 ;
1031
1032compound_assignment_operator:
1033 EXPassign { $$ = OperKinds::ExpAssn; }
1034 | MULTassign { $$ = OperKinds::MulAssn; }
1035 | DIVassign { $$ = OperKinds::DivAssn; }
1036 | MODassign { $$ = OperKinds::ModAssn; }
1037 | PLUSassign { $$ = OperKinds::PlusAssn; }
1038 | MINUSassign { $$ = OperKinds::MinusAssn; }
1039 | LSassign { $$ = OperKinds::LSAssn; }
1040 | RSassign { $$ = OperKinds::RSAssn; }
1041 | ANDassign { $$ = OperKinds::AndAssn; }
1042 | ERassign { $$ = OperKinds::ERAssn; }
1043 | ORassign { $$ = OperKinds::OrAssn; }
1044 ;
1045
1046tuple: // CFA, tuple
1047 // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
1048 // comma_expression in cfa_identifier_parameter_array and cfa_abstract_array
1049// '[' ']'
1050// { $$ = new ExpressionNode( build_tuple() ); }
1051// | '[' push assignment_expression pop ']'
1052// { $$ = new ExpressionNode( build_tuple( $3 ) ); }
1053 '[' ',' tuple_expression_list ']'
1054 { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)(new ExpressionNode( nullptr ) )->set_last( $3 ) ) ); }
1055 | '[' push assignment_expression pop ',' tuple_expression_list ']'
1056 { $$ = new ExpressionNode( build_tuple( (ExpressionNode *)($3->set_last( $6 ) ) )); }
1057 ;
1058
1059tuple_expression_list:
1060 assignment_expression
1061 | '@' // CFA
1062 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1063 | tuple_expression_list ',' assignment_expression
1064 { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1065 | tuple_expression_list ',' '@'
1066 { SemanticError( yylloc, "Eliding tuple element with '@' is currently unimplemented." ); $$ = nullptr; }
1067 ;
1068
1069comma_expression:
1070 assignment_expression
1071 | comma_expression ',' assignment_expression
1072 { $$ = new ExpressionNode( new CommaExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
1073 ;
1074
1075comma_expression_opt:
1076 // empty
1077 { $$ = nullptr; }
1078 | comma_expression
1079 ;
1080
1081//*************************** STATEMENTS *******************************
1082
1083statement:
1084 labeled_statement
1085 | compound_statement
1086 | expression_statement
1087 | selection_statement
1088 | iteration_statement
1089 | jump_statement
1090 | with_statement
1091 | mutex_statement
1092 | waitfor_statement
1093 | exception_statement
1094 | enable_disable_statement
1095 { SemanticError( yylloc, "enable/disable statement is currently unimplemented." ); $$ = nullptr; }
1096 | asm_statement
1097 | DIRECTIVE
1098 { $$ = new StatementNode( build_directive( $1 ) ); }
1099 ;
1100
1101labeled_statement:
1102 // labels cannot be identifiers 0 or 1
1103 identifier_or_type_name ':' attribute_list_opt statement
1104 { $$ = $4->add_label( $1, $3 ); }
1105 | identifier_or_type_name ':' attribute_list_opt error // syntax error
1106 {
1107 SemanticError( yylloc, ::toString( "Label \"", *$1.str, "\" must be associated with a statement, "
1108 "where a declaration, case, or default is not a statement. "
1109 "Move the label or terminate with a semi-colon." ) );
1110 $$ = nullptr;
1111 }
1112 ;
1113
1114compound_statement:
1115 '{' '}'
1116 { $$ = new StatementNode( build_compound( (StatementNode *)0 ) ); }
1117 | '{' push
1118 local_label_declaration_opt // GCC, local labels appear at start of block
1119 statement_decl_list // C99, intermix declarations and statements
1120 pop '}'
1121 { $$ = new StatementNode( build_compound( $4 ) ); }
1122 ;
1123
1124statement_decl_list: // C99
1125 statement_decl
1126 | statement_decl_list statement_decl
1127 { assert( $1 ); $1->set_last( $2 ); $$ = $1; }
1128 ;
1129
1130statement_decl:
1131 declaration // CFA, new & old style declarations
1132 { $$ = new StatementNode( $1 ); }
1133 | EXTENSION declaration // GCC
1134 { distExt( $2 ); $$ = new StatementNode( $2 ); }
1135 | function_definition
1136 { $$ = new StatementNode( $1 ); }
1137 | EXTENSION function_definition // GCC
1138 { distExt( $2 ); $$ = new StatementNode( $2 ); }
1139 | statement
1140 ;
1141
1142statement_list_nodecl:
1143 statement
1144 | statement_list_nodecl statement
1145 { assert( $1 ); $1->set_last( $2 ); $$ = $1; }
1146 | statement_list_nodecl error // syntax error
1147 { SemanticError( yylloc, "Declarations only allowed at the start of the switch body, i.e., after the '{'." ); $$ = nullptr; }
1148 ;
1149
1150expression_statement:
1151 comma_expression_opt ';'
1152 { $$ = new StatementNode( build_expr( $1 ) ); }
1153 | MUTEX '(' ')' comma_expression ';'
1154 { $$ = new StatementNode( build_mutex( nullptr, new StatementNode( build_expr( $4 ) ) ) ); }
1155 ;
1156
1157selection_statement:
1158 // pop causes a S/R conflict without separating the IF statement into a non-terminal even after resolving
1159 // the inherent S/R conflict with THEN/ELSE.
1160 push if_statement pop
1161 { $$ = $2; }
1162 | SWITCH '(' comma_expression ')' case_clause
1163 { $$ = new StatementNode( build_switch( true, $3, $5 ) ); }
1164 | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
1165 {
1166 StatementNode *sw = new StatementNode( build_switch( true, $3, $8 ) );
1167 // The semantics of the declaration list is changed to include associated initialization, which is performed
1168 // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
1169 // statement around the switch. Statements after the initial declaration list can never be executed, and
1170 // therefore, are removed from the grammar even though C allows it. The change also applies to choose
1171 // statement.
1172 $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
1173 }
1174 | SWITCH '(' comma_expression ')' '{' error '}' // CFA, syntax error
1175 { SemanticError( yylloc, "Only declarations can appear before the list of case clauses." ); $$ = nullptr; }
1176 | CHOOSE '(' comma_expression ')' case_clause // CFA
1177 { $$ = new StatementNode( build_switch( false, $3, $5 ) ); }
1178 | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt pop '}' // CFA
1179 {
1180 StatementNode *sw = new StatementNode( build_switch( false, $3, $8 ) );
1181 $$ = $7 ? new StatementNode( build_compound( (StatementNode *)((new StatementNode( $7 ))->set_last( sw )) ) ) : sw;
1182 }
1183 | CHOOSE '(' comma_expression ')' '{' error '}' // CFA, syntax error
1184 { SemanticError( yylloc, "Only declarations can appear before the list of case clauses." ); $$ = nullptr; }
1185 ;
1186
1187if_statement:
1188 IF '(' conditional_declaration ')' statement %prec THEN
1189 // explicitly deal with the shift/reduce conflict on if/else
1190 { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), nullptr ) ); }
1191 | IF '(' conditional_declaration ')' statement ELSE statement
1192 { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), maybe_build_compound( $7 ) ) ); }
1193 ;
1194
1195conditional_declaration:
1196 comma_expression
1197 { $$ = new CondCtl( nullptr, $1 ); }
1198 | c_declaration // no semi-colon
1199 { $$ = new CondCtl( $1, nullptr ); }
1200 | cfa_declaration // no semi-colon
1201 { $$ = new CondCtl( $1, nullptr ); }
1202 | declaration comma_expression // semi-colon separated
1203 { $$ = new CondCtl( $1, $2 ); }
1204 ;
1205
1206// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
1207// clause allows a list of values and subranges.
1208
1209case_value: // CFA
1210 constant_expression { $$ = $1; }
1211 | constant_expression ELLIPSIS constant_expression // GCC, subrange
1212 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
1213 | subrange // CFA, subrange
1214 ;
1215
1216case_value_list: // CFA
1217 case_value { $$ = new StatementNode( build_case( $1 ) ); }
1218 // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
1219 | case_value_list ',' case_value { $$ = (StatementNode *)($1->set_last( new StatementNode( build_case( $3 ) ) ) ); }
1220 ;
1221
1222case_label: // CFA
1223 CASE error // syntax error
1224 { SemanticError( yylloc, "Missing case list after case." ); $$ = nullptr; }
1225 | CASE case_value_list ':' { $$ = $2; }
1226 | CASE case_value_list error // syntax error
1227 { SemanticError( yylloc, "Missing colon after case list." ); $$ = nullptr; }
1228 | DEFAULT ':' { $$ = new StatementNode( build_default() ); }
1229 // A semantic check is required to ensure only one default clause per switch/choose statement.
1230 | DEFAULT error // syntax error
1231 { SemanticError( yylloc, "Missing colon after default." ); $$ = nullptr; }
1232 ;
1233
1234case_label_list: // CFA
1235 case_label
1236 | case_label_list case_label { $$ = (StatementNode *)( $1->set_last( $2 )); }
1237 ;
1238
1239case_clause: // CFA
1240 case_label_list statement { $$ = $1->append_last_case( maybe_build_compound( $2 ) ); }
1241 ;
1242
1243switch_clause_list_opt: // CFA
1244 // empty
1245 { $$ = nullptr; }
1246 | switch_clause_list
1247 ;
1248
1249switch_clause_list: // CFA
1250 case_label_list statement_list_nodecl
1251 { $$ = $1->append_last_case( new StatementNode( build_compound( $2 ) ) ); }
1252 | switch_clause_list case_label_list statement_list_nodecl
1253 { $$ = (StatementNode *)( $1->set_last( $2->append_last_case( new StatementNode( build_compound( $3 ) ) ) ) ); }
1254 ;
1255
1256iteration_statement:
1257 WHILE '(' ')' statement %prec THEN // CFA => while ( 1 )
1258 { $$ = new StatementNode( build_while( new CondCtl( nullptr, NEW_ONE ), maybe_build_compound( $4 ) ) ); }
1259 | WHILE '(' ')' statement ELSE statement // CFA
1260 {
1261 $$ = new StatementNode( build_while( new CondCtl( nullptr, NEW_ONE ), maybe_build_compound( $4 ) ) );
1262 SemanticWarning( yylloc, Warning::SuperfluousElse, "" );
1263 }
1264 | WHILE '(' conditional_declaration ')' statement %prec THEN
1265 { $$ = new StatementNode( build_while( $3, maybe_build_compound( $5 ) ) ); }
1266 | WHILE '(' conditional_declaration ')' statement ELSE statement // CFA
1267 { $$ = new StatementNode( build_while( $3, maybe_build_compound( $5 ), $7 ) ); }
1268 | DO statement WHILE '(' ')' ';' // CFA => do while( 1 )
1269 { $$ = new StatementNode( build_do_while( NEW_ONE, maybe_build_compound( $2 ) ) ); }
1270 | DO statement WHILE '(' ')' ELSE statement // CFA
1271 {
1272 $$ = new StatementNode( build_do_while( NEW_ONE, maybe_build_compound( $2 ) ) );
1273 SemanticWarning( yylloc, Warning::SuperfluousElse, "" );
1274 }
1275 | DO statement WHILE '(' comma_expression ')' ';'
1276 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ) ) ); }
1277 | DO statement WHILE '(' comma_expression ')' ELSE statement // CFA
1278 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ), $8 ) ); }
1279 | FOR '(' ')' statement %prec THEN // CFA => for ( ;; )
1280 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); }
1281 | FOR '(' ')' statement ELSE statement // CFA
1282 {
1283 $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) );
1284 SemanticWarning( yylloc, Warning::SuperfluousElse, "" );
1285 }
1286 | FOR '(' for_control_expression_list ')' statement %prec THEN
1287 { $$ = new StatementNode( build_for( $3, maybe_build_compound( $5 ) ) ); }
1288 | FOR '(' for_control_expression_list ')' statement ELSE statement // CFA
1289 { $$ = new StatementNode( build_for( $3, maybe_build_compound( $5 ), $7 ) ); }
1290 ;
1291
1292for_control_expression_list:
1293 for_control_expression
1294 | for_control_expression_list ':' for_control_expression
1295 // ForCtrl + ForCtrl:
1296 // init + init => multiple declaration statements that are hoisted
1297 // condition + condition => (expression) && (expression)
1298 // change + change => (expression), (expression)
1299 {
1300 $1->init->set_last( $3->init );
1301 if ( $1->condition ) {
1302 if ( $3->condition ) {
1303 $1->condition->expr.reset( new LogicalExpr( $1->condition->expr.release(), $3->condition->expr.release(), true ) );
1304 } // if
1305 } else $1->condition = $3->condition;
1306 if ( $1->change ) {
1307 if ( $3->change ) {
1308 $1->change->expr.reset( new CommaExpr( $1->change->expr.release(), $3->change->expr.release() ) );
1309 } // if
1310 } else $1->change = $3->change;
1311 $$ = $1;
1312 }
1313 ;
1314
1315for_control_expression:
1316 ';' comma_expression_opt ';' comma_expression_opt
1317 { $$ = new ForCtrl( (ExpressionNode * )nullptr, $2, $4 ); }
1318 | comma_expression ';' comma_expression_opt ';' comma_expression_opt
1319 { $$ = new ForCtrl( $1, $3, $5 ); }
1320 | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';'
1321 { $$ = new ForCtrl( $1, $2, $4 ); }
1322
1323 | comma_expression // CFA
1324 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), NEW_ZERO, OperKinds::LThan, $1->clone(), NEW_ONE ); }
1325 | downupdowneq comma_expression // CFA
1326 { $$ = forCtrl( $2, new string( DeclarationNode::anonymous.newName() ), $1 == OperKinds::GThan || $1 == OperKinds::GEThan ? $2->clone() : NEW_ZERO, $1, $2->clone(), NEW_ONE ); }
1327 | comma_expression updowneq comma_expression // CFA
1328 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, $3, NEW_ONE ); }
1329 | '@' updowneq comma_expression // CFA
1330 { $$ = forCtrl( $3, new string( DeclarationNode::anonymous.newName() ),
1331 $2 == OperKinds::GThan || $2 == OperKinds::GEThan ? $3->clone() : NEW_ZERO, $2, $3->clone(), NEW_ONE ); }
1332 | comma_expression updown '@' // CFA
1333 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, nullptr, NEW_ONE ); }
1334
1335 | comma_expression updowneq comma_expression '~' comma_expression // CFA
1336 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, $3, $5 ); }
1337 | '@' updowneq comma_expression '~' comma_expression // CFA
1338 { $$ = forCtrl( $3, new string( DeclarationNode::anonymous.newName() ),
1339 $2 == OperKinds::GThan || $2 == OperKinds::GEThan ? $3->clone() : NEW_ZERO, $2, $3->clone(), $5 ); }
1340 | comma_expression updown '@' '~' comma_expression // CFA
1341 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, nullptr, $5 ); }
1342 | comma_expression updown '@' '~' '@' // CFA
1343 {
1344 if ( $2 == OperKinds::GThan ) { SemanticError( yylloc, "Negative range \"-~\" is meaningless when comparison and iterator are empty. Use \"~\"." ); $$ = nullptr; }
1345 else $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, nullptr, nullptr );
1346 }
1347 | comma_expression ';' // CFA
1348 { $$ = forCtrl( new ExpressionNode( build_constantInteger( *new string( "0u" ) ) ), $1, nullptr, OperKinds::LThan, nullptr, nullptr ); }
1349 | comma_expression ';' comma_expression // CFA
1350 { $$ = forCtrl( $3, $1, NEW_ZERO, OperKinds::LThan, $3->clone(), NEW_ONE ); }
1351 | comma_expression ';' downupdowneq comma_expression // CFA
1352 { $$ = forCtrl( $4, $1, $3 == OperKinds::GThan || $3 == OperKinds::GEThan ? $4->clone() : NEW_ZERO, $3, $4->clone(), NEW_ONE ); }
1353 | comma_expression ';' comma_expression updowneq comma_expression // CFA
1354 { $$ = forCtrl( $3, $1, $3->clone(), $4, $5, NEW_ONE ); }
1355 | comma_expression ';' '@' updowneq comma_expression // CFA
1356 { $$ = forCtrl( $5, $1, $4 == OperKinds::GThan || $4 == OperKinds::GEThan ? $5->clone() : NEW_ZERO, $4, $5->clone(), NEW_ONE ); }
1357 | comma_expression ';' comma_expression updown '@' // CFA
1358 { $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, NEW_ONE ); }
1359
1360 | comma_expression ';' comma_expression updowneq comma_expression '~' comma_expression // CFA
1361 { $$ = forCtrl( $3, $1, $3->clone(), $4, $5, $7 ); }
1362 | comma_expression ';' '@' updowneq comma_expression '~' comma_expression // CFA
1363 { $$ = forCtrl( $5, $1, $4 == OperKinds::GThan || $4 == OperKinds::GEThan ? $5->clone() : NEW_ZERO, $4, $5->clone(), $7 ); }
1364 | comma_expression ';' comma_expression updown '@' '~' comma_expression // CFA
1365 { $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, $7 ); }
1366 | comma_expression ';' comma_expression updown '@' '~' '@' // CFA
1367 {
1368 if ( $4 == OperKinds::GThan ) { SemanticError( yylloc, "Negative range \"-~\" is meaningless when comparison and iterator are empty. Use \"~\"." ); $$ = nullptr; }
1369 else $$ = forCtrl( $3, $1, $3->clone(), $4, nullptr, nullptr );
1370 }
1371
1372 | declaration comma_expression // CFA
1373 { $$ = forCtrl( $1, NEW_ZERO, OperKinds::LThan, $2, NEW_ONE ); }
1374 | declaration downupdowneq comma_expression // CFA
1375 { $$ = forCtrl( $1, $2 == OperKinds::GThan || $2 == OperKinds::GEThan ? $3->clone() : NEW_ZERO, $2, $3, NEW_ONE ); }
1376 | declaration comma_expression updowneq comma_expression // CFA
1377 { $$ = forCtrl( $1, $2, $3, $4, NEW_ONE ); }
1378 | declaration '@' updowneq comma_expression // CFA
1379 { $$ = forCtrl( $1, $3 == OperKinds::GThan || $3 == OperKinds::GEThan ? $4->clone() : NEW_ZERO, $3, $4, NEW_ONE ); }
1380 | declaration comma_expression updown '@' // CFA
1381 { $$ = forCtrl( $1, $2, $3, nullptr, NEW_ONE ); }
1382
1383 | declaration comma_expression updowneq comma_expression '~' comma_expression // CFA
1384 { $$ = forCtrl( $1, $2, $3, $4, $6 ); }
1385 | declaration '@' updowneq comma_expression '~' comma_expression // CFA
1386 { $$ = forCtrl( $1, $3 == OperKinds::GThan || $3 == OperKinds::GEThan ? $4->clone() : NEW_ZERO, $3, $4, $6 ); }
1387 | declaration comma_expression updown '@' '~' comma_expression // CFA
1388 { $$ = forCtrl( $1, $2, $3, nullptr, $6 ); }
1389 | declaration comma_expression updown '@' '~' '@' // CFA
1390 {
1391 if ( $3 == OperKinds::GThan ) { SemanticError( yylloc, "Negative range \"-~\" is meaningless when comparison and iterator are empty. Use \"~\"." ); $$ = nullptr; }
1392 else $$ = forCtrl( $1, $2, $3, nullptr, nullptr );
1393 }
1394
1395 | comma_expression ';' TYPEDEFname // CFA, array type
1396 {
1397 SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1398 //$$ = forCtrl( new ExpressionNode( build_varref( $3 ) ), $1, nullptr, OperKinds::Range, nullptr, nullptr );
1399 }
1400 | comma_expression ';' downupdowneq TYPEDEFname // CFA, array type
1401 {
1402 SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
1403 }
1404 ;
1405
1406downupdowneq:
1407 ErangeDown
1408 { $$ = OperKinds::GThan; }
1409 | ErangeUpEq
1410 { $$ = OperKinds::LEThan; }
1411 | ErangeDownEq
1412 { $$ = OperKinds::GEThan; }
1413 ;
1414
1415updown:
1416 '~'
1417 { $$ = OperKinds::LThan; }
1418 | ErangeDown
1419 { $$ = OperKinds::GThan; }
1420 ;
1421
1422updowneq:
1423 updown
1424 | ErangeUpEq
1425 { $$ = OperKinds::LEThan; }
1426 | ErangeDownEq
1427 { $$ = OperKinds::GEThan; }
1428 ;
1429
1430jump_statement:
1431 GOTO identifier_or_type_name ';'
1432 { $$ = new StatementNode( build_branch( $2, BranchStmt::Goto ) ); }
1433 | GOTO '*' comma_expression ';' // GCC, computed goto
1434 // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
1435 // whereas normal operator precedence yields goto (*i)+3;
1436 { $$ = new StatementNode( build_computedgoto( $3 ) ); }
1437 // A semantic check is required to ensure fallthru appears only in the body of a choose statement.
1438 | fall_through_name ';' // CFA
1439 { $$ = new StatementNode( build_branch( BranchStmt::FallThrough ) ); }
1440 | fall_through_name identifier_or_type_name ';' // CFA
1441 { $$ = new StatementNode( build_branch( $2, BranchStmt::FallThrough ) ); }
1442 | fall_through_name DEFAULT ';' // CFA
1443 { $$ = new StatementNode( build_branch( BranchStmt::FallThroughDefault ) ); }
1444 | CONTINUE ';'
1445 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1446 { $$ = new StatementNode( build_branch( BranchStmt::Continue ) ); }
1447 | CONTINUE identifier_or_type_name ';' // CFA, multi-level continue
1448 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1449 // the target of the transfer appears only at the start of an iteration statement.
1450 { $$ = new StatementNode( build_branch( $2, BranchStmt::Continue ) ); }
1451 | BREAK ';'
1452 // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
1453 { $$ = new StatementNode( build_branch( BranchStmt::Break ) ); }
1454 | BREAK identifier_or_type_name ';' // CFA, multi-level exit
1455 // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
1456 // the target of the transfer appears only at the start of an iteration statement.
1457 { $$ = new StatementNode( build_branch( $2, BranchStmt::Break ) ); }
1458 | RETURN comma_expression_opt ';'
1459 { $$ = new StatementNode( build_return( $2 ) ); }
1460 | RETURN '{' initializer_list_opt comma_opt '}' ';'
1461 { SemanticError( yylloc, "Initializer return is currently unimplemented." ); $$ = nullptr; }
1462 | SUSPEND ';'
1463 { $$ = new StatementNode( build_suspend( nullptr ) ); }
1464 | SUSPEND compound_statement
1465 { $$ = new StatementNode( build_suspend( $2 ) ); }
1466 | SUSPEND COROUTINE ';'
1467 { $$ = new StatementNode( build_suspend( nullptr, SuspendStmt::Coroutine ) ); }
1468 | SUSPEND COROUTINE compound_statement
1469 { $$ = new StatementNode( build_suspend( $3, SuspendStmt::Coroutine ) ); }
1470 | SUSPEND GENERATOR ';'
1471 { $$ = new StatementNode( build_suspend( nullptr, SuspendStmt::Generator ) ); }
1472 | SUSPEND GENERATOR compound_statement
1473 { $$ = new StatementNode( build_suspend( $3, SuspendStmt::Generator ) ); }
1474 | THROW assignment_expression_opt ';' // handles rethrow
1475 { $$ = new StatementNode( build_throw( $2 ) ); }
1476 | THROWRESUME assignment_expression_opt ';' // handles reresume
1477 { $$ = new StatementNode( build_resume( $2 ) ); }
1478 | THROWRESUME assignment_expression_opt AT assignment_expression ';' // handles reresume
1479 { $$ = new StatementNode( build_resume_at( $2, $4 ) ); }
1480 ;
1481
1482fall_through_name: // CFA
1483 FALLTHRU
1484 | FALLTHROUGH
1485 ;
1486
1487with_statement:
1488 WITH '(' tuple_expression_list ')' statement
1489 { $$ = new StatementNode( build_with( $3, $5 ) ); }
1490 ;
1491
1492// If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so change syntax to "with mutex".
1493mutex_statement:
1494 MUTEX '(' argument_expression_list ')' statement
1495 { $$ = new StatementNode( build_mutex( $3, $5 ) ); }
1496 ;
1497
1498when_clause:
1499 WHEN '(' comma_expression ')' { $$ = $3; }
1500 ;
1501
1502when_clause_opt:
1503 // empty
1504 { $$ = nullptr; }
1505 | when_clause
1506 ;
1507
1508waitfor:
1509 WAITFOR '(' cast_expression ')'
1510 { $$ = $3; }
1511// | WAITFOR '(' cast_expression ',' argument_expression_list_opt ')'
1512// { $$ = (ExpressionNode *)$3->set_last( $5 ); }
1513 | WAITFOR '(' cast_expression_list ':' argument_expression_list_opt ')'
1514 { $$ = (ExpressionNode *)($3->set_last( $5 )); }
1515 ;
1516
1517cast_expression_list:
1518 cast_expression
1519 | cast_expression_list ',' cast_expression
1520 // { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1521 { SemanticError( yylloc, "List of mutex member is currently unimplemented." ); $$ = nullptr; }
1522 ;
1523
1524timeout:
1525 TIMEOUT '(' comma_expression ')' { $$ = $3; }
1526 ;
1527
1528waitfor_clause:
1529 when_clause_opt waitfor statement %prec THEN
1530 { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1 ); }
1531 | when_clause_opt waitfor statement WOR waitfor_clause
1532 { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1, $5 ); }
1533 | when_clause_opt timeout statement %prec THEN
1534 { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1 ); }
1535 | when_clause_opt ELSE statement
1536 { $$ = build_waitfor_timeout( nullptr, maybe_build_compound( $3 ), $1 ); }
1537 // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless)
1538 | when_clause_opt timeout statement WOR ELSE statement // syntax error
1539 { SemanticError( yylloc, "else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; }
1540 | when_clause_opt timeout statement WOR when_clause ELSE statement
1541 { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1, maybe_build_compound( $7 ), $5 ); }
1542 ;
1543
1544waitfor_statement:
1545 when_clause_opt waitfor statement %prec THEN
1546 { $$ = new StatementNode( build_waitfor( $2, $3, $1 ) ); }
1547 | when_clause_opt waitfor statement WOR waitfor_clause
1548 { $$ = new StatementNode( build_waitfor( $2, $3, $1, $5 ) ); }
1549 ;
1550
1551exception_statement:
1552 TRY compound_statement handler_clause %prec THEN
1553 { $$ = new StatementNode( build_try( $2, $3, 0 ) ); }
1554 | TRY compound_statement finally_clause
1555 { $$ = new StatementNode( build_try( $2, 0, $3 ) ); }
1556 | TRY compound_statement handler_clause finally_clause
1557 { $$ = new StatementNode( build_try( $2, $3, $4 ) ); }
1558 ;
1559
1560handler_clause:
1561 handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1562 { $$ = new StatementNode( build_catch( $1, $4, $6, $8 ) ); }
1563 | handler_clause handler_key '(' push exception_declaration pop handler_predicate_opt ')' compound_statement
1564 { $$ = (StatementNode *)$1->set_last( new StatementNode( build_catch( $2, $5, $7, $9 ) ) ); }
1565 ;
1566
1567handler_predicate_opt:
1568 // empty
1569 { $$ = nullptr; }
1570 | ';' conditional_expression { $$ = $2; }
1571 ;
1572
1573handler_key:
1574 CATCH { $$ = CatchStmt::Terminate; }
1575 | RECOVER { $$ = CatchStmt::Terminate; }
1576 | CATCHRESUME { $$ = CatchStmt::Resume; }
1577 | FIXUP { $$ = CatchStmt::Resume; }
1578 ;
1579
1580finally_clause:
1581 FINALLY compound_statement { $$ = new StatementNode( build_finally( $2 ) ); }
1582 ;
1583
1584exception_declaration:
1585 // No SUE declaration in parameter list.
1586 type_specifier_nobody
1587 | type_specifier_nobody declarator
1588 { $$ = $2->addType( $1 ); }
1589 | type_specifier_nobody variable_abstract_declarator
1590 { $$ = $2->addType( $1 ); }
1591 | cfa_abstract_declarator_tuple identifier // CFA
1592 { $$ = $1->addName( $2 ); }
1593 | cfa_abstract_declarator_tuple // CFA
1594 ;
1595
1596enable_disable_statement:
1597 enable_disable_key identifier_list compound_statement
1598 ;
1599
1600enable_disable_key:
1601 ENABLE
1602 | DISABLE
1603 ;
1604
1605asm_statement:
1606 ASM asm_volatile_opt '(' string_literal ')' ';'
1607 { $$ = new StatementNode( build_asm( $2, $4, 0 ) ); }
1608 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ')' ';' // remaining GCC
1609 { $$ = new StatementNode( build_asm( $2, $4, $6 ) ); }
1610 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ')' ';'
1611 { $$ = new StatementNode( build_asm( $2, $4, $6, $8 ) ); }
1612 | ASM asm_volatile_opt '(' string_literal ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
1613 { $$ = new StatementNode( build_asm( $2, $4, $6, $8, $10 ) ); }
1614 | ASM asm_volatile_opt GOTO '(' string_literal ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
1615 { $$ = new StatementNode( build_asm( $2, $5, 0, $8, $10, $12 ) ); }
1616 ;
1617
1618asm_volatile_opt: // GCC
1619 // empty
1620 { $$ = false; }
1621 | VOLATILE
1622 { $$ = true; }
1623 ;
1624
1625asm_operands_opt: // GCC
1626 // empty
1627 { $$ = nullptr; } // use default argument
1628 | asm_operands_list
1629 ;
1630
1631asm_operands_list: // GCC
1632 asm_operand
1633 | asm_operands_list ',' asm_operand
1634 { $$ = (ExpressionNode *)($1->set_last( $3 )); }
1635 ;
1636
1637asm_operand: // GCC
1638 string_literal '(' constant_expression ')'
1639 { $$ = new ExpressionNode( new AsmExpr( nullptr, $1, maybeMoveBuild<Expression>( $3 ) ) ); }
1640 | '[' IDENTIFIER ']' string_literal '(' constant_expression ')'
1641 { $$ = new ExpressionNode( new AsmExpr( $2, $4, maybeMoveBuild<Expression>( $6 ) ) ); }
1642 ;
1643
1644asm_clobbers_list_opt: // GCC
1645 // empty
1646 { $$ = nullptr; } // use default argument
1647 | string_literal
1648 { $$ = new ExpressionNode( $1 ); }
1649 | asm_clobbers_list_opt ',' string_literal
1650 { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( $3 ) )); }
1651 ;
1652
1653label_list:
1654 identifier
1655 {
1656 $$ = new LabelNode(); $$->labels.push_back( *$1 );
1657 delete $1; // allocated by lexer
1658 }
1659 | label_list ',' identifier
1660 {
1661 $$ = $1; $1->labels.push_back( *$3 );
1662 delete $3; // allocated by lexer
1663 }
1664 ;
1665
1666//******************************* DECLARATIONS *********************************
1667
1668declaration_list_opt: // used at beginning of switch statement
1669 // empty
1670 { $$ = nullptr; }
1671 | declaration_list
1672 ;
1673
1674declaration_list:
1675 declaration
1676 | declaration_list declaration
1677 { $$ = $1->appendList( $2 ); }
1678 ;
1679
1680KR_parameter_list_opt: // used to declare parameter types in K&R style functions
1681 // empty
1682 { $$ = nullptr; }
1683 | KR_parameter_list
1684 ;
1685
1686KR_parameter_list:
1687 push c_declaration pop ';'
1688 { $$ = $2; }
1689 | KR_parameter_list push c_declaration pop ';'
1690 { $$ = $1->appendList( $3 ); }
1691 ;
1692
1693local_label_declaration_opt: // GCC, local label
1694 // empty
1695 | local_label_declaration_list
1696 ;
1697
1698local_label_declaration_list: // GCC, local label
1699 LABEL local_label_list ';'
1700 | local_label_declaration_list LABEL local_label_list ';'
1701 ;
1702
1703local_label_list: // GCC, local label
1704 identifier_or_type_name
1705 | local_label_list ',' identifier_or_type_name
1706 ;
1707
1708declaration: // old & new style declarations
1709 c_declaration ';'
1710 {
1711 // printf( "C_DECLARATION1 %p %s\n", $$, $$->name ? $$->name->c_str() : "(nil)" );
1712 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
1713 // printf( "\tattr %s\n", attr->name.c_str() );
1714 // } // for
1715 }
1716 | cfa_declaration ';' // CFA
1717 | static_assert // C11
1718 ;
1719
1720static_assert:
1721 STATICASSERT '(' constant_expression ',' string_literal ')' ';' // C11
1722 { $$ = DeclarationNode::newStaticAssert( $3, $5 ); }
1723 | STATICASSERT '(' constant_expression ')' ';' // CFA
1724 { $$ = DeclarationNode::newStaticAssert( $3, build_constantStr( *new string( "\"\"" ) ) ); }
1725
1726// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1727// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1728// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1729// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1730// the declaration list (as in Pascal). ANSI C and the new CFA declarations may appear together in the same program
1731// block, but cannot be mixed within a specific declaration.
1732//
1733// CFA C
1734// [10] int x; int x[10]; // array of 10 integers
1735// [10] * char y; char *y[10]; // array of 10 pointers to char
1736
1737cfa_declaration: // CFA
1738 cfa_variable_declaration
1739 | cfa_typedef_declaration
1740 | cfa_function_declaration
1741 | type_declaring_list
1742 { SemanticError( yylloc, "otype declaration is currently unimplemented." ); $$ = nullptr; }
1743 | trait_specifier
1744 ;
1745
1746cfa_variable_declaration: // CFA
1747 cfa_variable_specifier initializer_opt
1748 { $$ = $1->addInitializer( $2 ); }
1749 | declaration_qualifier_list cfa_variable_specifier initializer_opt
1750 // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1751 // them as a type_qualifier cannot appear in that context.
1752 { $$ = $2->addQualifiers( $1 )->addInitializer( $3 ); }
1753 | cfa_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1754 { $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) ); }
1755 ;
1756
1757cfa_variable_specifier: // CFA
1758 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1759 // storage-class
1760 cfa_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1761 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1762 | cfa_abstract_tuple identifier_or_type_name asm_name_opt
1763 { $$ = $1->addName( $2 )->addAsmName( $3 ); }
1764 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name asm_name_opt
1765 { $$ = $2->addQualifiers( $1 )->addName( $3 )->addAsmName( $4 ); }
1766 ;
1767
1768cfa_function_declaration: // CFA
1769 cfa_function_specifier
1770 | type_qualifier_list cfa_function_specifier
1771 { $$ = $2->addQualifiers( $1 ); }
1772 | declaration_qualifier_list cfa_function_specifier
1773 { $$ = $2->addQualifiers( $1 ); }
1774 | declaration_qualifier_list type_qualifier_list cfa_function_specifier
1775 { $$ = $3->addQualifiers( $1 )->addQualifiers( $2 ); }
1776 | cfa_function_declaration ',' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')'
1777 {
1778 // Append the return type at the start (left-hand-side) to each identifier in the list.
1779 DeclarationNode * ret = new DeclarationNode;
1780 ret->type = maybeClone( $1->type->base );
1781 $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
1782 }
1783 ;
1784
1785cfa_function_specifier: // CFA
1786// '[' ']' identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' // S/R conflict
1787// {
1788// $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1789// }
1790// '[' ']' identifier '(' push cfa_parameter_ellipsis_list_opt pop ')'
1791// {
1792// typedefTable.setNextIdentifier( *$5 );
1793// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1794// }
1795// | '[' ']' TYPEDEFname '(' push cfa_parameter_ellipsis_list_opt pop ')'
1796// {
1797// typedefTable.setNextIdentifier( *$5 );
1798// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1799// }
1800// | '[' ']' typegen_name
1801 // identifier_or_type_name must be broken apart because of the sequence:
1802 //
1803 // '[' ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
1804 // '[' ']' type_specifier
1805 //
1806 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1807 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1808 cfa_abstract_tuple identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1809 // To obtain LR(1 ), this rule must be factored out from function return type (see cfa_abstract_declarator).
1810 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 )->addQualifiers( $8 ); }
1811 | cfa_function_return identifier_or_type_name '(' push cfa_parameter_ellipsis_list_opt pop ')' attribute_list_opt
1812 { $$ = DeclarationNode::newFunction( $2, $1, $5, 0 )->addQualifiers( $8 ); }
1813 ;
1814
1815cfa_function_return: // CFA
1816 '[' push cfa_parameter_list pop ']'
1817 { $$ = DeclarationNode::newTuple( $3 ); }
1818 | '[' push cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ']'
1819 // To obtain LR(1 ), the last cfa_abstract_parameter_list is added into this flattened rule to lookahead to the ']'.
1820 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1821 ;
1822
1823cfa_typedef_declaration: // CFA
1824 TYPEDEF cfa_variable_specifier
1825 {
1826 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "1" );
1827 $$ = $2->addTypedef();
1828 }
1829 | TYPEDEF cfa_function_specifier
1830 {
1831 typedefTable.addToEnclosingScope( *$2->name, TYPEDEFname, "2" );
1832 $$ = $2->addTypedef();
1833 }
1834 | cfa_typedef_declaration pop ',' push identifier
1835 {
1836 typedefTable.addToEnclosingScope( *$5, TYPEDEFname, "3" );
1837 $$ = $1->appendList( $1->cloneType( $5 ) );
1838 }
1839 ;
1840
1841// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1842// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1843
1844typedef_declaration:
1845 TYPEDEF type_specifier declarator
1846 {
1847 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "4" );
1848 $$ = $3->addType( $2 )->addTypedef();
1849 }
1850 | typedef_declaration pop ',' push declarator
1851 {
1852 typedefTable.addToEnclosingScope( *$5->name, TYPEDEFname, "5" );
1853 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1854 }
1855 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1856 {
1857 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "6" );
1858 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1859 }
1860 | type_specifier TYPEDEF declarator
1861 {
1862 typedefTable.addToEnclosingScope( *$3->name, TYPEDEFname, "7" );
1863 $$ = $3->addType( $1 )->addTypedef();
1864 }
1865 | type_specifier TYPEDEF type_qualifier_list declarator
1866 {
1867 typedefTable.addToEnclosingScope( *$4->name, TYPEDEFname, "8" );
1868 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1869 }
1870 ;
1871
1872typedef_expression:
1873 // deprecated GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1874 TYPEDEF identifier '=' assignment_expression
1875 {
1876 SemanticError( yylloc, "Typedef expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
1877 }
1878 | typedef_expression pop ',' push identifier '=' assignment_expression
1879 {
1880 SemanticError( yylloc, "Typedef expression is deprecated, use typeof(...) instead." ); $$ = nullptr;
1881 }
1882 ;
1883
1884c_declaration:
1885 declaration_specifier declaring_list
1886 { $$ = distAttr( $1, $2 ); }
1887 | typedef_declaration
1888 | typedef_expression // deprecated GCC, naming expression type
1889 | sue_declaration_specifier
1890 ;
1891
1892declaring_list:
1893 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1894 // storage-class
1895 declarator asm_name_opt initializer_opt
1896 { $$ = $1->addAsmName( $2 )->addInitializer( $3 ); }
1897 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1898 { $$ = $1->appendList( $4->addQualifiers( $3 )->addAsmName( $5 )->addInitializer( $6 ) ); }
1899 ;
1900
1901declaration_specifier: // type specifier + storage class
1902 basic_declaration_specifier
1903 | sue_declaration_specifier
1904 | type_declaration_specifier
1905 ;
1906
1907declaration_specifier_nobody: // type specifier + storage class - {...}
1908 // Preclude SUE declarations in restricted scopes:
1909 //
1910 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1911 //
1912 // because it is impossible to call f due to name equivalence.
1913 basic_declaration_specifier
1914 | sue_declaration_specifier_nobody
1915 | type_declaration_specifier
1916 ;
1917
1918type_specifier: // type specifier
1919 basic_type_specifier
1920 | sue_type_specifier
1921 {
1922 // printf( "sue_type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
1923 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
1924 // printf( "\tattr %s\n", attr->name.c_str() );
1925 // } // for
1926 }
1927 | type_type_specifier
1928 ;
1929
1930type_specifier_nobody: // type specifier - {...}
1931 // Preclude SUE declarations in restricted scopes:
1932 //
1933 // int f( struct S { int i; } s1, Struct S s2 ) { struct S s3; ... }
1934 //
1935 // because it is impossible to call f due to name equivalence.
1936 basic_type_specifier
1937 | sue_type_specifier_nobody
1938 | type_type_specifier
1939 ;
1940
1941type_qualifier_list_opt: // GCC, used in asm_statement
1942 // empty
1943 { $$ = nullptr; }
1944 | type_qualifier_list
1945 ;
1946
1947type_qualifier_list:
1948 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1949 //
1950 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1951 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1952 // appeared only once.
1953 type_qualifier
1954 | type_qualifier_list type_qualifier
1955 { $$ = $1->addQualifiers( $2 ); }
1956 ;
1957
1958type_qualifier:
1959 type_qualifier_name
1960 | attribute // trick handles most atrribute locations
1961 ;
1962
1963type_qualifier_name:
1964 CONST
1965 { $$ = DeclarationNode::newTypeQualifier( Type::Const ); }
1966 | RESTRICT
1967 { $$ = DeclarationNode::newTypeQualifier( Type::Restrict ); }
1968 | VOLATILE
1969 { $$ = DeclarationNode::newTypeQualifier( Type::Volatile ); }
1970 | ATOMIC
1971 { $$ = DeclarationNode::newTypeQualifier( Type::Atomic ); }
1972 | forall
1973 ;
1974
1975forall:
1976 FORALL '(' type_parameter_list ')' // CFA
1977 { $$ = DeclarationNode::newForall( $3 ); }
1978 ;
1979
1980declaration_qualifier_list:
1981 storage_class_list
1982 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
1983 { $$ = $1->addQualifiers( $2 ); }
1984 | declaration_qualifier_list type_qualifier_list storage_class_list
1985 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1986 ;
1987
1988storage_class_list:
1989 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1990 // only one of each is specified, except for inline, which can appear with the others.
1991 //
1992 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1993 // specifiers in a declaration.
1994 storage_class
1995 | storage_class_list storage_class
1996 { $$ = $1->addQualifiers( $2 ); }
1997 ;
1998
1999storage_class:
2000 EXTERN
2001 { $$ = DeclarationNode::newStorageClass( Type::Extern ); }
2002 | STATIC
2003 { $$ = DeclarationNode::newStorageClass( Type::Static ); }
2004 | AUTO
2005 { $$ = DeclarationNode::newStorageClass( Type::Auto ); }
2006 | REGISTER
2007 { $$ = DeclarationNode::newStorageClass( Type::Register ); }
2008 | THREADLOCAL // C11
2009 { $$ = DeclarationNode::newStorageClass( Type::Threadlocal ); }
2010 // Put function specifiers here to simplify parsing rules, but separate them semantically.
2011 | INLINE // C99
2012 { $$ = DeclarationNode::newFuncSpecifier( Type::Inline ); }
2013 | FORTRAN // C99
2014 { $$ = DeclarationNode::newFuncSpecifier( Type::Fortran ); }
2015 | NORETURN // C11
2016 { $$ = DeclarationNode::newFuncSpecifier( Type::Noreturn ); }
2017 ;
2018
2019basic_type_name:
2020 VOID
2021 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2022 | BOOL // C99
2023 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
2024 | CHAR
2025 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
2026 | INT
2027 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
2028 | INT128
2029 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 ); }
2030 | UINT128
2031 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ); }
2032 | FLOAT
2033 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
2034 | DOUBLE
2035 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
2036 | uuFLOAT80
2037 { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat80 ); }
2038 | uuFLOAT128
2039 { $$ = DeclarationNode::newBasicType( DeclarationNode::uuFloat128 ); }
2040 | uFLOAT16
2041 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat16 ); }
2042 | uFLOAT32
2043 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32 ); }
2044 | uFLOAT32X
2045 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat32x ); }
2046 | uFLOAT64
2047 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64 ); }
2048 | uFLOAT64X
2049 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat64x ); }
2050 | uFLOAT128
2051 { $$ = DeclarationNode::newBasicType( DeclarationNode::uFloat128 ); }
2052 | DECIMAL32
2053 { SemanticError( yylloc, "_Decimal32 is currently unimplemented." ); $$ = nullptr; }
2054 | DECIMAL64
2055 { SemanticError( yylloc, "_Decimal64 is currently unimplemented." ); $$ = nullptr; }
2056 | DECIMAL128
2057 { SemanticError( yylloc, "_Decimal128 is currently unimplemented." ); $$ = nullptr; }
2058 | COMPLEX // C99
2059 { $$ = DeclarationNode::newComplexType( DeclarationNode::Complex ); }
2060 | IMAGINARY // C99
2061 { $$ = DeclarationNode::newComplexType( DeclarationNode::Imaginary ); }
2062 | SIGNED
2063 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Signed ); }
2064 | UNSIGNED
2065 { $$ = DeclarationNode::newSignedNess( DeclarationNode::Unsigned ); }
2066 | SHORT
2067 { $$ = DeclarationNode::newLength( DeclarationNode::Short ); }
2068 | LONG
2069 { $$ = DeclarationNode::newLength( DeclarationNode::Long ); }
2070 | VALIST // GCC, __builtin_va_list
2071 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
2072 | AUTO_TYPE
2073 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::AutoType ); }
2074 | vtable
2075 ;
2076
2077vtable_opt:
2078 // empty
2079 { $$ = nullptr; }
2080 | vtable
2081 ;
2082
2083vtable:
2084 VTABLE '(' type_name ')' default_opt
2085 { $$ = DeclarationNode::newVtableType( $3 ); }
2086 // { SemanticError( yylloc, "vtable is currently unimplemented." ); $$ = nullptr; }
2087 ;
2088
2089default_opt:
2090 // empty
2091 { $$ = nullptr; }
2092 | DEFAULT
2093 { SemanticError( yylloc, "vtable default is currently unimplemented." ); $$ = nullptr; }
2094 ;
2095
2096basic_declaration_specifier:
2097 // A semantic check is necessary for conflicting storage classes.
2098 basic_type_specifier
2099 | declaration_qualifier_list basic_type_specifier
2100 { $$ = $2->addQualifiers( $1 ); }
2101 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
2102 { $$ = $1->addQualifiers( $2 ); }
2103 | basic_declaration_specifier storage_class type_qualifier_list
2104 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2105 | basic_declaration_specifier storage_class basic_type_specifier
2106 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
2107 ;
2108
2109basic_type_specifier:
2110 direct_type
2111 // Cannot have type modifiers, e.g., short, long, etc.
2112 | type_qualifier_list_opt indirect_type type_qualifier_list_opt
2113 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
2114 ;
2115
2116direct_type:
2117 basic_type_name
2118 | type_qualifier_list basic_type_name
2119 { $$ = $2->addQualifiers( $1 ); }
2120 | direct_type type_qualifier
2121 { $$ = $1->addQualifiers( $2 ); }
2122 | direct_type basic_type_name
2123 { $$ = $1->addType( $2 ); }
2124 ;
2125
2126indirect_type:
2127 TYPEOF '(' type ')' // GCC: typeof( x ) y;
2128 { $$ = $3; }
2129 | TYPEOF '(' comma_expression ')' // GCC: typeof( a+b ) y;
2130 { $$ = DeclarationNode::newTypeof( $3 ); }
2131 | BASETYPEOF '(' type ')' // CFA: basetypeof( x ) y;
2132 { $$ = DeclarationNode::newTypeof( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ), true ); }
2133 | BASETYPEOF '(' comma_expression ')' // CFA: basetypeof( a+b ) y;
2134 { $$ = DeclarationNode::newTypeof( $3, true ); }
2135 | ZERO_T // CFA
2136 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Zero ); }
2137 | ONE_T // CFA
2138 { $$ = DeclarationNode::newBuiltinType( DeclarationNode::One ); }
2139 ;
2140
2141sue_declaration_specifier: // struct, union, enum + storage class + type specifier
2142 sue_type_specifier
2143 {
2144 // printf( "sue_declaration_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2145 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2146 // printf( "\tattr %s\n", attr->name.c_str() );
2147 // } // for
2148 }
2149 | declaration_qualifier_list sue_type_specifier
2150 { $$ = $2->addQualifiers( $1 ); }
2151 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
2152 { $$ = $1->addQualifiers( $2 ); }
2153 | sue_declaration_specifier storage_class type_qualifier_list
2154 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2155 ;
2156
2157sue_type_specifier: // struct, union, enum + type specifier
2158 elaborated_type
2159 {
2160 // printf( "sue_type_specifier %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2161 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2162 // printf( "\tattr %s\n", attr->name.c_str() );
2163 // } // for
2164 }
2165 | type_qualifier_list
2166 { if ( $1->type != nullptr && $1->type->forall ) forall = true; } // remember generic type
2167 elaborated_type
2168 { $$ = $3->addQualifiers( $1 ); }
2169 | sue_type_specifier type_qualifier
2170 {
2171 if ( $2->type != nullptr && $2->type->forall ) forall = true; // remember generic type
2172 $$ = $1->addQualifiers( $2 );
2173 }
2174 ;
2175
2176sue_declaration_specifier_nobody: // struct, union, enum - {...} + storage class + type specifier
2177 sue_type_specifier_nobody
2178 | declaration_qualifier_list sue_type_specifier_nobody
2179 { $$ = $2->addQualifiers( $1 ); }
2180 | sue_declaration_specifier_nobody storage_class // remaining OBSOLESCENT (see 2)
2181 { $$ = $1->addQualifiers( $2 ); }
2182 | sue_declaration_specifier_nobody storage_class type_qualifier_list
2183 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2184 ;
2185
2186sue_type_specifier_nobody: // struct, union, enum - {...} + type specifier
2187 elaborated_type_nobody
2188 | type_qualifier_list elaborated_type_nobody
2189 { $$ = $2->addQualifiers( $1 ); }
2190 | sue_type_specifier_nobody type_qualifier
2191 { $$ = $1->addQualifiers( $2 ); }
2192 ;
2193
2194type_declaration_specifier:
2195 type_type_specifier
2196 | declaration_qualifier_list type_type_specifier
2197 { $$ = $2->addQualifiers( $1 ); }
2198 | type_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
2199 { $$ = $1->addQualifiers( $2 ); }
2200 | type_declaration_specifier storage_class type_qualifier_list
2201 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
2202 ;
2203
2204type_type_specifier: // typedef types
2205 type_name
2206 | type_qualifier_list type_name
2207 { $$ = $2->addQualifiers( $1 ); }
2208 | type_type_specifier type_qualifier
2209 { $$ = $1->addQualifiers( $2 ); }
2210 ;
2211
2212type_name:
2213 TYPEDEFname
2214 { $$ = DeclarationNode::newFromTypedef( $1 ); }
2215 | '.' TYPEDEFname
2216 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); }
2217 | type_name '.' TYPEDEFname
2218 { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); }
2219 | typegen_name
2220 | '.' typegen_name
2221 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); }
2222 | type_name '.' typegen_name
2223 { $$ = DeclarationNode::newQualifiedType( $1, $3 ); }
2224 ;
2225
2226typegen_name: // CFA
2227 TYPEGENname
2228 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2229 | TYPEGENname '(' ')'
2230 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); }
2231 | TYPEGENname '(' type_list ')'
2232 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
2233 ;
2234
2235elaborated_type: // struct, union, enum
2236 aggregate_type
2237 {
2238 // printf( "elaborated_type %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2239 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2240 // printf( "\tattr %s\n", attr->name.c_str() );
2241 // } // for
2242 }
2243 | enum_type
2244 ;
2245
2246elaborated_type_nobody: // struct, union, enum - {...}
2247 aggregate_type_nobody
2248 | enum_type_nobody
2249 ;
2250
2251aggregate_type: // struct, union
2252 aggregate_key attribute_list_opt
2253 { forall = false; } // reset
2254 '{' field_declaration_list_opt '}' type_parameters_opt
2255 { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); }
2256 | aggregate_key attribute_list_opt identifier
2257 {
2258 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2259 forall = false; // reset
2260 }
2261 '{' field_declaration_list_opt '}' type_parameters_opt
2262 {
2263 // printf( "aggregate_type1 %s\n", $3.str->c_str() );
2264 // if ( $2 )
2265 // for ( Attribute * attr: reverseIterate( $2->attributes ) ) {
2266 // printf( "copySpecifiers12 %s\n", attr->name.c_str() );
2267 // } // for
2268 $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2269 // printf( "aggregate_type2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2270 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2271 // printf( "aggregate_type3 %s\n", attr->name.c_str() );
2272 // } // for
2273 }
2274 | aggregate_key attribute_list_opt TYPEDEFname // unqualified type name
2275 {
2276 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2277 forall = false; // reset
2278 }
2279 '{' field_declaration_list_opt '}' type_parameters_opt
2280 {
2281 // printf( "AGG3\n" );
2282 DeclarationNode::newFromTypedef( $3 );
2283 $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2284 }
2285 | aggregate_key attribute_list_opt TYPEGENname // unqualified type name
2286 {
2287 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef
2288 forall = false; // reset
2289 }
2290 '{' field_declaration_list_opt '}' type_parameters_opt
2291 {
2292 // printf( "AGG4\n" );
2293 DeclarationNode::newFromTypeGen( $3, nullptr );
2294 $$ = DeclarationNode::newAggregate( $1, $3, $8, $6, true )->addQualifiers( $2 );
2295 }
2296 | aggregate_type_nobody
2297 ;
2298
2299type_parameters_opt:
2300 // empty
2301 { $$ = nullptr; } %prec '}'
2302 | '(' type_list ')'
2303 { $$ = $2; }
2304 ;
2305
2306aggregate_type_nobody: // struct, union - {...}
2307 aggregate_key attribute_list_opt identifier
2308 {
2309 typedefTable.makeTypedef( *$3, forall || typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname );
2310 forall = false; // reset
2311 $$ = DeclarationNode::newAggregate( $1, $3, nullptr, nullptr, false )->addQualifiers( $2 );
2312 }
2313 | aggregate_key attribute_list_opt type_name
2314 {
2315 forall = false; // reset
2316 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is
2317 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and
2318 // delete newFromTypeGen.
2319 $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 );
2320 $3->type->symbolic.name = nullptr;
2321 $3->type->symbolic.actuals = nullptr;
2322 delete $3;
2323 }
2324 ;
2325
2326aggregate_key:
2327 aggregate_data
2328 | aggregate_control
2329 ;
2330
2331aggregate_data:
2332 STRUCT vtable_opt
2333 { $$ = AggregateDecl::Struct; }
2334 | UNION
2335 { $$ = AggregateDecl::Union; }
2336 | EXCEPTION // CFA
2337 { $$ = AggregateDecl::Exception; }
2338 // { SemanticError( yylloc, "exception aggregate is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2339 ;
2340
2341aggregate_control: // CFA
2342 MONITOR
2343 { $$ = AggregateDecl::Monitor; }
2344 | MUTEX STRUCT
2345 { $$ = AggregateDecl::Monitor; }
2346 | GENERATOR
2347 { $$ = AggregateDecl::Generator; }
2348 | MUTEX GENERATOR
2349 { SemanticError( yylloc, "monitor generator is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2350 | COROUTINE
2351 { $$ = AggregateDecl::Coroutine; }
2352 | MUTEX COROUTINE
2353 { SemanticError( yylloc, "monitor coroutine is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2354 | THREAD
2355 { $$ = AggregateDecl::Thread; }
2356 | MUTEX THREAD
2357 { SemanticError( yylloc, "monitor thread is currently unimplemented." ); $$ = AggregateDecl::NoAggregate; }
2358 ;
2359
2360field_declaration_list_opt:
2361 // empty
2362 { $$ = nullptr; }
2363 | field_declaration_list_opt field_declaration
2364 { $$ = $1 ? $1->appendList( $2 ) : $2; }
2365 ;
2366
2367field_declaration:
2368 type_specifier field_declaring_list_opt ';'
2369 {
2370 // printf( "type_specifier1 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2371 $$ = fieldDecl( $1, $2 );
2372 // printf( "type_specifier2 %p %s\n", $$, $$->type->aggregate.name ? $$->type->aggregate.name->c_str() : "(nil)" );
2373 // for ( Attribute * attr: reverseIterate( $$->attributes ) ) {
2374 // printf( "\tattr %s\n", attr->name.c_str() );
2375 // } // for
2376 }
2377 | EXTENSION type_specifier field_declaring_list_opt ';' // GCC
2378 { $$ = fieldDecl( $2, $3 ); distExt( $$ ); }
2379 | INLINE type_specifier field_abstract_list_opt ';' // CFA
2380 {
2381 if ( ! $3 ) { // field declarator ?
2382 $3 = DeclarationNode::newName( nullptr );
2383 } // if
2384 $3->inLine = true;
2385 $$ = distAttr( $2, $3 ); // mark all fields in list
2386 distInl( $3 );
2387 }
2388 | INLINE aggregate_control ';' // CFA
2389 { SemanticError( yylloc, "INLINE aggregate control currently unimplemented." ); $$ = nullptr; }
2390 | typedef_declaration ';' // CFA
2391 | cfa_field_declaring_list ';' // CFA, new style field declaration
2392 | EXTENSION cfa_field_declaring_list ';' // GCC
2393 { distExt( $2 ); $$ = $2; } // mark all fields in list
2394 | INLINE cfa_field_abstract_list ';' // CFA, new style field declaration
2395 { $$ = $2; } // mark all fields in list
2396 | cfa_typedef_declaration ';' // CFA
2397 | static_assert // C11
2398 ;
2399
2400field_declaring_list_opt:
2401 // empty
2402 { $$ = nullptr; }
2403 | field_declarator
2404 | field_declaring_list_opt ',' attribute_list_opt field_declarator
2405 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2406 ;
2407
2408field_declarator:
2409 bit_subrange_size // C special case, no field name
2410 { $$ = DeclarationNode::newBitfield( $1 ); }
2411 | variable_declarator bit_subrange_size_opt
2412 // A semantic check is required to ensure bit_subrange only appears on integral types.
2413 { $$ = $1->addBitfield( $2 ); }
2414 | variable_type_redeclarator bit_subrange_size_opt
2415 // A semantic check is required to ensure bit_subrange only appears on integral types.
2416 { $$ = $1->addBitfield( $2 ); }
2417 ;
2418
2419field_abstract_list_opt:
2420 // empty
2421 { $$ = nullptr; }
2422 | field_abstract
2423 | field_abstract_list_opt ',' attribute_list_opt field_abstract
2424 { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
2425 ;
2426
2427field_abstract:
2428 // no bit fields
2429 variable_abstract_declarator
2430 ;
2431
2432cfa_field_declaring_list: // CFA, new style field declaration
2433 // bit-fields are handled by C declarations
2434 cfa_abstract_declarator_tuple identifier_or_type_name
2435 { $$ = $1->addName( $2 ); }
2436 | cfa_field_declaring_list ',' identifier_or_type_name
2437 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
2438 ;
2439
2440cfa_field_abstract_list: // CFA, new style field declaration
2441 // bit-fields are handled by C declarations
2442 cfa_abstract_declarator_tuple
2443 | cfa_field_abstract_list ','
2444 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
2445 ;
2446
2447bit_subrange_size_opt:
2448 // empty
2449 { $$ = nullptr; }
2450 | bit_subrange_size
2451 ;
2452
2453bit_subrange_size:
2454 ':' assignment_expression
2455 { $$ = $2; }
2456 ;
2457
2458enum_type:
2459 ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
2460 { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); }
2461 | ENUM attribute_list_opt identifier
2462 { typedefTable.makeTypedef( *$3 ); }
2463 '{' enumerator_list comma_opt '}'
2464 { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
2465 | ENUM attribute_list_opt typedef_name // unqualified type name
2466 '{' enumerator_list comma_opt '}'
2467 { $$ = DeclarationNode::newEnum( $3->name, $5, true )->addQualifiers( $2 ); }
2468 | ENUM '(' ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2469 { SemanticError( yylloc, "Unvalued enumerated type is currently unimplemented." ); $$ = nullptr; }
2470 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt '{' enumerator_list comma_opt '}'
2471 {
2472 if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 )
2473 { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
2474
2475 $$ = DeclarationNode::newEnum( nullptr, $7, true, $3 )->addQualifiers( $5 );
2476 }
2477 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt identifier attribute_list_opt
2478 {
2479 if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 ) { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
2480 typedefTable.makeTypedef( *$6 );
2481 }
2482 '{' enumerator_list comma_opt '}'
2483 {
2484 $$ = DeclarationNode::newEnum( $6, $10, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2485 }
2486 | ENUM '(' cfa_abstract_parameter_declaration ')' attribute_list_opt typedef_name attribute_list_opt '{' enumerator_list comma_opt '}'
2487 {
2488 if ( $3->storageClasses.val != 0 || $3->type->qualifiers.val != 0 ) { SemanticError( yylloc, "storage-class and CV qualifiers are not meaningful for enumeration constants, which are const." ); }
2489 typedefTable.makeTypedef( *$6->name );
2490 $$ = DeclarationNode::newEnum( $6->name, $9, true, $3 )->addQualifiers( $5 )->addQualifiers( $7 );
2491 }
2492 | enum_type_nobody
2493 ;
2494
2495enum_type_nobody: // enum - {...}
2496 ENUM attribute_list_opt identifier
2497 { typedefTable.makeTypedef( *$3 ); $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 ); }
2498 | ENUM attribute_list_opt type_name // qualified type name
2499 { typedefTable.makeTypedef( *$3->type->symbolic.name ); $$ = DeclarationNode::newEnum( $3->type->symbolic.name, 0, false )->addQualifiers( $2 ); }
2500 ;
2501
2502enumerator_list:
2503 identifier_or_type_name enumerator_value_opt
2504 { $$ = DeclarationNode::newEnumValueGeneric( $1, $2 ); }
2505 | INLINE type_name
2506 { $$ = DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ); }
2507 | enumerator_list ',' identifier_or_type_name enumerator_value_opt
2508 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( $3, $4 ) ); }
2509 | enumerator_list ',' INLINE type_name enumerator_value_opt
2510 { $$ = $1->appendList( DeclarationNode::newEnumValueGeneric( new string("inline"), nullptr ) ); }
2511 ;
2512
2513enumerator_value_opt:
2514 // empty
2515 { $$ = nullptr; }
2516 | '=' constant_expression { $$ = new InitializerNode( $2 ); }
2517 | '=' '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $3, true ); }
2518 // | simple_assignment_operator initializer
2519 // { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2520 ;
2521
2522cfa_parameter_ellipsis_list_opt: // CFA, abstract + real
2523 // empty
2524 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
2525 | ELLIPSIS
2526 { $$ = nullptr; }
2527 | cfa_abstract_parameter_list
2528 | cfa_parameter_list
2529 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
2530 { $$ = $1->appendList( $5 ); }
2531 | cfa_abstract_parameter_list pop ',' push ELLIPSIS
2532 { $$ = $1->addVarArgs(); }
2533 | cfa_parameter_list pop ',' push ELLIPSIS
2534 { $$ = $1->addVarArgs(); }
2535 ;
2536
2537cfa_parameter_list: // CFA
2538 // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
2539 // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
2540 cfa_parameter_declaration
2541 | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2542 { $$ = $1->appendList( $5 ); }
2543 | cfa_parameter_list pop ',' push cfa_parameter_declaration
2544 { $$ = $1->appendList( $5 ); }
2545 | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
2546 { $$ = $1->appendList( $5 )->appendList( $9 ); }
2547 ;
2548
2549cfa_abstract_parameter_list: // CFA, new & old style abstract
2550 cfa_abstract_parameter_declaration
2551 | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
2552 { $$ = $1->appendList( $5 ); }
2553 ;
2554
2555parameter_type_list_opt:
2556 // empty
2557 { $$ = nullptr; }
2558 | ELLIPSIS
2559 { $$ = nullptr; }
2560 | parameter_list
2561 | parameter_list pop ',' push ELLIPSIS
2562 { $$ = $1->addVarArgs(); }
2563 ;
2564
2565parameter_list: // abstract + real
2566 abstract_parameter_declaration
2567 | parameter_declaration
2568 | parameter_list pop ',' push abstract_parameter_declaration
2569 { $$ = $1->appendList( $5 ); }
2570 | parameter_list pop ',' push parameter_declaration
2571 { $$ = $1->appendList( $5 ); }
2572 ;
2573
2574// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
2575// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
2576
2577cfa_parameter_declaration: // CFA, new & old style parameter declaration
2578 parameter_declaration
2579 | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name default_initializer_opt
2580 { $$ = $1->addName( $2 ); }
2581 | cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2582 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2583 { $$ = $1->addName( $2 ); }
2584 | type_qualifier_list cfa_abstract_tuple identifier_or_type_name default_initializer_opt
2585 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
2586 | cfa_function_specifier
2587 ;
2588
2589cfa_abstract_parameter_declaration: // CFA, new & old style parameter declaration
2590 abstract_parameter_declaration
2591 | cfa_identifier_parameter_declarator_no_tuple
2592 | cfa_abstract_tuple
2593 // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
2594 | type_qualifier_list cfa_abstract_tuple
2595 { $$ = $2->addQualifiers( $1 ); }
2596 | cfa_abstract_function
2597 ;
2598
2599parameter_declaration:
2600 // No SUE declaration in parameter list.
2601 declaration_specifier_nobody identifier_parameter_declarator default_initializer_opt
2602 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2603 | declaration_specifier_nobody type_parameter_redeclarator default_initializer_opt
2604 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2605 ;
2606
2607abstract_parameter_declaration:
2608 declaration_specifier_nobody default_initializer_opt
2609 { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
2610 | declaration_specifier_nobody abstract_parameter_declarator default_initializer_opt
2611 { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
2612 ;
2613
2614// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
2615// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
2616// identifiers. The ANSI-style parameter-list can redefine a typedef name.
2617
2618identifier_list: // K&R-style parameter list => no types
2619 identifier
2620 { $$ = DeclarationNode::newName( $1 ); }
2621 | identifier_list ',' identifier
2622 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
2623 ;
2624
2625identifier_or_type_name:
2626 identifier
2627 | TYPEDEFname
2628 | TYPEGENname
2629 ;
2630
2631type_no_function: // sizeof, alignof, cast (constructor)
2632 cfa_abstract_declarator_tuple // CFA
2633 | type_specifier
2634 | type_specifier abstract_declarator
2635 { $$ = $2->addType( $1 ); }
2636 ;
2637
2638type: // typeof, assertion
2639 type_no_function
2640 | cfa_abstract_function // CFA
2641 ;
2642
2643initializer_opt:
2644 // empty
2645 { $$ = nullptr; }
2646 | simple_assignment_operator initializer { $$ = $1 == OperKinds::Assign ? $2 : $2->set_maybeConstructed( false ); }
2647 | '=' VOID { $$ = new InitializerNode( true ); }
2648 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2649 ;
2650
2651initializer:
2652 assignment_expression { $$ = new InitializerNode( $1 ); }
2653 | '{' initializer_list_opt comma_opt '}' { $$ = new InitializerNode( $2, true ); }
2654 ;
2655
2656initializer_list_opt:
2657 // empty
2658 { $$ = nullptr; }
2659 | initializer
2660 | designation initializer { $$ = $2->set_designators( $1 ); }
2661 | initializer_list_opt ',' initializer { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
2662 | initializer_list_opt ',' designation initializer { $$ = (InitializerNode *)($1->set_last( $4->set_designators( $3 ) )); }
2663 ;
2664
2665// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
2666// '=' to separator the designator from the initializer value, as in:
2667//
2668// int x[10] = { [1] = 3 };
2669//
2670// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
2671// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
2672// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
2673// shift/reduce conflicts
2674
2675designation:
2676 designator_list ':' // C99, CFA uses ":" instead of "="
2677 | identifier_at ':' // GCC, field name
2678 { $$ = new ExpressionNode( build_varref( $1 ) ); }
2679 ;
2680
2681designator_list: // C99
2682 designator
2683 | designator_list designator
2684 { $$ = (ExpressionNode *)($1->set_last( $2 )); }
2685 //| designator_list designator { $$ = new ExpressionNode( $1, $2 ); }
2686 ;
2687
2688designator:
2689 '.' identifier_at // C99, field name
2690 { $$ = new ExpressionNode( build_varref( $2 ) ); }
2691 | '[' push assignment_expression pop ']' // C99, single array element
2692 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
2693 { $$ = $3; }
2694 | '[' push subrange pop ']' // CFA, multiple array elements
2695 { $$ = $3; }
2696 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
2697 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $3 ), maybeMoveBuild<Expression>( $5 ) ) ); }
2698 | '.' '[' push field_name_list pop ']' // CFA, tuple field selector
2699 { $$ = $4; }
2700 ;
2701
2702// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
2703// rather than an object-oriented type system. This required four groups of extensions:
2704//
2705// Overloading: function, data, and operator identifiers may be overloaded.
2706//
2707// Type declarations: "otype" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
2708// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
2709// definitions of new types. Type declarations with storage class "extern" provide opaque types.
2710//
2711// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
2712// site. A polymorphic function is not a template; it is a function, with an address and a type.
2713//
2714// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
2715// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
2716// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
2717// types provide the operations declared by a specification. Assertions are normally used to declare requirements
2718// on type arguments of polymorphic functions.
2719
2720type_parameter_list: // CFA
2721 type_parameter
2722 | type_parameter_list ',' type_parameter
2723 { $$ = $1->appendList( $3 ); }
2724 ;
2725
2726type_initializer_opt: // CFA
2727 // empty
2728 { $$ = nullptr; }
2729 | '=' type
2730 { $$ = $2; }
2731 ;
2732
2733type_parameter: // CFA
2734 type_class identifier_or_type_name
2735 {
2736 typedefTable.addToScope( *$2, TYPEDEFname, "9" );
2737 if ( $1 == TypeDecl::Otype ) { SemanticError( yylloc, "otype keyword is deprecated, use T " ); }
2738 if ( $1 == TypeDecl::Dtype ) { SemanticError( yylloc, "dtype keyword is deprecated, use T &" ); }
2739 if ( $1 == TypeDecl::Ttype ) { SemanticError( yylloc, "ttype keyword is deprecated, use T ..." ); }
2740 }
2741 type_initializer_opt assertion_list_opt
2742 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2743 | identifier_or_type_name new_type_class
2744 { typedefTable.addToScope( *$1, TYPEDEFname, "9" ); }
2745 type_initializer_opt assertion_list_opt
2746 { $$ = DeclarationNode::newTypeParam( $2, $1 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2747 | '[' identifier_or_type_name ']'
2748 {
2749 typedefTable.addToScope( *$2, TYPEDIMname, "9" );
2750 $$ = DeclarationNode::newTypeParam( TypeDecl::Dimension, $2 );
2751 }
2752 // | type_specifier identifier_parameter_declarator
2753 | assertion_list
2754 { $$ = DeclarationNode::newTypeParam( TypeDecl::Dtype, new string( DeclarationNode::anonymous.newName() ) )->addAssertions( $1 ); }
2755 ;
2756
2757new_type_class: // CFA
2758 // empty
2759 { $$ = TypeDecl::Otype; }
2760 | '&'
2761 { $$ = TypeDecl::Dtype; }
2762 | '*'
2763 { $$ = TypeDecl::DStype; } // dtype + sized
2764 // | '(' '*' ')'
2765 // { $$ = TypeDecl::Ftype; }
2766 | ELLIPSIS
2767 { $$ = TypeDecl::Ttype; }
2768 ;
2769
2770type_class: // CFA
2771 OTYPE
2772 { $$ = TypeDecl::Otype; }
2773 | DTYPE
2774 { $$ = TypeDecl::Dtype; }
2775 | FTYPE
2776 { $$ = TypeDecl::Ftype; }
2777 | TTYPE
2778 { $$ = TypeDecl::Ttype; }
2779 ;
2780
2781assertion_list_opt: // CFA
2782 // empty
2783 { $$ = nullptr; }
2784 | assertion_list
2785 ;
2786
2787assertion_list: // CFA
2788 assertion
2789 | assertion_list assertion
2790 { $$ = $1->appendList( $2 ); }
2791 ;
2792
2793assertion: // CFA
2794 '|' identifier_or_type_name '(' type_list ')'
2795 { $$ = DeclarationNode::newTraitUse( $2, $4 ); }
2796 | '|' '{' push trait_declaration_list pop '}'
2797 { $$ = $4; }
2798 // | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list pop '}' '(' type_list ')'
2799 // { SemanticError( yylloc, "Generic data-type assertion is currently unimplemented." ); $$ = nullptr; }
2800 ;
2801
2802type_list: // CFA
2803 type
2804 { $$ = new ExpressionNode( new TypeExpr( maybeMoveBuildType( $1 ) ) ); }
2805 | assignment_expression
2806 | type_list ',' type
2807 { $$ = (ExpressionNode *)($1->set_last( new ExpressionNode( new TypeExpr( maybeMoveBuildType( $3 ) ) ) )); }
2808 | type_list ',' assignment_expression
2809 { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2810 ;
2811
2812type_declaring_list: // CFA
2813 OTYPE type_declarator
2814 { $$ = $2; }
2815 | storage_class_list OTYPE type_declarator
2816 { $$ = $3->addQualifiers( $1 ); }
2817 | type_declaring_list ',' type_declarator
2818 { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2819 ;
2820
2821type_declarator: // CFA
2822 type_declarator_name assertion_list_opt
2823 { $$ = $1->addAssertions( $2 ); }
2824 | type_declarator_name assertion_list_opt '=' type
2825 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2826 ;
2827
2828type_declarator_name: // CFA
2829 identifier_or_type_name
2830 {
2831 typedefTable.addToEnclosingScope( *$1, TYPEDEFname, "10" );
2832 $$ = DeclarationNode::newTypeDecl( $1, 0 );
2833 }
2834 | identifier_or_type_name '(' type_parameter_list ')'
2835 {
2836 typedefTable.addToEnclosingScope( *$1, TYPEGENname, "11" );
2837 $$ = DeclarationNode::newTypeDecl( $1, $3 );
2838 }
2839 ;
2840
2841trait_specifier: // CFA
2842 TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' '}'
2843 { $$ = DeclarationNode::newTrait( $2, $4, 0 ); }
2844 | TRAIT identifier_or_type_name '(' type_parameter_list ')' '{' push trait_declaration_list pop '}'
2845 { $$ = DeclarationNode::newTrait( $2, $4, $8 ); }
2846 ;
2847
2848trait_declaration_list: // CFA
2849 trait_declaration
2850 | trait_declaration_list pop push trait_declaration
2851 { $$ = $1->appendList( $4 ); }
2852 ;
2853
2854trait_declaration: // CFA
2855 cfa_trait_declaring_list ';'
2856 | trait_declaring_list ';'
2857 ;
2858
2859cfa_trait_declaring_list: // CFA
2860 cfa_variable_specifier
2861 | cfa_function_specifier
2862 | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2863 { $$ = $1->appendList( $1->cloneType( $5 ) ); }
2864 ;
2865
2866trait_declaring_list: // CFA
2867 type_specifier declarator
2868 { $$ = $2->addType( $1 ); }
2869 | trait_declaring_list pop ',' push declarator
2870 { $$ = $1->appendList( $1->cloneBaseType( $5 ) ); }
2871 ;
2872
2873//***************************** EXTERNAL DEFINITIONS *****************************
2874
2875translation_unit:
2876 // empty, input file
2877 | external_definition_list
2878 { parseTree = parseTree ? parseTree->appendList( $1 ) : $1; }
2879 ;
2880
2881external_definition_list:
2882 push external_definition pop
2883 { $$ = $2; }
2884 | external_definition_list push external_definition pop
2885 { $$ = $1 ? $1->appendList( $3 ) : $3; }
2886 ;
2887
2888external_definition_list_opt:
2889 // empty
2890 { $$ = nullptr; }
2891 | external_definition_list
2892 ;
2893
2894up:
2895 { typedefTable.up( forall ); forall = false; }
2896 ;
2897
2898down:
2899 { typedefTable.down(); }
2900 ;
2901
2902external_definition:
2903 DIRECTIVE
2904 { $$ = DeclarationNode::newDirectiveStmt( new StatementNode( build_directive( $1 ) ) ); }
2905 | declaration
2906 | external_function_definition
2907 | EXTENSION external_definition // GCC, multiple __extension__ allowed, meaning unknown
2908 {
2909 distExt( $2 ); // mark all fields in list
2910 $$ = $2;
2911 }
2912 | ASM '(' string_literal ')' ';' // GCC, global assembler statement
2913 { $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asm( false, $3, 0 ) ) ); }
2914 | EXTERN STRINGliteral
2915 {
2916 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2917 linkage = LinkageSpec::update( yylloc, linkage, $2 );
2918 }
2919 up external_definition down
2920 {
2921 linkage = linkageStack.top();
2922 linkageStack.pop();
2923 $$ = $5;
2924 }
2925 | EXTERN STRINGliteral // C++-style linkage specifier
2926 {
2927 linkageStack.push( linkage ); // handle nested extern "C"/"Cforall"
2928 linkage = LinkageSpec::update( yylloc, linkage, $2 );
2929 }
2930 '{' up external_definition_list_opt down '}'
2931 {
2932 linkage = linkageStack.top();
2933 linkageStack.pop();
2934 $$ = $6;
2935 }
2936 | type_qualifier_list
2937 {
2938 if ( $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2939 if ( $1->type->forall ) forall = true; // remember generic type
2940 }
2941 '{' up external_definition_list_opt down '}' // CFA, namespace
2942 {
2943 distQual( $5, $1 );
2944 forall = false;
2945 $$ = $5;
2946 }
2947 | declaration_qualifier_list
2948 {
2949 if ( $1->type && $1->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2950 if ( $1->type && $1->type->forall ) forall = true; // remember generic type
2951 }
2952 '{' up external_definition_list_opt down '}' // CFA, namespace
2953 {
2954 distQual( $5, $1 );
2955 forall = false;
2956 $$ = $5;
2957 }
2958 | declaration_qualifier_list type_qualifier_list
2959 {
2960 if ( ($1->type && $1->type->qualifiers.val) || $2->type->qualifiers.val ) { SemanticError( yylloc, "CV qualifiers cannot be distributed; only storage-class and forall qualifiers." ); }
2961 if ( ($1->type && $1->type->forall) || $2->type->forall ) forall = true; // remember generic type
2962 }
2963 '{' up external_definition_list_opt down '}' // CFA, namespace
2964 {
2965 distQual( $6, $1->addQualifiers( $2 ) );
2966 forall = false;
2967 $$ = $6;
2968 }
2969 ;
2970
2971external_function_definition:
2972 function_definition
2973 // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2974 // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2975 // Parsing is possible because function_definition does not appear in the context of an expression (nested
2976 // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2977 // declaration must still have a type_specifier. OBSOLESCENT (see 1)
2978 | function_declarator compound_statement
2979 { $$ = $1->addFunctionBody( $2 ); }
2980 | KR_function_declarator KR_parameter_list_opt compound_statement
2981 { $$ = $1->addOldDeclList( $2 )->addFunctionBody( $3 ); }
2982 ;
2983
2984with_clause_opt:
2985 // empty
2986 { $$ = nullptr; forall = false; }
2987 | WITH '(' tuple_expression_list ')' attribute_list_opt
2988 {
2989 $$ = $3; forall = false;
2990 if ( $5 ) {
2991 SemanticError( yylloc, "Attributes cannot be associated with function body. Move attribute(s) before \"with\" clause." );
2992 $$ = nullptr;
2993 } // if
2994 }
2995 ;
2996
2997function_definition:
2998 cfa_function_declaration with_clause_opt compound_statement // CFA
2999 {
3000 // Add the function body to the last identifier in the function definition list, i.e., foo3:
3001 // [const double] foo1(), foo2( int ), foo3( double ) { return 3.0; }
3002 $1->get_last()->addFunctionBody( $3, $2 );
3003 $$ = $1;
3004 }
3005 | declaration_specifier function_declarator with_clause_opt compound_statement
3006 {
3007 rebindForall( $1, $2 );
3008 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3009 }
3010 | declaration_specifier variable_type_redeclarator with_clause_opt compound_statement
3011 {
3012 rebindForall( $1, $2 );
3013 $$ = $2->addFunctionBody( $4, $3 )->addType( $1 );
3014 }
3015 // handles default int return type, OBSOLESCENT (see 1)
3016 | type_qualifier_list function_declarator with_clause_opt compound_statement
3017 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3018 // handles default int return type, OBSOLESCENT (see 1)
3019 | declaration_qualifier_list function_declarator with_clause_opt compound_statement
3020 { $$ = $2->addFunctionBody( $4, $3 )->addQualifiers( $1 ); }
3021 // handles default int return type, OBSOLESCENT (see 1)
3022 | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
3023 { $$ = $3->addFunctionBody( $5, $4 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3024
3025 // Old-style K&R function definition, OBSOLESCENT (see 4)
3026 | declaration_specifier KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3027 {
3028 rebindForall( $1, $2 );
3029 $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addType( $1 );
3030 }
3031 // handles default int return type, OBSOLESCENT (see 1)
3032 | type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3033 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3034 // handles default int return type, OBSOLESCENT (see 1)
3035 | declaration_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3036 { $$ = $2->addOldDeclList( $3 )->addFunctionBody( $5, $4 )->addQualifiers( $1 ); }
3037 // handles default int return type, OBSOLESCENT (see 1)
3038 | declaration_qualifier_list type_qualifier_list KR_function_declarator KR_parameter_list_opt with_clause_opt compound_statement
3039 { $$ = $3->addOldDeclList( $4 )->addFunctionBody( $6, $5 )->addQualifiers( $2 )->addQualifiers( $1 ); }
3040 ;
3041
3042declarator:
3043 variable_declarator
3044 | variable_type_redeclarator
3045 | function_declarator
3046 ;
3047
3048subrange:
3049 constant_expression '~' constant_expression // CFA, integer subrange
3050 { $$ = new ExpressionNode( new RangeExpr( maybeMoveBuild<Expression>( $1 ), maybeMoveBuild<Expression>( $3 ) ) ); }
3051 ;
3052
3053asm_name_opt: // GCC
3054 // empty
3055 { $$ = nullptr; }
3056 | ASM '(' string_literal ')' attribute_list_opt
3057 {
3058 DeclarationNode * name = new DeclarationNode();
3059 name->asmName = $3;
3060 $$ = name->addQualifiers( $5 );
3061 }
3062 ;
3063
3064attribute_list_opt: // GCC
3065 // empty
3066 { $$ = nullptr; }
3067 | attribute_list
3068 ;
3069
3070attribute_list: // GCC
3071 attribute
3072 | attribute_list attribute
3073 { $$ = $2->addQualifiers( $1 ); }
3074 ;
3075
3076attribute: // GCC
3077 ATTRIBUTE '(' '(' attribute_name_list ')' ')'
3078 { $$ = $4; }
3079 ;
3080
3081attribute_name_list: // GCC
3082 attribute_name
3083 | attribute_name_list ',' attribute_name
3084 { $$ = $3->addQualifiers( $1 ); }
3085 ;
3086
3087attribute_name: // GCC
3088 // empty
3089 { $$ = nullptr; }
3090 | attr_name
3091 { $$ = DeclarationNode::newAttribute( $1 ); }
3092 | attr_name '(' argument_expression_list_opt ')'
3093 { $$ = DeclarationNode::newAttribute( $1, $3 ); }
3094 ;
3095
3096attr_name: // GCC
3097 IDENTIFIER
3098 | quasi_keyword
3099 | TYPEDEFname
3100 | TYPEGENname
3101 | FALLTHROUGH
3102 { $$ = Token{ new string( "fallthrough" ), { nullptr, -1 } }; }
3103 | CONST
3104 { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
3105 ;
3106
3107// ============================================================================
3108// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
3109// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
3110// in:
3111//
3112// int (*f())[10] { ... };
3113// ... (*f())[3] += 1; // definition mimics usage
3114//
3115// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
3116// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
3117// ============================================================================
3118
3119// ----------------------------------------------------------------------------
3120// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
3121// to define a variable or function prototype, e.g.:
3122//
3123// valid declaration invalid definition
3124// ----------------- ------------------
3125// int f; int f {}
3126// int *f; int *f {}
3127// int f[10]; int f[10] {}
3128// int (*f)(int); int (*f)(int) {}
3129//
3130// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
3131// variable_declarator and function_declarator.
3132// ----------------------------------------------------------------------------
3133
3134// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
3135// declaring an array of functions versus a pointer to an array of functions.
3136
3137paren_identifier:
3138 identifier_at
3139 { $$ = DeclarationNode::newName( $1 ); }
3140 | '(' paren_identifier ')' // redundant parenthesis
3141 { $$ = $2; }
3142 ;
3143
3144variable_declarator:
3145 paren_identifier attribute_list_opt
3146 { $$ = $1->addQualifiers( $2 ); }
3147 | variable_ptr
3148 | variable_array attribute_list_opt
3149 { $$ = $1->addQualifiers( $2 ); }
3150 | variable_function attribute_list_opt
3151 { $$ = $1->addQualifiers( $2 ); }
3152 ;
3153
3154variable_ptr:
3155 ptrref_operator variable_declarator
3156 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3157 | ptrref_operator type_qualifier_list variable_declarator
3158 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3159 | '(' variable_ptr ')' attribute_list_opt // redundant parenthesis
3160 { $$ = $2->addQualifiers( $4 ); }
3161 | '(' attribute_list variable_ptr ')' attribute_list_opt // redundant parenthesis
3162 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3163 ;
3164
3165variable_array:
3166 paren_identifier array_dimension
3167 { $$ = $1->addArray( $2 ); }
3168 | '(' variable_ptr ')' array_dimension
3169 { $$ = $2->addArray( $4 ); }
3170 | '(' attribute_list variable_ptr ')' array_dimension
3171 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3172 | '(' variable_array ')' multi_array_dimension // redundant parenthesis
3173 { $$ = $2->addArray( $4 ); }
3174 | '(' attribute_list variable_array ')' multi_array_dimension // redundant parenthesis
3175 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3176 | '(' variable_array ')' // redundant parenthesis
3177 { $$ = $2; }
3178 | '(' attribute_list variable_array ')' // redundant parenthesis
3179 { $$ = $3->addQualifiers( $2 ); }
3180 ;
3181
3182variable_function:
3183 '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3184 { $$ = $2->addParamList( $6 ); }
3185 | '(' attribute_list variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3186 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3187 | '(' variable_function ')' // redundant parenthesis
3188 { $$ = $2; }
3189 | '(' attribute_list variable_function ')' // redundant parenthesis
3190 { $$ = $3->addQualifiers( $2 ); }
3191 ;
3192
3193// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
3194// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
3195// is the same scope. The pattern precludes returning arrays and functions versus pointers to arrays and functions.
3196
3197function_declarator:
3198 function_no_ptr attribute_list_opt
3199 { $$ = $1->addQualifiers( $2 ); }
3200 | function_ptr
3201 | function_array attribute_list_opt
3202 { $$ = $1->addQualifiers( $2 ); }
3203 ;
3204
3205function_no_ptr:
3206 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3207 { $$ = $1->addParamList( $4 ); }
3208 | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
3209 { $$ = $2->addParamList( $6 ); }
3210 | '(' attribute_list function_ptr ')' '(' push parameter_type_list_opt pop ')'
3211 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3212 | '(' function_no_ptr ')' // redundant parenthesis
3213 { $$ = $2; }
3214 | '(' attribute_list function_no_ptr ')' // redundant parenthesis
3215 { $$ = $3->addQualifiers( $2 ); }
3216 ;
3217
3218function_ptr:
3219 ptrref_operator function_declarator
3220 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3221 | ptrref_operator type_qualifier_list function_declarator
3222 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3223 | '(' function_ptr ')' attribute_list_opt
3224 { $$ = $2->addQualifiers( $4 ); }
3225 | '(' attribute_list function_ptr ')' attribute_list_opt
3226 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3227 ;
3228
3229function_array:
3230 '(' function_ptr ')' array_dimension
3231 { $$ = $2->addArray( $4 ); }
3232 | '(' attribute_list function_ptr ')' array_dimension
3233 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3234 | '(' function_array ')' multi_array_dimension // redundant parenthesis
3235 { $$ = $2->addArray( $4 ); }
3236 | '(' attribute_list function_array ')' multi_array_dimension // redundant parenthesis
3237 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3238 | '(' function_array ')' // redundant parenthesis
3239 { $$ = $2; }
3240 | '(' attribute_list function_array ')' // redundant parenthesis
3241 { $$ = $3->addQualifiers( $2 ); }
3242 ;
3243
3244// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
3245//
3246// f( a, b, c ) int a, *b, c[]; {}
3247//
3248// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
3249// returning arrays and functions versus pointers to arrays and functions.
3250
3251KR_function_declarator:
3252 KR_function_no_ptr
3253 | KR_function_ptr
3254 | KR_function_array
3255 ;
3256
3257KR_function_no_ptr:
3258 paren_identifier '(' identifier_list ')' // function_declarator handles empty parameter
3259 { $$ = $1->addIdList( $3 ); }
3260 | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3261 { $$ = $2->addParamList( $6 ); }
3262 | '(' attribute_list KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
3263 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3264 | '(' KR_function_no_ptr ')' // redundant parenthesis
3265 { $$ = $2; }
3266 | '(' attribute_list KR_function_no_ptr ')' // redundant parenthesis
3267 { $$ = $3->addQualifiers( $2 ); }
3268 ;
3269
3270KR_function_ptr:
3271 ptrref_operator KR_function_declarator
3272 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3273 | ptrref_operator type_qualifier_list KR_function_declarator
3274 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3275 | '(' KR_function_ptr ')'
3276 { $$ = $2; }
3277 | '(' attribute_list KR_function_ptr ')'
3278 { $$ = $3->addQualifiers( $2 ); }
3279 ;
3280
3281KR_function_array:
3282 '(' KR_function_ptr ')' array_dimension
3283 { $$ = $2->addArray( $4 ); }
3284 | '(' attribute_list KR_function_ptr ')' array_dimension
3285 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3286 | '(' KR_function_array ')' multi_array_dimension // redundant parenthesis
3287 { $$ = $2->addArray( $4 ); }
3288 | '(' attribute_list KR_function_array ')' multi_array_dimension // redundant parenthesis
3289 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3290 | '(' KR_function_array ')' // redundant parenthesis
3291 { $$ = $2; }
3292 | '(' attribute_list KR_function_array ')' // redundant parenthesis
3293 { $$ = $3->addQualifiers( $2 ); }
3294 ;
3295
3296// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
3297//
3298// typedef int foo;
3299// {
3300// int foo; // redefine typedef name in new scope
3301// }
3302//
3303// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3304// and functions versus pointers to arrays and functions.
3305
3306paren_type:
3307 typedef_name
3308 {
3309 // hide type name in enclosing scope by variable name
3310 typedefTable.addToEnclosingScope( *$1->name, IDENTIFIER, "ID" );
3311 }
3312 | '(' paren_type ')'
3313 { $$ = $2; }
3314 ;
3315
3316variable_type_redeclarator:
3317 paren_type attribute_list_opt
3318 { $$ = $1->addQualifiers( $2 ); }
3319 | type_ptr
3320 | type_array attribute_list_opt
3321 { $$ = $1->addQualifiers( $2 ); }
3322 | type_function attribute_list_opt
3323 { $$ = $1->addQualifiers( $2 ); }
3324 ;
3325
3326type_ptr:
3327 ptrref_operator variable_type_redeclarator
3328 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3329 | ptrref_operator type_qualifier_list variable_type_redeclarator
3330 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3331 | '(' type_ptr ')' attribute_list_opt // redundant parenthesis
3332 { $$ = $2->addQualifiers( $4 ); }
3333 | '(' attribute_list type_ptr ')' attribute_list_opt // redundant parenthesis
3334 { $$ = $3->addQualifiers( $2 )->addQualifiers( $5 ); }
3335 ;
3336
3337type_array:
3338 paren_type array_dimension
3339 { $$ = $1->addArray( $2 ); }
3340 | '(' type_ptr ')' array_dimension
3341 { $$ = $2->addArray( $4 ); }
3342 | '(' attribute_list type_ptr ')' array_dimension
3343 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3344 | '(' type_array ')' multi_array_dimension // redundant parenthesis
3345 { $$ = $2->addArray( $4 ); }
3346 | '(' attribute_list type_array ')' multi_array_dimension // redundant parenthesis
3347 { $$ = $3->addQualifiers( $2 )->addArray( $5 ); }
3348 | '(' type_array ')' // redundant parenthesis
3349 { $$ = $2; }
3350 | '(' attribute_list type_array ')' // redundant parenthesis
3351 { $$ = $3->addQualifiers( $2 ); }
3352 ;
3353
3354type_function:
3355 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3356 { $$ = $1->addParamList( $4 ); }
3357 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3358 { $$ = $2->addParamList( $6 ); }
3359 | '(' attribute_list type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3360 { $$ = $3->addQualifiers( $2 )->addParamList( $7 ); }
3361 | '(' type_function ')' // redundant parenthesis
3362 { $$ = $2; }
3363 | '(' attribute_list type_function ')' // redundant parenthesis
3364 { $$ = $3->addQualifiers( $2 ); }
3365 ;
3366
3367// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
3368// typedef name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes
3369// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
3370// pointers to arrays and functions.
3371
3372identifier_parameter_declarator:
3373 paren_identifier attribute_list_opt
3374 { $$ = $1->addQualifiers( $2 ); }
3375 | '&' MUTEX paren_identifier attribute_list_opt
3376 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3377 | identifier_parameter_ptr
3378 | identifier_parameter_array attribute_list_opt
3379 { $$ = $1->addQualifiers( $2 ); }
3380 | identifier_parameter_function attribute_list_opt
3381 { $$ = $1->addQualifiers( $2 ); }
3382 ;
3383
3384identifier_parameter_ptr:
3385 ptrref_operator identifier_parameter_declarator
3386 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3387 | ptrref_operator type_qualifier_list identifier_parameter_declarator
3388 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3389 | '(' identifier_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3390 { $$ = $2->addQualifiers( $4 ); }
3391 ;
3392
3393identifier_parameter_array:
3394 paren_identifier array_parameter_dimension
3395 { $$ = $1->addArray( $2 ); }
3396 | '(' identifier_parameter_ptr ')' array_dimension
3397 { $$ = $2->addArray( $4 ); }
3398 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
3399 { $$ = $2->addArray( $4 ); }
3400 | '(' identifier_parameter_array ')' // redundant parenthesis
3401 { $$ = $2; }
3402 ;
3403
3404identifier_parameter_function:
3405 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3406 { $$ = $1->addParamList( $4 ); }
3407 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3408 { $$ = $2->addParamList( $6 ); }
3409 | '(' identifier_parameter_function ')' // redundant parenthesis
3410 { $$ = $2; }
3411 ;
3412
3413// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
3414// e.g.:
3415//
3416// typedef int foo;
3417// forall( otype T ) struct foo;
3418// int f( int foo ); // redefine typedef name in new scope
3419//
3420// and allows the C99 array options, which can only appear in a parameter list.
3421
3422type_parameter_redeclarator:
3423 typedef_name attribute_list_opt
3424 { $$ = $1->addQualifiers( $2 ); }
3425 | '&' MUTEX typedef_name attribute_list_opt
3426 { $$ = $3->addPointer( DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf ) )->addQualifiers( $4 ); }
3427 | type_parameter_ptr
3428 | type_parameter_array attribute_list_opt
3429 { $$ = $1->addQualifiers( $2 ); }
3430 | type_parameter_function attribute_list_opt
3431 { $$ = $1->addQualifiers( $2 ); }
3432 ;
3433
3434typedef_name:
3435 TYPEDEFname
3436 { $$ = DeclarationNode::newName( $1 ); }
3437 | TYPEGENname
3438 { $$ = DeclarationNode::newName( $1 ); }
3439 ;
3440
3441type_parameter_ptr:
3442 ptrref_operator type_parameter_redeclarator
3443 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3444 | ptrref_operator type_qualifier_list type_parameter_redeclarator
3445 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3446 | '(' type_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3447 { $$ = $2->addQualifiers( $4 ); }
3448 ;
3449
3450type_parameter_array:
3451 typedef_name array_parameter_dimension
3452 { $$ = $1->addArray( $2 ); }
3453 | '(' type_parameter_ptr ')' array_parameter_dimension
3454 { $$ = $2->addArray( $4 ); }
3455 ;
3456
3457type_parameter_function:
3458 typedef_name '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3459 { $$ = $1->addParamList( $4 ); }
3460 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3461 { $$ = $2->addParamList( $6 ); }
3462 ;
3463
3464// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
3465// which the type applies, e.g.:
3466//
3467// sizeof( int );
3468// sizeof( int * );
3469// sizeof( int [10] );
3470// sizeof( int (*)() );
3471// sizeof( int () );
3472//
3473// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3474// and functions versus pointers to arrays and functions.
3475
3476abstract_declarator:
3477 abstract_ptr
3478 | abstract_array attribute_list_opt
3479 { $$ = $1->addQualifiers( $2 ); }
3480 | abstract_function attribute_list_opt
3481 { $$ = $1->addQualifiers( $2 ); }
3482 ;
3483
3484abstract_ptr:
3485 ptrref_operator
3486 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3487 | ptrref_operator type_qualifier_list
3488 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3489 | ptrref_operator abstract_declarator
3490 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3491 | ptrref_operator type_qualifier_list abstract_declarator
3492 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3493 | '(' abstract_ptr ')' attribute_list_opt
3494 { $$ = $2->addQualifiers( $4 ); }
3495 ;
3496
3497abstract_array:
3498 array_dimension
3499 | '(' abstract_ptr ')' array_dimension
3500 { $$ = $2->addArray( $4 ); }
3501 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
3502 { $$ = $2->addArray( $4 ); }
3503 | '(' abstract_array ')' // redundant parenthesis
3504 { $$ = $2; }
3505 ;
3506
3507abstract_function:
3508 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3509 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3510 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3511 { $$ = $2->addParamList( $6 ); }
3512 | '(' abstract_function ')' // redundant parenthesis
3513 { $$ = $2; }
3514 ;
3515
3516array_dimension:
3517 // Only the first dimension can be empty.
3518 '[' ']'
3519 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3520 | '[' ']' multi_array_dimension
3521 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
3522 | '[' push assignment_expression pop ',' comma_expression ']'
3523 { $$ = DeclarationNode::newArray( $3, 0, false )->addArray( DeclarationNode::newArray( $6, 0, false ) ); }
3524 // { SemanticError( yylloc, "New array dimension is currently unimplemented." ); $$ = nullptr; }
3525 | multi_array_dimension
3526 ;
3527
3528multi_array_dimension:
3529 '[' push assignment_expression pop ']'
3530 { $$ = DeclarationNode::newArray( $3, 0, false ); }
3531 | '[' push '*' pop ']' // C99
3532 { $$ = DeclarationNode::newVarArray( 0 ); }
3533 | multi_array_dimension '[' push assignment_expression pop ']'
3534 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
3535 | multi_array_dimension '[' push '*' pop ']' // C99
3536 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
3537 ;
3538
3539// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
3540// identifier to which the type applies, e.g.:
3541//
3542// int f( int ); // not handled here
3543// int f( int * ); // abstract function-prototype parameter; no parameter name specified
3544// int f( int (*)() ); // abstract function-prototype parameter; no parameter name specified
3545// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
3546//
3547// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
3548// and functions versus pointers to arrays and functions. In addition, the pattern handles the
3549// special meaning of parenthesis around a typedef name:
3550//
3551// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
3552// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
3553// not as redundant parentheses around the identifier."
3554//
3555// For example:
3556//
3557// typedef float T;
3558// int f( int ( T [5] ) ); // see abstract_parameter_declarator
3559// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
3560// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
3561// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
3562//
3563// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
3564// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
3565// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
3566// functions.
3567
3568abstract_parameter_declarator:
3569 abstract_parameter_ptr
3570 | '&' MUTEX attribute_list_opt
3571 { $$ = DeclarationNode::newPointer( DeclarationNode::newTypeQualifier( Type::Mutex ), OperKinds::AddressOf )->addQualifiers( $3 ); }
3572 | abstract_parameter_array attribute_list_opt
3573 { $$ = $1->addQualifiers( $2 ); }
3574 | abstract_parameter_function attribute_list_opt
3575 { $$ = $1->addQualifiers( $2 ); }
3576 ;
3577
3578abstract_parameter_ptr:
3579 ptrref_operator
3580 { $$ = DeclarationNode::newPointer( nullptr, $1 ); }
3581 | ptrref_operator type_qualifier_list
3582 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3583 | ptrref_operator abstract_parameter_declarator
3584 { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr, $1 ) ); }
3585 | ptrref_operator type_qualifier_list abstract_parameter_declarator
3586 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3587 | '(' abstract_parameter_ptr ')' attribute_list_opt // redundant parenthesis
3588 { $$ = $2->addQualifiers( $4 ); }
3589 ;
3590
3591abstract_parameter_array:
3592 array_parameter_dimension
3593 | '(' abstract_parameter_ptr ')' array_parameter_dimension
3594 { $$ = $2->addArray( $4 ); }
3595 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
3596 { $$ = $2->addArray( $4 ); }
3597 | '(' abstract_parameter_array ')' // redundant parenthesis
3598 { $$ = $2; }
3599 ;
3600
3601abstract_parameter_function:
3602 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3603 { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
3604 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3605 { $$ = $2->addParamList( $6 ); }
3606 | '(' abstract_parameter_function ')' // redundant parenthesis
3607 { $$ = $2; }
3608 ;
3609
3610array_parameter_dimension:
3611 // Only the first dimension can be empty or have qualifiers.
3612 array_parameter_1st_dimension
3613 | array_parameter_1st_dimension multi_array_dimension
3614 { $$ = $1->addArray( $2 ); }
3615 | multi_array_dimension
3616 ;
3617
3618// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
3619//
3620// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
3621// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
3622
3623array_parameter_1st_dimension:
3624 '[' ']'
3625 { $$ = DeclarationNode::newArray( 0, 0, false ); }
3626 // multi_array_dimension handles the '[' '*' ']' case
3627 | '[' push type_qualifier_list '*' pop ']' // remaining C99
3628 { $$ = DeclarationNode::newVarArray( $3 ); }
3629 | '[' push type_qualifier_list pop ']'
3630 { $$ = DeclarationNode::newArray( 0, $3, false ); }
3631 // multi_array_dimension handles the '[' assignment_expression ']' case
3632 | '[' push type_qualifier_list assignment_expression pop ']'
3633 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3634 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
3635 { $$ = DeclarationNode::newArray( $5, $4, true ); }
3636 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
3637 { $$ = DeclarationNode::newArray( $5, $3, true ); }
3638 ;
3639
3640// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
3641//
3642// struct S {
3643// int;
3644// int *;
3645// int [10];
3646// int (*)();
3647// };
3648
3649variable_abstract_declarator:
3650 variable_abstract_ptr
3651 | variable_abstract_array attribute_list_opt
3652 { $$ = $1->addQualifiers( $2 ); }
3653 | variable_abstract_function attribute_list_opt
3654 { $$ = $1->addQualifiers( $2 ); }
3655 ;
3656
3657variable_abstract_ptr:
3658 ptrref_operator
3659 { $$ = DeclarationNode::newPointer( 0, $1 ); }
3660 | ptrref_operator type_qualifier_list
3661 { $$ = DeclarationNode::newPointer( $2, $1 ); }
3662 | ptrref_operator variable_abstract_declarator
3663 { $$ = $2->addPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3664 | ptrref_operator type_qualifier_list variable_abstract_declarator
3665 { $$ = $3->addPointer( DeclarationNode::newPointer( $2, $1 ) ); }
3666 | '(' variable_abstract_ptr ')' attribute_list_opt // redundant parenthesis
3667 { $$ = $2->addQualifiers( $4 ); }
3668 ;
3669
3670variable_abstract_array:
3671 array_dimension
3672 | '(' variable_abstract_ptr ')' array_dimension
3673 { $$ = $2->addArray( $4 ); }
3674 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
3675 { $$ = $2->addArray( $4 ); }
3676 | '(' variable_abstract_array ')' // redundant parenthesis
3677 { $$ = $2; }
3678 ;
3679
3680variable_abstract_function:
3681 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
3682 { $$ = $2->addParamList( $6 ); }
3683 | '(' variable_abstract_function ')' // redundant parenthesis
3684 { $$ = $2; }
3685 ;
3686
3687// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
3688// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
3689
3690cfa_identifier_parameter_declarator_tuple: // CFA
3691 cfa_identifier_parameter_declarator_no_tuple
3692 | cfa_abstract_tuple
3693 | type_qualifier_list cfa_abstract_tuple
3694 { $$ = $2->addQualifiers( $1 ); }
3695 ;
3696
3697cfa_identifier_parameter_declarator_no_tuple: // CFA
3698 cfa_identifier_parameter_ptr
3699 | cfa_identifier_parameter_array
3700 ;
3701
3702cfa_identifier_parameter_ptr: // CFA
3703 // No SUE declaration in parameter list.
3704 ptrref_operator type_specifier_nobody
3705 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3706 | type_qualifier_list ptrref_operator type_specifier_nobody
3707 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3708 | ptrref_operator cfa_abstract_function
3709 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3710 | type_qualifier_list ptrref_operator cfa_abstract_function
3711 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3712 | ptrref_operator cfa_identifier_parameter_declarator_tuple
3713 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3714 | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
3715 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3716 ;
3717
3718cfa_identifier_parameter_array: // CFA
3719 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
3720 // shift/reduce conflict with new-style empty (void) function return type.
3721 '[' ']' type_specifier_nobody
3722 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3723 | cfa_array_parameter_1st_dimension type_specifier_nobody
3724 { $$ = $2->addNewArray( $1 ); }
3725 | '[' ']' multi_array_dimension type_specifier_nobody
3726 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3727 | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
3728 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3729 | multi_array_dimension type_specifier_nobody
3730 { $$ = $2->addNewArray( $1 ); }
3731
3732 | '[' ']' cfa_identifier_parameter_ptr
3733 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3734 | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
3735 { $$ = $2->addNewArray( $1 ); }
3736 | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
3737 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
3738 | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
3739 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
3740 | multi_array_dimension cfa_identifier_parameter_ptr
3741 { $$ = $2->addNewArray( $1 ); }
3742 ;
3743
3744cfa_array_parameter_1st_dimension:
3745 '[' push type_qualifier_list '*' pop ']' // remaining C99
3746 { $$ = DeclarationNode::newVarArray( $3 ); }
3747 | '[' push type_qualifier_list assignment_expression pop ']'
3748 { $$ = DeclarationNode::newArray( $4, $3, false ); }
3749 | '[' push declaration_qualifier_list assignment_expression pop ']'
3750 // declaration_qualifier_list must be used because of shift/reduce conflict with
3751 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
3752 // appear in this context.
3753 { $$ = DeclarationNode::newArray( $4, $3, true ); }
3754 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
3755 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
3756 ;
3757
3758// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
3759// identifier to which the type applies, e.g.:
3760//
3761// [int] f( int ); // abstract variable parameter; no parameter name specified
3762// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
3763//
3764// These rules need LR(3):
3765//
3766// cfa_abstract_tuple identifier_or_type_name
3767// '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_ellipsis_list_opt ')'
3768//
3769// since a function return type can be syntactically identical to a tuple type:
3770//
3771// [int, int] t;
3772// [int, int] f( int );
3773//
3774// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
3775// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
3776// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
3777// duplicated when appearing with cfa_function_specifier.
3778
3779cfa_abstract_declarator_tuple: // CFA
3780 cfa_abstract_tuple
3781 | type_qualifier_list cfa_abstract_tuple
3782 { $$ = $2->addQualifiers( $1 ); }
3783 | cfa_abstract_declarator_no_tuple
3784 ;
3785
3786cfa_abstract_declarator_no_tuple: // CFA
3787 cfa_abstract_ptr
3788 | cfa_abstract_array
3789 ;
3790
3791cfa_abstract_ptr: // CFA
3792 ptrref_operator type_specifier
3793 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3794 | type_qualifier_list ptrref_operator type_specifier
3795 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3796 | ptrref_operator cfa_abstract_function
3797 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3798 | type_qualifier_list ptrref_operator cfa_abstract_function
3799 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3800 | ptrref_operator cfa_abstract_declarator_tuple
3801 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0, $1 ) ); }
3802 | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
3803 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1, $2 ) ); }
3804 ;
3805
3806cfa_abstract_array: // CFA
3807 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
3808 // empty (void) function return type.
3809 '[' ']' type_specifier
3810 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3811 | '[' ']' multi_array_dimension type_specifier
3812 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3813 | multi_array_dimension type_specifier
3814 { $$ = $2->addNewArray( $1 ); }
3815 | '[' ']' cfa_abstract_ptr
3816 { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3817 | '[' ']' multi_array_dimension cfa_abstract_ptr
3818 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3819 | multi_array_dimension cfa_abstract_ptr
3820 { $$ = $2->addNewArray( $1 ); }
3821 ;
3822
3823cfa_abstract_tuple: // CFA
3824 '[' push cfa_abstract_parameter_list pop ']'
3825 { $$ = DeclarationNode::newTuple( $3 ); }
3826 | '[' push type_specifier_nobody ELLIPSIS pop ']'
3827 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3828 | '[' push type_specifier_nobody ELLIPSIS constant_expression pop ']'
3829 { SemanticError( yylloc, "Tuple array currently unimplemented." ); $$ = nullptr; }
3830 ;
3831
3832cfa_abstract_function: // CFA
3833// '[' ']' '(' cfa_parameter_ellipsis_list_opt ')'
3834// { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3835 cfa_abstract_tuple '(' push cfa_parameter_ellipsis_list_opt pop ')'
3836 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3837 | cfa_function_return '(' push cfa_parameter_ellipsis_list_opt pop ')'
3838 { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3839 ;
3840
3841// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3842// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3843//
3844// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3845// the declaration specifiers in a declaration is an obsolescent feature."
3846//
3847// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3848// prototype-format parameter type declarators) is an obsolescent feature."
3849//
3850// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3851// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3852
3853//************************* MISCELLANEOUS ********************************
3854
3855comma_opt: // redundant comma
3856 // empty
3857 | ','
3858 ;
3859
3860default_initializer_opt:
3861 // empty
3862 { $$ = nullptr; }
3863 | '=' assignment_expression
3864 { $$ = $2; }
3865 ;
3866
3867%%
3868
3869// ----end of grammar----
3870
3871// Local Variables: //
3872// mode: c++ //
3873// tab-width: 4 //
3874// compile-command: "make install" //
3875// End: //
Note: See TracBrowser for help on using the repository browser.