Skip to content

AST NFC: Minor cleanups around ASTContext #23785

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
Apr 16, 2019
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
9 changes: 0 additions & 9 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,6 @@ class ASTContext final {
/// Retrieve the type Swift.Never.
CanType getNeverType() const;

/// Retrieve the declaration of Swift.Void.
TypeAliasDecl *getVoidDecl() const;

/// Retrieve the declaration of ObjectiveC.ObjCBool.
StructDecl *getObjCBoolDecl() const;

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

/// Retrieve the declaration of Swift._unimplementedInitializer.
FuncDecl *getUnimplementedInitializerDecl() const;

/// Retrieve the declaration of Swift._undefined.
FuncDecl *getUndefinedDecl() const;

// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
FuncDecl *getIsOSVersionAtLeastDecl() const;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/KnownDecls.def
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ FUNC_DECL(ModifyAtReferenceWritableKeyPath, "_modifyAtReferenceWritableKeyPath")

FUNC_DECL(Swap, "swap")

FUNC_DECL(UnimplementedInitializer, "_unimplementedInitializer")
FUNC_DECL(Undefined, "_undefined")

#undef FUNC_DECL
2 changes: 2 additions & 0 deletions include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
#endif

KNOWN_STDLIB_TYPE_DECL(Void, TypeAliasDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)
Expand Down
97 changes: 14 additions & 83 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ struct ASTContext::Implementation {

/// The declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.memory.
VarDecl *AutoreleasingUnsafeMutablePointerMemoryDecl = nullptr;

/// The declaration of Swift.Void.
TypeAliasDecl *VoidDecl = nullptr;

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

/// func _unimplementedInitializer(className: StaticString).
FuncDecl *UnimplementedInitializerDecl = nullptr;

/// func _undefined<T>(msg: StaticString, file: StaticString, line: UInt) -> T
FuncDecl *UndefinedDecl = nullptr;

/// func _stdlib_isOSVersionAtLeast(Builtin.Word,Builtin.Word, Builtin.word)
// -> Builtin.Int1
FuncDecl *IsOSVersionAtLeastDecl = nullptr;
Expand Down Expand Up @@ -641,25 +632,6 @@ void ASTContext::lookupInSwiftModule(
M->lookupValue({ }, identifier, NLKind::UnqualifiedLookup, results);
}

/// Find the generic implementation declaration for the named syntactic-sugar
/// type.
static NominalTypeDecl *findStdlibType(const ASTContext &ctx, StringRef name,
unsigned genericParams) {
// Find all of the declarations with this name in the Swift module.
SmallVector<ValueDecl *, 1> results;
ctx.lookupInSwiftModule(name, results);
for (auto result : results) {
if (auto nominal = dyn_cast<NominalTypeDecl>(result)) {
auto params = nominal->getGenericParams();
if (genericParams == (params == nullptr ? 0 : params->size())) {
// We found it.
return nominal;
}
}
}
return nullptr;
}

FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
if (getImpl().PlusFunctionOnRangeReplaceableCollection) {
return getImpl().PlusFunctionOnRangeReplaceableCollection;
Expand Down Expand Up @@ -716,10 +688,20 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
if (!getImpl().NAME##Decl) \
getImpl().NAME##Decl = dyn_cast_or_null<DECL_CLASS>( \
findStdlibType(*this, #NAME, NUM_GENERIC_PARAMS)); \
return getImpl().NAME##Decl; \
if (getImpl().NAME##Decl) \
return getImpl().NAME##Decl; \
SmallVector<ValueDecl *, 1> results; \
lookupInSwiftModule(#NAME, results); \
for (auto result : results) { \
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
auto params = type->getGenericParams(); \
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
getImpl().NAME##Decl = type; \
return type; \
} \
} \
} \
return nullptr; \
}
#include "swift/AST/KnownStdlibTypes.def"

Expand Down Expand Up @@ -820,24 +802,6 @@ CanType ASTContext::getNeverType() const {
return neverDecl->getDeclaredType()->getCanonicalType();
}

TypeAliasDecl *ASTContext::getVoidDecl() const {
if (getImpl().VoidDecl) {
return getImpl().VoidDecl;
}

// Go find 'Void' in the Swift module.
SmallVector<ValueDecl *, 1> results;
lookupInSwiftModule("Void", results);
for (auto result : results) {
if (auto typeAlias = dyn_cast<TypeAliasDecl>(result)) {
getImpl().VoidDecl = typeAlias;
return typeAlias;
}
}

return getImpl().VoidDecl;
}

StructDecl *ASTContext::getObjCBoolDecl() const {
if (!getImpl().ObjCBoolDecl) {
SmallVector<ValueDecl *, 1> results;
Expand Down Expand Up @@ -1186,39 +1150,6 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
return nullptr;
}

FuncDecl *
ASTContext::getUnimplementedInitializerDecl() const {
if (getImpl().UnimplementedInitializerDecl)
return getImpl().UnimplementedInitializerDecl;

// Look for the function.
auto decl = findLibraryIntrinsic(*this, "_unimplementedInitializer");
if (!decl)
return nullptr;

if (!getIntrinsicCandidateType(decl, /*allowTypeMembers=*/false))
return nullptr;

// FIXME: Check inputs and outputs.

getImpl().UnimplementedInitializerDecl = decl;
return decl;
}

FuncDecl *
ASTContext::getUndefinedDecl() const {
if (getImpl().UndefinedDecl)
return getImpl().UndefinedDecl;

// Look for the function.
auto decl = findLibraryIntrinsic(*this, "_undefined");
if (!decl)
return nullptr;

getImpl().UndefinedDecl = decl;
return decl;
}

FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
if (getImpl().IsOSVersionAtLeastDecl)
return getImpl().IsOSVersionAtLeastDecl;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3795,7 +3795,7 @@ namespace {
auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
// Synthesize a call to _undefined() of appropriate type.
FuncDecl *undefinedDecl = ctx.getUndefinedDecl();
FuncDecl *undefinedDecl = ctx.getUndefined();
if (!undefinedDecl) {
tc.diagnose(E->getLoc(), diag::missing_undefined_runtime);
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ static void synthesizeStubBody(AbstractFunctionDecl *fn, void *) {
auto *ctor = cast<ConstructorDecl>(fn);
auto &ctx = ctor->getASTContext();

auto unimplementedInitDecl = ctx.getUnimplementedInitializerDecl();
auto unimplementedInitDecl = ctx.getUnimplementedInitializer();
auto classDecl = ctor->getDeclContext()->getSelfClassDecl();
if (!unimplementedInitDecl) {
ctx.Diags.diagnose(classDecl->getLoc(),
Expand Down