AuroraRuntime/Source/Debug/Demangle.cpp

93 lines
2.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Demangle.cpp
Date: 2022-3-31
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Debug.hpp"
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include <DbgHelp.h>
#elif defined(AURORA_COMPILER_CLANG)
#include <cxxabi.h>
#endif
namespace Aurora::Debug
{
AUKN_SYM AuResult<AuString> DemangleName(const AuRONString &pName)
{
// Wine impl: https://github.com/wine-mirror/wine/blob/master/dlls/msvcrt/undname.c
// Google impl: idr where i saw it last
AuResult<AuString> ret;
#if defined(AURORA_IS_MODERNNT_DERIVED)
struct win32poll
{
const char *ptr;
char name[4096] = {0};
} poll;
poll.ptr = {};
if (pName.size() >= AuArraySize(poll.name))
{
return ret;
}
AuMemcpy(poll.name, pName.data(), pName.size());
auto pInfo = (__std_type_info_data *)&poll;
#ifdef _M_CEE_PURE
ret.type = __std_type_info_name((__std_type_info_data *)&poll, static_cast<__type_info_node *>(__type_info_root_node.ToPointer()));
#else
ret.type = __std_type_info_name(pInfo, &__type_info_root_node);
#endif
// On failure, the undecorated name is assigned the dname docorated name, which is offset by +1 (the '?' or '_' hint)
if (strcmp(pInfo->_UndecoratedName, pInfo->_DecoratedName + 1) == 0)
{
if (pUnDecorateSymbolName)
{
auto characters = pUnDecorateSymbolName(pName.c_str(), poll.name, AuArraySize(poll.name), UNDNAME_32_BIT_DECODE);
if (!characters)
{
return ret;
}
ret.type = AuString(poll.name, characters);
}
else
{
return ret;
}
}
#else
int status;
char *res = abi::__cxa_demangle(pName.c_str(),
nullptr,
nullptr,
&status);
if (!res)
{
return ret;
}
if (status != 0)
{
free(res);
return ret;
}
ret.type = res;
free(res);
#endif
ret.Complete();
return ret;
}
}