Skip to content

[AST] Remove SerializedLocalDeclContext #70983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2488,16 +2488,13 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
/// SerializedTopLevelCodeDeclContext - This represents what was originally a
/// TopLevelCodeDecl during serialization. It is preserved only to maintain the
/// correct AST structure and remangling after deserialization.
class SerializedTopLevelCodeDeclContext : public SerializedLocalDeclContext {
class SerializedTopLevelCodeDeclContext : public DeclContext {
public:
SerializedTopLevelCodeDeclContext(DeclContext *Parent)
: SerializedLocalDeclContext(LocalDeclContextKind::TopLevelCodeDecl,
Parent) {}
: DeclContext(DeclContextKind::SerializedTopLevelCodeDecl, Parent) {}

static bool classof(const DeclContext *DC) {
if (auto LDC = dyn_cast<SerializedLocalDeclContext>(DC))
return LDC->getLocalDeclContextKind() ==
LocalDeclContextKind::TopLevelCodeDecl;
return false;
return DC->getContextKind() == DeclContextKind::SerializedTopLevelCodeDecl;
}
};

Expand Down
47 changes: 9 additions & 38 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ enum class DeclContextKind : unsigned {
SubscriptDecl,
EnumElementDecl,
AbstractFunctionDecl,
SerializedLocal,
SerializedAbstractClosure,
SerializedTopLevelCodeDecl,
MacroDecl,
Last_LocalDeclContextKind = MacroDecl,
Package,
Expand All @@ -108,14 +109,6 @@ enum class DeclContextKind : unsigned {
Last_DeclContextKind = ExtensionDecl
};

/// Kinds of DeclContexts after deserialization.
///
/// \see SerializedLocalDeclContext.
enum class LocalDeclContextKind : uint8_t {
AbstractClosure,
TopLevelCodeDecl
};

/// Describes the kind of a particular conformance.
///
/// The following code involves conformances of the three different kinds:
Expand Down Expand Up @@ -249,10 +242,11 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
FileUnit,
Package,
Initializer,
SerializedLocal,
SerializedAbstractClosure,
SerializedTopLevelCodeDecl,
// If you add a new AST hierarchies, then update the static_assert() below.
};
static_assert(unsigned(ASTHierarchy::SerializedLocal) <
static_assert(unsigned(ASTHierarchy::SerializedTopLevelCodeDecl) <
(1 << DeclContextAlignInBits),
"ASTHierarchy exceeds bits available");

Expand Down Expand Up @@ -281,8 +275,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
return ASTHierarchy::Expr;
case DeclContextKind::Initializer:
return ASTHierarchy::Initializer;
case DeclContextKind::SerializedLocal:
return ASTHierarchy::SerializedLocal;
case DeclContextKind::SerializedAbstractClosure:
return ASTHierarchy::SerializedAbstractClosure;
case DeclContextKind::SerializedTopLevelCodeDecl:
return ASTHierarchy::SerializedTopLevelCodeDecl;
case DeclContextKind::FileUnit:
return ASTHierarchy::FileUnit;
case DeclContextKind::Package:
Expand Down Expand Up @@ -704,31 +700,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
static bool classof(const Decl *D);
};

/// SerializedLocalDeclContext - the base class for DeclContexts that were
/// serialized to preserve AST structure and accurate mangling after
/// deserialization.
class SerializedLocalDeclContext : public DeclContext {
private:
unsigned LocalKind : 3;

protected:
unsigned SpareBits : 29;

public:
SerializedLocalDeclContext(LocalDeclContextKind LocalKind,
DeclContext *Parent)
: DeclContext(DeclContextKind::SerializedLocal, Parent),
LocalKind(static_cast<unsigned>(LocalKind)) {}

LocalDeclContextKind getLocalDeclContextKind() const {
return static_cast<LocalDeclContextKind>(LocalKind);
}

static bool classof(const DeclContext *DC) {
return DC->getContextKind() == DeclContextKind::SerializedLocal;
}
};

/// An iterator that walks through a list of declarations stored
/// within some iterable declaration context.
class DeclIterator {
Expand Down
10 changes: 3 additions & 7 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3947,16 +3947,15 @@ class AbstractClosureExpr : public DeclContext, public Expr {
/// SerializedAbstractClosureExpr - This represents what was originally an
/// AbstractClosureExpr during serialization. It is preserved only to maintain
/// the correct AST structure and remangling after deserialization.
class SerializedAbstractClosureExpr : public SerializedLocalDeclContext {
class SerializedAbstractClosureExpr : public DeclContext {
const Type Ty;
llvm::PointerIntPair<Type, 1> TypeAndImplicit;
const unsigned Discriminator;

public:
SerializedAbstractClosureExpr(Type Ty, bool Implicit, unsigned Discriminator,
DeclContext *Parent)
: SerializedLocalDeclContext(LocalDeclContextKind::AbstractClosure,
Parent),
: DeclContext(DeclContextKind::SerializedAbstractClosure, Parent),
TypeAndImplicit(llvm::PointerIntPair<Type, 1>(Ty, Implicit)),
Discriminator(Discriminator) {}

Expand All @@ -3973,10 +3972,7 @@ class SerializedAbstractClosureExpr : public SerializedLocalDeclContext {
}

static bool classof(const DeclContext *DC) {
if (auto LDC = dyn_cast<SerializedLocalDeclContext>(DC))
return LDC->getLocalDeclContextKind() ==
LocalDeclContextKind::AbstractClosure;
return false;
return DC->getContextKind() == DeclContextKind::SerializedAbstractClosure;
}
};

Expand Down
5 changes: 3 additions & 2 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,8 +1732,8 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
os << "(file)";
break;

case DeclContextKind::SerializedLocal:
os << "local context";
case DeclContextKind::SerializedAbstractClosure:
os << "serialized abstract closure";
break;

case DeclContextKind::AbstractClosureExpr: {
Expand Down Expand Up @@ -1782,6 +1782,7 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
break;

case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
os << "top-level code";
break;

Expand Down
15 changes: 4 additions & 11 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2317,17 +2317,6 @@ void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName)
appendContext(ctx->getParent(), useModuleName);
return;

case DeclContextKind::SerializedLocal: {
auto local = cast<SerializedLocalDeclContext>(ctx);
switch (local->getLocalDeclContextKind()) {
case LocalDeclContextKind::AbstractClosure:
appendClosureEntity(cast<SerializedAbstractClosureExpr>(local));
return;
case LocalDeclContextKind::TopLevelCodeDecl:
return appendContext(local->getParent(), useModuleName);
}
}

case DeclContextKind::GenericTypeDecl:
appendAnyGenericType(cast<GenericTypeDecl>(ctx));
return;
Expand Down Expand Up @@ -2368,6 +2357,9 @@ void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName)
case DeclContextKind::AbstractClosureExpr:
return appendClosureEntity(cast<AbstractClosureExpr>(ctx));

case DeclContextKind::SerializedAbstractClosure:
return appendClosureEntity(cast<SerializedAbstractClosureExpr>(ctx));

case DeclContextKind::AbstractFunctionDecl: {
auto fn = cast<AbstractFunctionDecl>(ctx);

Expand Down Expand Up @@ -2428,6 +2420,7 @@ void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName)
llvm_unreachable("bad initializer kind");

case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
// Mangle the containing module context.
return appendContext(ctx->getParent(), useModuleName);

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/AccessRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,19 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {

switch (DC->getContextKind()) {
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
// Variables declared in a top-level 'guard' statement can be accessed in
// later top-level code.
return AccessLevel::FilePrivate;
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::SerializedAbstractClosure:
Comment on lines +113 to +118
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I've changed these to match the non-serialized cases, not that it really matters since we'll deserialize the access level anyway

if (isa<ParamDecl>(D)) {
// Closure parameters may need to be accessible to the enclosing
// context, for single-expression closures.
return AccessLevel::FilePrivate;
} else {
return AccessLevel::Private;
}
case DeclContextKind::SerializedLocal:
case DeclContextKind::Initializer:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SubscriptDecl:
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11764,7 +11764,8 @@ MacroDiscriminatorContext::getInnermostMacroContext(DeclContext *dc) {

case DeclContextKind::EnumElementDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SerializedLocal:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
Expand Down
52 changes: 31 additions & 21 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ SourceFile *DeclContext::getParentSourceFile() const {
case DeclContextKind::FileUnit:
case DeclContextKind::Module:
case DeclContextKind::Package:
case DeclContextKind::SerializedLocal:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
break;
}
}
Expand Down Expand Up @@ -613,7 +614,8 @@ bool DeclContext::walkContext(ASTWalker &Walker) {
return cast<EnumElementDecl>(this)->walk(Walker);
case DeclContextKind::MacroDecl:
return cast<MacroDecl>(this)->walk(Walker);
case DeclContextKind::SerializedLocal:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
llvm_unreachable("walk is unimplemented for deserialized contexts");
case DeclContextKind::Initializer:
// Is there any point in trying to walk the expression?
Expand Down Expand Up @@ -689,10 +691,12 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
case DeclContextKind::Package: Kind = "Package"; break;
case DeclContextKind::Module: Kind = "Module"; break;
case DeclContextKind::FileUnit: Kind = "FileUnit"; break;
case DeclContextKind::SerializedLocal: Kind = "Serialized Local"; break;
case DeclContextKind::AbstractClosureExpr:
Kind = "AbstractClosureExpr";
break;
case DeclContextKind::SerializedAbstractClosure:
Kind = "SerializedAbstractClosure";
break;
case DeclContextKind::GenericTypeDecl:
switch (cast<GenericTypeDecl>(this)->getKind()) {
#define DECL(ID, PARENT) \
Expand All @@ -702,6 +706,9 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
break;
case DeclContextKind::ExtensionDecl: Kind = "ExtensionDecl"; break;
case DeclContextKind::TopLevelCodeDecl: Kind = "TopLevelCodeDecl"; break;
case DeclContextKind::SerializedTopLevelCodeDecl:
Kind = "SerializedTopLevelCodeDecl";
break;
case DeclContextKind::Initializer: Kind = "Initializer"; break;
case DeclContextKind::AbstractFunctionDecl:
Kind = "AbstractFunctionDecl";
Expand Down Expand Up @@ -741,16 +748,29 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
OS << " line=" << getLineNumber(cast<AbstractClosureExpr>(this));
OS << " : " << cast<AbstractClosureExpr>(this)->getType();
break;

case DeclContextKind::SerializedAbstractClosure: {
OS << " : " << cast<SerializedAbstractClosureExpr>(this)->getType();
break;
}

case DeclContextKind::GenericTypeDecl:
OS << " name=" << cast<GenericTypeDecl>(this)->getName();
break;

case DeclContextKind::ExtensionDecl:
OS << " line=" << getLineNumber(cast<ExtensionDecl>(this));
OS << " base=" << cast<ExtensionDecl>(this)->getExtendedType();
break;

case DeclContextKind::TopLevelCodeDecl:
OS << " line=" << getLineNumber(cast<TopLevelCodeDecl>(this));
break;

case DeclContextKind::SerializedTopLevelCodeDecl:
// Already printed the kind, nothing else to do.
break;

case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(this);
OS << " name=" << AFD->getName();
Expand Down Expand Up @@ -815,20 +835,6 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
}
}
break;

case DeclContextKind::SerializedLocal: {
auto local = cast<SerializedLocalDeclContext>(this);
switch (local->getLocalDeclContextKind()) {
case LocalDeclContextKind::AbstractClosure: {
auto serializedClosure = cast<SerializedAbstractClosureExpr>(local);
OS << " closure : " << serializedClosure->getType();
break;
}
case LocalDeclContextKind::TopLevelCodeDecl:
OS << " TopLevelCode";
break;
}
}
}

if (auto decl = getAsDecl())
Expand Down Expand Up @@ -1308,8 +1314,10 @@ DeclContextKind DeclContext::getContextKind() const {
return DeclContextKind::AbstractClosureExpr;
case ASTHierarchy::Initializer:
return DeclContextKind::Initializer;
case ASTHierarchy::SerializedLocal:
return DeclContextKind::SerializedLocal;
case ASTHierarchy::SerializedAbstractClosure:
return DeclContextKind::SerializedAbstractClosure;
case ASTHierarchy::SerializedTopLevelCodeDecl:
return DeclContextKind::SerializedTopLevelCodeDecl;
case ASTHierarchy::FileUnit:
return DeclContextKind::FileUnit;
case ASTHierarchy::Package:
Expand Down Expand Up @@ -1362,7 +1370,8 @@ bool DeclContext::isAsyncContext() const {
case DeclContextKind::Initializer:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::SerializedLocal:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::GenericTypeDecl:
Expand Down Expand Up @@ -1414,7 +1423,8 @@ SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
return SourceLoc();

case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
return extractNearestSourceLoc(dc->getParent());
}
llvm_unreachable("Unhandled DeclContextKindIn switch");
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {

case DeclContextKind::FileUnit:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
break;

case DeclContextKind::ExtensionDecl:
Expand All @@ -1257,9 +1258,9 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
ppDecl = cast<GenericTypeDecl>(dc);
break;

case DeclContextKind::SerializedLocal:
case DeclContextKind::Initializer:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::EnumElementDecl:
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,12 +886,13 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,
bool fromAnonymousContext) {
switch (parent->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return {getAddrOfAnonymousContextDescriptor(
fromAnonymousContext ? parent : ofChild),
ConstantReference::Direct};
Expand Down
Loading