mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Merge pull request #14874 from huonw/at-owned-cleanup
Various refactorings for __owned.
This commit is contained in:
@@ -328,7 +328,7 @@ struct SDKNodeInitInfo {
|
||||
bool IsMutating = false;
|
||||
bool IsStatic = false;
|
||||
Optional<uint8_t> SelfIndex;
|
||||
Ownership Ownership = Ownership::Strong;
|
||||
ReferenceOwnership ReferenceOwnership = ReferenceOwnership::Strong;
|
||||
std::vector<SDKDeclAttrKind> DeclAttrs;
|
||||
std::vector<TypeAttrKind> TypeAttrs;
|
||||
std::vector<StringRef> ConformingProtocols;
|
||||
@@ -402,17 +402,18 @@ class SDKNodeDecl : public SDKNode {
|
||||
StringRef ModuleName;
|
||||
std::vector<SDKDeclAttrKind> DeclAttributes;
|
||||
bool IsStatic;
|
||||
uint8_t Ownership;
|
||||
uint8_t ReferenceOwnership;
|
||||
bool hasDeclAttribute(SDKDeclAttrKind DAKind) const;
|
||||
// Non-null ExtInfo implies this decl is defined in an type extension.
|
||||
ParentExtensionInfo *ExtInfo;
|
||||
|
||||
protected:
|
||||
SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind) : SDKNode(Info, Kind),
|
||||
DKind(Info.DKind), Usr(Info.USR), Location(Info.Location),
|
||||
ModuleName(Info.ModuleName), DeclAttributes(Info.DeclAttrs),
|
||||
IsStatic(Info.IsStatic), Ownership(uint8_t(Info.Ownership)),
|
||||
ExtInfo(Info.ExtInfo) {}
|
||||
SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
|
||||
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.USR),
|
||||
Location(Info.Location), ModuleName(Info.ModuleName),
|
||||
DeclAttributes(Info.DeclAttrs), IsStatic(Info.IsStatic),
|
||||
ReferenceOwnership(uint8_t(Info.ReferenceOwnership)),
|
||||
ExtInfo(Info.ExtInfo) {}
|
||||
|
||||
public:
|
||||
StringRef getUsr() const { return Usr; }
|
||||
@@ -421,7 +422,9 @@ public:
|
||||
StringRef getHeaderName() const;
|
||||
void addDeclAttribute(SDKDeclAttrKind DAKind);
|
||||
ArrayRef<SDKDeclAttrKind> getDeclAttributes() const;
|
||||
swift::Ownership getOwnership() const { return swift::Ownership(Ownership); }
|
||||
swift::ReferenceOwnership getReferenceOwnership() const {
|
||||
return swift::ReferenceOwnership(ReferenceOwnership);
|
||||
}
|
||||
bool isObjc() const { return Usr.startswith("c:"); }
|
||||
static bool classof(const SDKNode *N);
|
||||
DeclKind getDeclKind() const { return DKind; }
|
||||
@@ -978,8 +981,9 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
|
||||
Info.IsStatic = true;
|
||||
break;
|
||||
case KeyKind::KK_ownership:
|
||||
Info.Ownership = swift::Ownership(getAsInt(Pair.getValue()));
|
||||
assert(Info.Ownership != swift::Ownership::Strong &&
|
||||
Info.ReferenceOwnership =
|
||||
swift::ReferenceOwnership(getAsInt(Pair.getValue()));
|
||||
assert(Info.ReferenceOwnership != swift::ReferenceOwnership::Strong &&
|
||||
"Strong is implied.");
|
||||
break;
|
||||
|
||||
@@ -1077,7 +1081,7 @@ bool SDKNode::operator==(const SDKNode &Other) const {
|
||||
auto Right = (&Other)->getAs<SDKNodeDecl>();
|
||||
if (Left->isStatic() ^ Right->isStatic())
|
||||
return false;
|
||||
if (Left->getOwnership() != Right->getOwnership())
|
||||
if (Left->getReferenceOwnership() != Right->getReferenceOwnership())
|
||||
return false;
|
||||
LLVM_FALLTHROUGH;
|
||||
}
|
||||
@@ -1238,11 +1242,11 @@ static Optional<uint8_t> getSelfIndex(ValueDecl *VD) {
|
||||
return None;
|
||||
}
|
||||
|
||||
static Ownership getOwnership(ValueDecl *VD) {
|
||||
if (auto OA = VD->getAttrs().getAttribute<OwnershipAttr>()) {
|
||||
static ReferenceOwnership getReferenceOwnership(ValueDecl *VD) {
|
||||
if (auto OA = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
|
||||
return OA->get();
|
||||
}
|
||||
return Ownership::Strong;
|
||||
return ReferenceOwnership::Strong;
|
||||
}
|
||||
|
||||
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
|
||||
@@ -1254,14 +1258,15 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
|
||||
TypeAttrs.push_back(TypeAttrKind::TAK_noescape);
|
||||
}
|
||||
|
||||
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD) : Ctx(Ctx),
|
||||
Name(VD->hasName() ? getEscapedName(VD->getBaseName()) : Ctx.buffer("_")),
|
||||
PrintedName(getPrintedName(Ctx, VD)), DKind(VD->getKind()),
|
||||
USR(calculateUsr(Ctx, VD)), Location(calculateLocation(Ctx, VD)),
|
||||
ModuleName(VD->getModuleContext()->getName().str()),
|
||||
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
|
||||
IsStatic(VD->isStatic()), SelfIndex(getSelfIndex(VD)),
|
||||
Ownership(getOwnership(VD)), ExtInfo(nullptr) {
|
||||
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
|
||||
: Ctx(Ctx),
|
||||
Name(VD->hasName() ? getEscapedName(VD->getBaseName()) : Ctx.buffer("_")),
|
||||
PrintedName(getPrintedName(Ctx, VD)), DKind(VD->getKind()),
|
||||
USR(calculateUsr(Ctx, VD)), Location(calculateLocation(Ctx, VD)),
|
||||
ModuleName(VD->getModuleContext()->getName().str()),
|
||||
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
|
||||
IsStatic(VD->isStatic()), SelfIndex(getSelfIndex(VD)),
|
||||
ReferenceOwnership(getReferenceOwnership(VD)), ExtInfo(nullptr) {
|
||||
|
||||
// Calculate usr for its super class.
|
||||
if (auto *CD = dyn_cast_or_null<ClassDecl>(VD)) {
|
||||
@@ -1685,8 +1690,8 @@ namespace swift {
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declAttributes).data(),
|
||||
Attributes);
|
||||
// Strong reference is implied, no need for serialization.
|
||||
if (D->getOwnership() != Ownership::Strong) {
|
||||
uint8_t Raw = uint8_t(D->getOwnership());
|
||||
if (D->getReferenceOwnership() != ReferenceOwnership::Strong) {
|
||||
uint8_t Raw = uint8_t(D->getReferenceOwnership());
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_ownership).data(), Raw);
|
||||
}
|
||||
} else if (auto T = dyn_cast<SDKNodeType>(value)) {
|
||||
@@ -2250,7 +2255,7 @@ static void detectDeclChange(NodePtr L, NodePtr R) {
|
||||
auto *RD = R->getAs<SDKNodeDecl>();
|
||||
if (LD->isStatic() ^ RD->isStatic())
|
||||
L->annotate(NodeAnnotation::StaticChange);
|
||||
if (LD->getOwnership() != RD->getOwnership())
|
||||
if (LD->getReferenceOwnership() != RD->getReferenceOwnership())
|
||||
L->annotate(NodeAnnotation::OwnershipChange);
|
||||
detectRename(L, R);
|
||||
}
|
||||
@@ -3093,22 +3098,25 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
|
||||
return;
|
||||
}
|
||||
case NodeAnnotation::OwnershipChange: {
|
||||
auto getOwnershipDescription = [&](swift::Ownership O) {
|
||||
auto getOwnershipDescription = [&](swift::ReferenceOwnership O) {
|
||||
switch (O) {
|
||||
case Ownership::Strong: return Ctx.buffer("strong");
|
||||
case Ownership::Weak: return Ctx.buffer("weak");
|
||||
case Ownership::Unowned: return Ctx.buffer("unowned");
|
||||
case Ownership::Unmanaged: return Ctx.buffer("unowned(unsafe)");
|
||||
case ReferenceOwnership::Strong:
|
||||
return Ctx.buffer("strong");
|
||||
case ReferenceOwnership::Weak:
|
||||
return Ctx.buffer("weak");
|
||||
case ReferenceOwnership::Unowned:
|
||||
return Ctx.buffer("unowned");
|
||||
case ReferenceOwnership::Unmanaged:
|
||||
return Ctx.buffer("unowned(unsafe)");
|
||||
}
|
||||
|
||||
llvm_unreachable("Unhandled Ownership in switch.");
|
||||
};
|
||||
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
|
||||
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
|
||||
Node->getDeclKind(),
|
||||
Node->getFullyQualifiedName(),
|
||||
getOwnershipDescription(Node->getOwnership()),
|
||||
getOwnershipDescription(Count->getOwnership()));
|
||||
AttrChangedDecls.Diags.emplace_back(
|
||||
ScreenInfo, Node->getDeclKind(), Node->getFullyQualifiedName(),
|
||||
getOwnershipDescription(Node->getReferenceOwnership()),
|
||||
getOwnershipDescription(Count->getReferenceOwnership()));
|
||||
return;
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user