Skip to content

Commit 8c3f154

Browse files
committed
[NFC] Wean SILParser off of TypeLocs
It was just using them as a currency type because performTypeLocChecking accepted them as a parameter. Cleaning up performTypeLocChecking is something that needs to be done independently of this changeset, so for now just encapsulate the use of TypeLocs as much as possible and use TypeRepr instead.
1 parent 552134a commit 8c3f154

File tree

1 file changed

+60
-64
lines changed

1 file changed

+60
-64
lines changed

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace {
174174
/// A callback to be invoked every time a type was deserialized.
175175
std::function<void(Type)> ParsedTypeCallback;
176176

177-
bool performTypeLocChecking(TypeLoc &T, bool IsSILType,
177+
Type performTypeLocChecking(TypeRepr *TyR, bool IsSILType,
178178
GenericEnvironment *GenericEnv = nullptr);
179179

180180
void convertRequirements(SILFunction *F, ArrayRef<RequirementRepr> From,
@@ -868,34 +868,32 @@ void SILParser::convertRequirements(SILFunction *F,
868868
IdentTypeReprLookup PerformLookup(P);
869869
// Use parser lexical scopes to resolve references
870870
// to the generic parameters.
871-
auto ResolveToInterfaceType = [&](TypeLoc Ty) -> Type {
872-
Ty.getTypeRepr()->walk(PerformLookup);
873-
performTypeLocChecking(Ty, /* IsSIL */ false);
874-
assert(Ty.getType());
875-
return Ty.getType()->mapTypeOutOfContext();
871+
auto ResolveToInterfaceType = [&](TypeRepr *Ty) -> Type {
872+
Ty->walk(PerformLookup);
873+
return performTypeLocChecking(Ty, /* IsSIL */ false)->mapTypeOutOfContext();
876874
};
877875

878876
for (auto &Req : From) {
879877
if (Req.getKind() == RequirementReprKind::SameType) {
880-
auto FirstType = ResolveToInterfaceType(Req.getFirstTypeLoc());
881-
auto SecondType = ResolveToInterfaceType(Req.getSecondTypeLoc());
878+
auto FirstType = ResolveToInterfaceType(Req.getFirstTypeRepr());
879+
auto SecondType = ResolveToInterfaceType(Req.getSecondTypeRepr());
882880
Requirement ConvertedRequirement(RequirementKind::SameType, FirstType,
883881
SecondType);
884882
To.push_back(ConvertedRequirement);
885883
continue;
886884
}
887885

888886
if (Req.getKind() == RequirementReprKind::TypeConstraint) {
889-
auto Subject = ResolveToInterfaceType(Req.getSubjectLoc());
890-
auto Constraint = ResolveToInterfaceType(Req.getConstraintLoc());
887+
auto Subject = ResolveToInterfaceType(Req.getSubjectRepr());
888+
auto Constraint = ResolveToInterfaceType(Req.getConstraintRepr());
891889
Requirement ConvertedRequirement(RequirementKind::Conformance, Subject,
892890
Constraint);
893891
To.push_back(ConvertedRequirement);
894892
continue;
895893
}
896894

897895
if (Req.getKind() == RequirementReprKind::LayoutConstraint) {
898-
auto Subject = ResolveToInterfaceType(Req.getSubjectLoc());
896+
auto Subject = ResolveToInterfaceType(Req.getSubjectRepr());
899897
Requirement ConvertedRequirement(RequirementKind::Layout, Subject,
900898
Req.getLayoutConstraint());
901899
To.push_back(ConvertedRequirement);
@@ -1094,14 +1092,16 @@ static bool parseDeclSILOptional(bool *isTransparent,
10941092
return false;
10951093
}
10961094

1097-
bool SILParser::performTypeLocChecking(TypeLoc &T, bool IsSILType,
1095+
Type SILParser::performTypeLocChecking(TypeRepr *T, bool IsSILType,
10981096
GenericEnvironment *GenericEnv) {
10991097
if (GenericEnv == nullptr)
11001098
GenericEnv = ContextGenericEnv;
11011099

1102-
return swift::performTypeLocChecking(P.Context, T,
1100+
TypeLoc loc(T);
1101+
(void) swift::performTypeLocChecking(P.Context, loc,
11031102
/*isSILMode=*/true, IsSILType,
11041103
GenericEnv, &P.SF);
1104+
return loc.getType();
11051105
}
11061106

11071107
/// Find the top-level ValueDecl or Module given a name.
@@ -1155,17 +1155,17 @@ static ValueDecl *lookupMember(Parser &P, Type Ty, DeclBaseName Name,
11551155
bool SILParser::parseASTType(CanType &result, GenericEnvironment *env) {
11561156
ParserResult<TypeRepr> parsedType = P.parseType();
11571157
if (parsedType.isNull()) return true;
1158-
TypeLoc loc = parsedType.get();
1159-
if (performTypeLocChecking(loc, /*IsSILType=*/ false, env))
1158+
auto resolvedType = performTypeLocChecking(parsedType.get(), /*IsSILType=*/ false, env);
1159+
if (resolvedType->hasError())
11601160
return true;
11611161

11621162
if (env)
1163-
result = loc.getType()->mapTypeOutOfContext()->getCanonicalType();
1163+
result = resolvedType->mapTypeOutOfContext()->getCanonicalType();
11641164
else
1165-
result = loc.getType()->getCanonicalType();
1165+
result = resolvedType->getCanonicalType();
11661166

11671167
// Invoke the callback on the parsed type.
1168-
ParsedTypeCallback(loc.getType());
1168+
ParsedTypeCallback(resolvedType);
11691169
return false;
11701170
}
11711171

@@ -1244,16 +1244,16 @@ bool SILParser::parseSILType(SILType &Result,
12441244
ParsedGenericEnv = env;
12451245

12461246
// Apply attributes to the type.
1247-
TypeLoc Ty = P.applyAttributeToType(TyR.get(), attrs, specifier, specifierLoc);
1248-
1249-
if (performTypeLocChecking(Ty, /*IsSILType=*/true, OuterGenericEnv))
1247+
auto *attrRepr = P.applyAttributeToType(TyR.get(), attrs, specifier, specifierLoc);
1248+
auto Ty = performTypeLocChecking(attrRepr, /*IsSILType=*/true, OuterGenericEnv);
1249+
if (Ty->hasError())
12501250
return true;
12511251

1252-
Result = SILType::getPrimitiveType(Ty.getType()->getCanonicalType(),
1252+
Result = SILType::getPrimitiveType(Ty->getCanonicalType(),
12531253
category);
12541254

12551255
// Invoke the callback on the parsed type.
1256-
ParsedTypeCallback(Ty.getType());
1256+
ParsedTypeCallback(Ty);
12571257

12581258
return false;
12591259
}
@@ -1644,34 +1644,34 @@ bool SILParser::parseSILBBArgsAtBranch(SmallVector<SILValue, 6> &Args,
16441644
///
16451645
/// FIXME: This is a hack to work around the lack of a DeclContext for
16461646
/// witness tables.
1647-
static void bindProtocolSelfInTypeRepr(TypeLoc &TL, ProtocolDecl *proto) {
1648-
if (auto typeRepr = TL.getTypeRepr()) {
1649-
// AST walker to update 'Self' references.
1650-
class BindProtocolSelf : public ASTWalker {
1651-
ProtocolDecl *proto;
1652-
GenericTypeParamDecl *selfParam;
1653-
Identifier selfId;
1654-
1655-
public:
1656-
BindProtocolSelf(ProtocolDecl *proto)
1657-
: proto(proto),
1658-
selfParam(proto->getProtocolSelfType()->getDecl()),
1659-
selfId(proto->getASTContext().Id_Self) {
1660-
}
1647+
static void bindProtocolSelfInTypeRepr(TypeRepr *typeRepr, ProtocolDecl *proto) {
1648+
assert(typeRepr);
16611649

1662-
virtual bool walkToTypeReprPre(TypeRepr *T) override {
1663-
if (auto ident = dyn_cast<IdentTypeRepr>(T)) {
1664-
auto firstComponent = ident->getComponentRange().front();
1665-
if (firstComponent->getNameRef().isSimpleName(selfId))
1666-
firstComponent->setValue(selfParam, proto);
1667-
}
1650+
// AST walker to update 'Self' references.
1651+
class BindProtocolSelf : public ASTWalker {
1652+
ProtocolDecl *proto;
1653+
GenericTypeParamDecl *selfParam;
1654+
Identifier selfId;
16681655

1669-
return true;
1656+
public:
1657+
BindProtocolSelf(ProtocolDecl *proto)
1658+
: proto(proto),
1659+
selfParam(proto->getProtocolSelfType()->getDecl()),
1660+
selfId(proto->getASTContext().Id_Self) {
1661+
}
1662+
1663+
virtual bool walkToTypeReprPre(TypeRepr *T) override {
1664+
if (auto ident = dyn_cast<IdentTypeRepr>(T)) {
1665+
auto firstComponent = ident->getComponentRange().front();
1666+
if (firstComponent->getNameRef().isSimpleName(selfId))
1667+
firstComponent->setValue(selfParam, proto);
16701668
}
1671-
};
16721669

1673-
typeRepr->walk(BindProtocolSelf(proto));
1674-
}
1670+
return true;
1671+
}
1672+
};
1673+
1674+
typeRepr->walk(BindProtocolSelf(proto));
16751675
}
16761676

16771677
/// Parse the substitution list for an apply instruction or
@@ -1693,12 +1693,13 @@ bool SILParser::parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
16931693
ParserResult<TypeRepr> TyR = P.parseType();
16941694
if (TyR.isNull())
16951695
return true;
1696-
TypeLoc Ty = TyR.get();
16971696
if (defaultForProto)
1698-
bindProtocolSelfInTypeRepr(Ty, defaultForProto);
1699-
if (performTypeLocChecking(Ty, /*IsSILType=*/ false, GenericEnv))
1697+
bindProtocolSelfInTypeRepr(TyR.get(), defaultForProto);
1698+
1699+
auto Ty = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, GenericEnv);
1700+
if (Ty->hasError())
17001701
return true;
1701-
parsed.push_back({Loc, Ty.getType()});
1702+
parsed.push_back({Loc, Ty});
17021703
} while (P.consumeIf(tok::comma));
17031704

17041705
// Consume the closing '>'.
@@ -2090,31 +2091,27 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired) {
20902091
GenericsScope.reset();
20912092
if (TyR.isNull())
20922093
return true;
2093-
TypeLoc Ty = TyR.get();
20942094

20952095
// The type can be polymorphic.
20962096
GenericEnvironment *genericEnv = nullptr;
20972097
if (auto fnType = dyn_cast<FunctionTypeRepr>(TyR.get())) {
20982098
if (auto generics = fnType->getGenericParams()) {
2099-
assert(!Ty.wasValidated() && Ty.getType().isNull());
2100-
21012099
genericEnv = handleSILGenericParams(generics, &P.SF);
21022100
fnType->setGenericEnvironment(genericEnv);
21032101
}
21042102
if (auto generics = fnType->getPatternGenericParams()) {
2105-
assert(!Ty.wasValidated() && Ty.getType().isNull());
2106-
21072103
genericEnv = handleSILGenericParams(generics, &P.SF);
21082104
fnType->setPatternGenericEnvironment(genericEnv);
21092105
}
21102106
}
21112107

2112-
if (performTypeLocChecking(Ty, /*IsSILType=*/ false, genericEnv))
2108+
auto Ty = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, genericEnv);
2109+
if (Ty->hasError())
21132110
return true;
21142111

21152112
// Pick the ValueDecl that has the right type.
21162113
ValueDecl *TheDecl = nullptr;
2117-
auto declTy = Ty.getType()->getCanonicalType();
2114+
auto declTy = Ty->getCanonicalType();
21182115
for (unsigned I = 0, E = values.size(); I < E; ++I) {
21192116
auto *decl = values[I];
21202117

@@ -6306,14 +6303,13 @@ ProtocolConformanceRef SILParser::parseProtocolConformanceHelper(
63066303
ParserResult<TypeRepr> TyR = P.parseType();
63076304
if (TyR.isNull())
63086305
return ProtocolConformanceRef();
6309-
TypeLoc Ty = TyR.get();
63106306
if (defaultForProto) {
6311-
bindProtocolSelfInTypeRepr(Ty, defaultForProto);
6307+
bindProtocolSelfInTypeRepr(TyR.get(), defaultForProto);
63126308
}
63136309

6314-
if (performTypeLocChecking(Ty, /*IsSILType=*/ false, witnessEnv))
6310+
auto ConformingTy = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, witnessEnv);
6311+
if (ConformingTy->hasError())
63156312
return ProtocolConformanceRef();
6316-
auto ConformingTy = Ty.getType();
63176313

63186314
if (P.parseToken(tok::colon, diag::expected_sil_witness_colon))
63196315
return ProtocolConformanceRef();
@@ -6429,7 +6425,7 @@ static bool parseSILVTableEntry(
64296425
return true;
64306426
TypeLoc Ty = TyR.get();
64316427
if (isDefaultWitnessTable)
6432-
bindProtocolSelfInTypeRepr(Ty, proto);
6428+
bindProtocolSelfInTypeRepr(TyR.get(), proto);
64336429
if (swift::performTypeLocChecking(P.Context, Ty,
64346430
/*isSILMode=*/false,
64356431
/*isSILType=*/false,
@@ -6490,7 +6486,7 @@ static bool parseSILVTableEntry(
64906486
return true;
64916487
TypeLoc Ty = TyR.get();
64926488
if (isDefaultWitnessTable)
6493-
bindProtocolSelfInTypeRepr(Ty, proto);
6489+
bindProtocolSelfInTypeRepr(TyR.get(), proto);
64946490
if (swift::performTypeLocChecking(P.Context, Ty,
64956491
/*isSILMode=*/false,
64966492
/*isSILType=*/false,

0 commit comments

Comments
 (0)