Skip to content

Commit 9516575

Browse files
committed
ASTDemangler: Simplify createBoundGenericType()
It's clever how it leverages the type checker to check generic arguments, but calling checkGenericArguments() would have been simpler, and it doesn't work for interface types anyway, so we would fail to demangle those. Let's just cut this all out for now and build a BoundGenericType directly.
1 parent bbe6a56 commit 9516575

File tree

3 files changed

+10
-118
lines changed

3 files changed

+10
-118
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "llvm/ADT/ArrayRef.h"
2626
#include "llvm/ADT/StringRef.h"
2727
#include "swift/AST/Types.h"
28-
#include "swift/AST/TypeRepr.h"
2928
#include "swift/Demangling/Demangler.h"
3029
#include "swift/Demangling/TypeDecoder.h"
3130

@@ -134,30 +133,8 @@ class ASTBuilder {
134133
ForeignModuleKind lookupKind,
135134
Demangle::Node::Kind kind);
136135

137-
Type checkTypeRepr(TypeRepr *repr);
138-
139136
static NominalTypeDecl *getAcceptableNominalTypeCandidate(ValueDecl *decl,
140137
Demangle::Node::Kind kind);
141-
142-
class TypeReprList {
143-
SmallVector<FixedTypeRepr, 4> Reprs;
144-
SmallVector<TypeRepr*, 4> Refs;
145-
146-
public:
147-
explicit TypeReprList(ArrayRef<Type> types) {
148-
Reprs.reserve(types.size());
149-
Refs.reserve(types.size());
150-
151-
for (auto type : types) {
152-
Reprs.emplace_back(type, SourceLoc());
153-
Refs.push_back(&Reprs.back());
154-
}
155-
}
156-
157-
ArrayRef<TypeRepr*> getList() const {
158-
return Refs;
159-
}
160-
};
161138
};
162139

163140
} // namespace Demangle

lib/AST/ASTDemangler.cpp

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "swift/AST/ASTDemangler.h"
2323

24-
#include "swift/Subsystems.h"
2524
#include "swift/AST/ASTContext.h"
2625
#include "swift/AST/Decl.h"
2726
#include "swift/AST/GenericSignature.h"
@@ -155,90 +154,7 @@ Type ASTBuilder::createBoundGenericType(NominalTypeDecl *decl,
155154
if (!validateNominalParent(decl, parent))
156155
return Type();
157156

158-
// Make a generic type repr that's been resolved to this decl.
159-
TypeReprList genericArgReprs(args);
160-
auto genericRepr = GenericIdentTypeRepr::create(Ctx, SourceLoc(),
161-
decl->getName(),
162-
genericArgReprs.getList(),
163-
SourceRange());
164-
// FIXME
165-
genericRepr->setValue(decl, nullptr);
166-
167-
Type genericType;
168-
169-
// If we have a parent type, we need to build a compound type repr.
170-
if (parent) {
171-
// Life would be much easier if we could just use a FixedTypeRepr for
172-
// the parent. But we can't! So we have to recursively expand
173-
// like this; and recursing with a lambda isn't impossible, so it gets
174-
// even worse.
175-
SmallVector<Type, 4> ancestry;
176-
for (auto p = parent; p; p = p->getNominalParent()) {
177-
ancestry.push_back(p);
178-
}
179-
180-
struct GenericRepr {
181-
TypeReprList GenericArgs;
182-
GenericIdentTypeRepr *Ident;
183-
184-
GenericRepr(const ASTContext &Ctx, BoundGenericType *type)
185-
: GenericArgs(type->getGenericArgs()),
186-
Ident(GenericIdentTypeRepr::create(Ctx, SourceLoc(),
187-
type->getDecl()->getName(),
188-
GenericArgs.getList(),
189-
SourceRange())) {
190-
// FIXME
191-
Ident->setValue(type->getDecl(), nullptr);
192-
}
193-
194-
// SmallVector::emplace_back will never need to call this because
195-
// we reserve the right size, but it does try statically.
196-
GenericRepr(const GenericRepr &other) : GenericArgs({}), Ident(nullptr) {
197-
llvm_unreachable("should not be called dynamically");
198-
}
199-
};
200-
201-
// Pre-allocate the component vectors so that we can form references
202-
// into them safely.
203-
SmallVector<SimpleIdentTypeRepr, 4> simpleComponents;
204-
SmallVector<GenericRepr, 4> genericComponents;
205-
simpleComponents.reserve(ancestry.size());
206-
genericComponents.reserve(ancestry.size());
207-
208-
// Build the parent hierarchy.
209-
SmallVector<ComponentIdentTypeRepr*, 4> componentReprs;
210-
for (size_t i = ancestry.size(); i != 0; --i) {
211-
Type p = ancestry[i - 1];
212-
if (auto boundGeneric = p->getAs<BoundGenericType>()) {
213-
genericComponents.emplace_back(Ctx, boundGeneric);
214-
componentReprs.push_back(genericComponents.back().Ident);
215-
} else {
216-
auto nominal = p->castTo<NominalType>();
217-
simpleComponents.emplace_back(SourceLoc(),
218-
nominal->getDecl()->getName());
219-
// FIXME
220-
simpleComponents.back().setValue(nominal->getDecl(), nullptr);
221-
componentReprs.push_back(&simpleComponents.back());
222-
}
223-
}
224-
componentReprs.push_back(genericRepr);
225-
226-
auto compoundRepr = CompoundIdentTypeRepr::create(Ctx, componentReprs);
227-
genericType = checkTypeRepr(compoundRepr);
228-
} else {
229-
genericType = checkTypeRepr(genericRepr);
230-
}
231-
232-
// If type-checking failed, we've failed.
233-
if (!genericType) return Type();
234-
235-
// Validate that we used the right decl.
236-
if (auto bgt = genericType->getAs<BoundGenericType>()) {
237-
if (bgt->getDecl() != decl)
238-
return Type();
239-
}
240-
241-
return genericType;
157+
return BoundGenericType::get(decl, parent, args);
242158
}
243159

244160
Type ASTBuilder::createTupleType(ArrayRef<Type> eltTypes,
@@ -434,16 +350,6 @@ bool ASTBuilder::validateNominalParent(NominalTypeDecl *decl,
434350
return true;
435351
}
436352

437-
Type ASTBuilder::checkTypeRepr(TypeRepr *repr) {
438-
DeclContext *dc = getNotionalDC();
439-
440-
TypeLoc loc(repr);
441-
if (performTypeLocChecking(Ctx, loc, dc, /*diagnose*/ false))
442-
return Type();
443-
444-
return loc.getType();
445-
}
446-
447353
NominalTypeDecl *
448354
ASTBuilder::getAcceptableNominalTypeCandidate(ValueDecl *decl,
449355
Demangle::Node::Kind kind) {

test/TypeDecoder/nominal_types.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ do {
4141
blackHole(x1, x2, x3)
4242
}
4343

44+
protocol P {}
45+
46+
struct Constrained<T : P> {}
47+
48+
func generic<T>(_: Constrained<T>) {}
49+
4450
// DEMANGLE: $s13nominal_types5OuterVD
4551
// CHECK: Outer
4652

@@ -66,3 +72,6 @@ do {
6672
// DEMANGLE: $s13nominal_types12GenericOuterOySiSSGD
6773
// CHECK: GenericOuter<τ_0_0, τ_0_1>
6874
// CHECK: GenericOuter<Int, String>
75+
76+
// DEMANGLE: $s13nominal_types11ConstrainedVyxGD
77+
// CHECK: Constrained<τ_0_0>

0 commit comments

Comments
 (0)