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,27 +6,28 @@
#include "helpers.h" #include "helpers.h"
#include "preprocessor.h" #include "preprocessor.h"
#include "types.h" #include "types.h"
#include <unordered_set>
bool TestLexer(Lexer & lexer, const std::string & filename) bool TestLexer(Lexer& lexer, const std::string& filename)
{ {
if(!lexer.ReadInputFile("tests\\" + filename)) if (!lexer.ReadInputFile("tests\\" + filename))
{ {
printf("failed to read \"%s\"\n", filename.c_str()); printf("failed to read \"%s\"\n", filename.c_str());
return false; return false;
} }
std::string actual; std::string actual;
actual.reserve(65536); actual.reserve(65536);
auto success = lexer.Test([&](const std::string & line) auto success = lexer.Test([&](const std::string& line)
{ {
actual.append(line); actual.append(line);
}); });
std::string expected; std::string expected;
if(FileHelper::ReadAllText("tests\\exp_lex\\" + filename, expected) && expected == actual) if (FileHelper::ReadAllText("tests\\exp_lex\\" + filename, expected) && expected == actual)
{ {
printf("lexer test for \"%s\" success!\n", filename.c_str()); printf("lexer test for \"%s\" success!\n", filename.c_str());
return true; return true;
} }
if(success) if (success)
return true; return true;
printf("lexer test for \"%s\" failed...\n", filename.c_str()); printf("lexer test for \"%s\" failed...\n", filename.c_str());
FileHelper::WriteAllText("expected.out", expected); FileHelper::WriteAllText("expected.out", expected);
@ -34,32 +35,32 @@ bool TestLexer(Lexer & lexer, const std::string & filename)
return false; return false;
} }
bool DebugLexer(Lexer & lexer, const std::string & filename, bool output) bool DebugLexer(Lexer& lexer, const std::string& filename, bool output)
{ {
if(!lexer.ReadInputFile("tests\\" + filename)) if (!lexer.ReadInputFile("tests\\" + filename))
{ {
printf("failed to read \"%s\"\n", filename.c_str()); printf("failed to read \"%s\"\n", filename.c_str());
return false; return false;
} }
auto success = lexer.Test([](const std::string & line) auto success = lexer.Test([](const std::string& line)
{ {
printf("%s", line.c_str()); printf("%s", line.c_str());
}, output); }, output);
if(output) if (output)
puts(""); puts("");
return success; return success;
} }
void GenerateExpected(Lexer & lexer, const std::string & filename) void GenerateExpected(Lexer& lexer, const std::string& filename)
{ {
if(!lexer.ReadInputFile("tests\\" + filename)) if (!lexer.ReadInputFile("tests\\" + filename))
{ {
printf("failed to read \"%s\"\n", filename.c_str()); printf("failed to read \"%s\"\n", filename.c_str());
return; return;
} }
std::string actual; std::string actual;
actual.reserve(65536); actual.reserve(65536);
lexer.Test([&](const std::string & line) lexer.Test([&](const std::string& line)
{ {
actual.append(line); actual.append(line);
}); });
@ -69,25 +70,90 @@ void GenerateExpected(Lexer & lexer, const std::string & filename)
void GenerateExpectedTests() void GenerateExpectedTests()
{ {
Lexer lexer; Lexer lexer;
for(auto file : testFiles) for (auto file : testFiles)
GenerateExpected(lexer, file); GenerateExpected(lexer, file);
} }
void RunLexerTests() void RunLexerTests()
{ {
Lexer lexer; Lexer lexer;
for(auto file : testFiles) for (auto file : testFiles)
TestLexer(lexer, file); TestLexer(lexer, file);
} }
void DebugLexerTests(bool output = true) void DebugLexerTests(bool output = true)
{ {
Lexer lexer; Lexer lexer;
for(auto file : testFiles) for (auto file : testFiles)
DebugLexer(lexer, file, output); DebugLexer(lexer, file, output);
} }
bool DebugParser(const std::string & filename) 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; std::string data;
if (!FileHelper::ReadAllText("tests\\" + filename, data)) if (!FileHelper::ReadAllText("tests\\" + filename, data))
@ -110,16 +176,18 @@ bool DebugParser(const std::string & filename)
FileHelper::WriteAllText("tests\\" + filename + ".pp.h", ppData); FileHelper::WriteAllText("tests\\" + filename + ".pp.h", ppData);
std::vector<std::string> errors; std::vector<std::string> errors;
Types::TypeManager typeManager(sizeof(void*)); Types::Model model;
if (!typeManager.ParseTypes(ppData, filename, errors)) if (!Types::ParseModel(ppData, filename, errors, model))
{ {
puts("Failed to parse types:"); puts("Failed to parse types:");
for (const auto& error : errors) for (const auto& error : errors)
puts(error.c_str()); puts(error.c_str());
return false; return false;
} }
puts("ParseModel success!");
HandleVTable(model);
puts("ParseFile success!");
return true; return true;
} }
@ -131,7 +199,7 @@ int main()
//Lexer lexer; //Lexer lexer;
//DebugLexer(lexer, "AndroidManifestTemplate.bt", false); //DebugLexer(lexer, "AndroidManifestTemplate.bt", false);
//RunLexerTests(); //RunLexerTests();
printf("finished in %ums\n", GetTickCount() - ticks); printf("finished in %ums\n", (unsigned int)(GetTickCount() - ticks));
system("pause"); system("pause");
return 0; return 0;
} }