Skip to content
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
98 changes: 98 additions & 0 deletions include/swift/AST/ConformanceLookup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//===--- ConformanceLookup.h - Global conformance lookup --------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_AST_CONFORMANCELOOKUP_H
#define SWIFT_AST_CONFORMANCELOOKUP_H

#include "llvm/ADT/ArrayRef.h"
#include <optional>

namespace swift {

class CanType;
class Type;
class ProtocolConformanceRef;
class ProtocolDecl;

/// Global conformance lookup, does not check conditional requirements.
///
/// \param type The type for which we are computing conformance.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search failed, otherwise an
/// abstract, concrete or pack conformance, depending on the lookup type.
ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
bool allowMissing = false);

/// Global conformance lookup, checks conditional requirements.
/// Requires a contextualized type.
///
/// \param type The type for which we are computing conformance. Must not
/// contain type parameters.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search failed, otherwise an
/// abstract, concrete or pack conformance, depending on the lookup type.
ProtocolConformanceRef checkConformance(Type type, ProtocolDecl *protocol,
// Note: different default from
// lookupConformance
bool allowMissing = true);

/// Global conformance lookup, checks conditional requirements.
/// Accepts interface types without context. If the conformance cannot be
/// definitively established without the missing context, returns \c nullopt.
///
/// \param type The type for which we are computing conformance. Must not
/// contain type parameters.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search definitively failed. An
/// abstract, concrete or pack conformance, depending on the lookup type,
/// if the search succeeded. `std::nullopt` if the type could have
/// conditionally conformed depending on the context of the interface types.
std::optional<ProtocolConformanceRef>
checkConformanceWithoutContext(Type type,
ProtocolDecl *protocol,
// Note: different default from
// lookupConformance
bool allowMissing = true);


/// Look for the conformance of the given existential type to the given
/// protocol.
ProtocolConformanceRef lookupExistentialConformance(Type type,
ProtocolDecl *protocol);

/// Collect the conformances of \c fromType to each of the protocols of an
/// existential type's layout.
llvm::ArrayRef<ProtocolConformanceRef>
collectExistentialConformances(CanType fromType, CanType existential,
bool allowMissing = false);

}

#endif // SWIFT_AST_CONFORMANCELOOKUP_H
70 changes: 0 additions & 70 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,76 +876,6 @@ class ModuleDecl
DeclName name,
SmallVectorImpl<ValueDecl*> &results) const;

/// Global conformance lookup, does not check conditional requirements.
///
/// \param type The type for which we are computing conformance.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search failed, otherwise an
/// abstract, concrete or pack conformance, depending on the lookup type.
static ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
bool allowMissing = false);

/// Global conformance lookup, checks conditional requirements.
/// Requires a contextualized type.
///
/// \param type The type for which we are computing conformance. Must not
/// contain type parameters.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search failed, otherwise an
/// abstract, concrete or pack conformance, depending on the lookup type.
static ProtocolConformanceRef checkConformance(Type type, ProtocolDecl *protocol,
// Note: different default from
// lookupConformance
bool allowMissing = true);

/// Global conformance lookup, checks conditional requirements.
/// Accepts interface types without context. If the conformance cannot be
/// definitively established without the missing context, returns \c nullopt.
///
/// \param type The type for which we are computing conformance. Must not
/// contain type parameters.
///
/// \param protocol The protocol to which we are computing conformance.
///
/// \param allowMissing When \c true, the resulting conformance reference
/// might include "missing" conformances, which are synthesized for some
/// protocols as an error recovery mechanism.
///
/// \returns An invalid conformance if the search definitively failed. An
/// abstract, concrete or pack conformance, depending on the lookup type,
/// if the search succeeded. `std::nullopt` if the type could have
/// conditionally conformed depending on the context of the interface types.
std::optional<ProtocolConformanceRef>
static checkConformanceWithoutContext(Type type,
ProtocolDecl *protocol,
// Note: different default from
// lookupConformance
bool allowMissing = true);


/// Look for the conformance of the given existential type to the given
/// protocol.
static ProtocolConformanceRef lookupExistentialConformance(Type type,
ProtocolDecl *protocol);

/// Collect the conformances of \c fromType to each of the protocols of an
/// existential type's layout.
ArrayRef<ProtocolConformanceRef>
static collectExistentialConformances(CanType fromType, CanType existential,
bool allowMissing = false);

/// Find a member named \p name in \p container that was declared in this
/// module.
///
Expand Down
3 changes: 2 additions & 1 deletion include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef SWIFT_SIL_SILCLONER_H
#define SWIFT_SIL_SILCLONER_H

#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/SIL/BasicBlockUtils.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ struct SubstitutionMapWithLocalArchetypes {
Type substType,
ProtocolDecl *proto) {
if (isa<LocalArchetypeType>(origType))
return ModuleDecl::lookupConformance(substType, proto);
return swift::lookupConformance(substType, proto);
if (SubsMap)
return SubsMap->lookupConformance(origType, proto);

Expand Down
8 changes: 4 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/ConcreteDeclRef.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/DiagnosticsSema.h"
Expand Down Expand Up @@ -1613,8 +1614,7 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,

auto type = decl->getDeclaredInterfaceType();
auto builtinProtocol = getProtocol(builtinProtocolKind);
auto builtinConformance = ModuleDecl::lookupConformance(
type, builtinProtocol);
auto builtinConformance = lookupConformance(type, builtinProtocol);
if (builtinConformance.isInvalid()) {
assert(false && "Missing required conformance");
witness = ConcreteDeclRef();
Expand Down Expand Up @@ -5834,7 +5834,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
if (nominal != dc->getASTContext().getOptionalDecl()) {
if (auto objcBridgeable
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
auto conformance = ModuleDecl::lookupConformance(
auto conformance = lookupConformance(
nominal->getDeclaredInterfaceType(), objcBridgeable);
if (conformance) {
result =
Expand Down Expand Up @@ -5986,7 +5986,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
if (!proto)
return ProtocolConformanceRef::forInvalid();

return ModuleDecl::lookupConformance(type, proto);
return lookupConformance(type, proto);
};

// Do we conform to _ObjectiveCBridgeable?
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/AST/Builtins.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/Comment.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/FileUnit.h"
Expand Down Expand Up @@ -1799,7 +1800,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
return type;
},
[&](CanType depType, Type substType, ProtocolDecl *proto) {
return ModuleDecl::lookupConformance(substType, proto);
return lookupConformance(substType, proto);
});
};

Expand Down
Loading