Skip to content

Commit aaf4b88

Browse files
committed
AST: Add MangleLocalTypeDeclRequest
1 parent 161eba7 commit aaf4b88

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class ASTMangler : public Mangler {
176176
const AbstractStorageDecl *decl,
177177
StringRef USRPrefix);
178178

179+
std::string mangleLocalTypeDecl(const TypeDecl *type);
180+
179181
enum SpecialContext {
180182
ObjCContext,
181183
ClangImporterContext,

include/swift/AST/TypeCheckRequests.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,32 @@ class USRGenerationRequest :
311311
friend SimpleRequest;
312312

313313
// Evaluation.
314-
llvm::Expected<std::string> evaluate(Evaluator &eval, const ValueDecl* d) const;
314+
llvm::Expected<std::string> evaluate(Evaluator &eval, const ValueDecl *d) const;
315+
316+
public:
317+
// Cycle handling
318+
void diagnoseCycle(DiagnosticEngine &diags) const;
319+
void noteCycleStep(DiagnosticEngine &diags) const;
320+
321+
// Caching
322+
bool isCached() const { return true; }
323+
};
324+
325+
/// Generate the mangling for the given local type declaration.
326+
class MangleLocalTypeDeclRequest :
327+
public SimpleRequest<MangleLocalTypeDeclRequest,
328+
CacheKind::Cached,
329+
std::string,
330+
const TypeDecl*>
331+
{
332+
public:
333+
using SimpleRequest::SimpleRequest;
334+
335+
private:
336+
friend SimpleRequest;
337+
338+
// Evaluation.
339+
llvm::Expected<std::string> evaluate(Evaluator &eval, const TypeDecl *d) const;
315340

316341
public:
317342
// Cycle handling

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ SWIFT_TYPEID(IsDynamicRequest)
2323
SWIFT_TYPEID(RequirementRequest)
2424
SWIFT_TYPEID(USRGenerationRequest)
2525
SWIFT_TYPEID(DefaultTypeRequest)
26+
SWIFT_TYPEID(MangleLocalTypeDeclRequest)

lib/AST/ASTMangler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,22 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
506506
return finalize();
507507
}
508508

509+
std::string ASTMangler::mangleLocalTypeDecl(const TypeDecl *type) {
510+
beginManglingWithoutPrefix();
511+
AllowNamelessEntities = true;
512+
OptimizeProtocolNames = false;
513+
514+
if (auto GTD = dyn_cast<GenericTypeDecl>(type)) {
515+
appendAnyGenericType(GTD);
516+
} else {
517+
assert(isa<AssociatedTypeDecl>(type));
518+
appendContextOf(type);
519+
appendDeclName(type);
520+
appendOperator("Qa");
521+
}
522+
523+
return finalize();
524+
}
509525

510526
void ASTMangler::appendSymbolKind(SymbolKind SKind) {
511527
switch (SKind) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ void USRGenerationRequest::noteCycleStep(DiagnosticEngine &diags) const {
431431
diags.diagnose(d, diag::circular_reference);
432432
}
433433

434+
//----------------------------------------------------------------------------//
435+
// Mangled local type name computation.
436+
//----------------------------------------------------------------------------//
437+
438+
void MangleLocalTypeDeclRequest::diagnoseCycle(DiagnosticEngine &diags) const {
439+
const auto &storage = getStorage();
440+
auto &d = std::get<0>(storage);
441+
diags.diagnose(d, diag::circular_reference);
442+
}
443+
444+
void MangleLocalTypeDeclRequest::noteCycleStep(DiagnosticEngine &diags) const {
445+
const auto &storage = getStorage();
446+
auto &d = std::get<0>(storage);
447+
diags.diagnose(d, diag::circular_reference);
448+
}
449+
434450
//----------------------------------------------------------------------------//
435451
// DefaultTypeRequest.
436452
//----------------------------------------------------------------------------//

lib/AST/USRGeneration.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ static bool shouldUseObjCUSR(const Decl *D) {
165165
}
166166

167167
llvm::Expected<std::string>
168-
swift::USRGenerationRequest::evaluate(Evaluator &evaluator, const ValueDecl* D) const {
168+
swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
169+
const ValueDecl *D) const {
169170
if (!D->hasName() && !isa<ParamDecl>(D) && !isa<AccessorDecl>(D))
170171
return std::string(); // Ignore.
171172
if (D->getModuleContext()->isBuiltinModule())
@@ -249,6 +250,19 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator, const ValueDecl* D)
249250
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
250251
}
251252

253+
llvm::Expected<std::string>
254+
swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
255+
const TypeDecl *D) const {
256+
if (!D->hasInterfaceType())
257+
return std::string();
258+
259+
if (isa<ModuleDecl>(D))
260+
return std::string(); // Ignore.
261+
262+
Mangle::ASTMangler NewMangler;
263+
return NewMangler.mangleLocalTypeDecl(D);
264+
}
265+
252266
bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
253267
if (auto *D = Mod.getAsSwiftModule()) {
254268
StringRef moduleName = D->getName().str();

0 commit comments

Comments
 (0)