Skip to content

[NFC] Create PackageUnit class, and Package entries to DeclContext / ASTHierarchy #64034

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 3 commits into from
Mar 3, 2023
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
1 change: 1 addition & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace swift {
class Identifier;
class InheritedNameSet;
class ModuleDecl;
class PackageUnit;
class ModuleDependenciesCache;
class ModuleLoader;
class NominalTypeDecl;
Expand Down
8 changes: 7 additions & 1 deletion include/swift/AST/ASTWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Expr;
class ClosureExpr;
class CustomAttr;
class ModuleDecl;
class PackageUnit;
class Stmt;
class Pattern;
class TypeRepr;
Expand Down Expand Up @@ -101,14 +102,15 @@ enum class MacroWalking {
class ASTWalker {
public:
enum class ParentKind {
Module, Decl, Stmt, Expr, Pattern, TypeRepr
Package, Module, Decl, Stmt, Expr, Pattern, TypeRepr
};

class ParentTy {
ParentKind Kind;
void *Ptr = nullptr;

public:
ParentTy(PackageUnit *Pkg) : Kind(ParentKind::Package), Ptr(Pkg) {}
ParentTy(ModuleDecl *Mod) : Kind(ParentKind::Module), Ptr(Mod) {}
ParentTy(Decl *D) : Kind(ParentKind::Decl), Ptr(D) {}
ParentTy(Stmt *S) : Kind(ParentKind::Stmt), Ptr(S) {}
Expand All @@ -123,6 +125,10 @@ class ASTWalker {
return Kind;
}

PackageUnit *getAsPackage() const {
return Kind == ParentKind::Package ? static_cast<PackageUnit*>(Ptr)
: nullptr;
}
ModuleDecl *getAsModule() const {
return Kind == ParentKind::Module ? static_cast<ModuleDecl*>(Ptr)
: nullptr;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4009,7 +4009,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
bool isGlobalActor() const {
return getGlobalActorInstance() != nullptr;
}

/// Return the `DestructorDecl` for a struct or enum's `deinit` declaration.
/// Returns null if the type is a class, or does not have a declared `deinit`.
DestructorDecl *getValueTypeDestructor();
Expand Down
21 changes: 19 additions & 2 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace swift {
class SourceFile;
class Type;
class ModuleDecl;
class PackageUnit;
class GenericTypeDecl;
class NominalTypeDecl;
class PrecedenceGroupLookupResult;
Expand Down Expand Up @@ -96,7 +97,7 @@ enum class DeclContextKind : unsigned {
SerializedLocal,
MacroDecl,
Last_LocalDeclContextKind = MacroDecl,

Package,
Module,
FileUnit,
GenericTypeDecl,
Expand Down Expand Up @@ -234,6 +235,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
Decl,
Expr,
FileUnit,
Package,
Initializer,
SerializedLocal,
// If you add a new AST hierarchies, then update the static_assert() below.
Expand Down Expand Up @@ -271,6 +273,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
return ASTHierarchy::SerializedLocal;
case DeclContextKind::FileUnit:
return ASTHierarchy::FileUnit;
case DeclContextKind::Package:
return ASTHierarchy::Package;
case DeclContextKind::Module:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
Expand All @@ -296,7 +300,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext

DeclContext(DeclContextKind Kind, DeclContext *Parent)
: ParentAndKind(Parent, getASTHierarchyFromKind(Kind)) {
if (Kind != DeclContextKind::Module)
// if Module kind, it may (or may not) have Package as its parent
if (Kind != DeclContextKind::Package && Kind != DeclContextKind::Module)
assert(Parent != nullptr && "DeclContext must have a parent context");
}

Expand All @@ -319,6 +324,14 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
return getContextKind() <= DeclContextKind::Last_LocalDeclContextKind;
}

/// \returns true if this is a context with package-wide scope, e.g. a package,
/// a module, or a source file.
LLVM_READONLY
bool isPackageScopeContext() const; // see swift/AST/Module.h

LLVM_READONLY
bool isPackageContext() const; // see swift/AST/Module.h

/// isModuleContext - Return true if this is a subclass of Module.
LLVM_READONLY
bool isModuleContext() const; // see swift/AST/Module.h
Expand Down Expand Up @@ -499,6 +512,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
return false;
}

/// Returns the package context of the parent module.
LLVM_READONLY
PackageUnit *getParentModulePackage() const;

/// Returns the module context that contains this context.
LLVM_READONLY
ModuleDecl *getParentModule() const;
Expand Down
48 changes: 43 additions & 5 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ class OverlayFile;
/// location.
class ModuleSourceFileLocationMap;

/// Package unit used to allow grouping of modules by a package name.
/// ModuleDecl can set PackageUnit as a parent in its DeclContext
/// See \c ModuleDecl
class PackageUnit: public DeclContext {

Identifier PackageName;

PackageUnit(Identifier name);

public:
static PackageUnit *
create(Identifier name, ASTContext &ctx) {
return new (ctx) PackageUnit(name);
}

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

static bool classof(const PackageUnit *PU) {
// FIXME: add a correct check
return true;
}

Identifier getName() const {
return PackageName;
}
};

/// The minimum unit of compilation.
///
/// A module is made up of several file-units, which are all part of the same
Expand Down Expand Up @@ -284,7 +313,7 @@ class ModuleDecl
/// Used by the debugger to bypass resilient access to fields.
bool BypassResilience = false;

ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo);
ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo, PackageUnit *pkg);

public:
/// Creates a new module with a given \p name.
Expand All @@ -293,13 +322,14 @@ class ModuleDecl
/// imported by each file of this module.
static ModuleDecl *
create(Identifier name, ASTContext &ctx,
ImplicitImportInfo importInfo = ImplicitImportInfo()) {
return new (ctx) ModuleDecl(name, ctx, importInfo);
ImplicitImportInfo importInfo = ImplicitImportInfo(),
PackageUnit *pkg = nullptr) {
return new (ctx) ModuleDecl(name, ctx, importInfo, pkg);
}

static ModuleDecl *
createMainModule(ASTContext &ctx, Identifier name, ImplicitImportInfo iinfo) {
auto *Mod = ModuleDecl::create(name, ctx, iinfo);
createMainModule(ASTContext &ctx, Identifier name, ImplicitImportInfo iinfo, PackageUnit *pkg = nullptr) {
auto *Mod = ModuleDecl::create(name, ctx, iinfo, pkg);
Mod->Bits.ModuleDecl.IsMainModule = true;
return Mod;
}
Expand Down Expand Up @@ -416,6 +446,10 @@ class ModuleDecl
/// Set the name of the package this module belongs to
void setPackageName(Identifier name) {
PackageName = name;
// TODO: uncomment when PackageUnit gets passed to constructor
// PackageUnit *pkgUnit = PackageUnit::create(name, getASTContext());
// DeclContext newContext = DeclContext(DeclContextKind::Module, pkgUnit);
// setDeclContext(&newContext);
}

Identifier getExportAsName() const { return ExportAsName; }
Expand Down Expand Up @@ -1036,6 +1070,10 @@ inline bool DeclContext::isModuleScopeContext() const {
return isModuleContext();
}

inline bool DeclContext::isPackageScopeContext() const {
return ParentAndKind.getInt() == ASTHierarchy::Package;
}

/// Extract the source location from the given module declaration.
inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
return extractNearestSourceLoc(static_cast<const Decl *>(mod));
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,10 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
}

switch (dc->getContextKind()) {
case DeclContextKind::Package:
printName(os, cast<PackageUnit>(dc)->getName());
break;

case DeclContextKind::Module:
printName(os, cast<ModuleDecl>(dc)->getRealName());
break;
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,8 @@ static Optional<VarDecl*> findFirstVariable(PatternBindingDecl *binding) {

void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName) {
switch (ctx->getContextKind()) {
case DeclContextKind::Package:
return;
case DeclContextKind::Module:
return appendModule(cast<ModuleDecl>(ctx), useModuleName);

Expand Down
2 changes: 2 additions & 0 deletions lib/AST/AccessRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
case DeclContextKind::SubscriptDecl:
case DeclContextKind::EnumElementDecl:
return AccessLevel::Private;
case DeclContextKind::Package:
return AccessLevel::Package;
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
return AccessLevel::Internal;
Expand Down
21 changes: 20 additions & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ DeclContext *DeclContext::getParentForLookup() const {
return getParent();
}

PackageUnit *DeclContext::getParentModulePackage() const {
auto parentModule = getParentModule();
auto pkg = parentModule->getParent();
if (pkg)
return const_cast<PackageUnit *>(cast<PackageUnit>(pkg));
return nullptr;
}

ModuleDecl *DeclContext::getParentModule() const {
const DeclContext *DC = this;
while (!DC->isModuleContext())
Expand Down Expand Up @@ -306,6 +314,7 @@ SourceFile *DeclContext::getParentSourceFile() const {
case DeclContextKind::Initializer:
case DeclContextKind::FileUnit:
case DeclContextKind::Module:
case DeclContextKind::Package:
case DeclContextKind::SerializedLocal:
break;
}
Expand Down Expand Up @@ -559,6 +568,8 @@ bool DeclContext::canBeParentOfExtension() const {

bool DeclContext::walkContext(ASTWalker &Walker) {
switch (getContextKind()) {
case DeclContextKind::Package:
return false;
case DeclContextKind::Module:
return cast<ModuleDecl>(this)->walk(Walker);
case DeclContextKind::FileUnit:
Expand Down Expand Up @@ -652,6 +663,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,

const char *Kind;
switch (getContextKind()) {
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;
Expand All @@ -678,6 +690,9 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
OS.indent(Depth*2 + indent) << (void*)this << " " << Kind;

switch (getContextKind()) {
case DeclContextKind::Package:
OS << " name=" << cast<PackageUnit>(this)->getName();
break;
case DeclContextKind::Module:
OS << " name=" << cast<ModuleDecl>(this)->getName();
break;
Expand Down Expand Up @@ -1206,7 +1221,7 @@ AccessScope::AccessScope(const DeclContext *DC, AccessLimitKind limitKind)
Value.setPointer(DC);
isPrivate = true;
}
if (!DC || isa<ModuleDecl>(DC))
if (!DC || isa<ModuleDecl>(DC) || isa<PackageUnit>(DC))
assert(!isPrivate && "public, package, or internal scope can't be private");
}

Expand Down Expand Up @@ -1285,6 +1300,8 @@ DeclContextKind DeclContext::getContextKind() const {
return DeclContextKind::SerializedLocal;
case ASTHierarchy::FileUnit:
return DeclContextKind::FileUnit;
case ASTHierarchy::Package:
return DeclContextKind::Package;
case ASTHierarchy::Decl: {
auto decl = reinterpret_cast<const Decl*>(this + 1);
if (isa<AbstractFunctionDecl>(decl))
Expand Down Expand Up @@ -1334,6 +1351,7 @@ bool DeclContext::isAsyncContext() const {
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::SerializedLocal:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::MacroDecl:
Expand Down Expand Up @@ -1361,6 +1379,7 @@ bool DeclContext::isAsyncContext() const {

SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
switch (dc->getContextKind()) {
case DeclContextKind::Package:
case DeclContextKind::Module:
return SourceLoc();
case DeclContextKind::AbstractFunctionDecl:
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,9 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {

while (!dc->isModuleContext()) {
switch (dc->getContextKind()) {
case DeclContextKind::Package:
llvm_unreachable("Not in a package context!");
break;
case DeclContextKind::Module:
llvm_unreachable("Not in a module context!");
break;
Expand Down
10 changes: 8 additions & 2 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,19 @@ void SourceLookupCache::invalidate() {
(void)SameSizeSmallVector{std::move(AllVisibleValues)};
}

PackageUnit::PackageUnit(Identifier name)
: DeclContext(DeclContextKind::Package, nullptr) {
PackageName = name;
}

//===----------------------------------------------------------------------===//
// Module Implementation
//===----------------------------------------------------------------------===//

ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
ImplicitImportInfo importInfo)
: DeclContext(DeclContextKind::Module, nullptr),
ImplicitImportInfo importInfo,
PackageUnit *pkg = nullptr)
: DeclContext(DeclContextKind::Module, pkg),
TypeDecl(DeclKind::Module, &ctx, name, SourceLoc(), {}),
ImportInfo(importInfo) {

Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,8 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,
ConstantReference::Direct};
}

case DeclContextKind::Package:
assert(false && "package decl context kind should not have been reached");
case DeclContextKind::FileUnit:
case DeclContextKind::MacroDecl:
parent = parent->getParentModule();
Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
case DeclContextKind::TopLevelCodeDecl:
return getOrCreateContext(DC->getParent());

case DeclContextKind::Package: {
auto *pkg = cast<PackageUnit>(DC);
return getOrCreateContext(pkg);
}
case DeclContextKind::Module:
return getOrCreateModule(
{ImportPath::Access(), cast<ModuleDecl>(DC)});
Expand Down
2 changes: 2 additions & 0 deletions lib/Refactoring/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ static void analyzeRenameScope(ValueDecl *VD, Optional<RenameRefInfo> RefInfo,
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::MacroDecl:
Expand Down Expand Up @@ -1085,6 +1086,7 @@ ExtractCheckResult checkExtractConditions(const ResolvedRangeInfo &RangeInfo,
break;

case swift::DeclContextKind::SerializedLocal:
case swift::DeclContextKind::Package:
case swift::DeclContextKind::Module:
case swift::DeclContextKind::FileUnit:
case swift::DeclContextKind::GenericTypeDecl:
Expand Down
2 changes: 2 additions & 0 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ static void printFullContext(const DeclContext *Context, raw_ostream &Buffer) {
if (!Context)
return;
switch (Context->getContextKind()) {
case swift::DeclContextKind::Package:
return;
case DeclContextKind::Module:
if (Context == cast<ModuleDecl>(Context)->getASTContext().TheBuiltinModule)
Buffer << cast<ModuleDecl>(Context)->getName() << ".";
Expand Down
Loading