geode/codegen/src/BindingGen.cpp

269 lines
8.7 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#include "Shared.hpp"
#include <iostream>
#include <set>
namespace { namespace format_strings {
// requires: base_classes, class_name
2022-10-13 04:31:23 -04:00
char const* binding_include = R"GEN(#include "binding/{file_name}"
)GEN";
char const* class_includes = R"GEN(#pragma once
#include <stdexcept>
#include <Geode/platform/platform.hpp>
2022-10-13 04:31:23 -04:00
#include <Geode/c++stl/gdstdlib.hpp>
#include <cocos2d.h>
2022-10-13 05:56:23 -04:00
#include <cocos-ext.h>
#include <Geode/GeneratedPredeclare.hpp>
2022-10-13 05:56:23 -04:00
#include <Geode/Enums.hpp>
#include <Geode/utils/SeedValue.hpp>
2022-10-13 04:31:23 -04:00
)GEN";
char const* class_no_includes = R"GEN(#pragma once
#include <Geode/platform/platform.hpp>
2023-01-19 19:58:22 -05:00
#include <stdexcept>
2022-10-13 04:31:23 -04:00
)GEN";
char const* class_include_prereq = R"GEN(#include "{file_name}"
)GEN";
2022-07-30 12:24:03 -04:00
char const* class_start = R"GEN(
class {class_name}{base_classes} {{
public:
static constexpr auto CLASS_NAME = "{class_name}";
2022-07-30 12:24:03 -04:00
)GEN";
char const* custom_constructor = R"GEN( GEODE_CUSTOM_CONSTRUCTOR_GD({class_name}, {first_base})
)GEN";
char const* custom_constructor_cutoff = R"GEN( GEODE_CUSTOM_CONSTRUCTOR_CUTOFF({class_name}, {first_base})
2022-07-30 12:24:03 -04:00
)GEN";
2023-02-18 05:52:56 -05:00
char const* function_definition = R"GEN(
/**
{docs}{docs_addresses} */
2023-02-18 05:52:56 -05:00
{static}{virtual}{return_type} {function_name}({parameters}){const};
2022-07-30 12:24:03 -04:00
)GEN";
char const* error_definition = R"GEN(
private:
[[deprecated("{class_name}::{function_name} not implemented")]]
2023-02-18 05:52:56 -05:00
/**
{docs}{docs_addresses} */
{static}{virtual}{return_type} {function_name}({parameters}){const};
public:
2023-02-18 05:52:56 -05:00
)GEN";
2022-12-31 07:48:05 -05:00
2022-07-30 12:24:03 -04:00
char const* structor_definition = R"GEN(
2023-02-18 05:52:56 -05:00
/**
{docs}{docs_addresses} */
2023-02-18 05:52:56 -05:00
{function_name}({parameters});
)GEN";
2022-07-30 12:24:03 -04:00
// requires: type, member_name, array
char const* member_definition = R"GEN({private} {type} {member_name};{public}
2022-10-13 04:31:23 -04:00
)GEN";
2022-07-30 12:24:03 -04:00
2022-10-13 04:31:23 -04:00
char const* pad_definition = R"GEN( GEODE_PAD({hardcode});
)GEN";
2022-07-30 12:24:03 -04:00
2022-10-13 04:31:23 -04:00
char const* class_end = R"GEN(};
2022-07-30 12:24:03 -04:00
)GEN";
}}
2023-02-18 05:52:56 -05:00
inline std::string nameForPlatform(Platform platform) {
switch (platform) {
case Platform::Mac: return "MacOS";
case Platform::Windows: return "Windows";
case Platform::iOS: return "iOS";
case Platform::Android: return "Android";
default: // unreachable
return "Windows";
}
}
template <class T>
std::string generateAddressDocs(T const& f, PlatformNumber pn) {
2023-02-18 05:52:56 -05:00
std::string ret;
for (auto platform : {Platform::Mac, Platform::Windows, Platform::iOS, Platform::Android}) {
auto status = codegen::getStatusWithPlatform(platform, f);
2023-02-18 05:52:56 -05:00
if (status == BindStatus::NeedsBinding) {
ret += fmt::format(" * @note[short] {}: 0x{:x}\n",
nameForPlatform(platform),
codegen::platformNumberWithPlatform(platform, pn)
2023-02-18 05:52:56 -05:00
);
}
else if (status == BindStatus::Binded) {
ret += fmt::format(" * @note[short] {}\n",
nameForPlatform(platform)
);
}
}
return ret;
}
std::string generateDocs(std::string const& docs) {
if (docs.size() < 7) return "";
2023-02-18 06:24:24 -05:00
auto ret = docs.substr(1, docs.size() - 6); // i hate this but idk how to generalize
2023-02-18 05:52:56 -05:00
for (auto next = ret.find(" "); next != std::string::npos; next = ret.find(" ")) {
ret.replace(next, 8, " * ");
}
2023-02-18 06:24:24 -05:00
2023-02-18 05:52:56 -05:00
return ret;
}
std::string generateBindingHeader(Root const& root, ghc::filesystem::path const& singleFolder) {
2022-10-13 04:31:23 -04:00
std::string output;
2022-07-30 12:24:03 -04:00
{
std::string filename = "Standalones.hpp";
output += fmt::format(format_strings::binding_include,
fmt::arg("file_name", filename)
);
std::string single_output;
single_output += format_strings::class_includes;
for (auto& f : root.functions) {
FunctionProto const* fb = &f.prototype;
char const* used_format = format_strings::function_definition;
std::string addressDocs = generateAddressDocs(f, f.binds);
std::string docs = generateDocs(fb->docs);
single_output += fmt::format(used_format,
fmt::arg("virtual", ""),
fmt::arg("static", ""),
fmt::arg("class_name", ""),
fmt::arg("const", ""),
fmt::arg("function_name", fb->name),
fmt::arg("parameters", codegen::getParameters(*fb)),
fmt::arg("return_type", fb->ret.name),
fmt::arg("docs_addresses", addressDocs),
fmt::arg("docs", docs)
);
}
writeFile(singleFolder / filename, single_output);
}
2022-10-13 04:31:23 -04:00
for (auto& cls : root.classes) {
2023-04-02 10:22:03 -04:00
if (is_cocos_class(cls.name))
2022-07-30 12:24:03 -04:00
continue;
2022-10-13 04:31:23 -04:00
std::string filename = (codegen::getUnqualifiedClassName(cls.name) + ".hpp");
output += fmt::format(format_strings::binding_include,
fmt::arg("file_name", filename)
2022-07-30 12:24:03 -04:00
);
2022-10-13 04:31:23 -04:00
std::string single_output;
if (cls.name != "GDString") {
single_output += format_strings::class_includes;
} else {
single_output += format_strings::class_no_includes;
}
2022-10-13 04:31:23 -04:00
2022-10-13 05:56:23 -04:00
for (auto dep : cls.depends) {
2022-10-13 04:31:23 -04:00
if (can_find(dep, "cocos2d::")) continue;
std::string depfilename = (codegen::getUnqualifiedClassName(dep) + ".hpp");
single_output += fmt::format(format_strings::class_include_prereq, fmt::arg("file_name", depfilename));
}
2022-07-30 12:24:03 -04:00
std::string supers = str_if(
fmt::format(" : public {}", fmt::join(cls.superclasses, ", public ")),
2022-07-30 12:24:03 -04:00
!cls.superclasses.empty()
);
2022-10-13 04:31:23 -04:00
single_output += fmt::format(::format_strings::class_start,
2022-07-30 12:24:03 -04:00
fmt::arg("class_name", cls.name),
fmt::arg("base_classes", supers)
);
// what.
if (!cls.superclasses.empty()) {
2022-10-13 04:31:23 -04:00
single_output += fmt::format(
2023-04-02 10:22:03 -04:00
is_cocos_class(cls.superclasses[0])
? format_strings::custom_constructor_cutoff
: format_strings::custom_constructor,
2022-07-30 12:24:03 -04:00
fmt::arg("class_name", cls.name),
fmt::arg("first_base", cls.superclasses[0])
);
}
2022-10-13 04:31:23 -04:00
bool unimplementedField = false;
2022-07-30 12:24:03 -04:00
for (auto field : cls.fields) {
2023-08-15 05:46:49 -04:00
MemberFunctionProto* fb;
2022-07-30 12:24:03 -04:00
char const* used_format = format_strings::function_definition;
2023-02-18 05:52:56 -05:00
std::string addressDocs;
2022-07-30 12:24:03 -04:00
if (auto i = field.get_as<InlineField>()) {
2022-10-13 04:31:23 -04:00
single_output += "\t" + i->inner + "\n";
2022-07-30 12:24:03 -04:00
continue;
} else if (auto m = field.get_as<MemberField>()) {
2022-10-13 04:31:23 -04:00
single_output += fmt::format(format_strings::member_definition,
fmt::arg("private", unimplementedField ? "private:\n" : ""),
fmt::arg("public", unimplementedField ? "\npublic:" : ""),
2022-07-30 12:24:03 -04:00
fmt::arg("type", m->type.name),
fmt::arg("member_name", m->name + str_if(fmt::format("[{}]", m->count), m->count))
);
continue;
} else if (auto p = field.get_as<PadField>()) {
auto hardcode = codegen::platformNumber(p->amount);
if (hardcode) {
2022-10-13 04:31:23 -04:00
single_output += fmt::format(format_strings::pad_definition, fmt::arg("hardcode", hardcode));
2022-07-30 12:24:03 -04:00
} else {
2022-10-13 04:31:23 -04:00
unimplementedField = true;
2022-07-30 12:24:03 -04:00
}
continue;
} else if (auto fn = field.get_as<OutOfLineField>()) {
2023-08-15 05:46:49 -04:00
fb = &fn->prototype;
2023-02-18 05:52:56 -05:00
addressDocs = " * @note[short] Out of line\n";
2022-07-30 12:24:03 -04:00
} else if (auto fn = field.get_as<FunctionBindField>()) {
2023-08-15 05:46:49 -04:00
fb = &fn->prototype;
2022-07-30 12:24:03 -04:00
if (!codegen::platformNumber(fn->binds)) {
used_format = format_strings::error_definition;
if (fb->type != FunctionType::Normal)
continue;
}
2023-02-18 05:52:56 -05:00
addressDocs = generateAddressDocs(field, fn->binds);
2022-07-30 12:24:03 -04:00
}
2023-02-18 09:09:25 -05:00
std::string docs = generateDocs(fb->docs);
2022-10-13 04:31:23 -04:00
single_output += fmt::format(used_format,
2022-07-30 12:24:03 -04:00
fmt::arg("virtual", str_if("virtual ", fb->is_virtual)),
fmt::arg("static", str_if("static ", fb->is_static)),
fmt::arg("class_name", cls.name),
fmt::arg("const", str_if(" const ", fb->is_const)),
fmt::arg("function_name", fb->name),
fmt::arg("parameters", codegen::getParameters(*fb)),
fmt::arg("return_type", fb->ret.name),
2023-02-18 05:52:56 -05:00
fmt::arg("docs_addresses", addressDocs),
fmt::arg("docs", docs)
2022-07-30 12:24:03 -04:00
);
}
// if (hasClass)
2022-10-13 04:31:23 -04:00
single_output += ::format_strings::class_end;
2022-07-30 12:24:03 -04:00
2022-10-13 04:31:23 -04:00
writeFile(singleFolder / filename, single_output);
2022-07-30 12:24:03 -04:00
}
return output;
}