Skip to content

Commit

Permalink
[Demangle] Add minimal support for D simple basic types
Browse files Browse the repository at this point in the history
This patch implements simple demangling of two basic types to add minimal type functionality. This will be later used in function type parsing. After that being implemented we can add the rest of the types and test the result of the type name.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D111416
  • Loading branch information
ljmf00 committed Jan 12, 2022
1 parent 7e29da8 commit 669bfcf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
38 changes: 36 additions & 2 deletions llvm/lib/Demangle/DLangDemangle.cpp
Expand Up @@ -115,6 +115,16 @@ struct Demangler {
/// \see https://dlang.org/spec/abi.html#QualifiedName .
const char *parseQualified(OutputBuffer *Demangled, const char *Mangled);

/// Extract and demangle a type from a given mangled symbol append it to
/// the output string.
///
/// \param Mangled mangled symbol to be demangled.
///
/// \return the remaining string on success or nullptr on failure.
///
/// \see https://dlang.org/spec/abi.html#Type .
const char *parseType(const char *Mangled);

/// The string we are demangling.
const char *Str;
};
Expand Down Expand Up @@ -174,8 +184,7 @@ const char *Demangler::parseMangle(OutputBuffer *Demangled,
if (*Mangled == 'Z')
++Mangled;
else {
// TODO: Implement symbols with types.
return nullptr;
Mangled = parseType(Mangled);
}
}

Expand Down Expand Up @@ -262,6 +271,31 @@ const char *Demangler::parseIdentifier(OutputBuffer *Demangled,
return parseLName(Demangled, Mangled, Len);
}

const char *Demangler::parseType(const char *Mangled) {
if (*Mangled == '\0')
return nullptr;

switch (*Mangled) {
// TODO: Parse type qualifiers.
// TODO: Parse function types.
// TODO: Parse compound types.
// TODO: Parse delegate types.
// TODO: Parse tuple types.

// Basic types.
case 'i':
++Mangled;
// TODO: Add type name dumping
return Mangled;

// TODO: Add support for the rest of the basic types.
// TODO: Parse back referenced types.

default: // unhandled.
return nullptr;
}
}

const char *Demangler::parseLName(OutputBuffer *Demangled, const char *Mangled,
unsigned long Len) {
switch (Len) {
Expand Down
7 changes: 6 additions & 1 deletion llvm/unittests/Demangle/DLangDemangleTest.cpp
Expand Up @@ -47,4 +47,9 @@ INSTANTIATE_TEST_SUITE_P(
std::make_pair("_D8demangle4test12__ModuleInfoZ",
"ModuleInfo for demangle.test"),
std::make_pair("_D8demangle4__S14testZ", "demangle.test"),
std::make_pair("_D8demangle4__Sd4testZ", "demangle.__Sd.test")));
std::make_pair("_D8demangle4__Sd4testZ", "demangle.__Sd.test"),
std::make_pair("_D8demangle3fooi", "demangle.foo"),
std::make_pair("_D8demangle3foo",
nullptr), // symbol without a type sequence.
std::make_pair("_D8demangle3fooinvalidtypeseq",
nullptr))); // invalid type sequence.

0 comments on commit 669bfcf

Please sign in to comment.