mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-10 15:16:28 +00:00
fixed bfbs gen to pass extra options (#7949)
This commit is contained in:
@@ -101,15 +101,15 @@ class BaseBfbsGenerator : public CodeGenerator {
|
||||
virtual ~BaseBfbsGenerator() {}
|
||||
BaseBfbsGenerator() : schema_(nullptr) {}
|
||||
|
||||
virtual Status GenerateFromSchema(
|
||||
const reflection::Schema *schema) = 0;
|
||||
virtual Status GenerateFromSchema(const reflection::Schema *schema,
|
||||
const CodeGenOptions &options) = 0;
|
||||
|
||||
virtual uint64_t SupportedAdvancedFeatures() const = 0;
|
||||
|
||||
// Override of the Generator::GenerateCode method that does the initial
|
||||
// deserialization and verification steps.
|
||||
Status GenerateCode(const uint8_t *buffer,
|
||||
int64_t length) FLATBUFFERS_OVERRIDE {
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length,
|
||||
const CodeGenOptions &options) FLATBUFFERS_OVERRIDE {
|
||||
flatbuffers::Verifier verifier(buffer, static_cast<size_t>(length));
|
||||
if (!reflection::VerifySchemaBuffer(verifier)) {
|
||||
return FAILED_VERIFICATION;
|
||||
@@ -124,7 +124,7 @@ class BaseBfbsGenerator : public CodeGenerator {
|
||||
return FAILED_VERIFICATION;
|
||||
}
|
||||
|
||||
Status status = GenerateFromSchema(schema_);
|
||||
Status status = GenerateFromSchema(schema_, options);
|
||||
schema_ = nullptr;
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "bfbs_namer.h"
|
||||
|
||||
// The intermediate representation schema.
|
||||
#include "flatbuffers/code_generator.h"
|
||||
#include "flatbuffers/reflection.h"
|
||||
#include "flatbuffers/reflection_generated.h"
|
||||
|
||||
@@ -78,7 +79,10 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
flatc_version_(flatc_version),
|
||||
namer_(LuaDefaultConfig(), LuaKeywords()) {}
|
||||
|
||||
Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
|
||||
Status GenerateFromSchema(const r::Schema *schema,
|
||||
const CodeGenOptions &options)
|
||||
FLATBUFFERS_OVERRIDE {
|
||||
options_ = options;
|
||||
if (!GenerateEnums(schema->enums())) { return ERROR; }
|
||||
if (!GenerateObjects(schema->objects(), schema->root_table())) {
|
||||
return ERROR;
|
||||
@@ -89,7 +93,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
using BaseBfbsGenerator::GenerateCode;
|
||||
|
||||
Status GenerateCode(const Parser &, const std::string &,
|
||||
const std::string &) FLATBUFFERS_OVERRIDE {
|
||||
const std::string &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
@@ -652,12 +656,14 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
// TODO(derekbailey): figure out a save file without depending on util.h
|
||||
EnsureDirExists(path);
|
||||
const std::string file_name = path + "/" + namer_.File(name);
|
||||
const std::string file_name = options_.output_path + path + "/" + namer_.File(name);
|
||||
SaveFile(file_name.c_str(), code, false);
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> keywords_;
|
||||
std::map<std::string, std::string> requires_;
|
||||
CodeGenOptions options_;
|
||||
|
||||
const r::Object *current_obj_;
|
||||
const r::Enum *current_enum_;
|
||||
const std::string flatc_version_;
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "bfbs_namer.h"
|
||||
|
||||
// The intermediate representation schema.
|
||||
#include "flatbuffers/code_generator.h"
|
||||
#include "flatbuffers/reflection.h"
|
||||
#include "flatbuffers/reflection_generated.h"
|
||||
|
||||
@@ -95,7 +96,10 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
flatc_version_(flatc_version),
|
||||
namer_(NimDefaultConfig(), NimKeywords()) {}
|
||||
|
||||
Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
|
||||
Status GenerateFromSchema(const r::Schema *schema,
|
||||
const CodeGenOptions &options)
|
||||
FLATBUFFERS_OVERRIDE {
|
||||
options_ = options;
|
||||
ForAllEnums(schema->enums(), [&](const r::Enum *enum_def) {
|
||||
StartCodeBlock(enum_def);
|
||||
GenerateEnum(enum_def);
|
||||
@@ -671,12 +675,15 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
// TODO(derekbailey): figure out a save file without depending on util.h
|
||||
EnsureDirExists(path);
|
||||
const std::string file_name = path + "/" + namer_.File(name);
|
||||
const std::string file_name =
|
||||
options_.output_path + path + "/" + namer_.File(name);
|
||||
SaveFile(file_name.c_str(), code, false);
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> keywords_;
|
||||
std::map<std::string, std::string> imports_;
|
||||
CodeGenOptions options_;
|
||||
|
||||
const r::Object *current_obj_;
|
||||
const r::Enum *current_enum_;
|
||||
const std::string flatc_version_;
|
||||
|
||||
@@ -866,8 +866,11 @@ std::unique_ptr<Parser> FlatCompiler::GenerateCode(const FlatCOptions &options,
|
||||
|
||||
// Prefer bfbs generators if present.
|
||||
if (code_generator->SupportsBfbsGeneration()) {
|
||||
const CodeGenerator::Status status =
|
||||
code_generator->GenerateCode(bfbs_buffer, bfbs_length);
|
||||
CodeGenOptions code_gen_options;
|
||||
code_gen_options.output_path = options.output_path;
|
||||
|
||||
const CodeGenerator::Status status = code_generator->GenerateCode(
|
||||
bfbs_buffer, bfbs_length, code_gen_options);
|
||||
if (status != CodeGenerator::Status::OK) {
|
||||
Error("Unable to generate " + code_generator->LanguageName() +
|
||||
" for " + filebase + code_generator->status_detail +
|
||||
|
||||
@@ -34,13 +34,13 @@ namespace flatbuffers {
|
||||
namespace {
|
||||
|
||||
static std::string BinaryFileName(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
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) {
|
||||
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));
|
||||
@@ -57,7 +57,7 @@ static bool GenerateBinary(const Parser &parser, const std::string &path,
|
||||
}
|
||||
|
||||
static std::string BinaryMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
if (!parser.builder_.GetSize()) return "";
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
@@ -81,9 +81,8 @@ class BinaryCodeGenerator : public CodeGenerator {
|
||||
|
||||
// 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;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -4067,7 +4067,7 @@ class CppGenerator : public BaseGenerator {
|
||||
} // namespace cpp
|
||||
|
||||
static bool GenerateCPP(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
cpp::IDLOptionsCpp opts(parser.opts);
|
||||
// The '--cpp_std' argument could be extended (like ASAN):
|
||||
// Example: "flatc --cpp_std c++17:option1:option2".
|
||||
@@ -4106,7 +4106,7 @@ static bool GenerateCPP(const Parser &parser, const std::string &path,
|
||||
}
|
||||
|
||||
static std::string CPPMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
const auto filebase = StripPath(StripExtension(file_name));
|
||||
cpp::CppGenerator geneartor(parser, path, file_name, parser.opts);
|
||||
const auto included_files = parser.GetIncludedFilesRecursive(file_name);
|
||||
@@ -4130,9 +4130,8 @@ class CppCodeGenerator : public CodeGenerator {
|
||||
|
||||
// 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;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -625,7 +625,8 @@ class CSharpGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Get the value of a table verification function start
|
||||
void GetStartOfTableVerifier(const StructDef &struct_def, std::string *code_ptr) {
|
||||
void GetStartOfTableVerifier(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "\n";
|
||||
code += "static public class " + struct_def.name + "Verify\n";
|
||||
@@ -645,17 +646,18 @@ class CSharpGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string GetNestedFlatBufferName(const FieldDef &field) {
|
||||
std::string name;
|
||||
std::string name;
|
||||
if (field.nested_flatbuffer) {
|
||||
name = NamespacedName(*field.nested_flatbuffer);
|
||||
} else {
|
||||
name = "";
|
||||
}
|
||||
return name ;
|
||||
return name;
|
||||
}
|
||||
|
||||
// Generate the code to call the appropriate Verify function(s) for a field.
|
||||
void GenVerifyCall(CodeWriter &code_, const FieldDef &field, const char *prefix) {
|
||||
void GenVerifyCall(CodeWriter &code_, const FieldDef &field,
|
||||
const char *prefix) {
|
||||
code_.SetValue("PRE", prefix);
|
||||
code_.SetValue("NAME", ConvertCase(field.name, Case::kUpperCamel));
|
||||
code_.SetValue("REQUIRED", field.IsRequired() ? "Required" : "");
|
||||
@@ -663,14 +665,16 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code_.SetValue("TYPE", GenTypeGet(field.value.type));
|
||||
code_.SetValue("INLINESIZE", NumToString(InlineSize(field.value.type)));
|
||||
code_.SetValue("OFFSET", NumToString(field.value.offset));
|
||||
|
||||
|
||||
if (IsScalar(field.value.type.base_type) || IsStruct(field.value.type)) {
|
||||
code_.SetValue("ALIGN", NumToString(InlineAlignment(field.value.type)));
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyField(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{INLINESIZE}} /*{{TYPE}}*/, {{ALIGN}}, {{REQUIRED_FLAG}})";
|
||||
"{{PRE}} && verifier.VerifyField(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{INLINESIZE}} /*{{TYPE}}*/, {{ALIGN}}, "
|
||||
"{{REQUIRED_FLAG}})";
|
||||
} else {
|
||||
// TODO - probably code below should go to this 'else' - code_ += "{{PRE}}VerifyOffset{{REQUIRED}}(verifier, {{OFFSET}})\\";
|
||||
// TODO - probably code below should go to this 'else' - code_ +=
|
||||
// "{{PRE}}VerifyOffset{{REQUIRED}}(verifier, {{OFFSET}})\\";
|
||||
}
|
||||
|
||||
switch (field.value.type.base_type) {
|
||||
@@ -679,37 +683,47 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code_.SetValue("ENUM_NAME1", field.value.type.enum_def->name);
|
||||
code_.SetValue("ENUM_NAME", union_name);
|
||||
code_.SetValue("SUFFIX", UnionTypeFieldSuffix());
|
||||
// Caution: This construction assumes, that UNION type id element has been created just before union data and
|
||||
// its offset precedes union. Such assumption is common in flatbuffer implementation
|
||||
code_.SetValue("TYPE_ID_OFFSET", NumToString(field.value.offset - sizeof(voffset_t)));
|
||||
code_ += "{{PRE}} && verifier.VerifyUnion(tablePos, {{TYPE_ID_OFFSET}}, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{ENUM_NAME}}Verify.Verify, {{REQUIRED_FLAG}})";
|
||||
// Caution: This construction assumes, that UNION type id element has
|
||||
// been created just before union data and its offset precedes union.
|
||||
// Such assumption is common in flatbuffer implementation
|
||||
code_.SetValue("TYPE_ID_OFFSET",
|
||||
NumToString(field.value.offset - sizeof(voffset_t)));
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyUnion(tablePos, "
|
||||
"{{TYPE_ID_OFFSET}}, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{ENUM_NAME}}Verify.Verify, "
|
||||
"{{REQUIRED_FLAG}})";
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
if (!field.value.type.struct_def->fixed) {
|
||||
code_ += "{{PRE}} && verifier.VerifyTable(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{TYPE}}Verify.Verify, {{REQUIRED_FLAG}})";
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyTable(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{TYPE}}Verify.Verify, "
|
||||
"{{REQUIRED_FLAG}})";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRING: {
|
||||
code_ += "{{PRE}} && verifier.VerifyString(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{REQUIRED_FLAG}})";
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyString(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{REQUIRED_FLAG}})";
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_VECTOR: {
|
||||
|
||||
switch (field.value.type.element) {
|
||||
case BASE_TYPE_STRING: {
|
||||
code_ += "{{PRE}} && verifier.VerifyVectorOfStrings(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{REQUIRED_FLAG}})";
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyVectorOfStrings(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{REQUIRED_FLAG}})";
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
if (!field.value.type.struct_def->fixed) {
|
||||
code_ += "{{PRE}} && verifier.VerifyVectorOfTables(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{TYPE}}Verify.Verify, {{REQUIRED_FLAG}})";
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyVectorOfTables(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{TYPE}}Verify.Verify, "
|
||||
"{{REQUIRED_FLAG}})";
|
||||
} else {
|
||||
code_.SetValue(
|
||||
"VECTOR_ELEM_INLINESIZE",
|
||||
@@ -733,16 +747,22 @@ class CSharpGenerator : public BaseGenerator {
|
||||
if (!nfn.empty()) {
|
||||
code_.SetValue("CPP_NAME", nfn);
|
||||
// FIXME: file_identifier.
|
||||
code_ += "{{PRE}} && verifier.VerifyNestedBuffer(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{CPP_NAME}}Verify.Verify, {{REQUIRED_FLAG}})";
|
||||
} else if (field.flexbuffer) {
|
||||
code_ += "{{PRE}} && verifier.VerifyNestedBuffer(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, null, {{REQUIRED_FLAG}})";
|
||||
} else {
|
||||
code_.SetValue("VECTOR_ELEM_INLINESIZE", NumToString(InlineSize(field.value.type.VectorType())));
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyVectorOfData(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{VECTOR_ELEM_INLINESIZE}} /*{{TYPE}}*/, {{REQUIRED_FLAG}})";
|
||||
"{{PRE}} && verifier.VerifyNestedBuffer(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{CPP_NAME}}Verify.Verify, "
|
||||
"{{REQUIRED_FLAG}})";
|
||||
} else if (field.flexbuffer) {
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyNestedBuffer(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, null, {{REQUIRED_FLAG}})";
|
||||
} else {
|
||||
code_.SetValue(
|
||||
"VECTOR_ELEM_INLINESIZE",
|
||||
NumToString(InlineSize(field.value.type.VectorType())));
|
||||
code_ +=
|
||||
"{{PRE}} && verifier.VerifyVectorOfData(tablePos, "
|
||||
"{{OFFSET}} /*{{NAME}}*/, {{VECTOR_ELEM_INLINESIZE}} "
|
||||
"/*{{TYPE}}*/, {{REQUIRED_FLAG}})";
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -758,7 +778,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// Generate table constructors, conditioned on its members' types.
|
||||
void GenTableVerifier(const StructDef &struct_def, std::string *code_ptr) {
|
||||
CodeWriter code_;
|
||||
|
||||
|
||||
GetStartOfTableVerifier(struct_def, code_ptr);
|
||||
|
||||
// Generate struct fields accessors
|
||||
@@ -771,7 +791,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
*code_ptr += code_.ToString();
|
||||
|
||||
|
||||
GetEndOfTableVerifier(code_ptr);
|
||||
}
|
||||
|
||||
@@ -787,7 +807,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// verification - instead structure size is verified using VerifyField
|
||||
} else {
|
||||
// Create table verification function
|
||||
GenTableVerifier(struct_def, code_ptr);
|
||||
GenTableVerifier(struct_def, code_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1602,8 +1622,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
if (union_type.enum_def) {
|
||||
const auto &enum_def = *union_type.enum_def;
|
||||
|
||||
auto ret =
|
||||
"\n\nstatic public class " + enum_def.name + "Verify\n";
|
||||
auto ret = "\n\nstatic public class " + enum_def.name + "Verify\n";
|
||||
ret += "{\n";
|
||||
ret +=
|
||||
" static public bool Verify(Google.FlatBuffers.Verifier verifier, "
|
||||
@@ -1615,25 +1634,26 @@ class CSharpGenerator : public BaseGenerator {
|
||||
ret += " switch((" + enum_def.name + ")typeId)\n";
|
||||
ret += " {\n";
|
||||
|
||||
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
|
||||
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
|
||||
++it) {
|
||||
const auto &ev = **it;
|
||||
if (ev.IsZero()) { continue; }
|
||||
|
||||
ret += " case " + Name(enum_def) + "." + Name(ev) + ":\n";
|
||||
|
||||
if (IsString(ev.union_type)) {
|
||||
ret +=
|
||||
" result = verifier.VerifyUnionString(tablePos);\n";
|
||||
ret += " result = verifier.VerifyUnionString(tablePos);\n";
|
||||
ret += " break;";
|
||||
} else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
|
||||
if (! ev.union_type.struct_def->fixed) {
|
||||
if (!ev.union_type.struct_def->fixed) {
|
||||
auto type = GenTypeGet(ev.union_type);
|
||||
ret += " result = " + type + "Verify.Verify(verifier, tablePos);\n";
|
||||
ret += " result = " + type +
|
||||
"Verify.Verify(verifier, tablePos);\n";
|
||||
} else {
|
||||
ret += " result = verifier.VerifyUnionData(tablePos, " +
|
||||
NumToString(InlineSize(ev.union_type)) + ", " +
|
||||
NumToString(InlineAlignment(ev.union_type)) +
|
||||
");\n";;
|
||||
NumToString(InlineSize(ev.union_type)) + ", " +
|
||||
NumToString(InlineAlignment(ev.union_type)) + ");\n";
|
||||
;
|
||||
}
|
||||
ret += " break;";
|
||||
} else {
|
||||
@@ -1676,7 +1696,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// Type
|
||||
code += " public " + enum_def.name + " Type { get; set; }\n";
|
||||
// Value
|
||||
code += " public object " + class_member + " { get; set; }\n";
|
||||
code += " public object " + class_member + " { get; set; }\n";
|
||||
code += "\n";
|
||||
// Constructor
|
||||
code += " public " + union_name + "() {\n";
|
||||
@@ -1736,7 +1756,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
code += "}\n\n";
|
||||
|
||||
code += GenUnionVerify(enum_def.underlying_type);
|
||||
|
||||
|
||||
// JsonConverter
|
||||
if (opts.cs_gen_json_serializer) {
|
||||
if (enum_def.attributes.Lookup("private")) {
|
||||
@@ -1773,7 +1793,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
" _o, "
|
||||
"Newtonsoft.Json.JsonSerializer serializer) {\n";
|
||||
code += " if (_o == null) return;\n";
|
||||
code += " serializer.Serialize(writer, _o." + class_member + ");\n";
|
||||
code += " serializer.Serialize(writer, _o." + class_member + ");\n";
|
||||
code += " }\n";
|
||||
code +=
|
||||
" public override object ReadJson(Newtonsoft.Json.JsonReader "
|
||||
@@ -2499,7 +2519,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
} // namespace csharp
|
||||
|
||||
static bool GenerateCSharp(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
csharp::CSharpGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -2514,9 +2534,8 @@ class CSharpCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,8 +90,8 @@ class DartGenerator : public BaseGenerator {
|
||||
|
||||
template<typename T>
|
||||
void import_generator(const std::vector<T *> &definitions,
|
||||
const std::string &included,
|
||||
std::set<std::string> &imports) {
|
||||
const std::string &included,
|
||||
std::set<std::string> &imports) {
|
||||
for (const auto &item : definitions) {
|
||||
if (item->file == included) {
|
||||
std::string component = namer_.Namespace(*item->defined_namespace);
|
||||
@@ -760,9 +760,7 @@ class DartGenerator : public BaseGenerator {
|
||||
|
||||
std::string getDefaultValue(const Value &value) const {
|
||||
if (!value.constant.empty() && value.constant != "0") {
|
||||
if (IsBool(value.type.base_type)) {
|
||||
return "true";
|
||||
}
|
||||
if (IsBool(value.type.base_type)) { return "true"; }
|
||||
if (IsScalar(value.type.base_type)) {
|
||||
if (StringIsFlatbufferNan(value.constant)) {
|
||||
return "double.nan";
|
||||
@@ -1125,13 +1123,13 @@ class DartGenerator : public BaseGenerator {
|
||||
} // namespace dart
|
||||
|
||||
static bool GenerateDart(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
dart::DartGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
|
||||
static std::string DartMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
auto filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
dart::DartGenerator generator(parser, path, file_name);
|
||||
@@ -1154,9 +1152,8 @@ class DartCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -253,8 +253,9 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema,
|
||||
}
|
||||
|
||||
// Generate a flatbuffer schema from the Parser's internal representation.
|
||||
static std::string GenerateFBS(const Parser &parser, const std::string &file_name,
|
||||
bool no_log = false) {
|
||||
static std::string GenerateFBS(const Parser &parser,
|
||||
const std::string &file_name,
|
||||
bool no_log = false) {
|
||||
// Proto namespaces may clash with table names, escape the ones that were
|
||||
// generated from a table:
|
||||
for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end();
|
||||
@@ -376,7 +377,7 @@ static std::string GenerateFBS(const Parser &parser, const std::string &file_nam
|
||||
}
|
||||
|
||||
static bool GenerateFBS(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name, bool no_log = false) {
|
||||
const std::string &file_name, bool no_log = false) {
|
||||
const std::string fbs = GenerateFBS(parser, file_name, no_log);
|
||||
if (fbs.empty()) { return false; }
|
||||
// TODO: Use LogCompilerWarn
|
||||
@@ -388,7 +389,6 @@ static bool GenerateFBS(const Parser &parser, const std::string &path,
|
||||
return SaveFile((path + file_name + ".fbs").c_str(), fbs, false);
|
||||
}
|
||||
|
||||
|
||||
class FBSCodeGenerator : public CodeGenerator {
|
||||
public:
|
||||
explicit FBSCodeGenerator(const bool no_log) : no_log_(no_log) {}
|
||||
@@ -400,16 +400,15 @@ class FBSCodeGenerator : public CodeGenerator {
|
||||
}
|
||||
|
||||
Status GenerateCodeString(const Parser &parser, const std::string &filename,
|
||||
std::string &output) override {
|
||||
std::string &output) override {
|
||||
output = GenerateFBS(parser, filename, no_log_);
|
||||
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;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -1607,7 +1607,7 @@ class GoGenerator : public BaseGenerator {
|
||||
} // namespace go
|
||||
|
||||
static bool GenerateGo(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
go::GoGenerator generator(parser, path, file_name, parser.opts.go_namespace);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -1622,9 +1622,8 @@ class GoCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,9 +69,10 @@ static std::set<std::string> JavaKeywords() {
|
||||
};
|
||||
}
|
||||
|
||||
static const TypedFloatConstantGenerator JavaFloatGen("Double.", "Float.", "NaN",
|
||||
"POSITIVE_INFINITY",
|
||||
"NEGATIVE_INFINITY");
|
||||
static const TypedFloatConstantGenerator JavaFloatGen("Double.", "Float.",
|
||||
"NaN",
|
||||
"POSITIVE_INFINITY",
|
||||
"NEGATIVE_INFINITY");
|
||||
|
||||
static const CommentConfig comment_config = {
|
||||
"/**",
|
||||
@@ -79,7 +80,7 @@ static const CommentConfig comment_config = {
|
||||
" */",
|
||||
};
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
class JavaGenerator : public BaseGenerator {
|
||||
struct FieldArrayLength {
|
||||
@@ -89,16 +90,15 @@ class JavaGenerator : public BaseGenerator {
|
||||
|
||||
public:
|
||||
JavaGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name,
|
||||
const std::string &package_prefix)
|
||||
const std::string &file_name, const std::string &package_prefix)
|
||||
: BaseGenerator(parser, path, file_name, "", ".", "java"),
|
||||
cur_name_space_(nullptr),
|
||||
cur_name_space_(nullptr),
|
||||
namer_(WithFlagOptions(JavaDefaultConfig(), parser.opts, path),
|
||||
JavaKeywords()) {
|
||||
if (!package_prefix.empty()) {
|
||||
std::istringstream iss(package_prefix);
|
||||
std::string component;
|
||||
while(std::getline(iss, component, '.')) {
|
||||
while (std::getline(iss, component, '.')) {
|
||||
package_prefix_ns_.components.push_back(component);
|
||||
}
|
||||
package_prefix_ = package_prefix_ns_.GetFullyQualifiedName("") + ".";
|
||||
@@ -184,10 +184,8 @@ class JavaGenerator : public BaseGenerator {
|
||||
code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
|
||||
|
||||
Namespace combined_ns = package_prefix_ns_;
|
||||
std::copy(
|
||||
ns.components.begin(),
|
||||
ns.components.end(),
|
||||
std::back_inserter(combined_ns.components));
|
||||
std::copy(ns.components.begin(), ns.components.end(),
|
||||
std::back_inserter(combined_ns.components));
|
||||
|
||||
const std::string namespace_name = FullNamespace(".", combined_ns);
|
||||
if (!namespace_name.empty()) {
|
||||
@@ -368,9 +366,9 @@ class JavaGenerator : public BaseGenerator {
|
||||
FLATBUFFERS_ASSERT(value.type.enum_def);
|
||||
auto &enum_def = *value.type.enum_def;
|
||||
auto enum_val = enum_def.FindByValue(value.constant);
|
||||
return
|
||||
enum_val ? Prefixed(namer_.NamespacedEnumVariant(enum_def, *enum_val))
|
||||
: value.constant;
|
||||
return enum_val
|
||||
? Prefixed(namer_.NamespacedEnumVariant(enum_def, *enum_val))
|
||||
: value.constant;
|
||||
}
|
||||
|
||||
std::string GenDefaultValue(const FieldDef &field) const {
|
||||
@@ -1653,9 +1651,9 @@ class JavaGenerator : public BaseGenerator {
|
||||
break;
|
||||
case BASE_TYPE_UNION:
|
||||
array_type = "int";
|
||||
element_type =
|
||||
Prefixed(namer_.NamespacedType(*field.value.type.enum_def))
|
||||
+ "Union";
|
||||
element_type = Prefixed(namer_.NamespacedType(
|
||||
*field.value.type.enum_def)) +
|
||||
"Union";
|
||||
to_array = element_type + ".pack(builder, _o." +
|
||||
namer_.Method("get", property_name) + "()[_j])";
|
||||
break;
|
||||
@@ -2007,8 +2005,7 @@ class JavaGenerator : public BaseGenerator {
|
||||
|
||||
case BASE_TYPE_UNION: {
|
||||
if (wrap_in_namespace) {
|
||||
type_name =
|
||||
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
type_name = Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
} else {
|
||||
type_name = namer_.Type(*type.enum_def) + "Union";
|
||||
}
|
||||
@@ -2042,15 +2039,13 @@ class JavaGenerator : public BaseGenerator {
|
||||
type_name.replace(type_name.length() - type_name_length,
|
||||
type_name_length, new_type_name);
|
||||
} else if (type.element == BASE_TYPE_UNION) {
|
||||
type_name =
|
||||
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
type_name = Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case BASE_TYPE_UNION: {
|
||||
type_name =
|
||||
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
type_name = Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
@@ -2192,12 +2187,11 @@ class JavaGenerator : public BaseGenerator {
|
||||
|
||||
std::string package_prefix_;
|
||||
Namespace package_prefix_ns_;
|
||||
|
||||
};
|
||||
} // namespace java
|
||||
|
||||
static bool GenerateJava(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
java::JavaGenerator generator(parser, path, file_name,
|
||||
parser.opts.java_package_prefix);
|
||||
return generator.generate();
|
||||
@@ -2213,9 +2207,8 @@ class JavaCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +30,7 @@ namespace jsons {
|
||||
|
||||
namespace {
|
||||
|
||||
template<class T>
|
||||
static std::string GenFullName(const T *enum_def) {
|
||||
template<class T> static std::string GenFullName(const T *enum_def) {
|
||||
std::string full_name;
|
||||
const auto &name_spaces = enum_def->defined_namespace->components;
|
||||
for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) {
|
||||
@@ -41,8 +40,7 @@ static std::string GenFullName(const T *enum_def) {
|
||||
return full_name;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static std::string GenTypeRef(const T *enum_def) {
|
||||
template<class T> static std::string GenTypeRef(const T *enum_def) {
|
||||
return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\"";
|
||||
}
|
||||
|
||||
@@ -144,7 +142,7 @@ static std::string GenType(const Type &type) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
class JsonSchemaGenerator : public BaseGenerator {
|
||||
private:
|
||||
@@ -320,7 +318,7 @@ class JsonSchemaGenerator : public BaseGenerator {
|
||||
} // namespace jsons
|
||||
|
||||
static bool GenerateJsonSchema(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
jsons::JsonSchemaGenerator generator(parser, path, file_name);
|
||||
if (!generator.generate()) { return false; }
|
||||
return generator.save();
|
||||
@@ -336,9 +334,8 @@ class JsonSchemaCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ static Namer::Config KotlinDefaultConfig() {
|
||||
/*filename_suffix=*/"",
|
||||
/*filename_extension=*/".kt" };
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
class KotlinGenerator : public BaseGenerator {
|
||||
public:
|
||||
@@ -1594,7 +1594,7 @@ class KotlinGenerator : public BaseGenerator {
|
||||
} // namespace kotlin
|
||||
|
||||
static bool GenerateKotlin(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
kotlin::KotlinGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -1609,9 +1609,8 @@ class KotlinCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,8 +63,10 @@ class LobsterGenerator : public BaseGenerator {
|
||||
std::string GenTypeName(const Type &type) {
|
||||
auto bits = NumToString(SizeOf(type.base_type) * 8);
|
||||
if (IsInteger(type.base_type)) {
|
||||
if (IsUnsigned(type.base_type)) return "uint" + bits;
|
||||
else return "int" + bits;
|
||||
if (IsUnsigned(type.base_type))
|
||||
return "uint" + bits;
|
||||
else
|
||||
return "int" + bits;
|
||||
}
|
||||
if (IsFloat(type.base_type)) return "float" + bits;
|
||||
if (IsString(type)) return "string";
|
||||
@@ -120,16 +122,17 @@ class LobsterGenerator : public BaseGenerator {
|
||||
auto defval = field.IsOptional() ? "0" : field.value.constant;
|
||||
acc = "buf_.flatbuffers_field_" + GenTypeName(field.value.type) +
|
||||
"(pos_, " + offsets + ", " + defval + ")";
|
||||
if (IsBool(field.value.type.base_type))
|
||||
acc = "bool(" + acc + ")";
|
||||
if (IsBool(field.value.type.base_type)) acc = "bool(" + acc + ")";
|
||||
}
|
||||
if (field.value.type.enum_def)
|
||||
acc = NormalizedName(*field.value.type.enum_def) + "(" + acc + ")";
|
||||
if (field.IsOptional()) {
|
||||
acc += ", buf_.flatbuffers_field_present(pos_, " + offsets + ")";
|
||||
code += def + "() -> " + LobsterType(field.value.type) + ", bool:\n return " + acc + "\n";
|
||||
code += def + "() -> " + LobsterType(field.value.type) +
|
||||
", bool:\n return " + acc + "\n";
|
||||
} else {
|
||||
code += def + "() -> " + LobsterType(field.value.type) + ":\n return " + acc + "\n";
|
||||
code += def + "() -> " + LobsterType(field.value.type) +
|
||||
":\n return " + acc + "\n";
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -150,7 +153,8 @@ class LobsterGenerator : public BaseGenerator {
|
||||
}
|
||||
case BASE_TYPE_STRING:
|
||||
code += def +
|
||||
"() -> string:\n return buf_.flatbuffers_field_string(pos_, " +
|
||||
"() -> string:\n return "
|
||||
"buf_.flatbuffers_field_string(pos_, " +
|
||||
offsets + ")\n";
|
||||
break;
|
||||
case BASE_TYPE_VECTOR: {
|
||||
@@ -161,7 +165,9 @@ class LobsterGenerator : public BaseGenerator {
|
||||
if (!(vectortype.struct_def->fixed)) {
|
||||
start = "buf_.flatbuffers_indirect(" + start + ")";
|
||||
}
|
||||
code += def + "(i:int) -> " + NamespacedName(*field.value.type.struct_def) + ":\n return ";
|
||||
code += def + "(i:int) -> " +
|
||||
NamespacedName(*field.value.type.struct_def) +
|
||||
":\n return ";
|
||||
code += NamespacedName(*field.value.type.struct_def) + " { buf_, " +
|
||||
start + " }\n";
|
||||
} else {
|
||||
@@ -169,7 +175,8 @@ class LobsterGenerator : public BaseGenerator {
|
||||
code += def + "(i:int) -> string:\n return ";
|
||||
code += "buf_.flatbuffers_string";
|
||||
} else {
|
||||
code += def + "(i:int) -> " + LobsterType(vectortype) + ":\n return ";
|
||||
code += def + "(i:int) -> " + LobsterType(vectortype) +
|
||||
":\n return ";
|
||||
code += "buf_.read_" + GenTypeName(vectortype) + "_le";
|
||||
}
|
||||
code += "(buf_.flatbuffers_field_vector(pos_, " + offsets +
|
||||
@@ -399,7 +406,7 @@ class LobsterGenerator : public BaseGenerator {
|
||||
} // namespace lobster
|
||||
|
||||
static bool GenerateLobster(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
lobster::LobsterGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -414,9 +421,8 @@ class LobsterCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -940,7 +940,7 @@ class PhpGenerator : public BaseGenerator {
|
||||
} // namespace php
|
||||
|
||||
static bool GeneratePhp(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
php::PhpGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -955,9 +955,8 @@ class PhpCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ static Namer::Config PythonDefaultConfig() {
|
||||
static const CommentConfig def_comment = { nullptr, "#", nullptr };
|
||||
static const std::string Indent = " ";
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
class PythonGenerator : public BaseGenerator {
|
||||
public:
|
||||
@@ -149,10 +149,11 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (!parser_.opts.python_no_type_prefix_suffix) {
|
||||
// Add an alias with the old name
|
||||
code += Indent + "@classmethod\n";
|
||||
code += Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n";
|
||||
code +=
|
||||
Indent + Indent +
|
||||
"\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
|
||||
Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n";
|
||||
code += Indent + Indent +
|
||||
"\"\"\"This method is deprecated. Please switch to "
|
||||
"GetRootAs.\"\"\"\n";
|
||||
code += Indent + Indent + "return cls.GetRootAs(buf, offset)\n";
|
||||
}
|
||||
}
|
||||
@@ -179,16 +180,15 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field) + "Length(self)";
|
||||
if (parser_.opts.python_typing) {
|
||||
code += " -> int";
|
||||
}
|
||||
if (parser_.opts.python_typing) { code += " -> int"; }
|
||||
code += ":";
|
||||
if(!IsArray(field.value.type)){
|
||||
code += OffsetPrefix(field,false);
|
||||
if (!IsArray(field.value.type)) {
|
||||
code += OffsetPrefix(field, false);
|
||||
code += GenIndents(3) + "return self._tab.VectorLen(o)";
|
||||
code += GenIndents(2) + "return 0\n\n";
|
||||
}else{
|
||||
code += GenIndents(2) + "return "+NumToString(field.value.type.fixed_length)+"\n\n";
|
||||
} else {
|
||||
code += GenIndents(2) + "return " +
|
||||
NumToString(field.value.type.fixed_length) + "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,17 +199,15 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field) + "IsNone(self)";
|
||||
if (parser_.opts.python_typing) {
|
||||
code += " -> bool";
|
||||
}
|
||||
if (parser_.opts.python_typing) { code += " -> bool"; }
|
||||
code += ":";
|
||||
if(!IsArray(field.value.type)){
|
||||
if (!IsArray(field.value.type)) {
|
||||
code += GenIndents(2) +
|
||||
"o = flatbuffers.number_types.UOffsetTFlags.py_type" +
|
||||
"(self._tab.Offset(" + NumToString(field.value.offset) + "))";
|
||||
code += GenIndents(2) + "return o == 0";
|
||||
} else {
|
||||
//assume that we always have an array as memory is preassigned
|
||||
// assume that we always have an array as memory is preassigned
|
||||
code += GenIndents(2) + "return False";
|
||||
}
|
||||
code += "\n\n";
|
||||
@@ -290,7 +288,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += "(self, i):";
|
||||
}
|
||||
|
||||
if (parser_.opts.include_dependence_headers && !parser_.opts.python_typing) {
|
||||
if (parser_.opts.include_dependence_headers &&
|
||||
!parser_.opts.python_typing) {
|
||||
code += GenIndents(2);
|
||||
code += "from " + import_entry.first + " import " + import_entry.second +
|
||||
"\n";
|
||||
@@ -317,8 +316,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += NumToString(field.value.offset) + " + i * ";
|
||||
code += NumToString(InlineSize(field.value.type.VectorType()));
|
||||
code += ")) for i in range(";
|
||||
code += "self."+namer_.Method(field)+"Length()" + ")]";
|
||||
code += GenIndents(2) +"elif j >= 0 and j < self."+namer_.Method(field)+"Length():";
|
||||
code += "self." + namer_.Method(field) + "Length()" + ")]";
|
||||
code += GenIndents(2) + "elif j >= 0 and j < self." + namer_.Method(field) +
|
||||
"Length():";
|
||||
code += GenIndents(3) + "return " + GenGetter(field.value.type);
|
||||
code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
|
||||
code += NumToString(field.value.offset) + " + j * ";
|
||||
@@ -355,7 +355,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += "x = self._tab.Indirect(o + self._tab.Pos)\n";
|
||||
}
|
||||
|
||||
if (parser_.opts.include_dependence_headers && !parser_.opts.python_typing) {
|
||||
if (parser_.opts.include_dependence_headers &&
|
||||
!parser_.opts.python_typing) {
|
||||
code += Indent + Indent + Indent;
|
||||
code += "from " + import_entry.first + " import " + import_entry.second +
|
||||
"\n";
|
||||
@@ -464,7 +465,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (!(vectortype.struct_def->fixed)) {
|
||||
code += Indent + Indent + Indent + "x = self._tab.Indirect(x)\n";
|
||||
}
|
||||
if (parser_.opts.include_dependence_headers && !parser_.opts.python_typing) {
|
||||
if (parser_.opts.include_dependence_headers &&
|
||||
!parser_.opts.python_typing) {
|
||||
code += Indent + Indent + Indent;
|
||||
code += "from " + import_entry.first + " import " + import_entry.second +
|
||||
"\n";
|
||||
@@ -519,7 +521,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field) + "AsNumpy(self):";
|
||||
if(!IsArray(field.value.type)){
|
||||
if (!IsArray(field.value.type)) {
|
||||
code += OffsetPrefix(field, false);
|
||||
|
||||
code += GenIndents(3);
|
||||
@@ -533,11 +535,13 @@ class PythonGenerator : public BaseGenerator {
|
||||
} else {
|
||||
code += GenIndents(2) + "return 0\n";
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
code += GenIndents(2) + "return ";
|
||||
code += "self._tab.GetArrayAsNumpy(flatbuffers.number_types.";
|
||||
code += namer_.Method(GenTypeGet(field.value.type.VectorType()));
|
||||
code += "Flags, self._tab.Pos + "+NumToString(field.value.offset)+", "+NumToString("self."+namer_.Method(field)+"Length()")+")\n";
|
||||
code += "Flags, self._tab.Pos + " + NumToString(field.value.offset) +
|
||||
", " + NumToString("self." + namer_.Method(field) + "Length()") +
|
||||
")\n";
|
||||
}
|
||||
code += "\n";
|
||||
}
|
||||
@@ -564,9 +568,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
const std::string unqualified_name = nested->constant;
|
||||
std::string qualified_name = NestedFlatbufferType(unqualified_name);
|
||||
if (qualified_name.empty()) {
|
||||
qualified_name = nested->constant;
|
||||
}
|
||||
if (qualified_name.empty()) { qualified_name = nested->constant; }
|
||||
|
||||
const ImportMapEntry import_entry = { "." + qualified_name,
|
||||
unqualified_name };
|
||||
@@ -704,7 +706,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
// Generate method with struct name.
|
||||
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix ? "Start" : struct_type + "Start";
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix
|
||||
? "Start"
|
||||
: struct_type + "Start";
|
||||
|
||||
code += "def " + name;
|
||||
if (parser_.opts.python_typing) {
|
||||
@@ -736,12 +740,14 @@ class PythonGenerator : public BaseGenerator {
|
||||
const std::string field_method = namer_.Method(field);
|
||||
const std::string field_ty = GenFieldTy(field);
|
||||
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix ? "Add" + field_method : namer_.Type(struct_def) + "Add" + field_method;
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix
|
||||
? "Add" + field_method
|
||||
: namer_.Type(struct_def) + "Add" + field_method;
|
||||
|
||||
// Generate method with struct name.
|
||||
code += "def " + name;
|
||||
if (parser_.opts.python_typing) {
|
||||
code += "(builder: flatbuffers.Builder, " + field_var + ": " + field_ty;
|
||||
code += "(builder: flatbuffers.Builder, " + field_var + ": " + field_ty;
|
||||
} else {
|
||||
code += "(builder, " + field_var;
|
||||
}
|
||||
@@ -767,9 +773,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
|
||||
// Generate method without struct name.
|
||||
code += "def Add" + field_method + "(builder: flatbuffers.Builder, " + field_var + ": " + field_ty + "):\n";
|
||||
code +=
|
||||
Indent + namer_.Type(struct_def) + "Add" + field_method;
|
||||
code += "def Add" + field_method + "(builder: flatbuffers.Builder, " +
|
||||
field_var + ": " + field_ty + "):\n";
|
||||
code += Indent + namer_.Type(struct_def) + "Add" + field_method;
|
||||
code += "(builder, ";
|
||||
code += field_var;
|
||||
code += ")\n\n";
|
||||
@@ -784,7 +790,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
const std::string field_method = namer_.Method(field);
|
||||
|
||||
// Generate method with struct name.
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix ? "Start" + field_method : struct_type + "Start" + field_method;
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix
|
||||
? "Start" + field_method
|
||||
: struct_type + "Start" + field_method;
|
||||
code += "def " + name;
|
||||
if (parser_.opts.python_typing) {
|
||||
code += "Vector(builder, numElems: int) -> int:\n";
|
||||
@@ -802,7 +810,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
|
||||
// Generate method without struct name.
|
||||
code += "def Start" + field_method + "Vector(builder, numElems: int) -> int:\n";
|
||||
code += "def Start" + field_method +
|
||||
"Vector(builder, numElems: int) -> int:\n";
|
||||
code += Indent + "return " + struct_type + "Start";
|
||||
code += field_method + "Vector(builder, numElems)\n\n";
|
||||
}
|
||||
@@ -849,7 +858,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix ? "End" : namer_.Type(struct_def) + "End";
|
||||
const auto name = parser_.opts.python_no_type_prefix_suffix
|
||||
? "End"
|
||||
: namer_.Type(struct_def) + "End";
|
||||
// Generate method with struct name.
|
||||
if (parser_.opts.python_typing) {
|
||||
code += "def " + name + "(builder: flatbuffers.Builder) -> int:\n";
|
||||
@@ -921,7 +932,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr, imports); break;
|
||||
case BASE_TYPE_UNION:
|
||||
GetUnionField(struct_def, field, code_ptr, imports);
|
||||
break;
|
||||
default: FLATBUFFERS_ASSERT(0);
|
||||
}
|
||||
}
|
||||
@@ -1255,14 +1268,15 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
void InitializeFromPackedBuf(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
code += GenIndents(1) + "@classmethod";
|
||||
code += GenIndents(1) + "def InitFromPackedBuf(cls, buf, pos=0):";
|
||||
code += GenIndents(2) + "n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, pos)";
|
||||
code += GenIndents(2) +
|
||||
"n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, pos)";
|
||||
code += GenIndents(2) + "return cls.InitFromBuf(buf, pos+n)";
|
||||
code += "\n";
|
||||
}
|
||||
@@ -1281,20 +1295,21 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
void GenCompareOperator(const StructDef &struct_def,
|
||||
void GenCompareOperator(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += GenIndents(1) + "def __eq__(self, other):";
|
||||
code += GenIndents(2) + "return type(self) == type(other)";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
// Wrties the comparison statement for this field.
|
||||
const auto field_field = namer_.Field(field);
|
||||
code += " and \\" + GenIndents(3) + "self." + field_field + " == " + "other." + field_field;
|
||||
}
|
||||
// Wrties the comparison statement for this field.
|
||||
const auto field_field = namer_.Field(field);
|
||||
code += " and \\" + GenIndents(3) + "self." + field_field +
|
||||
" == " + "other." + field_field;
|
||||
}
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
@@ -1317,8 +1332,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += field_type + "()";
|
||||
}
|
||||
code += ") is not None:";
|
||||
code += GenIndents(3) + "self." + field_field + " = " + namer_.ObjectType(field_type) +
|
||||
+ ".InitFromObj(" + struct_var + "." + field_method + "(";
|
||||
code += GenIndents(3) + "self." + field_field + " = " +
|
||||
namer_.ObjectType(field_type) + +".InitFromObj(" + struct_var +
|
||||
"." + field_method + "(";
|
||||
// A struct's accessor requires a struct buf instance.
|
||||
if (struct_def.fixed && field.value.type.base_type == BASE_TYPE_STRUCT) {
|
||||
code += field_type + "()";
|
||||
@@ -1368,8 +1384,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
"(i) is None:";
|
||||
code += GenIndents(5) + "self." + field_field + ".append(None)";
|
||||
code += GenIndents(4) + "else:";
|
||||
code += GenIndents(5) + one_instance + " = " + namer_.ObjectType(field_type) +
|
||||
".InitFromObj(" + struct_var + "." + field_method + "(i))";
|
||||
code += GenIndents(5) + one_instance + " = " +
|
||||
namer_.ObjectType(field_type) + ".InitFromObj(" + struct_var + "." +
|
||||
field_method + "(i))";
|
||||
code +=
|
||||
GenIndents(5) + "self." + field_field + ".append(" + one_instance + ")";
|
||||
}
|
||||
@@ -1399,8 +1416,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
"(i) is None:";
|
||||
code += GenIndents(5) + "self." + field_field + ".append(None)";
|
||||
code += GenIndents(4) + "else:";
|
||||
code += GenIndents(5) + one_instance + " = " + namer_.ObjectType(field_type) +
|
||||
".InitFromObj(" + struct_var + "." + field_method + "(i))";
|
||||
code += GenIndents(5) + one_instance + " = " +
|
||||
namer_.ObjectType(field_type) + ".InitFromObj(" + struct_var + "." +
|
||||
field_method + "(i))";
|
||||
code +=
|
||||
GenIndents(5) + "self." + field_field + ".append(" + one_instance + ")";
|
||||
}
|
||||
@@ -1780,9 +1798,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
InitializeFromObjForObject(struct_def, &code);
|
||||
|
||||
if (parser_.opts.gen_compare) {
|
||||
GenCompareOperator(struct_def, &code);
|
||||
}
|
||||
if (parser_.opts.gen_compare) { GenCompareOperator(struct_def, &code); }
|
||||
|
||||
GenUnPack(struct_def, &code);
|
||||
|
||||
@@ -1891,17 +1907,11 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string GenFieldTy(const FieldDef &field) const {
|
||||
if (IsScalar(field.value.type.base_type) || IsArray(field.value.type)) {
|
||||
const std::string ty = GenTypeBasic(field.value.type);
|
||||
if (ty.find("int") != std::string::npos) {
|
||||
return "int";
|
||||
}
|
||||
if (ty.find("int") != std::string::npos) { return "int"; }
|
||||
|
||||
if (ty.find("float") != std::string::npos) {
|
||||
return "float";
|
||||
}
|
||||
if (ty.find("float") != std::string::npos) { return "float"; }
|
||||
|
||||
if (ty == "bool") {
|
||||
return "bool";
|
||||
}
|
||||
if (ty == "bool") { return "bool"; }
|
||||
|
||||
return "Any";
|
||||
} else {
|
||||
@@ -2052,13 +2062,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
if (parser_.opts.one_file) {
|
||||
if (!declcode.empty()) {
|
||||
*one_file_code += declcode + "\n\n";
|
||||
}
|
||||
if (!declcode.empty()) { *one_file_code += declcode + "\n\n"; }
|
||||
|
||||
for (auto import_str: imports) {
|
||||
one_file_imports.insert(import_str);
|
||||
}
|
||||
for (auto import_str : imports) { one_file_imports.insert(import_str); }
|
||||
} else {
|
||||
const std::string mod =
|
||||
namer_.File(struct_def, SkipFile::SuffixAndExtension);
|
||||
@@ -2136,7 +2142,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
} // namespace python
|
||||
|
||||
static bool GeneratePython(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
python::PythonGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -2151,9 +2157,8 @@ class PythonCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -277,10 +277,10 @@ static bool IsBitFlagsEnum(const EnumDef &enum_def) {
|
||||
static bool IsOptionalToBuilder(const FieldDef &field) {
|
||||
return field.IsOptional() || !IsScalar(field.value.type.base_type);
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
static bool GenerateRustModuleRootFile(const Parser &parser,
|
||||
const std::string &output_dir) {
|
||||
const std::string &output_dir) {
|
||||
if (!parser.opts.rust_module_root_file) {
|
||||
// Don't generate a root file when generating one file. This isn't an error
|
||||
// so return true.
|
||||
@@ -708,7 +708,7 @@ class RustGenerator : public BaseGenerator {
|
||||
// and an enum array of values
|
||||
void GenEnum(const EnumDef &enum_def) {
|
||||
const bool is_private = parser_.opts.no_leak_private_annotations &&
|
||||
(enum_def.attributes.Lookup("private") != nullptr);
|
||||
(enum_def.attributes.Lookup("private") != nullptr);
|
||||
code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub");
|
||||
code_.SetValue("ENUM_TY", namer_.Type(enum_def));
|
||||
code_.SetValue("BASE_TYPE", GetEnumTypeForDecl(enum_def.underlying_type));
|
||||
@@ -842,15 +842,21 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " type Inner = Self;";
|
||||
code_ += " #[inline]";
|
||||
code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {";
|
||||
code_ += " let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);";
|
||||
code_ +=
|
||||
" let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);";
|
||||
if (IsBitFlagsEnum(enum_def)) {
|
||||
// Safety:
|
||||
// This is safe because we know bitflags is implemented with a repr transparent uint of the correct size.
|
||||
// from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0
|
||||
// This is safe because we know bitflags is implemented with a repr
|
||||
// transparent uint of the correct size. from_bits_unchecked will be
|
||||
// replaced by an equivalent but safe from_bits_retain in bitflags 2.0
|
||||
// https://github.com/bitflags/bitflags/issues/262
|
||||
code_ += " // Safety:";
|
||||
code_ += " // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size.";
|
||||
code_ += " // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0";
|
||||
code_ +=
|
||||
" // This is safe because we know bitflags is implemented with a "
|
||||
"repr transparent uint of the correct size.";
|
||||
code_ +=
|
||||
" // from_bits_unchecked will be replaced by an equivalent but "
|
||||
"safe from_bits_retain in bitflags 2.0";
|
||||
code_ += " // https://github.com/bitflags/bitflags/issues/262";
|
||||
code_ += " Self::from_bits_unchecked(b)";
|
||||
} else {
|
||||
@@ -863,7 +869,9 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " type Output = {{ENUM_TY}};";
|
||||
code_ += " #[inline]";
|
||||
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
|
||||
code_ += " flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, {{INTO_BASE}});";
|
||||
code_ +=
|
||||
" flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, "
|
||||
"{{INTO_BASE}});";
|
||||
code_ += " }";
|
||||
code_ += "}";
|
||||
code_ += "";
|
||||
@@ -879,12 +887,17 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " let b = {{BASE_TYPE}}::from_le(v);";
|
||||
if (IsBitFlagsEnum(enum_def)) {
|
||||
// Safety:
|
||||
// This is safe because we know bitflags is implemented with a repr transparent uint of the correct size.
|
||||
// from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0
|
||||
// This is safe because we know bitflags is implemented with a repr
|
||||
// transparent uint of the correct size. from_bits_unchecked will be
|
||||
// replaced by an equivalent but safe from_bits_retain in bitflags 2.0
|
||||
// https://github.com/bitflags/bitflags/issues/262
|
||||
code_ += " // Safety:";
|
||||
code_ += " // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size.";
|
||||
code_ += " // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0";
|
||||
code_ +=
|
||||
" // This is safe because we know bitflags is implemented with a "
|
||||
"repr transparent uint of the correct size.";
|
||||
code_ +=
|
||||
" // from_bits_unchecked will be replaced by an equivalent but "
|
||||
"safe from_bits_retain in bitflags 2.0";
|
||||
code_ += " // https://github.com/bitflags/bitflags/issues/262";
|
||||
code_ += " unsafe { Self::from_bits_unchecked(b) }";
|
||||
} else {
|
||||
@@ -1458,7 +1471,8 @@ class RustGenerator : public BaseGenerator {
|
||||
case ftVectorOfBool:
|
||||
case ftVectorOfFloat: {
|
||||
const auto typname = GetTypeBasic(type.VectorType());
|
||||
return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + ">");
|
||||
return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname +
|
||||
">");
|
||||
}
|
||||
case ftVectorOfEnumKey: {
|
||||
const auto typname = WrapInNameSpace(*type.enum_def);
|
||||
@@ -1467,7 +1481,8 @@ class RustGenerator : public BaseGenerator {
|
||||
}
|
||||
case ftVectorOfStruct: {
|
||||
const auto typname = WrapInNameSpace(*type.struct_def);
|
||||
return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + ">");
|
||||
return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname +
|
||||
">");
|
||||
}
|
||||
case ftVectorOfTable: {
|
||||
const auto typname = WrapInNameSpace(*type.struct_def);
|
||||
@@ -1585,8 +1600,9 @@ class RustGenerator : public BaseGenerator {
|
||||
: "None";
|
||||
const std::string unwrap = field.IsOptional() ? "" : ".unwrap()";
|
||||
|
||||
return "unsafe { self._tab.get::<" + typname + ">({{STRUCT_TY}}::" + vt_offset +
|
||||
", " + default_value + ")" + unwrap + "}";
|
||||
return "unsafe { self._tab.get::<" + typname +
|
||||
">({{STRUCT_TY}}::" + vt_offset + ", " + default_value + ")" +
|
||||
unwrap + "}";
|
||||
}
|
||||
|
||||
// Generates a fully-qualified name getter for use with --gen-name-strings
|
||||
@@ -1646,8 +1662,8 @@ class RustGenerator : public BaseGenerator {
|
||||
// Generate an accessor struct, builder struct, and create function for a
|
||||
// table.
|
||||
void GenTable(const StructDef &struct_def) {
|
||||
|
||||
const bool is_private = parser_.opts.no_leak_private_annotations &&
|
||||
const bool is_private =
|
||||
parser_.opts.no_leak_private_annotations &&
|
||||
(struct_def.attributes.Lookup("private") != nullptr);
|
||||
code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub");
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def));
|
||||
@@ -1933,13 +1949,17 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " // Safety:";
|
||||
code_ += " // Created from a valid Table for this object";
|
||||
code_ += " // Which contains a valid union in this slot";
|
||||
code_ += " Some(unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(u) })";
|
||||
code_ +=
|
||||
" Some(unsafe { "
|
||||
"{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u) })";
|
||||
} else {
|
||||
code_ +=" self.{{FIELD}}().map(|t| {";
|
||||
code_ += " self.{{FIELD}}().map(|t| {";
|
||||
code_ += " // Safety:";
|
||||
code_ += " // Created from a valid Table for this object";
|
||||
code_ += " // Which contains a valid union in this slot";
|
||||
code_ += " unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(t) }";
|
||||
code_ +=
|
||||
" unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(t) "
|
||||
"}";
|
||||
code_ += " })";
|
||||
}
|
||||
code_ += " } else {";
|
||||
@@ -2264,7 +2284,8 @@ class RustGenerator : public BaseGenerator {
|
||||
case ftUnionValue: {
|
||||
code_.SetValue("ENUM_METHOD",
|
||||
namer_.Method(*field.value.type.enum_def));
|
||||
code_.SetValue("DISCRIMINANT", namer_.LegacyRustUnionTypeMethod(field));
|
||||
code_.SetValue("DISCRIMINANT",
|
||||
namer_.LegacyRustUnionTypeMethod(field));
|
||||
code_ +=
|
||||
" let {{DISCRIMINANT}} = "
|
||||
"self.{{FIELD}}.{{ENUM_METHOD}}_type();";
|
||||
@@ -2312,10 +2333,10 @@ class RustGenerator : public BaseGenerator {
|
||||
// TODO(cneo): create_vector* should be more generic to avoid
|
||||
// allocations.
|
||||
|
||||
MapNativeTableField(
|
||||
field,
|
||||
"let w: Vec<_> = x.iter().map(|s| _fbb.create_string(s)).collect();"
|
||||
"_fbb.create_vector(&w)");
|
||||
MapNativeTableField(field,
|
||||
"let w: Vec<_> = x.iter().map(|s| "
|
||||
"_fbb.create_string(s)).collect();"
|
||||
"_fbb.create_vector(&w)");
|
||||
return;
|
||||
}
|
||||
case ftVectorOfTable: {
|
||||
@@ -2601,7 +2622,8 @@ class RustGenerator : public BaseGenerator {
|
||||
}
|
||||
// Generate an accessor struct with constructor for a flatbuffers struct.
|
||||
void GenStruct(const StructDef &struct_def) {
|
||||
const bool is_private = parser_.opts.no_leak_private_annotations &&
|
||||
const bool is_private =
|
||||
parser_.opts.no_leak_private_annotations &&
|
||||
(struct_def.attributes.Lookup("private") != nullptr);
|
||||
code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub");
|
||||
// Generates manual padding and alignment.
|
||||
@@ -2665,7 +2687,9 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " type Output = {{STRUCT_TY}};";
|
||||
code_ += " #[inline]";
|
||||
code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {";
|
||||
code_ += " let src = ::core::slice::from_raw_parts(self as *const {{STRUCT_TY}} as *const u8, Self::size());";
|
||||
code_ +=
|
||||
" let src = ::core::slice::from_raw_parts(self as *const "
|
||||
"{{STRUCT_TY}} as *const u8, Self::size());";
|
||||
code_ += " dst.copy_from_slice(src);";
|
||||
code_ += " }";
|
||||
code_ += "}";
|
||||
@@ -2759,7 +2783,9 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " // Safety:";
|
||||
code_ += " // Created from a valid Table for this object";
|
||||
code_ += " // Which contains a valid array in this slot";
|
||||
code_ += " unsafe { flatbuffers::Array::follow(&self.0, {{FIELD_OFFSET}}) }";
|
||||
code_ +=
|
||||
" unsafe { flatbuffers::Array::follow(&self.0, {{FIELD_OFFSET}}) "
|
||||
"}";
|
||||
} else {
|
||||
code_ += "pub fn {{FIELD}}(&self) -> {{FIELD_TYPE}} {";
|
||||
code_ +=
|
||||
@@ -2772,7 +2798,9 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " core::ptr::copy_nonoverlapping(";
|
||||
code_ += " self.0[{{FIELD_OFFSET}}..].as_ptr(),";
|
||||
code_ += " mem.as_mut_ptr() as *mut u8,";
|
||||
code_ += " core::mem::size_of::<<{{FIELD_TYPE}} as EndianScalar>::Scalar>(),";
|
||||
code_ +=
|
||||
" core::mem::size_of::<<{{FIELD_TYPE}} as "
|
||||
"EndianScalar>::Scalar>(),";
|
||||
code_ += " );";
|
||||
code_ += " mem.assume_init()";
|
||||
code_ += " })";
|
||||
@@ -2827,7 +2855,9 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += " core::ptr::copy_nonoverlapping(";
|
||||
code_ += " &x_le as *const _ as *const u8,";
|
||||
code_ += " self.0[{{FIELD_OFFSET}}..].as_mut_ptr(),";
|
||||
code_ += " core::mem::size_of::<<{{FIELD_TYPE}} as EndianScalar>::Scalar>(),";
|
||||
code_ +=
|
||||
" core::mem::size_of::<<{{FIELD_TYPE}} as "
|
||||
"EndianScalar>::Scalar>(),";
|
||||
code_ += " );";
|
||||
code_ += " }";
|
||||
}
|
||||
@@ -2990,13 +3020,13 @@ class RustGenerator : public BaseGenerator {
|
||||
} // namespace rust
|
||||
|
||||
static bool GenerateRust(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
rust::RustGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
|
||||
static std::string RustMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
rust::RustGenerator generator(parser, path, file_name);
|
||||
@@ -3020,9 +3050,8 @@ class RustCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ static std::string GenArrayMainBody(const std::string &optional) {
|
||||
optional + " { ";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
class SwiftGenerator : public BaseGenerator {
|
||||
private:
|
||||
@@ -261,8 +261,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "private var _{{FIELDVAR}}: " + valueType;
|
||||
const auto accessing_value = IsEnum(field.value.type) ? ".value" : "";
|
||||
const auto base_value =
|
||||
IsStruct(field.value.type) ? (type + "()")
|
||||
: SwiftConstant(field);
|
||||
IsStruct(field.value.type) ? (type + "()") : SwiftConstant(field);
|
||||
|
||||
main_constructor.push_back("_" + field_var + " = " + field_var +
|
||||
accessing_value);
|
||||
@@ -720,8 +719,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
if (IsBool(field.value.type.base_type)) {
|
||||
std::string default_value =
|
||||
field.IsOptional() ? "nil"
|
||||
: SwiftConstant(field);
|
||||
field.IsOptional() ? "nil" : SwiftConstant(field);
|
||||
code_.SetValue("CONSTANT", default_value);
|
||||
code_.SetValue("VALUETYPE", "Bool");
|
||||
code_ += GenReaderMainBody(optional) + "\\";
|
||||
@@ -984,8 +982,9 @@ class SwiftGenerator : public BaseGenerator {
|
||||
} else if (IsEnum(type) && !field.IsOptional()) {
|
||||
code_.SetValue("CONSTANT", GenEnumDefaultValue(field));
|
||||
code_ += "if {{FIELDVAR}} != {{CONSTANT}} {";
|
||||
} else if (IsFloat(type.base_type) && StringIsFlatbufferNan(field.value.constant)) {
|
||||
code_ += "if !{{FIELDVAR}}.isNaN {";
|
||||
} else if (IsFloat(type.base_type) &&
|
||||
StringIsFlatbufferNan(field.value.constant)) {
|
||||
code_ += "if !{{FIELDVAR}}.isNaN {";
|
||||
} else if (IsScalar(type.base_type) && !IsEnum(type) &&
|
||||
!IsBool(type.base_type) && !field.IsOptional()) {
|
||||
code_ += "if {{FIELDVAR}} != {{CONSTANT}} {";
|
||||
@@ -1158,8 +1157,9 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
void GenEnum(const EnumDef &enum_def) {
|
||||
if (enum_def.generated) return;
|
||||
const bool is_private_access = parser_.opts.swift_implementation_only ||
|
||||
enum_def.attributes.Lookup("private") != nullptr;
|
||||
const bool is_private_access =
|
||||
parser_.opts.swift_implementation_only ||
|
||||
enum_def.attributes.Lookup("private") != nullptr;
|
||||
code_.SetValue("ENUM_TYPE",
|
||||
enum_def.is_union ? "UnionEnum" : "Enum, Verifiable");
|
||||
code_.SetValue("ACCESS_TYPE", is_private_access ? "internal" : "public");
|
||||
@@ -1579,7 +1579,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
if (IsBool(field.value.type.base_type)) {
|
||||
code_ += "{{ACCESS_TYPE}} var {{FIELDVAR}}: Bool" + nullable;
|
||||
if (!field.IsOptional())
|
||||
base_constructor.push_back(field_var + " = " + SwiftConstant(field));
|
||||
base_constructor.push_back(field_var + " = " +
|
||||
SwiftConstant(field));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1827,15 +1828,17 @@ class SwiftGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string SwiftConstant(const FieldDef& field) {
|
||||
std::string SwiftConstant(const FieldDef &field) {
|
||||
const auto default_value =
|
||||
StringIsFlatbufferNan(field.value.constant) ? ".nan" :
|
||||
StringIsFlatbufferPositiveInfinity(field.value.constant) ? ".infinity" :
|
||||
StringIsFlatbufferNegativeInfinity(field.value.constant) ? "-.infinity" :
|
||||
IsBool(field.value.type.base_type) ? ("0" == field.value.constant ? "false" : "true") :
|
||||
field.value.constant;
|
||||
StringIsFlatbufferNan(field.value.constant) ? ".nan"
|
||||
: StringIsFlatbufferPositiveInfinity(field.value.constant) ? ".infinity"
|
||||
: StringIsFlatbufferNegativeInfinity(field.value.constant)
|
||||
? "-.infinity"
|
||||
: IsBool(field.value.type.base_type)
|
||||
? ("0" == field.value.constant ? "false" : "true")
|
||||
: field.value.constant;
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenEnumConstructor(const std::string &at) {
|
||||
return "{{VALUETYPE}}(rawValue: " + GenReader("BASEVALUE", at) + ") ";
|
||||
@@ -1901,7 +1904,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
} // namespace swift
|
||||
|
||||
static bool GenerateSwift(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
swift::SwiftGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
@@ -1916,9 +1919,8 @@ class SwiftCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -454,9 +454,8 @@ class TextCodeGenerator : public CodeGenerator {
|
||||
|
||||
// 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;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -477,10 +477,9 @@ class TsGenerator : public BaseGenerator {
|
||||
EnumVal *val = value.type.enum_def->FindByValue(value.constant);
|
||||
if (val == nullptr)
|
||||
val = const_cast<EnumVal *>(value.type.enum_def->MinValue());
|
||||
return AddImport(imports, *value.type.enum_def,
|
||||
*value.type.enum_def)
|
||||
.name +
|
||||
"." + namer_.Variant(*val);
|
||||
return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
||||
.name +
|
||||
"." + namer_.Variant(*val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2162,13 +2161,13 @@ class TsGenerator : public BaseGenerator {
|
||||
} // namespace ts
|
||||
|
||||
static bool GenerateTS(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
ts::TsGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
|
||||
static std::string TSMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string &file_name) {
|
||||
std::string filebase =
|
||||
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
|
||||
ts::TsGenerator generator(parser, path, file_name);
|
||||
@@ -2192,9 +2191,8 @@ class TsCodeGenerator : public CodeGenerator {
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
Status GenerateCode(const uint8_t *, int64_t,
|
||||
const CodeGenOptions &) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user