Swift: introduce type mangling

This commit is contained in:
Alex Denisov
2023-03-03 16:28:41 +01:00
parent 4aeff0f8dc
commit 7f3e7224df
4 changed files with 32 additions and 12 deletions

View File

@@ -30,3 +30,11 @@ std::string SwiftMangler::mangledName(const swift::Decl& decl) {
}
return ret.str();
}
std::optional<std::string> SwiftMangler::mangleType(const swift::ModuleType& type) {
auto key = type.getModule()->getRealName().str().str();
if (type.getModule()->isNonSwiftModule()) {
key += "|clang";
}
return key;
}

View File

@@ -1,16 +1,20 @@
#pragma once
#include <vector>
#include <unordered_set>
#include <filesystem>
#include <swift/AST/ASTMangler.h>
#include <swift/AST/Types.h>
namespace codeql {
class SwiftMangler {
public:
std::string mangledName(const swift::Decl& decl);
template <typename T>
std::optional<std::string> mangleType(const T& type) {
return std::nullopt;
}
std::optional<std::string> mangleType(const swift::ModuleType& type);
private:
swift::Mangle::ASTMangler mangler;
};

View File

@@ -242,11 +242,7 @@ codeql::OpenedArchetypeType TypeTranslator::translateOpenedArchetypeType(
}
codeql::ModuleType TypeTranslator::translateModuleType(const swift::ModuleType& type) {
auto key = type.getModule()->getRealName().str().str();
if (type.getModule()->isNonSwiftModule()) {
key += "|clang";
}
auto entry = createTypeEntry(type, key);
auto entry = createTypeEntry(type);
entry.module = dispatcher.fetchLabel(type.getModule());
return entry;
}

View File

@@ -2,6 +2,7 @@
#include "swift/extractor/translators/TranslatorBase.h"
#include "swift/extractor/trap/generated/type/TrapClasses.h"
#include "swift/extractor/mangler/SwiftMangler.h"
namespace codeql {
class TypeTranslator : public TypeTranslatorBase<TypeTranslator> {
@@ -87,12 +88,23 @@ class TypeTranslator : public TypeTranslatorBase<TypeTranslator> {
void fillBoundGenericType(const swift::BoundGenericType& type, codeql::BoundGenericType& entry);
void fillAnyGenericType(const swift::AnyGenericType& type, codeql::AnyGenericType& entry);
template <typename T, typename... Args>
auto createTypeEntry(const T& type, const Args&... args) {
auto entry = dispatcher.createEntry(type, args...);
template <typename T>
auto createMangledTypeEntry(const T& type) {
auto mangledName = mangler.mangleType(type);
if (mangledName) {
return dispatcher.createEntry(type, mangledName.value());
}
return dispatcher.createEntry(type);
}
template <typename T>
auto createTypeEntry(const T& type) {
auto entry = createMangledTypeEntry(type);
fillType(type, entry);
return entry;
}
SwiftMangler mangler;
};
} // namespace codeql