Skip to content

Commit f192b92

Browse files
authored
Merge pull request #23785 from Azoy/context-cleanups
AST NFC: Minor cleanups around ASTContext
2 parents fb29702 + c47ca78 commit f192b92

File tree

6 files changed

+21
-94
lines changed

6 files changed

+21
-94
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@ class ASTContext final {
469469
/// Retrieve the type Swift.Never.
470470
CanType getNeverType() const;
471471

472-
/// Retrieve the declaration of Swift.Void.
473-
TypeAliasDecl *getVoidDecl() const;
474-
475472
/// Retrieve the declaration of ObjectiveC.ObjCBool.
476473
StructDecl *getObjCBoolDecl() const;
477474

@@ -530,12 +527,6 @@ class ASTContext final {
530527
/// Array.reserveCapacityForAppend(newElementsCount: Int)
531528
FuncDecl *getArrayReserveCapacityDecl() const;
532529

533-
/// Retrieve the declaration of Swift._unimplementedInitializer.
534-
FuncDecl *getUnimplementedInitializerDecl() const;
535-
536-
/// Retrieve the declaration of Swift._undefined.
537-
FuncDecl *getUndefinedDecl() const;
538-
539530
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
540531
FuncDecl *getIsOSVersionAtLeastDecl() const;
541532

include/swift/AST/KnownDecls.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ FUNC_DECL(ModifyAtReferenceWritableKeyPath, "_modifyAtReferenceWritableKeyPath")
8383

8484
FUNC_DECL(Swap, "swap")
8585

86+
FUNC_DECL(UnimplementedInitializer, "_unimplementedInitializer")
87+
FUNC_DECL(Undefined, "_undefined")
88+
8689
#undef FUNC_DECL

include/swift/AST/KnownStdlibTypes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
2626
#endif
2727

28+
KNOWN_STDLIB_TYPE_DECL(Void, TypeAliasDecl, 0)
29+
2830
KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)
2931

3032
KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ struct ASTContext::Implementation {
178178

179179
/// The declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.memory.
180180
VarDecl *AutoreleasingUnsafeMutablePointerMemoryDecl = nullptr;
181-
182-
/// The declaration of Swift.Void.
183-
TypeAliasDecl *VoidDecl = nullptr;
184181

185182
/// The declaration of ObjectiveC.ObjCBool.
186183
StructDecl *ObjCBoolDecl = nullptr;
@@ -210,12 +207,6 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
210207
/// func reserveCapacityForAppend(newElementsCount: Int)
211208
FuncDecl *ArrayReserveCapacityDecl = nullptr;
212209

213-
/// func _unimplementedInitializer(className: StaticString).
214-
FuncDecl *UnimplementedInitializerDecl = nullptr;
215-
216-
/// func _undefined<T>(msg: StaticString, file: StaticString, line: UInt) -> T
217-
FuncDecl *UndefinedDecl = nullptr;
218-
219210
/// func _stdlib_isOSVersionAtLeast(Builtin.Word,Builtin.Word, Builtin.word)
220211
// -> Builtin.Int1
221212
FuncDecl *IsOSVersionAtLeastDecl = nullptr;
@@ -641,25 +632,6 @@ void ASTContext::lookupInSwiftModule(
641632
M->lookupValue({ }, identifier, NLKind::UnqualifiedLookup, results);
642633
}
643634

644-
/// Find the generic implementation declaration for the named syntactic-sugar
645-
/// type.
646-
static NominalTypeDecl *findStdlibType(const ASTContext &ctx, StringRef name,
647-
unsigned genericParams) {
648-
// Find all of the declarations with this name in the Swift module.
649-
SmallVector<ValueDecl *, 1> results;
650-
ctx.lookupInSwiftModule(name, results);
651-
for (auto result : results) {
652-
if (auto nominal = dyn_cast<NominalTypeDecl>(result)) {
653-
auto params = nominal->getGenericParams();
654-
if (genericParams == (params == nullptr ? 0 : params->size())) {
655-
// We found it.
656-
return nominal;
657-
}
658-
}
659-
}
660-
return nullptr;
661-
}
662-
663635
FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
664636
if (getImpl().PlusFunctionOnRangeReplaceableCollection) {
665637
return getImpl().PlusFunctionOnRangeReplaceableCollection;
@@ -716,10 +688,20 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {
716688

717689
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
718690
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
719-
if (!getImpl().NAME##Decl) \
720-
getImpl().NAME##Decl = dyn_cast_or_null<DECL_CLASS>( \
721-
findStdlibType(*this, #NAME, NUM_GENERIC_PARAMS)); \
722-
return getImpl().NAME##Decl; \
691+
if (getImpl().NAME##Decl) \
692+
return getImpl().NAME##Decl; \
693+
SmallVector<ValueDecl *, 1> results; \
694+
lookupInSwiftModule(#NAME, results); \
695+
for (auto result : results) { \
696+
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
697+
auto params = type->getGenericParams(); \
698+
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
699+
getImpl().NAME##Decl = type; \
700+
return type; \
701+
} \
702+
} \
703+
} \
704+
return nullptr; \
723705
}
724706
#include "swift/AST/KnownStdlibTypes.def"
725707

@@ -820,24 +802,6 @@ CanType ASTContext::getNeverType() const {
820802
return neverDecl->getDeclaredType()->getCanonicalType();
821803
}
822804

823-
TypeAliasDecl *ASTContext::getVoidDecl() const {
824-
if (getImpl().VoidDecl) {
825-
return getImpl().VoidDecl;
826-
}
827-
828-
// Go find 'Void' in the Swift module.
829-
SmallVector<ValueDecl *, 1> results;
830-
lookupInSwiftModule("Void", results);
831-
for (auto result : results) {
832-
if (auto typeAlias = dyn_cast<TypeAliasDecl>(result)) {
833-
getImpl().VoidDecl = typeAlias;
834-
return typeAlias;
835-
}
836-
}
837-
838-
return getImpl().VoidDecl;
839-
}
840-
841805
StructDecl *ASTContext::getObjCBoolDecl() const {
842806
if (!getImpl().ObjCBoolDecl) {
843807
SmallVector<ValueDecl *, 1> results;
@@ -1186,39 +1150,6 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
11861150
return nullptr;
11871151
}
11881152

1189-
FuncDecl *
1190-
ASTContext::getUnimplementedInitializerDecl() const {
1191-
if (getImpl().UnimplementedInitializerDecl)
1192-
return getImpl().UnimplementedInitializerDecl;
1193-
1194-
// Look for the function.
1195-
auto decl = findLibraryIntrinsic(*this, "_unimplementedInitializer");
1196-
if (!decl)
1197-
return nullptr;
1198-
1199-
if (!getIntrinsicCandidateType(decl, /*allowTypeMembers=*/false))
1200-
return nullptr;
1201-
1202-
// FIXME: Check inputs and outputs.
1203-
1204-
getImpl().UnimplementedInitializerDecl = decl;
1205-
return decl;
1206-
}
1207-
1208-
FuncDecl *
1209-
ASTContext::getUndefinedDecl() const {
1210-
if (getImpl().UndefinedDecl)
1211-
return getImpl().UndefinedDecl;
1212-
1213-
// Look for the function.
1214-
auto decl = findLibraryIntrinsic(*this, "_undefined");
1215-
if (!decl)
1216-
return nullptr;
1217-
1218-
getImpl().UndefinedDecl = decl;
1219-
return decl;
1220-
}
1221-
12221153
FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
12231154
if (getImpl().IsOSVersionAtLeastDecl)
12241155
return getImpl().IsOSVersionAtLeastDecl;

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4021,7 +4021,7 @@ namespace {
40214021
auto &tc = cs.getTypeChecker();
40224022
auto &ctx = tc.Context;
40234023
// Synthesize a call to _undefined() of appropriate type.
4024-
FuncDecl *undefinedDecl = ctx.getUndefinedDecl();
4024+
FuncDecl *undefinedDecl = ctx.getUndefined();
40254025
if (!undefinedDecl) {
40264026
tc.diagnose(E->getLoc(), diag::missing_undefined_runtime);
40274027
return nullptr;

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ static void synthesizeStubBody(AbstractFunctionDecl *fn, void *) {
18021802
auto *ctor = cast<ConstructorDecl>(fn);
18031803
auto &ctx = ctor->getASTContext();
18041804

1805-
auto unimplementedInitDecl = ctx.getUnimplementedInitializerDecl();
1805+
auto unimplementedInitDecl = ctx.getUnimplementedInitializer();
18061806
auto classDecl = ctor->getDeclContext()->getSelfClassDecl();
18071807
if (!unimplementedInitDecl) {
18081808
ctx.Diags.diagnose(classDecl->getLoc(),

0 commit comments

Comments
 (0)