From dd6daa709b4a105ce123726d98378cfcc27dab9a Mon Sep 17 00:00:00 2001 From: Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> Date: Tue, 9 Apr 2019 00:05:50 +0700 Subject: [PATCH] Part of #5265, neutral changes (#5281) --- docs/source/Schemas.md | 4 +-- include/flatbuffers/idl.h | 20 ++++++++++----- src/idl_gen_cpp.cpp | 62 +++++++++++++++++++++------------------------ src/idl_gen_dart.cpp | 13 +++++----- src/idl_gen_fbs.cpp | 3 +-- src/idl_gen_general.cpp | 7 +++-- src/idl_gen_go.cpp | 8 +++--- src/idl_gen_js_ts.cpp | 7 +++-- src/idl_gen_json_schema.cpp | 10 ++++---- src/idl_gen_lobster.cpp | 11 ++++---- src/idl_gen_lua.cpp | 13 +++++----- src/idl_gen_php.cpp | 12 ++++----- src/idl_gen_python.cpp | 9 ++++--- src/idl_gen_rust.cpp | 18 ++++++------- src/idl_parser.cpp | 42 +++++++++++++++--------------- 15 files changed, 117 insertions(+), 122 deletions(-) diff --git a/docs/source/Schemas.md b/docs/source/Schemas.md index 66c800d..4f7bd72 100644 --- a/docs/source/Schemas.md +++ b/docs/source/Schemas.md @@ -324,8 +324,8 @@ Current understood attributes: Note: currently not guaranteed to have an effect when used with `--object-api`, since that may allocate objects at alignments less than what you specify with `force_align`. -- `bit_flags` (on an enum): the values of this field indicate bits, - meaning that any value N specified in the schema will end up +- `bit_flags` (on an unsigned enum): the values of this field indicate bits, + meaning that any unsigned value N specified in the schema will end up representing 1< Serialize(FlatBufferBuilder *builder, const Parser &parser) const; bool Deserialize(const Parser &parser, const reflection::EnumVal *val); + bool IsZero() const { return 0 == value; } + bool IsNonZero() const { return !IsZero(); } std::string name; std::vector doc_comment; @@ -345,9 +347,9 @@ struct EnumDef : public Definition { EnumDef() : is_union(false), uses_multiple_type_instances(false) {} EnumVal *ReverseLookup(int64_t enum_idx, bool skip_union_default = true) { - for (auto it = vals.vec.begin() + + for (auto it = Vals().begin() + static_cast(is_union && skip_union_default); - it != vals.vec.end(); ++it) { + it != Vals().end(); ++it) { if ((*it)->value == enum_idx) { return *it; } } return nullptr; @@ -357,6 +359,12 @@ struct EnumDef : public Definition { bool Deserialize(Parser &parser, const reflection::Enum *values); + size_t size() const { return vals.vec.size(); } + + const std::vector &Vals() const { + return vals.vec; + } + SymbolTable vals; bool is_union; // Type is a union which uses type aliases where at least one type is @@ -691,17 +699,15 @@ class Parser : public ParserState { bool ParseFlexBuffer(const char *source, const char *source_filename, flexbuffers::Builder *builder); - FLATBUFFERS_CHECKED_ERROR InvalidNumber(const char *number, - const std::string &msg); - StructDef *LookupStruct(const std::string &id) const; std::string UnqualifiedName(std::string fullQualifiedName); + FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg); + private: void Message(const std::string &msg); void Warning(const std::string &msg); - FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg); FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val); FLATBUFFERS_CHECKED_ERROR Next(); FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark(); @@ -745,7 +751,7 @@ class Parser : public ParserState { FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef* field); FLATBUFFERS_CHECKED_ERROR TokenError(); FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e, bool check_now); - FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(Type &type, int64_t *result); + FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type &type, std::string *result); StructDef *LookupCreateStruct(const std::string &name, bool create_if_new = true, bool definition = false); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 50cb82f..17da069 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -785,7 +785,7 @@ class CppGenerator : public BaseGenerator { struct_def ? (struct_def->fixed ? "ST_STRUCT" : "ST_TABLE") : (enum_def->is_union ? "ST_UNION" : "ST_ENUM")); auto num_fields = - struct_def ? struct_def->fields.vec.size() : enum_def->vals.vec.size(); + struct_def ? struct_def->fields.vec.size() : enum_def->size(); code_.SetValue("NUM_FIELDS", NumToString(num_fields)); std::vector names; std::vector types; @@ -798,13 +798,13 @@ class CppGenerator : public BaseGenerator { types.push_back(field.value.type); } } else { - for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end(); + for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end(); ++it) { const auto &ev = **it; names.push_back(Name(ev)); types.push_back(enum_def->is_union ? ev.union_type : Type(enum_def->underlying_type)); - if (static_cast(it - enum_def->vals.vec.begin()) != ev.value) { + if (static_cast(it - enum_def->Vals().begin()) != ev.value) { consecutive_enum_from_zero = false; } } @@ -852,7 +852,7 @@ class CppGenerator : public BaseGenerator { } std::string vs; if (enum_def && !consecutive_enum_from_zero) { - for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end(); + for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end(); ++it) { const auto &ev = **it; if (!vs.empty()) vs += ", "; @@ -919,8 +919,7 @@ class CppGenerator : public BaseGenerator { int64_t anyv = 0; const EnumVal *minv = nullptr, *maxv = nullptr; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; GenComment(ev.doc_comment, " "); @@ -966,15 +965,14 @@ class CppGenerator : public BaseGenerator { code_ += ""; // Generate an array of all enumeration values - auto num_fields = NumToString(enum_def.vals.vec.size()); + auto num_fields = NumToString(enum_def.size()); code_ += "inline const {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" + num_fields + "] {"; code_ += " static const {{ENUM_NAME}} values[] = {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; auto value = GetEnumValUse(enum_def, ev); - auto suffix = *it != enum_def.vals.vec.back() ? "," : ""; + auto suffix = *it != enum_def.Vals().back() ? "," : ""; code_ += " " + value + suffix; } code_ += " };"; @@ -996,8 +994,8 @@ class CppGenerator : public BaseGenerator { code_ += "inline const char * const *EnumNames{{ENUM_NAME}}() {"; code_ += " static const char * const names[] = {"; - auto val = enum_def.vals.vec.front()->value; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + auto val = enum_def.Vals().front()->value; + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; while (val++ != ev.value) { code_ += " \"\","; } @@ -1032,7 +1030,7 @@ class CppGenerator : public BaseGenerator { code_ += " switch (e) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; code_ += " case " + GetEnumValUse(enum_def, ev) + ": return \"" + @@ -1048,11 +1046,11 @@ class CppGenerator : public BaseGenerator { // Generate type traits for unions to map from a type to union enum value. if (enum_def.is_union && !enum_def.uses_multiple_type_instances) { - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; - if (it == enum_def.vals.vec.begin()) { + if (it == enum_def.Vals().begin()) { code_ += "template struct {{ENUM_NAME}}Traits {"; } else { auto name = GetUnionElement(ev, true, true); @@ -1114,10 +1112,10 @@ class CppGenerator : public BaseGenerator { code_ += " " + UnionPackSignature(enum_def, true) + ";"; code_ += ""; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; - if (!ev.value) { continue; } + if (ev.IsZero()) { continue; } const auto native_type = NativeName(GetUnionElement(ev, true, true, true), @@ -1148,11 +1146,11 @@ class CppGenerator : public BaseGenerator { code_ += " if (lhs.type != rhs.type) return false;"; code_ += " switch (lhs.type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev)); - if (ev.value) { + if (ev.IsNonZero()) { const auto native_type = NativeName(GetUnionElement(ev, true, true, true), ev.union_type.struct_def, parser_.opts); @@ -1204,12 +1202,11 @@ class CppGenerator : public BaseGenerator { code_ += "inline " + UnionVerifySignature(enum_def) + " {"; code_ += " switch (type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); - if (ev.value) { + if (ev.IsNonZero()) { code_.SetValue("TYPE", GetUnionElement(ev, true, true)); code_ += " case {{LABEL}}: {"; auto getptr = @@ -1257,10 +1254,10 @@ class CppGenerator : public BaseGenerator { // Generate union Unpack() and Pack() functions. code_ += "inline " + UnionUnPackSignature(enum_def, false) + " {"; code_ += " switch (type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; - if (!ev.value) { continue; } + if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); code_.SetValue("TYPE", GetUnionElement(ev, true, true)); @@ -1287,10 +1284,10 @@ class CppGenerator : public BaseGenerator { code_ += "inline " + UnionPackSignature(enum_def, false) + " {"; code_ += " switch (type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; - if (!ev.value) { continue; } + if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); code_.SetValue("TYPE", @@ -1324,10 +1321,10 @@ class CppGenerator : public BaseGenerator { "{{ENUM_NAME}}Union &u) FLATBUFFERS_NOEXCEPT : type(u.type), " "value(nullptr) {"; code_ += " switch (type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; - if (!ev.value) { continue; } + if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true), @@ -1370,10 +1367,10 @@ class CppGenerator : public BaseGenerator { code_ += "inline void {{ENUM_NAME}}Union::Reset() {"; code_ += " switch (type) {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; - if (!ev.value) { continue; } + if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true), @@ -1834,8 +1831,7 @@ class CppGenerator : public BaseGenerator { " template " "const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;"; - for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); - ++u_it) { + for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) { auto &ev = **u_it; if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; } auto full_struct_name = GetUnionElement(ev, true, true); @@ -1967,7 +1963,7 @@ class CppGenerator : public BaseGenerator { code_.SetValue("FIELD_NAME", Name(field)); - for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); ++u_it) { + for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) { auto &ev = **u_it; if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; } diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp index fb7e0bd..2141eb3 100644 --- a/src/idl_gen_dart.cpp +++ b/src/idl_gen_dart.cpp @@ -251,12 +251,11 @@ class DartGenerator : public BaseGenerator { " static bool containsValue(int value) =>" " values.containsKey(value);\n\n"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; if (!ev.doc_comment.empty()) { - if (it != enum_def.vals.vec.begin()) { code += '\n'; } + if (it != enum_def.Vals().begin()) { code += '\n'; } GenDocComment(ev.doc_comment, &code, "", " "); } code += " static const " + name + " " + ev.name + " = "; @@ -264,8 +263,7 @@ class DartGenerator : public BaseGenerator { } code += " static get values => {"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; code += NumToString(ev.value) + ": " + ev.name + ","; } @@ -503,8 +501,9 @@ class DartGenerator : public BaseGenerator { if (field.value.type.base_type == BASE_TYPE_UNION) { code += " {\n"; code += " switch (" + field_name + "Type?.value) {\n"; - for (auto en_it = field.value.type.enum_def->vals.vec.begin() + 1; - en_it != field.value.type.enum_def->vals.vec.end(); ++en_it) { + auto &enum_def = *field.value.type.enum_def; + for (auto en_it = enum_def.Vals().begin() + 1; + en_it != enum_def.Vals().end(); ++en_it) { auto &ev = **en_it; auto enum_name = NamespaceAliasFromUnionType(ev.name); diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 37c6026..5a85a95 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -96,8 +96,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) { else schema += "enum " + enum_def.name + " : "; schema += GenType(enum_def.underlying_type, true) + " {\n"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, &schema, nullptr, " "); if (enum_def.is_union) diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index c47ccff..fe793bf 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -543,8 +543,7 @@ class GeneralGenerator : public BaseGenerator { if (lang_.language == IDLOptions::kJava) { code += " private " + enum_def.name + "() { }\n"; } - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, &lang_.comment_config, " "); if (lang_.language != IDLOptions::kCSharp) { @@ -574,8 +573,8 @@ class GeneralGenerator : public BaseGenerator { code += lang_.const_decl; code += lang_.string_type; code += "[] names = { "; - auto val = enum_def.vals.vec.front()->value; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + auto val = enum_def.Vals().front()->value; + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { while (val++ != (*it)->value) code += "\"\", "; code += "\"" + (*it)->name + "\", "; diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 7a68e8a..412d1e8 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -153,7 +153,7 @@ class GoGenerator : public BaseGenerator { } // A single enum member. - void EnumMember(const EnumDef &enum_def, const EnumVal ev, + void EnumMember(const EnumDef &enum_def, const EnumVal &ev, std::string *code_ptr) { std::string &code = *code_ptr; code += "\t"; @@ -725,8 +725,7 @@ class GoGenerator : public BaseGenerator { GenComment(enum_def.doc_comment, code_ptr, nullptr); GenEnumType(enum_def, code_ptr); BeginEnum(code_ptr); - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr, "\t"); EnumMember(enum_def, ev, code_ptr); @@ -734,8 +733,7 @@ class GoGenerator : public BaseGenerator { EndEnum(code_ptr); BeginEnumNames(enum_def, code_ptr); - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; EnumNameMember(enum_def, ev, code_ptr); } diff --git a/src/idl_gen_js_ts.cpp b/src/idl_gen_js_ts.cpp index 9b7942b..fea4620 100644 --- a/src/idl_gen_js_ts.cpp +++ b/src/idl_gen_js_ts.cpp @@ -350,11 +350,10 @@ class JsTsGenerator : public BaseGenerator { } code += WrapInNameSpace(enum_def) + (reverse ? "Name" : "") + " = {\n"; } - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; if (!ev.doc_comment.empty()) { - if (it != enum_def.vals.vec.begin()) { code += '\n'; } + if (it != enum_def.Vals().begin()) { code += '\n'; } GenDocComment(ev.doc_comment, code_ptr, "", " "); } @@ -369,7 +368,7 @@ class JsTsGenerator : public BaseGenerator { code += NumToString(ev.value); } - code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n"; + code += (it + 1) != enum_def.Vals().end() ? ",\n" : "\n"; if (ev.union_type.struct_def) { ReexportDescription desc = { ev.name, diff --git a/src/idl_gen_json_schema.cpp b/src/idl_gen_json_schema.cpp index 1883580..a0f193b 100644 --- a/src/idl_gen_json_schema.cpp +++ b/src/idl_gen_json_schema.cpp @@ -86,7 +86,7 @@ std::string GenType(const Type &type) { } case BASE_TYPE_UNION: { std::string union_type_string("\"anyOf\": ["); - const auto &union_types = type.enum_def->vals.vec; + const auto &union_types = type.enum_def->Vals(); for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) { auto &union_type = *ut; if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; } @@ -94,7 +94,7 @@ std::string GenType(const Type &type) { union_type_string.append( "{ " + GenTypeRef(union_type->union_type.struct_def) + " }"); } - if (union_type != *type.enum_def->vals.vec.rbegin()) { + if (union_type != *type.enum_def->Vals().rbegin()) { union_type_string.append(","); } } @@ -128,10 +128,10 @@ class JsonSchemaGenerator : public BaseGenerator { code_ += " \"" + GenFullName(*e) + "\" : {"; code_ += " " + GenType("string") + ","; std::string enumdef(" \"enum\": ["); - for (auto enum_value = (*e)->vals.vec.begin(); - enum_value != (*e)->vals.vec.end(); ++enum_value) { + for (auto enum_value = (*e)->Vals().begin(); + enum_value != (*e)->Vals().end(); ++enum_value) { enumdef.append("\"" + (*enum_value)->name + "\""); - if (*enum_value != (*e)->vals.vec.back()) { enumdef.append(", "); } + if (*enum_value != (*e)->Vals().back()) { enumdef.append(", "); } } enumdef.append("]"); code_ += enumdef; diff --git a/src/idl_gen_lobster.cpp b/src/idl_gen_lobster.cpp index 5f199e3..fe23d97 100644 --- a/src/idl_gen_lobster.cpp +++ b/src/idl_gen_lobster.cpp @@ -149,10 +149,10 @@ class LobsterGenerator : public BaseGenerator { break; } case BASE_TYPE_UNION: { - for (auto it = field.value.type.enum_def->vals.vec.begin(); - it != field.value.type.enum_def->vals.vec.end(); ++it) { + for (auto it = field.value.type.enum_def->Vals().begin(); + it != field.value.type.enum_def->Vals().end(); ++it) { auto &ev = **it; - if (ev.value) { + if (ev.IsNonZero()) { code += def + "_as_" + ev.name + "():\n " + NamespacedName(*ev.union_type.struct_def) + " { buf_, buf_.flatbuffers_field_table(pos_, " + offsets + @@ -259,13 +259,12 @@ class LobsterGenerator : public BaseGenerator { CheckNameSpace(enum_def, &code); GenComment(enum_def.doc_comment, code_ptr, nullptr, ""); code += "enum + \n"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr, " "); code += " " + enum_def.name + "_" + NormalizedName(ev) + " = " + NumToString(ev.value); - if (it + 1 != enum_def.vals.vec.end()) code += ","; + if (it + 1 != enum_def.Vals().end()) code += ","; code += "\n"; } code += "\n"; diff --git a/src/idl_gen_lua.cpp b/src/idl_gen_lua.cpp index e8ae328..b26f907 100644 --- a/src/idl_gen_lua.cpp +++ b/src/idl_gen_lua.cpp @@ -109,9 +109,10 @@ namespace lua { } // A single enum member. - void EnumMember(const EnumVal ev, std::string *code_ptr) { + void EnumMember(const EnumDef &enum_def, const EnumVal &ev, std::string *code_ptr) { std::string &code = *code_ptr; code += std::string(Indent) + NormalizedName(ev) + " = " + NumToString(ev.value) + ",\n"; + (void)enum_def; } // End enum code. @@ -336,7 +337,7 @@ namespace lua { } code += EndFunc; } - + // Begin the creator function signature. void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) { @@ -497,7 +498,7 @@ namespace lua { GetMemberOfVectorOfStruct(struct_def, field, code_ptr); } else { - GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr); + GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr); } break; } @@ -572,11 +573,11 @@ namespace lua { GenComment(enum_def.doc_comment, code_ptr, nullptr, Comment); BeginEnum(NormalizedName(enum_def), code_ptr); - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); + ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr, Comment); - EnumMember(ev, code_ptr); + EnumMember(enum_def, ev, code_ptr); } EndEnum(code_ptr); } diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 3c6747e..a4b9a46 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -119,12 +119,14 @@ class PhpGenerator : public BaseGenerator { } // A single enum member. - static void EnumMember(const EnumVal ev, std::string *code_ptr) { + static void EnumMember(const EnumDef &enum_def, const EnumVal &ev, + std::string *code_ptr) { std::string &code = *code_ptr; code += Indent + "const "; code += ev.name; code += " = "; code += NumToString(ev.value) + ";\n"; + (void)enum_def; } // End enum code. @@ -815,18 +817,16 @@ class PhpGenerator : public BaseGenerator { GenComment(enum_def.doc_comment, code_ptr, nullptr); BeginEnum(enum_def.name, code_ptr); - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr); - EnumMember(ev, code_ptr); + EnumMember(enum_def, ev, code_ptr); } std::string &code = *code_ptr; code += "\n"; code += Indent + "private static $names = array(\n"; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; code += Indent + Indent + enum_def.name + "::" + ev.name + "=>" + "\"" + ev.name + "\",\n"; } diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index fbb0805..f2688e4 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -112,12 +112,14 @@ class PythonGenerator : public BaseGenerator { } // A single enum member. - void EnumMember(const EnumVal ev, std::string *code_ptr) { + void EnumMember(const EnumDef &enum_def, const EnumVal &ev, + std::string *code_ptr) { std::string &code = *code_ptr; code += Indent; code += NormalizedName(ev); code += " = "; code += NumToString(ev.value) + "\n"; + (void)enum_def; } // End enum code. @@ -589,11 +591,10 @@ class PythonGenerator : public BaseGenerator { GenComment(enum_def.doc_comment, code_ptr, nullptr, "# "); BeginEnum(NormalizedName(enum_def), code_ptr); - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr, "# "); - EnumMember(ev, code_ptr); + EnumMember(enum_def, ev, code_ptr); } EndEnum(code_ptr); } diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 1b8ff11..23fd34a 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -295,7 +295,7 @@ class RustGenerator : public BaseGenerator { // structs, and tables) and output them to a single file. bool generate() { code_.Clear(); - code_ += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n"; + code_ += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n"; assert(!cur_name_space_); @@ -591,8 +591,7 @@ class RustGenerator : public BaseGenerator { int64_t anyv = 0; const EnumVal *minv = nullptr, *maxv = nullptr; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; GenComment(ev.doc_comment, " "); @@ -655,15 +654,14 @@ class RustGenerator : public BaseGenerator { code_ += ""; // Generate an array of all enumeration values. - auto num_fields = NumToString(enum_def.vals.vec.size()); + auto num_fields = NumToString(enum_def.size()); code_ += "#[allow(non_camel_case_types)]"; code_ += "const ENUM_VALUES_{{ENUM_NAME_CAPS}}:[{{ENUM_NAME}}; " + num_fields + "] = ["; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); - ++it) { + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; auto value = GetEnumValUse(enum_def, ev); - auto suffix = *it != enum_def.vals.vec.back() ? "," : ""; + auto suffix = *it != enum_def.Vals().back() ? "," : ""; code_ += " " + value + suffix; } code_ += "];"; @@ -684,8 +682,8 @@ class RustGenerator : public BaseGenerator { code_ += "const ENUM_NAMES_{{ENUM_NAME_CAPS}}:[&'static str; " + NumToString(range) + "] = ["; - auto val = enum_def.vals.vec.front()->value; - for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + auto val = enum_def.Vals().front()->value; + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { const auto &ev = **it; while (val++ != ev.value) { code_ += " \"\","; } @@ -1317,7 +1315,7 @@ class RustGenerator : public BaseGenerator { code_.SetValue("FIELD_NAME", Name(field)); - for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); ++u_it) { + for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) { auto &ev = **u_it; if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 583538f..70f6cab 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -135,21 +135,21 @@ template CheckedError Parser::Recurse(F f) { return ce; } -CheckedError Parser::InvalidNumber(const char *number, const std::string &msg) { - return Error("invalid number: \"" + std::string(number) + "\"" + msg); +template std::string TypeToIntervalString() { + return "[" + NumToString((flatbuffers::numeric_limits::lowest)()) + "; " + + NumToString((flatbuffers::numeric_limits::max)()) + "]"; } -// atot: templated version of atoi/atof: convert a string to an instance of T. + +// atot: template version of atoi/atof: convert a string to an instance of T. template inline CheckedError atot(const char *s, Parser &parser, T *val) { auto done = StringToNumber(s, val); if (done) return NoError(); - - return parser.InvalidNumber( - s, (0 == *val) - ? "" - : (", constant does not fit [" + - NumToString(flatbuffers::numeric_limits::lowest()) + "; " + - NumToString(flatbuffers::numeric_limits::max()) + "]")); + if (0 == *val) + return parser.Error("invalid number: \"" + std::string(s) + "\""); + else + return parser.Error("invalid number: \"" + std::string(s) + "\"" + + ", constant does not fit " + TypeToIntervalString()); } template<> inline CheckedError atot>(const char *s, Parser &parser, @@ -1254,7 +1254,7 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field, nested_parser.enums_.vec.clear(); if (!ok) { - ECHECK(Error(nested_parser.error_)); + ECHECK(Error(nested_parser.error_)); } // Force alignment for nested flatbuffer builder_.ForceVectorAlignment(nested_parser.builder_.GetSize(), sizeof(uint8_t), @@ -1334,8 +1334,9 @@ CheckedError Parser::TryTypedValue(const std::string *name, int dtoken, return NoError(); } -CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) { - *result = 0; +CheckedError Parser::ParseEnumFromString(const Type &type, + std::string *result) { + int64_t i64 = 0; // Parse one or more enum identifiers, separated by spaces. const char *next = attribute_.c_str(); do { @@ -1353,7 +1354,7 @@ CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) { if (!enum_val) return Error("unknown enum value: " + word + ", for enum: " + type.enum_def->name); - *result |= enum_val->value; + i64 |= enum_val->value; } else { // No enum type, probably integral field. if (!IsInteger(type.base_type)) return Error("not a valid value for this field: " + word); @@ -1367,9 +1368,10 @@ CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) { if (!enum_def) return Error("unknown enum: " + enum_def_str); auto enum_val = enum_def->vals.Lookup(enum_val_str); if (!enum_val) return Error("unknown enum value: " + enum_val_str); - *result |= enum_val->value; + i64 |= enum_val->value; } } while (*next); + *result = NumToString(i64); return NoError(); } @@ -1503,9 +1505,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e, // Enum can have only true integer base type. if (!match && IsInteger(e.type.base_type) && !IsBool(e.type.base_type) && IsIdentifierStart(*attribute_.c_str())) { - int64_t val; - ECHECK(ParseEnumFromString(e.type, &val)); - e.constant = NumToString(val); + ECHECK(ParseEnumFromString(e.type, &e.constant)); NEXT(); match = true; } @@ -2482,8 +2482,8 @@ CheckedError Parser::ParseRoot(const char *source, const char **include_paths, for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) { auto &enum_def = **it; if (enum_def.is_union) { - for (auto val_it = enum_def.vals.vec.begin(); - val_it != enum_def.vals.vec.end(); ++val_it) { + for (auto val_it = enum_def.Vals().begin(); + val_it != enum_def.Vals().end(); ++val_it) { auto &val = **val_it; if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def && val.union_type.struct_def->fixed) @@ -3192,7 +3192,7 @@ std::string Parser::ConformTo(const Parser &base) { enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name); auto enum_def_base = base.enums_.Lookup(qualified_name); if (!enum_def_base) continue; - for (auto evit = enum_def.vals.vec.begin(); evit != enum_def.vals.vec.end(); + for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end(); ++evit) { auto &enum_val = **evit; auto enum_val_base = enum_def_base->vals.Lookup(enum_val.name); -- 2.7.4