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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
//===--- Context.cpp - Demangler Context ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the demangler Context.
//
//===----------------------------------------------------------------------===//
#include "swift/Demangling/Demangler.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/ManglingUtils.h"
#include "swift/Demangling/NamespaceMacros.h"
namespace swift {
namespace Demangle {
SWIFT_BEGIN_INLINE_NAMESPACE
//////////////////////////////////
// Context member functions //
//////////////////////////////////
Context::Context() : D(new Demangler) {
}
Context::~Context() {
delete D;
}
void Context::clear() {
D->clear();
}
NodePointer Context::demangleSymbolAsNode(llvm::StringRef MangledName) {
#if SWIFT_SUPPORT_OLD_MANGLING
if (isMangledName(MangledName)) {
return D->demangleSymbol(MangledName);
}
return demangleOldSymbolAsNode(MangledName, *D);
#else
return D->demangleSymbol(MangledName);
#endif
}
NodePointer Context::demangleTypeAsNode(llvm::StringRef MangledName) {
return D->demangleType(MangledName);
}
#if SWIFT_STDLIB_HAS_TYPE_PRINTING
std::string Context::demangleSymbolAsString(llvm::StringRef MangledName,
const DemangleOptions &Options) {
NodePointer root = demangleSymbolAsNode(MangledName);
if (!root) return MangledName.str();
std::string demangling = nodeToString(root, Options);
if (demangling.empty())
return MangledName.str();
return demangling;
}
std::string Context::demangleTypeAsString(llvm::StringRef MangledName,
const DemangleOptions &Options) {
NodePointer root = demangleTypeAsNode(MangledName);
if (!root) return MangledName.str();
std::string demangling = nodeToString(root, Options);
if (demangling.empty())
return MangledName.str();
return demangling;
}
#endif
// Removes a '.<n>' suffix from \p Name. <n> is either a number or a combination of
// '.<other-text>.<n>'.
// Such symbols are produced in IRGen or in LLVM optimizations.
static llvm::StringRef stripSuffix(llvm::StringRef Name) {
// A suffix always ends with a digit. Do this quick check to avoid scanning through the whole
// symbol name if the symbol has no suffix (= the common case).
if (swift::Mangle::isDigit(Name.back())) {
size_t dotPos = Name.find('.');
if (dotPos != StringRef::npos) {
Name = Name.substr(0, dotPos);
}
}
return Name;
}
// Removes a 'TQ<index>' or 'TY<index>' from \p Name.
static llvm::StringRef stripAsyncContinuation(llvm::StringRef Name) {
if (!Name.endswith("_"))
return Name;
StringRef Stripped = Name.drop_back();
while (!Stripped.empty() && swift::Mangle::isDigit(Stripped.back()))
Stripped = Stripped.drop_back();
if (Stripped.endswith("TQ") || Stripped.endswith("TY"))
return Stripped.drop_back(2);
return Name;
}
bool Context::isThunkSymbol(llvm::StringRef MangledName) {
if (isMangledName(MangledName)) {
MangledName = stripAsyncContinuation(stripSuffix(MangledName));
// First do a quick check
if (MangledName.endswith("TA") || // partial application forwarder
MangledName.endswith("Ta") || // ObjC partial application forwarder
MangledName.endswith("To") || // swift-as-ObjC thunk
MangledName.endswith("TO") || // ObjC-as-swift thunk
MangledName.endswith("TR") || // reabstraction thunk helper function
MangledName.endswith("Tr") || // reabstraction thunk
MangledName.endswith("TW") || // protocol witness thunk
MangledName.endswith("fC")) { // allocating constructor
// To avoid false positives, we need to fully demangle the symbol.
NodePointer Nd = D->demangleSymbol(MangledName);
if (!Nd || Nd->getKind() != Node::Kind::Global ||
Nd->getNumChildren() == 0)
return false;
switch (Nd->getFirstChild()->getKind()) {
case Node::Kind::ObjCAttribute:
case Node::Kind::NonObjCAttribute:
case Node::Kind::PartialApplyObjCForwarder:
case Node::Kind::PartialApplyForwarder:
case Node::Kind::ReabstractionThunkHelper:
case Node::Kind::ReabstractionThunk:
case Node::Kind::ProtocolWitness:
case Node::Kind::Allocator:
return true;
default:
break;
}
}
return false;
}
if (MangledName.starts_with("_T")) {
// Old mangling.
StringRef Remaining = MangledName.substr(2);
if (Remaining.starts_with("To") || // swift-as-ObjC thunk
Remaining.starts_with("TO") || // ObjC-as-swift thunk
Remaining.starts_with("PA_") || // partial application forwarder
Remaining.starts_with("PAo_")) { // ObjC partial application forwarder
return true;
}
}
return false;
}
std::string Context::getThunkTarget(llvm::StringRef MangledName) {
if (!isThunkSymbol(MangledName))
return std::string();
if (isMangledName(MangledName)) {
// If the symbol has a suffix we cannot derive the target.
if (stripSuffix(MangledName) != MangledName)
return std::string();
// Ignore any async continuation suffix
MangledName = stripAsyncContinuation(MangledName);
// The targets of those thunks not derivable from the mangling.
if (MangledName.endswith("TR") ||
MangledName.endswith("Tr") ||
MangledName.endswith("TW") )
return std::string();
if (MangledName.endswith("fC")) {
std::string target = MangledName.str();
target[target.size() - 1] = 'c';
return target;
}
return MangledName.substr(0, MangledName.size() - 2).str();
}
// Old mangling.
assert(MangledName.starts_with("_T"));
StringRef Remaining = MangledName.substr(2);
if (Remaining.starts_with("PA_"))
return Remaining.substr(3).str();
if (Remaining.starts_with("PAo_"))
return Remaining.substr(4).str();
assert(Remaining.starts_with("To") || Remaining.starts_with("TO"));
return std::string("_T") + Remaining.substr(2).str();
}
bool Context::hasSwiftCallingConvention(llvm::StringRef MangledName) {
Node *Global = demangleSymbolAsNode(MangledName);
if (!Global || Global->getKind() != Node::Kind::Global ||
Global->getNumChildren() == 0)
return false;
Node *TopLevel = Global->getFirstChild();
switch (TopLevel->getKind()) {
// Functions, which don't have the swift calling conventions:
case Node::Kind::TypeMetadataAccessFunction:
case Node::Kind::ValueWitness:
case Node::Kind::ProtocolWitnessTableAccessor:
case Node::Kind::GenericProtocolWitnessTableInstantiationFunction:
case Node::Kind::LazyProtocolWitnessTableAccessor:
case Node::Kind::AssociatedTypeMetadataAccessor:
case Node::Kind::AssociatedTypeWitnessTableAccessor:
case Node::Kind::BaseWitnessTableAccessor:
case Node::Kind::ObjCAttribute:
return false;
default:
break;
}
return true;
}
std::string Context::getModuleName(llvm::StringRef mangledName) {
NodePointer node = demangleSymbolAsNode(mangledName);
while (node) {
switch (node->getKind()) {
case Demangle::Node::Kind::Module:
return node->getText().str();
case Demangle::Node::Kind::TypeMangling:
case Demangle::Node::Kind::Type:
node = node->getFirstChild();
break;
case Demangle::Node::Kind::Global: {
NodePointer newNode = nullptr;
for (NodePointer child : *node) {
if (!isFunctionAttr(child->getKind())) {
newNode = child;
break;
}
}
node = newNode;
break;
}
default:
if (isSpecialized(node)) {
auto unspec = getUnspecialized(node, *D);
if (!unspec.isSuccess())
node = nullptr;
else
node = unspec.result();
break;
}
if (isContext(node->getKind())) {
node = node->getFirstChild();
break;
}
return std::string();
}
}
return std::string();
}
//////////////////////////////////
// Public utility functions //
//////////////////////////////////
#if SWIFT_STDLIB_HAS_TYPE_PRINTING
std::string demangleSymbolAsString(const char *MangledName,
size_t MangledNameLength,
const DemangleOptions &Options) {
Context Ctx;
return Ctx.demangleSymbolAsString(StringRef(MangledName, MangledNameLength),
Options);
}
std::string demangleTypeAsString(const char *MangledName,
size_t MangledNameLength,
const DemangleOptions &Options) {
Context Ctx;
return Ctx.demangleTypeAsString(StringRef(MangledName, MangledNameLength),
Options);
}
#endif
SWIFT_END_INLINE_NAMESPACE
} // namespace Demangle
} // namespace swift
|