the "Capture" struct is now a class

This commit is contained in:
Vern Paxson 2023-06-16 15:30:52 -07:00 committed by Arne Welzel
parent 528aa6766a
commit b6464814c9
6 changed files with 24 additions and 8 deletions

View file

@ -4726,7 +4726,7 @@ bool LambdaExpr::CheckCaptures(StmtPtr when_parent)
for ( const auto& c : *captures )
{
auto cid = c.id.get();
auto cid = c.Id().get();
if ( ! cid )
// This happens for undefined/inappropriate
@ -4768,7 +4768,7 @@ bool LambdaExpr::CheckCaptures(StmtPtr when_parent)
for ( const auto& c : *captures )
{
auto cid = c.id.get();
auto cid = c.Id().get();
if ( cid && capture_is_matched.count(cid) == 0 )
{
auto msg = util::fmt("%s is captured but not used inside %s", cid->Name(), desc);

View file

@ -2018,7 +2018,7 @@ WhenInfo::WhenInfo(ExprPtr arg_cond, FuncType::CaptureList* arg_cl, bool arg_is_
bool is_present = false;
for ( auto& c : *cl )
if ( c.id == wl )
if ( c.Id() == wl )
{
is_present = true;
break;

View file

@ -699,6 +699,12 @@ TypePtr SetType::ShallowClone()
SetType::~SetType() = default;
FuncType::Capture::Capture(detail::IDPtr _id, bool _deep_copy)
: id(std::move(_id)), deep_copy(_deep_copy)
{
is_managed = id ? ZVal::IsManagedType(id->GetType()) : false;
}
FuncType::FuncType(RecordTypePtr arg_args, TypePtr arg_yield, FunctionFlavor arg_flavor)
: Type(TYPE_FUNC), args(std::move(arg_args)), arg_types(make_intrusive<TypeList>()),
yield(std::move(arg_yield))

View file

@ -511,10 +511,22 @@ public:
/**
* A single lambda "capture" (outer variable used in a lambda's body).
*/
struct Capture
class Capture
{
public:
Capture(detail::IDPtr _id, bool _deep_copy);
auto& Id() const { return id; }
bool IsDeepCopy() const { return deep_copy; }
bool IsManaged() const { return is_managed; }
// For script optimization:
void SetID(detail::IDPtr new_id) { id = std::move(new_id); }
private:
detail::IDPtr id;
bool deep_copy;
bool is_managed;
};
using CaptureList = std::vector<Capture>;

View file

@ -1640,9 +1640,7 @@ capture:
delete [] $2;
$$ = new FuncType::Capture;
$$->id = id;
$$->deep_copy = $1;
$$ = new FuncType::Capture(id, $1);
}
;

View file

@ -1301,7 +1301,7 @@ string CPPCompile::GenLambdaClone(const LambdaExpr* l, bool all_deep)
if ( captures && ! IsNativeType(id_t) )
{
for ( const auto& c : *captures )
if ( id == c.id && (c.deep_copy || all_deep) )
if ( id == c.Id() && (c.IsDeepCopy() || all_deep) )
arg = string("cast_intrusive<") + TypeName(id_t) + ">(" + arg + "->Clone())";
}