diff --git a/BUILD.bazel b/BUILD.bazel index f88da4155d2..de910bc530b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -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", diff --git a/CMakeLists.txt b/CMakeLists.txt index 38f48a6bb28..27d80859f70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/android/app/src/main/cpp/flatbuffers/CMakeLists.txt b/android/app/src/main/cpp/flatbuffers/CMakeLists.txt index e1dd1e8d358..ee927153257 100644 --- a/android/app/src/main/cpp/flatbuffers/CMakeLists.txt +++ b/android/app/src/main/cpp/flatbuffers/CMakeLists.txt @@ -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 diff --git a/include/flatbuffers/code_generator.h b/include/flatbuffers/code_generator.h index 15baf46bd2a..85d4430cfab 100644 --- a/include/flatbuffers/code_generator.h +++ b/include/flatbuffers/code_generator.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: diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index b3730521968..a8fa9508776 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -23,7 +23,6 @@ #include #include -#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) {} - - const Generator *generators; - size_t num_generators; + InitParams() : warn_fn(nullptr), error_fn(nullptr) {} + WarnFn warn_fn; ErrorFn error_fn; }; diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 9f77d7f9ce8..3f4ba0c7f9b 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -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", diff --git a/src/bfbs_gen.h b/src/bfbs_gen.h index a18cdcb9920..ed20dc8965e 100644 --- a/src/bfbs_gen.h +++ b/src/bfbs_gen.h @@ -19,7 +19,7 @@ #include -#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(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; } diff --git a/src/bfbs_gen_lua.cpp b/src/bfbs_gen_lua.cpp index 2c140bb15cb..8823d912b17 100644 --- a/src/bfbs_gen_lua.cpp +++ b/src/bfbs_gen_lua.cpp @@ -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 NewLuaBfbsGenerator( +std::unique_ptr NewLuaBfbsGenerator( const std::string &flatc_version) { return std::unique_ptr(new LuaBfbsGenerator(flatc_version)); } diff --git a/src/bfbs_gen_lua.h b/src/bfbs_gen_lua.h index 9aa3801154b..86d97621ebf 100644 --- a/src/bfbs_gen_lua.h +++ b/src/bfbs_gen_lua.h @@ -20,12 +20,12 @@ #include #include -#include "flatbuffers/bfbs_generator.h" +#include "flatbuffers/code_generator.h" namespace flatbuffers { // Constructs a new Lua Code generator. -std::unique_ptr NewLuaBfbsGenerator( +std::unique_ptr NewLuaBfbsGenerator( const std::string &flatc_version); } // namespace flatbuffers diff --git a/src/bfbs_gen_nim.cpp b/src/bfbs_gen_nim.cpp index 6b2c130f956..45bd3c33d41 100644 --- a/src/bfbs_gen_nim.cpp +++ b/src/bfbs_gen_nim.cpp @@ -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::infinity()) { + } else if (field->default_real() == + std::numeric_limits::infinity()) { return "Inf"; - } else if (field->default_real() == -std::numeric_limits::infinity()) { + } else if (field->default_real() == + -std::numeric_limits::infinity()) { return "-Inf"; } return NumToString(field->default_real()); @@ -639,7 +684,7 @@ class NimBfbsGenerator : public BaseBfbsGenerator { }; } // namespace -std::unique_ptr NewNimBfbsGenerator( +std::unique_ptr NewNimBfbsGenerator( const std::string &flatc_version) { return std::unique_ptr(new NimBfbsGenerator(flatc_version)); } diff --git a/src/bfbs_gen_nim.h b/src/bfbs_gen_nim.h index 80be16d0141..39e8b218081 100644 --- a/src/bfbs_gen_nim.h +++ b/src/bfbs_gen_nim.h @@ -20,12 +20,12 @@ #include #include -#include "flatbuffers/bfbs_generator.h" +#include "flatbuffers/code_generator.h" namespace flatbuffers { // Constructs a new Nim Code generator. -std::unique_ptr NewNimBfbsGenerator( +std::unique_ptr NewNimBfbsGenerator( const std::string &flatc_version); } // namespace flatbuffers diff --git a/src/flatc.cpp b/src/flatc.cpp index dc7dcb9a320..0534cdffb6e 100644 --- a/src/flatc.cpp +++ b/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 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 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 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 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 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 = 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); } } diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index 882384471bd..52203b135eb 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -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 bfbs_gen_lua = - flatbuffers::NewLuaBfbsGenerator(flatbuffers_version); - std::unique_ptr 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" }, diff --git a/src/idl_gen_binary.cpp b/src/idl_gen_binary.cpp index a4ecd0def31..feb4e2f55e7 100644 --- a/src/idl_gen_binary.cpp +++ b/src/idl_gen_binary.cpp @@ -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"; } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 6fac430606e..90cc25fff01 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -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++"; } diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index 2811727bf20..9f384a7a01c 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -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"; } diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp index 93f18094b70..299409bac2d 100644 --- a/src/idl_gen_dart.cpp +++ b/src/idl_gen_dart.cpp @@ -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"; } diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index a293f9df477..6a66b5c6290 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -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"; } diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 66ccc5c9de0..9642551807f 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -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"; } diff --git a/src/idl_gen_json_schema.cpp b/src/idl_gen_json_schema.cpp index 27d6381d250..3849da856a2 100644 --- a/src/idl_gen_json_schema.cpp +++ b/src/idl_gen_json_schema.cpp @@ -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; } diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp index 84d817d9c80..3bf2bd6b07e 100644 --- a/src/idl_gen_kotlin.cpp +++ b/src/idl_gen_kotlin.cpp @@ -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"; } diff --git a/src/idl_gen_lobster.cpp b/src/idl_gen_lobster.cpp index 6cedceeb6d9..a8b0a6f7a5e 100644 --- a/src/idl_gen_lobster.cpp +++ b/src/idl_gen_lobster.cpp @@ -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; } diff --git a/src/idl_gen_lua.cpp b/src/idl_gen_lua.cpp index 3ce593d7bac..551a4b26f8a 100644 --- a/src/idl_gen_lua.cpp +++ b/src/idl_gen_lua.cpp @@ -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"; } diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index ba8c1633f22..222cc3d63ae 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -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"; } diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 222c0faa6f8..5b5ce353a01 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -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; } diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 7a5e4a534e4..ac6097faae4 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -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"; } diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index ae1de97d8c3..d85b71839f3 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -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"; } diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index 52f854dd458..9de3a6d378c 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -15,9 +15,11 @@ */ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_text.h" #include +#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 NewTextCodeGenerator() { + return std::unique_ptr(new TextCodeGenerator()); +} + } // namespace flatbuffers diff --git a/include/flatbuffers/bfbs_generator.h b/src/idl_gen_text.h similarity index 50% rename from include/flatbuffers/bfbs_generator.h rename to src/idl_gen_text.h index 08faeb3eb50..3179a4cfa12 100644 --- a/include/flatbuffers/bfbs_generator.h +++ b/src/idl_gen_text.h @@ -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 +#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 NewTextCodeGenerator(); } // namespace flatbuffers -#endif // FLATBUFFERS_BFBS_GENERATOR_H_ +#endif // FLATBUFFERS_IDL_GEN_TEXT_H_ diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index 84c7e9c7c0e..a3e1f1274f6 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -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; }