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:
Khanh Nguyen
2023-01-31 09:35:34 -08:00
committed by GitHub
parent a6f4194489
commit 08ebd202e2
30 changed files with 357 additions and 222 deletions

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2021 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_BFBS_GENERATOR_H_
#define FLATBUFFERS_BFBS_GENERATOR_H_
#include <cstdint>
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;
};
} // namespace flatbuffers
#endif // FLATBUFFERS_BFBS_GENERATOR_H_

View File

@@ -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:

View File

@@ -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;
};