source: src/Parser/parser.yy@ 056bee8

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

print unimplemented error for forall in typedef

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