source: src/Parser/parser.yy@ be1d00c

ADT ast-experimental
Last change on this file since be1d00c was d41735a, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

parse CFA dimension-list with types

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