Skip to content

Commit 43e37b7

Browse files
committed
AST: Move GenericParamListRequest::evaluate() to NameLookup.cpp
1 parent 14dd4ee commit 43e37b7

File tree

2 files changed

+109
-108
lines changed

2 files changed

+109
-108
lines changed

lib/AST/Decl.cpp

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,114 +1351,6 @@ Type ExtensionDecl::getExtendedType() const {
13511351
return ErrorType::get(ctx);
13521352
}
13531353

1354-
/// Clone the given generic parameters in the given list. We don't need any
1355-
/// of the requirements, because they will be inferred.
1356-
static GenericParamList *cloneGenericParams(ASTContext &ctx,
1357-
ExtensionDecl *ext,
1358-
GenericParamList *fromParams) {
1359-
// Clone generic parameters.
1360-
SmallVector<GenericTypeParamDecl *, 2> toGenericParams;
1361-
for (auto fromGP : *fromParams) {
1362-
// Create the new generic parameter.
1363-
auto toGP = new (ctx) GenericTypeParamDecl(ext, fromGP->getName(),
1364-
SourceLoc(),
1365-
fromGP->getDepth(),
1366-
fromGP->getIndex());
1367-
toGP->setImplicit(true);
1368-
1369-
// Record new generic parameter.
1370-
toGenericParams.push_back(toGP);
1371-
}
1372-
1373-
return GenericParamList::create(ctx, SourceLoc(), toGenericParams,
1374-
SourceLoc());
1375-
}
1376-
1377-
static GenericParamList *
1378-
createExtensionGenericParams(ASTContext &ctx,
1379-
ExtensionDecl *ext,
1380-
NominalTypeDecl *nominal) {
1381-
// Collect generic parameters from all outer contexts.
1382-
SmallVector<GenericParamList *, 2> allGenericParams;
1383-
nominal->forEachGenericContext([&](GenericParamList *gpList) {
1384-
allGenericParams.push_back(
1385-
cloneGenericParams(ctx, ext, gpList));
1386-
});
1387-
1388-
GenericParamList *toParams = nullptr;
1389-
for (auto *gpList : llvm::reverse(allGenericParams)) {
1390-
gpList->setOuterParameters(toParams);
1391-
toParams = gpList;
1392-
}
1393-
1394-
return toParams;
1395-
}
1396-
1397-
GenericParamList *
1398-
GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) const {
1399-
if (auto *ext = dyn_cast<ExtensionDecl>(value)) {
1400-
// Create the generic parameter list for the extension by cloning the
1401-
// generic parameter lists of the nominal and any of its parent types.
1402-
auto &ctx = value->getASTContext();
1403-
auto *nominal = ext->getExtendedNominal();
1404-
if (!nominal) {
1405-
return nullptr;
1406-
}
1407-
auto *genericParams = createExtensionGenericParams(ctx, ext, nominal);
1408-
1409-
// Protocol extensions need an inheritance clause due to how name lookup
1410-
// is implemented.
1411-
if (auto *proto = ext->getExtendedProtocolDecl()) {
1412-
auto protoType = proto->getDeclaredType();
1413-
TypeLoc selfInherited[1] = { TypeLoc::withoutLoc(protoType) };
1414-
genericParams->getParams().front()->setInherited(
1415-
ctx.AllocateCopy(selfInherited));
1416-
}
1417-
1418-
// Set the depth of every generic parameter.
1419-
unsigned depth = nominal->getGenericContextDepth();
1420-
for (auto *outerParams = genericParams;
1421-
outerParams != nullptr;
1422-
outerParams = outerParams->getOuterParameters())
1423-
outerParams->setDepth(depth--);
1424-
1425-
// If we have a trailing where clause, deal with it now.
1426-
// For now, trailing where clauses are only permitted on protocol extensions.
1427-
if (auto trailingWhereClause = ext->getTrailingWhereClause()) {
1428-
if (genericParams) {
1429-
// Merge the trailing where clause into the generic parameter list.
1430-
// FIXME: Long-term, we'd like clients to deal with the trailing where
1431-
// clause explicitly, but for now it's far more direct to represent
1432-
// the trailing where clause as part of the requirements.
1433-
genericParams->addTrailingWhereClause(
1434-
ext->getASTContext(),
1435-
trailingWhereClause->getWhereLoc(),
1436-
trailingWhereClause->getRequirements());
1437-
}
1438-
1439-
// If there's no generic parameter list, the where clause is diagnosed
1440-
// in typeCheckDecl().
1441-
}
1442-
return genericParams;
1443-
} else if (auto *proto = dyn_cast<ProtocolDecl>(value)) {
1444-
// The generic parameter 'Self'.
1445-
auto &ctx = value->getASTContext();
1446-
auto selfId = ctx.Id_Self;
1447-
auto selfDecl = new (ctx) GenericTypeParamDecl(
1448-
proto, selfId, SourceLoc(), /*depth=*/0, /*index=*/0);
1449-
auto protoType = proto->getDeclaredType();
1450-
TypeLoc selfInherited[1] = { TypeLoc::withoutLoc(protoType) };
1451-
selfDecl->setInherited(ctx.AllocateCopy(selfInherited));
1452-
selfDecl->setImplicit();
1453-
1454-
// The generic parameter list itself.
1455-
auto result = GenericParamList::create(ctx, SourceLoc(), selfDecl,
1456-
SourceLoc());
1457-
return result;
1458-
}
1459-
return nullptr;
1460-
}
1461-
14621354
PatternBindingDecl::PatternBindingDecl(SourceLoc StaticLoc,
14631355
StaticSpellingKind StaticSpelling,
14641356
SourceLoc VarLoc,

lib/AST/NameLookup.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,115 @@ static bool declsAreAssociatedTypes(ArrayRef<TypeDecl *> decls) {
22862286
return true;
22872287
}
22882288

2289+
2290+
/// Clone the given generic parameters in the given list. We don't need any
2291+
/// of the requirements, because they will be inferred.
2292+
static GenericParamList *cloneGenericParams(ASTContext &ctx,
2293+
ExtensionDecl *ext,
2294+
GenericParamList *fromParams) {
2295+
// Clone generic parameters.
2296+
SmallVector<GenericTypeParamDecl *, 2> toGenericParams;
2297+
for (auto fromGP : *fromParams) {
2298+
// Create the new generic parameter.
2299+
auto toGP = new (ctx) GenericTypeParamDecl(ext, fromGP->getName(),
2300+
SourceLoc(),
2301+
fromGP->getDepth(),
2302+
fromGP->getIndex());
2303+
toGP->setImplicit(true);
2304+
2305+
// Record new generic parameter.
2306+
toGenericParams.push_back(toGP);
2307+
}
2308+
2309+
return GenericParamList::create(ctx, SourceLoc(), toGenericParams,
2310+
SourceLoc());
2311+
}
2312+
2313+
static GenericParamList *
2314+
createExtensionGenericParams(ASTContext &ctx,
2315+
ExtensionDecl *ext,
2316+
NominalTypeDecl *nominal) {
2317+
// Collect generic parameters from all outer contexts.
2318+
SmallVector<GenericParamList *, 2> allGenericParams;
2319+
nominal->forEachGenericContext([&](GenericParamList *gpList) {
2320+
allGenericParams.push_back(
2321+
cloneGenericParams(ctx, ext, gpList));
2322+
});
2323+
2324+
GenericParamList *toParams = nullptr;
2325+
for (auto *gpList : llvm::reverse(allGenericParams)) {
2326+
gpList->setOuterParameters(toParams);
2327+
toParams = gpList;
2328+
}
2329+
2330+
return toParams;
2331+
}
2332+
2333+
GenericParamList *
2334+
GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) const {
2335+
if (auto *ext = dyn_cast<ExtensionDecl>(value)) {
2336+
// Create the generic parameter list for the extension by cloning the
2337+
// generic parameter lists of the nominal and any of its parent types.
2338+
auto &ctx = value->getASTContext();
2339+
auto *nominal = ext->getExtendedNominal();
2340+
if (!nominal) {
2341+
return nullptr;
2342+
}
2343+
auto *genericParams = createExtensionGenericParams(ctx, ext, nominal);
2344+
2345+
// Protocol extensions need an inheritance clause due to how name lookup
2346+
// is implemented.
2347+
if (auto *proto = ext->getExtendedProtocolDecl()) {
2348+
auto protoType = proto->getDeclaredType();
2349+
TypeLoc selfInherited[1] = { TypeLoc::withoutLoc(protoType) };
2350+
genericParams->getParams().front()->setInherited(
2351+
ctx.AllocateCopy(selfInherited));
2352+
}
2353+
2354+
// Set the depth of every generic parameter.
2355+
unsigned depth = nominal->getGenericContextDepth();
2356+
for (auto *outerParams = genericParams;
2357+
outerParams != nullptr;
2358+
outerParams = outerParams->getOuterParameters())
2359+
outerParams->setDepth(depth--);
2360+
2361+
// If we have a trailing where clause, deal with it now.
2362+
// For now, trailing where clauses are only permitted on protocol extensions.
2363+
if (auto trailingWhereClause = ext->getTrailingWhereClause()) {
2364+
if (genericParams) {
2365+
// Merge the trailing where clause into the generic parameter list.
2366+
// FIXME: Long-term, we'd like clients to deal with the trailing where
2367+
// clause explicitly, but for now it's far more direct to represent
2368+
// the trailing where clause as part of the requirements.
2369+
genericParams->addTrailingWhereClause(
2370+
ext->getASTContext(),
2371+
trailingWhereClause->getWhereLoc(),
2372+
trailingWhereClause->getRequirements());
2373+
}
2374+
2375+
// If there's no generic parameter list, the where clause is diagnosed
2376+
// in typeCheckDecl().
2377+
}
2378+
return genericParams;
2379+
} else if (auto *proto = dyn_cast<ProtocolDecl>(value)) {
2380+
// The generic parameter 'Self'.
2381+
auto &ctx = value->getASTContext();
2382+
auto selfId = ctx.Id_Self;
2383+
auto selfDecl = new (ctx) GenericTypeParamDecl(
2384+
proto, selfId, SourceLoc(), /*depth=*/0, /*index=*/0);
2385+
auto protoType = proto->getDeclaredType();
2386+
TypeLoc selfInherited[1] = { TypeLoc::withoutLoc(protoType) };
2387+
selfDecl->setInherited(ctx.AllocateCopy(selfInherited));
2388+
selfDecl->setImplicit();
2389+
2390+
// The generic parameter list itself.
2391+
auto result = GenericParamList::create(ctx, SourceLoc(), selfDecl,
2392+
SourceLoc());
2393+
return result;
2394+
}
2395+
return nullptr;
2396+
}
2397+
22892398
NominalTypeDecl *
22902399
CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
22912400
CustomAttr *attr, DeclContext *dc) const {

0 commit comments

Comments
 (0)