4343#include " swift/Basic/StringExtras.h"
4444#include " swift/ClangImporter/ClangImporter.h"
4545#include " swift/Strings.h"
46+ #include " swift/Subsystems.h"
4647#include " llvm/ADT/APInt.h"
4748#include " llvm/ADT/SmallPtrSet.h"
4849#include " llvm/ADT/SmallString.h"
@@ -670,6 +671,7 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
670671// /
671672// / \param type The generic type to which to apply arguments.
672673// / \param resolution The type resolution to perform.
674+ // / \param silParams Used to look up generic parameters in SIL mode.
673675// / \param comp The arguments to apply with the angle bracket range for
674676// / diagnostics.
675677// /
@@ -678,6 +680,7 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
678680// /
679681// / \see applyUnboundGenericArguments
680682static Type applyGenericArguments (Type type, TypeResolution resolution,
683+ GenericParamList *silParams,
681684 ComponentIdentTypeRepr *comp) {
682685 const auto options = resolution.getOptions ();
683686 auto dc = resolution.getDeclContext ();
@@ -775,7 +778,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
775778 if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
776779 if (nominal->isOptionalDecl ()) {
777780 // Validate the generic argument.
778- Type objectType = resolution.resolveType (genericArgs[0 ]);
781+ Type objectType = resolution.resolveType (genericArgs[0 ], silParams );
779782 if (objectType->hasError ()) {
780783 return ErrorType::get (ctx);
781784 }
@@ -800,7 +803,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
800803 SmallVector<Type, 2 > args;
801804 for (auto tyR : genericArgs) {
802805 // Propagate failure.
803- Type substTy = genericResolution.resolveType (tyR);
806+ Type substTy = genericResolution.resolveType (tyR, silParams );
804807 if (!substTy || substTy->hasError ())
805808 return ErrorType::get (ctx);
806809
@@ -1010,6 +1013,7 @@ static void maybeDiagnoseBadConformanceRef(DeclContext *dc,
10101013// / Returns a valid type or ErrorType in case of an error.
10111014static Type resolveTypeDecl (TypeDecl *typeDecl, DeclContext *foundDC,
10121015 TypeResolution resolution,
1016+ GenericParamList *silParams,
10131017 ComponentIdentTypeRepr *comp) {
10141018 // Resolve the type declaration to a specific type. How this occurs
10151019 // depends on the current context and where the type was found.
@@ -1025,7 +1029,7 @@ static Type resolveTypeDecl(TypeDecl *typeDecl, DeclContext *foundDC,
10251029 typeDecl);
10261030 }
10271031
1028- return applyGenericArguments (type, resolution, comp);
1032+ return applyGenericArguments (type, resolution, silParams, comp);
10291033}
10301034
10311035static std::string getDeclNameFromContext (DeclContext *dc,
@@ -1287,6 +1291,7 @@ static SelfTypeKind getSelfTypeKind(DeclContext *dc,
12871291// / \returns Either the resolved type or a null type, the latter of
12881292// / which indicates that some dependencies were unsatisfied.
12891293static Type resolveTopLevelIdentTypeComponent (TypeResolution resolution,
1294+ GenericParamList *silParams,
12901295 ComponentIdentTypeRepr *comp) {
12911296 const auto options = resolution.getOptions ();
12921297 ASTContext &ctx = resolution.getASTContext ();
@@ -1299,14 +1304,27 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12991304 // that now.
13001305 if (auto *typeDecl = comp->getBoundDecl ()) {
13011306 // Resolve the type declaration within this context.
1302- return resolveTypeDecl (typeDecl, comp->getDeclContext (), resolution, comp);
1307+ return resolveTypeDecl (typeDecl, comp->getDeclContext (), resolution,
1308+ silParams, comp);
13031309 }
13041310
13051311 // Resolve the first component, which is the only one that requires
13061312 // unqualified name lookup.
13071313 auto DC = resolution.getDeclContext ();
13081314 auto id = comp->getNameRef ();
13091315
1316+ // In SIL mode, we bind generic parameters here, since name lookup
1317+ // won't find them.
1318+ if (silParams != nullptr ) {
1319+ auto name = id.getBaseIdentifier ();
1320+ if (auto *paramDecl = silParams->lookUpGenericParam (name)) {
1321+ comp->setValue (paramDecl, DC);
1322+
1323+ return resolveTypeDecl (paramDecl, DC, resolution,
1324+ silParams, comp);
1325+ }
1326+ }
1327+
13101328 NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
13111329 if (options.contains (TypeResolutionFlags::KnownNonCascadingDependency))
13121330 lookupOptions |= NameLookupFlags::KnownPrivate;
@@ -1322,7 +1340,7 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
13221340 auto *foundDC = entry.getDeclContext ();
13231341 auto *typeDecl = cast<TypeDecl>(entry.getValueDecl ());
13241342
1325- Type type = resolveTypeDecl (typeDecl, foundDC, resolution, comp);
1343+ Type type = resolveTypeDecl (typeDecl, foundDC, resolution, silParams, comp);
13261344 if (type->is <ErrorType>())
13271345 return type;
13281346
@@ -1417,7 +1435,9 @@ static void diagnoseAmbiguousMemberType(Type baseTy, SourceRange baseRange,
14171435// / Resolve the given identifier type representation as a qualified
14181436// / lookup within the given parent type, returning the type it
14191437// / references.
1438+ // / \param silParams Used to look up generic parameters in SIL mode.
14201439static Type resolveNestedIdentTypeComponent (TypeResolution resolution,
1440+ GenericParamList *silParams,
14211441 Type parentTy,
14221442 SourceRange parentRange,
14231443 ComponentIdentTypeRepr *comp) {
@@ -1473,7 +1493,8 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
14731493 }
14741494
14751495 // If there are generic arguments, apply them now.
1476- return applyGenericArguments (memberType, resolution, comp);
1496+ return applyGenericArguments (memberType, resolution,
1497+ silParams, comp);
14771498 };
14781499
14791500 // Short-circuiting.
@@ -1490,7 +1511,7 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
14901511 // type later on.
14911512 if (!memberType->is <DependentMemberType>() ||
14921513 memberType->castTo <DependentMemberType>()->getAssocType ()) {
1493- return applyGenericArguments (memberType, resolution, comp);
1514+ return applyGenericArguments (memberType, resolution, silParams, comp);
14941515 }
14951516
14961517 return memberType;
@@ -1558,28 +1579,33 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
15581579 return maybeDiagnoseBadMemberType (member, memberType, inferredAssocType);
15591580}
15601581
1582+ // / \param silParams Used to look up generic parameters in SIL mode.
15611583static Type
15621584resolveIdentTypeComponent (TypeResolution resolution,
1585+ GenericParamList *silParams,
15631586 ArrayRef<ComponentIdentTypeRepr *> components) {
15641587 auto comp = components.back ();
15651588
15661589 // The first component uses unqualified lookup.
15671590 const auto parentComps = components.drop_back ();
15681591 if (parentComps.empty ()) {
1569- return resolveTopLevelIdentTypeComponent (resolution, comp);
1592+ return resolveTopLevelIdentTypeComponent (resolution, silParams,
1593+ comp);
15701594 }
15711595
15721596 // All remaining components use qualified lookup.
15731597
15741598 // Resolve the parent type.
1575- Type parentTy = resolveIdentTypeComponent (resolution, parentComps);
1599+ Type parentTy = resolveIdentTypeComponent (resolution, silParams,
1600+ parentComps);
15761601 if (!parentTy || parentTy->hasError ()) return parentTy;
15771602
15781603 SourceRange parentRange (parentComps.front ()->getStartLoc (),
15791604 parentComps.back ()->getEndLoc ());
15801605
15811606 // Resolve the nested type.
1582- return resolveNestedIdentTypeComponent (resolution, parentTy, parentRange,
1607+ return resolveNestedIdentTypeComponent (resolution, silParams,
1608+ parentTy, parentRange,
15831609 comp);
15841610}
15851611
@@ -1720,9 +1746,13 @@ namespace {
17201746 class TypeResolver {
17211747 const TypeResolution &resolution;
17221748
1749+ // / Used in SIL mode.
1750+ GenericParamList *genericParams;
1751+
17231752 public:
1724- explicit TypeResolver (const TypeResolution &resolution)
1725- : resolution(resolution) {}
1753+ explicit TypeResolver (const TypeResolution &resolution,
1754+ GenericParamList *genericParams = nullptr )
1755+ : resolution(resolution), genericParams(genericParams) {}
17261756
17271757 NeverNullType resolveType (TypeRepr *repr, TypeResolutionOptions options);
17281758
@@ -1828,22 +1858,26 @@ namespace {
18281858 };
18291859} // end anonymous namespace
18301860
1831- Type TypeResolution::resolveType (TypeRepr *TyR) const {
1861+ Type TypeResolution::resolveType (TypeRepr *TyR,
1862+ GenericParamList *silParams) const {
18321863 auto &ctx = getASTContext ();
18331864 auto Ty =
1834- evaluateOrDefault (ctx.evaluator , ResolveTypeRequest{this , TyR}, Type ());
1865+ evaluateOrDefault (ctx.evaluator ,
1866+ ResolveTypeRequest{this , TyR, silParams}, Type ());
18351867 if (!Ty)
18361868 return ErrorType::get (ctx);
18371869 return Ty;
18381870}
18391871
18401872Type ResolveTypeRequest::evaluate (Evaluator &evaluator,
18411873 const TypeResolution *resolution,
1842- TypeRepr *TyR) const {
1874+ TypeRepr *TyR,
1875+ GenericParamList *silParams) const {
18431876 const auto options = resolution->getOptions ();
18441877 auto &ctx = resolution->getASTContext ();
18451878 auto result =
1846- TypeResolver (*resolution).resolveType (TyR, resolution->getOptions ());
1879+ TypeResolver (*resolution, silParams)
1880+ .resolveType (TyR, resolution->getOptions ());
18471881
18481882 // If we resolved down to an error, make sure to mark the typeRepr as invalid
18491883 // so we don't produce a redundant diagnostic.
@@ -2677,6 +2711,23 @@ Type TypeResolver::resolveASTFunctionType(
26772711 const clang::Type *parsedClangFunctionType,
26782712 DifferentiabilityKind diffKind) {
26792713
2714+ Optional<llvm::SaveAndRestore<GenericParamList *>> saveGenericParams;
2715+
2716+ if (auto *genericParams = repr->getGenericParams ())
2717+ saveGenericParams.emplace (this ->genericParams , genericParams);
2718+
2719+ // Diagnose a couple of things that we can parse in SIL mode but we don't
2720+ // allow in formal types.
2721+ if (auto patternParams = repr->getPatternGenericParams ()) {
2722+ diagnose (patternParams->getLAngleLoc (),
2723+ diag::ast_subst_function_type);
2724+ return ErrorType::get (getASTContext ());
2725+ } else if (!repr->getInvocationSubstitutions ().empty ()) {
2726+ diagnose (repr->getInvocationSubstitutions ()[0 ]->getStartLoc (),
2727+ diag::ast_subst_function_type);
2728+ return ErrorType::get (getASTContext ());
2729+ }
2730+
26802731 TypeResolutionOptions options = None;
26812732 options |= parentOptions.withoutContext ().getFlags ();
26822733 auto params = resolveASTFunctionTypeParams (
@@ -2724,16 +2775,6 @@ Type TypeResolver::resolveASTFunctionType(
27242775 .withClangFunctionType (clangFnType)
27252776 .build ();
27262777
2727- // Diagnose a couple of things that we can parse in SIL mode but we don't
2728- // allow in formal types.
2729- if (auto patternParams = repr->getPatternGenericParams ()) {
2730- diagnose (patternParams->getLAngleLoc (),
2731- diag::ast_subst_function_type);
2732- } else if (!repr->getInvocationSubstitutions ().empty ()) {
2733- diagnose (repr->getInvocationSubstitutions ()[0 ]->getStartLoc (),
2734- diag::ast_subst_function_type);
2735- }
2736-
27372778 // SIL uses polymorphic function types to resolve overloaded member functions.
27382779 if (auto genericEnv = repr->getGenericEnvironment ()) {
27392780 outputTy = outputTy->mapTypeOutOfContext ();
@@ -2809,12 +2850,18 @@ Type TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
28092850 // has one. (TODO: Field types should never refer to generic parameters
28102851 // outside the box's own environment; we should really validate that...)
28112852 TypeResolution fieldResolution{resolution};
2812- if (auto env = repr->getGenericEnvironment ()) {
2853+
2854+ auto *genericEnv = repr->getGenericEnvironment ();
2855+ auto *genericParams = repr->getGenericParams ();
2856+
2857+ if (genericParams) {
28132858 fieldResolution = TypeResolution::forContextual (
2814- getDeclContext (), env, options, resolution.getUnboundTypeOpener ());
2859+ getDeclContext (), genericEnv, options,
2860+ resolution.getUnboundTypeOpener ());
28152861 }
28162862
2817- TypeResolver fieldResolver{fieldResolution};
2863+ TypeResolver fieldResolver{fieldResolution,
2864+ genericParams};
28182865 for (auto &fieldRepr : repr->getFields ()) {
28192866 auto fieldTy = fieldResolver.resolveType (fieldRepr.getFieldType (), options);
28202867 fields.push_back ({fieldTy->getCanonicalType (), fieldRepr.isMutable ()});
@@ -2878,6 +2925,13 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
28782925
28792926 // Resolve generic params in the pattern environment, if present, or
28802927 // else the function's generic environment, if it has one.
2928+ GenericParamList *genericParams = repr->getGenericParams ();
2929+ if (genericParams == nullptr )
2930+ genericParams = this ->genericParams ;
2931+ GenericParamList *componentGenericParams = repr->getPatternGenericParams ();
2932+ if (componentGenericParams == nullptr )
2933+ componentGenericParams = genericParams;
2934+
28812935 GenericEnvironment *genericEnv = repr->getGenericEnvironment ();
28822936 GenericEnvironment *componentTypeEnv =
28832937 repr->getPatternGenericEnvironment ()
@@ -2903,7 +2957,7 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
29032957 diagnose (element.UnderscoreLoc , diag::sil_function_input_label);
29042958 }
29052959
2906- TypeResolver silResolver{functionResolution};
2960+ TypeResolver silResolver{functionResolution, componentGenericParams };
29072961 for (auto elt : argsTuple->getElements ()) {
29082962 auto elementOptions = options;
29092963 elementOptions.setContext (TypeResolverContext::FunctionInput);
@@ -2915,7 +2969,6 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
29152969 }
29162970
29172971 {
2918- // FIXME: Deal with unsatisfied dependencies.
29192972 if (silResolver.resolveSILResults (repr->getResultTypeRepr (),
29202973 options, yields,
29212974 results, errorResult)) {
@@ -2957,11 +3010,13 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
29573010 resolution.getUnboundTypeOpener ());
29583011 patternSubs = resolveSubstitutions (repr->getPatternGenericEnvironment (),
29593012 repr->getPatternSubstitutions (),
2960- TypeResolver{resolveSILParameters});
3013+ TypeResolver{resolveSILParameters,
3014+ genericParams});
29613015 } else {
29623016 patternSubs = resolveSubstitutions (repr->getPatternGenericEnvironment (),
29633017 repr->getPatternSubstitutions (),
2964- TypeResolver{resolution});
3018+ TypeResolver{resolution,
3019+ genericParams});
29653020 }
29663021 }
29673022
@@ -2970,7 +3025,8 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
29703025 if (!repr->getInvocationSubstitutions ().empty ()) {
29713026 invocationSubs = resolveSubstitutions (repr->getGenericEnvironment (),
29723027 repr->getInvocationSubstitutions (),
2973- TypeResolver{resolution});
3028+ TypeResolver{resolution,
3029+ genericParams});
29743030 }
29753031
29763032 if (hasError) {
@@ -3261,7 +3317,7 @@ Type TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
32613317 auto Components = llvm::makeArrayRef (ComponentRange.begin (),
32623318 ComponentRange.end ());
32633319 Type result = resolveIdentTypeComponent (resolution.withOptions (options),
3264- Components);
3320+ genericParams, Components);
32653321 if (!result || result->hasError ()) {
32663322 return ErrorType::get (getASTContext ());
32673323 }
0 commit comments