2018-07-21 01:27:48 +08:00
|
|
|
//===- MicrosoftDemangle.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a demangler for MSVC-style mangled symbols.
|
|
|
|
//
|
|
|
|
// This file has no dependencies on the rest of LLVM so that it can be
|
|
|
|
// easily reused in other programs such as libcxxabi.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Demangle/Demangle.h"
|
|
|
|
|
|
|
|
#include "Compiler.h"
|
|
|
|
#include "StringView.h"
|
|
|
|
#include "Utility.h"
|
|
|
|
|
|
|
|
#include <cctype>
|
2018-08-02 02:44:12 +08:00
|
|
|
#include <cstdio>
|
2018-07-27 03:56:09 +08:00
|
|
|
#include <tuple>
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// This memory allocator is extremely fast, but it doesn't call dtors
|
|
|
|
// for allocated objects. That means you can't use STL containers
|
|
|
|
// (such as std::vector) with this allocator. But it pays off --
|
|
|
|
// the demangler is 3x faster with this allocator compared to one with
|
|
|
|
// STL containers.
|
|
|
|
namespace {
|
2018-07-30 11:25:27 +08:00
|
|
|
constexpr size_t AllocUnit = 4096;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
class ArenaAllocator {
|
|
|
|
struct AllocatorNode {
|
|
|
|
uint8_t *Buf = nullptr;
|
|
|
|
size_t Used = 0;
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
size_t Capacity = 0;
|
2018-07-21 01:27:48 +08:00
|
|
|
AllocatorNode *Next = nullptr;
|
|
|
|
};
|
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
void addNode(size_t Capacity) {
|
|
|
|
AllocatorNode *NewHead = new AllocatorNode;
|
|
|
|
NewHead->Buf = new uint8_t[Capacity];
|
|
|
|
NewHead->Next = Head;
|
|
|
|
NewHead->Capacity = Capacity;
|
|
|
|
Head = NewHead;
|
|
|
|
NewHead->Used = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
public:
|
2018-07-30 11:25:27 +08:00
|
|
|
ArenaAllocator() { addNode(AllocUnit); }
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
~ArenaAllocator() {
|
|
|
|
while (Head) {
|
|
|
|
assert(Head->Buf);
|
|
|
|
delete[] Head->Buf;
|
2018-07-24 02:21:43 +08:00
|
|
|
AllocatorNode *Next = Head->Next;
|
|
|
|
delete Head;
|
|
|
|
Head = Next;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
char *allocUnalignedBuffer(size_t Length) {
|
|
|
|
uint8_t *Buf = Head->Buf + Head->Used;
|
|
|
|
|
|
|
|
Head->Used += Length;
|
|
|
|
if (Head->Used > Head->Capacity) {
|
|
|
|
// It's possible we need a buffer which is larger than our default unit
|
|
|
|
// size, so we need to be careful to add a node with capacity that is at
|
|
|
|
// least as large as what we need.
|
2018-07-30 11:25:27 +08:00
|
|
|
addNode(std::max(AllocUnit, Length));
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
Head->Used = Length;
|
|
|
|
Buf = Head->Buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<char *>(Buf);
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:35:06 +08:00
|
|
|
template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
|
|
|
|
|
|
|
|
size_t Size = sizeof(T);
|
2018-07-21 01:27:48 +08:00
|
|
|
assert(Head && Head->Buf);
|
|
|
|
|
2018-07-21 02:35:06 +08:00
|
|
|
size_t P = (size_t)Head->Buf + Head->Used;
|
|
|
|
uintptr_t AlignedP =
|
|
|
|
(((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
|
|
|
|
uint8_t *PP = (uint8_t *)AlignedP;
|
|
|
|
size_t Adjustment = AlignedP - P;
|
|
|
|
|
|
|
|
Head->Used += Size + Adjustment;
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
if (Head->Used < Head->Capacity)
|
2018-07-21 02:35:06 +08:00
|
|
|
return new (PP) T(std::forward<Args>(ConstructorArgs)...);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 11:25:27 +08:00
|
|
|
addNode(AllocUnit);
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
Head->Used = Size;
|
|
|
|
return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AllocatorNode *Head = nullptr;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static bool startsWithDigit(StringView S) {
|
|
|
|
return !S.empty() && std::isdigit(S.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes a space if the last token does not end with a punctuation.
|
|
|
|
static void outputSpaceIfNecessary(OutputStream &OS) {
|
|
|
|
if (OS.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
char C = OS.back();
|
|
|
|
if (isalnum(C) || C == '>')
|
|
|
|
OS << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage classes
|
|
|
|
enum Qualifiers : uint8_t {
|
|
|
|
Q_None = 0,
|
|
|
|
Q_Const = 1 << 0,
|
|
|
|
Q_Volatile = 1 << 1,
|
|
|
|
Q_Far = 1 << 2,
|
|
|
|
Q_Huge = 1 << 3,
|
|
|
|
Q_Unaligned = 1 << 4,
|
|
|
|
Q_Restrict = 1 << 5,
|
|
|
|
Q_Pointer64 = 1 << 6
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class StorageClass : uint8_t {
|
|
|
|
None,
|
|
|
|
PrivateStatic,
|
|
|
|
ProtectedStatic,
|
|
|
|
PublicStatic,
|
|
|
|
Global,
|
2018-08-18 00:14:05 +08:00
|
|
|
FunctionLocalStatic,
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class QualifierMangleMode { Drop, Mangle, Result };
|
2018-07-27 03:56:09 +08:00
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
enum class PointerAffinity { Pointer, Reference, RValueReference };
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Calling conventions
|
|
|
|
enum class CallingConv : uint8_t {
|
|
|
|
None,
|
|
|
|
Cdecl,
|
|
|
|
Pascal,
|
|
|
|
Thiscall,
|
|
|
|
Stdcall,
|
|
|
|
Fastcall,
|
|
|
|
Clrcall,
|
|
|
|
Eabi,
|
|
|
|
Vectorcall,
|
|
|
|
Regcall,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ReferenceKind : uint8_t { None, LValueRef, RValueRef };
|
|
|
|
|
|
|
|
// Types
|
|
|
|
enum class PrimTy : uint8_t {
|
|
|
|
Unknown,
|
|
|
|
None,
|
|
|
|
Function,
|
|
|
|
Ptr,
|
2018-07-27 03:56:09 +08:00
|
|
|
MemberPtr,
|
2018-07-21 01:27:48 +08:00
|
|
|
Array,
|
|
|
|
|
|
|
|
Struct,
|
|
|
|
Union,
|
|
|
|
Class,
|
|
|
|
Enum,
|
|
|
|
|
|
|
|
Void,
|
|
|
|
Bool,
|
|
|
|
Char,
|
|
|
|
Schar,
|
|
|
|
Uchar,
|
2018-07-31 07:02:10 +08:00
|
|
|
Char16,
|
|
|
|
Char32,
|
2018-07-21 01:27:48 +08:00
|
|
|
Short,
|
|
|
|
Ushort,
|
|
|
|
Int,
|
|
|
|
Uint,
|
|
|
|
Long,
|
|
|
|
Ulong,
|
|
|
|
Int64,
|
|
|
|
Uint64,
|
|
|
|
Wchar,
|
|
|
|
Float,
|
|
|
|
Double,
|
|
|
|
Ldouble,
|
2018-08-18 00:14:05 +08:00
|
|
|
Nullptr,
|
|
|
|
Vftable,
|
|
|
|
Vbtable,
|
|
|
|
LocalStaticGuard
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class OperatorTy : uint8_t {
|
|
|
|
Ctor, // ?0 # Foo::Foo()
|
|
|
|
Dtor, // ?1 # Foo::~Foo()
|
|
|
|
New, // ?2 # operator new
|
|
|
|
Delete, // ?3 # operator delete
|
|
|
|
Assign, // ?4 # operator=
|
|
|
|
RightShift, // ?5 # operator>>
|
|
|
|
LeftShift, // ?6 # operator<<
|
|
|
|
LogicalNot, // ?7 # operator!
|
|
|
|
Equals, // ?8 # operator==
|
|
|
|
NotEquals, // ?9 # operator!=
|
|
|
|
ArraySubscript, // ?A # operator[]
|
|
|
|
Conversion, // ?B # Foo::operator <type>()
|
|
|
|
Pointer, // ?C # operator->
|
|
|
|
Dereference, // ?D # operator*
|
|
|
|
Increment, // ?E # operator++
|
|
|
|
Decrement, // ?F # operator--
|
|
|
|
Minus, // ?G # operator-
|
|
|
|
Plus, // ?H # operator+
|
|
|
|
BitwiseAnd, // ?I # operator&
|
|
|
|
MemberPointer, // ?J # operator->*
|
|
|
|
Divide, // ?K # operator/
|
|
|
|
Modulus, // ?L # operator%
|
|
|
|
LessThan, // ?M operator<
|
|
|
|
LessThanEqual, // ?N operator<=
|
|
|
|
GreaterThan, // ?O operator>
|
|
|
|
GreaterThanEqual, // ?P operator>=
|
|
|
|
Comma, // ?Q operator,
|
|
|
|
Parens, // ?R operator()
|
|
|
|
BitwiseNot, // ?S operator~
|
|
|
|
BitwiseXor, // ?T operator^
|
|
|
|
BitwiseOr, // ?U operator|
|
|
|
|
LogicalAnd, // ?V operator&&
|
|
|
|
LogicalOr, // ?W operator||
|
|
|
|
TimesEqual, // ?X operator*=
|
|
|
|
PlusEqual, // ?Y operator+=
|
|
|
|
MinusEqual, // ?Z operator-=
|
|
|
|
DivEqual, // ?_0 operator/=
|
|
|
|
ModEqual, // ?_1 operator%=
|
|
|
|
RshEqual, // ?_2 operator>>=
|
|
|
|
LshEqual, // ?_3 operator<<=
|
|
|
|
BitwiseAndEqual, // ?_4 operator&=
|
|
|
|
BitwiseOrEqual, // ?_5 operator|=
|
|
|
|
BitwiseXorEqual, // ?_6 operator^=
|
|
|
|
Vftable, // ?_7 # vftable
|
|
|
|
Vbtable, // ?_8 # vbtable
|
|
|
|
Vcall, // ?_9 # vcall
|
|
|
|
Typeof, // ?_A # typeof
|
|
|
|
LocalStaticGuard, // ?_B # local static guard
|
|
|
|
StringLiteral, // ?_C # string literal
|
|
|
|
VbaseDtor, // ?_D # vbase destructor
|
|
|
|
VecDelDtor, // ?_E # vector deleting destructor
|
|
|
|
DefaultCtorClosure, // ?_F # default constructor closure
|
|
|
|
ScalarDelDtor, // ?_G # scalar deleting destructor
|
|
|
|
VecCtorIter, // ?_H # vector constructor iterator
|
|
|
|
VecDtorIter, // ?_I # vector destructor iterator
|
|
|
|
VecVbaseCtorIter, // ?_J # vector vbase constructor iterator
|
|
|
|
VdispMap, // ?_K # virtual displacement map
|
|
|
|
EHVecCtorIter, // ?_L # eh vector constructor iterator
|
|
|
|
EHVecDtorIter, // ?_M # eh vector destructor iterator
|
|
|
|
EHVecVbaseCtorIter, // ?_N # eh vector vbase constructor iterator
|
|
|
|
CopyCtorClosure, // ?_O # copy constructor closure
|
|
|
|
UdtReturning, // ?_P<name> # udt returning <name>
|
|
|
|
Unknown, // ?_Q # <unknown>
|
|
|
|
RttiTypeDescriptor, // ?_R0 # RTTI Type Descriptor
|
|
|
|
RttiBaseClassDescriptor, // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
|
|
|
|
RttiBaseClassArray, // ?_R2 # RTTI Base Class Array
|
|
|
|
RttiClassHierarchyDescriptor, // ?_R3 # RTTI Class Hierarchy Descriptor
|
|
|
|
RttiCompleteObjLocator, // ?_R4 # RTTI Complete Object Locator
|
|
|
|
LocalVftable, // ?_S # local vftable
|
|
|
|
LocalVftableCtorClosure, // ?_T # local vftable constructor closure
|
|
|
|
ArrayNew, // ?_U operator new[]
|
|
|
|
ArrayDelete, // ?_V operator delete[]
|
|
|
|
LiteralOperator, // ?__K operator ""_name
|
|
|
|
CoAwait, // ?__L co_await
|
|
|
|
Spaceship, // operator<=>
|
|
|
|
};
|
|
|
|
|
|
|
|
// A map to translate from operator prefix to operator type.
|
|
|
|
struct OperatorMapEntry {
|
|
|
|
StringView Prefix;
|
|
|
|
StringView Name;
|
|
|
|
OperatorTy Operator;
|
|
|
|
};
|
|
|
|
|
|
|
|
OperatorMapEntry OperatorMap[] = {
|
|
|
|
{"0", " <ctor>", OperatorTy::Ctor},
|
|
|
|
{"1", " <dtor>", OperatorTy::Dtor},
|
|
|
|
{"2", "operator new", OperatorTy::New},
|
|
|
|
{"3", "operator delete", OperatorTy::Delete},
|
|
|
|
{"4", "operator=", OperatorTy::Assign},
|
|
|
|
{"5", "operator>>", OperatorTy::RightShift},
|
|
|
|
{"6", "operator<<", OperatorTy::LeftShift},
|
|
|
|
{"7", "operator!", OperatorTy::LogicalNot},
|
|
|
|
{"8", "operator==", OperatorTy::Equals},
|
|
|
|
{"9", "operator!=", OperatorTy::NotEquals},
|
|
|
|
{"A", "operator[]", OperatorTy::ArraySubscript},
|
|
|
|
{"B", "operator <conversion>", OperatorTy::Conversion},
|
|
|
|
{"C", "operator->", OperatorTy::Pointer},
|
|
|
|
{"D", "operator*", OperatorTy::Dereference},
|
|
|
|
{"E", "operator++", OperatorTy::Increment},
|
|
|
|
{"F", "operator--", OperatorTy::Decrement},
|
|
|
|
{"G", "operator-", OperatorTy::Minus},
|
|
|
|
{"H", "operator+", OperatorTy::Plus},
|
|
|
|
{"I", "operator&", OperatorTy::BitwiseAnd},
|
|
|
|
{"J", "operator->*", OperatorTy::MemberPointer},
|
|
|
|
{"K", "operator/", OperatorTy::Divide},
|
|
|
|
{"L", "operator%", OperatorTy::Modulus},
|
|
|
|
{"M", "operator<", OperatorTy::LessThan},
|
|
|
|
{"N", "operator<=", OperatorTy::LessThanEqual},
|
|
|
|
{"O", "operator>", OperatorTy::GreaterThan},
|
|
|
|
{"P", "operator>=", OperatorTy::GreaterThanEqual},
|
|
|
|
{"Q", "operator,", OperatorTy::Comma},
|
|
|
|
{"R", "operator()", OperatorTy::Parens},
|
|
|
|
{"S", "operator~", OperatorTy::BitwiseNot},
|
|
|
|
{"T", "operator^", OperatorTy::BitwiseXor},
|
|
|
|
{"U", "operator|", OperatorTy::BitwiseOr},
|
|
|
|
{"V", "operator&&", OperatorTy::LogicalAnd},
|
|
|
|
{"W", "operator||", OperatorTy::LogicalOr},
|
|
|
|
{"X", "operator*=", OperatorTy::TimesEqual},
|
|
|
|
{"Y", "operator+=", OperatorTy::PlusEqual},
|
|
|
|
{"Z", "operator-=", OperatorTy::MinusEqual},
|
|
|
|
{"_0", "operator/=", OperatorTy::DivEqual},
|
|
|
|
{"_1", "operator%=", OperatorTy::ModEqual},
|
|
|
|
{"_2", "operator>>=", OperatorTy::RshEqual},
|
|
|
|
{"_3", "operator<<=", OperatorTy::LshEqual},
|
|
|
|
{"_4", "operator&=", OperatorTy::BitwiseAndEqual},
|
|
|
|
{"_5", "operator|=", OperatorTy::BitwiseOrEqual},
|
|
|
|
{"_6", "operator^=", OperatorTy::BitwiseXorEqual},
|
|
|
|
{"_7", "`vftable'", OperatorTy::Vftable},
|
|
|
|
{"_8", "`vbtable'", OperatorTy::Vbtable},
|
|
|
|
{"_9", "`vcall'", OperatorTy::Vcall},
|
|
|
|
{"_A", "`typeof'", OperatorTy::Typeof},
|
|
|
|
{"_B", "`local static guard'", OperatorTy::LocalStaticGuard},
|
|
|
|
{"_C", "`string'", OperatorTy::StringLiteral},
|
|
|
|
{"_D", "`vbase dtor'", OperatorTy::VbaseDtor},
|
|
|
|
{"_E", "`vector deleting dtor'", OperatorTy::VecDelDtor},
|
|
|
|
{"_F", "`default ctor closure'", OperatorTy::DefaultCtorClosure},
|
|
|
|
{"_G", "`scalar deleting dtor'", OperatorTy::ScalarDelDtor},
|
|
|
|
{"_H", "`vector ctor iterator'", OperatorTy::VecCtorIter},
|
|
|
|
{"_I", "`vector dtor iterator'", OperatorTy::VecDtorIter},
|
|
|
|
{"_J", "`vector vbase ctor iterator'", OperatorTy::VecVbaseCtorIter},
|
|
|
|
{"_K", "`virtual displacement map'", OperatorTy::VdispMap},
|
|
|
|
{"_L", "`eh vector ctor iterator'", OperatorTy::EHVecCtorIter},
|
|
|
|
{"_M", "`eh vector dtor iterator'", OperatorTy::EHVecDtorIter},
|
|
|
|
{"_N", "`eh vector vbase ctor iterator'", OperatorTy::EHVecVbaseCtorIter},
|
|
|
|
{"_O", "`copy ctor closure'", OperatorTy::CopyCtorClosure},
|
|
|
|
{"_P", "`udt returning'", OperatorTy::UdtReturning},
|
|
|
|
{"_Q", "`unknown'", OperatorTy::Unknown},
|
|
|
|
{"_R0", "`RTTI Type Descriptor'", OperatorTy::RttiTypeDescriptor},
|
|
|
|
{"_R1", "`RTTI Base Class Descriptor'",
|
|
|
|
OperatorTy::RttiBaseClassDescriptor},
|
|
|
|
{"_R2", "`RTTI Base Class Array'", OperatorTy::RttiBaseClassArray},
|
|
|
|
{"_R3", "`RTTI Class Hierarchy Descriptor'",
|
|
|
|
OperatorTy::RttiClassHierarchyDescriptor},
|
|
|
|
{"_R4", "`RTTI Complete Object Locator'",
|
|
|
|
OperatorTy::RttiCompleteObjLocator},
|
|
|
|
{"_S", "`local vftable'", OperatorTy::LocalVftable},
|
|
|
|
{"_T", "`local vftable ctor closure'", OperatorTy::LocalVftableCtorClosure},
|
|
|
|
{"_U", "operator new[]", OperatorTy::ArrayNew},
|
|
|
|
{"_V", "operator delete[]", OperatorTy::ArrayDelete},
|
|
|
|
{"__K", "operator \"\"", OperatorTy::LiteralOperator},
|
|
|
|
{"__L", "co_await", OperatorTy::CoAwait},
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function classes
|
2018-08-11 05:09:05 +08:00
|
|
|
enum FuncClass : uint16_t {
|
2018-07-21 01:27:48 +08:00
|
|
|
Public = 1 << 0,
|
|
|
|
Protected = 1 << 1,
|
|
|
|
Private = 1 << 2,
|
|
|
|
Global = 1 << 3,
|
|
|
|
Static = 1 << 4,
|
|
|
|
Virtual = 1 << 5,
|
2018-07-21 02:07:33 +08:00
|
|
|
Far = 1 << 6,
|
2018-08-11 05:09:05 +08:00
|
|
|
ExternC = 1 << 7,
|
|
|
|
NoPrototype = 1 << 8,
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
|
2018-08-08 08:43:31 +08:00
|
|
|
enum NameBackrefBehavior : uint8_t {
|
|
|
|
NBB_None = 0, // don't save any names as backrefs.
|
|
|
|
NBB_Template = 1 << 0, // save template instanations.
|
|
|
|
NBB_Simple = 1 << 1, // save simple names.
|
|
|
|
};
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
enum class SymbolCategory { Unknown, Function, Variable };
|
2018-08-02 02:32:47 +08:00
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
struct NameResolver {
|
2018-08-03 01:33:33 +08:00
|
|
|
virtual ~NameResolver() = default;
|
2018-08-03 01:08:03 +08:00
|
|
|
virtual StringView resolve(StringView S) = 0;
|
|
|
|
};
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
struct Type;
|
2018-07-31 07:02:10 +08:00
|
|
|
struct Name;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
struct FunctionParams {
|
2018-07-27 04:20:10 +08:00
|
|
|
bool IsVariadic = false;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Type *Current = nullptr;
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
FunctionParams *Next = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TemplateParams {
|
|
|
|
bool IsTemplateTemplate = false;
|
|
|
|
bool IsAliasTemplate = false;
|
2018-08-10 22:31:04 +08:00
|
|
|
bool IsIntegerLiteral = false;
|
|
|
|
bool IntegerLiteralIsNegative = false;
|
|
|
|
bool IsEmptyParameterPack = false;
|
|
|
|
bool PointerToSymbol = false;
|
|
|
|
bool ReferenceToSymbol = false;
|
|
|
|
|
|
|
|
// If IsIntegerLiteral is true, this is a non-type template parameter
|
|
|
|
// whose value is contained in this field.
|
|
|
|
uint64_t IntegralValue = 0;
|
2018-08-01 01:16:44 +08:00
|
|
|
|
|
|
|
// Type can be null if this is a template template parameter. In that case
|
|
|
|
// only Name will be valid.
|
|
|
|
Type *ParamType = nullptr;
|
2018-07-31 07:02:10 +08:00
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
// Name can be valid if this is a template template parameter (see above) or
|
|
|
|
// this is a function declaration (e.g. foo<&SomeFunc>). In the latter case
|
|
|
|
// Name contains the name of the function and Type contains the signature.
|
|
|
|
Name *ParamName = nullptr;
|
|
|
|
|
|
|
|
TemplateParams *Next = nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The type class. Mangled symbols are first parsed and converted to
|
|
|
|
// this type and then converted to string.
|
|
|
|
struct Type {
|
|
|
|
virtual ~Type() {}
|
|
|
|
|
|
|
|
virtual Type *clone(ArenaAllocator &Arena) const;
|
|
|
|
|
|
|
|
// Write the "first half" of a given type. This is a static functions to
|
|
|
|
// give the code a chance to do processing that is common to a subset of
|
|
|
|
// subclasses
|
2018-08-03 01:08:03 +08:00
|
|
|
static void outputPre(OutputStream &OS, Type &Ty, NameResolver &Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Write the "second half" of a given type. This is a static functions to
|
|
|
|
// give the code a chance to do processing that is common to a subset of
|
|
|
|
// subclasses
|
2018-08-03 01:08:03 +08:00
|
|
|
static void outputPost(OutputStream &OS, Type &Ty, NameResolver &Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
virtual void outputPre(OutputStream &OS, NameResolver &Resolver);
|
|
|
|
virtual void outputPost(OutputStream &OS, NameResolver &Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Primitive type such as Int.
|
|
|
|
PrimTy Prim = PrimTy::Unknown;
|
|
|
|
|
|
|
|
Qualifiers Quals = Q_None;
|
|
|
|
StorageClass Storage = StorageClass::None; // storage class
|
|
|
|
};
|
|
|
|
|
|
|
|
// Represents an identifier which may be a template.
|
|
|
|
struct Name {
|
2018-08-18 00:14:05 +08:00
|
|
|
virtual ~Name() = default;
|
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
bool IsTemplateInstantiation = false;
|
|
|
|
bool IsOperator = false;
|
2018-08-03 01:08:03 +08:00
|
|
|
bool IsBackReference = false;
|
2018-08-17 00:17:36 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
bool isStringLiteralOperatorInfo() const;
|
2018-08-10 23:04:56 +08:00
|
|
|
|
|
|
|
// Name read from an MangledName string.
|
|
|
|
StringView Str;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
// Template parameters. Only valid if IsTemplateInstantiation is true.
|
2018-08-01 01:16:44 +08:00
|
|
|
TemplateParams *TParams = nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Nested BackReferences (e.g. "A::B::C") are represented as a linked list.
|
|
|
|
Name *Next = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
struct OperatorInfo : public Name {
|
|
|
|
const OperatorMapEntry *Info = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StringLiteral : public OperatorInfo {
|
|
|
|
PrimTy CharType;
|
|
|
|
bool IsTruncated = false;
|
|
|
|
};
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
struct PointerType : public Type {
|
|
|
|
Type *clone(ArenaAllocator &Arena) const override;
|
2018-08-03 01:08:03 +08:00
|
|
|
void outputPre(OutputStream &OS, NameResolver &Resolver) override;
|
|
|
|
void outputPost(OutputStream &OS, NameResolver &Resolver) override;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
PointerAffinity Affinity;
|
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
// Represents a type X in "a pointer to X", "a reference to X",
|
|
|
|
// "an array of X", or "a function returning X".
|
|
|
|
Type *Pointee = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MemberPointerType : public Type {
|
|
|
|
Type *clone(ArenaAllocator &Arena) const override;
|
2018-08-03 01:08:03 +08:00
|
|
|
void outputPre(OutputStream &OS, NameResolver &Resolver) override;
|
|
|
|
void outputPost(OutputStream &OS, NameResolver &Resolver) override;
|
2018-07-27 03:56:09 +08:00
|
|
|
|
|
|
|
Name *MemberName = nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Represents a type X in "a pointer to X", "a reference to X",
|
|
|
|
// "an array of X", or "a function returning X".
|
|
|
|
Type *Pointee = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionType : public Type {
|
|
|
|
Type *clone(ArenaAllocator &Arena) const override;
|
2018-08-03 01:08:03 +08:00
|
|
|
void outputPre(OutputStream &OS, NameResolver &Resolver) override;
|
|
|
|
void outputPost(OutputStream &OS, NameResolver &Resolver) override;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 04:33:48 +08:00
|
|
|
// True if this FunctionType instance is the Pointee of a PointerType or
|
|
|
|
// MemberPointerType.
|
|
|
|
bool IsFunctionPointer = false;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Type *ReturnType = nullptr;
|
|
|
|
// If this is a reference, the type of reference.
|
|
|
|
ReferenceKind RefKind;
|
|
|
|
|
|
|
|
CallingConv CallConvention;
|
|
|
|
FuncClass FunctionClass;
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
FunctionParams Params;
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UdtType : public Type {
|
|
|
|
Type *clone(ArenaAllocator &Arena) const override;
|
2018-08-03 01:08:03 +08:00
|
|
|
void outputPre(OutputStream &OS, NameResolver &Resolver) override;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
Name *UdtName = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-08-10 22:31:04 +08:00
|
|
|
struct ArrayDimension {
|
|
|
|
uint64_t Dim = 0;
|
|
|
|
ArrayDimension *Next = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
struct ArrayType : public Type {
|
|
|
|
Type *clone(ArenaAllocator &Arena) const override;
|
2018-08-03 01:08:03 +08:00
|
|
|
void outputPre(OutputStream &OS, NameResolver &Resolver) override;
|
|
|
|
void outputPost(OutputStream &OS, NameResolver &Resolver) override;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Either NextDimension or ElementType will be valid.
|
2018-08-10 22:31:04 +08:00
|
|
|
ArrayDimension *Dims = nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
Type *ElementType = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
static bool isMemberPointer(StringView MangledName) {
|
|
|
|
switch (MangledName.popFront()) {
|
2018-07-31 07:02:10 +08:00
|
|
|
case '$':
|
|
|
|
// This is probably an rvalue reference (e.g. $$Q), and you cannot have an
|
|
|
|
// rvalue reference to a member.
|
|
|
|
return false;
|
2018-07-27 03:56:09 +08:00
|
|
|
case 'A':
|
|
|
|
// 'A' indicates a reference, and you cannot have a reference to a member
|
2018-07-31 07:02:10 +08:00
|
|
|
// function or member.
|
2018-07-27 03:56:09 +08:00
|
|
|
return false;
|
|
|
|
case 'P':
|
|
|
|
case 'Q':
|
|
|
|
case 'R':
|
|
|
|
case 'S':
|
|
|
|
// These 4 values indicate some kind of pointer, but we still don't know
|
|
|
|
// what.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "Ty is not a pointer type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it starts with a number, then 6 indicates a non-member function
|
|
|
|
// pointer, and 8 indicates a member function pointer.
|
|
|
|
if (startsWithDigit(MangledName)) {
|
|
|
|
assert(MangledName[0] == '6' || MangledName[0] == '8');
|
|
|
|
return (MangledName[0] == '8');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove ext qualifiers since those can appear on either type and are
|
|
|
|
// therefore not indicative.
|
|
|
|
MangledName.consumeFront('E'); // 64-bit
|
|
|
|
MangledName.consumeFront('I'); // restrict
|
|
|
|
MangledName.consumeFront('F'); // unaligned
|
|
|
|
|
|
|
|
assert(!MangledName.empty());
|
|
|
|
|
|
|
|
// The next value should be either ABCD (non-member) or QRST (member).
|
|
|
|
switch (MangledName.front()) {
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
return false;
|
|
|
|
case 'Q':
|
|
|
|
case 'R':
|
|
|
|
case 'S':
|
|
|
|
case 'T':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
static void outputCallingConvention(OutputStream &OS, CallingConv CC) {
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
|
|
|
|
switch (CC) {
|
|
|
|
case CallingConv::Cdecl:
|
|
|
|
OS << "__cdecl";
|
|
|
|
break;
|
|
|
|
case CallingConv::Fastcall:
|
|
|
|
OS << "__fastcall";
|
|
|
|
break;
|
|
|
|
case CallingConv::Pascal:
|
|
|
|
OS << "__pascal";
|
|
|
|
break;
|
|
|
|
case CallingConv::Regcall:
|
|
|
|
OS << "__regcall";
|
|
|
|
break;
|
|
|
|
case CallingConv::Stdcall:
|
|
|
|
OS << "__stdcall";
|
|
|
|
break;
|
|
|
|
case CallingConv::Thiscall:
|
|
|
|
OS << "__thiscall";
|
|
|
|
break;
|
|
|
|
case CallingConv::Eabi:
|
|
|
|
OS << "__eabi";
|
|
|
|
break;
|
|
|
|
case CallingConv::Vectorcall:
|
|
|
|
OS << "__vectorcall";
|
|
|
|
break;
|
|
|
|
case CallingConv::Clrcall:
|
|
|
|
OS << "__clrcall";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
static bool startsWithLocalScopePattern(StringView S) {
|
|
|
|
if (!S.consumeFront('?'))
|
|
|
|
return false;
|
|
|
|
if (S.size() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t End = S.find('?');
|
|
|
|
if (End == StringView::npos)
|
|
|
|
return false;
|
|
|
|
StringView Candidate = S.substr(0, End);
|
|
|
|
if (Candidate.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// \?[0-9]\?
|
|
|
|
// ?@? is the discriminator 0.
|
|
|
|
if (Candidate.size() == 1)
|
|
|
|
return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
|
|
|
|
|
|
|
|
// If it's not 0-9, then it's an encoded number terminated with an @
|
|
|
|
if (Candidate.back() != '@')
|
|
|
|
return false;
|
|
|
|
Candidate = Candidate.dropBack();
|
|
|
|
|
|
|
|
// An encoded number starts with B-P and all subsequent digits are in A-P.
|
|
|
|
// Note that the reason the first digit cannot be A is two fold. First, it
|
|
|
|
// would create an ambiguity with ?A which delimits the beginning of an
|
|
|
|
// anonymous namespace. Second, A represents 0, and you don't start a multi
|
|
|
|
// digit number with a leading 0. Presumably the anonymous namespace
|
|
|
|
// ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
|
|
|
|
if (Candidate[0] < 'B' || Candidate[0] > 'P')
|
|
|
|
return false;
|
|
|
|
Candidate = Candidate.dropFront();
|
|
|
|
while (!Candidate.empty()) {
|
|
|
|
if (Candidate[0] < 'A' || Candidate[0] > 'P')
|
|
|
|
return false;
|
|
|
|
Candidate = Candidate.dropFront();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Write a function or template parameter list.
|
2018-08-03 01:08:03 +08:00
|
|
|
static void outputParameterList(OutputStream &OS, const FunctionParams &Params,
|
|
|
|
NameResolver &Resolver) {
|
2018-08-01 01:16:44 +08:00
|
|
|
if (!Params.Current) {
|
|
|
|
OS << "void";
|
2018-07-27 04:20:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
const FunctionParams *Head = &Params;
|
2018-07-21 01:27:48 +08:00
|
|
|
while (Head) {
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPre(OS, *Head->Current, Resolver);
|
|
|
|
Type::outputPost(OS, *Head->Current, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
Head = Head->Next;
|
|
|
|
|
|
|
|
if (Head)
|
|
|
|
OS << ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
static void outputStringLiteral(OutputStream &OS, const StringLiteral &Str) {
|
|
|
|
switch (Str.CharType) {
|
2018-08-17 00:17:36 +08:00
|
|
|
case PrimTy::Wchar:
|
|
|
|
OS << "const wchar_t * {L\"";
|
|
|
|
break;
|
|
|
|
case PrimTy::Char:
|
|
|
|
OS << "const char * {\"";
|
|
|
|
break;
|
|
|
|
case PrimTy::Char16:
|
|
|
|
OS << "const char16_t * {u\"";
|
|
|
|
break;
|
|
|
|
case PrimTy::Char32:
|
|
|
|
OS << "const char32_t * {U\"";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LLVM_BUILTIN_UNREACHABLE;
|
|
|
|
}
|
2018-08-18 00:14:05 +08:00
|
|
|
OS << Str.Str << "\"";
|
|
|
|
if (Str.IsTruncated)
|
2018-08-17 00:17:36 +08:00
|
|
|
OS << "...";
|
|
|
|
OS << "}";
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:04:56 +08:00
|
|
|
static void outputName(OutputStream &OS, const Name *TheName, const Type *Ty,
|
2018-08-03 01:08:03 +08:00
|
|
|
NameResolver &Resolver);
|
|
|
|
|
|
|
|
static void outputParameterList(OutputStream &OS, const TemplateParams &Params,
|
|
|
|
NameResolver &Resolver) {
|
2018-08-10 22:31:04 +08:00
|
|
|
if (Params.IsEmptyParameterPack) {
|
2018-08-01 01:16:44 +08:00
|
|
|
OS << "<>";
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
2018-08-01 01:16:44 +08:00
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
OS << "<";
|
2018-08-01 01:16:44 +08:00
|
|
|
const TemplateParams *Head = &Params;
|
|
|
|
while (Head) {
|
|
|
|
// Type can be null if this is a template template parameter,
|
|
|
|
// and Name can be null if this is a simple type.
|
|
|
|
|
2018-08-10 22:31:04 +08:00
|
|
|
if (Head->IsIntegerLiteral) {
|
|
|
|
if (Head->IntegerLiteralIsNegative)
|
|
|
|
OS << '-';
|
|
|
|
OS << Head->IntegralValue;
|
|
|
|
} else if (Head->PointerToSymbol || Head->ReferenceToSymbol) {
|
|
|
|
if (Head->PointerToSymbol)
|
|
|
|
OS << "&";
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPre(OS, *Head->ParamType, Resolver);
|
2018-08-10 23:04:56 +08:00
|
|
|
outputName(OS, Head->ParamName, Head->ParamType, Resolver);
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPost(OS, *Head->ParamType, Resolver);
|
2018-08-01 01:16:44 +08:00
|
|
|
} else if (Head->ParamType) {
|
|
|
|
// simple type.
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPre(OS, *Head->ParamType, Resolver);
|
|
|
|
Type::outputPost(OS, *Head->ParamType, Resolver);
|
2018-08-01 01:16:44 +08:00
|
|
|
} else {
|
|
|
|
// Template alias.
|
2018-08-10 23:04:56 +08:00
|
|
|
outputName(OS, Head->ParamName, Head->ParamType, Resolver);
|
2018-08-01 01:16:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Head = Head->Next;
|
|
|
|
|
|
|
|
if (Head)
|
|
|
|
OS << ", ";
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << ">";
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
static void outputNameComponent(OutputStream &OS, bool IsBackReference,
|
|
|
|
const TemplateParams *TParams, StringView Str,
|
2018-08-03 01:08:03 +08:00
|
|
|
NameResolver &Resolver) {
|
2018-08-18 00:14:05 +08:00
|
|
|
if (IsBackReference)
|
|
|
|
Str = Resolver.resolve(Str);
|
|
|
|
OS << Str;
|
2018-08-03 01:08:03 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
if (TParams)
|
|
|
|
outputParameterList(OS, *TParams, Resolver);
|
|
|
|
}
|
2018-08-03 01:08:03 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
static void outputNameComponent(OutputStream &OS, const Name &N,
|
|
|
|
NameResolver &Resolver) {
|
|
|
|
outputNameComponent(OS, N.IsBackReference, N.TParams, N.Str, Resolver);
|
2018-08-03 01:08:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 23:04:56 +08:00
|
|
|
static void outputName(OutputStream &OS, const Name *TheName, const Type *Ty,
|
2018-08-03 01:08:03 +08:00
|
|
|
NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
if (!TheName)
|
|
|
|
return;
|
|
|
|
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
|
2018-07-29 06:10:42 +08:00
|
|
|
const Name *Previous = nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
// Print out namespaces or outer class BackReferences.
|
|
|
|
for (; TheName->Next; TheName = TheName->Next) {
|
2018-07-29 06:10:42 +08:00
|
|
|
Previous = TheName;
|
2018-08-03 01:08:03 +08:00
|
|
|
outputNameComponent(OS, *TheName, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << "::";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out a regular name.
|
2018-08-02 02:32:47 +08:00
|
|
|
if (!TheName->IsOperator) {
|
2018-08-03 01:08:03 +08:00
|
|
|
outputNameComponent(OS, *TheName, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
const OperatorInfo &Operator = static_cast<const OperatorInfo &>(*TheName);
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Print out ctor or dtor.
|
2018-08-18 00:14:05 +08:00
|
|
|
switch (Operator.Info->Operator) {
|
|
|
|
case OperatorTy::Dtor:
|
2018-07-29 06:10:42 +08:00
|
|
|
OS << "~";
|
2018-08-18 00:14:05 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case OperatorTy::Ctor:
|
2018-08-03 01:08:03 +08:00
|
|
|
outputNameComponent(OS, *Previous, Resolver);
|
2018-08-18 00:14:05 +08:00
|
|
|
break;
|
|
|
|
case OperatorTy::Conversion:
|
2018-08-10 23:04:56 +08:00
|
|
|
OS << "operator";
|
|
|
|
if (TheName->IsTemplateInstantiation && TheName->TParams)
|
|
|
|
outputParameterList(OS, *TheName->TParams, Resolver);
|
|
|
|
OS << " ";
|
|
|
|
if (Ty) {
|
|
|
|
const FunctionType *FTy = static_cast<const FunctionType *>(Ty);
|
|
|
|
Type::outputPre(OS, *FTy->ReturnType, Resolver);
|
|
|
|
Type::outputPost(OS, *FTy->ReturnType, Resolver);
|
|
|
|
} else {
|
|
|
|
OS << "<conversion>";
|
|
|
|
}
|
2018-08-18 00:14:05 +08:00
|
|
|
break;
|
|
|
|
case OperatorTy::StringLiteral: {
|
|
|
|
const StringLiteral &SL = static_cast<const StringLiteral &>(Operator);
|
|
|
|
outputStringLiteral(OS, SL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OperatorTy::LiteralOperator:
|
|
|
|
OS << Operator.Info->Name;
|
2018-08-10 23:04:56 +08:00
|
|
|
outputNameComponent(OS, *TheName, Resolver);
|
2018-08-18 00:14:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OS << Operator.Info->Name;
|
|
|
|
if (Operator.IsTemplateInstantiation)
|
|
|
|
outputParameterList(OS, *Operator.TParams, Resolver);
|
|
|
|
break;
|
2018-08-10 23:04:56 +08:00
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
bool Name::isStringLiteralOperatorInfo() const {
|
|
|
|
if (!IsOperator)
|
|
|
|
return false;
|
|
|
|
const OperatorInfo &O = static_cast<const OperatorInfo &>(*this);
|
|
|
|
return O.Info->Operator == OperatorTy::StringLiteral;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Type *Type::clone(ArenaAllocator &Arena) const {
|
2018-07-21 02:35:06 +08:00
|
|
|
return Arena.alloc<Type>(*this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the "first half" of a given type.
|
2018-08-03 01:08:03 +08:00
|
|
|
void Type::outputPre(OutputStream &OS, Type &Ty, NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
// Function types require custom handling of const and static so we
|
|
|
|
// handle them separately. All other types use the same decoration
|
|
|
|
// for these modifiers, so handle them here in common code.
|
|
|
|
if (Ty.Prim == PrimTy::Function) {
|
2018-08-03 01:08:03 +08:00
|
|
|
Ty.outputPre(OS, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Ty.Storage) {
|
|
|
|
case StorageClass::PrivateStatic:
|
|
|
|
case StorageClass::PublicStatic:
|
|
|
|
case StorageClass::ProtectedStatic:
|
|
|
|
OS << "static ";
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-08-03 01:08:03 +08:00
|
|
|
Ty.outputPre(OS, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
if (Ty.Quals & Q_Const) {
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
OS << "const";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ty.Quals & Q_Volatile) {
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
OS << "volatile";
|
|
|
|
}
|
2018-07-27 04:25:35 +08:00
|
|
|
|
|
|
|
if (Ty.Quals & Q_Restrict) {
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
OS << "__restrict";
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the "second half" of a given type.
|
2018-08-03 01:08:03 +08:00
|
|
|
void Type::outputPost(OutputStream &OS, Type &Ty, NameResolver &Resolver) {
|
|
|
|
Ty.outputPost(OS, Resolver);
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void Type::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (Prim) {
|
|
|
|
case PrimTy::Void:
|
|
|
|
OS << "void";
|
|
|
|
break;
|
|
|
|
case PrimTy::Bool:
|
|
|
|
OS << "bool";
|
|
|
|
break;
|
|
|
|
case PrimTy::Char:
|
|
|
|
OS << "char";
|
|
|
|
break;
|
|
|
|
case PrimTy::Schar:
|
|
|
|
OS << "signed char";
|
|
|
|
break;
|
|
|
|
case PrimTy::Uchar:
|
|
|
|
OS << "unsigned char";
|
|
|
|
break;
|
2018-07-31 07:02:10 +08:00
|
|
|
case PrimTy::Char16:
|
|
|
|
OS << "char16_t";
|
|
|
|
break;
|
|
|
|
case PrimTy::Char32:
|
|
|
|
OS << "char32_t";
|
|
|
|
break;
|
2018-07-21 01:27:48 +08:00
|
|
|
case PrimTy::Short:
|
|
|
|
OS << "short";
|
|
|
|
break;
|
|
|
|
case PrimTy::Ushort:
|
|
|
|
OS << "unsigned short";
|
|
|
|
break;
|
|
|
|
case PrimTy::Int:
|
|
|
|
OS << "int";
|
|
|
|
break;
|
|
|
|
case PrimTy::Uint:
|
|
|
|
OS << "unsigned int";
|
|
|
|
break;
|
|
|
|
case PrimTy::Long:
|
|
|
|
OS << "long";
|
|
|
|
break;
|
|
|
|
case PrimTy::Ulong:
|
|
|
|
OS << "unsigned long";
|
|
|
|
break;
|
|
|
|
case PrimTy::Int64:
|
|
|
|
OS << "__int64";
|
|
|
|
break;
|
|
|
|
case PrimTy::Uint64:
|
|
|
|
OS << "unsigned __int64";
|
|
|
|
break;
|
|
|
|
case PrimTy::Wchar:
|
|
|
|
OS << "wchar_t";
|
|
|
|
break;
|
|
|
|
case PrimTy::Float:
|
|
|
|
OS << "float";
|
|
|
|
break;
|
|
|
|
case PrimTy::Double:
|
|
|
|
OS << "double";
|
|
|
|
break;
|
|
|
|
case PrimTy::Ldouble:
|
|
|
|
OS << "long double";
|
|
|
|
break;
|
2018-07-31 07:02:10 +08:00
|
|
|
case PrimTy::Nullptr:
|
|
|
|
OS << "std::nullptr_t";
|
|
|
|
break;
|
2018-08-18 00:14:05 +08:00
|
|
|
case PrimTy::Vbtable:
|
|
|
|
case PrimTy::Vftable:
|
|
|
|
break;
|
2018-07-21 01:27:48 +08:00
|
|
|
default:
|
|
|
|
assert(false && "Invalid primitive type!");
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 01:08:03 +08:00
|
|
|
void Type::outputPost(OutputStream &OS, NameResolver &Resolver) {}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
Type *PointerType::clone(ArenaAllocator &Arena) const {
|
2018-07-21 02:35:06 +08:00
|
|
|
return Arena.alloc<PointerType>(*this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:33:48 +08:00
|
|
|
static void outputPointerIndicator(OutputStream &OS, PointerAffinity Affinity,
|
2018-08-03 01:08:03 +08:00
|
|
|
const Name *MemberName, const Type *Pointee,
|
|
|
|
NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
// "[]" and "()" (for function parameters) take precedence over "*",
|
|
|
|
// so "int *x(int)" means "x is a function returning int *". We need
|
|
|
|
// parentheses to supercede the default precedence. (e.g. we want to
|
|
|
|
// emit something like "int (*x)(int)".)
|
2018-07-27 04:33:48 +08:00
|
|
|
if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array) {
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << "(";
|
2018-07-27 04:33:48 +08:00
|
|
|
if (Pointee->Prim == PrimTy::Function) {
|
|
|
|
const FunctionType *FTy = static_cast<const FunctionType *>(Pointee);
|
|
|
|
assert(FTy->IsFunctionPointer);
|
|
|
|
outputCallingConvention(OS, FTy->CallConvention);
|
|
|
|
OS << " ";
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 04:33:48 +08:00
|
|
|
if (MemberName) {
|
2018-08-10 23:04:56 +08:00
|
|
|
outputName(OS, MemberName, Pointee, Resolver);
|
2018-07-27 04:33:48 +08:00
|
|
|
OS << "::";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Affinity == PointerAffinity::Pointer)
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << "*";
|
2018-07-31 07:02:10 +08:00
|
|
|
else if (Affinity == PointerAffinity::Reference)
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << "&";
|
2018-07-31 07:02:10 +08:00
|
|
|
else
|
|
|
|
OS << "&&";
|
2018-07-27 04:33:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void PointerType::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
|
|
|
Type::outputPre(OS, *Pointee, Resolver);
|
2018-07-27 04:33:48 +08:00
|
|
|
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
|
|
|
|
if (Quals & Q_Unaligned)
|
|
|
|
OS << "__unaligned ";
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
outputPointerIndicator(OS, Affinity, nullptr, Pointee, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-21 02:07:33 +08:00
|
|
|
// FIXME: We should output this, but it requires updating lots of tests.
|
2018-07-21 01:27:48 +08:00
|
|
|
// if (Ty.Quals & Q_Pointer64)
|
|
|
|
// OS << " __ptr64";
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void PointerType::outputPost(OutputStream &OS, NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
|
|
|
|
OS << ")";
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPost(OS, *Pointee, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
Type *MemberPointerType::clone(ArenaAllocator &Arena) const {
|
|
|
|
return Arena.alloc<MemberPointerType>(*this);
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void MemberPointerType::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
|
|
|
Type::outputPre(OS, *Pointee, Resolver);
|
2018-07-27 03:56:09 +08:00
|
|
|
|
|
|
|
outputSpaceIfNecessary(OS);
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
outputPointerIndicator(OS, PointerAffinity::Pointer, MemberName, Pointee,
|
|
|
|
Resolver);
|
2018-07-27 03:56:09 +08:00
|
|
|
|
|
|
|
// FIXME: We should output this, but it requires updating lots of tests.
|
|
|
|
// if (Ty.Quals & Q_Pointer64)
|
|
|
|
// OS << " __ptr64";
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void MemberPointerType::outputPost(OutputStream &OS, NameResolver &Resolver) {
|
2018-07-27 03:56:09 +08:00
|
|
|
if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
|
|
|
|
OS << ")";
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPost(OS, *Pointee, Resolver);
|
2018-07-27 03:56:09 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Type *FunctionType::clone(ArenaAllocator &Arena) const {
|
2018-07-21 02:35:06 +08:00
|
|
|
return Arena.alloc<FunctionType>(*this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void FunctionType::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
if (!(FunctionClass & Global)) {
|
|
|
|
if (FunctionClass & Static)
|
|
|
|
OS << "static ";
|
|
|
|
}
|
2018-08-18 00:14:05 +08:00
|
|
|
if (FunctionClass & ExternC)
|
2018-08-11 05:09:05 +08:00
|
|
|
OS << "extern \"C\" ";
|
2018-08-18 00:14:05 +08:00
|
|
|
|
|
|
|
if (FunctionClass & Virtual)
|
|
|
|
OS << "virtual ";
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 04:20:10 +08:00
|
|
|
if (ReturnType) {
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPre(OS, *ReturnType, Resolver);
|
2018-07-27 04:20:10 +08:00
|
|
|
OS << " ";
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 04:33:48 +08:00
|
|
|
// Function pointers print the calling convention as void (__cdecl *)(params)
|
|
|
|
// rather than void __cdecl (*)(params). So we need to let the PointerType
|
|
|
|
// class handle this.
|
|
|
|
if (!IsFunctionPointer)
|
|
|
|
outputCallingConvention(OS, CallConvention);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void FunctionType::outputPost(OutputStream &OS, NameResolver &Resolver) {
|
2018-08-11 05:09:05 +08:00
|
|
|
// extern "C" functions don't have a prototype.
|
|
|
|
if (FunctionClass & NoPrototype)
|
|
|
|
return;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << "(";
|
2018-08-03 01:08:03 +08:00
|
|
|
outputParameterList(OS, Params, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
OS << ")";
|
|
|
|
if (Quals & Q_Const)
|
|
|
|
OS << " const";
|
|
|
|
if (Quals & Q_Volatile)
|
|
|
|
OS << " volatile";
|
2018-07-31 07:02:10 +08:00
|
|
|
if (Quals & Q_Restrict)
|
|
|
|
OS << " __restrict";
|
|
|
|
if (Quals & Q_Unaligned)
|
|
|
|
OS << " __unaligned";
|
|
|
|
|
|
|
|
if (RefKind == ReferenceKind::LValueRef)
|
|
|
|
OS << " &";
|
|
|
|
else if (RefKind == ReferenceKind::RValueRef)
|
|
|
|
OS << " &&";
|
2018-07-27 04:20:10 +08:00
|
|
|
|
|
|
|
if (ReturnType)
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPost(OS, *ReturnType, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type *UdtType::clone(ArenaAllocator &Arena) const {
|
2018-07-21 02:35:06 +08:00
|
|
|
return Arena.alloc<UdtType>(*this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void UdtType::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (Prim) {
|
|
|
|
case PrimTy::Class:
|
|
|
|
OS << "class ";
|
|
|
|
break;
|
|
|
|
case PrimTy::Struct:
|
|
|
|
OS << "struct ";
|
|
|
|
break;
|
|
|
|
case PrimTy::Union:
|
|
|
|
OS << "union ";
|
|
|
|
break;
|
|
|
|
case PrimTy::Enum:
|
|
|
|
OS << "enum ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "Not a udt type!");
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:04:56 +08:00
|
|
|
outputName(OS, UdtName, this, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Type *ArrayType::clone(ArenaAllocator &Arena) const {
|
2018-07-21 02:35:06 +08:00
|
|
|
return Arena.alloc<ArrayType>(*this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void ArrayType::outputPre(OutputStream &OS, NameResolver &Resolver) {
|
|
|
|
Type::outputPre(OS, *ElementType, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
void ArrayType::outputPost(OutputStream &OS, NameResolver &Resolver) {
|
2018-08-10 22:31:04 +08:00
|
|
|
ArrayDimension *D = Dims;
|
|
|
|
while (D) {
|
|
|
|
OS << "[";
|
|
|
|
if (D->Dim > 0)
|
|
|
|
OS << D->Dim;
|
|
|
|
OS << "]";
|
|
|
|
D = D->Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type::outputPost(OS, *ElementType, Resolver);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
struct Symbol {
|
2018-08-02 02:32:47 +08:00
|
|
|
SymbolCategory Category;
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *SymbolName = nullptr;
|
|
|
|
Type *SymbolType = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-09 01:17:04 +08:00
|
|
|
struct BackrefContext {
|
|
|
|
static constexpr size_t Max = 10;
|
|
|
|
|
|
|
|
Type *FunctionParams[Max];
|
|
|
|
size_t FunctionParamCount = 0;
|
|
|
|
|
|
|
|
// The first 10 BackReferences in a mangled name can be back-referenced by
|
|
|
|
// special name @[0-9]. This is a storage for the first 10 BackReferences.
|
|
|
|
StringView Names[Max];
|
|
|
|
size_t NamesCount = 0;
|
|
|
|
};
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Demangler class takes the main role in demangling symbols.
|
|
|
|
// It has a set of functions to parse mangled symbols into Type instances.
|
|
|
|
// It also has a set of functions to cnovert Type instances to strings.
|
2018-08-03 01:08:03 +08:00
|
|
|
class Demangler : public NameResolver {
|
2018-07-21 01:27:48 +08:00
|
|
|
public:
|
2018-07-30 00:38:02 +08:00
|
|
|
Demangler() = default;
|
2018-08-03 01:18:01 +08:00
|
|
|
virtual ~Demangler() = default;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// You are supposed to call parse() first and then check if error is true. If
|
|
|
|
// it is false, call output() to write the formatted name to the given stream.
|
2018-07-30 00:38:02 +08:00
|
|
|
Symbol *parse(StringView &MangledName);
|
|
|
|
void output(const Symbol *S, OutputStream &OS);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
StringView resolve(StringView N) override;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// True if an error occurred.
|
|
|
|
bool Error = false;
|
|
|
|
|
2018-08-02 02:33:04 +08:00
|
|
|
void dumpBackReferences();
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
private:
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *demangleVariableEncoding(StringView &MangledName);
|
|
|
|
Type *demangleFunctionEncoding(StringView &MangledName);
|
2018-08-18 00:14:05 +08:00
|
|
|
Type *demangleVtableEncoding(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Qualifiers demanglePointerExtQualifiers(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Parser functions. This is a recursive-descent parser.
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *demangleType(StringView &MangledName, QualifierMangleMode QMM);
|
|
|
|
Type *demangleBasicType(StringView &MangledName);
|
|
|
|
UdtType *demangleClassType(StringView &MangledName);
|
|
|
|
PointerType *demanglePointerType(StringView &MangledName);
|
|
|
|
MemberPointerType *demangleMemberPointerType(StringView &MangledName);
|
|
|
|
FunctionType *demangleFunctionType(StringView &MangledName, bool HasThisQuals,
|
|
|
|
bool IsFunctionPointer);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
ArrayType *demangleArrayType(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
TemplateParams *demangleTemplateParameterList(StringView &MangledName);
|
|
|
|
FunctionParams demangleFunctionParameterList(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-10 22:31:04 +08:00
|
|
|
std::pair<uint64_t, bool> demangleNumber(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
void memorizeString(StringView s);
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
|
|
|
|
/// Allocate a copy of \p Borrowed into memory that we own.
|
|
|
|
StringView copyString(StringView Borrowed);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *demangleFullyQualifiedTypeName(StringView &MangledName);
|
|
|
|
Name *demangleFullyQualifiedSymbolName(StringView &MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
Name *demangleUnqualifiedTypeName(StringView &MangledName, bool Memorize);
|
2018-08-08 08:43:31 +08:00
|
|
|
Name *demangleUnqualifiedSymbolName(StringView &MangledName,
|
|
|
|
NameBackrefBehavior NBB);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *demangleNameScopeChain(StringView &MangledName, Name *UnqualifiedName);
|
|
|
|
Name *demangleNameScopePiece(StringView &MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *demangleBackRefName(StringView &MangledName);
|
2018-08-08 08:43:31 +08:00
|
|
|
Name *demangleTemplateInstantiationName(StringView &MangledName,
|
|
|
|
NameBackrefBehavior NBB);
|
2018-08-18 00:14:05 +08:00
|
|
|
OperatorInfo *demangleOperatorName(StringView &MangledName);
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *demangleSimpleName(StringView &MangledName, bool Memorize);
|
|
|
|
Name *demangleAnonymousNamespaceName(StringView &MangledName);
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
Name *demangleLocallyScopedNamePiece(StringView &MangledName);
|
2018-08-18 00:14:05 +08:00
|
|
|
StringLiteral *demangleStringLiteral(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
StringView demangleSimpleString(StringView &MangledName, bool Memorize);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
FuncClass demangleFunctionClass(StringView &MangledName);
|
|
|
|
CallingConv demangleCallingConvention(StringView &MangledName);
|
|
|
|
StorageClass demangleVariableStorageClass(StringView &MangledName);
|
|
|
|
ReferenceKind demangleReferenceKind(StringView &MangledName);
|
|
|
|
void demangleThrowSpecification(StringView &MangledName);
|
2018-08-17 00:17:36 +08:00
|
|
|
wchar_t demangleWcharLiteral(StringView &MangledName);
|
|
|
|
uint8_t demangleCharLiteral(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
std::pair<Qualifiers, bool> demangleQualifiers(StringView &MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// Memory allocator.
|
|
|
|
ArenaAllocator Arena;
|
|
|
|
|
2018-07-27 06:13:39 +08:00
|
|
|
// A single type uses one global back-ref table for all function params.
|
|
|
|
// This means back-refs can even go "into" other types. Examples:
|
|
|
|
//
|
|
|
|
// // Second int* is a back-ref to first.
|
|
|
|
// void foo(int *, int*);
|
|
|
|
//
|
|
|
|
// // Second int* is not a back-ref to first (first is not a function param).
|
|
|
|
// int* foo(int*);
|
|
|
|
//
|
|
|
|
// // Second int* is a back-ref to first (ALL function types share the same
|
|
|
|
// // back-ref map.
|
|
|
|
// using F = void(*)(int*);
|
|
|
|
// F G(int *);
|
2018-08-09 01:17:04 +08:00
|
|
|
BackrefContext Backrefs;
|
2018-07-21 01:27:48 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
StringView Demangler::copyString(StringView Borrowed) {
|
|
|
|
char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
|
|
|
|
std::strcpy(Stable, Borrowed.begin());
|
|
|
|
|
|
|
|
return {Stable, Borrowed.size()};
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Parser entry point.
|
2018-07-30 00:38:02 +08:00
|
|
|
Symbol *Demangler::parse(StringView &MangledName) {
|
|
|
|
Symbol *S = Arena.alloc<Symbol>();
|
|
|
|
|
2018-08-17 00:17:17 +08:00
|
|
|
// We can't demangle MD5 names, just output them as-is.
|
|
|
|
if (MangledName.startsWith("??@")) {
|
|
|
|
S->Category = SymbolCategory::Unknown;
|
|
|
|
S->SymbolName = Arena.alloc<Name>();
|
|
|
|
S->SymbolName->Str = MangledName;
|
2018-08-17 00:17:36 +08:00
|
|
|
S->SymbolType = nullptr;
|
2018-08-17 00:17:17 +08:00
|
|
|
MangledName = StringView();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// MSVC-style mangled symbols must start with '?'.
|
|
|
|
if (!MangledName.consumeFront("?")) {
|
2018-08-17 00:17:36 +08:00
|
|
|
S->Category = SymbolCategory::Unknown;
|
2018-07-30 00:38:02 +08:00
|
|
|
S->SymbolName = Arena.alloc<Name>();
|
|
|
|
S->SymbolName->Str = MangledName;
|
2018-08-17 00:17:36 +08:00
|
|
|
S->SymbolType = nullptr;
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// What follows is a main symbol name. This may include
|
|
|
|
// namespaces or class BackReferences.
|
2018-07-30 00:38:02 +08:00
|
|
|
S->SymbolName = demangleFullyQualifiedSymbolName(MangledName);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-08-18 00:14:05 +08:00
|
|
|
|
|
|
|
if (S->SymbolName->isStringLiteralOperatorInfo())
|
|
|
|
return S;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Read a variable.
|
2018-08-18 00:14:05 +08:00
|
|
|
switch (MangledName.front()) {
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
2018-08-02 02:32:47 +08:00
|
|
|
S->Category = SymbolCategory::Variable;
|
|
|
|
S->SymbolType = demangleVariableEncoding(MangledName);
|
2018-08-18 00:14:05 +08:00
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
S->Category = SymbolCategory::Variable;
|
|
|
|
S->SymbolType = demangleVtableEncoding(MangledName);
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-02 02:32:47 +08:00
|
|
|
S->Category = SymbolCategory::Function;
|
|
|
|
S->SymbolType = demangleFunctionEncoding(MangledName);
|
|
|
|
}
|
|
|
|
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
return S;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
Type *Demangler::demangleVtableEncoding(StringView &MangledName) {
|
|
|
|
Type *Ty = Arena.alloc<Type>();
|
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case '6':
|
|
|
|
Ty->Prim = PrimTy::Vftable;
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
Ty->Prim = PrimTy::Vbtable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bool IsMember = false;
|
|
|
|
std::tie(Ty->Quals, IsMember) = demangleQualifiers(MangledName);
|
|
|
|
Ty->Storage = StorageClass::None;
|
|
|
|
MangledName.consumeFront('@');
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// <type-encoding> ::= <storage-class> <variable-type>
|
|
|
|
// <storage-class> ::= 0 # private static member
|
|
|
|
// ::= 1 # protected static member
|
|
|
|
// ::= 2 # public static member
|
|
|
|
// ::= 3 # global
|
|
|
|
// ::= 4 # static local
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *Demangler::demangleVariableEncoding(StringView &MangledName) {
|
|
|
|
StorageClass SC = demangleVariableStorageClass(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *Ty = demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
Ty->Storage = SC;
|
|
|
|
|
|
|
|
// <variable-type> ::= <type> <cvr-qualifiers>
|
|
|
|
// ::= <type> <pointee-cvr-qualifiers> # pointers, references
|
|
|
|
switch (Ty->Prim) {
|
|
|
|
case PrimTy::Ptr:
|
2018-07-27 03:56:09 +08:00
|
|
|
case PrimTy::MemberPtr: {
|
2018-07-21 01:27:48 +08:00
|
|
|
Qualifiers ExtraChildQuals = Q_None;
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty->Quals =
|
|
|
|
Qualifiers(Ty->Quals | demanglePointerExtQualifiers(MangledName));
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
bool IsMember = false;
|
2018-07-30 00:38:02 +08:00
|
|
|
std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
if (Ty->Prim == PrimTy::MemberPtr) {
|
|
|
|
assert(IsMember);
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *BackRefName = demangleFullyQualifiedTypeName(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
(void)BackRefName;
|
2018-07-27 03:56:09 +08:00
|
|
|
MemberPointerType *MPTy = static_cast<MemberPointerType *>(Ty);
|
|
|
|
MPTy->Pointee->Quals = Qualifiers(MPTy->Pointee->Quals | ExtraChildQuals);
|
|
|
|
} else {
|
|
|
|
PointerType *PTy = static_cast<PointerType *>(Ty);
|
|
|
|
PTy->Pointee->Quals = Qualifiers(PTy->Pointee->Quals | ExtraChildQuals);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty->Quals = demangleQualifiers(MangledName).first;
|
2018-07-21 01:27:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sometimes numbers are encoded in mangled symbols. For example,
|
|
|
|
// "int (*x)[20]" is a valid C type (x is a pointer to an array of
|
|
|
|
// length 20), so we need some way to embed numbers as part of symbols.
|
|
|
|
// This function parses it.
|
|
|
|
//
|
|
|
|
// <number> ::= [?] <non-negative integer>
|
|
|
|
//
|
|
|
|
// <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
|
|
|
|
// ::= <hex digit>+ @ # when Numbrer == 0 or >= 10
|
|
|
|
//
|
|
|
|
// <hex-digit> ::= [A-P] # A = 0, B = 1, ...
|
2018-08-10 22:31:04 +08:00
|
|
|
std::pair<uint64_t, bool> Demangler::demangleNumber(StringView &MangledName) {
|
|
|
|
bool IsNegative = MangledName.consumeFront('?');
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
if (startsWithDigit(MangledName)) {
|
2018-08-10 22:31:04 +08:00
|
|
|
uint64_t Ret = MangledName[0] - '0' + 1;
|
2018-07-21 01:27:48 +08:00
|
|
|
MangledName = MangledName.dropFront(1);
|
2018-08-10 22:31:04 +08:00
|
|
|
return {Ret, IsNegative};
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 22:31:04 +08:00
|
|
|
uint64_t Ret = 0;
|
2018-07-21 01:27:48 +08:00
|
|
|
for (size_t i = 0; i < MangledName.size(); ++i) {
|
|
|
|
char C = MangledName[i];
|
|
|
|
if (C == '@') {
|
|
|
|
MangledName = MangledName.dropFront(i + 1);
|
2018-08-10 22:31:04 +08:00
|
|
|
return {Ret, IsNegative};
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
if ('A' <= C && C <= 'P') {
|
|
|
|
Ret = (Ret << 4) + (C - 'A');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error = true;
|
2018-08-10 22:31:04 +08:00
|
|
|
return {0ULL, false};
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
|
|
|
|
// Memorize it.
|
|
|
|
void Demangler::memorizeString(StringView S) {
|
2018-08-09 01:17:04 +08:00
|
|
|
if (Backrefs.NamesCount >= BackrefContext::Max)
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
2018-08-09 01:17:04 +08:00
|
|
|
for (size_t i = 0; i < Backrefs.NamesCount; ++i)
|
|
|
|
if (S == Backrefs.Names[i])
|
2018-07-21 01:27:48 +08:00
|
|
|
return;
|
2018-08-09 01:17:04 +08:00
|
|
|
Backrefs.Names[Backrefs.NamesCount++] = S;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleBackRefName(StringView &MangledName) {
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(startsWithDigit(MangledName));
|
|
|
|
Name *Node = Arena.alloc<Name>();
|
2018-08-03 01:08:03 +08:00
|
|
|
Node->IsBackReference = true;
|
|
|
|
Node->Str = {MangledName.begin(), 1};
|
|
|
|
MangledName = MangledName.dropFront();
|
2018-07-29 06:10:42 +08:00
|
|
|
return Node;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 08:43:31 +08:00
|
|
|
Name *Demangler::demangleTemplateInstantiationName(StringView &MangledName,
|
|
|
|
NameBackrefBehavior NBB) {
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(MangledName.startsWith("?$"));
|
|
|
|
MangledName.consumeFront("?$");
|
|
|
|
|
2018-08-09 01:17:04 +08:00
|
|
|
BackrefContext OuterContext;
|
|
|
|
std::swap(OuterContext, Backrefs);
|
|
|
|
|
2018-08-08 08:43:31 +08:00
|
|
|
Name *Node = demangleUnqualifiedSymbolName(MangledName, NBB_None);
|
2018-08-09 01:17:04 +08:00
|
|
|
if (!Error)
|
|
|
|
Node->TParams = demangleTemplateParameterList(MangledName);
|
2018-08-02 02:32:28 +08:00
|
|
|
|
2018-08-09 01:17:04 +08:00
|
|
|
std::swap(OuterContext, Backrefs);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
Node->IsTemplateInstantiation = true;
|
|
|
|
|
2018-08-08 08:43:31 +08:00
|
|
|
if (NBB & NBB_Template) {
|
|
|
|
// Render this class template name into a string buffer so that we can
|
|
|
|
// memorize it for the purpose of back-referencing.
|
|
|
|
OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
|
2018-08-10 23:04:56 +08:00
|
|
|
outputName(OS, Node, nullptr, *this);
|
2018-08-08 08:43:31 +08:00
|
|
|
OS << '\0';
|
|
|
|
char *Name = OS.getBuffer();
|
|
|
|
|
|
|
|
StringView Owned = copyString(Name);
|
|
|
|
memorizeString(Owned);
|
|
|
|
std::free(Name);
|
|
|
|
}
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
|
2018-07-29 06:10:42 +08:00
|
|
|
return Node;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
OperatorInfo *Demangler::demangleOperatorName(StringView &MangledName) {
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(MangledName.startsWith('?'));
|
|
|
|
MangledName.consumeFront('?');
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
const OperatorMapEntry *Entry = nullptr;
|
|
|
|
for (const auto &MapEntry : OperatorMap) {
|
|
|
|
if (!MangledName.consumeFront(MapEntry.Prefix))
|
|
|
|
continue;
|
|
|
|
Entry = &MapEntry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!Entry) {
|
2018-07-29 06:10:42 +08:00
|
|
|
Error = true;
|
2018-08-18 00:14:05 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
OperatorInfo *Oper = nullptr;
|
|
|
|
switch (Entry->Operator) {
|
|
|
|
case OperatorTy::StringLiteral:
|
|
|
|
Oper = demangleStringLiteral(MangledName);
|
|
|
|
break;
|
|
|
|
case OperatorTy::LiteralOperator:
|
|
|
|
Oper = Arena.alloc<OperatorInfo>();
|
|
|
|
Oper->Str = demangleSimpleString(MangledName, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Oper = Arena.alloc<OperatorInfo>();
|
2018-08-10 23:04:56 +08:00
|
|
|
}
|
2018-08-18 00:14:05 +08:00
|
|
|
|
|
|
|
Oper->Info = Entry;
|
|
|
|
Oper->IsOperator = true;
|
2018-08-17 00:17:36 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
return Oper;
|
2018-07-29 06:10:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleSimpleName(StringView &MangledName, bool Memorize) {
|
2018-07-31 07:02:10 +08:00
|
|
|
StringView S = demangleSimpleString(MangledName, Memorize);
|
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-07-29 06:10:42 +08:00
|
|
|
Name *Node = Arena.alloc<Name>();
|
2018-07-31 07:02:10 +08:00
|
|
|
Node->Str = S;
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:17:36 +08:00
|
|
|
static bool isRebasedHexDigit(char C) { return (C >= 'A' && C <= 'P'); }
|
|
|
|
|
|
|
|
static uint8_t rebasedHexDigitToNumber(char C) {
|
|
|
|
assert(isRebasedHexDigit(C));
|
|
|
|
return (C <= 'J') ? (C - 'A') : (10 + C - 'K');
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Demangler::demangleCharLiteral(StringView &MangledName) {
|
|
|
|
if (!MangledName.startsWith('?'))
|
|
|
|
return MangledName.popFront();
|
|
|
|
|
|
|
|
MangledName = MangledName.dropFront();
|
|
|
|
if (MangledName.empty())
|
|
|
|
goto CharLiteralError;
|
|
|
|
|
|
|
|
if (MangledName.consumeFront('$')) {
|
|
|
|
// Two hex digits
|
|
|
|
if (MangledName.size() < 2)
|
|
|
|
goto CharLiteralError;
|
|
|
|
StringView Nibbles = MangledName.substr(0, 2);
|
|
|
|
if (!isRebasedHexDigit(Nibbles[0]) || !isRebasedHexDigit(Nibbles[1]))
|
|
|
|
goto CharLiteralError;
|
|
|
|
// Don't append the null terminator.
|
|
|
|
uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]);
|
|
|
|
uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]);
|
|
|
|
MangledName = MangledName.dropFront(2);
|
|
|
|
return (C1 << 4) | C2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startsWithDigit(MangledName)) {
|
|
|
|
const char *Lookup = ",/\\:. \n\t'-";
|
|
|
|
char C = Lookup[MangledName[0] - '0'];
|
|
|
|
MangledName = MangledName.dropFront();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MangledName[0] >= 'a' && MangledName[0] <= 'z') {
|
|
|
|
char Lookup[26] = {'\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7',
|
|
|
|
'\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE',
|
|
|
|
'\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5',
|
|
|
|
'\xF6', '\xF7', '\xF8', '\xF9', '\xFA'};
|
|
|
|
char C = Lookup[MangledName[0] - 'a'];
|
|
|
|
MangledName = MangledName.dropFront();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MangledName[0] >= 'A' && MangledName[0] <= 'Z') {
|
|
|
|
char Lookup[26] = {'\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7',
|
|
|
|
'\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE',
|
|
|
|
'\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5',
|
|
|
|
'\xD6', '\xD7', '\xD8', '\xD9', '\xDA'};
|
|
|
|
char C = Lookup[MangledName[0] - 'A'];
|
|
|
|
MangledName = MangledName.dropFront();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharLiteralError:
|
|
|
|
Error = true;
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t Demangler::demangleWcharLiteral(StringView &MangledName) {
|
2018-08-17 00:30:27 +08:00
|
|
|
uint8_t C1, C2;
|
|
|
|
|
|
|
|
C1 = demangleCharLiteral(MangledName);
|
2018-08-17 00:17:36 +08:00
|
|
|
if (Error)
|
|
|
|
goto WCharLiteralError;
|
2018-08-17 00:30:27 +08:00
|
|
|
C2 = demangleCharLiteral(MangledName);
|
2018-08-17 00:17:36 +08:00
|
|
|
if (Error)
|
|
|
|
goto WCharLiteralError;
|
|
|
|
|
|
|
|
return ((wchar_t)C1 << 8) | (wchar_t)C2;
|
|
|
|
|
|
|
|
WCharLiteralError:
|
|
|
|
Error = true;
|
|
|
|
return L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeHexDigit(char *Buffer, uint8_t Digit) {
|
|
|
|
assert(Digit <= 15);
|
|
|
|
*Buffer = (Digit < 10) ? ('0' + Digit) : ('A' + Digit - 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void outputHex(OutputStream &OS, unsigned C) {
|
|
|
|
if (C == 0) {
|
|
|
|
OS << "\\x00";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// It's easier to do the math if we can work from right to left, but we need
|
|
|
|
// to print the numbers from left to right. So render this into a temporary
|
|
|
|
// buffer first, then output the temporary buffer. Each byte is of the form
|
|
|
|
// \xAB, which means that each byte needs 4 characters. Since there are at
|
|
|
|
// most 4 bytes, we need a 4*4+1 = 17 character temporary buffer.
|
|
|
|
char TempBuffer[17];
|
|
|
|
|
|
|
|
::memset(TempBuffer, 0, sizeof(TempBuffer));
|
|
|
|
constexpr int MaxPos = 15;
|
|
|
|
|
|
|
|
int Pos = MaxPos - 1;
|
|
|
|
while (C != 0) {
|
|
|
|
for (int I = 0; I < 2; ++I) {
|
|
|
|
writeHexDigit(&TempBuffer[Pos--], C % 16);
|
|
|
|
C /= 16;
|
|
|
|
}
|
|
|
|
TempBuffer[Pos--] = 'x';
|
|
|
|
TempBuffer[Pos--] = '\\';
|
|
|
|
assert(Pos >= 0);
|
|
|
|
}
|
|
|
|
OS << StringView(&TempBuffer[Pos + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void outputEscapedChar(OutputStream &OS, unsigned C) {
|
|
|
|
switch (C) {
|
|
|
|
case '\'': // single quote
|
|
|
|
OS << "\\\'";
|
|
|
|
return;
|
|
|
|
case '\"': // double quote
|
|
|
|
OS << "\\\"";
|
|
|
|
return;
|
|
|
|
case '\\': // backslash
|
|
|
|
OS << "\\\\";
|
|
|
|
return;
|
|
|
|
case '\a': // bell
|
|
|
|
OS << "\\a";
|
|
|
|
return;
|
|
|
|
case '\b': // backspace
|
|
|
|
OS << "\\b";
|
|
|
|
return;
|
|
|
|
case '\f': // form feed
|
|
|
|
OS << "\\f";
|
|
|
|
return;
|
|
|
|
case '\n': // new line
|
|
|
|
OS << "\\n";
|
|
|
|
return;
|
|
|
|
case '\r': // carriage return
|
|
|
|
OS << "\\r";
|
|
|
|
return;
|
|
|
|
case '\t': // tab
|
|
|
|
OS << "\\t";
|
|
|
|
return;
|
|
|
|
case '\v': // vertical tab
|
|
|
|
OS << "\\v";
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C > 0x1F && C < 0x7F) {
|
|
|
|
// Standard ascii char.
|
|
|
|
OS << (char)C;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputHex(OS, C);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length) {
|
|
|
|
const uint8_t *End = StringBytes + Length - 1;
|
2018-08-18 00:14:05 +08:00
|
|
|
unsigned Count = 0;
|
2018-08-17 00:17:36 +08:00
|
|
|
while (Length > 0 && *End == 0) {
|
|
|
|
--Length;
|
|
|
|
--End;
|
2018-08-18 00:14:05 +08:00
|
|
|
++Count;
|
2018-08-17 00:17:36 +08:00
|
|
|
}
|
2018-08-18 00:14:05 +08:00
|
|
|
return Count;
|
2018-08-17 00:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned countEmbeddedNulls(const uint8_t *StringBytes, unsigned Length) {
|
|
|
|
unsigned Result = 0;
|
|
|
|
for (unsigned I = 0; I < Length; ++I) {
|
|
|
|
if (*StringBytes++ == 0)
|
|
|
|
++Result;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars,
|
|
|
|
unsigned NumBytes) {
|
|
|
|
assert(NumBytes > 0);
|
|
|
|
|
|
|
|
// If the number of bytes is odd, this is guaranteed to be a char string.
|
|
|
|
if (NumBytes % 2 == 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// All strings can encode at most 32 bytes of data. If it's less than that,
|
|
|
|
// then we encoded the entire string. In this case we check for a 1-byte,
|
|
|
|
// 2-byte, or 4-byte null terminator.
|
|
|
|
if (NumBytes < 32) {
|
|
|
|
unsigned TrailingNulls = countTrailingNullBytes(StringBytes, NumChars);
|
|
|
|
if (TrailingNulls >= 4)
|
|
|
|
return 4;
|
|
|
|
if (TrailingNulls >= 2)
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The whole string was not able to be encoded. Try to look at embedded null
|
|
|
|
// terminators to guess. The heuristic is that we count all embedded null
|
|
|
|
// terminators. If more than 2/3 are null, it's a char32. If more than 1/3
|
|
|
|
// are null, it's a char16. Otherwise it's a char8. This obviously isn't
|
|
|
|
// perfect and is biased towards languages that have ascii alphabets, but this
|
|
|
|
// was always going to be best effort since the encoding is lossy.
|
|
|
|
unsigned Nulls = countEmbeddedNulls(StringBytes, NumChars);
|
|
|
|
if (Nulls >= 2 * NumChars / 3)
|
|
|
|
return 4;
|
|
|
|
if (Nulls >= NumChars / 3)
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned decodeMultiByteChar(const uint8_t *StringBytes,
|
|
|
|
unsigned CharIndex, unsigned CharBytes) {
|
|
|
|
assert(CharBytes == 1 || CharBytes == 2 || CharBytes == 4);
|
|
|
|
unsigned Offset = CharIndex * CharBytes;
|
|
|
|
unsigned Result = 0;
|
|
|
|
StringBytes = StringBytes + Offset;
|
|
|
|
for (unsigned I = 0; I < CharBytes; ++I) {
|
|
|
|
unsigned C = static_cast<unsigned>(StringBytes[I]);
|
|
|
|
Result |= C << (8 * I);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
StringLiteral *Demangler::demangleStringLiteral(StringView &MangledName) {
|
2018-08-17 00:30:27 +08:00
|
|
|
// This function uses goto, so declare all variables up front.
|
2018-08-17 00:17:36 +08:00
|
|
|
OutputStream OS;
|
|
|
|
StringView CRC;
|
2018-08-17 00:30:27 +08:00
|
|
|
uint64_t StringByteSize;
|
|
|
|
bool IsWcharT = false;
|
|
|
|
bool IsNegative = false;
|
|
|
|
size_t CrcEndPos = 0;
|
|
|
|
char *ResultBuffer = nullptr;
|
|
|
|
|
2018-08-18 00:14:05 +08:00
|
|
|
StringLiteral *Result = Arena.alloc<StringLiteral>();
|
2018-08-17 00:17:36 +08:00
|
|
|
|
|
|
|
// Prefix indicating the beginning of a string literal
|
2018-08-18 00:14:05 +08:00
|
|
|
if (!MangledName.consumeFront("@_"))
|
|
|
|
goto StringLiteralError;
|
2018-08-17 00:17:36 +08:00
|
|
|
if (MangledName.empty())
|
|
|
|
goto StringLiteralError;
|
|
|
|
|
|
|
|
// Char Type (regular or wchar_t)
|
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case '1':
|
|
|
|
IsWcharT = true;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case '0':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto StringLiteralError;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encoded Length
|
|
|
|
std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName);
|
|
|
|
if (Error || IsNegative)
|
|
|
|
goto StringLiteralError;
|
|
|
|
|
|
|
|
// CRC 32 (always 8 characters plus a terminator)
|
2018-08-17 00:30:27 +08:00
|
|
|
CrcEndPos = MangledName.find('@');
|
2018-08-17 00:17:36 +08:00
|
|
|
if (CrcEndPos == StringView::npos)
|
|
|
|
goto StringLiteralError;
|
|
|
|
CRC = MangledName.substr(0, CrcEndPos);
|
|
|
|
MangledName = MangledName.dropFront(CrcEndPos + 1);
|
|
|
|
if (MangledName.empty())
|
|
|
|
goto StringLiteralError;
|
|
|
|
|
|
|
|
OS = OutputStream::create(nullptr, nullptr, 1024);
|
|
|
|
if (IsWcharT) {
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->CharType = PrimTy::Wchar;
|
2018-08-17 00:17:36 +08:00
|
|
|
if (StringByteSize > 64)
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->IsTruncated = true;
|
2018-08-17 00:17:36 +08:00
|
|
|
|
|
|
|
while (!MangledName.consumeFront('@')) {
|
|
|
|
assert(StringByteSize >= 2);
|
|
|
|
wchar_t W = demangleWcharLiteral(MangledName);
|
2018-08-18 00:14:05 +08:00
|
|
|
if (StringByteSize != 2 || Result->IsTruncated)
|
2018-08-17 00:17:36 +08:00
|
|
|
outputEscapedChar(OS, W);
|
|
|
|
StringByteSize -= 2;
|
|
|
|
if (Error)
|
|
|
|
goto StringLiteralError;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (StringByteSize > 32)
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->IsTruncated = true;
|
2018-08-17 00:17:36 +08:00
|
|
|
|
|
|
|
constexpr unsigned MaxStringByteLength = 32;
|
|
|
|
uint8_t StringBytes[MaxStringByteLength];
|
|
|
|
|
|
|
|
unsigned BytesDecoded = 0;
|
|
|
|
while (!MangledName.consumeFront('@')) {
|
|
|
|
assert(StringByteSize >= 1);
|
|
|
|
StringBytes[BytesDecoded++] = demangleCharLiteral(MangledName);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CharBytes =
|
|
|
|
guessCharByteSize(StringBytes, BytesDecoded, StringByteSize);
|
|
|
|
assert(StringByteSize % CharBytes == 0);
|
|
|
|
switch (CharBytes) {
|
|
|
|
case 1:
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->CharType = PrimTy::Char;
|
2018-08-17 00:17:36 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->CharType = PrimTy::Char16;
|
2018-08-17 00:17:36 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-08-18 00:14:05 +08:00
|
|
|
Result->CharType = PrimTy::Char32;
|
2018-08-17 00:17:36 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LLVM_BUILTIN_UNREACHABLE;
|
|
|
|
}
|
|
|
|
const unsigned NumChars = BytesDecoded / CharBytes;
|
|
|
|
for (unsigned CharIndex = 0; CharIndex < NumChars; ++CharIndex) {
|
|
|
|
unsigned NextChar =
|
|
|
|
decodeMultiByteChar(StringBytes, CharIndex, CharBytes);
|
2018-08-18 00:14:05 +08:00
|
|
|
if (CharIndex + 1 < NumChars || Result->IsTruncated)
|
2018-08-17 00:17:36 +08:00
|
|
|
outputEscapedChar(OS, NextChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '\0';
|
2018-08-17 00:30:27 +08:00
|
|
|
ResultBuffer = OS.getBuffer();
|
2018-08-17 00:17:36 +08:00
|
|
|
Result->Str = copyString(ResultBuffer);
|
2018-08-17 01:48:32 +08:00
|
|
|
std::free(ResultBuffer);
|
2018-08-17 00:17:36 +08:00
|
|
|
return Result;
|
|
|
|
|
|
|
|
StringLiteralError:
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
StringView Demangler::demangleSimpleString(StringView &MangledName,
|
|
|
|
bool Memorize) {
|
|
|
|
StringView S;
|
2018-07-29 06:10:42 +08:00
|
|
|
for (size_t i = 0; i < MangledName.size(); ++i) {
|
|
|
|
if (MangledName[i] != '@')
|
|
|
|
continue;
|
2018-07-31 07:02:10 +08:00
|
|
|
S = MangledName.substr(0, i);
|
2018-07-29 06:10:42 +08:00
|
|
|
MangledName = MangledName.dropFront(i + 1);
|
|
|
|
|
|
|
|
if (Memorize)
|
2018-07-31 07:02:10 +08:00
|
|
|
memorizeString(S);
|
|
|
|
return S;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error = true;
|
2018-07-31 07:02:10 +08:00
|
|
|
return {};
|
2018-07-29 06:10:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(MangledName.startsWith("?A"));
|
|
|
|
MangledName.consumeFront("?A");
|
|
|
|
|
|
|
|
Name *Node = Arena.alloc<Name>();
|
|
|
|
Node->Str = "`anonymous namespace'";
|
|
|
|
if (MangledName.consumeFront('@'))
|
|
|
|
return Node;
|
|
|
|
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
Name *Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
|
|
|
|
assert(startsWithLocalScopePattern(MangledName));
|
|
|
|
|
|
|
|
Name *Node = Arena.alloc<Name>();
|
|
|
|
MangledName.consumeFront('?');
|
2018-08-10 22:31:04 +08:00
|
|
|
auto Number = demangleNumber(MangledName);
|
|
|
|
assert(!Number.second);
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
|
|
|
|
// One ? to terminate the number
|
|
|
|
MangledName.consumeFront('?');
|
|
|
|
|
|
|
|
assert(!Error);
|
|
|
|
Symbol *Scope = parse(MangledName);
|
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Render the parent symbol's name into a buffer.
|
|
|
|
OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
|
|
|
|
OS << '`';
|
|
|
|
output(Scope, OS);
|
|
|
|
OS << '\'';
|
2018-08-10 22:31:04 +08:00
|
|
|
OS << "::`" << Number.first << "'";
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
OS << '\0';
|
|
|
|
char *Result = OS.getBuffer();
|
|
|
|
Node->Str = copyString(Result);
|
|
|
|
std::free(Result);
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
2018-07-29 06:10:42 +08:00
|
|
|
// Parses a type name in the form of A@B@C@@ which represents C::B::A.
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
|
2018-08-02 02:32:47 +08:00
|
|
|
Name *TypeName = demangleUnqualifiedTypeName(MangledName, true);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(TypeName);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *QualName = demangleNameScopeChain(MangledName, TypeName);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(QualName);
|
|
|
|
return QualName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
|
|
|
|
// Symbol names have slightly different rules regarding what can appear
|
|
|
|
// so we separate out the implementations for flexibility.
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
|
2018-08-08 08:43:31 +08:00
|
|
|
// This is the final component of a symbol name (i.e. the leftmost component
|
|
|
|
// of a mangled name. Since the only possible template instantiation that
|
|
|
|
// can appear in this context is a function template, and since those are
|
|
|
|
// not saved for the purposes of name backreferences, only backref simple
|
|
|
|
// names.
|
|
|
|
Name *SymbolName = demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-08-18 00:14:05 +08:00
|
|
|
|
|
|
|
// This is a special case that isn't followed by a scope.
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(SymbolName);
|
2018-08-18 00:14:05 +08:00
|
|
|
if (SymbolName->isStringLiteralOperatorInfo())
|
|
|
|
return SymbolName;
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *QualName = demangleNameScopeChain(MangledName, SymbolName);
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-07-29 06:10:42 +08:00
|
|
|
assert(QualName);
|
|
|
|
return QualName;
|
|
|
|
}
|
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
Name *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
|
|
|
|
bool Memorize) {
|
2018-07-29 06:10:42 +08:00
|
|
|
// An inner-most name can be a back-reference, because a fully-qualified name
|
|
|
|
// (e.g. Scope + Inner) can contain other fully qualified names inside of
|
|
|
|
// them (for example template parameters), and these nested parameters can
|
|
|
|
// refer to previously mangled types.
|
|
|
|
if (startsWithDigit(MangledName))
|
2018-07-30 00:38:02 +08:00
|
|
|
return demangleBackRefName(MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
|
|
|
if (MangledName.startsWith("?$"))
|
2018-08-08 08:43:31 +08:00
|
|
|
return demangleTemplateInstantiationName(MangledName, NBB_Template);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
return demangleSimpleName(MangledName, Memorize);
|
2018-07-29 06:10:42 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 02:32:47 +08:00
|
|
|
Name *Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
|
2018-08-08 08:43:31 +08:00
|
|
|
NameBackrefBehavior NBB) {
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
if (startsWithDigit(MangledName))
|
|
|
|
return demangleBackRefName(MangledName);
|
|
|
|
if (MangledName.startsWith("?$"))
|
2018-08-08 08:43:31 +08:00
|
|
|
return demangleTemplateInstantiationName(MangledName, NBB);
|
2018-07-29 06:10:42 +08:00
|
|
|
if (MangledName.startsWith('?'))
|
2018-07-30 00:38:02 +08:00
|
|
|
return demangleOperatorName(MangledName);
|
2018-08-08 08:43:31 +08:00
|
|
|
return demangleSimpleName(MangledName, (NBB & NBB_Simple) != 0);
|
2018-07-29 06:10:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleNameScopePiece(StringView &MangledName) {
|
2018-07-29 06:10:42 +08:00
|
|
|
if (startsWithDigit(MangledName))
|
2018-07-30 00:38:02 +08:00
|
|
|
return demangleBackRefName(MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
|
|
|
if (MangledName.startsWith("?$"))
|
2018-08-08 08:43:31 +08:00
|
|
|
return demangleTemplateInstantiationName(MangledName, NBB_Template);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
|
|
|
if (MangledName.startsWith("?A"))
|
2018-07-30 00:38:02 +08:00
|
|
|
return demangleAnonymousNamespaceName(MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
if (startsWithLocalScopePattern(MangledName))
|
|
|
|
return demangleLocallyScopedNamePiece(MangledName);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
return demangleSimpleName(MangledName, true);
|
2018-07-29 06:10:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Demangler::demangleNameScopeChain(StringView &MangledName,
|
|
|
|
Name *UnqualifiedName) {
|
2018-07-29 06:10:42 +08:00
|
|
|
Name *Head = UnqualifiedName;
|
|
|
|
|
|
|
|
while (!MangledName.consumeFront("@")) {
|
|
|
|
if (MangledName.empty()) {
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!Error);
|
2018-07-30 00:38:02 +08:00
|
|
|
Name *Elem = demangleNameScopePiece(MangledName);
|
2018-07-29 06:10:42 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Elem->Next = Head;
|
|
|
|
Head = Elem;
|
|
|
|
}
|
|
|
|
return Head;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
SwapAndRestore<StringView> RestoreOnError(MangledName, MangledName);
|
|
|
|
RestoreOnError.shouldRestore(false);
|
|
|
|
|
2018-08-11 05:09:05 +08:00
|
|
|
FuncClass TempFlags = FuncClass(0);
|
|
|
|
if (MangledName.consumeFront("$$J0"))
|
|
|
|
TempFlags = ExternC;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (MangledName.popFront()) {
|
2018-08-11 05:09:05 +08:00
|
|
|
case '9':
|
|
|
|
return FuncClass(TempFlags | ExternC | NoPrototype);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'A':
|
|
|
|
return Private;
|
|
|
|
case 'B':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Private | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'C':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Private | Static);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'D':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Private | Static);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'E':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Private | Virtual);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'F':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Private | Virtual);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'I':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'J':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'K':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected | Static);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'L':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected | Static | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'M':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected | Virtual);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'N':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Protected | Virtual | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'Q':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'R':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'S':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public | Static);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'T':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public | Static | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'U':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public | Virtual);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'V':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Public | Virtual | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'Y':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Global);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'Z':
|
2018-08-11 05:09:05 +08:00
|
|
|
return FuncClass(TempFlags | Global | Far);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error = true;
|
|
|
|
RestoreOnError.shouldRestore(true);
|
2018-07-27 04:20:10 +08:00
|
|
|
return Public;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
return CallingConv::Cdecl;
|
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
return CallingConv::Pascal;
|
|
|
|
case 'E':
|
|
|
|
case 'F':
|
|
|
|
return CallingConv::Thiscall;
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
return CallingConv::Stdcall;
|
|
|
|
case 'I':
|
|
|
|
case 'J':
|
|
|
|
return CallingConv::Fastcall;
|
|
|
|
case 'M':
|
|
|
|
case 'N':
|
|
|
|
return CallingConv::Clrcall;
|
|
|
|
case 'O':
|
|
|
|
case 'P':
|
|
|
|
return CallingConv::Eabi;
|
|
|
|
case 'Q':
|
|
|
|
return CallingConv::Vectorcall;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallingConv::None;
|
2018-07-21 02:43:42 +08:00
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
assert(std::isdigit(MangledName.front()));
|
|
|
|
|
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case '0':
|
|
|
|
return StorageClass::PrivateStatic;
|
|
|
|
case '1':
|
|
|
|
return StorageClass::ProtectedStatic;
|
|
|
|
case '2':
|
|
|
|
return StorageClass::PublicStatic;
|
|
|
|
case '3':
|
|
|
|
return StorageClass::Global;
|
|
|
|
case '4':
|
|
|
|
return StorageClass::FunctionLocalStatic;
|
|
|
|
}
|
|
|
|
Error = true;
|
|
|
|
return StorageClass::None;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
std::pair<Qualifiers, bool>
|
|
|
|
Demangler::demangleQualifiers(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
switch (MangledName.popFront()) {
|
2018-07-27 03:56:09 +08:00
|
|
|
// Member qualifiers
|
|
|
|
case 'Q':
|
|
|
|
return std::make_pair(Q_None, true);
|
|
|
|
case 'R':
|
|
|
|
return std::make_pair(Q_Const, true);
|
|
|
|
case 'S':
|
|
|
|
return std::make_pair(Q_Volatile, true);
|
|
|
|
case 'T':
|
|
|
|
return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
|
|
|
|
// Non-Member qualifiers
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'A':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_None, false);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'B':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_Const, false);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'C':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_Volatile, false);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'D':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
Error = true;
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_None, false);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
static bool isTagType(StringView S) {
|
|
|
|
switch (S.front()) {
|
|
|
|
case 'T': // union
|
|
|
|
case 'U': // struct
|
|
|
|
case 'V': // class
|
|
|
|
case 'W': // enum
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPointerType(StringView S) {
|
|
|
|
if (S.startsWith("$$Q")) // foo &&
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (S.front()) {
|
|
|
|
case 'A': // foo &
|
|
|
|
case 'P': // foo *
|
|
|
|
case 'Q': // foo *const
|
|
|
|
case 'R': // foo *volatile
|
|
|
|
case 'S': // foo *const volatile
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isArrayType(StringView S) { return S[0] == 'Y'; }
|
|
|
|
|
|
|
|
static bool isFunctionType(StringView S) {
|
|
|
|
return S.startsWith("$$A8@@") || S.startsWith("$$A6");
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// <variable-type> ::= <type> <cvr-qualifiers>
|
|
|
|
// ::= <type> <pointee-cvr-qualifiers> # pointers, references
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *Demangler::demangleType(StringView &MangledName,
|
|
|
|
QualifierMangleMode QMM) {
|
2018-07-21 01:27:48 +08:00
|
|
|
Qualifiers Quals = Q_None;
|
2018-07-27 03:56:09 +08:00
|
|
|
bool IsMember = false;
|
|
|
|
bool IsMemberKnown = false;
|
|
|
|
if (QMM == QualifierMangleMode::Mangle) {
|
2018-07-30 00:38:02 +08:00
|
|
|
std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
|
2018-07-27 03:56:09 +08:00
|
|
|
IsMemberKnown = true;
|
|
|
|
} else if (QMM == QualifierMangleMode::Result) {
|
|
|
|
if (MangledName.consumeFront('?')) {
|
2018-07-30 00:38:02 +08:00
|
|
|
std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
|
2018-07-27 03:56:09 +08:00
|
|
|
IsMemberKnown = true;
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Type *Ty = nullptr;
|
2018-07-31 07:02:10 +08:00
|
|
|
if (isTagType(MangledName))
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty = demangleClassType(MangledName);
|
2018-07-31 07:02:10 +08:00
|
|
|
else if (isPointerType(MangledName)) {
|
2018-07-27 03:56:09 +08:00
|
|
|
if (!IsMemberKnown)
|
|
|
|
IsMember = isMemberPointer(MangledName);
|
2018-07-31 07:02:10 +08:00
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
if (IsMember)
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty = demangleMemberPointerType(MangledName);
|
2018-07-27 03:56:09 +08:00
|
|
|
else
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty = demanglePointerType(MangledName);
|
2018-07-31 07:02:10 +08:00
|
|
|
} else if (isArrayType(MangledName))
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty = demangleArrayType(MangledName);
|
2018-07-31 07:02:10 +08:00
|
|
|
else if (isFunctionType(MangledName)) {
|
|
|
|
if (MangledName.consumeFront("$$A8@@"))
|
|
|
|
Ty = demangleFunctionType(MangledName, true, false);
|
|
|
|
else {
|
|
|
|
assert(MangledName.startsWith("$$A6"));
|
|
|
|
MangledName.consumeFront("$$A6");
|
|
|
|
Ty = demangleFunctionType(MangledName, false, false);
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-30 00:38:02 +08:00
|
|
|
Ty = demangleBasicType(MangledName);
|
2018-07-31 07:02:10 +08:00
|
|
|
assert(Ty && !Error);
|
|
|
|
if (!Ty || Error)
|
|
|
|
return Ty;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
2018-07-31 07:02:10 +08:00
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Ty->Quals = Qualifiers(Ty->Quals | Quals);
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
ReferenceKind Demangler::demangleReferenceKind(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
if (MangledName.consumeFront('G'))
|
|
|
|
return ReferenceKind::LValueRef;
|
|
|
|
else if (MangledName.consumeFront('H'))
|
|
|
|
return ReferenceKind::RValueRef;
|
|
|
|
return ReferenceKind::None;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
void Demangler::demangleThrowSpecification(StringView &MangledName) {
|
2018-07-27 04:20:10 +08:00
|
|
|
if (MangledName.consumeFront('Z'))
|
|
|
|
return;
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 04:20:10 +08:00
|
|
|
Error = true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
FunctionType *Demangler::demangleFunctionType(StringView &MangledName,
|
|
|
|
bool HasThisQuals,
|
2018-07-27 04:33:48 +08:00
|
|
|
bool IsFunctionPointer) {
|
2018-07-27 04:20:10 +08:00
|
|
|
FunctionType *FTy = Arena.alloc<FunctionType>();
|
2018-07-21 01:27:48 +08:00
|
|
|
FTy->Prim = PrimTy::Function;
|
2018-07-27 04:33:48 +08:00
|
|
|
FTy->IsFunctionPointer = IsFunctionPointer;
|
2018-07-27 04:20:10 +08:00
|
|
|
|
|
|
|
if (HasThisQuals) {
|
2018-07-30 00:38:02 +08:00
|
|
|
FTy->Quals = demanglePointerExtQualifiers(MangledName);
|
|
|
|
FTy->RefKind = demangleReferenceKind(MangledName);
|
|
|
|
FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields that appear on both member and non-member functions.
|
2018-07-30 00:38:02 +08:00
|
|
|
FTy->CallConvention = demangleCallingConvention(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
// <return-type> ::= <type>
|
|
|
|
// ::= @ # structors (they have no declared return type)
|
|
|
|
bool IsStructor = MangledName.consumeFront('@');
|
|
|
|
if (!IsStructor)
|
2018-07-30 00:38:02 +08:00
|
|
|
FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
FTy->Params = demangleFunctionParameterList(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
demangleThrowSpecification(MangledName);
|
2018-07-27 04:20:10 +08:00
|
|
|
|
|
|
|
return FTy;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *Demangler::demangleFunctionEncoding(StringView &MangledName) {
|
|
|
|
FuncClass FC = demangleFunctionClass(MangledName);
|
2018-08-11 05:09:05 +08:00
|
|
|
FunctionType *FTy = nullptr;
|
|
|
|
if (FC & NoPrototype) {
|
|
|
|
// This is an extern "C" function whose full signature hasn't been mangled.
|
|
|
|
// This happens when we need to mangle a local symbol inside of an extern
|
|
|
|
// "C" function.
|
|
|
|
FTy = Arena.alloc<FunctionType>();
|
|
|
|
} else {
|
|
|
|
bool HasThisQuals = !(FC & (Global | Static));
|
|
|
|
FTy = demangleFunctionType(MangledName, HasThisQuals, false);
|
|
|
|
}
|
2018-07-27 04:20:10 +08:00
|
|
|
FTy->FunctionClass = FC;
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
return FTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a primitive type.
|
2018-07-30 00:38:02 +08:00
|
|
|
Type *Demangler::demangleBasicType(StringView &MangledName) {
|
2018-07-21 02:35:06 +08:00
|
|
|
Type *Ty = Arena.alloc<Type>();
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
if (MangledName.consumeFront("$$T")) {
|
|
|
|
Ty->Prim = PrimTy::Nullptr;
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case 'X':
|
|
|
|
Ty->Prim = PrimTy::Void;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
Ty->Prim = PrimTy::Char;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
Ty->Prim = PrimTy::Schar;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
Ty->Prim = PrimTy::Uchar;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
Ty->Prim = PrimTy::Short;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
Ty->Prim = PrimTy::Ushort;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
Ty->Prim = PrimTy::Int;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
Ty->Prim = PrimTy::Uint;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
Ty->Prim = PrimTy::Long;
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
Ty->Prim = PrimTy::Ulong;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
Ty->Prim = PrimTy::Float;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
Ty->Prim = PrimTy::Double;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
Ty->Prim = PrimTy::Ldouble;
|
|
|
|
break;
|
|
|
|
case '_': {
|
2018-07-21 02:07:33 +08:00
|
|
|
if (MangledName.empty()) {
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case 'N':
|
|
|
|
Ty->Prim = PrimTy::Bool;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
Ty->Prim = PrimTy::Int64;
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
Ty->Prim = PrimTy::Uint64;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
Ty->Prim = PrimTy::Wchar;
|
|
|
|
break;
|
2018-07-31 07:02:10 +08:00
|
|
|
case 'S':
|
|
|
|
Ty->Prim = PrimTy::Char16;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
Ty->Prim = PrimTy::Char32;
|
|
|
|
break;
|
2018-07-21 02:07:33 +08:00
|
|
|
default:
|
2018-07-31 07:02:10 +08:00
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-07-31 07:02:10 +08:00
|
|
|
default:
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
UdtType *Demangler::demangleClassType(StringView &MangledName) {
|
2018-07-21 02:35:06 +08:00
|
|
|
UdtType *UTy = Arena.alloc<UdtType>();
|
2018-07-21 01:27:48 +08:00
|
|
|
|
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case 'T':
|
|
|
|
UTy->Prim = PrimTy::Union;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
UTy->Prim = PrimTy::Struct;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
UTy->Prim = PrimTy::Class;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
if (MangledName.popFront() != '4') {
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
UTy->Prim = PrimTy::Enum;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
UTy->UdtName = demangleFullyQualifiedTypeName(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
return UTy;
|
|
|
|
}
|
|
|
|
|
2018-07-27 03:56:09 +08:00
|
|
|
static std::pair<Qualifiers, PointerAffinity>
|
|
|
|
demanglePointerCVQualifiers(StringView &MangledName) {
|
2018-07-31 07:02:10 +08:00
|
|
|
if (MangledName.consumeFront("$$Q"))
|
|
|
|
return std::make_pair(Q_None, PointerAffinity::RValueReference);
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
switch (MangledName.popFront()) {
|
|
|
|
case 'A':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_None, PointerAffinity::Reference);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'P':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_None, PointerAffinity::Pointer);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'Q':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_Const, PointerAffinity::Pointer);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'R':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
|
2018-07-21 01:27:48 +08:00
|
|
|
case 'S':
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
|
|
|
|
PointerAffinity::Pointer);
|
2018-07-21 01:27:48 +08:00
|
|
|
default:
|
|
|
|
assert(false && "Ty is not a pointer type!");
|
|
|
|
}
|
2018-07-27 03:56:09 +08:00
|
|
|
return std::make_pair(Q_None, PointerAffinity::Pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
|
|
|
|
// # the E is required for 64-bit non-static pointers
|
2018-07-30 00:38:02 +08:00
|
|
|
PointerType *Demangler::demanglePointerType(StringView &MangledName) {
|
2018-07-27 03:56:09 +08:00
|
|
|
PointerType *Pointer = Arena.alloc<PointerType>();
|
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
std::tie(Pointer->Quals, Pointer->Affinity) =
|
|
|
|
demanglePointerCVQualifiers(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-31 07:02:10 +08:00
|
|
|
Pointer->Prim = PrimTy::Ptr;
|
2018-07-21 01:27:48 +08:00
|
|
|
if (MangledName.consumeFront("6")) {
|
2018-07-30 00:38:02 +08:00
|
|
|
Pointer->Pointee = demangleFunctionType(MangledName, false, true);
|
2018-07-21 01:27:48 +08:00
|
|
|
return Pointer;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
|
2018-07-21 01:27:48 +08:00
|
|
|
Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
|
2018-07-21 01:27:48 +08:00
|
|
|
return Pointer;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
MemberPointerType *
|
|
|
|
Demangler::demangleMemberPointerType(StringView &MangledName) {
|
2018-07-27 03:56:09 +08:00
|
|
|
MemberPointerType *Pointer = Arena.alloc<MemberPointerType>();
|
|
|
|
Pointer->Prim = PrimTy::MemberPtr;
|
|
|
|
|
|
|
|
PointerAffinity Affinity;
|
|
|
|
std::tie(Pointer->Quals, Affinity) = demanglePointerCVQualifiers(MangledName);
|
|
|
|
assert(Affinity == PointerAffinity::Pointer);
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
|
2018-07-27 03:56:09 +08:00
|
|
|
Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
|
|
|
|
|
2018-07-27 04:20:10 +08:00
|
|
|
if (MangledName.consumeFront("8")) {
|
2018-07-30 00:38:02 +08:00
|
|
|
Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
|
|
|
|
Pointer->Pointee = demangleFunctionType(MangledName, true, true);
|
2018-07-27 04:20:10 +08:00
|
|
|
} else {
|
|
|
|
Qualifiers PointeeQuals = Q_None;
|
|
|
|
bool IsMember = false;
|
2018-07-30 00:38:02 +08:00
|
|
|
std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
|
2018-07-27 04:20:10 +08:00
|
|
|
assert(IsMember);
|
2018-07-30 00:38:02 +08:00
|
|
|
Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
|
2018-07-27 04:20:10 +08:00
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-07-27 04:20:10 +08:00
|
|
|
Pointer->Pointee->Quals = PointeeQuals;
|
|
|
|
}
|
2018-07-27 03:56:09 +08:00
|
|
|
|
|
|
|
return Pointer;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
Qualifiers Quals = Q_None;
|
|
|
|
if (MangledName.consumeFront('E'))
|
|
|
|
Quals = Qualifiers(Quals | Q_Pointer64);
|
|
|
|
if (MangledName.consumeFront('I'))
|
|
|
|
Quals = Qualifiers(Quals | Q_Restrict);
|
|
|
|
if (MangledName.consumeFront('F'))
|
|
|
|
Quals = Qualifiers(Quals | Q_Unaligned);
|
|
|
|
|
|
|
|
return Quals;
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
ArrayType *Demangler::demangleArrayType(StringView &MangledName) {
|
2018-07-21 01:27:48 +08:00
|
|
|
assert(MangledName.front() == 'Y');
|
|
|
|
MangledName.popFront();
|
|
|
|
|
2018-08-10 22:31:04 +08:00
|
|
|
uint64_t Rank = 0;
|
|
|
|
bool IsNegative = false;
|
|
|
|
std::tie(Rank, IsNegative) = demangleNumber(MangledName);
|
|
|
|
if (IsNegative || Rank == 0) {
|
2018-07-21 01:27:48 +08:00
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:35:06 +08:00
|
|
|
ArrayType *ATy = Arena.alloc<ArrayType>();
|
2018-08-10 22:31:04 +08:00
|
|
|
ATy->Prim = PrimTy::Array;
|
|
|
|
ATy->Dims = Arena.alloc<ArrayDimension>();
|
|
|
|
ArrayDimension *Dim = ATy->Dims;
|
|
|
|
for (uint64_t I = 0; I < Rank; ++I) {
|
|
|
|
std::tie(Dim->Dim, IsNegative) = demangleNumber(MangledName);
|
|
|
|
if (IsNegative) {
|
|
|
|
Error = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (I + 1 < Rank) {
|
|
|
|
Dim->Next = Arena.alloc<ArrayDimension>();
|
|
|
|
Dim = Dim->Next;
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MangledName.consumeFront("$$C")) {
|
2018-08-15 02:54:28 +08:00
|
|
|
bool IsMember = false;
|
|
|
|
std::tie(ATy->Quals, IsMember) = demangleQualifiers(MangledName);
|
|
|
|
if (IsMember) {
|
2018-07-21 01:27:48 +08:00
|
|
|
Error = true;
|
2018-08-15 02:54:28 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-07-21 01:27:48 +08:00
|
|
|
return ATy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a function or a template parameters.
|
2018-08-01 01:16:44 +08:00
|
|
|
FunctionParams
|
|
|
|
Demangler::demangleFunctionParameterList(StringView &MangledName) {
|
2018-07-27 04:20:10 +08:00
|
|
|
// Empty parameter list.
|
|
|
|
if (MangledName.consumeFront('X'))
|
|
|
|
return {};
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
FunctionParams *Head;
|
|
|
|
FunctionParams **Current = &Head;
|
2018-07-21 01:27:48 +08:00
|
|
|
while (!Error && !MangledName.startsWith('@') &&
|
|
|
|
!MangledName.startsWith('Z')) {
|
2018-07-27 06:13:39 +08:00
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
if (startsWithDigit(MangledName)) {
|
2018-07-27 06:24:01 +08:00
|
|
|
size_t N = MangledName[0] - '0';
|
2018-08-09 01:17:04 +08:00
|
|
|
if (N >= Backrefs.FunctionParamCount) {
|
2018-07-21 01:27:48 +08:00
|
|
|
Error = true;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
MangledName = MangledName.dropFront();
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
*Current = Arena.alloc<FunctionParams>();
|
2018-08-09 01:17:04 +08:00
|
|
|
(*Current)->Current = Backrefs.FunctionParams[N]->clone(Arena);
|
2018-07-21 01:27:48 +08:00
|
|
|
Current = &(*Current)->Next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-27 06:13:39 +08:00
|
|
|
size_t OldSize = MangledName.size();
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
*Current = Arena.alloc<FunctionParams>();
|
2018-07-30 00:38:02 +08:00
|
|
|
(*Current)->Current = demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-07-27 06:13:39 +08:00
|
|
|
size_t CharsConsumed = OldSize - MangledName.size();
|
|
|
|
assert(CharsConsumed != 0);
|
|
|
|
|
|
|
|
// Single-letter types are ignored for backreferences because memorizing
|
|
|
|
// them doesn't save anything.
|
2018-08-09 01:17:04 +08:00
|
|
|
if (Backrefs.FunctionParamCount <= 9 && CharsConsumed > 1)
|
|
|
|
Backrefs.FunctionParams[Backrefs.FunctionParamCount++] =
|
|
|
|
(*Current)->Current;
|
2018-07-27 06:13:39 +08:00
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
Current = &(*Current)->Next;
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:20:10 +08:00
|
|
|
if (Error)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// A non-empty parameter list is terminated by either 'Z' (variadic) parameter
|
|
|
|
// list or '@' (non variadic). Careful not to consume "@Z", as in that case
|
|
|
|
// the following Z could be a throw specifier.
|
|
|
|
if (MangledName.consumeFront('@'))
|
|
|
|
return *Head;
|
|
|
|
|
|
|
|
if (MangledName.consumeFront('Z')) {
|
|
|
|
Head->IsVariadic = true;
|
|
|
|
return *Head;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error = true;
|
|
|
|
return {};
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
TemplateParams *
|
|
|
|
Demangler::demangleTemplateParameterList(StringView &MangledName) {
|
|
|
|
TemplateParams *Head;
|
|
|
|
TemplateParams **Current = &Head;
|
2018-07-27 06:13:39 +08:00
|
|
|
while (!Error && !MangledName.startsWith('@')) {
|
|
|
|
// Template parameter lists don't participate in back-referencing.
|
2018-08-01 01:16:44 +08:00
|
|
|
*Current = Arena.alloc<TemplateParams>();
|
2018-07-31 07:02:10 +08:00
|
|
|
|
|
|
|
// Empty parameter pack.
|
|
|
|
if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
|
|
|
|
MangledName.consumeFront("$$$V")) {
|
2018-08-10 22:31:04 +08:00
|
|
|
(*Current)->IsEmptyParameterPack = true;
|
2018-08-02 02:32:28 +08:00
|
|
|
break;
|
2018-07-31 07:02:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 01:16:44 +08:00
|
|
|
if (MangledName.consumeFront("$$Y")) {
|
2018-08-10 22:31:04 +08:00
|
|
|
// Template alias
|
2018-08-01 01:16:44 +08:00
|
|
|
(*Current)->IsTemplateTemplate = true;
|
|
|
|
(*Current)->IsAliasTemplate = true;
|
|
|
|
(*Current)->ParamName = demangleFullyQualifiedTypeName(MangledName);
|
2018-08-10 22:31:04 +08:00
|
|
|
} else if (MangledName.consumeFront("$$B")) {
|
|
|
|
// Array
|
|
|
|
(*Current)->ParamType =
|
|
|
|
demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-08-11 03:57:36 +08:00
|
|
|
} else if (MangledName.consumeFront("$$C")) {
|
|
|
|
// Type has qualifiers.
|
|
|
|
(*Current)->ParamType =
|
|
|
|
demangleType(MangledName, QualifierMangleMode::Mangle);
|
2018-08-10 22:31:04 +08:00
|
|
|
} else if (MangledName.startsWith("$1?")) {
|
|
|
|
MangledName.consumeFront("$1");
|
|
|
|
// Pointer to symbol
|
|
|
|
Symbol *S = parse(MangledName);
|
|
|
|
(*Current)->ParamName = S->SymbolName;
|
|
|
|
(*Current)->ParamType = S->SymbolType;
|
|
|
|
(*Current)->PointerToSymbol = true;
|
|
|
|
} else if (MangledName.startsWith("$E?")) {
|
|
|
|
MangledName.consumeFront("$E");
|
|
|
|
// Reference to symbol
|
|
|
|
Symbol *S = parse(MangledName);
|
|
|
|
(*Current)->ParamName = S->SymbolName;
|
|
|
|
(*Current)->ParamType = S->SymbolType;
|
|
|
|
(*Current)->ReferenceToSymbol = true;
|
|
|
|
} else if (MangledName.consumeFront("$0")) {
|
|
|
|
// Integral non-type template parameter
|
|
|
|
bool IsNegative = false;
|
|
|
|
uint64_t Value = 0;
|
|
|
|
std::tie(Value, IsNegative) = demangleNumber(MangledName);
|
|
|
|
|
|
|
|
(*Current)->IsIntegerLiteral = true;
|
|
|
|
(*Current)->IntegerLiteralIsNegative = IsNegative;
|
|
|
|
(*Current)->IntegralValue = Value;
|
2018-08-01 01:16:44 +08:00
|
|
|
} else {
|
|
|
|
(*Current)->ParamType =
|
2018-07-31 09:08:42 +08:00
|
|
|
demangleType(MangledName, QualifierMangleMode::Drop);
|
2018-08-01 01:16:44 +08:00
|
|
|
}
|
2018-08-02 02:32:28 +08:00
|
|
|
if (Error)
|
|
|
|
return nullptr;
|
2018-07-27 06:13:39 +08:00
|
|
|
|
|
|
|
Current = &(*Current)->Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Error)
|
2018-08-02 02:32:28 +08:00
|
|
|
return nullptr;
|
2018-07-27 06:13:39 +08:00
|
|
|
|
|
|
|
// Template parameter lists cannot be variadic, so it can only be terminated
|
|
|
|
// by @.
|
|
|
|
if (MangledName.consumeFront('@'))
|
2018-07-31 07:02:10 +08:00
|
|
|
return Head;
|
2018-07-27 06:13:39 +08:00
|
|
|
Error = true;
|
2018-08-02 02:32:28 +08:00
|
|
|
return nullptr;
|
2018-07-27 06:13:39 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:08:03 +08:00
|
|
|
StringView Demangler::resolve(StringView N) {
|
|
|
|
assert(N.size() == 1 && isdigit(N[0]));
|
2018-08-03 01:18:01 +08:00
|
|
|
size_t Digit = N[0] - '0';
|
2018-08-09 01:17:04 +08:00
|
|
|
if (Digit >= Backrefs.NamesCount)
|
2018-08-03 01:08:03 +08:00
|
|
|
return N;
|
2018-08-09 01:17:04 +08:00
|
|
|
return Backrefs.Names[Digit];
|
2018-08-03 01:08:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:38:02 +08:00
|
|
|
void Demangler::output(const Symbol *S, OutputStream &OS) {
|
2018-08-17 00:17:17 +08:00
|
|
|
if (S->Category == SymbolCategory::Unknown) {
|
|
|
|
outputName(OS, S->SymbolName, S->SymbolType, *this);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-17 00:17:36 +08:00
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
// Converts an AST to a string.
|
|
|
|
//
|
|
|
|
// Converting an AST representing a C++ type to a string is tricky due
|
|
|
|
// to the bad grammar of the C++ declaration inherited from C. You have
|
|
|
|
// to construct a string from inside to outside. For example, if a type
|
|
|
|
// X is a pointer to a function returning int, the order you create a
|
|
|
|
// string becomes something like this:
|
|
|
|
//
|
|
|
|
// (1) X is a pointer: *X
|
|
|
|
// (2) (1) is a function returning int: int (*X)()
|
|
|
|
//
|
|
|
|
// So you cannot construct a result just by appending strings to a result.
|
|
|
|
//
|
|
|
|
// To deal with this, we split the function into two. outputPre() writes
|
|
|
|
// the "first half" of type declaration, and outputPost() writes the
|
|
|
|
// "second half". For example, outputPre() writes a return type for a
|
|
|
|
// function and outputPost() writes an parameter list.
|
2018-08-18 00:14:05 +08:00
|
|
|
if (S->SymbolType) {
|
|
|
|
Type::outputPre(OS, *S->SymbolType, *this);
|
|
|
|
outputName(OS, S->SymbolName, S->SymbolType, *this);
|
|
|
|
Type::outputPost(OS, *S->SymbolType, *this);
|
|
|
|
} else
|
|
|
|
outputName(OS, S->SymbolName, nullptr, *this);
|
2018-07-21 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 02:33:04 +08:00
|
|
|
void Demangler::dumpBackReferences() {
|
2018-08-02 02:44:12 +08:00
|
|
|
std::printf("%d function parameter backreferences\n",
|
2018-08-09 01:17:04 +08:00
|
|
|
(int)Backrefs.FunctionParamCount);
|
2018-08-02 02:33:04 +08:00
|
|
|
|
|
|
|
// Create an output stream so we can render each type.
|
|
|
|
OutputStream OS = OutputStream::create(nullptr, 0, 1024);
|
2018-08-09 01:17:04 +08:00
|
|
|
for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
|
2018-08-02 02:33:04 +08:00
|
|
|
OS.setCurrentPosition(0);
|
|
|
|
|
2018-08-09 01:17:04 +08:00
|
|
|
Type *T = Backrefs.FunctionParams[I];
|
2018-08-03 01:08:03 +08:00
|
|
|
Type::outputPre(OS, *T, *this);
|
|
|
|
Type::outputPost(OS, *T, *this);
|
2018-08-02 02:33:04 +08:00
|
|
|
|
2018-08-03 01:08:24 +08:00
|
|
|
std::printf(" [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
|
2018-08-02 02:44:12 +08:00
|
|
|
OS.getBuffer());
|
2018-08-02 02:33:04 +08:00
|
|
|
}
|
|
|
|
std::free(OS.getBuffer());
|
|
|
|
|
2018-08-09 01:17:04 +08:00
|
|
|
if (Backrefs.FunctionParamCount > 0)
|
2018-08-02 02:44:12 +08:00
|
|
|
std::printf("\n");
|
2018-08-09 01:17:04 +08:00
|
|
|
std::printf("%d name backreferences\n", (int)Backrefs.NamesCount);
|
|
|
|
for (size_t I = 0; I < Backrefs.NamesCount; ++I) {
|
|
|
|
std::printf(" [%d] - %.*s\n", (int)I, (int)Backrefs.Names[I].size(),
|
|
|
|
Backrefs.Names[I].begin());
|
2018-08-02 02:33:04 +08:00
|
|
|
}
|
2018-08-09 01:17:04 +08:00
|
|
|
if (Backrefs.NamesCount > 0)
|
2018-08-02 02:44:12 +08:00
|
|
|
std::printf("\n");
|
2018-08-02 02:33:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:27:48 +08:00
|
|
|
char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
|
2018-08-02 02:33:04 +08:00
|
|
|
int *Status, MSDemangleFlags Flags) {
|
2018-07-30 00:38:02 +08:00
|
|
|
Demangler D;
|
|
|
|
StringView Name{MangledName};
|
|
|
|
Symbol *S = D.parse(Name);
|
2018-07-21 01:27:48 +08:00
|
|
|
|
2018-08-02 02:33:04 +08:00
|
|
|
if (Flags & MSDF_DumpBackrefs)
|
|
|
|
D.dumpBackReferences();
|
2018-08-02 02:32:28 +08:00
|
|
|
OutputStream OS = OutputStream::create(Buf, N, 1024);
|
|
|
|
if (D.Error) {
|
|
|
|
OS << MangledName;
|
2018-07-21 01:27:48 +08:00
|
|
|
*Status = llvm::demangle_invalid_mangled_name;
|
2018-08-02 02:32:28 +08:00
|
|
|
} else {
|
|
|
|
D.output(S, OS);
|
2018-07-21 01:27:48 +08:00
|
|
|
*Status = llvm::demangle_success;
|
2018-08-02 02:32:28 +08:00
|
|
|
}
|
2018-07-21 01:27:48 +08:00
|
|
|
|
[MS Demangler] Demangle symbols in function scopes.
There are a couple of issues you run into when you start getting into
more complex names, especially with regards to function local statics.
When you've got something like:
int x() {
static int n = 0;
return n;
}
Then this needs to demangle to something like
int `int __cdecl x()'::`1'::n
The nested mangled symbols (e.g. `int __cdecl x()` in the above
example) also share state with regards to back-referencing, so
we need to be able to re-use the demangler in the middle of
demangling a symbol while sharing back-ref state.
To make matters more complicated, there are a lot of ambiguities
when demangling a symbol's qualified name, because a function local
scope pattern (usually something like `?1??name?`) looks suspiciously
like many other possible things that can occur, such as `?1` meaning
the second back-ref and disambiguating these cases is rather
interesting. The `?1?` in a local scope pattern is actually a special
case of the more general pattern of `? + <encoded number> + ?`, where
"encoded number" can itself have embedded `@` symbols, which is a
common delimeter in mangled names. So we have to take care during the
disambiguation, which is the reason for the overly complicated
`isLocalScopePattern` function in this patch.
I've added some pretty obnoxious tests to exercise all of this, which
exposed several other problems related to back-referencing, so those
are fixed here as well. Finally, I've uncommented some tests that were
previously marked as `FIXME`, since now these work.
Differential Revision: https://reviews.llvm.org/D49965
llvm-svn: 338226
2018-07-30 11:12:34 +08:00
|
|
|
OS << '\0';
|
2018-07-21 01:27:48 +08:00
|
|
|
return OS.getBuffer();
|
|
|
|
}
|