-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSwiftMangler.cpp
More file actions
89 lines (79 loc) · 2.96 KB
/
Copy pathSwiftMangler.cpp
File metadata and controls
89 lines (79 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "swift/extractor/mangler/SwiftMangler.h"
#include "swift/extractor/infra/SwiftDispatcher.h"
#include "swift/extractor/trap/generated/decl/TrapClasses.h"
#include <swift/AST/Module.h>
#include <sstream>
using namespace codeql;
namespace {
SwiftMangledName initMangled(const swift::TypeBase* type) {
switch (type->getKind()) {
#define TYPE(ID, PARENT) \
case swift::TypeKind::ID: \
return {{#ID "Type_"}};
#include <swift/AST/TypeNodes.def>
default:
return {};
}
}
SwiftMangledName initMangled(const swift::Decl* decl) {
SwiftMangledName ret;
ret << swift::Decl::getKindName(decl->getKind()) << "Decl_";
return ret;
}
} // namespace
SwiftMangledName SwiftMangler::mangleModuleName(std::string_view name) {
SwiftMangledName ret = {{"ModuleDecl_"}};
ret << name;
return ret;
}
SwiftMangledName SwiftMangler::mangleDecl(const swift::Decl& decl) {
if (!llvm::isa<swift::ValueDecl>(decl)) {
return {};
}
// We do not deduplicate local variables, but for the moment also non-local vars from non-swift
// (PCM, clang modules) modules as the mangler crashes sometimes
if (decl.getKind() == swift::DeclKind::Var &&
(decl.getDeclContext()->isLocalContext() || decl.getModuleContext()->isNonSwiftModule())) {
return {};
}
// we do not deduplicate GenericTypeParamDecl, as their mangling is ambiguous in the presence of
// extensions
if (decl.getKind() == swift::DeclKind::GenericTypeParam) {
return {};
}
if (decl.getKind() == swift::DeclKind::Module) {
return mangleModuleName(llvm::cast<swift::ModuleDecl>(decl).getRealName().str());
}
auto ret = initMangled(&decl);
const auto& valueDecl = llvm::cast<swift::ValueDecl>(decl);
// stamp all declarations with an id-ref of the containing module
auto moduleLabel = dispatcher.fetchLabel(decl.getModuleContext());
ret << moduleLabel;
if (decl.getKind() == swift::DeclKind::TypeAlias) {
// In cases like this (when coming from PCM)
// typealias CFXMLTree = CFTree
// typealias CFXMLTreeRef = CFXMLTree
// mangleAnyDecl mangles both CFXMLTree and CFXMLTreeRef into 'So12CFXMLTreeRefa'
// which is not correct and causes inconsistencies. mangleEntity makes these two distinct
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleEntity(&valueDecl);
} else {
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleAnyDecl(&valueDecl, /* prefix = */ false);
}
return ret;
}
SwiftMangledName SwiftMangler::visitModuleType(const swift::ModuleType* type) {
auto ret = initMangled(type);
ret << type->getModule()->getRealName().str();
if (type->getModule()->isNonSwiftModule()) {
ret << "|clang";
}
return ret;
}
SwiftMangledName SwiftMangler::visitBuiltinType(const swift::BuiltinType* type) {
auto ret = initMangled(type);
llvm::SmallString<32> buffer;
ret << type->getTypeName(buffer, /* prependBuiltinNamespace= */ false);
return ret;
}