Skip to content

[NFC] Abstracting isStdlibType and isStdLibDecl logic into Type and Decl #30115

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
Feb 28, 2020
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
3 changes: 3 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ class alignas(1 << DeclAlignInBits) Decl {
bool hasUnderscoredNaming() const;

bool isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic = true) const;

/// Check if this is a declaration defined at the top level of the Swift module
bool isStdlibDecl() const;

AvailabilityContext getAvailabilityForLinkage() const;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {

/// Check if this type is equal to Builtin.IntN.
bool isBuiltinIntegerType(unsigned bitWidth);

/// Check if this is a nominal type defined at the top level of the Swift module
bool isStdlibType();

/// If this is a class type or a bound generic class type, returns the
/// (possibly generic) class.
Expand Down
9 changes: 2 additions & 7 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,6 @@ static const char *getMetatypeRepresentationOp(MetatypeRepresentation Rep) {
llvm_unreachable("Unhandled MetatypeRepresentation in switch.");
}

static bool isStdlibType(const TypeDecl *decl) {
DeclContext *dc = decl->getDeclContext();
return dc->isModuleScopeContext() && dc->getParentModule()->isStdlibModule();
}

/// Whether to mangle the given type as generic.
static bool shouldMangleAsGeneric(Type type) {
if (!type)
Expand Down Expand Up @@ -986,7 +981,7 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
if (tryMangleTypeSubstitution(tybase))
return;

if (isStdlibType(Decl) && Decl->getName().str() == "Optional") {
if (Decl->isStdlibDecl() && Decl->getName().str() == "Optional") {
auto GenArgs = type->castTo<BoundGenericType>()->getGenericArgs();
assert(GenArgs.size() == 1);
appendType(GenArgs[0], forDecl);
Expand Down Expand Up @@ -2483,7 +2478,7 @@ void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling)

bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
// Bail out if our parent isn't the swift standard library.
if (!isStdlibType(decl))
if (!decl->isStdlibDecl())
return false;

if (isa<NominalTypeDecl>(decl)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ bool Decl::isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic) const {
return hasUnderscoredNaming();
}

bool Decl::isStdlibDecl() const {
DeclContext *DC = getDeclContext();
return DC->isModuleScopeContext() &&
DC->getParentModule()->isStdlibModule();
}

AvailabilityContext Decl::getAvailabilityForLinkage() const {
auto containingContext =
AvailabilityInference::annotatedAvailableRange(this, getASTContext());
Expand Down
9 changes: 9 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,15 @@ bool TypeBase::isExistentialWithError() {
return layout.isExistentialWithError(getASTContext());
}

bool TypeBase::isStdlibType() {
if (auto *NTD = getAnyNominal()) {
auto *DC = NTD->getDeclContext();
return DC->isModuleScopeContext() &&
DC->getParentModule()->isStdlibModule();
}
return false;
}

/// Remove argument labels from the function type.
Type TypeBase::removeArgumentLabels(unsigned numArgumentLabels) {
// If there is nothing to remove, don't.
Expand Down
12 changes: 1 addition & 11 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,18 +515,8 @@ bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() {
if (!ODRE)
return false;

auto isStdlibType = [](Type type) {
if (auto *NTD = type->getAnyNominal()) {
auto *DC = NTD->getDeclContext();
return DC->isModuleScopeContext() &&
DC->getParentModule()->isStdlibModule();
}

return false;
};

auto name = ODRE->getDecls().front()->getBaseName();
if (!(name.isOperator() && isStdlibType(getLHS()) && isStdlibType(getRHS())))
if (!(name.isOperator() && getLHS()->isStdlibType() && getRHS()->isStdlibType()))
return false;

// If this is an operator reference and both types are from stdlib,
Expand Down
11 changes: 1 addition & 10 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,16 +1330,7 @@ assessRequirementFailureImpact(ConstraintSystem &cs, Type requirementType,
if (!anchor)
return 1;

auto isStdlibType = [&](Type type) {
if (auto *NTD = type->getAnyNominal()) {
auto *DC = NTD->getDeclContext();
return DC->isModuleScopeContext() &&
DC->getParentModule()->isStdlibModule();
}
return false;
};

if (requirementType && isStdlibType(cs.simplifyType(requirementType))) {
if (requirementType && cs.simplifyType(requirementType)->isStdlibType()) {
if (auto last = locator.last()) {
if (auto requirement = last->getAs<LocatorPathElt::AnyRequirement>()) {
auto kind = requirement->getRequirementKind();
Expand Down