WIP: handling vtable generation

This commit is contained in:
Duncan Ogilvie 2023-07-07 11:38:31 +02:00
parent b92d01f5c8
commit 8e19f7ce0d
1 changed files with 165 additions and 97 deletions

View File

@ -6,6 +6,7 @@
#include "helpers.h"
#include "preprocessor.h"
#include "types.h"
#include <unordered_set>
bool TestLexer(Lexer& lexer, const std::string& filename)
{
@ -87,6 +88,71 @@ void DebugLexerTests(bool output = true)
DebugLexer(lexer, file, output);
}
static std::vector<std::string> SplitNamespace(const std::string& name, std::string& type)
{
auto namespaces = StringUtils::Split(name, ':');
if (namespaces.empty())
{
type.clear();
}
else
{
type = namespaces.back();
namespaces.pop_back();
}
return namespaces;
}
static void HandleVTable(Types::Model& model)
{
std::unordered_set<std::string> classes;
std::unordered_set<std::string> visited;
auto handleType = [&](const Types::QualifiedType& type)
{
if (!type.kind.empty())
{
const auto& name = type.name;
const auto& kind = type.kind;
if (visited.count(name) != 0)
return;
visited.insert(name);
std::string rettype;
auto namespaces = SplitNamespace(name, rettype);
if (kind == "class" && rettype[0] == 'I')
classes.insert(name);
for (const auto& ns : namespaces)
{
printf("namespace %s {\n", ns.c_str());
}
printf(" %s %s {\n };\n", kind.c_str(), rettype.c_str());
for (const auto& ns : namespaces)
printf("}\n");
}
};
for (const auto& su : model.structUnions)
{
for (const auto& vf : su.vtable)
{
handleType(vf.rettype);
for (const auto& arg : vf.args)
handleType(arg.type);
}
}
for (const auto& f : model.functions)
{
handleType(f.rettype);
for (const auto& arg : f.args)
handleType(arg.type);
}
puts("Used classes:");
for (const auto& name : classes)
{
printf("%s\n", name.c_str());
}
puts("vtable chuj");
}
bool DebugParser(const std::string& filename)
{
std::string data;
@ -110,16 +176,18 @@ bool DebugParser(const std::string & filename)
FileHelper::WriteAllText("tests\\" + filename + ".pp.h", ppData);
std::vector<std::string> errors;
Types::TypeManager typeManager(sizeof(void*));
if (!typeManager.ParseTypes(ppData, filename, errors))
Types::Model model;
if (!Types::ParseModel(ppData, filename, errors, model))
{
puts("Failed to parse types:");
for (const auto& error : errors)
puts(error.c_str());
return false;
}
puts("ParseModel success!");
HandleVTable(model);
puts("ParseFile success!");
return true;
}
@ -131,7 +199,7 @@ int main()
//Lexer lexer;
//DebugLexer(lexer, "AndroidManifestTemplate.bt", false);
//RunLexerTests();
printf("finished in %ums\n", GetTickCount() - ticks);
printf("finished in %ums\n", (unsigned int)(GetTickCount() - ticks));
system("pause");
return 0;
}