Skip to content

Commit 2abf14a

Browse files
authored
Merge pull request #64034 from apple/es-declctx
[NFC] Create `PackageUnit` class, and `Package` entries to DeclContext / ASTHierarchy
2 parents 877d03f + 1a5882e commit 2abf14a

22 files changed

+130
-14
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ namespace swift {
103103
class Identifier;
104104
class InheritedNameSet;
105105
class ModuleDecl;
106+
class PackageUnit;
106107
class ModuleDependenciesCache;
107108
class ModuleLoader;
108109
class NominalTypeDecl;

include/swift/AST/ASTWalker.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Expr;
2626
class ClosureExpr;
2727
class CustomAttr;
2828
class ModuleDecl;
29+
class PackageUnit;
2930
class Stmt;
3031
class Pattern;
3132
class TypeRepr;
@@ -101,14 +102,15 @@ enum class MacroWalking {
101102
class ASTWalker {
102103
public:
103104
enum class ParentKind {
104-
Module, Decl, Stmt, Expr, Pattern, TypeRepr
105+
Package, Module, Decl, Stmt, Expr, Pattern, TypeRepr
105106
};
106107

107108
class ParentTy {
108109
ParentKind Kind;
109110
void *Ptr = nullptr;
110111

111112
public:
113+
ParentTy(PackageUnit *Pkg) : Kind(ParentKind::Package), Ptr(Pkg) {}
112114
ParentTy(ModuleDecl *Mod) : Kind(ParentKind::Module), Ptr(Mod) {}
113115
ParentTy(Decl *D) : Kind(ParentKind::Decl), Ptr(D) {}
114116
ParentTy(Stmt *S) : Kind(ParentKind::Stmt), Ptr(S) {}
@@ -123,6 +125,10 @@ class ASTWalker {
123125
return Kind;
124126
}
125127

128+
PackageUnit *getAsPackage() const {
129+
return Kind == ParentKind::Package ? static_cast<PackageUnit*>(Ptr)
130+
: nullptr;
131+
}
126132
ModuleDecl *getAsModule() const {
127133
return Kind == ParentKind::Module ? static_cast<ModuleDecl*>(Ptr)
128134
: nullptr;

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4009,7 +4009,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
40094009
bool isGlobalActor() const {
40104010
return getGlobalActorInstance() != nullptr;
40114011
}
4012-
4012+
40134013
/// Return the `DestructorDecl` for a struct or enum's `deinit` declaration.
40144014
/// Returns null if the type is a class, or does not have a declared `deinit`.
40154015
DestructorDecl *getValueTypeDestructor();

include/swift/AST/DeclContext.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace swift {
6666
class SourceFile;
6767
class Type;
6868
class ModuleDecl;
69+
class PackageUnit;
6970
class GenericTypeDecl;
7071
class NominalTypeDecl;
7172
class PrecedenceGroupLookupResult;
@@ -96,7 +97,7 @@ enum class DeclContextKind : unsigned {
9697
SerializedLocal,
9798
MacroDecl,
9899
Last_LocalDeclContextKind = MacroDecl,
99-
100+
Package,
100101
Module,
101102
FileUnit,
102103
GenericTypeDecl,
@@ -234,6 +235,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
234235
Decl,
235236
Expr,
236237
FileUnit,
238+
Package,
237239
Initializer,
238240
SerializedLocal,
239241
// If you add a new AST hierarchies, then update the static_assert() below.
@@ -271,6 +273,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
271273
return ASTHierarchy::SerializedLocal;
272274
case DeclContextKind::FileUnit:
273275
return ASTHierarchy::FileUnit;
276+
case DeclContextKind::Package:
277+
return ASTHierarchy::Package;
274278
case DeclContextKind::Module:
275279
case DeclContextKind::TopLevelCodeDecl:
276280
case DeclContextKind::AbstractFunctionDecl:
@@ -296,7 +300,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
296300

297301
DeclContext(DeclContextKind Kind, DeclContext *Parent)
298302
: ParentAndKind(Parent, getASTHierarchyFromKind(Kind)) {
299-
if (Kind != DeclContextKind::Module)
303+
// if Module kind, it may (or may not) have Package as its parent
304+
if (Kind != DeclContextKind::Package && Kind != DeclContextKind::Module)
300305
assert(Parent != nullptr && "DeclContext must have a parent context");
301306
}
302307

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

327+
/// \returns true if this is a context with package-wide scope, e.g. a package,
328+
/// a module, or a source file.
329+
LLVM_READONLY
330+
bool isPackageScopeContext() const; // see swift/AST/Module.h
331+
332+
LLVM_READONLY
333+
bool isPackageContext() const; // see swift/AST/Module.h
334+
322335
/// isModuleContext - Return true if this is a subclass of Module.
323336
LLVM_READONLY
324337
bool isModuleContext() const; // see swift/AST/Module.h
@@ -499,6 +512,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
499512
return false;
500513
}
501514

515+
/// Returns the package context of the parent module.
516+
LLVM_READONLY
517+
PackageUnit *getParentModulePackage() const;
518+
502519
/// Returns the module context that contains this context.
503520
LLVM_READONLY
504521
ModuleDecl *getParentModule() const;

include/swift/AST/Module.h

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ class OverlayFile;
159159
/// location.
160160
class ModuleSourceFileLocationMap;
161161

162+
/// Package unit used to allow grouping of modules by a package name.
163+
/// ModuleDecl can set PackageUnit as a parent in its DeclContext
164+
/// See \c ModuleDecl
165+
class PackageUnit: public DeclContext {
166+
167+
Identifier PackageName;
168+
169+
PackageUnit(Identifier name);
170+
171+
public:
172+
static PackageUnit *
173+
create(Identifier name, ASTContext &ctx) {
174+
return new (ctx) PackageUnit(name);
175+
}
176+
177+
static bool classof(const DeclContext *DC) {
178+
return DC->getContextKind() == DeclContextKind::Package;
179+
}
180+
181+
static bool classof(const PackageUnit *PU) {
182+
// FIXME: add a correct check
183+
return true;
184+
}
185+
186+
Identifier getName() const {
187+
return PackageName;
188+
}
189+
};
190+
162191
/// The minimum unit of compilation.
163192
///
164193
/// A module is made up of several file-units, which are all part of the same
@@ -284,7 +313,7 @@ class ModuleDecl
284313
/// Used by the debugger to bypass resilient access to fields.
285314
bool BypassResilience = false;
286315

287-
ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo);
316+
ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo, PackageUnit *pkg);
288317

289318
public:
290319
/// Creates a new module with a given \p name.
@@ -293,13 +322,14 @@ class ModuleDecl
293322
/// imported by each file of this module.
294323
static ModuleDecl *
295324
create(Identifier name, ASTContext &ctx,
296-
ImplicitImportInfo importInfo = ImplicitImportInfo()) {
297-
return new (ctx) ModuleDecl(name, ctx, importInfo);
325+
ImplicitImportInfo importInfo = ImplicitImportInfo(),
326+
PackageUnit *pkg = nullptr) {
327+
return new (ctx) ModuleDecl(name, ctx, importInfo, pkg);
298328
}
299329

300330
static ModuleDecl *
301-
createMainModule(ASTContext &ctx, Identifier name, ImplicitImportInfo iinfo) {
302-
auto *Mod = ModuleDecl::create(name, ctx, iinfo);
331+
createMainModule(ASTContext &ctx, Identifier name, ImplicitImportInfo iinfo, PackageUnit *pkg = nullptr) {
332+
auto *Mod = ModuleDecl::create(name, ctx, iinfo, pkg);
303333
Mod->Bits.ModuleDecl.IsMainModule = true;
304334
return Mod;
305335
}
@@ -416,6 +446,10 @@ class ModuleDecl
416446
/// Set the name of the package this module belongs to
417447
void setPackageName(Identifier name) {
418448
PackageName = name;
449+
// TODO: uncomment when PackageUnit gets passed to constructor
450+
// PackageUnit *pkgUnit = PackageUnit::create(name, getASTContext());
451+
// DeclContext newContext = DeclContext(DeclContextKind::Module, pkgUnit);
452+
// setDeclContext(&newContext);
419453
}
420454

421455
Identifier getExportAsName() const { return ExportAsName; }
@@ -1036,6 +1070,10 @@ inline bool DeclContext::isModuleScopeContext() const {
10361070
return isModuleContext();
10371071
}
10381072

1073+
inline bool DeclContext::isPackageScopeContext() const {
1074+
return ParentAndKind.getInt() == ASTHierarchy::Package;
1075+
}
1076+
10391077
/// Extract the source location from the given module declaration.
10401078
inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
10411079
return extractNearestSourceLoc(static_cast<const Decl *>(mod));

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,10 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
13591359
}
13601360

13611361
switch (dc->getContextKind()) {
1362+
case DeclContextKind::Package:
1363+
printName(os, cast<PackageUnit>(dc)->getName());
1364+
break;
1365+
13621366
case DeclContextKind::Module:
13631367
printName(os, cast<ModuleDecl>(dc)->getRealName());
13641368
break;

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,8 @@ static Optional<VarDecl*> findFirstVariable(PatternBindingDecl *binding) {
22752275

22762276
void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName) {
22772277
switch (ctx->getContextKind()) {
2278+
case DeclContextKind::Package:
2279+
return;
22782280
case DeclContextKind::Module:
22792281
return appendModule(cast<ModuleDecl>(ctx), useModuleName);
22802282

lib/AST/AccessRequests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
115115
case DeclContextKind::SubscriptDecl:
116116
case DeclContextKind::EnumElementDecl:
117117
return AccessLevel::Private;
118+
case DeclContextKind::Package:
119+
return AccessLevel::Package;
118120
case DeclContextKind::Module:
119121
case DeclContextKind::FileUnit:
120122
return AccessLevel::Internal;

lib/AST/DeclContext.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ DeclContext *DeclContext::getParentForLookup() const {
274274
return getParent();
275275
}
276276

277+
PackageUnit *DeclContext::getParentModulePackage() const {
278+
auto parentModule = getParentModule();
279+
auto pkg = parentModule->getParent();
280+
if (pkg)
281+
return const_cast<PackageUnit *>(cast<PackageUnit>(pkg));
282+
return nullptr;
283+
}
284+
277285
ModuleDecl *DeclContext::getParentModule() const {
278286
const DeclContext *DC = this;
279287
while (!DC->isModuleContext())
@@ -306,6 +314,7 @@ SourceFile *DeclContext::getParentSourceFile() const {
306314
case DeclContextKind::Initializer:
307315
case DeclContextKind::FileUnit:
308316
case DeclContextKind::Module:
317+
case DeclContextKind::Package:
309318
case DeclContextKind::SerializedLocal:
310319
break;
311320
}
@@ -559,6 +568,8 @@ bool DeclContext::canBeParentOfExtension() const {
559568

560569
bool DeclContext::walkContext(ASTWalker &Walker) {
561570
switch (getContextKind()) {
571+
case DeclContextKind::Package:
572+
return false;
562573
case DeclContextKind::Module:
563574
return cast<ModuleDecl>(this)->walk(Walker);
564575
case DeclContextKind::FileUnit:
@@ -652,6 +663,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
652663

653664
const char *Kind;
654665
switch (getContextKind()) {
666+
case DeclContextKind::Package: Kind = "Package"; break;
655667
case DeclContextKind::Module: Kind = "Module"; break;
656668
case DeclContextKind::FileUnit: Kind = "FileUnit"; break;
657669
case DeclContextKind::SerializedLocal: Kind = "Serialized Local"; break;
@@ -678,6 +690,9 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
678690
OS.indent(Depth*2 + indent) << (void*)this << " " << Kind;
679691

680692
switch (getContextKind()) {
693+
case DeclContextKind::Package:
694+
OS << " name=" << cast<PackageUnit>(this)->getName();
695+
break;
681696
case DeclContextKind::Module:
682697
OS << " name=" << cast<ModuleDecl>(this)->getName();
683698
break;
@@ -1206,7 +1221,7 @@ AccessScope::AccessScope(const DeclContext *DC, AccessLimitKind limitKind)
12061221
Value.setPointer(DC);
12071222
isPrivate = true;
12081223
}
1209-
if (!DC || isa<ModuleDecl>(DC))
1224+
if (!DC || isa<ModuleDecl>(DC) || isa<PackageUnit>(DC))
12101225
assert(!isPrivate && "public, package, or internal scope can't be private");
12111226
}
12121227

@@ -1285,6 +1300,8 @@ DeclContextKind DeclContext::getContextKind() const {
12851300
return DeclContextKind::SerializedLocal;
12861301
case ASTHierarchy::FileUnit:
12871302
return DeclContextKind::FileUnit;
1303+
case ASTHierarchy::Package:
1304+
return DeclContextKind::Package;
12881305
case ASTHierarchy::Decl: {
12891306
auto decl = reinterpret_cast<const Decl*>(this + 1);
12901307
if (isa<AbstractFunctionDecl>(decl))
@@ -1334,6 +1351,7 @@ bool DeclContext::isAsyncContext() const {
13341351
case DeclContextKind::EnumElementDecl:
13351352
case DeclContextKind::ExtensionDecl:
13361353
case DeclContextKind::SerializedLocal:
1354+
case DeclContextKind::Package:
13371355
case DeclContextKind::Module:
13381356
case DeclContextKind::GenericTypeDecl:
13391357
case DeclContextKind::MacroDecl:
@@ -1361,6 +1379,7 @@ bool DeclContext::isAsyncContext() const {
13611379

13621380
SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
13631381
switch (dc->getContextKind()) {
1382+
case DeclContextKind::Package:
13641383
case DeclContextKind::Module:
13651384
return SourceLoc();
13661385
case DeclContextKind::AbstractFunctionDecl:

lib/AST/DiagnosticEngine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,9 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
11631163

11641164
while (!dc->isModuleContext()) {
11651165
switch (dc->getContextKind()) {
1166+
case DeclContextKind::Package:
1167+
llvm_unreachable("Not in a package context!");
1168+
break;
11661169
case DeclContextKind::Module:
11671170
llvm_unreachable("Not in a module context!");
11681171
break;

lib/AST/Module.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,19 @@ void SourceLookupCache::invalidate() {
463463
(void)SameSizeSmallVector{std::move(AllVisibleValues)};
464464
}
465465

466+
PackageUnit::PackageUnit(Identifier name)
467+
: DeclContext(DeclContextKind::Package, nullptr) {
468+
PackageName = name;
469+
}
470+
466471
//===----------------------------------------------------------------------===//
467472
// Module Implementation
468473
//===----------------------------------------------------------------------===//
469474

470475
ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
471-
ImplicitImportInfo importInfo)
472-
: DeclContext(DeclContextKind::Module, nullptr),
476+
ImplicitImportInfo importInfo,
477+
PackageUnit *pkg = nullptr)
478+
: DeclContext(DeclContextKind::Module, pkg),
473479
TypeDecl(DeclKind::Module, &ctx, name, SourceLoc(), {}),
474480
ImportInfo(importInfo) {
475481

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,8 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,
893893
ConstantReference::Direct};
894894
}
895895

896+
case DeclContextKind::Package:
897+
assert(false && "package decl context kind should not have been reached");
896898
case DeclContextKind::FileUnit:
897899
case DeclContextKind::MacroDecl:
898900
parent = parent->getParentModule();

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
612612
case DeclContextKind::TopLevelCodeDecl:
613613
return getOrCreateContext(DC->getParent());
614614

615+
case DeclContextKind::Package: {
616+
auto *pkg = cast<PackageUnit>(DC);
617+
return getOrCreateContext(pkg);
618+
}
615619
case DeclContextKind::Module:
616620
return getOrCreateModule(
617621
{ImportPath::Access(), cast<ModuleDecl>(DC)});

lib/Refactoring/Refactoring.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ static void analyzeRenameScope(ValueDecl *VD, Optional<RenameRefInfo> RefInfo,
888888
case DeclContextKind::AbstractClosureExpr:
889889
case DeclContextKind::Initializer:
890890
case DeclContextKind::SerializedLocal:
891+
case DeclContextKind::Package:
891892
case DeclContextKind::Module:
892893
case DeclContextKind::FileUnit:
893894
case DeclContextKind::MacroDecl:
@@ -1085,6 +1086,7 @@ ExtractCheckResult checkExtractConditions(const ResolvedRangeInfo &RangeInfo,
10851086
break;
10861087

10871088
case swift::DeclContextKind::SerializedLocal:
1089+
case swift::DeclContextKind::Package:
10881090
case swift::DeclContextKind::Module:
10891091
case swift::DeclContextKind::FileUnit:
10901092
case swift::DeclContextKind::GenericTypeDecl:

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ static void printFullContext(const DeclContext *Context, raw_ostream &Buffer) {
186186
if (!Context)
187187
return;
188188
switch (Context->getContextKind()) {
189+
case swift::DeclContextKind::Package:
190+
return;
189191
case DeclContextKind::Module:
190192
if (Context == cast<ModuleDecl>(Context)->getASTContext().TheBuiltinModule)
191193
Buffer << cast<ModuleDecl>(Context)->getName() << ".";

0 commit comments

Comments
 (0)