source: src/Parser/parser.yy@ 0860d9c

Last change on this file since 0860d9c was 11ab0b4a, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add corun/cofor statement, update old cofor

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