mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Final refactor for bfsb_generator* and text generator (#7806)
* Refactor BfbsGenerator to use CodeGenerator interface * Update * Refactor bfbs generator * Refactor bfbs generator for lua and nim. Remove old code that use Generator interface. * Update import * Update CMakeLists * Update BUILD file * Update BUILD file for src * Remove from Android CMakeLists and add error message * Update * Add generate root file function to Code Generator interface * Update * Update * Minor format fix
This commit is contained in:
@@ -41,7 +41,6 @@ filegroup(
|
||||
"include/flatbuffers/allocator.h",
|
||||
"include/flatbuffers/array.h",
|
||||
"include/flatbuffers/base.h",
|
||||
"include/flatbuffers/bfbs_generator.h",
|
||||
"include/flatbuffers/buffer.h",
|
||||
"include/flatbuffers/buffer_ref.h",
|
||||
"include/flatbuffers/code_generator.h",
|
||||
|
||||
@@ -122,7 +122,6 @@ set(FlatBuffers_Library_SRCS
|
||||
include/flatbuffers/allocator.h
|
||||
include/flatbuffers/array.h
|
||||
include/flatbuffers/base.h
|
||||
include/flatbuffers/bfbs_generator.h
|
||||
include/flatbuffers/buffer.h
|
||||
include/flatbuffers/buffer_ref.h
|
||||
include/flatbuffers/default_allocator.h
|
||||
|
||||
@@ -18,7 +18,6 @@ set(FlatBuffers_Library_SRCS
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/allocator.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/array.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/base.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/bfbs_generator.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/buffer.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/buffer_ref.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/default_allocator.h
|
||||
|
||||
@@ -32,7 +32,8 @@ class CodeGenerator {
|
||||
enum Status {
|
||||
OK = 0,
|
||||
ERROR = 1,
|
||||
NOT_IMPLEMENTED = 2,
|
||||
FAILED_VERIFICATION = 2,
|
||||
NOT_IMPLEMENTED = 3
|
||||
};
|
||||
|
||||
// Generate code from the provided `parser`.
|
||||
@@ -52,11 +53,17 @@ class CodeGenerator {
|
||||
virtual Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) = 0;
|
||||
|
||||
virtual Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) = 0;
|
||||
|
||||
virtual bool IsSchemaOnly() const = 0;
|
||||
|
||||
virtual bool SupportsBfbsGeneration() const = 0;
|
||||
|
||||
virtual bool SupportsRootFileGeneration() const = 0;
|
||||
|
||||
virtual IDLOptions::Language Language() const = 0;
|
||||
|
||||
virtual std::string LanguageName() const = 0;
|
||||
|
||||
protected:
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
#include "flatbuffers/code_generator.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
@@ -69,29 +68,6 @@ struct FlatCOption {
|
||||
|
||||
class FlatCompiler {
|
||||
public:
|
||||
// Output generator for the various programming languages and formats we
|
||||
// support.
|
||||
struct Generator {
|
||||
typedef bool (*GenerateFn)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
typedef std::string (*MakeRuleFn)(const flatbuffers::Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name);
|
||||
typedef bool (*ParsingCompletedFn)(const flatbuffers::Parser &parser,
|
||||
const std::string &output_path);
|
||||
|
||||
GenerateFn generate;
|
||||
const char *lang_name;
|
||||
bool schema_only;
|
||||
GenerateFn generateGRPC;
|
||||
flatbuffers::IDLOptions::Language lang;
|
||||
FlatCOption option;
|
||||
MakeRuleFn make_rule;
|
||||
BfbsGenerator *bfbs_generator;
|
||||
ParsingCompletedFn parsing_completed;
|
||||
};
|
||||
|
||||
typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
|
||||
bool show_exe_name);
|
||||
|
||||
@@ -100,14 +76,8 @@ class FlatCompiler {
|
||||
|
||||
// Parameters required to initialize the FlatCompiler.
|
||||
struct InitParams {
|
||||
InitParams()
|
||||
: generators(nullptr),
|
||||
num_generators(0),
|
||||
warn_fn(nullptr),
|
||||
error_fn(nullptr) {}
|
||||
InitParams() : warn_fn(nullptr), error_fn(nullptr) {}
|
||||
|
||||
const Generator *generators;
|
||||
size_t num_generators;
|
||||
WarnFn warn_fn;
|
||||
ErrorFn error_fn;
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@ cc_library(
|
||||
"code_generators.cpp",
|
||||
"idl_gen_fbs.cpp",
|
||||
"idl_gen_text.cpp",
|
||||
"idl_gen_text.h",
|
||||
"idl_parser.cpp",
|
||||
"reflection.cpp",
|
||||
"util.cpp",
|
||||
@@ -98,6 +99,7 @@ cc_library(
|
||||
"idl_gen_swift.cpp",
|
||||
"idl_gen_swift.h",
|
||||
"idl_gen_text.cpp",
|
||||
"idl_gen_text.h",
|
||||
"idl_gen_ts.cpp",
|
||||
"idl_gen_ts.h",
|
||||
"idl_namer.h",
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
#include "flatbuffers/code_generator.h"
|
||||
#include "flatbuffers/reflection_generated.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
@@ -96,20 +96,19 @@ static bool IsVector(const reflection::BaseType base_type) {
|
||||
|
||||
// A concrete base Flatbuffer Generator that specific language generators can
|
||||
// derive from.
|
||||
class BaseBfbsGenerator : public BfbsGenerator {
|
||||
class BaseBfbsGenerator : public CodeGenerator {
|
||||
public:
|
||||
virtual ~BaseBfbsGenerator() {}
|
||||
BaseBfbsGenerator() : schema_(nullptr) {}
|
||||
|
||||
virtual GeneratorStatus GenerateFromSchema(
|
||||
virtual Status GenerateFromSchema(
|
||||
const reflection::Schema *schema) = 0;
|
||||
|
||||
//
|
||||
virtual uint64_t SupportedAdvancedFeatures() const = 0;
|
||||
|
||||
// Override of the Generator::generate method that does the initial
|
||||
// Override of the Generator::GenerateCode method that does the initial
|
||||
// deserialization and verification steps.
|
||||
GeneratorStatus Generate(const uint8_t *buffer,
|
||||
Status GenerateCode(const uint8_t *buffer,
|
||||
int64_t length) FLATBUFFERS_OVERRIDE {
|
||||
flatbuffers::Verifier verifier(buffer, static_cast<size_t>(length));
|
||||
if (!reflection::VerifySchemaBuffer(verifier)) {
|
||||
@@ -125,7 +124,7 @@ class BaseBfbsGenerator : public BfbsGenerator {
|
||||
return FAILED_VERIFICATION;
|
||||
}
|
||||
|
||||
GeneratorStatus status = GenerateFromSchema(schema_);
|
||||
Status status = GenerateFromSchema(schema_);
|
||||
schema_ = nullptr;
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
// Ensure no includes to flatc internals. bfbs_gen.h and generator.h are OK.
|
||||
#include "bfbs_gen.h"
|
||||
#include "bfbs_namer.h"
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
|
||||
// The intermediate representation schema.
|
||||
#include "flatbuffers/reflection.h"
|
||||
@@ -79,15 +78,57 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
flatc_version_(flatc_version),
|
||||
namer_(LuaDefaultConfig(), LuaKeywords()) {}
|
||||
|
||||
GeneratorStatus GenerateFromSchema(const r::Schema *schema)
|
||||
FLATBUFFERS_OVERRIDE {
|
||||
if (!GenerateEnums(schema->enums())) { return FAILED; }
|
||||
Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
|
||||
if (!GenerateEnums(schema->enums())) { return ERROR; }
|
||||
if (!GenerateObjects(schema->objects(), schema->root_table())) {
|
||||
return FAILED;
|
||||
return ERROR;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
using BaseBfbsGenerator::GenerateCode;
|
||||
|
||||
Status GenerateCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) FLATBUFFERS_OVERRIDE {
|
||||
if (!GenerateLua(parser, path, filename)) { return ERROR; }
|
||||
return OK;
|
||||
}
|
||||
|
||||
Status GenerateMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &filename,
|
||||
std::string &output) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
(void)output;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return true; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kLua; }
|
||||
|
||||
std::string LanguageName() const override { return "Lua"; }
|
||||
|
||||
uint64_t SupportedAdvancedFeatures() const FLATBUFFERS_OVERRIDE {
|
||||
return 0xF;
|
||||
}
|
||||
@@ -625,7 +666,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<BfbsGenerator> NewLuaBfbsGenerator(
|
||||
std::unique_ptr<CodeGenerator> NewLuaBfbsGenerator(
|
||||
const std::string &flatc_version) {
|
||||
return std::unique_ptr<LuaBfbsGenerator>(new LuaBfbsGenerator(flatc_version));
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
#include "flatbuffers/code_generator.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Constructs a new Lua Code generator.
|
||||
std::unique_ptr<BfbsGenerator> NewLuaBfbsGenerator(
|
||||
std::unique_ptr<CodeGenerator> NewLuaBfbsGenerator(
|
||||
const std::string &flatc_version);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
// Ensure no includes to flatc internals. bfbs_gen.h and generator.h are OK.
|
||||
#include "bfbs_gen.h"
|
||||
#include "bfbs_namer.h"
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
|
||||
// The intermediate representation schema.
|
||||
#include "flatbuffers/reflection.h"
|
||||
@@ -96,8 +95,7 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
flatc_version_(flatc_version),
|
||||
namer_(NimDefaultConfig(), NimKeywords()) {}
|
||||
|
||||
GeneratorStatus GenerateFromSchema(const r::Schema *schema)
|
||||
FLATBUFFERS_OVERRIDE {
|
||||
Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
|
||||
ForAllEnums(schema->enums(), [&](const r::Enum *enum_def) {
|
||||
StartCodeBlock(enum_def);
|
||||
GenerateEnum(enum_def);
|
||||
@@ -109,6 +107,51 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
return OK;
|
||||
}
|
||||
|
||||
using BaseBfbsGenerator::GenerateCode;
|
||||
|
||||
Status GenerateCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
return NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &filename,
|
||||
std::string &output) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
(void)output;
|
||||
return NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
return NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return true; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kNim; }
|
||||
|
||||
std::string LanguageName() const override { return "Nim"; }
|
||||
|
||||
uint64_t SupportedAdvancedFeatures() const FLATBUFFERS_OVERRIDE {
|
||||
return r::AdvancedArrayFeatures | r::AdvancedUnionFeatures |
|
||||
r::OptionalScalars | r::DefaultVectorsAndStrings;
|
||||
@@ -472,9 +515,11 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
if (IsFloatingPoint(base_type)) {
|
||||
if (field->default_real() != field->default_real()) {
|
||||
return "NaN";
|
||||
} else if (field->default_real() == std::numeric_limits<double>::infinity()) {
|
||||
} else if (field->default_real() ==
|
||||
std::numeric_limits<double>::infinity()) {
|
||||
return "Inf";
|
||||
} else if (field->default_real() == -std::numeric_limits<double>::infinity()) {
|
||||
} else if (field->default_real() ==
|
||||
-std::numeric_limits<double>::infinity()) {
|
||||
return "-Inf";
|
||||
}
|
||||
return NumToString(field->default_real());
|
||||
@@ -639,7 +684,7 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<BfbsGenerator> NewNimBfbsGenerator(
|
||||
std::unique_ptr<CodeGenerator> NewNimBfbsGenerator(
|
||||
const std::string &flatc_version) {
|
||||
return std::unique_ptr<NimBfbsGenerator>(new NimBfbsGenerator(flatc_version));
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "flatbuffers/bfbs_generator.h"
|
||||
#include "flatbuffers/code_generator.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
// Constructs a new Nim Code generator.
|
||||
std::unique_ptr<BfbsGenerator> NewNimBfbsGenerator(
|
||||
std::unique_ptr<CodeGenerator> NewNimBfbsGenerator(
|
||||
const std::string &flatc_version);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
139
src/flatc.cpp
139
src/flatc.cpp
@@ -321,17 +321,11 @@ std::string FlatCompiler::GetShortUsageString(
|
||||
ss << ", ";
|
||||
}
|
||||
|
||||
// TODO(derekbailey): These should be generated from this.generators
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
const Generator &g = params_.generators[i];
|
||||
AppendShortOption(ss, g.option);
|
||||
ss << ", ";
|
||||
}
|
||||
|
||||
for (const FlatCOption &option : flatc_options) {
|
||||
AppendShortOption(ss, option);
|
||||
ss << ", ";
|
||||
}
|
||||
|
||||
ss.seekp(-2, ss.cur);
|
||||
ss << "]... FILE... [-- BINARY_FILE...]";
|
||||
std::string help = ss.str();
|
||||
@@ -349,14 +343,8 @@ std::string FlatCompiler::GetUsageString(
|
||||
for (const FlatCOption &option : language_options) {
|
||||
AppendOption(ss, option, 80, 25);
|
||||
}
|
||||
|
||||
// TODO(derekbailey): These should be generated from this.generators
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
const Generator &g = params_.generators[i];
|
||||
AppendOption(ss, g.option, 80, 25);
|
||||
}
|
||||
|
||||
ss << "\n";
|
||||
|
||||
for (const FlatCOption &option : flatc_options) {
|
||||
AppendOption(ss, option, 80, 25);
|
||||
}
|
||||
@@ -412,9 +400,6 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
|
||||
|
||||
FlatCOptions options;
|
||||
|
||||
// Default all generates to disabled.
|
||||
options.generator_enabled.resize(params_.num_generators, false);
|
||||
|
||||
options.program_name = std::string(argv[0]);
|
||||
|
||||
IDLOptions &opts = options.opts;
|
||||
@@ -641,41 +626,23 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
|
||||
} else {
|
||||
// Look up if the command line argument refers to a code generator.
|
||||
auto code_generator_it = code_generators_.find(arg);
|
||||
if (code_generator_it != code_generators_.end()) {
|
||||
std::shared_ptr<CodeGenerator> code_generator =
|
||||
code_generator_it->second;
|
||||
|
||||
// TODO(derekbailey): remove in favor of just checking if
|
||||
// generators.empty().
|
||||
options.any_generator = true;
|
||||
opts.lang_to_generate |= code_generator->Language();
|
||||
|
||||
if (code_generator->SupportsBfbsGeneration()) {
|
||||
opts.binary_schema_comments = true;
|
||||
options.requires_bfbs = true;
|
||||
}
|
||||
|
||||
options.generators.push_back(std::move(code_generator));
|
||||
} else {
|
||||
// TODO(derekbailey): deprecate the following logic in favor of the
|
||||
// code generator map above.
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
if (arg == "--" + params_.generators[i].option.long_opt ||
|
||||
arg == "-" + params_.generators[i].option.short_opt) {
|
||||
options.generator_enabled[i] = true;
|
||||
options.any_generator = true;
|
||||
opts.lang_to_generate |= params_.generators[i].lang;
|
||||
if (params_.generators[i].bfbs_generator) {
|
||||
opts.binary_schema_comments = true;
|
||||
options.requires_bfbs = true;
|
||||
}
|
||||
goto found;
|
||||
}
|
||||
}
|
||||
if (code_generator_it == code_generators_.end()) {
|
||||
Error("unknown commandline argument: " + arg, true);
|
||||
return options;
|
||||
}
|
||||
|
||||
found:;
|
||||
std::shared_ptr<CodeGenerator> code_generator =
|
||||
code_generator_it->second;
|
||||
|
||||
// TODO(derekbailey): remove in favor of just checking if
|
||||
// generators.empty().
|
||||
options.any_generator = true;
|
||||
opts.lang_to_generate |= code_generator->Language();
|
||||
|
||||
auto is_binary_schema = code_generator->SupportsBfbsGeneration();
|
||||
opts.binary_schema_comments = is_binary_schema;
|
||||
options.requires_bfbs = is_binary_schema;
|
||||
options.generators.push_back(std::move(code_generator));
|
||||
}
|
||||
} else {
|
||||
options.filenames.push_back(flatbuffers::PosixPath(argv[argi]));
|
||||
@@ -886,58 +853,6 @@ std::unique_ptr<Parser> FlatCompiler::GenerateCode(const FlatCOptions &options,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(derekbailey): Deprecate the following in favor to the above.
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
if (options.generator_enabled[i]) {
|
||||
if (!options.print_make_rules) {
|
||||
flatbuffers::EnsureDirExists(options.output_path);
|
||||
|
||||
// Prefer bfbs generators if present.
|
||||
if (params_.generators[i].bfbs_generator) {
|
||||
const GeneratorStatus status =
|
||||
params_.generators[i].bfbs_generator->Generate(bfbs_buffer,
|
||||
bfbs_length);
|
||||
if (status != OK) {
|
||||
Error(std::string("Unable to generate ") +
|
||||
params_.generators[i].lang_name + " for " + filebase +
|
||||
" using bfbs generator.");
|
||||
}
|
||||
} else {
|
||||
if ((!params_.generators[i].schema_only ||
|
||||
(is_schema || is_binary_schema)) &&
|
||||
!params_.generators[i].generate(*parser, options.output_path,
|
||||
filebase)) {
|
||||
Error(std::string("Unable to generate ") +
|
||||
params_.generators[i].lang_name + " for " + filebase);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (params_.generators[i].make_rule == nullptr) {
|
||||
Error(std::string("Cannot generate make rule for ") +
|
||||
params_.generators[i].lang_name);
|
||||
} else {
|
||||
std::string make_rule = params_.generators[i].make_rule(
|
||||
*parser, options.output_path, filename);
|
||||
if (!make_rule.empty())
|
||||
printf("%s\n",
|
||||
flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str());
|
||||
}
|
||||
}
|
||||
if (options.grpc_enabled) {
|
||||
if (params_.generators[i].generateGRPC != nullptr) {
|
||||
if (!params_.generators[i].generateGRPC(
|
||||
*parser, options.output_path, filebase)) {
|
||||
Error(std::string("Unable to generate GRPC interface for ") +
|
||||
params_.generators[i].lang_name);
|
||||
}
|
||||
} else {
|
||||
Warn(std::string("GRPC interface generator not implemented for ") +
|
||||
params_.generators[i].lang_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!opts.root_type.empty()) {
|
||||
if (!parser->SetRootType(opts.root_type.c_str()))
|
||||
Error("unknown root type: " + opts.root_type);
|
||||
@@ -956,10 +871,6 @@ std::unique_ptr<Parser> FlatCompiler::GenerateCode(const FlatCOptions &options,
|
||||
}
|
||||
|
||||
int FlatCompiler::Compile(const FlatCOptions &options) {
|
||||
if (params_.generators == nullptr || params_.num_generators == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO(derekbailey): change to std::optional<Parser>
|
||||
Parser conform_parser = GetConformParser(options);
|
||||
|
||||
@@ -1014,18 +925,16 @@ int FlatCompiler::Compile(const FlatCOptions &options) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (options.generators.empty()) {
|
||||
Error("No generator registered");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::unique_ptr<Parser> parser = GenerateCode(options, conform_parser);
|
||||
|
||||
// Once all the files have been parsed, run any generators Parsing Completed
|
||||
// function for final generation.
|
||||
for (size_t i = 0; i < params_.num_generators; ++i) {
|
||||
if (options.generator_enabled[i] &&
|
||||
params_.generators[i].parsing_completed != nullptr) {
|
||||
if (!params_.generators[i].parsing_completed(*parser,
|
||||
options.output_path)) {
|
||||
Error("failed running parsing completed for " +
|
||||
std::string(params_.generators[i].lang_name));
|
||||
}
|
||||
for (const auto &code_generator : options.generators) {
|
||||
if (code_generator->SupportsRootFileGeneration()) {
|
||||
code_generator->GenerateRootFile(*parser, options.output_path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "idl_gen_python.h"
|
||||
#include "idl_gen_rust.h"
|
||||
#include "idl_gen_swift.h"
|
||||
#include "idl_gen_text.h"
|
||||
#include "idl_gen_ts.h"
|
||||
|
||||
static const char *g_program_name = nullptr;
|
||||
@@ -70,34 +71,9 @@ void LogCompilerError(const std::string &err) {
|
||||
int main(int argc, const char *argv[]) {
|
||||
const std::string flatbuffers_version(flatbuffers::FLATBUFFERS_VERSION());
|
||||
|
||||
std::unique_ptr<flatbuffers::BfbsGenerator> bfbs_gen_lua =
|
||||
flatbuffers::NewLuaBfbsGenerator(flatbuffers_version);
|
||||
std::unique_ptr<flatbuffers::BfbsGenerator> bfbs_gen_nim =
|
||||
flatbuffers::NewNimBfbsGenerator(flatbuffers_version);
|
||||
|
||||
g_program_name = argv[0];
|
||||
|
||||
const flatbuffers::FlatCompiler::Generator generators[] = {
|
||||
{ flatbuffers::GenerateTextFile, "text", false, nullptr,
|
||||
flatbuffers::IDLOptions::kJson,
|
||||
flatbuffers::FlatCOption{
|
||||
"t", "json", "", "Generate text output for any data definitions" },
|
||||
|
||||
flatbuffers::TextMakeRule, nullptr, nullptr },
|
||||
{ flatbuffers::GenerateLua, "Lua", true, nullptr,
|
||||
flatbuffers::IDLOptions::kLua,
|
||||
flatbuffers::FlatCOption{ "l", "lua", "",
|
||||
"Generate Lua files for tables/structs" },
|
||||
nullptr, bfbs_gen_lua.get(), nullptr },
|
||||
{ nullptr, "Nim", true, nullptr, flatbuffers::IDLOptions::kNim,
|
||||
flatbuffers::FlatCOption{ "", "nim", "",
|
||||
"Generate Nim files for tables/structs" },
|
||||
nullptr, bfbs_gen_nim.get(), nullptr },
|
||||
};
|
||||
|
||||
flatbuffers::FlatCompiler::InitParams params;
|
||||
params.generators = generators;
|
||||
params.num_generators = sizeof(generators) / sizeof(generators[0]);
|
||||
params.warn_fn = Warn;
|
||||
params.error_fn = Error;
|
||||
|
||||
@@ -149,20 +125,35 @@ int main(int argc, const char *argv[]) {
|
||||
flatbuffers::NewLobsterCodeGenerator());
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "", "php", "",
|
||||
"Generate PHP files for tables/structs" },
|
||||
flatbuffers::NewPhpCodeGenerator());
|
||||
flatbuffers::FlatCOption{ "l", "lua", "",
|
||||
"Generate Lua files for tables/structs" },
|
||||
flatbuffers::NewLuaBfbsGenerator(flatbuffers_version));
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "", "nim", "",
|
||||
"Generate Nim files for tables/structs" },
|
||||
flatbuffers::NewNimBfbsGenerator(flatbuffers_version));
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "p", "python", "",
|
||||
"Generate Python files for tables/structs" },
|
||||
flatbuffers::NewPythonCodeGenerator());
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "", "php", "",
|
||||
"Generate PHP files for tables/structs" },
|
||||
flatbuffers::NewPhpCodeGenerator());
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "r", "rust", "",
|
||||
"Generate Rust files for tables/structs" },
|
||||
flatbuffers::NewRustCodeGenerator());
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{
|
||||
"t", "json", "", "Generate text output for any data definitions" },
|
||||
flatbuffers::NewTextCodeGenerator());
|
||||
|
||||
flatc.RegisterCodeGenerator(
|
||||
flatbuffers::FlatCOption{ "", "swift", "",
|
||||
"Generate Swift files for tables/structs" },
|
||||
|
||||
@@ -65,10 +65,19 @@ class BinaryCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return false; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kBinary; }
|
||||
|
||||
std::string LanguageName() const override { return "binary"; }
|
||||
|
||||
@@ -3998,10 +3998,19 @@ class CppCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kCpp; }
|
||||
|
||||
std::string LanguageName() const override { return "C++"; }
|
||||
|
||||
@@ -2289,10 +2289,19 @@ class CSharpCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kCSharp; }
|
||||
|
||||
std::string LanguageName() const override { return "CSharp"; }
|
||||
|
||||
@@ -1175,10 +1175,19 @@ class DartCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kDart; }
|
||||
|
||||
std::string LanguageName() const override { return "Dart"; }
|
||||
|
||||
@@ -1615,10 +1615,19 @@ class GoCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kGo; }
|
||||
|
||||
std::string LanguageName() const override { return "Go"; }
|
||||
|
||||
@@ -2198,10 +2198,19 @@ class JavaCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kJava; }
|
||||
|
||||
std::string LanguageName() const override { return "Java"; }
|
||||
|
||||
@@ -367,10 +367,18 @@ class JsonSchemaCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override {
|
||||
return IDLOptions::kJsonSchema;
|
||||
}
|
||||
|
||||
@@ -1632,10 +1632,18 @@ class KotlinCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kKotlin; }
|
||||
|
||||
std::string LanguageName() const override { return "Kotlin"; }
|
||||
|
||||
@@ -438,10 +438,19 @@ class LobsterCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override {
|
||||
return IDLOptions::kLobster;
|
||||
}
|
||||
|
||||
@@ -780,10 +780,19 @@ class LuaCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return true; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kLua; }
|
||||
|
||||
std::string LanguageName() const override { return "Lua"; }
|
||||
|
||||
@@ -979,10 +979,19 @@ class PhpCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kPhp; }
|
||||
|
||||
std::string LanguageName() const override { return "Php"; }
|
||||
|
||||
@@ -1946,9 +1946,17 @@ class PythonCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kPython; }
|
||||
|
||||
|
||||
@@ -3041,10 +3041,18 @@ class RustCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
if (!GenerateRustModuleRootFile(parser, path)) { return Status::ERROR; }
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return true; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kRust; }
|
||||
|
||||
std::string LanguageName() const override { return "Rust"; }
|
||||
|
||||
@@ -1937,10 +1937,19 @@ class SwiftCodeGenerator : public CodeGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kSwift; }
|
||||
|
||||
std::string LanguageName() const override { return "Swift"; }
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
#include "idl_gen_text.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "flatbuffers/code_generator.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/flexbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
@@ -431,4 +433,61 @@ std::string TextMakeRule(const Parser &parser, const std::string &path,
|
||||
return make_rule;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class TextCodeGenerator : public CodeGenerator {
|
||||
public:
|
||||
Status GenerateCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
if (!GenerateTextFile(parser, path, filename)) { return Status::ERROR; }
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
// Generate code from the provided `buffer` of given `length`. The buffer is a
|
||||
// serialized reflection.fbs.
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &filename,
|
||||
std::string &output) override {
|
||||
output = TextMakeRule(parser, path, filename);
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
bool IsSchemaOnly() const override { return false; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kJson; }
|
||||
|
||||
std::string LanguageName() const override { return "text"; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<CodeGenerator> NewTextCodeGenerator() {
|
||||
return std::unique_ptr<TextCodeGenerator>(new TextCodeGenerator());
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Google Inc. All rights reserved.
|
||||
* Copyright 2023 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,30 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef FLATBUFFERS_BFBS_GENERATOR_H_
|
||||
#define FLATBUFFERS_BFBS_GENERATOR_H_
|
||||
#ifndef FLATBUFFERS_IDL_GEN_TEXT_H_
|
||||
#define FLATBUFFERS_IDL_GEN_TEXT_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include "flatbuffers/code_generator.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
enum GeneratorStatus {
|
||||
OK,
|
||||
FAILED,
|
||||
FAILED_VERIFICATION,
|
||||
};
|
||||
|
||||
// A Flatbuffer Code Generator that receives a binary serialized reflection.fbs
|
||||
// and generates code from it.
|
||||
class BfbsGenerator {
|
||||
public:
|
||||
virtual ~BfbsGenerator() {}
|
||||
|
||||
// Generate code from the provided `buffer` of given `length`. The buffer is
|
||||
// a serialized reflection.fbs.
|
||||
virtual GeneratorStatus Generate(const uint8_t *buffer, int64_t length) = 0;
|
||||
};
|
||||
// Constructs a new Text code generator.
|
||||
std::unique_ptr<CodeGenerator> NewTextCodeGenerator();
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#endif // FLATBUFFERS_BFBS_GENERATOR_H_
|
||||
#endif // FLATBUFFERS_IDL_GEN_TEXT_H_
|
||||
@@ -2206,9 +2206,16 @@ class TsCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
bool IsSchemaOnly() const override { return true; }
|
||||
|
||||
bool SupportsBfbsGeneration() const override { return false; }
|
||||
bool SupportsRootFileGeneration() const override { return false; }
|
||||
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kTs; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user