Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final refactor for bfsb_generator* and text generator #7806

Merged
merged 14 commits into from
Jan 31, 2023
1 change: 0 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion android/app/src/main/cpp/flatbuffers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion include/flatbuffers/code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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:
Expand Down
34 changes: 2 additions & 32 deletions include/flatbuffers/flatc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -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;
};
Expand Down
2 changes: 2 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 6 additions & 7 deletions src/bfbs_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <cstdint>

#include "flatbuffers/bfbs_generator.h"
#include "flatbuffers/code_generator.h"
#include "flatbuffers/reflection_generated.h"

namespace flatbuffers {
Expand Down Expand Up @@ -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 {
Kn99HN marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Expand All @@ -125,7 +124,7 @@ class BaseBfbsGenerator : public BfbsGenerator {
return FAILED_VERIFICATION;
}

GeneratorStatus status = GenerateFromSchema(schema_);
Status status = GenerateFromSchema(schema_);
schema_ = nullptr;
return status;
}
Expand Down
53 changes: 47 additions & 6 deletions src/bfbs_gen_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/bfbs_gen_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 51 additions & 6 deletions src/bfbs_gen_nim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/bfbs_gen_nim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading