2 * Copyright 2014 Google Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "flatbuffers/idl.h"
25 #include "flatbuffers/util.h"
27 namespace flatbuffers {
29 // Reflects the version at the compiling time of binary(lib/dll/so).
30 const char *FLATBUFFERS_VERSION() {
33 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
34 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
35 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
39 const double kPi = 3.14159265358979323846;
41 const char *const kTypeNames[] = {
43 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
44 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
46 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
52 const char kTypeSizes[] = {
54 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
55 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
57 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
62 // The enums in the reflection schema should match the ones we use internally.
63 // Compare the last element to check if these go out of sync.
64 static_assert(BASE_TYPE_UNION == static_cast<BaseType>(reflection::Union),
67 // Any parsing calls have to be wrapped in this macro, which automates
68 // handling of recursive error checking a bit. It will check the received
69 // CheckedError object, and return straight away on error.
70 #define ECHECK(call) \
73 if (ce.Check()) return ce; \
76 // These two functions are called hundreds of times below, so define a short
78 #define NEXT() ECHECK(Next())
79 #define EXPECT(tok) ECHECK(Expect(tok))
81 static bool ValidateUTF8(const std::string &str) {
82 const char *s = &str[0];
83 const char *const sEnd = s + str.length();
85 if (FromUTF8(&s) < 0) { return false; }
90 // Convert an underscore_based_indentifier in to camelCase.
91 // Also uppercases the first character if first is true.
92 std::string MakeCamel(const std::string &in, bool first) {
94 for (size_t i = 0; i < in.length(); i++) {
96 s += static_cast<char>(toupper(in[0]));
97 else if (in[i] == '_' && i + 1 < in.length())
98 s += static_cast<char>(toupper(in[++i]));
105 // Convert an underscore_based_identifier in to screaming snake case.
106 std::string MakeScreamingCamel(const std::string &in) {
108 for (size_t i = 0; i < in.length(); i++) {
110 s += static_cast<char>(toupper(in[i]));
117 void DeserializeDoc( std::vector<std::string> &doc,
118 const Vector<Offset<String>> *documentation) {
119 if (documentation == nullptr) return;
120 for (uoffset_t index = 0; index < documentation->size(); index++)
121 doc.push_back(documentation->Get(index)->str());
124 void Parser::Message(const std::string &msg) {
125 if (!error_.empty()) error_ += "\n"; // log all warnings and errors
126 error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
129 #ifdef _WIN32 // MSVC alike
131 "(" + NumToString(line_) + ", " + NumToString(CursorPosition()) + ")";
133 if (file_being_parsed_.length()) error_ += ":";
134 error_ += NumToString(line_) + ": " + NumToString(CursorPosition());
137 error_ += ": " + msg;
140 void Parser::Warning(const std::string &msg) { Message("warning: " + msg); }
142 CheckedError Parser::Error(const std::string &msg) {
143 Message("error: " + msg);
144 return CheckedError(true);
147 inline CheckedError NoError() { return CheckedError(false); }
149 CheckedError Parser::RecurseError() {
150 return Error("maximum parsing recursion of " +
151 NumToString(FLATBUFFERS_MAX_PARSING_DEPTH) + " reached");
154 template<typename F> CheckedError Parser::Recurse(F f) {
155 if (recurse_protection_counter >= (FLATBUFFERS_MAX_PARSING_DEPTH))
156 return RecurseError();
157 recurse_protection_counter++;
159 recurse_protection_counter--;
163 template<typename T> std::string TypeToIntervalString() {
164 return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
165 NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
168 // atot: template version of atoi/atof: convert a string to an instance of T.
170 inline CheckedError atot(const char *s, Parser &parser, T *val) {
171 auto done = StringToNumber(s, val);
172 if (done) return NoError();
174 return parser.Error("invalid number: \"" + std::string(s) + "\"");
176 return parser.Error("invalid number: \"" + std::string(s) + "\"" +
177 ", constant does not fit " + TypeToIntervalString<T>());
180 inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
183 *val = Offset<void>(atoi(s));
187 std::string Namespace::GetFullyQualifiedName(const std::string &name,
188 size_t max_components) const {
189 // Early exit if we don't have a defined namespace.
190 if (components.empty() || !max_components) { return name; }
191 std::string stream_str;
192 for (size_t i = 0; i < std::min(components.size(), max_components); i++) {
193 if (i) { stream_str += '.'; }
194 stream_str += std::string(components[i]);
203 // Declare tokens we'll use. Single character tokens are represented by their
204 // ascii character code (e.g. '{'), others above 256.
206 #define FLATBUFFERS_GEN_TOKENS(TD) \
207 TD(Eof, 256, "end of file") \
208 TD(StringConstant, 257, "string constant") \
209 TD(IntegerConstant, 258, "integer constant") \
210 TD(FloatConstant, 259, "float constant") \
211 TD(Identifier, 260, "identifier")
213 __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
216 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
217 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
218 #undef FLATBUFFERS_TOKEN
221 static std::string TokenToString(int t) {
222 static const char * const tokens[] = {
223 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
224 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
225 #undef FLATBUFFERS_TOKEN
226 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
227 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
229 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
230 #undef FLATBUFFERS_TD
232 if (t < 256) { // A single ascii char token.
234 s.append(1, static_cast<char>(t));
236 } else { // Other tokens.
237 return tokens[t - 256];
242 std::string Parser::TokenToStringId(int t) const {
243 return t == kTokenIdentifier ? attribute_ : TokenToString(t);
246 // Parses exactly nibbles worth of hex digits into a number, or error.
247 CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
248 FLATBUFFERS_ASSERT(nibbles > 0);
249 for (int i = 0; i < nibbles; i++)
250 if (!is_xdigit(cursor_[i]))
251 return Error("escape code must be followed by " + NumToString(nibbles) +
253 std::string target(cursor_, cursor_ + nibbles);
254 *val = StringToUInt(target.c_str(), 16);
259 CheckedError Parser::SkipByteOrderMark() {
260 if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
262 if (static_cast<unsigned char>(*cursor_) != 0xbb)
263 return Error("invalid utf-8 byte order mark");
265 if (static_cast<unsigned char>(*cursor_) != 0xbf)
266 return Error("invalid utf-8 byte order mark");
271 static inline bool IsIdentifierStart(char c) {
272 return is_alpha(c) || (c == '_');
275 CheckedError Parser::Next() {
276 doc_comment_.clear();
277 bool seen_newline = cursor_ == source_;
279 attr_is_trivial_ascii_string_ = true;
304 case '=': return NoError();
307 int unicode_high_surrogate = -1;
309 while (*cursor_ != c) {
310 if (*cursor_ < ' ' && static_cast<signed char>(*cursor_) >= 0)
311 return Error("illegal character in string constant");
312 if (*cursor_ == '\\') {
313 attr_is_trivial_ascii_string_ = false; // has escape sequence
315 if (unicode_high_surrogate != -1 && *cursor_ != 'u') {
317 "illegal Unicode sequence (unpaired high surrogate)");
356 case 'x': { // Not in the JSON standard
359 ECHECK(ParseHexNum(2, &val));
360 attribute_ += static_cast<char>(val);
366 ECHECK(ParseHexNum(4, &val));
367 if (val >= 0xD800 && val <= 0xDBFF) {
368 if (unicode_high_surrogate != -1) {
370 "illegal Unicode sequence (multiple high surrogates)");
372 unicode_high_surrogate = static_cast<int>(val);
374 } else if (val >= 0xDC00 && val <= 0xDFFF) {
375 if (unicode_high_surrogate == -1) {
377 "illegal Unicode sequence (unpaired low surrogate)");
379 int code_point = 0x10000 +
380 ((unicode_high_surrogate & 0x03FF) << 10) +
382 ToUTF8(code_point, &attribute_);
383 unicode_high_surrogate = -1;
386 if (unicode_high_surrogate != -1) {
388 "illegal Unicode sequence (unpaired high surrogate)");
390 ToUTF8(static_cast<int>(val), &attribute_);
394 default: return Error("unknown escape code in string constant");
396 } else { // printable chars + UTF-8 bytes
397 if (unicode_high_surrogate != -1) {
399 "illegal Unicode sequence (unpaired high surrogate)");
401 // reset if non-printable
402 attr_is_trivial_ascii_string_ &= check_ascii_range(*cursor_, ' ', '~');
404 attribute_ += *cursor_++;
407 if (unicode_high_surrogate != -1) {
408 return Error("illegal Unicode sequence (unpaired high surrogate)");
411 if (!attr_is_trivial_ascii_string_ && !opts.allow_non_utf8 &&
412 !ValidateUTF8(attribute_)) {
413 return Error("illegal UTF-8 sequence");
415 token_ = kTokenStringConstant;
419 if (*cursor_ == '/') {
420 const char *start = ++cursor_;
421 while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++;
422 if (*start == '/') { // documentation comment
425 "a documentation comment should be on a line on its own");
426 doc_comment_.push_back(std::string(start + 1, cursor_));
429 } else if (*cursor_ == '*') {
431 // TODO: make nested.
432 while (*cursor_ != '*' || cursor_[1] != '/') {
433 if (*cursor_ == '\n') MarkNewLine();
434 if (!*cursor_) return Error("end of file in comment");
440 FLATBUFFERS_FALLTHROUGH(); // else fall thru
442 const auto has_sign = (c == '+') || (c == '-');
443 // '-'/'+' and following identifier - can be a predefined constant like:
444 // NAN, INF, PI, etc.
445 if (IsIdentifierStart(c) || (has_sign && IsIdentifierStart(*cursor_))) {
446 // Collect all chars of an identifier:
447 const char *start = cursor_ - 1;
448 while (IsIdentifierStart(*cursor_) || is_digit(*cursor_)) cursor_++;
449 attribute_.append(start, cursor_);
450 token_ = has_sign ? kTokenStringConstant : kTokenIdentifier;
454 auto dot_lvl = (c == '.') ? 0 : 1; // dot_lvl==0 <=> exactly one '.' seen
455 if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum?
456 // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4).
457 if (is_digit(c) || has_sign || !dot_lvl) {
458 const auto start = cursor_ - 1;
459 auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1;
460 if (!is_digit(c) && is_digit(*cursor_)){
461 start_digits = cursor_; // see digit in cursor_ position
464 // hex-float can't begind with '.'
465 auto use_hex = dot_lvl && (c == '0') && is_alpha_char(*cursor_, 'X');
466 if (use_hex) start_digits = ++cursor_; // '0x' is the prefix, skip it
467 // Read an integer number or mantisa of float-point number.
470 while (is_xdigit(*cursor_)) cursor_++;
472 while (is_digit(*cursor_)) cursor_++;
474 } while ((*cursor_ == '.') && (++cursor_) && (--dot_lvl >= 0));
475 // Exponent of float-point number.
476 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
477 // The exponent suffix of hexadecimal float number is mandatory.
478 if (use_hex && !dot_lvl) start_digits = cursor_;
479 if ((use_hex && is_alpha_char(*cursor_, 'P')) ||
480 is_alpha_char(*cursor_, 'E')) {
481 dot_lvl = 0; // Emulate dot to signal about float-point number.
483 if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
484 start_digits = cursor_; // the exponent-part has to have digits
485 // Exponent is decimal integer number
486 while (is_digit(*cursor_)) cursor_++;
487 if (*cursor_ == '.') {
488 cursor_++; // If see a dot treat it as part of invalid number.
489 dot_lvl = -1; // Fall thru to Error().
494 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
495 attribute_.append(start, cursor_);
496 token_ = dot_lvl ? kTokenIntegerConstant : kTokenFloatConstant;
499 return Error("invalid number: " + std::string(start, cursor_));
504 if (false == check_ascii_range(c, ' ', '~')) ch = "code: " + NumToString(c);
505 return Error("illegal character: " + ch);
510 // Check if a given token is next.
511 bool Parser::Is(int t) const { return t == token_; }
513 bool Parser::IsIdent(const char *id) const {
514 return token_ == kTokenIdentifier && attribute_ == id;
517 // Expect a given token to be next, consume it, or error if not present.
518 CheckedError Parser::Expect(int t) {
520 return Error("expecting: " + TokenToString(t) +
521 " instead got: " + TokenToStringId(token_));
527 CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
532 if (last) *last = attribute_;
533 EXPECT(kTokenIdentifier);
538 EnumDef *Parser::LookupEnum(const std::string &id) {
539 // Search thru parent namespaces.
540 for (int components = static_cast<int>(current_namespace_->components.size());
541 components >= 0; components--) {
542 auto ed = enums_.Lookup(
543 current_namespace_->GetFullyQualifiedName(id, components));
549 StructDef *Parser::LookupStruct(const std::string &id) const {
550 auto sd = structs_.Lookup(id);
551 if (sd) sd->refcount++;
555 CheckedError Parser::ParseTypeIdent(Type &type) {
556 std::string id = attribute_;
557 EXPECT(kTokenIdentifier);
558 ECHECK(ParseNamespacing(&id, nullptr));
559 auto enum_def = LookupEnum(id);
561 type = enum_def->underlying_type;
562 if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
564 type.base_type = BASE_TYPE_STRUCT;
565 type.struct_def = LookupCreateStruct(id);
570 // Parse any IDL type.
571 CheckedError Parser::ParseType(Type &type) {
572 if (token_ == kTokenIdentifier) {
573 if (IsIdent("bool")) {
574 type.base_type = BASE_TYPE_BOOL;
576 } else if (IsIdent("byte") || IsIdent("int8")) {
577 type.base_type = BASE_TYPE_CHAR;
579 } else if (IsIdent("ubyte") || IsIdent("uint8")) {
580 type.base_type = BASE_TYPE_UCHAR;
582 } else if (IsIdent("short") || IsIdent("int16")) {
583 type.base_type = BASE_TYPE_SHORT;
585 } else if (IsIdent("ushort") || IsIdent("uint16")) {
586 type.base_type = BASE_TYPE_USHORT;
588 } else if (IsIdent("int") || IsIdent("int32")) {
589 type.base_type = BASE_TYPE_INT;
591 } else if (IsIdent("uint") || IsIdent("uint32")) {
592 type.base_type = BASE_TYPE_UINT;
594 } else if (IsIdent("long") || IsIdent("int64")) {
595 type.base_type = BASE_TYPE_LONG;
597 } else if (IsIdent("ulong") || IsIdent("uint64")) {
598 type.base_type = BASE_TYPE_ULONG;
600 } else if (IsIdent("float") || IsIdent("float32")) {
601 type.base_type = BASE_TYPE_FLOAT;
603 } else if (IsIdent("double") || IsIdent("float64")) {
604 type.base_type = BASE_TYPE_DOUBLE;
606 } else if (IsIdent("string")) {
607 type.base_type = BASE_TYPE_STRING;
610 ECHECK(ParseTypeIdent(type));
612 } else if (token_ == '[') {
615 ECHECK(Recurse([&]() { return ParseType(subtype); }));
616 if (IsSeries(subtype)) {
617 // We could support this, but it will complicate things, and it's
618 // easier to work around with a struct around the inner vector.
619 return Error("nested vector types not supported (wrap in table first)");
623 if (token_ != kTokenIntegerConstant) {
624 return Error("length of fixed-length array must be an integer value");
626 uint16_t fixed_length = 0;
627 bool check = StringToNumber(attribute_.c_str(), &fixed_length);
628 if (!check || fixed_length < 1) {
630 "length of fixed-length array must be positive and fit to "
633 // Check if enum arrays are used in C++ without specifying --scoped-enums
634 if ((opts.lang_to_generate & IDLOptions::kCpp) && !opts.scoped_enums &&
637 "--scoped-enums must be enabled to use enum arrays in C++\n");
639 type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def,
643 type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
645 type.element = subtype.base_type;
648 return Error("illegal type syntax");
653 CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
654 const Type &type, FieldDef **dest) {
655 auto &field = *new FieldDef();
657 FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
659 field.file = struct_def.file;
660 field.value.type = type;
661 if (struct_def.fixed) { // statically compute the field offset
662 auto size = InlineSize(type);
663 auto alignment = InlineAlignment(type);
664 // structs_ need to have a predictable format, so we need to align to
665 // the largest scalar
666 struct_def.minalign = std::max(struct_def.minalign, alignment);
667 struct_def.PadLastField(alignment);
668 field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
669 struct_def.bytesize += size;
671 if (struct_def.fields.Add(name, &field))
672 return Error("field already exists: " + name);
677 CheckedError Parser::ParseField(StructDef &struct_def) {
678 std::string name = attribute_;
680 if (LookupCreateStruct(name, false, false))
681 return Error("field name can not be the same as table/struct name");
683 std::vector<std::string> dc = doc_comment_;
684 EXPECT(kTokenIdentifier);
687 ECHECK(ParseType(type));
689 if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type) &&
691 return Error("structs_ may contain only scalar or struct fields");
693 if (!struct_def.fixed && IsArray(type))
694 return Error("fixed-length array in table must be wrapped in struct");
696 if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
698 "Arrays are not yet supported in all "
699 "the specified programming languages.");
702 FieldDef *typefield = nullptr;
703 if (type.base_type == BASE_TYPE_UNION) {
704 // For union fields, add a second auto-generated field to hold the type,
705 // with a special suffix.
706 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
707 type.enum_def->underlying_type, &typefield));
708 } else if (type.base_type == BASE_TYPE_VECTOR &&
709 type.element == BASE_TYPE_UNION) {
710 // Only cpp, js and ts supports the union vector feature so far.
711 if (!SupportsAdvancedUnionFeatures()) {
713 "Vectors of unions are not yet supported in all "
714 "the specified programming languages.");
716 // For vector of union fields, add a second auto-generated vector field to
717 // hold the types, with a special suffix.
718 Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def);
719 union_vector.element = BASE_TYPE_UTYPE;
720 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector,
725 ECHECK(AddField(struct_def, name, type, &field));
729 ECHECK(ParseSingleValue(&field->name, field->value, true));
730 if (!IsScalar(type.base_type) ||
731 (struct_def.fixed && field->value.constant != "0"))
733 "default values currently only supported for scalars in tables");
735 // Append .0 if the value has not it (skip hex and scientific floats).
736 // This suffix needed for generated C++ code.
737 if (IsFloat(type.base_type)) {
738 auto &text = field->value.constant;
739 FLATBUFFERS_ASSERT(false == text.empty());
740 auto s = text.c_str();
741 while(*s == ' ') s++;
742 if (*s == '-' || *s == '+') s++;
743 // 1) A float constants (nan, inf, pi, etc) is a kind of identifier.
744 // 2) A float number needn't ".0" at the end if it has exponent.
745 if ((false == IsIdentifierStart(*s)) &&
746 (std::string::npos == field->value.constant.find_first_of(".eEpP"))) {
747 field->value.constant += ".0";
751 // The type.base_type can only be scalar, union, array or vector.
752 // Table, struct or string can't have enum_def.
753 // Default value of union and vector in NONE, NULL translated to "0".
754 FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
755 (type.base_type == BASE_TYPE_UNION) ||
756 (type.base_type == BASE_TYPE_VECTOR) ||
757 (type.base_type == BASE_TYPE_ARRAY));
758 if (type.base_type == BASE_TYPE_VECTOR) {
759 // Vector can't use initialization list.
760 FLATBUFFERS_ASSERT(field->value.constant == "0");
762 // All unions should have the NONE ("0") enum value.
763 auto in_enum = type.enum_def->attributes.Lookup("bit_flags") ||
764 type.enum_def->FindByValue(field->value.constant);
765 if (false == in_enum)
766 return Error("default value of " + field->value.constant +
767 " for field " + name + " is not part of enum " +
768 type.enum_def->name);
772 field->doc_comment = dc;
773 ECHECK(ParseMetaData(&field->attributes));
774 field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
775 auto hash_name = field->attributes.Lookup("hash");
777 switch ((type.base_type == BASE_TYPE_VECTOR) ? type.element : type.base_type) {
778 case BASE_TYPE_SHORT:
779 case BASE_TYPE_USHORT: {
780 if (FindHashFunction16(hash_name->constant.c_str()) == nullptr)
781 return Error("Unknown hashing algorithm for 16 bit types: " +
782 hash_name->constant);
786 case BASE_TYPE_UINT: {
787 if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
788 return Error("Unknown hashing algorithm for 32 bit types: " +
789 hash_name->constant);
793 case BASE_TYPE_ULONG: {
794 if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
795 return Error("Unknown hashing algorithm for 64 bit types: " +
796 hash_name->constant);
801 "only short, ushort, int, uint, long and ulong data types support hashing.");
804 auto cpp_type = field->attributes.Lookup("cpp_type");
807 return Error("cpp_type can only be used with a hashed field");
808 /// forcing cpp_ptr_type to 'naked' if unset
809 auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
811 auto val = new Value();
812 val->type = cpp_type->type;
813 val->constant = "naked";
814 field->attributes.Add("cpp_ptr_type", val);
817 if (field->deprecated && struct_def.fixed)
818 return Error("can't deprecate fields in a struct");
819 field->required = field->attributes.Lookup("required") != nullptr;
820 if (field->required &&
821 (struct_def.fixed || IsScalar(type.base_type)))
822 return Error("only non-scalar fields in tables may be 'required'");
823 field->key = field->attributes.Lookup("key") != nullptr;
825 if (struct_def.has_key) return Error("only one field may be set as 'key'");
826 struct_def.has_key = true;
827 if (!IsScalar(type.base_type)) {
828 field->required = true;
829 if (type.base_type != BASE_TYPE_STRING)
830 return Error("'key' field must be string or scalar type");
833 field->shared = field->attributes.Lookup("shared") != nullptr;
834 if (field->shared && field->value.type.base_type != BASE_TYPE_STRING)
835 return Error("shared can only be defined on strings");
837 auto field_native_custom_alloc =
838 field->attributes.Lookup("native_custom_alloc");
839 if (field_native_custom_alloc)
841 "native_custom_alloc can only be used with a table or struct "
844 field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
845 if (field->native_inline && !IsStruct(field->value.type))
846 return Error("native_inline can only be defined on structs");
848 auto nested = field->attributes.Lookup("nested_flatbuffer");
850 if (nested->type.base_type != BASE_TYPE_STRING)
852 "nested_flatbuffer attribute must be a string (the root type)");
853 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
855 "nested_flatbuffer attribute may only apply to a vector of ubyte");
856 // This will cause an error if the root type of the nested flatbuffer
857 // wasn't defined elsewhere.
858 field->nested_flatbuffer = LookupCreateStruct(nested->constant);
861 if (field->attributes.Lookup("flexbuffer")) {
862 field->flexbuffer = true;
863 uses_flexbuffers_ = true;
864 if (type.base_type != BASE_TYPE_VECTOR ||
865 type.element != BASE_TYPE_UCHAR)
866 return Error("flexbuffer attribute may only apply to a vector of ubyte");
870 if (!IsScalar(typefield->value.type.base_type)) {
871 // this is a union vector field
872 typefield->required = field->required;
874 // If this field is a union, and it has a manually assigned id,
875 // the automatically added type field should have an id as well (of N - 1).
876 auto attr = field->attributes.Lookup("id");
878 auto id = atoi(attr->constant.c_str());
879 auto val = new Value();
880 val->type = attr->type;
881 val->constant = NumToString(id - 1);
882 typefield->attributes.Add("id", val);
890 CheckedError Parser::ParseString(Value &val) {
892 EXPECT(kTokenStringConstant);
893 val.constant = NumToString(builder_.CreateString(s).o);
897 CheckedError Parser::ParseComma() {
898 if (!opts.protobuf_ascii_alike) EXPECT(',');
902 CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
903 size_t parent_fieldn,
904 const StructDef *parent_struct_def,
906 bool inside_vector) {
907 switch (val.type.base_type) {
908 case BASE_TYPE_UNION: {
909 FLATBUFFERS_ASSERT(field);
910 std::string constant;
911 Vector<uint8_t> *vector_of_union_types = nullptr;
912 // Find corresponding type field we may have already parsed.
913 for (auto elem = field_stack_.rbegin() + count;
914 elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) {
915 auto &type = elem->second->value.type;
916 if (type.enum_def == val.type.enum_def) {
918 if (type.base_type == BASE_TYPE_VECTOR &&
919 type.element == BASE_TYPE_UTYPE) {
920 // Vector of union type field.
922 ECHECK(atot(elem->first.constant.c_str(), *this, &offset));
923 vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>(
924 builder_.GetCurrentBufferPointer() +
925 builder_.GetSize() - offset);
929 if (type.base_type == BASE_TYPE_UTYPE) {
931 constant = elem->first.constant;
937 if (constant.empty() && !inside_vector) {
938 // We haven't seen the type field yet. Sadly a lot of JSON writers
939 // output these in alphabetical order, meaning it comes after this
940 // value. So we scan past the value to find it, then come back here.
941 // We currently don't do this for vectors of unions because the
942 // scanning/serialization logic would get very complicated.
943 auto type_name = field->name + UnionTypeFieldSuffix();
944 FLATBUFFERS_ASSERT(parent_struct_def);
945 auto type_field = parent_struct_def->fields.Lookup(type_name);
946 FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField().
947 // Remember where we are in the source file, so we can come back here.
948 auto backup = *static_cast<ParserState *>(this);
949 ECHECK(SkipAnyJsonValue()); // The table.
950 ECHECK(ParseComma());
951 auto next_name = attribute_;
952 if (Is(kTokenStringConstant)) {
955 EXPECT(kTokenIdentifier);
957 if (next_name == type_name) {
959 Value type_val = type_field->value;
960 ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr, 0));
961 constant = type_val.constant;
962 // Got the information we needed, now rewind:
963 *static_cast<ParserState *>(this) = backup;
966 if (constant.empty() && !vector_of_union_types) {
967 return Error("missing type field for this union value: " +
971 if (vector_of_union_types) {
972 enum_idx = vector_of_union_types->Get(count);
974 ECHECK(atot(constant.c_str(), *this, &enum_idx));
976 auto enum_val = val.type.enum_def->ReverseLookup(enum_idx, true);
977 if (!enum_val) return Error("illegal type id for: " + field->name);
978 if (enum_val->union_type.base_type == BASE_TYPE_STRUCT) {
979 ECHECK(ParseTable(*enum_val->union_type.struct_def, &val.constant,
981 if (enum_val->union_type.struct_def->fixed) {
982 // All BASE_TYPE_UNION values are offsets, so turn this into one.
983 SerializeStruct(*enum_val->union_type.struct_def, val);
984 builder_.ClearOffsets();
985 val.constant = NumToString(builder_.GetSize());
987 } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
988 ECHECK(ParseString(val));
990 FLATBUFFERS_ASSERT(false);
994 case BASE_TYPE_STRUCT:
995 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
997 case BASE_TYPE_STRING: {
998 ECHECK(ParseString(val));
1001 case BASE_TYPE_VECTOR: {
1003 ECHECK(ParseVector(val.type.VectorType(), &off, field, parent_fieldn));
1004 val.constant = NumToString(off);
1007 case BASE_TYPE_ARRAY: {
1008 ECHECK(ParseArray(val));
1012 case BASE_TYPE_UINT:
1013 case BASE_TYPE_LONG:
1014 case BASE_TYPE_ULONG: {
1015 if (field && field->attributes.Lookup("hash") &&
1016 (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
1017 ECHECK(ParseHash(val, field));
1019 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1024 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1030 void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
1031 SerializeStruct(builder_, struct_def, val);
1034 void Parser::SerializeStruct(FlatBufferBuilder &builder,
1035 const StructDef &struct_def, const Value &val) {
1036 FLATBUFFERS_ASSERT(val.constant.length() == struct_def.bytesize);
1037 builder.Align(struct_def.minalign);
1038 builder.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
1039 struct_def.bytesize);
1040 builder.AddStructOffset(val.offset, builder.GetSize());
1043 template <typename F>
1044 CheckedError Parser::ParseTableDelimiters(size_t &fieldn,
1045 const StructDef *struct_def,
1047 // We allow tables both as JSON object{ .. } with field names
1048 // or vector[..] with all fields in order
1049 char terminator = '}';
1050 bool is_nested_vector = struct_def && Is('[');
1051 if (is_nested_vector) {
1058 if ((!opts.strict_json || !fieldn) && Is(terminator)) break;
1060 if (is_nested_vector) {
1061 if (fieldn >= struct_def->fields.vec.size()) {
1062 return Error("too many unnamed fields in nested array");
1064 name = struct_def->fields.vec[fieldn]->name;
1067 if (Is(kTokenStringConstant)) {
1070 EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
1072 if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
1074 ECHECK(body(name, fieldn, struct_def));
1075 if (Is(terminator)) break;
1076 ECHECK(ParseComma());
1079 if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
1080 return Error("wrong number of unnamed fields in table vector");
1085 CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
1086 uoffset_t *ovalue) {
1087 size_t fieldn_outer = 0;
1088 auto err = ParseTableDelimiters(
1089 fieldn_outer, &struct_def,
1090 [&](const std::string &name, size_t &fieldn,
1091 const StructDef *struct_def_inner) -> CheckedError {
1092 if (name == "$schema") {
1093 ECHECK(Expect(kTokenStringConstant));
1096 auto field = struct_def_inner->fields.Lookup(name);
1098 if (!opts.skip_unexpected_fields_in_json) {
1099 return Error("unknown field: " + name);
1101 ECHECK(SkipAnyJsonValue());
1104 if (IsIdent("null") && !IsScalar(field->value.type.base_type)) {
1105 ECHECK(Next()); // Ignore this field.
1107 Value val = field->value;
1108 if (field->flexbuffer) {
1109 flexbuffers::Builder builder(1024,
1110 flexbuffers::BUILDER_FLAG_SHARE_ALL);
1111 ECHECK(ParseFlexBufferValue(&builder));
1113 // Force alignment for nested flexbuffer
1114 builder_.ForceVectorAlignment(builder.GetSize(), sizeof(uint8_t),
1115 sizeof(largest_scalar_t));
1116 auto off = builder_.CreateVector(builder.GetBuffer());
1117 val.constant = NumToString(off.o);
1118 } else if (field->nested_flatbuffer) {
1120 ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner));
1122 ECHECK(Recurse([&]() {
1123 return ParseAnyValue(val, field, fieldn, struct_def_inner, 0);
1126 // Hardcoded insertion-sort with error-check.
1127 // If fields are specified in order, then this loop exits
1129 auto elem = field_stack_.rbegin();
1130 for (; elem != field_stack_.rbegin() + fieldn; ++elem) {
1131 auto existing_field = elem->second;
1132 if (existing_field == field)
1133 return Error("field set more than once: " + field->name);
1134 if (existing_field->value.offset < field->value.offset) break;
1136 // Note: elem points to before the insertion point, thus .base()
1137 // points to the correct spot.
1138 field_stack_.insert(elem.base(), std::make_pair(val, field));
1146 // Check if all required fields are parsed.
1147 for (auto field_it = struct_def.fields.vec.begin();
1148 field_it != struct_def.fields.vec.end(); ++field_it) {
1149 auto required_field = *field_it;
1150 if (!required_field->required) { continue; }
1152 for (auto pf_it = field_stack_.end() - fieldn_outer;
1153 pf_it != field_stack_.end(); ++pf_it) {
1154 auto parsed_field = pf_it->second;
1155 if (parsed_field == required_field) {
1161 return Error("required field is missing: " + required_field->name +
1162 " in " + struct_def.name);
1166 if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size())
1167 return Error("struct: wrong number of initializers: " + struct_def.name);
1169 auto start = struct_def.fixed ? builder_.StartStruct(struct_def.minalign)
1170 : builder_.StartTable();
1172 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size;
1174 // Go through elements in reverse, since we're building the data backwards.
1175 for (auto it = field_stack_.rbegin();
1176 it != field_stack_.rbegin() + fieldn_outer; ++it) {
1177 auto &field_value = it->first;
1178 auto field = it->second;
1179 if (!struct_def.sortbysize ||
1180 size == SizeOf(field_value.type.base_type)) {
1181 switch (field_value.type.base_type) {
1183 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1184 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1185 case BASE_TYPE_ ## ENUM: \
1186 builder_.Pad(field->padding); \
1187 if (struct_def.fixed) { \
1189 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1190 builder_.PushElement(val); \
1192 CTYPE val, valdef; \
1193 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1194 ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
1195 builder_.AddElement(field_value.offset, val, valdef); \
1198 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1199 #undef FLATBUFFERS_TD
1200 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1201 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1202 case BASE_TYPE_ ## ENUM: \
1203 builder_.Pad(field->padding); \
1204 if (IsStruct(field->value.type)) { \
1205 SerializeStruct(*field->value.type.struct_def, field_value); \
1208 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1209 builder_.AddOffset(field_value.offset, val); \
1212 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
1213 #undef FLATBUFFERS_TD
1214 case BASE_TYPE_ARRAY:
1215 builder_.Pad(field->padding);
1217 reinterpret_cast<const uint8_t*>(field_value.constant.c_str()),
1218 InlineSize(field_value.type));
1225 for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back();
1227 if (struct_def.fixed) {
1228 builder_.ClearOffsets();
1229 builder_.EndStruct();
1230 FLATBUFFERS_ASSERT(value);
1231 // Temporarily store this struct in the value string, since it is to
1232 // be serialized in-place elsewhere.
1234 reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
1235 struct_def.bytesize);
1236 builder_.PopBytes(struct_def.bytesize);
1237 FLATBUFFERS_ASSERT(!ovalue);
1239 auto val = builder_.EndTable(start);
1240 if (ovalue) *ovalue = val;
1241 if (value) *value = NumToString(val);
1246 template <typename F>
1247 CheckedError Parser::ParseVectorDelimiters(uoffset_t &count, F body) {
1250 if ((!opts.strict_json || !count) && Is(']')) break;
1251 ECHECK(body(count));
1254 ECHECK(ParseComma());
1260 CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
1261 FieldDef *field, size_t fieldn) {
1262 uoffset_t count = 0;
1263 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1266 ECHECK(Recurse([&]() {
1267 return ParseAnyValue(val, field, fieldn, nullptr, count, true);
1269 field_stack_.push_back(std::make_pair(val, nullptr));
1274 builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
1275 InlineAlignment(type));
1276 for (uoffset_t i = 0; i < count; i++) {
1277 // start at the back, since we're building the data backwards.
1278 auto &val = field_stack_.back().first;
1279 switch (val.type.base_type) {
1281 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1282 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1283 case BASE_TYPE_ ## ENUM: \
1284 if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
1287 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1288 builder_.PushElement(elem); \
1291 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1292 #undef FLATBUFFERS_TD
1295 field_stack_.pop_back();
1298 builder_.ClearOffsets();
1299 *ovalue = builder_.EndVector(count);
1303 CheckedError Parser::ParseArray(Value &array) {
1304 std::vector<Value> stack;
1305 FlatBufferBuilder builder;
1306 const auto &type = array.type.VectorType();
1307 auto length = array.type.fixed_length;
1308 uoffset_t count = 0;
1309 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1310 vector_emplace_back(&stack, Value());
1311 auto &val = stack.back();
1313 if (IsStruct(type)) {
1314 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1316 ECHECK(ParseSingleValue(nullptr, val, false));
1321 if (length != count) return Error("Fixed-length array size is incorrect.");
1323 for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
1326 switch (val.type.base_type) {
1327 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1328 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1329 case BASE_TYPE_ ## ENUM: \
1330 if (IsStruct(val.type)) { \
1331 SerializeStruct(builder, *val.type.struct_def, val); \
1334 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1335 builder.PushElement(elem); \
1338 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1339 #undef FLATBUFFERS_TD
1340 default: FLATBUFFERS_ASSERT(0);
1345 array.constant.assign(
1346 reinterpret_cast<const char *>(builder.GetCurrentBufferPointer()),
1347 InlineSize(array.type));
1351 CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
1353 const StructDef *parent_struct_def) {
1354 if (token_ == '[') { // backwards compat for 'legacy' ubyte buffers
1355 ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
1357 auto cursor_at_value_begin = cursor_;
1358 ECHECK(SkipAnyJsonValue());
1359 std::string substring(cursor_at_value_begin - 1, cursor_ - 1);
1361 // Create and initialize new parser
1362 Parser nested_parser;
1363 FLATBUFFERS_ASSERT(field->nested_flatbuffer);
1364 nested_parser.root_struct_def_ = field->nested_flatbuffer;
1365 nested_parser.enums_ = enums_;
1366 nested_parser.opts = opts;
1367 nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
1369 // Parse JSON substring into new flatbuffer builder using nested_parser
1370 bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
1372 // Clean nested_parser to avoid deleting the elements in
1373 // the SymbolTables on destruction
1374 nested_parser.enums_.dict.clear();
1375 nested_parser.enums_.vec.clear();
1378 ECHECK(Error(nested_parser.error_));
1380 // Force alignment for nested flatbuffer
1381 builder_.ForceVectorAlignment(nested_parser.builder_.GetSize(), sizeof(uint8_t),
1382 nested_parser.builder_.GetBufferMinAlignment());
1384 auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
1385 nested_parser.builder_.GetSize());
1386 val.constant = NumToString(off.o);
1391 CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) {
1395 auto name = attribute_;
1396 if (false == (Is(kTokenIdentifier) || Is(kTokenStringConstant)))
1397 return Error("attribute name must be either identifier or string: " +
1399 if (known_attributes_.find(name) == known_attributes_.end())
1400 return Error("user define attributes must be declared before use: " +
1403 auto e = new Value();
1404 attributes->Add(name, e);
1407 ECHECK(ParseSingleValue(&name, *e, true));
1419 CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
1420 bool check, Value &e, BaseType req,
1422 bool match = dtoken == token_;
1424 FLATBUFFERS_ASSERT(*destmatch == false);
1426 e.constant = attribute_;
1427 // Check token match
1429 if (e.type.base_type == BASE_TYPE_NONE) {
1430 e.type.base_type = req;
1433 std::string("type mismatch: expecting: ") +
1434 kTypeNames[e.type.base_type] + ", found: " + kTypeNames[req] +
1435 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1438 // The exponent suffix of hexadecimal float-point number is mandatory.
1439 // A hex-integer constant is forbidden as an initializer of float number.
1440 if ((kTokenFloatConstant != dtoken) && IsFloat(e.type.base_type)) {
1441 const auto &s = e.constant;
1442 const auto k = s.find_first_of("0123456789.");
1443 if ((std::string::npos != k) && (s.length() > (k + 1)) &&
1444 (s[k] == '0' && is_alpha_char(s[k + 1], 'X')) &&
1445 (std::string::npos == s.find_first_of("pP", k + 2))) {
1447 "invalid number, the exponent suffix of hexadecimal "
1448 "floating-point literals is mandatory: \"" +
1458 CheckedError Parser::ParseEnumFromString(const Type &type,
1459 std::string *result) {
1460 const auto base_type =
1461 type.enum_def ? type.enum_def->underlying_type.base_type : type.base_type;
1462 if (!IsInteger(base_type)) return Error("not a valid value for this field");
1464 for (size_t pos = 0; pos != std::string::npos;) {
1465 const auto delim = attribute_.find_first_of(' ', pos);
1466 const auto last = (std::string::npos == delim);
1467 auto word = attribute_.substr(pos, !last ? delim - pos : std::string::npos);
1468 pos = !last ? delim + 1 : std::string::npos;
1469 const EnumVal *ev = nullptr;
1470 if (type.enum_def) {
1471 ev = type.enum_def->Lookup(word);
1473 auto dot = word.find_first_of('.');
1474 if (std::string::npos == dot)
1475 return Error("enum values need to be qualified by an enum type");
1476 auto enum_def_str = word.substr(0, dot);
1477 const auto enum_def = LookupEnum(enum_def_str);
1478 if (!enum_def) return Error("unknown enum: " + enum_def_str);
1479 auto enum_val_str = word.substr(dot + 1);
1480 ev = enum_def->Lookup(enum_val_str);
1482 if (!ev) return Error("unknown enum value: " + word);
1483 u64 |= ev->GetAsUInt64();
1485 *result = IsUnsigned(base_type) ? NumToString(u64)
1486 : NumToString(static_cast<int64_t>(u64));
1490 CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
1491 FLATBUFFERS_ASSERT(field);
1492 Value *hash_name = field->attributes.Lookup("hash");
1493 switch (e.type.base_type) {
1494 case BASE_TYPE_SHORT: {
1495 auto hash = FindHashFunction16(hash_name->constant.c_str());
1496 int16_t hashed_value = static_cast<int16_t>(hash(attribute_.c_str()));
1497 e.constant = NumToString(hashed_value);
1500 case BASE_TYPE_USHORT: {
1501 auto hash = FindHashFunction16(hash_name->constant.c_str());
1502 uint16_t hashed_value = hash(attribute_.c_str());
1503 e.constant = NumToString(hashed_value);
1506 case BASE_TYPE_INT: {
1507 auto hash = FindHashFunction32(hash_name->constant.c_str());
1508 int32_t hashed_value = static_cast<int32_t>(hash(attribute_.c_str()));
1509 e.constant = NumToString(hashed_value);
1512 case BASE_TYPE_UINT: {
1513 auto hash = FindHashFunction32(hash_name->constant.c_str());
1514 uint32_t hashed_value = hash(attribute_.c_str());
1515 e.constant = NumToString(hashed_value);
1518 case BASE_TYPE_LONG: {
1519 auto hash = FindHashFunction64(hash_name->constant.c_str());
1520 int64_t hashed_value = static_cast<int64_t>(hash(attribute_.c_str()));
1521 e.constant = NumToString(hashed_value);
1524 case BASE_TYPE_ULONG: {
1525 auto hash = FindHashFunction64(hash_name->constant.c_str());
1526 uint64_t hashed_value = hash(attribute_.c_str());
1527 e.constant = NumToString(hashed_value);
1530 default: FLATBUFFERS_ASSERT(0);
1536 CheckedError Parser::TokenError() {
1537 return Error("cannot parse value starting with: " + TokenToStringId(token_));
1540 // Re-pack helper (ParseSingleValue) to normalize defaults of scalars.
1541 template<typename T> inline void SingleValueRepack(Value &e, T val) {
1542 // Remove leading zeros.
1543 if (IsInteger(e.type.base_type)) { e.constant = NumToString(val); }
1545 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
1546 // Normilaze defaults NaN to unsigned quiet-NaN(0).
1547 static inline void SingleValueRepack(Value& e, float val) {
1548 if (val != val) e.constant = "nan";
1550 static inline void SingleValueRepack(Value& e, double val) {
1551 if (val != val) e.constant = "nan";
1555 CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
1557 // First see if this could be a conversion function:
1558 if (token_ == kTokenIdentifier && *cursor_ == '(') {
1559 // todo: Extract processing of conversion functions to ParseFunction.
1560 const auto functionname = attribute_;
1561 if (!IsFloat(e.type.base_type)) {
1562 return Error(functionname + ": type of argument mismatch, expecting: " +
1563 kTypeNames[BASE_TYPE_DOUBLE] +
1564 ", found: " + kTypeNames[e.type.base_type] +
1565 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1569 ECHECK(Recurse([&]() { return ParseSingleValue(name, e, false); }));
1571 // calculate with double precision
1573 ECHECK(atot(e.constant.c_str(), *this, &x));
1574 auto func_match = false;
1576 #define FLATBUFFERS_FN_DOUBLE(name, op) \
1577 if (!func_match && functionname == name) { y = op; func_match = true; }
1578 FLATBUFFERS_FN_DOUBLE("deg", x / kPi * 180);
1579 FLATBUFFERS_FN_DOUBLE("rad", x * kPi / 180);
1580 FLATBUFFERS_FN_DOUBLE("sin", sin(x));
1581 FLATBUFFERS_FN_DOUBLE("cos", cos(x));
1582 FLATBUFFERS_FN_DOUBLE("tan", tan(x));
1583 FLATBUFFERS_FN_DOUBLE("asin", asin(x));
1584 FLATBUFFERS_FN_DOUBLE("acos", acos(x));
1585 FLATBUFFERS_FN_DOUBLE("atan", atan(x));
1586 // TODO(wvo): add more useful conversion functions here.
1587 #undef FLATBUFFERS_FN_DOUBLE
1589 if (true != func_match) {
1590 return Error(std::string("Unknown conversion function: ") + functionname +
1591 ", field name: " + (name ? *name : "") +
1592 ", value: " + e.constant);
1594 e.constant = NumToString(y);
1599 const auto in_type = e.type.base_type;
1601 #define IF_ECHECK_(force, dtoken, check, req) \
1602 if (!match && ((check) || IsConstTrue(force))) \
1603 ECHECK(TryTypedValue(name, dtoken, check, e, req, &match))
1604 #define TRY_ECHECK(dtoken, check, req) IF_ECHECK_(false, dtoken, check, req)
1605 #define FORCE_ECHECK(dtoken, check, req) IF_ECHECK_(true, dtoken, check, req)
1608 if (token_ == kTokenStringConstant || token_ == kTokenIdentifier) {
1609 const auto kTokenStringOrIdent = token_;
1610 // The string type is a most probable type, check it first.
1611 TRY_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1614 // avoid escaped and non-ascii in the string
1615 if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type) &&
1616 !attr_is_trivial_ascii_string_) {
1618 std::string("type mismatch or invalid value, an initializer of "
1619 "non-string field must be trivial ASCII string: type: ") +
1620 kTypeNames[in_type] + ", name: " + (name ? *name : "") +
1621 ", value: " + attribute_);
1624 // A boolean as true/false. Boolean as Integer check below.
1625 if (!match && IsBool(in_type)) {
1626 auto is_true = attribute_ == "true";
1627 if (is_true || attribute_ == "false") {
1628 attribute_ = is_true ? "1" : "0";
1629 // accepts both kTokenStringConstant and kTokenIdentifier
1630 TRY_ECHECK(kTokenStringOrIdent, IsBool(in_type), BASE_TYPE_BOOL);
1633 // Check if this could be a string/identifier enum value.
1634 // Enum can have only true integer base type.
1635 if (!match && IsInteger(in_type) && !IsBool(in_type) &&
1636 IsIdentifierStart(*attribute_.c_str())) {
1637 ECHECK(ParseEnumFromString(e.type, &e.constant));
1641 // Parse a float/integer number from the string.
1642 if (!match) check_now = true; // Re-pack if parsed from string literal.
1643 if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type)) {
1644 // remove trailing whitespaces from attribute_
1645 auto last = attribute_.find_last_not_of(' ');
1646 if (std::string::npos != last) // has non-whitespace
1647 attribute_.resize(last + 1);
1649 // Float numbers or nan, inf, pi, etc.
1650 TRY_ECHECK(kTokenStringOrIdent, IsFloat(in_type), BASE_TYPE_FLOAT);
1651 // An integer constant in string.
1652 TRY_ECHECK(kTokenStringOrIdent, IsInteger(in_type), BASE_TYPE_INT);
1653 // Unknown tokens will be interpreted as string type.
1654 // An attribute value may be a scalar or string constant.
1655 FORCE_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1658 // Try a float number.
1659 TRY_ECHECK(kTokenFloatConstant, IsFloat(in_type), BASE_TYPE_FLOAT);
1660 // Integer token can init any scalar (integer of float).
1661 FORCE_ECHECK(kTokenIntegerConstant, IsScalar(in_type), BASE_TYPE_INT);
1669 msg += "Cannot assign token starting with '" + TokenToStringId(token_) +
1670 "' to value of <" + std::string(kTypeNames[in_type]) + "> type.";
1673 const auto match_type = e.type.base_type; // may differ from in_type
1674 // The check_now flag must be true when parse a fbs-schema.
1675 // This flag forces to check default scalar values or metadata of field.
1676 // For JSON parser the flag should be false.
1677 // If it is set for JSON each value will be checked twice (see ParseTable).
1678 if (check_now && IsScalar(match_type)) {
1680 switch (match_type) {
1681 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1682 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1683 case BASE_TYPE_ ## ENUM: {\
1685 ECHECK(atot(e.constant.c_str(), *this, &val)); \
1686 SingleValueRepack(e, val); \
1688 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1689 #undef FLATBUFFERS_TD
1697 StructDef *Parser::LookupCreateStruct(const std::string &name,
1698 bool create_if_new, bool definition) {
1699 std::string qualified_name = current_namespace_->GetFullyQualifiedName(name);
1700 // See if it exists pre-declared by an unqualified use.
1701 auto struct_def = LookupStruct(name);
1702 if (struct_def && struct_def->predecl) {
1704 // Make sure it has the current namespace, and is registered under its
1706 struct_def->defined_namespace = current_namespace_;
1707 structs_.Move(name, qualified_name);
1711 // See if it exists pre-declared by an qualified use.
1712 struct_def = LookupStruct(qualified_name);
1713 if (struct_def && struct_def->predecl) {
1715 // Make sure it has the current namespace.
1716 struct_def->defined_namespace = current_namespace_;
1721 // Search thru parent namespaces.
1722 for (size_t components = current_namespace_->components.size();
1723 components && !struct_def; components--) {
1724 struct_def = LookupStruct(
1725 current_namespace_->GetFullyQualifiedName(name, components - 1));
1728 if (!struct_def && create_if_new) {
1729 struct_def = new StructDef();
1731 structs_.Add(qualified_name, struct_def);
1732 struct_def->name = name;
1733 struct_def->defined_namespace = current_namespace_;
1735 // Not a definition.
1736 // Rather than failing, we create a "pre declared" StructDef, due to
1737 // circular references, and check for errors at the end of parsing.
1738 // It is defined in the current namespace, as the best guess what the
1739 // final namespace will be.
1740 structs_.Add(name, struct_def);
1741 struct_def->name = name;
1742 struct_def->defined_namespace = current_namespace_;
1743 struct_def->original_location.reset(
1744 new std::string(file_being_parsed_ + ":" + NumToString(line_)));
1750 const EnumVal *EnumDef::MinValue() const {
1751 return vals.vec.empty() ? nullptr : vals.vec.front();
1753 const EnumVal *EnumDef::MaxValue() const {
1754 return vals.vec.empty() ? nullptr : vals.vec.back();
1757 template<typename T> static uint64_t EnumDistanceImpl(T e1, T e2) {
1758 if (e1 < e2) { std::swap(e1, e2); } // use std for scalars
1759 // Signed overflow may occur, use unsigned calculation.
1760 // The unsigned overflow is well-defined by C++ standard (modulo 2^n).
1761 return static_cast<uint64_t>(e1) - static_cast<uint64_t>(e2);
1764 uint64_t EnumDef::Distance(const EnumVal *v1, const EnumVal *v2) const {
1765 return IsUInt64() ? EnumDistanceImpl(v1->GetAsUInt64(), v2->GetAsUInt64())
1766 : EnumDistanceImpl(v1->GetAsInt64(), v2->GetAsInt64());
1769 std::string EnumDef::AllFlags() const {
1770 FLATBUFFERS_ASSERT(attributes.Lookup("bit_flags"));
1772 for (auto it = Vals().begin(); it != Vals().end(); ++it) {
1773 u64 |= (*it)->GetAsUInt64();
1775 return IsUInt64() ? NumToString(u64) : NumToString(static_cast<int64_t>(u64));
1778 EnumVal *EnumDef::ReverseLookup(int64_t enum_idx,
1779 bool skip_union_default) const {
1780 auto skip_first = static_cast<int>(is_union && skip_union_default);
1781 for (auto it = Vals().begin() + skip_first; it != Vals().end(); ++it) {
1782 if ((*it)->GetAsInt64() == enum_idx) { return *it; }
1787 EnumVal *EnumDef::FindByValue(const std::string &constant) const {
1791 uint64_t u64; // avoid reinterpret_cast of pointers
1792 done = StringToNumber(constant.c_str(), &u64);
1793 i64 = static_cast<int64_t>(u64);
1795 done = StringToNumber(constant.c_str(), &i64);
1797 FLATBUFFERS_ASSERT(done);
1798 if (!done) return nullptr;
1799 return ReverseLookup(i64, false);
1802 void EnumDef::SortByValue() {
1805 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1806 return e1->GetAsUInt64() < e2->GetAsUInt64();
1809 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1810 return e1->GetAsInt64() < e2->GetAsInt64();
1814 void EnumDef::RemoveDuplicates() {
1815 // This method depends form SymbolTable implementation!
1816 // 1) vals.vec - owner (raw pointer)
1817 // 2) vals.dict - access map
1818 auto first = vals.vec.begin();
1819 auto last = vals.vec.end();
1820 if (first == last) return;
1821 auto result = first;
1822 while (++first != last) {
1823 if ((*result)->value != (*first)->value) {
1824 *(++result) = *first;
1827 for (auto it = vals.dict.begin(); it != vals.dict.end(); ++it) {
1828 if (it->second == ev) it->second = *result; // reassign
1830 delete ev; // delete enum value
1834 vals.vec.erase(++result, last);
1837 template<typename T> void EnumDef::ChangeEnumValue(EnumVal *ev, T new_value) {
1838 ev->value = static_cast<int64_t>(new_value);
1841 namespace EnumHelper {
1842 template<BaseType E> struct EnumValType { typedef int64_t type; };
1843 template<> struct EnumValType<BASE_TYPE_ULONG> { typedef uint64_t type; };
1844 } // namespace EnumHelper
1846 struct EnumValBuilder {
1847 EnumVal *CreateEnumerator(const std::string &ev_name) {
1848 FLATBUFFERS_ASSERT(!temp);
1849 auto first = enum_def.vals.vec.empty();
1851 temp = new EnumVal(ev_name, first ? 0 : enum_def.vals.vec.back()->value);
1855 EnumVal *CreateEnumerator(const std::string &ev_name, int64_t val) {
1856 FLATBUFFERS_ASSERT(!temp);
1858 temp = new EnumVal(ev_name, val);
1862 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator(const std::string &name) {
1863 FLATBUFFERS_ASSERT(temp);
1864 ECHECK(ValidateValue(&temp->value, false == user_value));
1865 FLATBUFFERS_ASSERT((temp->union_type.enum_def == nullptr) ||
1866 (temp->union_type.enum_def == &enum_def));
1867 auto not_unique = enum_def.vals.Add(name, temp);
1869 if (not_unique) return parser.Error("enum value already exists: " + name);
1873 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator() {
1874 return AcceptEnumerator(temp->name);
1877 FLATBUFFERS_CHECKED_ERROR AssignEnumeratorValue(const std::string &value) {
1880 auto ascending = false;
1881 if (enum_def.IsUInt64()) {
1883 fit = StringToNumber(value.c_str(), &u64);
1884 ascending = u64 > temp->GetAsUInt64();
1885 temp->value = static_cast<int64_t>(u64); // well-defined since C++20.
1888 fit = StringToNumber(value.c_str(), &i64);
1889 ascending = i64 > temp->GetAsInt64();
1892 if (!fit) return parser.Error("enum value does not fit, \"" + value + "\"");
1893 if (!ascending && strict_ascending && !enum_def.vals.vec.empty())
1894 return parser.Error("enum values must be specified in ascending order");
1898 template<BaseType E, typename CTYPE>
1899 inline FLATBUFFERS_CHECKED_ERROR ValidateImpl(int64_t *ev, int m) {
1900 typedef typename EnumHelper::EnumValType<E>::type T; // int64_t or uint64_t
1901 static_assert(sizeof(T) == sizeof(int64_t), "invalid EnumValType");
1902 const auto v = static_cast<T>(*ev);
1903 auto up = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::max)());
1904 auto dn = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::lowest)());
1905 if (v < dn || v > (up - m)) {
1906 return parser.Error("enum value does not fit, \"" + NumToString(v) +
1907 (m ? " + 1\"" : "\"") + " out of " +
1908 TypeToIntervalString<CTYPE>());
1910 *ev = static_cast<int64_t>(v + m); // well-defined since C++20.
1914 FLATBUFFERS_CHECKED_ERROR ValidateValue(int64_t *ev, bool next) {
1916 switch (enum_def.underlying_type.base_type) {
1917 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
1918 PTYPE, RTYPE, KTYPE) \
1919 case BASE_TYPE_##ENUM: { \
1920 if (!IsInteger(BASE_TYPE_##ENUM)) break; \
1921 return ValidateImpl<BASE_TYPE_##ENUM, CTYPE>(ev, next ? 1 : 0); \
1923 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1924 #undef FLATBUFFERS_TD
1928 return parser.Error("fatal: invalid enum underlying type");
1931 EnumValBuilder(Parser &_parser, EnumDef &_enum_def, bool strict_order = true)
1933 enum_def(_enum_def),
1935 strict_ascending(strict_order),
1936 user_value(false) {}
1938 ~EnumValBuilder() { delete temp; }
1943 const bool strict_ascending;
1947 CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest) {
1948 std::vector<std::string> enum_comment = doc_comment_;
1950 std::string enum_name = attribute_;
1951 EXPECT(kTokenIdentifier);
1953 ECHECK(StartEnum(enum_name, is_union, &enum_def));
1954 enum_def->doc_comment = enum_comment;
1955 if (!is_union && !opts.proto_mode) {
1956 // Give specialized error message, since this type spec used to
1957 // be optional in the first FlatBuffers release.
1960 "must specify the underlying integer type for this"
1961 " enum (e.g. \': short\', which was the default).");
1965 // Specify the integer type underlying this enum.
1966 ECHECK(ParseType(enum_def->underlying_type));
1967 if (!IsInteger(enum_def->underlying_type.base_type) ||
1968 IsBool(enum_def->underlying_type.base_type))
1969 return Error("underlying enum type must be integral");
1970 // Make this type refer back to the enum it was derived from.
1971 enum_def->underlying_type.enum_def = enum_def;
1973 ECHECK(ParseMetaData(&enum_def->attributes));
1974 const auto underlying_type = enum_def->underlying_type.base_type;
1975 if (enum_def->attributes.Lookup("bit_flags") &&
1976 !IsUnsigned(underlying_type)) {
1977 // todo: Convert to the Error in the future?
1978 Warning("underlying type of bit_flags enum must be unsigned");
1980 // Protobuf allows them to be specified in any order, so sort afterwards.
1981 const auto strict_ascending = (false == opts.proto_mode);
1982 EnumValBuilder evb(*this, *enum_def, strict_ascending);
1984 // A lot of code generatos expect that an enum is not-empty.
1985 if ((is_union || Is('}')) && !opts.proto_mode) {
1986 evb.CreateEnumerator("NONE");
1987 ECHECK(evb.AcceptEnumerator());
1989 std::set<std::pair<BaseType, StructDef *>> union_types;
1991 if (opts.proto_mode && attribute_ == "option") {
1992 ECHECK(ParseProtoOption());
1994 auto &ev = *evb.CreateEnumerator(attribute_);
1995 auto full_name = ev.name;
1996 ev.doc_comment = doc_comment_;
1997 EXPECT(kTokenIdentifier);
1999 ECHECK(ParseNamespacing(&full_name, &ev.name));
2000 if (opts.union_value_namespacing) {
2001 // Since we can't namespace the actual enum identifiers, turn
2002 // namespace parts into part of the identifier.
2003 ev.name = full_name;
2004 std::replace(ev.name.begin(), ev.name.end(), '.', '_');
2008 ECHECK(ParseType(ev.union_type));
2009 if (ev.union_type.base_type != BASE_TYPE_STRUCT &&
2010 ev.union_type.base_type != BASE_TYPE_STRING)
2011 return Error("union value type may only be table/struct/string");
2013 ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name));
2015 if (!enum_def->uses_multiple_type_instances) {
2016 auto ins = union_types.insert(std::make_pair(
2017 ev.union_type.base_type, ev.union_type.struct_def));
2018 enum_def->uses_multiple_type_instances = (false == ins.second);
2024 ECHECK(evb.AssignEnumeratorValue(attribute_));
2025 EXPECT(kTokenIntegerConstant);
2026 } else if (false == strict_ascending) {
2027 // The opts.proto_mode flag is active.
2028 return Error("Protobuf mode doesn't allow implicit enum values.");
2031 ECHECK(evb.AcceptEnumerator());
2033 if (opts.proto_mode && Is('[')) {
2035 // ignore attributes on enums.
2036 while (token_ != ']') NEXT();
2040 if (!Is(opts.proto_mode ? ';' : ',')) break;
2045 // At this point, the enum can be empty if input is invalid proto-file.
2046 if (!enum_def->size())
2047 return Error("incomplete enum declaration, values not found");
2049 if (enum_def->attributes.Lookup("bit_flags")) {
2050 const auto base_width = static_cast<uint64_t>(8 * SizeOf(underlying_type));
2051 for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
2054 const auto u = ev->GetAsUInt64();
2055 // Stop manipulations with the sign.
2056 if (!IsUnsigned(underlying_type) && u == (base_width - 1))
2057 return Error("underlying type of bit_flags enum must be unsigned");
2058 if (u >= base_width)
2059 return Error("bit flag out of range of underlying integral type");
2060 enum_def->ChangeEnumValue(ev, 1ULL << u);
2064 if (false == strict_ascending)
2065 enum_def->SortByValue(); // Must be sorted to use MinValue/MaxValue.
2067 if (dest) *dest = enum_def;
2068 types_.Add(current_namespace_->GetFullyQualifiedName(enum_def->name),
2069 new Type(BASE_TYPE_UNION, nullptr, enum_def));
2073 CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) {
2074 auto &struct_def = *LookupCreateStruct(name, true, true);
2075 if (!struct_def.predecl) return Error("datatype already exists: " + name);
2076 struct_def.predecl = false;
2077 struct_def.name = name;
2078 struct_def.file = file_being_parsed_;
2079 // Move this struct to the back of the vector just in case it was predeclared,
2080 // to preserve declaration order.
2081 *std::remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) =
2083 *dest = &struct_def;
2087 CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
2088 StructDef *struct_def, const char *suffix,
2089 BaseType basetype) {
2090 auto len = strlen(suffix);
2091 for (auto it = fields.begin(); it != fields.end(); ++it) {
2092 auto &fname = (*it)->name;
2093 if (fname.length() > len &&
2094 fname.compare(fname.length() - len, len, suffix) == 0 &&
2095 (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
2097 struct_def->fields.Lookup(fname.substr(0, fname.length() - len));
2098 if (field && field->value.type.base_type == basetype)
2099 return Error("Field " + fname +
2100 " would clash with generated functions for field " +
2107 bool Parser::SupportsAdvancedUnionFeatures() const {
2108 return opts.lang_to_generate != 0 &&
2109 (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kJs |
2110 IDLOptions::kTs | IDLOptions::kPhp |
2111 IDLOptions::kJava | IDLOptions::kCSharp |
2112 IDLOptions::kKotlin |
2113 IDLOptions::kBinary)) == 0;
2116 bool Parser::SupportsAdvancedArrayFeatures() const {
2117 return (opts.lang_to_generate &
2118 ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
2119 IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
2120 IDLOptions::kBinary)) == 0;
2123 Namespace *Parser::UniqueNamespace(Namespace *ns) {
2124 for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
2125 if (ns->components == (*it)->components) {
2130 namespaces_.push_back(ns);
2134 std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
2135 Namespace *ns = new Namespace();
2137 std::size_t current, previous = 0;
2138 current = full_qualified_name.find('.');
2139 while (current != std::string::npos) {
2140 ns->components.push_back(
2141 full_qualified_name.substr(previous, current - previous));
2142 previous = current + 1;
2143 current = full_qualified_name.find('.', previous);
2145 current_namespace_ = UniqueNamespace(ns);
2146 return full_qualified_name.substr(previous, current - previous);
2149 static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
2150 auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
2151 auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
2155 CheckedError Parser::ParseDecl() {
2156 std::vector<std::string> dc = doc_comment_;
2157 bool fixed = IsIdent("struct");
2158 if (!fixed && !IsIdent("table")) return Error("declaration expected");
2160 std::string name = attribute_;
2161 EXPECT(kTokenIdentifier);
2162 StructDef *struct_def;
2163 ECHECK(StartStruct(name, &struct_def));
2164 struct_def->doc_comment = dc;
2165 struct_def->fixed = fixed;
2166 ECHECK(ParseMetaData(&struct_def->attributes));
2167 struct_def->sortbysize =
2168 struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
2170 while (token_ != '}') ECHECK(ParseField(*struct_def));
2171 auto force_align = struct_def->attributes.Lookup("force_align");
2174 auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
2175 if (force_align->type.base_type != BASE_TYPE_INT ||
2176 align < struct_def->minalign || align > FLATBUFFERS_MAX_ALIGNMENT ||
2177 align & (align - 1))
2179 "force_align must be a power of two integer ranging from the"
2180 "struct\'s natural alignment to " +
2181 NumToString(FLATBUFFERS_MAX_ALIGNMENT));
2182 struct_def->minalign = align;
2184 if (!struct_def->bytesize) return Error("size 0 structs not allowed");
2186 struct_def->PadLastField(struct_def->minalign);
2187 // Check if this is a table that has manual id assignments
2188 auto &fields = struct_def->fields.vec;
2189 if (!fixed && fields.size()) {
2190 size_t num_id_fields = 0;
2191 for (auto it = fields.begin(); it != fields.end(); ++it) {
2192 if ((*it)->attributes.Lookup("id")) num_id_fields++;
2194 // If any fields have ids..
2195 if (num_id_fields) {
2196 // Then all fields must have them.
2197 if (num_id_fields != fields.size())
2199 "either all fields or no fields must have an 'id' attribute");
2200 // Simply sort by id, then the fields are the same as if no ids had
2202 std::sort(fields.begin(), fields.end(), compareFieldDefs);
2203 // Verify we have a contiguous set, and reassign vtable offsets.
2204 for (int i = 0; i < static_cast<int>(fields.size()); i++) {
2205 if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
2206 return Error("field id\'s must be consecutive from 0, id " +
2207 NumToString(i) + " missing or set twice");
2208 fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
2214 CheckClash(fields, struct_def, UnionTypeFieldSuffix(), BASE_TYPE_UNION));
2215 ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
2216 ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
2217 ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
2218 ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
2219 ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
2221 types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name),
2222 new Type(BASE_TYPE_STRUCT, struct_def, nullptr));
2226 CheckedError Parser::ParseService() {
2227 std::vector<std::string> service_comment = doc_comment_;
2229 auto service_name = attribute_;
2230 EXPECT(kTokenIdentifier);
2231 auto &service_def = *new ServiceDef();
2232 service_def.name = service_name;
2233 service_def.file = file_being_parsed_;
2234 service_def.doc_comment = service_comment;
2235 service_def.defined_namespace = current_namespace_;
2236 if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
2238 return Error("service already exists: " + service_name);
2239 ECHECK(ParseMetaData(&service_def.attributes));
2242 std::vector<std::string> doc_comment = doc_comment_;
2243 auto rpc_name = attribute_;
2244 EXPECT(kTokenIdentifier);
2246 Type reqtype, resptype;
2247 ECHECK(ParseTypeIdent(reqtype));
2250 ECHECK(ParseTypeIdent(resptype));
2251 if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
2252 resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
2253 return Error("rpc request and response types must be tables");
2254 auto &rpc = *new RPCCall();
2255 rpc.name = rpc_name;
2256 rpc.request = reqtype.struct_def;
2257 rpc.response = resptype.struct_def;
2258 rpc.doc_comment = doc_comment;
2259 if (service_def.calls.Add(rpc_name, &rpc))
2260 return Error("rpc already exists: " + rpc_name);
2261 ECHECK(ParseMetaData(&rpc.attributes));
2263 } while (token_ != '}');
2268 bool Parser::SetRootType(const char *name) {
2269 root_struct_def_ = LookupStruct(name);
2270 if (!root_struct_def_)
2272 LookupStruct(current_namespace_->GetFullyQualifiedName(name));
2273 return root_struct_def_ != nullptr;
2276 void Parser::MarkGenerated() {
2277 // This function marks all existing definitions as having already
2278 // been generated, which signals no code for included files should be
2280 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2281 (*it)->generated = true;
2283 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
2284 if (!(*it)->predecl) { (*it)->generated = true; }
2286 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
2287 (*it)->generated = true;
2291 CheckedError Parser::ParseNamespace() {
2293 auto ns = new Namespace();
2294 namespaces_.push_back(ns); // Store it here to not leak upon error.
2295 if (token_ != ';') {
2297 ns->components.push_back(attribute_);
2298 EXPECT(kTokenIdentifier);
2299 if (Is('.')) NEXT() else break;
2302 namespaces_.pop_back();
2303 current_namespace_ = UniqueNamespace(ns);
2308 // Best effort parsing of .proto declarations, with the aim to turn them
2309 // in the closest corresponding FlatBuffer equivalent.
2310 // We parse everything as identifiers instead of keywords, since we don't
2311 // want protobuf keywords to become invalid identifiers in FlatBuffers.
2312 CheckedError Parser::ParseProtoDecl() {
2313 bool isextend = IsIdent("extend");
2314 if (IsIdent("package")) {
2315 // These are identical in syntax to FlatBuffer's namespace decl.
2316 ECHECK(ParseNamespace());
2317 } else if (IsIdent("message") || isextend) {
2318 std::vector<std::string> struct_comment = doc_comment_;
2320 StructDef *struct_def = nullptr;
2321 Namespace *parent_namespace = nullptr;
2323 if (Is('.')) NEXT(); // qualified names may start with a . ?
2324 auto id = attribute_;
2325 EXPECT(kTokenIdentifier);
2326 ECHECK(ParseNamespacing(&id, nullptr));
2327 struct_def = LookupCreateStruct(id, false);
2329 return Error("cannot extend unknown message type: " + id);
2331 std::string name = attribute_;
2332 EXPECT(kTokenIdentifier);
2333 ECHECK(StartStruct(name, &struct_def));
2334 // Since message definitions can be nested, we create a new namespace.
2335 auto ns = new Namespace();
2336 // Copy of current namespace.
2337 *ns = *current_namespace_;
2338 // But with current message name.
2339 ns->components.push_back(name);
2341 parent_namespace = current_namespace_;
2342 current_namespace_ = UniqueNamespace(ns);
2344 struct_def->doc_comment = struct_comment;
2345 ECHECK(ParseProtoFields(struct_def, isextend, false));
2346 if (!isextend) { current_namespace_ = parent_namespace; }
2347 if (Is(';')) NEXT();
2348 } else if (IsIdent("enum")) {
2349 // These are almost the same, just with different terminator:
2351 ECHECK(ParseEnum(false, &enum_def));
2352 if (Is(';')) NEXT();
2353 // Temp: remove any duplicates, as .fbs files can't handle them.
2354 enum_def->RemoveDuplicates();
2355 } else if (IsIdent("syntax")) { // Skip these.
2358 EXPECT(kTokenStringConstant);
2360 } else if (IsIdent("option")) { // Skip these.
2361 ECHECK(ParseProtoOption());
2363 } else if (IsIdent("service")) { // Skip these.
2365 EXPECT(kTokenIdentifier);
2366 ECHECK(ParseProtoCurliesOrIdent());
2368 return Error("don\'t know how to parse .proto declaration starting with " +
2369 TokenToStringId(token_));
2374 CheckedError Parser::StartEnum(const std::string &enum_name, bool is_union,
2376 auto &enum_def = *new EnumDef();
2377 enum_def.name = enum_name;
2378 enum_def.file = file_being_parsed_;
2379 enum_def.doc_comment = doc_comment_;
2380 enum_def.is_union = is_union;
2381 enum_def.defined_namespace = current_namespace_;
2382 if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name),
2384 return Error("enum already exists: " + enum_name);
2385 enum_def.underlying_type.base_type = is_union ? BASE_TYPE_UTYPE
2387 enum_def.underlying_type.enum_def = &enum_def;
2388 if (dest) *dest = &enum_def;
2392 CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
2393 bool inside_oneof) {
2395 while (token_ != '}') {
2396 if (IsIdent("message") || IsIdent("extend") || IsIdent("enum")) {
2397 // Nested declarations.
2398 ECHECK(ParseProtoDecl());
2399 } else if (IsIdent("extensions")) { // Skip these.
2401 EXPECT(kTokenIntegerConstant);
2402 if (Is(kTokenIdentifier)) {
2407 } else if (IsIdent("option")) { // Skip these.
2408 ECHECK(ParseProtoOption());
2410 } else if (IsIdent("reserved")) { // Skip these.
2412 while (!Is(';')) { NEXT(); } // A variety of formats, just skip.
2415 std::vector<std::string> field_comment = doc_comment_;
2416 // Parse the qualifier.
2417 bool required = false;
2418 bool repeated = false;
2420 if (!inside_oneof) {
2421 if (IsIdent("optional")) {
2422 // This is the default.
2424 } else if (IsIdent("required")) {
2427 } else if (IsIdent("repeated")) {
2430 } else if (IsIdent("oneof")) {
2434 // can't error, proto3 allows decls without any of the above.
2437 StructDef *anonymous_struct = nullptr;
2438 EnumDef *oneof_union = nullptr;
2440 if (IsIdent("group") || oneof) {
2442 if (oneof && opts.proto_oneof_union) {
2443 auto name = MakeCamel(attribute_, true) + "Union";
2444 ECHECK(StartEnum(name, true, &oneof_union));
2445 type = Type(BASE_TYPE_UNION, nullptr, oneof_union);
2447 auto name = "Anonymous" + NumToString(anonymous_counter++);
2448 ECHECK(StartStruct(name, &anonymous_struct));
2449 type = Type(BASE_TYPE_STRUCT, anonymous_struct);
2452 ECHECK(ParseTypeFromProtoType(&type));
2454 // Repeated elements get mapped to a vector.
2456 type.element = type.base_type;
2457 type.base_type = BASE_TYPE_VECTOR;
2458 if (type.element == BASE_TYPE_VECTOR) {
2459 // We have a vector or vectors, which FlatBuffers doesn't support.
2460 // For now make it a vector of string (since the source is likely
2461 // "repeated bytes").
2462 // TODO(wvo): A better solution would be to wrap this in a table.
2463 type.element = BASE_TYPE_STRING;
2466 std::string name = attribute_;
2467 EXPECT(kTokenIdentifier);
2469 // Parse the field id. Since we're just translating schemas, not
2470 // any kind of binary compatibility, we can safely ignore these, and
2473 EXPECT(kTokenIntegerConstant);
2475 FieldDef *field = nullptr;
2477 // We allow a field to be re-defined when extending.
2478 // TODO: are there situations where that is problematic?
2479 field = struct_def->fields.Lookup(name);
2481 if (!field) ECHECK(AddField(*struct_def, name, type, &field));
2482 field->doc_comment = field_comment;
2483 if (!IsScalar(type.base_type)) field->required = required;
2484 // See if there's a default specified.
2488 auto key = attribute_;
2489 ECHECK(ParseProtoKey());
2491 auto val = attribute_;
2492 ECHECK(ParseProtoCurliesOrIdent());
2493 if (key == "default") {
2494 // Temp: skip non-numeric defaults (enums).
2495 auto numeric = strpbrk(val.c_str(), "0123456789-+.");
2496 if (IsScalar(type.base_type) && numeric == val.c_str())
2497 field->value.constant = val;
2498 } else if (key == "deprecated") {
2499 field->deprecated = val == "true";
2501 if (!Is(',')) break;
2506 if (anonymous_struct) {
2507 ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
2508 if (Is(';')) NEXT();
2509 } else if (oneof_union) {
2510 // Parse into a temporary StructDef, then transfer fields into an
2511 // EnumDef describing the oneof as a union.
2512 StructDef oneof_struct;
2513 ECHECK(ParseProtoFields(&oneof_struct, false, oneof));
2514 if (Is(';')) NEXT();
2515 for (auto field_it = oneof_struct.fields.vec.begin();
2516 field_it != oneof_struct.fields.vec.end(); ++field_it) {
2517 const auto &oneof_field = **field_it;
2518 const auto &oneof_type = oneof_field.value.type;
2519 if (oneof_type.base_type != BASE_TYPE_STRUCT ||
2520 !oneof_type.struct_def || oneof_type.struct_def->fixed)
2521 return Error("oneof '" + name +
2522 "' cannot be mapped to a union because member '" +
2523 oneof_field.name + "' is not a table type.");
2524 EnumValBuilder evb(*this, *oneof_union);
2525 auto ev = evb.CreateEnumerator(oneof_type.struct_def->name);
2526 ev->union_type = oneof_type;
2527 ev->doc_comment = oneof_field.doc_comment;
2528 ECHECK(evb.AcceptEnumerator(oneof_field.name));
2539 CheckedError Parser::ParseProtoKey() {
2540 if (token_ == '(') {
2542 // Skip "(a.b)" style custom attributes.
2543 while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
2547 EXPECT(kTokenIdentifier);
2550 EXPECT(kTokenIdentifier);
2555 CheckedError Parser::ParseProtoCurliesOrIdent() {
2558 for (int nesting = 1; nesting;) {
2561 else if (token_ == '}')
2566 NEXT(); // Any single token.
2571 CheckedError Parser::ParseProtoOption() {
2573 ECHECK(ParseProtoKey());
2575 ECHECK(ParseProtoCurliesOrIdent());
2579 // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
2580 CheckedError Parser::ParseTypeFromProtoType(Type *type) {
2581 struct type_lookup {
2582 const char *proto_type;
2583 BaseType fb_type, element;
2585 static type_lookup lookup[] = {
2586 { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE },
2587 { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE },
2588 { "int32", BASE_TYPE_INT, BASE_TYPE_NONE },
2589 { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2590 { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2591 { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2592 { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE },
2593 { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2594 { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2595 { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2596 { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE },
2597 { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2598 { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE },
2599 { "string", BASE_TYPE_STRING, BASE_TYPE_NONE },
2600 { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR },
2601 { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE }
2603 for (auto tl = lookup; tl->proto_type; tl++) {
2604 if (attribute_ == tl->proto_type) {
2605 type->base_type = tl->fb_type;
2606 type->element = tl->element;
2611 if (Is('.')) NEXT(); // qualified names may start with a . ?
2612 ECHECK(ParseTypeIdent(*type));
2616 CheckedError Parser::SkipAnyJsonValue() {
2619 size_t fieldn_outer = 0;
2620 return ParseTableDelimiters(
2621 fieldn_outer, nullptr,
2622 [&](const std::string &, size_t &fieldn,
2623 const StructDef *) -> CheckedError {
2624 ECHECK(Recurse([&]() { return SkipAnyJsonValue(); }));
2630 uoffset_t count = 0;
2631 return ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2632 return Recurse([&]() { return SkipAnyJsonValue(); });
2635 case kTokenStringConstant:
2636 case kTokenIntegerConstant:
2637 case kTokenFloatConstant: NEXT(); break;
2639 if (IsIdent("true") || IsIdent("false") || IsIdent("null")) {
2642 return TokenError();
2647 CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
2650 auto start = builder->StartMap();
2651 size_t fieldn_outer = 0;
2653 ParseTableDelimiters(fieldn_outer, nullptr,
2654 [&](const std::string &name, size_t &fieldn,
2655 const StructDef *) -> CheckedError {
2657 ECHECK(ParseFlexBufferValue(builder));
2662 builder->EndMap(start);
2666 auto start = builder->StartVector();
2667 uoffset_t count = 0;
2668 ECHECK(ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2669 return ParseFlexBufferValue(builder);
2671 builder->EndVector(start, false, false);
2674 case kTokenStringConstant:
2675 builder->String(attribute_);
2676 EXPECT(kTokenStringConstant);
2678 case kTokenIntegerConstant:
2679 builder->Int(StringToInt(attribute_.c_str()));
2680 EXPECT(kTokenIntegerConstant);
2682 case kTokenFloatConstant:
2683 builder->Double(strtod(attribute_.c_str(), nullptr));
2684 EXPECT(kTokenFloatConstant);
2687 if (IsIdent("true")) {
2688 builder->Bool(true);
2690 } else if (IsIdent("false")) {
2691 builder->Bool(false);
2693 } else if (IsIdent("null")) {
2697 return TokenError();
2702 bool Parser::ParseFlexBuffer(const char *source, const char *source_filename,
2703 flexbuffers::Builder *builder) {
2704 auto ok = !StartParseFile(source, source_filename).Check() &&
2705 !ParseFlexBufferValue(builder).Check();
2706 if (ok) builder->Finish();
2710 bool Parser::Parse(const char *source, const char **include_paths,
2711 const char *source_filename) {
2712 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2715 if (opts.use_flexbuffers) {
2716 r = ParseFlexBuffer(source, source_filename, &flex_builder_);
2718 r = !ParseRoot(source, include_paths, source_filename).Check();
2720 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2724 CheckedError Parser::StartParseFile(const char *source,
2725 const char *source_filename) {
2726 file_being_parsed_ = source_filename ? source_filename : "";
2728 ResetState(source_);
2730 ECHECK(SkipByteOrderMark());
2732 if (Is(kTokenEof)) return Error("input file is empty");
2736 CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
2737 const char *source_filename) {
2738 ECHECK(DoParse(source, include_paths, source_filename, nullptr));
2740 // Check that all types were defined.
2741 for (auto it = structs_.vec.begin(); it != structs_.vec.end();) {
2742 auto &struct_def = **it;
2743 if (struct_def.predecl) {
2744 if (opts.proto_mode) {
2745 // Protos allow enums to be used before declaration, so check if that
2746 // is the case here.
2747 EnumDef *enum_def = nullptr;
2748 for (size_t components =
2749 struct_def.defined_namespace->components.size() + 1;
2750 components && !enum_def; components--) {
2751 auto qualified_name =
2752 struct_def.defined_namespace->GetFullyQualifiedName(
2753 struct_def.name, components - 1);
2754 enum_def = LookupEnum(qualified_name);
2757 // This is pretty slow, but a simple solution for now.
2758 auto initial_count = struct_def.refcount;
2759 for (auto struct_it = structs_.vec.begin();
2760 struct_it != structs_.vec.end(); ++struct_it) {
2761 auto &sd = **struct_it;
2762 for (auto field_it = sd.fields.vec.begin();
2763 field_it != sd.fields.vec.end(); ++field_it) {
2764 auto &field = **field_it;
2765 if (field.value.type.struct_def == &struct_def) {
2766 field.value.type.struct_def = nullptr;
2767 field.value.type.enum_def = enum_def;
2768 auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
2769 ? field.value.type.element
2770 : field.value.type.base_type;
2771 FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
2772 bt = enum_def->underlying_type.base_type;
2773 struct_def.refcount--;
2774 enum_def->refcount++;
2778 if (struct_def.refcount)
2779 return Error("internal: " + NumToString(struct_def.refcount) + "/" +
2780 NumToString(initial_count) +
2781 " use(s) of pre-declaration enum not accounted for: " +
2783 structs_.dict.erase(structs_.dict.find(struct_def.name));
2784 it = structs_.vec.erase(it);
2786 continue; // Skip error.
2789 auto err = "type referenced but not defined (check namespace): " +
2791 if (struct_def.original_location)
2792 err += ", originally at: " + *struct_def.original_location;
2798 // This check has to happen here and not earlier, because only now do we
2799 // know for sure what the type of these are.
2800 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2801 auto &enum_def = **it;
2802 if (enum_def.is_union) {
2803 for (auto val_it = enum_def.Vals().begin();
2804 val_it != enum_def.Vals().end(); ++val_it) {
2805 auto &val = **val_it;
2806 if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def &&
2807 val.union_type.struct_def->fixed)
2809 "only tables can be union elements in the generated language: " +
2817 CheckedError Parser::DoParse(const char *source, const char **include_paths,
2818 const char *source_filename,
2819 const char *include_filename) {
2820 if (source_filename) {
2821 if (included_files_.find(source_filename) == included_files_.end()) {
2822 included_files_[source_filename] =
2823 include_filename ? include_filename : "";
2824 files_included_per_file_[source_filename] = std::set<std::string>();
2829 if (!include_paths) {
2830 static const char *current_directory[] = { "", nullptr };
2831 include_paths = current_directory;
2833 field_stack_.clear();
2835 // Start with a blank namespace just in case this file doesn't have one.
2836 current_namespace_ = empty_namespace_;
2838 ECHECK(StartParseFile(source, source_filename));
2840 // Includes must come before type declarations:
2842 // Parse pre-include proto statements if any:
2843 if (opts.proto_mode && (attribute_ == "option" || attribute_ == "syntax" ||
2844 attribute_ == "package")) {
2845 ECHECK(ParseProtoDecl());
2846 } else if (IsIdent("native_include")) {
2848 vector_emplace_back(&native_included_files_, attribute_);
2849 EXPECT(kTokenStringConstant);
2851 } else if (IsIdent("include") || (opts.proto_mode && IsIdent("import"))) {
2853 if (opts.proto_mode && attribute_ == "public") NEXT();
2854 auto name = flatbuffers::PosixPath(attribute_.c_str());
2855 EXPECT(kTokenStringConstant);
2856 // Look for the file in include_paths.
2857 std::string filepath;
2858 for (auto paths = include_paths; paths && *paths; paths++) {
2859 filepath = flatbuffers::ConCatPathFileName(*paths, name);
2860 if (FileExists(filepath.c_str())) break;
2862 if (filepath.empty())
2863 return Error("unable to locate include file: " + name);
2864 if (source_filename)
2865 files_included_per_file_[source_filename].insert(filepath);
2866 if (included_files_.find(filepath) == included_files_.end()) {
2867 // We found an include file that we have not parsed yet.
2868 // Load it and parse it.
2869 std::string contents;
2870 if (!LoadFile(filepath.c_str(), true, &contents))
2871 return Error("unable to load include file: " + name);
2872 ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
2874 // We generally do not want to output code for any included files:
2875 if (!opts.generate_all) MarkGenerated();
2876 // Reset these just in case the included file had them, and the
2878 root_struct_def_ = nullptr;
2879 file_identifier_.clear();
2880 file_extension_.clear();
2881 // This is the easiest way to continue this file after an include:
2882 // instead of saving and restoring all the state, we simply start the
2883 // file anew. This will cause it to encounter the same include
2884 // statement again, but this time it will skip it, because it was
2885 // entered into included_files_.
2886 // This is recursive, but only go as deep as the number of include
2888 if (source_filename) {
2889 included_files_.erase(source_filename);
2891 return DoParse(source, include_paths, source_filename,
2899 // Now parse all other kinds of declarations:
2900 while (token_ != kTokenEof) {
2901 if (opts.proto_mode) {
2902 ECHECK(ParseProtoDecl());
2903 } else if (IsIdent("namespace")) {
2904 ECHECK(ParseNamespace());
2905 } else if (token_ == '{') {
2906 if (!root_struct_def_)
2907 return Error("no root type set to parse json with");
2908 if (builder_.GetSize()) {
2909 return Error("cannot have more than one json object in a file");
2912 ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
2913 if (opts.size_prefixed) {
2914 builder_.FinishSizePrefixed(Offset<Table>(toff), file_identifier_.length()
2915 ? file_identifier_.c_str()
2918 builder_.Finish(Offset<Table>(toff), file_identifier_.length()
2919 ? file_identifier_.c_str()
2922 // Check that JSON file doesn't contain more objects or IDL directives.
2923 // Comments after JSON are allowed.
2925 } else if (IsIdent("enum")) {
2926 ECHECK(ParseEnum(false, nullptr));
2927 } else if (IsIdent("union")) {
2928 ECHECK(ParseEnum(true, nullptr));
2929 } else if (IsIdent("root_type")) {
2931 auto root_type = attribute_;
2932 EXPECT(kTokenIdentifier);
2933 ECHECK(ParseNamespacing(&root_type, nullptr));
2934 if (opts.root_type.empty()) {
2935 if (!SetRootType(root_type.c_str()))
2936 return Error("unknown root type: " + root_type);
2937 if (root_struct_def_->fixed)
2938 return Error("root type must be a table");
2941 } else if (IsIdent("file_identifier")) {
2943 file_identifier_ = attribute_;
2944 EXPECT(kTokenStringConstant);
2945 if (file_identifier_.length() != FlatBufferBuilder::kFileIdentifierLength)
2946 return Error("file_identifier must be exactly " +
2947 NumToString(FlatBufferBuilder::kFileIdentifierLength) +
2950 } else if (IsIdent("file_extension")) {
2952 file_extension_ = attribute_;
2953 EXPECT(kTokenStringConstant);
2955 } else if (IsIdent("include")) {
2956 return Error("includes must come before declarations");
2957 } else if (IsIdent("attribute")) {
2959 auto name = attribute_;
2960 if (Is(kTokenIdentifier)) {
2963 EXPECT(kTokenStringConstant);
2966 known_attributes_[name] = false;
2967 } else if (IsIdent("rpc_service")) {
2968 ECHECK(ParseService());
2970 ECHECK(ParseDecl());
2976 std::set<std::string> Parser::GetIncludedFilesRecursive(
2977 const std::string &file_name) const {
2978 std::set<std::string> included_files;
2979 std::list<std::string> to_process;
2981 if (file_name.empty()) return included_files;
2982 to_process.push_back(file_name);
2984 while (!to_process.empty()) {
2985 std::string current = to_process.front();
2986 to_process.pop_front();
2987 included_files.insert(current);
2989 // Workaround the lack of const accessor in C++98 maps.
2991 (*const_cast<std::map<std::string, std::set<std::string>> *>(
2992 &files_included_per_file_))[current];
2993 for (auto it = new_files.begin(); it != new_files.end(); ++it) {
2994 if (included_files.find(*it) == included_files.end())
2995 to_process.push_back(*it);
2999 return included_files;
3002 // Schema serialization functionality:
3004 template<typename T> bool compareName(const T *a, const T *b) {
3005 return a->defined_namespace->GetFullyQualifiedName(a->name) <
3006 b->defined_namespace->GetFullyQualifiedName(b->name);
3009 template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
3010 // Pre-sort these vectors, such that we can set the correct indices for them.
3012 std::sort(vec.begin(), vec.end(), compareName<T>);
3013 for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
3016 void Parser::Serialize() {
3018 AssignIndices(structs_.vec);
3019 AssignIndices(enums_.vec);
3020 std::vector<Offset<reflection::Object>> object_offsets;
3021 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
3022 auto offset = (*it)->Serialize(&builder_, *this);
3023 object_offsets.push_back(offset);
3024 (*it)->serialized_location = offset.o;
3026 std::vector<Offset<reflection::Enum>> enum_offsets;
3027 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3028 auto offset = (*it)->Serialize(&builder_, *this);
3029 enum_offsets.push_back(offset);
3030 (*it)->serialized_location = offset.o;
3032 std::vector<Offset<reflection::Service>> service_offsets;
3033 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
3034 auto offset = (*it)->Serialize(&builder_, *this);
3035 service_offsets.push_back(offset);
3036 (*it)->serialized_location = offset.o;
3038 auto objs__ = builder_.CreateVectorOfSortedTables(&object_offsets);
3039 auto enum__ = builder_.CreateVectorOfSortedTables(&enum_offsets);
3040 auto fiid__ = builder_.CreateString(file_identifier_);
3041 auto fext__ = builder_.CreateString(file_extension_);
3042 auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
3043 auto schema_offset =
3044 reflection::CreateSchema(builder_, objs__, enum__, fiid__, fext__,
3045 (root_struct_def_ ? root_struct_def_->serialized_location : 0),
3047 if (opts.size_prefixed) {
3048 builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
3050 builder_.Finish(schema_offset, reflection::SchemaIdentifier());
3054 static Namespace *GetNamespace(
3055 const std::string &qualified_name, std::vector<Namespace *> &namespaces,
3056 std::map<std::string, Namespace *> &namespaces_index) {
3057 size_t dot = qualified_name.find_last_of('.');
3058 std::string namespace_name = (dot != std::string::npos)
3059 ? std::string(qualified_name.c_str(), dot)
3061 Namespace *&ns = namespaces_index[namespace_name];
3064 ns = new Namespace();
3065 namespaces.push_back(ns);
3070 dot = qualified_name.find('.', pos);
3071 if (dot == std::string::npos) { break; }
3072 ns->components.push_back(qualified_name.substr(pos, dot - pos));
3080 Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
3081 const Parser &parser) const {
3082 std::vector<Offset<reflection::Field>> field_offsets;
3083 for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
3084 field_offsets.push_back((*it)->Serialize(
3085 builder, static_cast<uint16_t>(it - fields.vec.begin()), parser));
3087 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3088 auto name__ = builder->CreateString(qualified_name);
3089 auto flds__ = builder->CreateVectorOfSortedTables(&field_offsets);
3090 auto attr__ = SerializeAttributes(builder, parser);
3091 auto docs__ = parser.opts.binary_schema_comments
3092 ? builder->CreateVectorOfStrings(doc_comment)
3094 return reflection::CreateObject(*builder, name__, flds__, fixed,
3095 static_cast<int>(minalign),
3096 static_cast<int>(bytesize),
3100 bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
3101 if (!DeserializeAttributes(parser, object->attributes()))
3103 DeserializeDoc(doc_comment, object->documentation());
3104 name = parser.UnqualifiedName(object->name()->str());
3106 sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
3107 const auto& of = *(object->fields());
3108 auto indexes = std::vector<uoffset_t>(of.size());
3109 for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
3110 size_t tmp_struct_size = 0;
3111 for (size_t i = 0; i < indexes.size(); i++) {
3112 auto field = of.Get(indexes[i]);
3113 auto field_def = new FieldDef();
3114 if (!field_def->Deserialize(parser, field) ||
3115 fields.Add(field_def->name, field_def)) {
3120 // Recompute padding since that's currently not serialized.
3121 auto size = InlineSize(field_def->value.type);
3123 i + 1 < indexes.size()
3124 ? of.Get(indexes[i+1])
3126 tmp_struct_size += size;
3127 field_def->padding =
3128 next_field ? (next_field->offset() - field_def->value.offset) - size
3129 : PaddingBytes(tmp_struct_size, minalign);
3130 tmp_struct_size += field_def->padding;
3133 FLATBUFFERS_ASSERT(static_cast<int>(tmp_struct_size) == object->bytesize());
3137 Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
3139 const Parser &parser) const {
3140 auto name__ = builder->CreateString(name);
3141 auto type__ = value.type.Serialize(builder);
3142 auto attr__ = SerializeAttributes(builder, parser);
3143 auto docs__ = parser.opts.binary_schema_comments
3144 ? builder->CreateVectorOfStrings(doc_comment)
3146 return reflection::CreateField(*builder, name__, type__, id, value.offset,
3147 // Is uint64>max(int64) tested?
3148 IsInteger(value.type.base_type) ? StringToInt(value.constant.c_str()) : 0,
3149 // result may be platform-dependent if underlying is float (not double)
3150 IsFloat(value.type.base_type) ? strtod(value.constant.c_str(), nullptr)
3152 deprecated, required, key, attr__, docs__);
3153 // TODO: value.constant is almost always "0", we could save quite a bit of
3154 // space by sharing it. Same for common values of value.type.
3157 bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
3158 name = field->name()->str();
3159 defined_namespace = parser.current_namespace_;
3160 if (!value.type.Deserialize(parser, field->type()))
3162 value.offset = field->offset();
3163 if (IsInteger(value.type.base_type)) {
3164 value.constant = NumToString(field->default_integer());
3165 } else if (IsFloat(value.type.base_type)) {
3166 value.constant = FloatToString(field->default_real(), 16);
3167 size_t last_zero = value.constant.find_last_not_of('0');
3168 if (last_zero != std::string::npos && last_zero != 0) {
3169 value.constant.erase(last_zero, std::string::npos);
3172 deprecated = field->deprecated();
3173 required = field->required();
3175 if (!DeserializeAttributes(parser, field->attributes()))
3177 // TODO: this should probably be handled by a separate attribute
3178 if (attributes.Lookup("flexbuffer")) {
3180 parser.uses_flexbuffers_ = true;
3181 if (value.type.base_type != BASE_TYPE_VECTOR ||
3182 value.type.element != BASE_TYPE_UCHAR)
3185 if (auto nested = attributes.Lookup("nested_flatbuffer")) {
3186 auto nested_qualified_name =
3187 parser.current_namespace_->GetFullyQualifiedName(nested->constant);
3188 nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
3189 if (!nested_flatbuffer) return false;
3191 DeserializeDoc(doc_comment, field->documentation());
3195 Offset<reflection::RPCCall> RPCCall::Serialize(FlatBufferBuilder *builder,
3196 const Parser &parser) const {
3197 auto name__ = builder->CreateString(name);
3198 auto attr__ = SerializeAttributes(builder, parser);
3199 auto docs__ = parser.opts.binary_schema_comments
3200 ? builder->CreateVectorOfStrings(doc_comment)
3202 return reflection::CreateRPCCall(*builder, name__,
3203 request->serialized_location,
3204 response->serialized_location,
3208 bool RPCCall::Deserialize(Parser &parser, const reflection::RPCCall *call) {
3209 name = call->name()->str();
3210 if (!DeserializeAttributes(parser, call->attributes()))
3212 DeserializeDoc(doc_comment, call->documentation());
3213 request = parser.structs_.Lookup(call->request()->name()->str());
3214 response = parser.structs_.Lookup(call->response()->name()->str());
3215 if (!request || !response) { return false; }
3219 Offset<reflection::Service> ServiceDef::Serialize(FlatBufferBuilder *builder,
3220 const Parser &parser) const {
3221 std::vector<Offset<reflection::RPCCall>> servicecall_offsets;
3222 for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) {
3223 servicecall_offsets.push_back((*it)->Serialize(builder, parser));
3225 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3226 auto name__ = builder->CreateString(qualified_name);
3227 auto call__ = builder->CreateVector(servicecall_offsets);
3228 auto attr__ = SerializeAttributes(builder, parser);
3229 auto docs__ = parser.opts.binary_schema_comments
3230 ? builder->CreateVectorOfStrings(doc_comment)
3232 return reflection::CreateService(*builder, name__, call__, attr__, docs__);
3235 bool ServiceDef::Deserialize(Parser &parser,
3236 const reflection::Service *service) {
3237 name = parser.UnqualifiedName(service->name()->str());
3238 if (service->calls()) {
3239 for (uoffset_t i = 0; i < service->calls()->size(); ++i) {
3240 auto call = new RPCCall();
3241 if (!call->Deserialize(parser, service->calls()->Get(i)) ||
3242 calls.Add(call->name, call)) {
3248 if (!DeserializeAttributes(parser, service->attributes()))
3250 DeserializeDoc(doc_comment, service->documentation());
3254 Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
3255 const Parser &parser) const {
3256 std::vector<Offset<reflection::EnumVal>> enumval_offsets;
3257 for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
3258 enumval_offsets.push_back((*it)->Serialize(builder, parser));
3260 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3261 auto name__ = builder->CreateString(qualified_name);
3262 auto vals__ = builder->CreateVector(enumval_offsets);
3263 auto type__ = underlying_type.Serialize(builder);
3264 auto attr__ = SerializeAttributes(builder, parser);
3265 auto docs__ = parser.opts.binary_schema_comments
3266 ? builder->CreateVectorOfStrings(doc_comment)
3268 return reflection::CreateEnum(*builder, name__, vals__, is_union, type__,
3272 bool EnumDef::Deserialize(Parser &parser, const reflection::Enum *_enum) {
3273 name = parser.UnqualifiedName(_enum->name()->str());
3274 for (uoffset_t i = 0; i < _enum->values()->size(); ++i) {
3275 auto val = new EnumVal();
3276 if (!val->Deserialize(parser, _enum->values()->Get(i)) ||
3277 vals.Add(val->name, val)) {
3282 is_union = _enum->is_union();
3283 if (!underlying_type.Deserialize(parser, _enum->underlying_type())) {
3286 if (!DeserializeAttributes(parser, _enum->attributes()))
3288 DeserializeDoc(doc_comment, _enum->documentation());
3292 Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder,
3293 const Parser &parser) const {
3294 auto name__ = builder->CreateString(name);
3295 auto type__ = union_type.Serialize(builder);
3296 auto docs__ = parser.opts.binary_schema_comments
3297 ? builder->CreateVectorOfStrings(doc_comment)
3299 return reflection::CreateEnumVal(*builder, name__, value,
3300 union_type.struct_def ? union_type.struct_def->serialized_location : 0,
3304 bool EnumVal::Deserialize(const Parser &parser,
3305 const reflection::EnumVal *val) {
3306 name = val->name()->str();
3307 value = val->value();
3308 if (!union_type.Deserialize(parser, val->union_type()))
3310 DeserializeDoc(doc_comment, val->documentation());
3314 Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
3315 return reflection::CreateType(
3316 *builder, static_cast<reflection::BaseType>(base_type),
3317 static_cast<reflection::BaseType>(element),
3318 struct_def ? struct_def->index : (enum_def ? enum_def->index : -1),
3322 bool Type::Deserialize(const Parser &parser, const reflection::Type *type) {
3323 if (type == nullptr) return true;
3324 base_type = static_cast<BaseType>(type->base_type());
3325 element = static_cast<BaseType>(type->element());
3326 fixed_length = type->fixed_length();
3327 if (type->index() >= 0) {
3328 bool is_series = type->base_type() == reflection::Vector ||
3329 type->base_type() == reflection::Array;
3330 if (type->base_type() == reflection::Obj ||
3332 type->element() == reflection::Obj)) {
3333 if (static_cast<size_t>(type->index()) < parser.structs_.vec.size()) {
3334 struct_def = parser.structs_.vec[type->index()];
3335 struct_def->refcount++;
3340 if (static_cast<size_t>(type->index()) < parser.enums_.vec.size()) {
3341 enum_def = parser.enums_.vec[type->index()];
3350 flatbuffers::Offset<
3351 flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
3352 Definition::SerializeAttributes(FlatBufferBuilder *builder,
3353 const Parser &parser) const {
3354 std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
3355 for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
3356 auto it = parser.known_attributes_.find(kv->first);
3357 FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
3358 if (parser.opts.binary_schema_builtins || !it->second) {
3359 auto key = builder->CreateString(kv->first);
3360 auto val = builder->CreateString(kv->second->constant);
3361 attrs.push_back(reflection::CreateKeyValue(*builder, key, val));
3365 return builder->CreateVectorOfSortedTables(&attrs);
3371 bool Definition::DeserializeAttributes(
3372 Parser &parser, const Vector<Offset<reflection::KeyValue>> *attrs) {
3373 if (attrs == nullptr)
3375 for (uoffset_t i = 0; i < attrs->size(); ++i) {
3376 auto kv = attrs->Get(i);
3377 auto value = new Value();
3378 if (kv->value()) { value->constant = kv->value()->str(); }
3379 if (attributes.Add(kv->key()->str(), value)) {
3383 parser.known_attributes_[kv->key()->str()];
3388 /************************************************************************/
3389 /* DESERIALIZATION */
3390 /************************************************************************/
3391 bool Parser::Deserialize(const uint8_t *buf, const size_t size) {
3392 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t *>(buf), size);
3393 bool size_prefixed = false;
3394 if(!reflection::SchemaBufferHasIdentifier(buf)) {
3395 if (!flatbuffers::BufferHasIdentifier(buf, reflection::SchemaIdentifier(),
3399 size_prefixed = true;
3401 auto verify_fn = size_prefixed ? &reflection::VerifySizePrefixedSchemaBuffer
3402 : &reflection::VerifySchemaBuffer;
3403 if (!verify_fn(verifier)) {
3406 auto schema = size_prefixed ? reflection::GetSizePrefixedSchema(buf)
3407 : reflection::GetSchema(buf);
3408 return Deserialize(schema);
3411 bool Parser::Deserialize(const reflection::Schema *schema) {
3412 file_identifier_ = schema->file_ident() ? schema->file_ident()->str() : "";
3413 file_extension_ = schema->file_ext() ? schema->file_ext()->str() : "";
3414 std::map<std::string, Namespace *> namespaces_index;
3416 // Create defs without deserializing so references from fields to structs and
3417 // enums can be resolved.
3418 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3420 auto struct_def = new StructDef();
3421 struct_def->bytesize = it->bytesize();
3422 struct_def->fixed = it->is_struct();
3423 struct_def->minalign = it->minalign();
3424 if (structs_.Add(it->name()->str(), struct_def)) {
3428 auto type = new Type(BASE_TYPE_STRUCT, struct_def, nullptr);
3429 if (types_.Add(it->name()->str(), type)) {
3434 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3435 auto enum_def = new EnumDef();
3436 if (enums_.Add(it->name()->str(), enum_def)) {
3440 auto type = new Type(BASE_TYPE_UNION, nullptr, enum_def);
3441 if (types_.Add(it->name()->str(), type)) {
3447 // Now fields can refer to structs and enums by index.
3448 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3450 std::string qualified_name = it->name()->str();
3451 auto struct_def = structs_.Lookup(qualified_name);
3452 struct_def->defined_namespace =
3453 GetNamespace(qualified_name, namespaces_, namespaces_index);
3454 if (!struct_def->Deserialize(*this, * it)) { return false; }
3455 if (schema->root_table() == *it) { root_struct_def_ = struct_def; }
3457 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3458 std::string qualified_name = it->name()->str();
3459 auto enum_def = enums_.Lookup(qualified_name);
3460 enum_def->defined_namespace =
3461 GetNamespace(qualified_name, namespaces_, namespaces_index);
3462 if (!enum_def->Deserialize(*this, *it)) { return false; }
3465 if (schema->services()) {
3466 for (auto it = schema->services()->begin(); it != schema->services()->end();
3468 std::string qualified_name = it->name()->str();
3469 auto service_def = new ServiceDef();
3470 service_def->defined_namespace =
3471 GetNamespace(qualified_name, namespaces_, namespaces_index);
3472 if (!service_def->Deserialize(*this, *it) ||
3473 services_.Add(qualified_name, service_def)) {
3483 std::string Parser::ConformTo(const Parser &base) {
3484 for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
3485 auto &struct_def = **sit;
3486 auto qualified_name =
3487 struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
3488 auto struct_def_base = base.LookupStruct(qualified_name);
3489 if (!struct_def_base) continue;
3490 for (auto fit = struct_def.fields.vec.begin();
3491 fit != struct_def.fields.vec.end(); ++fit) {
3492 auto &field = **fit;
3493 auto field_base = struct_def_base->fields.Lookup(field.name);
3495 if (field.value.offset != field_base->value.offset)
3496 return "offsets differ for field: " + field.name;
3497 if (field.value.constant != field_base->value.constant)
3498 return "defaults differ for field: " + field.name;
3499 if (!EqualByName(field.value.type, field_base->value.type))
3500 return "types differ for field: " + field.name;
3502 // Doesn't have to exist, deleting fields is fine.
3503 // But we should check if there is a field that has the same offset
3504 // but is incompatible (in the case of field renaming).
3505 for (auto fbit = struct_def_base->fields.vec.begin();
3506 fbit != struct_def_base->fields.vec.end(); ++fbit) {
3508 if (field.value.offset == field_base->value.offset) {
3509 if (!EqualByName(field.value.type, field_base->value.type))
3510 return "field renamed to different type: " + field.name;
3517 for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
3518 auto &enum_def = **eit;
3519 auto qualified_name =
3520 enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
3521 auto enum_def_base = base.enums_.Lookup(qualified_name);
3522 if (!enum_def_base) continue;
3523 for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
3525 auto &enum_val = **evit;
3526 auto enum_val_base = enum_def_base->Lookup(enum_val.name);
3527 if (enum_val_base) {
3528 if (enum_val != *enum_val_base)
3529 return "values differ for enum: " + enum_val.name;
3536 } // namespace flatbuffers