diff --git a/include/swift/SIL/SILFunction.h b/include/swift/SIL/SILFunction.h index 9050d987a4f..0b9541b5b31 100644 --- a/include/swift/SIL/SILFunction.h +++ b/include/swift/SIL/SILFunction.h @@ -63,6 +63,9 @@ private: /// The lowered type of the function. CanSILFunctionType LoweredType; + /// The context archetypes of the function. + GenericParamList *ContextGenericParams; + /// The collection of all BasicBlocks in the SILFunction. Empty for external /// function references. BlockListType BlockList; @@ -168,6 +171,32 @@ public: /// Get this function's transparent attribute. IsTransparent_t isTransparent() const { return IsTransparent_t(Transparent); } void setTransparent(IsTransparent_t isT) { Transparent = isT; } + + /// Retrieve the generic parameter list containing the contextual archetypes + /// of the function. + /// + /// FIXME: We should remove this in favor of lazy archetype instantiation + /// using the 'getArchetype' and 'mapTypeIntoContext' interfaces. + GenericParamList *getContextGenericParams() const { + return ContextGenericParams; + } + void setContextGenericParams(GenericParamList *params) { + ContextGenericParams = params; + } + + /// Map the given type, which is based on an interface SILFunctionType and may + /// therefore be dependent, to a type based on the context archetypes of this + /// SILFunction. + Type mapTypeIntoContext(Type type) const; + + /// Map the given type, which is based on an interface SILFunctionType and may + /// therefore be dependent, to a type based on the context archetypes of this + /// SILFunction. + SILType mapTypeIntoContext(SILType type) const { + return SILType::getPrimitiveType( + mapTypeIntoContext(type.getSwiftRValueType())->getCanonicalType(), + type.getCategory()); + } //===--------------------------------------------------------------------===// // Block List Access diff --git a/lib/SIL/SILFunction.cpp b/lib/SIL/SILFunction.cpp index 48f64cfe1e5..e593c029471 100644 --- a/lib/SIL/SILFunction.cpp +++ b/lib/SIL/SILFunction.cpp @@ -12,6 +12,8 @@ #include "swift/SIL/SILFunction.h" #include "swift/SIL/SILModule.h" +// FIXME: For mapTypeInContext +#include "swift/AST/ArchetypeBuilder.h" using namespace swift; @@ -26,6 +28,8 @@ SILFunction::SILFunction(SILModule &Module, SILLinkage Linkage, : ModuleAndLinkage(&Module, Linkage), Name(Name), LoweredType(LoweredType), + // FIXME: Context params should be independent of the function type. + ContextGenericParams(LoweredType->getGenericParams()), Location(Loc), DeclCtx(DC), DebugScope(DebugScope), @@ -65,3 +69,11 @@ void SILFunction::setDeclContext(Expr *E) { ASTContext &SILFunction::getASTContext() const { return getModule().getASTContext(); } + +Type SILFunction::mapTypeIntoContext(Type type) const { + if (!type->isDependentType()) + return type; + + return ArchetypeBuilder::mapTypeIntoContext(getContextGenericParams(), + type); +}