Skip to content

Commit

Permalink
removed extern code generation declarations preferring direct includes (
Browse files Browse the repository at this point in the history
#7948)

* removed extern code generation definitions, preferring direct includes

* add static to functions

* remove idl_gen_lua
  • Loading branch information
dbaileychess committed May 10, 2023
1 parent 16a7df4 commit 10b79d8
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 1,081 deletions.
1 change: 0 additions & 1 deletion .github/labeler.yml
Expand Up @@ -52,7 +52,6 @@ kotlin:
lua:
- '**/*.lua'
- lua/**/*
- src/idl_gen_lua.cpp
- src/bfbs_gen_lua.cpp

lobster:
Expand Down
1 change: 0 additions & 1 deletion CMakeLists.txt
Expand Up @@ -166,7 +166,6 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_php.cpp
src/idl_gen_python.cpp
src/idl_gen_lobster.cpp
src/idl_gen_lua.cpp
src/idl_gen_rust.cpp
src/idl_gen_fbs.cpp
src/idl_gen_grpc.cpp
Expand Down
10 changes: 10 additions & 0 deletions include/flatbuffers/code_generator.h
Expand Up @@ -44,6 +44,16 @@ class CodeGenerator {
virtual Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) = 0;

// Generate code from the provided `parser` and place it in the output.
virtual Status GenerateCodeString(const Parser &parser,
const std::string &filename,
std::string &output) {
(void)parser;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}

// Generate code from the provided `buffer` of given `length`. The buffer is a
// serialized reflection.fbs.
virtual Status GenerateCode(const uint8_t *buffer, int64_t length) = 0;
Expand Down
4 changes: 4 additions & 0 deletions include/flatbuffers/code_generators.h
Expand Up @@ -229,6 +229,10 @@ class TypedFloatConstantGenerator : public FloatConstantGenerator {
const std::string neg_inf_number_;
};

std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
const std::string &path,
const std::string &file_name);

} // namespace flatbuffers

#endif // FLATBUFFERS_CODE_GENERATORS_H_
126 changes: 0 additions & 126 deletions include/flatbuffers/idl.h
Expand Up @@ -1217,129 +1217,6 @@ extern const char *GenerateTextFromTable(const Parser &parser, const void *table
std::string *text);
extern const char *GenerateText(const Parser &parser, const void *flatbuffer,
std::string *text);
extern const char *GenerateTextFile(const Parser &parser,
const std::string &path,
const std::string &file_name);

// Generate Json schema to string
// See idl_gen_json_schema.cpp.
extern bool GenerateJsonSchema(const Parser &parser, std::string *json);

// Generate binary files from a given FlatBuffer, and a given Parser
// object that has been populated with the corresponding schema.
// See code_generators.cpp.
extern bool GenerateBinary(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a C++ header from the definitions in the Parser object.
// See idl_gen_cpp.
extern bool GenerateCPP(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate C# files from the definitions in the Parser object.
// See idl_gen_csharp.cpp.
extern bool GenerateCSharp(const Parser &parser, const std::string &path,
const std::string &file_name);

extern bool GenerateDart(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Java files from the definitions in the Parser object.
// See idl_gen_java.cpp.
extern bool GenerateJava(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate JavaScript or TypeScript code from the definitions in the Parser
// object. See idl_gen_js.
extern bool GenerateTS(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Go files from the definitions in the Parser object.
// See idl_gen_go.cpp.
extern bool GenerateGo(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Php code from the definitions in the Parser object.
// See idl_gen_php.
extern bool GeneratePhp(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Python files from the definitions in the Parser object.
// See idl_gen_python.cpp.
extern bool GeneratePython(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Lobster files from the definitions in the Parser object.
// See idl_gen_lobster.cpp.
extern bool GenerateLobster(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Lua files from the definitions in the Parser object.
// See idl_gen_lua.cpp.
extern bool GenerateLua(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Rust files from the definitions in the Parser object.
// See idl_gen_rust.cpp.
extern bool GenerateRust(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Json schema file
// See idl_gen_json_schema.cpp.
extern bool GenerateJsonSchema(const Parser &parser, const std::string &path,
const std::string &file_name);

extern bool GenerateKotlin(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Swift classes.
// See idl_gen_swift.cpp
extern bool GenerateSwift(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a schema file from the internal representation, useful after
// parsing a .proto schema.
extern std::string GenerateFBS(const Parser &parser,
const std::string &file_name, bool no_log);
extern bool GenerateFBS(const Parser &parser, const std::string &path,
const std::string &file_name, bool no_log);

// Generate a make rule for the generated TypeScript code.
// See idl_gen_ts.cpp.
extern std::string TSMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a make rule for the generated C++ header.
// See idl_gen_cpp.cpp.
extern std::string CPPMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a make rule for the generated Dart code
// see idl_gen_dart.cpp
extern std::string DartMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a make rule for the generated Rust code.
// See idl_gen_rust.cpp.
extern std::string RustMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a make rule for generated Java or C# files.
// See code_generators.cpp.
extern std::string CSharpMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);
extern std::string JavaMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate a make rule for the generated text (JSON) files.
// See idl_gen_text.cpp.
extern std::string TextMakeRule(const Parser &parser, const std::string &path,
const std::string &file_names);

// Generate a make rule for the generated binary files.
// See code_generators.cpp.
extern std::string BinaryMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate GRPC Cpp interfaces.
// See idl_gen_grpc.cpp.
Expand Down Expand Up @@ -1368,9 +1245,6 @@ extern bool GenerateSwiftGRPC(const Parser &parser, const std::string &path,

extern bool GenerateTSGRPC(const Parser &parser, const std::string &path,
const std::string &file_name);

extern bool GenerateRustModuleRootFile(const Parser &parser,
const std::string &path);
} // namespace flatbuffers

#endif // FLATBUFFERS_IDL_H_
2 changes: 0 additions & 2 deletions src/BUILD.bazel
Expand Up @@ -125,8 +125,6 @@ cc_library(
"idl_gen_kotlin.h",
"idl_gen_lobster.cpp",
"idl_gen_lobster.h",
"idl_gen_lua.cpp",
"idl_gen_lua.h",
"idl_gen_php.cpp",
"idl_gen_php.h",
"idl_gen_python.cpp",
Expand Down
7 changes: 3 additions & 4 deletions src/bfbs_gen_lua.cpp
Expand Up @@ -88,10 +88,9 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {

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 GenerateCode(const Parser &, const std::string &,
const std::string &) FLATBUFFERS_OVERRIDE {
return Status::NOT_IMPLEMENTED;
}

Status GenerateMakeRule(const Parser &parser, const std::string &path,
Expand Down
58 changes: 1 addition & 57 deletions src/code_generators.cpp
Expand Up @@ -30,9 +30,7 @@

namespace flatbuffers {

namespace {

static std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
const std::string &path,
const std::string &file_name) {
const std::string file_extension = java ? ".java" : ".cs";
Expand Down Expand Up @@ -64,18 +62,6 @@ static std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
return make_rule;
}


static std::string BinaryFileName(const Parser &parser, const std::string &path,
const std::string &file_name) {
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
return path + file_name + "." + ext;
}

} // namespace




void CodeWriter::operator+=(std::string text) {
if (!ignore_ident_ && !text.empty()) AppendIdent(stream_);

Expand Down Expand Up @@ -346,48 +332,6 @@ std::string SimpleFloatConstantGenerator::NaN(float v) const {
return this->NaN(static_cast<double>(v));
}


std::string JavaMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
return JavaCSharpMakeRule(true, parser, path, file_name);
}
std::string CSharpMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
return JavaCSharpMakeRule(false, parser, path, file_name);
}

bool GenerateBinary(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (parser.opts.use_flexbuffers) {
auto data_vec = parser.flex_builder_.GetBuffer();
auto data_ptr = reinterpret_cast<char *>(data(data_vec));
return !parser.flex_builder_.GetSize() ||
flatbuffers::SaveFile(
BinaryFileName(parser, path, file_name).c_str(), data_ptr,
parser.flex_builder_.GetSize(), true);
}
return !parser.builder_.GetSize() ||
flatbuffers::SaveFile(
BinaryFileName(parser, path, file_name).c_str(),
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
parser.builder_.GetSize(), true);
}

std::string BinaryMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (!parser.builder_.GetSize()) return "";
std::string filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
std::string make_rule =
BinaryFileName(parser, path, filebase) + ": " + file_name;
auto included_files =
parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
make_rule += " " + *it;
}
return make_rule;
}

} // namespace flatbuffers

#if defined(_MSC_VER)
Expand Down
39 changes: 38 additions & 1 deletion src/idl_gen_binary.cpp
Expand Up @@ -31,9 +31,46 @@
#include "flatbuffers/util.h"

namespace flatbuffers {

namespace {

static std::string BinaryFileName(const Parser &parser, const std::string &path,
const std::string &file_name) {
auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
return path + file_name + "." + ext;
}

static bool GenerateBinary(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (parser.opts.use_flexbuffers) {
auto data_vec = parser.flex_builder_.GetBuffer();
auto data_ptr = reinterpret_cast<char *>(data(data_vec));
return !parser.flex_builder_.GetSize() ||
flatbuffers::SaveFile(
BinaryFileName(parser, path, file_name).c_str(), data_ptr,
parser.flex_builder_.GetSize(), true);
}
return !parser.builder_.GetSize() ||
flatbuffers::SaveFile(
BinaryFileName(parser, path, file_name).c_str(),
reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
parser.builder_.GetSize(), true);
}

static std::string BinaryMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
if (!parser.builder_.GetSize()) return "";
std::string filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
std::string make_rule =
BinaryFileName(parser, path, filebase) + ": " + file_name;
auto included_files =
parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
make_rule += " " + *it;
}
return make_rule;
}

class BinaryCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
Expand Down
4 changes: 2 additions & 2 deletions src/idl_gen_cpp.cpp
Expand Up @@ -4066,7 +4066,7 @@ class CppGenerator : public BaseGenerator {

} // namespace cpp

bool GenerateCPP(const Parser &parser, const std::string &path,
static bool GenerateCPP(const Parser &parser, const std::string &path,
const std::string &file_name) {
cpp::IDLOptionsCpp opts(parser.opts);
// The '--cpp_std' argument could be extended (like ASAN):
Expand Down Expand Up @@ -4105,7 +4105,7 @@ bool GenerateCPP(const Parser &parser, const std::string &path,
return generator.generate();
}

std::string CPPMakeRule(const Parser &parser, const std::string &path,
static std::string CPPMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
const auto filebase = StripPath(StripExtension(file_name));
cpp::CppGenerator geneartor(parser, path, file_name, parser.opts);
Expand Down
4 changes: 2 additions & 2 deletions src/idl_gen_csharp.cpp
Expand Up @@ -2498,7 +2498,7 @@ class CSharpGenerator : public BaseGenerator {
};
} // namespace csharp

bool GenerateCSharp(const Parser &parser, const std::string &path,
static bool GenerateCSharp(const Parser &parser, const std::string &path,
const std::string &file_name) {
csharp::CSharpGenerator generator(parser, path, file_name);
return generator.generate();
Expand All @@ -2523,7 +2523,7 @@ class CSharpCodeGenerator : public CodeGenerator {
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = CSharpMakeRule(parser, path, filename);
output = JavaCSharpMakeRule(false, parser, path, filename);
return Status::OK;
}

Expand Down
4 changes: 2 additions & 2 deletions src/idl_gen_dart.cpp
Expand Up @@ -1124,13 +1124,13 @@ class DartGenerator : public BaseGenerator {
};
} // namespace dart

bool GenerateDart(const Parser &parser, const std::string &path,
static bool GenerateDart(const Parser &parser, const std::string &path,
const std::string &file_name) {
dart::DartGenerator generator(parser, path, file_name);
return generator.generate();
}

std::string DartMakeRule(const Parser &parser, const std::string &path,
static std::string DartMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
auto filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
Expand Down

0 comments on commit 10b79d8

Please sign in to comment.