43
43
#include " swift/Basic/StringExtras.h"
44
44
#include " swift/ClangImporter/ClangImporter.h"
45
45
#include " swift/Strings.h"
46
+ #include " swift/Subsystems.h"
46
47
#include " llvm/ADT/APInt.h"
47
48
#include " llvm/ADT/SmallPtrSet.h"
48
49
#include " llvm/ADT/SmallString.h"
@@ -670,6 +671,7 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
670
671
// /
671
672
// / \param type The generic type to which to apply arguments.
672
673
// / \param resolution The type resolution to perform.
674
+ // / \param silParams Used to look up generic parameters in SIL mode.
673
675
// / \param comp The arguments to apply with the angle bracket range for
674
676
// / diagnostics.
675
677
// /
@@ -678,6 +680,7 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
678
680
// /
679
681
// / \see applyUnboundGenericArguments
680
682
static Type applyGenericArguments (Type type, TypeResolution resolution,
683
+ GenericParamList *silParams,
681
684
ComponentIdentTypeRepr *comp) {
682
685
const auto options = resolution.getOptions ();
683
686
auto dc = resolution.getDeclContext ();
@@ -775,7 +778,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
775
778
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
776
779
if (nominal->isOptionalDecl ()) {
777
780
// Validate the generic argument.
778
- Type objectType = resolution.resolveType (genericArgs[0 ]);
781
+ Type objectType = resolution.resolveType (genericArgs[0 ], silParams );
779
782
if (objectType->hasError ()) {
780
783
return ErrorType::get (ctx);
781
784
}
@@ -800,7 +803,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
800
803
SmallVector<Type, 2 > args;
801
804
for (auto tyR : genericArgs) {
802
805
// Propagate failure.
803
- Type substTy = genericResolution.resolveType (tyR);
806
+ Type substTy = genericResolution.resolveType (tyR, silParams );
804
807
if (!substTy || substTy->hasError ())
805
808
return ErrorType::get (ctx);
806
809
@@ -1010,6 +1013,7 @@ static void maybeDiagnoseBadConformanceRef(DeclContext *dc,
1010
1013
// / Returns a valid type or ErrorType in case of an error.
1011
1014
static Type resolveTypeDecl (TypeDecl *typeDecl, DeclContext *foundDC,
1012
1015
TypeResolution resolution,
1016
+ GenericParamList *silParams,
1013
1017
ComponentIdentTypeRepr *comp) {
1014
1018
// Resolve the type declaration to a specific type. How this occurs
1015
1019
// depends on the current context and where the type was found.
@@ -1025,7 +1029,7 @@ static Type resolveTypeDecl(TypeDecl *typeDecl, DeclContext *foundDC,
1025
1029
typeDecl);
1026
1030
}
1027
1031
1028
- return applyGenericArguments (type, resolution, comp);
1032
+ return applyGenericArguments (type, resolution, silParams, comp);
1029
1033
}
1030
1034
1031
1035
static std::string getDeclNameFromContext (DeclContext *dc,
@@ -1287,6 +1291,7 @@ static SelfTypeKind getSelfTypeKind(DeclContext *dc,
1287
1291
// / \returns Either the resolved type or a null type, the latter of
1288
1292
// / which indicates that some dependencies were unsatisfied.
1289
1293
static Type resolveTopLevelIdentTypeComponent (TypeResolution resolution,
1294
+ GenericParamList *silParams,
1290
1295
ComponentIdentTypeRepr *comp) {
1291
1296
const auto options = resolution.getOptions ();
1292
1297
ASTContext &ctx = resolution.getASTContext ();
@@ -1299,14 +1304,27 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
1299
1304
// that now.
1300
1305
if (auto *typeDecl = comp->getBoundDecl ()) {
1301
1306
// 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);
1303
1309
}
1304
1310
1305
1311
// Resolve the first component, which is the only one that requires
1306
1312
// unqualified name lookup.
1307
1313
auto DC = resolution.getDeclContext ();
1308
1314
auto id = comp->getNameRef ();
1309
1315
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
+
1310
1328
NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
1311
1329
if (options.contains (TypeResolutionFlags::KnownNonCascadingDependency))
1312
1330
lookupOptions |= NameLookupFlags::KnownPrivate;
@@ -1322,7 +1340,7 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
1322
1340
auto *foundDC = entry.getDeclContext ();
1323
1341
auto *typeDecl = cast<TypeDecl>(entry.getValueDecl ());
1324
1342
1325
- Type type = resolveTypeDecl (typeDecl, foundDC, resolution, comp);
1343
+ Type type = resolveTypeDecl (typeDecl, foundDC, resolution, silParams, comp);
1326
1344
if (type->is <ErrorType>())
1327
1345
return type;
1328
1346
@@ -1417,7 +1435,9 @@ static void diagnoseAmbiguousMemberType(Type baseTy, SourceRange baseRange,
1417
1435
// / Resolve the given identifier type representation as a qualified
1418
1436
// / lookup within the given parent type, returning the type it
1419
1437
// / references.
1438
+ // / \param silParams Used to look up generic parameters in SIL mode.
1420
1439
static Type resolveNestedIdentTypeComponent (TypeResolution resolution,
1440
+ GenericParamList *silParams,
1421
1441
Type parentTy,
1422
1442
SourceRange parentRange,
1423
1443
ComponentIdentTypeRepr *comp) {
@@ -1473,7 +1493,8 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
1473
1493
}
1474
1494
1475
1495
// If there are generic arguments, apply them now.
1476
- return applyGenericArguments (memberType, resolution, comp);
1496
+ return applyGenericArguments (memberType, resolution,
1497
+ silParams, comp);
1477
1498
};
1478
1499
1479
1500
// Short-circuiting.
@@ -1490,7 +1511,7 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
1490
1511
// type later on.
1491
1512
if (!memberType->is <DependentMemberType>() ||
1492
1513
memberType->castTo <DependentMemberType>()->getAssocType ()) {
1493
- return applyGenericArguments (memberType, resolution, comp);
1514
+ return applyGenericArguments (memberType, resolution, silParams, comp);
1494
1515
}
1495
1516
1496
1517
return memberType;
@@ -1558,28 +1579,33 @@ static Type resolveNestedIdentTypeComponent(TypeResolution resolution,
1558
1579
return maybeDiagnoseBadMemberType (member, memberType, inferredAssocType);
1559
1580
}
1560
1581
1582
+ // / \param silParams Used to look up generic parameters in SIL mode.
1561
1583
static Type
1562
1584
resolveIdentTypeComponent (TypeResolution resolution,
1585
+ GenericParamList *silParams,
1563
1586
ArrayRef<ComponentIdentTypeRepr *> components) {
1564
1587
auto comp = components.back ();
1565
1588
1566
1589
// The first component uses unqualified lookup.
1567
1590
const auto parentComps = components.drop_back ();
1568
1591
if (parentComps.empty ()) {
1569
- return resolveTopLevelIdentTypeComponent (resolution, comp);
1592
+ return resolveTopLevelIdentTypeComponent (resolution, silParams,
1593
+ comp);
1570
1594
}
1571
1595
1572
1596
// All remaining components use qualified lookup.
1573
1597
1574
1598
// Resolve the parent type.
1575
- Type parentTy = resolveIdentTypeComponent (resolution, parentComps);
1599
+ Type parentTy = resolveIdentTypeComponent (resolution, silParams,
1600
+ parentComps);
1576
1601
if (!parentTy || parentTy->hasError ()) return parentTy;
1577
1602
1578
1603
SourceRange parentRange (parentComps.front ()->getStartLoc (),
1579
1604
parentComps.back ()->getEndLoc ());
1580
1605
1581
1606
// Resolve the nested type.
1582
- return resolveNestedIdentTypeComponent (resolution, parentTy, parentRange,
1607
+ return resolveNestedIdentTypeComponent (resolution, silParams,
1608
+ parentTy, parentRange,
1583
1609
comp);
1584
1610
}
1585
1611
@@ -1720,9 +1746,13 @@ namespace {
1720
1746
class TypeResolver {
1721
1747
const TypeResolution &resolution;
1722
1748
1749
+ // / Used in SIL mode.
1750
+ GenericParamList *genericParams;
1751
+
1723
1752
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) {}
1726
1756
1727
1757
NeverNullType resolveType (TypeRepr *repr, TypeResolutionOptions options);
1728
1758
@@ -1828,22 +1858,26 @@ namespace {
1828
1858
};
1829
1859
} // end anonymous namespace
1830
1860
1831
- Type TypeResolution::resolveType (TypeRepr *TyR) const {
1861
+ Type TypeResolution::resolveType (TypeRepr *TyR,
1862
+ GenericParamList *silParams) const {
1832
1863
auto &ctx = getASTContext ();
1833
1864
auto Ty =
1834
- evaluateOrDefault (ctx.evaluator , ResolveTypeRequest{this , TyR}, Type ());
1865
+ evaluateOrDefault (ctx.evaluator ,
1866
+ ResolveTypeRequest{this , TyR, silParams}, Type ());
1835
1867
if (!Ty)
1836
1868
return ErrorType::get (ctx);
1837
1869
return Ty;
1838
1870
}
1839
1871
1840
1872
Type ResolveTypeRequest::evaluate (Evaluator &evaluator,
1841
1873
const TypeResolution *resolution,
1842
- TypeRepr *TyR) const {
1874
+ TypeRepr *TyR,
1875
+ GenericParamList *silParams) const {
1843
1876
const auto options = resolution->getOptions ();
1844
1877
auto &ctx = resolution->getASTContext ();
1845
1878
auto result =
1846
- TypeResolver (*resolution).resolveType (TyR, resolution->getOptions ());
1879
+ TypeResolver (*resolution, silParams)
1880
+ .resolveType (TyR, resolution->getOptions ());
1847
1881
1848
1882
// If we resolved down to an error, make sure to mark the typeRepr as invalid
1849
1883
// so we don't produce a redundant diagnostic.
@@ -2677,6 +2711,23 @@ Type TypeResolver::resolveASTFunctionType(
2677
2711
const clang::Type *parsedClangFunctionType,
2678
2712
DifferentiabilityKind diffKind) {
2679
2713
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
+
2680
2731
TypeResolutionOptions options = None;
2681
2732
options |= parentOptions.withoutContext ().getFlags ();
2682
2733
auto params = resolveASTFunctionTypeParams (
@@ -2724,16 +2775,6 @@ Type TypeResolver::resolveASTFunctionType(
2724
2775
.withClangFunctionType (clangFnType)
2725
2776
.build ();
2726
2777
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
-
2737
2778
// SIL uses polymorphic function types to resolve overloaded member functions.
2738
2779
if (auto genericEnv = repr->getGenericEnvironment ()) {
2739
2780
outputTy = outputTy->mapTypeOutOfContext ();
@@ -2809,12 +2850,18 @@ Type TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
2809
2850
// has one. (TODO: Field types should never refer to generic parameters
2810
2851
// outside the box's own environment; we should really validate that...)
2811
2852
TypeResolution fieldResolution{resolution};
2812
- if (auto env = repr->getGenericEnvironment ()) {
2853
+
2854
+ auto *genericEnv = repr->getGenericEnvironment ();
2855
+ auto *genericParams = repr->getGenericParams ();
2856
+
2857
+ if (genericParams) {
2813
2858
fieldResolution = TypeResolution::forContextual (
2814
- getDeclContext (), env, options, resolution.getUnboundTypeOpener ());
2859
+ getDeclContext (), genericEnv, options,
2860
+ resolution.getUnboundTypeOpener ());
2815
2861
}
2816
2862
2817
- TypeResolver fieldResolver{fieldResolution};
2863
+ TypeResolver fieldResolver{fieldResolution,
2864
+ genericParams};
2818
2865
for (auto &fieldRepr : repr->getFields ()) {
2819
2866
auto fieldTy = fieldResolver.resolveType (fieldRepr.getFieldType (), options);
2820
2867
fields.push_back ({fieldTy->getCanonicalType (), fieldRepr.isMutable ()});
@@ -2878,6 +2925,13 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2878
2925
2879
2926
// Resolve generic params in the pattern environment, if present, or
2880
2927
// 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
+
2881
2935
GenericEnvironment *genericEnv = repr->getGenericEnvironment ();
2882
2936
GenericEnvironment *componentTypeEnv =
2883
2937
repr->getPatternGenericEnvironment ()
@@ -2903,7 +2957,7 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2903
2957
diagnose (element.UnderscoreLoc , diag::sil_function_input_label);
2904
2958
}
2905
2959
2906
- TypeResolver silResolver{functionResolution};
2960
+ TypeResolver silResolver{functionResolution, componentGenericParams };
2907
2961
for (auto elt : argsTuple->getElements ()) {
2908
2962
auto elementOptions = options;
2909
2963
elementOptions.setContext (TypeResolverContext::FunctionInput);
@@ -2915,7 +2969,6 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2915
2969
}
2916
2970
2917
2971
{
2918
- // FIXME: Deal with unsatisfied dependencies.
2919
2972
if (silResolver.resolveSILResults (repr->getResultTypeRepr (),
2920
2973
options, yields,
2921
2974
results, errorResult)) {
@@ -2957,11 +3010,13 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2957
3010
resolution.getUnboundTypeOpener ());
2958
3011
patternSubs = resolveSubstitutions (repr->getPatternGenericEnvironment (),
2959
3012
repr->getPatternSubstitutions (),
2960
- TypeResolver{resolveSILParameters});
3013
+ TypeResolver{resolveSILParameters,
3014
+ genericParams});
2961
3015
} else {
2962
3016
patternSubs = resolveSubstitutions (repr->getPatternGenericEnvironment (),
2963
3017
repr->getPatternSubstitutions (),
2964
- TypeResolver{resolution});
3018
+ TypeResolver{resolution,
3019
+ genericParams});
2965
3020
}
2966
3021
}
2967
3022
@@ -2970,7 +3025,8 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2970
3025
if (!repr->getInvocationSubstitutions ().empty ()) {
2971
3026
invocationSubs = resolveSubstitutions (repr->getGenericEnvironment (),
2972
3027
repr->getInvocationSubstitutions (),
2973
- TypeResolver{resolution});
3028
+ TypeResolver{resolution,
3029
+ genericParams});
2974
3030
}
2975
3031
2976
3032
if (hasError) {
@@ -3261,7 +3317,7 @@ Type TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
3261
3317
auto Components = llvm::makeArrayRef (ComponentRange.begin (),
3262
3318
ComponentRange.end ());
3263
3319
Type result = resolveIdentTypeComponent (resolution.withOptions (options),
3264
- Components);
3320
+ genericParams, Components);
3265
3321
if (!result || result->hasError ()) {
3266
3322
return ErrorType::get (getASTContext ());
3267
3323
}
0 commit comments