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.
23 #include "flatbuffers/idl.h"
24 #include "flatbuffers/util.h"
26 namespace flatbuffers {
28 // Reflects the version at the compiling time of binary(lib/dll/so).
29 const char *FLATBUFFERS_VERSION() {
32 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
33 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
34 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
38 const double kPi = 3.14159265358979323846;
40 const char *const kTypeNames[] = {
42 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
43 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
45 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
51 const char kTypeSizes[] = {
53 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
54 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
56 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
61 // The enums in the reflection schema should match the ones we use internally.
62 // Compare the last element to check if these go out of sync.
63 static_assert(BASE_TYPE_UNION == static_cast<BaseType>(reflection::Union),
66 // Any parsing calls have to be wrapped in this macro, which automates
67 // handling of recursive error checking a bit. It will check the received
68 // CheckedError object, and return straight away on error.
69 #define ECHECK(call) \
72 if (ce.Check()) return ce; \
75 // These two functions are called hundreds of times below, so define a short
77 #define NEXT() ECHECK(Next())
78 #define EXPECT(tok) ECHECK(Expect(tok))
80 static bool ValidateUTF8(const std::string &str) {
81 const char *s = &str[0];
82 const char *const sEnd = s + str.length();
84 if (FromUTF8(&s) < 0) { return false; }
89 // Convert an underscore_based_indentifier in to camelCase.
90 // Also uppercases the first character if first is true.
91 std::string MakeCamel(const std::string &in, bool first) {
93 for (size_t i = 0; i < in.length(); i++) {
95 s += static_cast<char>(toupper(in[0]));
96 else if (in[i] == '_' && i + 1 < in.length())
97 s += static_cast<char>(toupper(in[++i]));
104 // Convert an underscore_based_identifier in to screaming snake case.
105 std::string MakeScreamingCamel(const std::string &in) {
107 for (size_t i = 0; i < in.length(); i++) {
109 s += static_cast<char>(toupper(in[i]));
116 void DeserializeDoc(std::vector<std::string> &doc,
117 const Vector<Offset<String>> *documentation) {
118 if (documentation == nullptr) return;
119 for (uoffset_t index = 0; index < documentation->size(); index++)
120 doc.push_back(documentation->Get(index)->str());
123 void Parser::Message(const std::string &msg) {
124 if (!error_.empty()) error_ += "\n"; // log all warnings and errors
125 error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
128 #ifdef _WIN32 // MSVC alike
130 "(" + NumToString(line_) + ", " + NumToString(CursorPosition()) + ")";
132 if (file_being_parsed_.length()) error_ += ":";
133 error_ += NumToString(line_) + ": " + NumToString(CursorPosition());
136 error_ += ": " + msg;
139 void Parser::Warning(const std::string &msg) { Message("warning: " + msg); }
141 CheckedError Parser::Error(const std::string &msg) {
142 Message("error: " + msg);
143 return CheckedError(true);
146 inline CheckedError NoError() { return CheckedError(false); }
148 CheckedError Parser::RecurseError() {
149 return Error("maximum parsing recursion of " +
150 NumToString(FLATBUFFERS_MAX_PARSING_DEPTH) + " reached");
153 template<typename F> CheckedError Parser::Recurse(F f) {
154 if (recurse_protection_counter >= (FLATBUFFERS_MAX_PARSING_DEPTH))
155 return RecurseError();
156 recurse_protection_counter++;
158 recurse_protection_counter--;
162 template<typename T> std::string TypeToIntervalString() {
163 return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
164 NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
167 // atot: template version of atoi/atof: convert a string to an instance of T.
169 inline CheckedError atot(const char *s, Parser &parser, T *val) {
170 auto done = StringToNumber(s, val);
171 if (done) return NoError();
173 return parser.Error("invalid number: \"" + std::string(s) + "\"");
175 return parser.Error("invalid number: \"" + std::string(s) + "\"" +
176 ", constant does not fit " + TypeToIntervalString<T>());
179 inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
182 *val = Offset<void>(atoi(s));
186 std::string Namespace::GetFullyQualifiedName(const std::string &name,
187 size_t max_components) const {
188 // Early exit if we don't have a defined namespace.
189 if (components.empty() || !max_components) { return name; }
190 std::string stream_str;
191 for (size_t i = 0; i < std::min(components.size(), max_components); i++) {
192 if (i) { stream_str += '.'; }
193 stream_str += std::string(components[i]);
202 // Declare tokens we'll use. Single character tokens are represented by their
203 // ascii character code (e.g. '{'), others above 256.
205 #define FLATBUFFERS_GEN_TOKENS(TD) \
206 TD(Eof, 256, "end of file") \
207 TD(StringConstant, 257, "string constant") \
208 TD(IntegerConstant, 258, "integer constant") \
209 TD(FloatConstant, 259, "float constant") \
210 TD(Identifier, 260, "identifier")
212 __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
215 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
216 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
217 #undef FLATBUFFERS_TOKEN
220 static std::string TokenToString(int t) {
221 static const char * const tokens[] = {
222 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
223 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
224 #undef FLATBUFFERS_TOKEN
225 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
226 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
228 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
229 #undef FLATBUFFERS_TD
231 if (t < 256) { // A single ascii char token.
233 s.append(1, static_cast<char>(t));
235 } else { // Other tokens.
236 return tokens[t - 256];
241 std::string Parser::TokenToStringId(int t) const {
242 return t == kTokenIdentifier ? attribute_ : TokenToString(t);
245 // Parses exactly nibbles worth of hex digits into a number, or error.
246 CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
247 FLATBUFFERS_ASSERT(nibbles > 0);
248 for (int i = 0; i < nibbles; i++)
249 if (!is_xdigit(cursor_[i]))
250 return Error("escape code must be followed by " + NumToString(nibbles) +
252 std::string target(cursor_, cursor_ + nibbles);
253 *val = StringToUInt(target.c_str(), 16);
258 CheckedError Parser::SkipByteOrderMark() {
259 if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
261 if (static_cast<unsigned char>(*cursor_) != 0xbb)
262 return Error("invalid utf-8 byte order mark");
264 if (static_cast<unsigned char>(*cursor_) != 0xbf)
265 return Error("invalid utf-8 byte order mark");
270 static inline bool IsIdentifierStart(char c) {
271 return is_alpha(c) || (c == '_');
274 CheckedError Parser::Next() {
275 doc_comment_.clear();
276 bool seen_newline = cursor_ == source_;
278 attr_is_trivial_ascii_string_ = true;
303 case '=': return NoError();
306 int unicode_high_surrogate = -1;
308 while (*cursor_ != c) {
309 if (*cursor_ < ' ' && static_cast<signed char>(*cursor_) >= 0)
310 return Error("illegal character in string constant");
311 if (*cursor_ == '\\') {
312 attr_is_trivial_ascii_string_ = false; // has escape sequence
314 if (unicode_high_surrogate != -1 && *cursor_ != 'u') {
316 "illegal Unicode sequence (unpaired high surrogate)");
355 case 'x': { // Not in the JSON standard
358 ECHECK(ParseHexNum(2, &val));
359 attribute_ += static_cast<char>(val);
365 ECHECK(ParseHexNum(4, &val));
366 if (val >= 0xD800 && val <= 0xDBFF) {
367 if (unicode_high_surrogate != -1) {
369 "illegal Unicode sequence (multiple high surrogates)");
371 unicode_high_surrogate = static_cast<int>(val);
373 } else if (val >= 0xDC00 && val <= 0xDFFF) {
374 if (unicode_high_surrogate == -1) {
376 "illegal Unicode sequence (unpaired low surrogate)");
378 int code_point = 0x10000 +
379 ((unicode_high_surrogate & 0x03FF) << 10) +
381 ToUTF8(code_point, &attribute_);
382 unicode_high_surrogate = -1;
385 if (unicode_high_surrogate != -1) {
387 "illegal Unicode sequence (unpaired high surrogate)");
389 ToUTF8(static_cast<int>(val), &attribute_);
393 default: return Error("unknown escape code in string constant");
395 } else { // printable chars + UTF-8 bytes
396 if (unicode_high_surrogate != -1) {
398 "illegal Unicode sequence (unpaired high surrogate)");
400 // reset if non-printable
401 attr_is_trivial_ascii_string_ &=
402 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;
455 (c == '.') ? 0 : 1; // dot_lvl==0 <=> exactly one '.' seen
456 if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum?
457 // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4).
458 if (is_digit(c) || has_sign || !dot_lvl) {
459 const auto start = cursor_ - 1;
460 auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1;
461 if (!is_digit(c) && is_digit(*cursor_)) {
462 start_digits = cursor_; // see digit in cursor_ position
465 // hex-float can't begind with '.'
466 auto use_hex = dot_lvl && (c == '0') && is_alpha_char(*cursor_, 'X');
467 if (use_hex) start_digits = ++cursor_; // '0x' is the prefix, skip it
468 // Read an integer number or mantisa of float-point number.
471 while (is_xdigit(*cursor_)) cursor_++;
473 while (is_digit(*cursor_)) cursor_++;
475 } while ((*cursor_ == '.') && (++cursor_) && (--dot_lvl >= 0));
476 // Exponent of float-point number.
477 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
478 // The exponent suffix of hexadecimal float number is mandatory.
479 if (use_hex && !dot_lvl) start_digits = cursor_;
480 if ((use_hex && is_alpha_char(*cursor_, 'P')) ||
481 is_alpha_char(*cursor_, 'E')) {
482 dot_lvl = 0; // Emulate dot to signal about float-point number.
484 if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
485 start_digits = cursor_; // the exponent-part has to have digits
486 // Exponent is decimal integer number
487 while (is_digit(*cursor_)) cursor_++;
488 if (*cursor_ == '.') {
489 cursor_++; // If see a dot treat it as part of invalid number.
490 dot_lvl = -1; // Fall thru to Error().
495 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
496 attribute_.append(start, cursor_);
497 token_ = dot_lvl ? kTokenIntegerConstant : kTokenFloatConstant;
500 return Error("invalid number: " + std::string(start, cursor_));
505 if (false == check_ascii_range(c, ' ', '~'))
506 ch = "code: " + NumToString(c);
507 return Error("illegal character: " + ch);
512 // Check if a given token is next.
513 bool Parser::Is(int t) const { return t == token_; }
515 bool Parser::IsIdent(const char *id) const {
516 return token_ == kTokenIdentifier && attribute_ == id;
519 // Expect a given token to be next, consume it, or error if not present.
520 CheckedError Parser::Expect(int t) {
522 return Error("expecting: " + TokenToString(t) +
523 " instead got: " + TokenToStringId(token_));
529 CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
534 if (last) *last = attribute_;
535 EXPECT(kTokenIdentifier);
540 EnumDef *Parser::LookupEnum(const std::string &id) {
541 // Search thru parent namespaces.
542 for (int components = static_cast<int>(current_namespace_->components.size());
543 components >= 0; components--) {
544 auto ed = enums_.Lookup(
545 current_namespace_->GetFullyQualifiedName(id, components));
551 StructDef *Parser::LookupStruct(const std::string &id) const {
552 auto sd = structs_.Lookup(id);
553 if (sd) sd->refcount++;
557 CheckedError Parser::ParseTypeIdent(Type &type) {
558 std::string id = attribute_;
559 EXPECT(kTokenIdentifier);
560 ECHECK(ParseNamespacing(&id, nullptr));
561 auto enum_def = LookupEnum(id);
563 type = enum_def->underlying_type;
564 if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
566 type.base_type = BASE_TYPE_STRUCT;
567 type.struct_def = LookupCreateStruct(id);
572 // Parse any IDL type.
573 CheckedError Parser::ParseType(Type &type) {
574 if (token_ == kTokenIdentifier) {
575 if (IsIdent("bool")) {
576 type.base_type = BASE_TYPE_BOOL;
578 } else if (IsIdent("byte") || IsIdent("int8")) {
579 type.base_type = BASE_TYPE_CHAR;
581 } else if (IsIdent("ubyte") || IsIdent("uint8")) {
582 type.base_type = BASE_TYPE_UCHAR;
584 } else if (IsIdent("short") || IsIdent("int16")) {
585 type.base_type = BASE_TYPE_SHORT;
587 } else if (IsIdent("ushort") || IsIdent("uint16")) {
588 type.base_type = BASE_TYPE_USHORT;
590 } else if (IsIdent("int") || IsIdent("int32")) {
591 type.base_type = BASE_TYPE_INT;
593 } else if (IsIdent("uint") || IsIdent("uint32")) {
594 type.base_type = BASE_TYPE_UINT;
596 } else if (IsIdent("long") || IsIdent("int64")) {
597 type.base_type = BASE_TYPE_LONG;
599 } else if (IsIdent("ulong") || IsIdent("uint64")) {
600 type.base_type = BASE_TYPE_ULONG;
602 } else if (IsIdent("float") || IsIdent("float32")) {
603 type.base_type = BASE_TYPE_FLOAT;
605 } else if (IsIdent("double") || IsIdent("float64")) {
606 type.base_type = BASE_TYPE_DOUBLE;
608 } else if (IsIdent("string")) {
609 type.base_type = BASE_TYPE_STRING;
612 ECHECK(ParseTypeIdent(type));
614 } else if (token_ == '[') {
617 ECHECK(Recurse([&]() { return ParseType(subtype); }));
618 if (IsSeries(subtype)) {
619 // We could support this, but it will complicate things, and it's
620 // easier to work around with a struct around the inner vector.
621 return Error("nested vector types not supported (wrap in table first)");
625 if (token_ != kTokenIntegerConstant) {
626 return Error("length of fixed-length array must be an integer value");
628 uint16_t fixed_length = 0;
629 bool check = StringToNumber(attribute_.c_str(), &fixed_length);
630 if (!check || fixed_length < 1) {
632 "length of fixed-length array must be positive and fit to "
635 // Check if enum arrays are used in C++ without specifying --scoped-enums
636 if ((opts.lang_to_generate & IDLOptions::kCpp) && !opts.scoped_enums &&
639 "--scoped-enums must be enabled to use enum arrays in C++\n");
641 type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def,
645 type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
647 type.element = subtype.base_type;
650 return Error("illegal type syntax");
655 CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
656 const Type &type, FieldDef **dest) {
657 auto &field = *new FieldDef();
659 FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
661 field.file = struct_def.file;
662 field.value.type = type;
663 if (struct_def.fixed) { // statically compute the field offset
664 auto size = InlineSize(type);
665 auto alignment = InlineAlignment(type);
666 // structs_ need to have a predictable format, so we need to align to
667 // the largest scalar
668 struct_def.minalign = std::max(struct_def.minalign, alignment);
669 struct_def.PadLastField(alignment);
670 field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
671 struct_def.bytesize += size;
673 if (struct_def.fields.Add(name, &field))
674 return Error("field already exists: " + name);
679 CheckedError Parser::ParseField(StructDef &struct_def) {
680 std::string name = attribute_;
682 if (LookupCreateStruct(name, false, false))
683 return Error("field name can not be the same as table/struct name");
685 std::vector<std::string> dc = doc_comment_;
686 EXPECT(kTokenIdentifier);
689 ECHECK(ParseType(type));
691 if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type) &&
693 return Error("structs_ may contain only scalar or struct fields");
695 if (!struct_def.fixed && IsArray(type))
696 return Error("fixed-length array in table must be wrapped in struct");
698 if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
700 "Arrays are not yet supported in all "
701 "the specified programming languages.");
704 FieldDef *typefield = nullptr;
705 if (type.base_type == BASE_TYPE_UNION) {
706 // For union fields, add a second auto-generated field to hold the type,
707 // with a special suffix.
708 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
709 type.enum_def->underlying_type, &typefield));
710 } else if (type.base_type == BASE_TYPE_VECTOR &&
711 type.element == BASE_TYPE_UNION) {
712 // Only cpp, js and ts supports the union vector feature so far.
713 if (!SupportsAdvancedUnionFeatures()) {
715 "Vectors of unions are not yet supported in all "
716 "the specified programming languages.");
718 // For vector of union fields, add a second auto-generated vector field to
719 // hold the types, with a special suffix.
720 Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def);
721 union_vector.element = BASE_TYPE_UTYPE;
722 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector,
727 ECHECK(AddField(struct_def, name, type, &field));
731 ECHECK(ParseSingleValue(&field->name, field->value, true));
732 if (!IsScalar(type.base_type) ||
733 (struct_def.fixed && field->value.constant != "0"))
735 "default values currently only supported for scalars in tables");
737 // Append .0 if the value has not it (skip hex and scientific floats).
738 // This suffix needed for generated C++ code.
739 if (IsFloat(type.base_type)) {
740 auto &text = field->value.constant;
741 FLATBUFFERS_ASSERT(false == text.empty());
742 auto s = text.c_str();
743 while (*s == ' ') s++;
744 if (*s == '-' || *s == '+') s++;
745 // 1) A float constants (nan, inf, pi, etc) is a kind of identifier.
746 // 2) A float number needn't ".0" at the end if it has exponent.
747 if ((false == IsIdentifierStart(*s)) &&
748 (std::string::npos == field->value.constant.find_first_of(".eEpP"))) {
749 field->value.constant += ".0";
753 // The type.base_type can only be scalar, union, array or vector.
754 // Table, struct or string can't have enum_def.
755 // Default value of union and vector in NONE, NULL translated to "0".
756 FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
757 (type.base_type == BASE_TYPE_UNION) ||
758 (type.base_type == BASE_TYPE_VECTOR) ||
759 (type.base_type == BASE_TYPE_ARRAY));
760 if (type.base_type == BASE_TYPE_VECTOR) {
761 // Vector can't use initialization list.
762 FLATBUFFERS_ASSERT(field->value.constant == "0");
764 // All unions should have the NONE ("0") enum value.
765 auto in_enum = type.enum_def->attributes.Lookup("bit_flags") ||
766 type.enum_def->FindByValue(field->value.constant);
767 if (false == in_enum)
768 return Error("default value of " + field->value.constant +
769 " for field " + name + " is not part of enum " +
770 type.enum_def->name);
774 field->doc_comment = dc;
775 ECHECK(ParseMetaData(&field->attributes));
776 field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
777 auto hash_name = field->attributes.Lookup("hash");
779 switch ((type.base_type == BASE_TYPE_VECTOR) ? type.element
781 case BASE_TYPE_SHORT:
782 case BASE_TYPE_USHORT: {
783 if (FindHashFunction16(hash_name->constant.c_str()) == nullptr)
784 return Error("Unknown hashing algorithm for 16 bit types: " +
785 hash_name->constant);
789 case BASE_TYPE_UINT: {
790 if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
791 return Error("Unknown hashing algorithm for 32 bit types: " +
792 hash_name->constant);
796 case BASE_TYPE_ULONG: {
797 if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
798 return Error("Unknown hashing algorithm for 64 bit types: " +
799 hash_name->constant);
804 "only short, ushort, int, uint, long and ulong data types support "
808 auto cpp_type = field->attributes.Lookup("cpp_type");
811 return Error("cpp_type can only be used with a hashed field");
812 /// forcing cpp_ptr_type to 'naked' if unset
813 auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
815 auto val = new Value();
816 val->type = cpp_type->type;
817 val->constant = "naked";
818 field->attributes.Add("cpp_ptr_type", val);
821 if (field->deprecated && struct_def.fixed)
822 return Error("can't deprecate fields in a struct");
823 field->required = field->attributes.Lookup("required") != nullptr;
824 if (field->required && (struct_def.fixed || IsScalar(type.base_type)))
825 return Error("only non-scalar fields in tables may be 'required'");
826 field->key = field->attributes.Lookup("key") != nullptr;
828 if (struct_def.has_key) return Error("only one field may be set as 'key'");
829 struct_def.has_key = true;
830 if (!IsScalar(type.base_type)) {
831 field->required = true;
832 if (type.base_type != BASE_TYPE_STRING)
833 return Error("'key' field must be string or scalar type");
836 field->shared = field->attributes.Lookup("shared") != nullptr;
837 if (field->shared && field->value.type.base_type != BASE_TYPE_STRING)
838 return Error("shared can only be defined on strings");
840 auto field_native_custom_alloc =
841 field->attributes.Lookup("native_custom_alloc");
842 if (field_native_custom_alloc)
844 "native_custom_alloc can only be used with a table or struct "
847 field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
848 if (field->native_inline && !IsStruct(field->value.type))
849 return Error("native_inline can only be defined on structs");
851 auto nested = field->attributes.Lookup("nested_flatbuffer");
853 if (nested->type.base_type != BASE_TYPE_STRING)
855 "nested_flatbuffer attribute must be a string (the root type)");
856 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
858 "nested_flatbuffer attribute may only apply to a vector of ubyte");
859 // This will cause an error if the root type of the nested flatbuffer
860 // wasn't defined elsewhere.
861 field->nested_flatbuffer = LookupCreateStruct(nested->constant);
864 if (field->attributes.Lookup("flexbuffer")) {
865 field->flexbuffer = true;
866 uses_flexbuffers_ = true;
867 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
868 return Error("flexbuffer attribute may only apply to a vector of ubyte");
872 if (!IsScalar(typefield->value.type.base_type)) {
873 // this is a union vector field
874 typefield->required = field->required;
876 // If this field is a union, and it has a manually assigned id,
877 // the automatically added type field should have an id as well (of N - 1).
878 auto attr = field->attributes.Lookup("id");
880 auto id = atoi(attr->constant.c_str());
881 auto val = new Value();
882 val->type = attr->type;
883 val->constant = NumToString(id - 1);
884 typefield->attributes.Add("id", val);
892 CheckedError Parser::ParseString(Value &val) {
894 EXPECT(kTokenStringConstant);
895 val.constant = NumToString(builder_.CreateString(s).o);
899 CheckedError Parser::ParseComma() {
900 if (!opts.protobuf_ascii_alike) EXPECT(',');
904 CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
905 size_t parent_fieldn,
906 const StructDef *parent_struct_def,
907 uoffset_t count, bool inside_vector) {
908 switch (val.type.base_type) {
909 case BASE_TYPE_UNION: {
910 FLATBUFFERS_ASSERT(field);
911 std::string constant;
912 Vector<uint8_t> *vector_of_union_types = nullptr;
913 // Find corresponding type field we may have already parsed.
914 for (auto elem = field_stack_.rbegin() + count;
915 elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) {
916 auto &type = elem->second->value.type;
917 if (type.enum_def == val.type.enum_def) {
919 if (type.base_type == BASE_TYPE_VECTOR &&
920 type.element == BASE_TYPE_UTYPE) {
921 // Vector of union type field.
923 ECHECK(atot(elem->first.constant.c_str(), *this, &offset));
924 vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>(
925 builder_.GetCurrentBufferPointer() + builder_.GetSize() -
930 if (type.base_type == BASE_TYPE_UTYPE) {
932 constant = elem->first.constant;
938 if (constant.empty() && !inside_vector) {
939 // We haven't seen the type field yet. Sadly a lot of JSON writers
940 // output these in alphabetical order, meaning it comes after this
941 // value. So we scan past the value to find it, then come back here.
942 // We currently don't do this for vectors of unions because the
943 // scanning/serialization logic would get very complicated.
944 auto type_name = field->name + UnionTypeFieldSuffix();
945 FLATBUFFERS_ASSERT(parent_struct_def);
946 auto type_field = parent_struct_def->fields.Lookup(type_name);
947 FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField().
948 // Remember where we are in the source file, so we can come back here.
949 auto backup = *static_cast<ParserState *>(this);
950 ECHECK(SkipAnyJsonValue()); // The table.
951 ECHECK(ParseComma());
952 auto next_name = attribute_;
953 if (Is(kTokenStringConstant)) {
956 EXPECT(kTokenIdentifier);
958 if (next_name == type_name) {
960 Value type_val = type_field->value;
961 ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr, 0));
962 constant = type_val.constant;
963 // Got the information we needed, now rewind:
964 *static_cast<ParserState *>(this) = backup;
967 if (constant.empty() && !vector_of_union_types) {
968 return Error("missing type field for this union value: " + field->name);
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, F body) {
1046 // We allow tables both as JSON object{ .. } with field names
1047 // or vector[..] with all fields in order
1048 char terminator = '}';
1049 bool is_nested_vector = struct_def && Is('[');
1050 if (is_nested_vector) {
1057 if ((!opts.strict_json || !fieldn) && Is(terminator)) break;
1059 if (is_nested_vector) {
1060 if (fieldn >= struct_def->fields.vec.size()) {
1061 return Error("too many unnamed fields in nested array");
1063 name = struct_def->fields.vec[fieldn]->name;
1066 if (Is(kTokenStringConstant)) {
1069 EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
1071 if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
1073 ECHECK(body(name, fieldn, struct_def));
1074 if (Is(terminator)) break;
1075 ECHECK(ParseComma());
1078 if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
1079 return Error("wrong number of unnamed fields in table vector");
1084 CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
1085 uoffset_t *ovalue) {
1086 size_t fieldn_outer = 0;
1087 auto err = ParseTableDelimiters(
1088 fieldn_outer, &struct_def,
1089 [&](const std::string &name, size_t &fieldn,
1090 const StructDef *struct_def_inner) -> CheckedError {
1091 if (name == "$schema") {
1092 ECHECK(Expect(kTokenStringConstant));
1095 auto field = struct_def_inner->fields.Lookup(name);
1097 if (!opts.skip_unexpected_fields_in_json) {
1098 return Error("unknown field: " + name);
1100 ECHECK(SkipAnyJsonValue());
1103 if (IsIdent("null") && !IsScalar(field->value.type.base_type)) {
1104 ECHECK(Next()); // Ignore this field.
1106 Value val = field->value;
1107 if (field->flexbuffer) {
1108 flexbuffers::Builder builder(1024,
1109 flexbuffers::BUILDER_FLAG_SHARE_ALL);
1110 ECHECK(ParseFlexBufferValue(&builder));
1112 // Force alignment for nested flexbuffer
1113 builder_.ForceVectorAlignment(builder.GetSize(), sizeof(uint8_t),
1114 sizeof(largest_scalar_t));
1115 auto off = builder_.CreateVector(builder.GetBuffer());
1116 val.constant = NumToString(off.o);
1117 } else if (field->nested_flatbuffer) {
1119 ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner));
1121 ECHECK(Recurse([&]() {
1122 return ParseAnyValue(val, field, fieldn, struct_def_inner, 0);
1125 // Hardcoded insertion-sort with error-check.
1126 // If fields are specified in order, then this loop exits
1128 auto elem = field_stack_.rbegin();
1129 for (; elem != field_stack_.rbegin() + fieldn; ++elem) {
1130 auto existing_field = elem->second;
1131 if (existing_field == field)
1132 return Error("field set more than once: " + field->name);
1133 if (existing_field->value.offset < field->value.offset) break;
1135 // Note: elem points to before the insertion point, thus .base()
1136 // points to the correct spot.
1137 field_stack_.insert(elem.base(), std::make_pair(val, field));
1145 // Check if all required fields are parsed.
1146 for (auto field_it = struct_def.fields.vec.begin();
1147 field_it != struct_def.fields.vec.end(); ++field_it) {
1148 auto required_field = *field_it;
1149 if (!required_field->required) { continue; }
1151 for (auto pf_it = field_stack_.end() - fieldn_outer;
1152 pf_it != field_stack_.end(); ++pf_it) {
1153 auto parsed_field = pf_it->second;
1154 if (parsed_field == required_field) {
1160 return Error("required field is missing: " + required_field->name +
1161 " in " + struct_def.name);
1165 if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size())
1166 return Error("struct: wrong number of initializers: " + struct_def.name);
1168 auto start = struct_def.fixed ? builder_.StartStruct(struct_def.minalign)
1169 : builder_.StartTable();
1171 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size;
1173 // Go through elements in reverse, since we're building the data backwards.
1174 for (auto it = field_stack_.rbegin();
1175 it != field_stack_.rbegin() + fieldn_outer; ++it) {
1176 auto &field_value = it->first;
1177 auto field = it->second;
1178 if (!struct_def.sortbysize ||
1179 size == SizeOf(field_value.type.base_type)) {
1180 switch (field_value.type.base_type) {
1182 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1183 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1184 case BASE_TYPE_ ## ENUM: \
1185 builder_.Pad(field->padding); \
1186 if (struct_def.fixed) { \
1188 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1189 builder_.PushElement(val); \
1191 CTYPE val, valdef; \
1192 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1193 ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
1194 builder_.AddElement(field_value.offset, val, valdef); \
1197 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1198 #undef FLATBUFFERS_TD
1199 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1200 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1201 case BASE_TYPE_ ## ENUM: \
1202 builder_.Pad(field->padding); \
1203 if (IsStruct(field->value.type)) { \
1204 SerializeStruct(*field->value.type.struct_def, field_value); \
1207 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1208 builder_.AddOffset(field_value.offset, val); \
1211 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
1212 #undef FLATBUFFERS_TD
1213 case BASE_TYPE_ARRAY:
1214 builder_.Pad(field->padding);
1216 reinterpret_cast<const uint8_t*>(field_value.constant.c_str()),
1217 InlineSize(field_value.type));
1224 for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back();
1226 if (struct_def.fixed) {
1227 builder_.ClearOffsets();
1228 builder_.EndStruct();
1229 FLATBUFFERS_ASSERT(value);
1230 // Temporarily store this struct in the value string, since it is to
1231 // be serialized in-place elsewhere.
1233 reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
1234 struct_def.bytesize);
1235 builder_.PopBytes(struct_def.bytesize);
1236 FLATBUFFERS_ASSERT(!ovalue);
1238 auto val = builder_.EndTable(start);
1239 if (ovalue) *ovalue = val;
1240 if (value) *value = NumToString(val);
1245 template<typename F>
1246 CheckedError Parser::ParseVectorDelimiters(uoffset_t &count, F body) {
1249 if ((!opts.strict_json || !count) && Is(']')) break;
1250 ECHECK(body(count));
1253 ECHECK(ParseComma());
1259 static bool CompareType(const uint8_t *a, const uint8_t *b, BaseType ftype) {
1261 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
1263 case BASE_TYPE_##ENUM: return ReadScalar<CTYPE>(a) < ReadScalar<CTYPE>(b);
1264 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
1265 #undef FLATBUFFERS_TD
1266 case BASE_TYPE_STRING:
1267 // Indirect offset pointer to string pointer.
1268 a += ReadScalar<uoffset_t>(a);
1269 b += ReadScalar<uoffset_t>(b);
1270 return *reinterpret_cast<const String *>(a) <
1271 *reinterpret_cast<const String *>(b);
1272 default: return false;
1276 // See below for why we need our own sort :(
1277 template<typename T, typename F, typename S>
1278 void SimpleQsort(T *begin, T *end, size_t width, F comparator, S swapper) {
1279 if (end - begin <= static_cast<ptrdiff_t>(width)) return;
1280 auto l = begin + width;
1283 if (comparator(begin, l)) {
1292 SimpleQsort(begin, l, width, comparator, swapper);
1293 SimpleQsort(r, end, width, comparator, swapper);
1296 CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
1297 FieldDef *field, size_t fieldn) {
1298 uoffset_t count = 0;
1299 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1302 ECHECK(Recurse([&]() {
1303 return ParseAnyValue(val, field, fieldn, nullptr, count, true);
1305 field_stack_.push_back(std::make_pair(val, nullptr));
1310 builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
1311 InlineAlignment(type));
1312 for (uoffset_t i = 0; i < count; i++) {
1313 // start at the back, since we're building the data backwards.
1314 auto &val = field_stack_.back().first;
1315 switch (val.type.base_type) {
1317 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1318 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1319 case BASE_TYPE_ ## ENUM: \
1320 if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
1323 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1324 builder_.PushElement(elem); \
1327 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1328 #undef FLATBUFFERS_TD
1331 field_stack_.pop_back();
1334 builder_.ClearOffsets();
1335 *ovalue = builder_.EndVector(count);
1337 if (type.base_type == BASE_TYPE_STRUCT && type.struct_def->has_key) {
1338 // We should sort this vector. Find the key first.
1339 const FieldDef *key = nullptr;
1340 for (auto it = type.struct_def->fields.vec.begin();
1341 it != type.struct_def->fields.vec.end(); ++it) {
1349 // We can't use std::sort because for structs the size is not known at
1350 // compile time, and for tables our iterators dereference offsets, so can't
1351 // be used to swap elements.
1352 // And we can't use C qsort either, since that would force use to use
1353 // globals, making parsing thread-unsafe.
1354 // So for now, we use SimpleQsort above.
1355 // TODO: replace with something better, preferably not recursive.
1356 static voffset_t offset = key->value.offset;
1357 static BaseType ftype = key->value.type.base_type;
1359 if (type.struct_def->fixed) {
1361 reinterpret_cast<VectorOfAny *>(builder_.GetCurrentBufferPointer());
1362 SimpleQsort<uint8_t>(
1363 v->Data(), v->Data() + v->size() * type.struct_def->bytesize,
1364 type.struct_def->bytesize,
1365 [](const uint8_t *a, const uint8_t *b) -> bool {
1366 return CompareType(a + offset, b + offset, ftype);
1368 [&](uint8_t *a, uint8_t *b) {
1370 for (size_t i = 0; i < type.struct_def->bytesize; i++) {
1371 std::swap(a[i], b[i]);
1375 auto v = reinterpret_cast<Vector<Offset<Table>> *>(
1376 builder_.GetCurrentBufferPointer());
1377 // Here also can't use std::sort. We do have an iterator type for it,
1378 // but it is non-standard as it will dereference the offsets, and thus
1379 // can't be used to swap elements.
1380 SimpleQsort<Offset<Table>>(
1381 v->data(), v->data() + v->size(), 1,
1382 [](const Offset<Table> *_a, const Offset<Table> *_b) -> bool {
1383 // Indirect offset pointer to table pointer.
1384 auto a = reinterpret_cast<const uint8_t *>(_a) +
1385 ReadScalar<uoffset_t>(_a);
1386 auto b = reinterpret_cast<const uint8_t *>(_b) +
1387 ReadScalar<uoffset_t>(_b);
1388 // Fetch field address from table.
1389 a = reinterpret_cast<const Table *>(a)->GetAddressOf(offset);
1390 b = reinterpret_cast<const Table *>(b)->GetAddressOf(offset);
1391 return CompareType(a, b, ftype);
1393 [&](Offset<Table> *a, Offset<Table> *b) {
1394 // These are serialized offsets, so are relative where they are
1395 // stored in memory, so compute the distance between these pointers:
1396 ptrdiff_t diff = (b - a) * sizeof(Offset<Table>);
1397 assert(diff >= 0); // Guaranteed by SimpleQsort.
1398 auto udiff = static_cast<uoffset_t>(diff);
1399 a->o = EndianScalar(ReadScalar<uoffset_t>(a) - udiff);
1400 b->o = EndianScalar(ReadScalar<uoffset_t>(b) + udiff);
1408 CheckedError Parser::ParseArray(Value &array) {
1409 std::vector<Value> stack;
1410 FlatBufferBuilder builder;
1411 const auto &type = array.type.VectorType();
1412 auto length = array.type.fixed_length;
1413 uoffset_t count = 0;
1414 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1415 vector_emplace_back(&stack, Value());
1416 auto &val = stack.back();
1418 if (IsStruct(type)) {
1419 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1421 ECHECK(ParseSingleValue(nullptr, val, false));
1426 if (length != count) return Error("Fixed-length array size is incorrect.");
1428 for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
1431 switch (val.type.base_type) {
1432 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1433 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1434 case BASE_TYPE_ ## ENUM: \
1435 if (IsStruct(val.type)) { \
1436 SerializeStruct(builder, *val.type.struct_def, val); \
1439 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1440 builder.PushElement(elem); \
1443 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1444 #undef FLATBUFFERS_TD
1445 default: FLATBUFFERS_ASSERT(0);
1450 array.constant.assign(
1451 reinterpret_cast<const char *>(builder.GetCurrentBufferPointer()),
1452 InlineSize(array.type));
1456 CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
1458 const StructDef *parent_struct_def) {
1459 if (token_ == '[') { // backwards compat for 'legacy' ubyte buffers
1460 ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
1462 auto cursor_at_value_begin = cursor_;
1463 ECHECK(SkipAnyJsonValue());
1464 std::string substring(cursor_at_value_begin - 1, cursor_ - 1);
1466 // Create and initialize new parser
1467 Parser nested_parser;
1468 FLATBUFFERS_ASSERT(field->nested_flatbuffer);
1469 nested_parser.root_struct_def_ = field->nested_flatbuffer;
1470 nested_parser.enums_ = enums_;
1471 nested_parser.opts = opts;
1472 nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
1474 // Parse JSON substring into new flatbuffer builder using nested_parser
1475 bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
1477 // Clean nested_parser to avoid deleting the elements in
1478 // the SymbolTables on destruction
1479 nested_parser.enums_.dict.clear();
1480 nested_parser.enums_.vec.clear();
1482 if (!ok) { ECHECK(Error(nested_parser.error_)); }
1483 // Force alignment for nested flatbuffer
1484 builder_.ForceVectorAlignment(
1485 nested_parser.builder_.GetSize(), sizeof(uint8_t),
1486 nested_parser.builder_.GetBufferMinAlignment());
1488 auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
1489 nested_parser.builder_.GetSize());
1490 val.constant = NumToString(off.o);
1495 CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) {
1499 auto name = attribute_;
1500 if (false == (Is(kTokenIdentifier) || Is(kTokenStringConstant)))
1501 return Error("attribute name must be either identifier or string: " +
1503 if (known_attributes_.find(name) == known_attributes_.end())
1504 return Error("user define attributes must be declared before use: " +
1507 auto e = new Value();
1508 attributes->Add(name, e);
1511 ECHECK(ParseSingleValue(&name, *e, true));
1523 CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
1524 bool check, Value &e, BaseType req,
1526 bool match = dtoken == token_;
1528 FLATBUFFERS_ASSERT(*destmatch == false);
1530 e.constant = attribute_;
1531 // Check token match
1533 if (e.type.base_type == BASE_TYPE_NONE) {
1534 e.type.base_type = req;
1537 std::string("type mismatch: expecting: ") +
1538 kTypeNames[e.type.base_type] + ", found: " + kTypeNames[req] +
1539 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1542 // The exponent suffix of hexadecimal float-point number is mandatory.
1543 // A hex-integer constant is forbidden as an initializer of float number.
1544 if ((kTokenFloatConstant != dtoken) && IsFloat(e.type.base_type)) {
1545 const auto &s = e.constant;
1546 const auto k = s.find_first_of("0123456789.");
1547 if ((std::string::npos != k) && (s.length() > (k + 1)) &&
1548 (s[k] == '0' && is_alpha_char(s[k + 1], 'X')) &&
1549 (std::string::npos == s.find_first_of("pP", k + 2))) {
1551 "invalid number, the exponent suffix of hexadecimal "
1552 "floating-point literals is mandatory: \"" +
1562 CheckedError Parser::ParseEnumFromString(const Type &type,
1563 std::string *result) {
1564 const auto base_type =
1565 type.enum_def ? type.enum_def->underlying_type.base_type : type.base_type;
1566 if (!IsInteger(base_type)) return Error("not a valid value for this field");
1568 for (size_t pos = 0; pos != std::string::npos;) {
1569 const auto delim = attribute_.find_first_of(' ', pos);
1570 const auto last = (std::string::npos == delim);
1571 auto word = attribute_.substr(pos, !last ? delim - pos : std::string::npos);
1572 pos = !last ? delim + 1 : std::string::npos;
1573 const EnumVal *ev = nullptr;
1574 if (type.enum_def) {
1575 ev = type.enum_def->Lookup(word);
1577 auto dot = word.find_first_of('.');
1578 if (std::string::npos == dot)
1579 return Error("enum values need to be qualified by an enum type");
1580 auto enum_def_str = word.substr(0, dot);
1581 const auto enum_def = LookupEnum(enum_def_str);
1582 if (!enum_def) return Error("unknown enum: " + enum_def_str);
1583 auto enum_val_str = word.substr(dot + 1);
1584 ev = enum_def->Lookup(enum_val_str);
1586 if (!ev) return Error("unknown enum value: " + word);
1587 u64 |= ev->GetAsUInt64();
1589 *result = IsUnsigned(base_type) ? NumToString(u64)
1590 : NumToString(static_cast<int64_t>(u64));
1594 CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
1595 FLATBUFFERS_ASSERT(field);
1596 Value *hash_name = field->attributes.Lookup("hash");
1597 switch (e.type.base_type) {
1598 case BASE_TYPE_SHORT: {
1599 auto hash = FindHashFunction16(hash_name->constant.c_str());
1600 int16_t hashed_value = static_cast<int16_t>(hash(attribute_.c_str()));
1601 e.constant = NumToString(hashed_value);
1604 case BASE_TYPE_USHORT: {
1605 auto hash = FindHashFunction16(hash_name->constant.c_str());
1606 uint16_t hashed_value = hash(attribute_.c_str());
1607 e.constant = NumToString(hashed_value);
1610 case BASE_TYPE_INT: {
1611 auto hash = FindHashFunction32(hash_name->constant.c_str());
1612 int32_t hashed_value = static_cast<int32_t>(hash(attribute_.c_str()));
1613 e.constant = NumToString(hashed_value);
1616 case BASE_TYPE_UINT: {
1617 auto hash = FindHashFunction32(hash_name->constant.c_str());
1618 uint32_t hashed_value = hash(attribute_.c_str());
1619 e.constant = NumToString(hashed_value);
1622 case BASE_TYPE_LONG: {
1623 auto hash = FindHashFunction64(hash_name->constant.c_str());
1624 int64_t hashed_value = static_cast<int64_t>(hash(attribute_.c_str()));
1625 e.constant = NumToString(hashed_value);
1628 case BASE_TYPE_ULONG: {
1629 auto hash = FindHashFunction64(hash_name->constant.c_str());
1630 uint64_t hashed_value = hash(attribute_.c_str());
1631 e.constant = NumToString(hashed_value);
1634 default: FLATBUFFERS_ASSERT(0);
1640 CheckedError Parser::TokenError() {
1641 return Error("cannot parse value starting with: " + TokenToStringId(token_));
1644 // Re-pack helper (ParseSingleValue) to normalize defaults of scalars.
1645 template<typename T> inline void SingleValueRepack(Value &e, T val) {
1646 // Remove leading zeros.
1647 if (IsInteger(e.type.base_type)) { e.constant = NumToString(val); }
1649 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
1650 // Normilaze defaults NaN to unsigned quiet-NaN(0).
1651 static inline void SingleValueRepack(Value &e, float val) {
1652 if (val != val) e.constant = "nan";
1654 static inline void SingleValueRepack(Value &e, double val) {
1655 if (val != val) e.constant = "nan";
1659 CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
1661 // First see if this could be a conversion function:
1662 if (token_ == kTokenIdentifier && *cursor_ == '(') {
1663 // todo: Extract processing of conversion functions to ParseFunction.
1664 const auto functionname = attribute_;
1665 if (!IsFloat(e.type.base_type)) {
1666 return Error(functionname + ": type of argument mismatch, expecting: " +
1667 kTypeNames[BASE_TYPE_DOUBLE] +
1668 ", found: " + kTypeNames[e.type.base_type] +
1669 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1673 ECHECK(Recurse([&]() { return ParseSingleValue(name, e, false); }));
1675 // calculate with double precision
1677 ECHECK(atot(e.constant.c_str(), *this, &x));
1678 auto func_match = false;
1680 #define FLATBUFFERS_FN_DOUBLE(name, op) \
1681 if (!func_match && functionname == name) { y = op; func_match = true; }
1682 FLATBUFFERS_FN_DOUBLE("deg", x / kPi * 180);
1683 FLATBUFFERS_FN_DOUBLE("rad", x * kPi / 180);
1684 FLATBUFFERS_FN_DOUBLE("sin", sin(x));
1685 FLATBUFFERS_FN_DOUBLE("cos", cos(x));
1686 FLATBUFFERS_FN_DOUBLE("tan", tan(x));
1687 FLATBUFFERS_FN_DOUBLE("asin", asin(x));
1688 FLATBUFFERS_FN_DOUBLE("acos", acos(x));
1689 FLATBUFFERS_FN_DOUBLE("atan", atan(x));
1690 // TODO(wvo): add more useful conversion functions here.
1691 #undef FLATBUFFERS_FN_DOUBLE
1693 if (true != func_match) {
1694 return Error(std::string("Unknown conversion function: ") + functionname +
1695 ", field name: " + (name ? *name : "") +
1696 ", value: " + e.constant);
1698 e.constant = NumToString(y);
1703 const auto in_type = e.type.base_type;
1705 #define IF_ECHECK_(force, dtoken, check, req) \
1706 if (!match && ((check) || IsConstTrue(force))) \
1707 ECHECK(TryTypedValue(name, dtoken, check, e, req, &match))
1708 #define TRY_ECHECK(dtoken, check, req) IF_ECHECK_(false, dtoken, check, req)
1709 #define FORCE_ECHECK(dtoken, check, req) IF_ECHECK_(true, dtoken, check, req)
1712 if (token_ == kTokenStringConstant || token_ == kTokenIdentifier) {
1713 const auto kTokenStringOrIdent = token_;
1714 // The string type is a most probable type, check it first.
1715 TRY_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1718 // avoid escaped and non-ascii in the string
1719 if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type) &&
1720 !attr_is_trivial_ascii_string_) {
1722 std::string("type mismatch or invalid value, an initializer of "
1723 "non-string field must be trivial ASCII string: type: ") +
1724 kTypeNames[in_type] + ", name: " + (name ? *name : "") +
1725 ", value: " + attribute_);
1728 // A boolean as true/false. Boolean as Integer check below.
1729 if (!match && IsBool(in_type)) {
1730 auto is_true = attribute_ == "true";
1731 if (is_true || attribute_ == "false") {
1732 attribute_ = is_true ? "1" : "0";
1733 // accepts both kTokenStringConstant and kTokenIdentifier
1734 TRY_ECHECK(kTokenStringOrIdent, IsBool(in_type), BASE_TYPE_BOOL);
1737 // Check if this could be a string/identifier enum value.
1738 // Enum can have only true integer base type.
1739 if (!match && IsInteger(in_type) && !IsBool(in_type) &&
1740 IsIdentifierStart(*attribute_.c_str())) {
1741 ECHECK(ParseEnumFromString(e.type, &e.constant));
1745 // Parse a float/integer number from the string.
1746 if (!match) check_now = true; // Re-pack if parsed from string literal.
1747 if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type)) {
1748 // remove trailing whitespaces from attribute_
1749 auto last = attribute_.find_last_not_of(' ');
1750 if (std::string::npos != last) // has non-whitespace
1751 attribute_.resize(last + 1);
1753 // Float numbers or nan, inf, pi, etc.
1754 TRY_ECHECK(kTokenStringOrIdent, IsFloat(in_type), BASE_TYPE_FLOAT);
1755 // An integer constant in string.
1756 TRY_ECHECK(kTokenStringOrIdent, IsInteger(in_type), BASE_TYPE_INT);
1757 // Unknown tokens will be interpreted as string type.
1758 // An attribute value may be a scalar or string constant.
1759 FORCE_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1762 // Try a float number.
1763 TRY_ECHECK(kTokenFloatConstant, IsFloat(in_type), BASE_TYPE_FLOAT);
1764 // Integer token can init any scalar (integer of float).
1765 FORCE_ECHECK(kTokenIntegerConstant, IsScalar(in_type), BASE_TYPE_INT);
1773 msg += "Cannot assign token starting with '" + TokenToStringId(token_) +
1774 "' to value of <" + std::string(kTypeNames[in_type]) + "> type.";
1777 const auto match_type = e.type.base_type; // may differ from in_type
1778 // The check_now flag must be true when parse a fbs-schema.
1779 // This flag forces to check default scalar values or metadata of field.
1780 // For JSON parser the flag should be false.
1781 // If it is set for JSON each value will be checked twice (see ParseTable).
1782 if (check_now && IsScalar(match_type)) {
1784 switch (match_type) {
1785 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1786 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1787 case BASE_TYPE_ ## ENUM: {\
1789 ECHECK(atot(e.constant.c_str(), *this, &val)); \
1790 SingleValueRepack(e, val); \
1792 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1793 #undef FLATBUFFERS_TD
1801 StructDef *Parser::LookupCreateStruct(const std::string &name,
1802 bool create_if_new, bool definition) {
1803 std::string qualified_name = current_namespace_->GetFullyQualifiedName(name);
1804 // See if it exists pre-declared by an unqualified use.
1805 auto struct_def = LookupStruct(name);
1806 if (struct_def && struct_def->predecl) {
1808 // Make sure it has the current namespace, and is registered under its
1810 struct_def->defined_namespace = current_namespace_;
1811 structs_.Move(name, qualified_name);
1815 // See if it exists pre-declared by an qualified use.
1816 struct_def = LookupStruct(qualified_name);
1817 if (struct_def && struct_def->predecl) {
1819 // Make sure it has the current namespace.
1820 struct_def->defined_namespace = current_namespace_;
1825 // Search thru parent namespaces.
1826 for (size_t components = current_namespace_->components.size();
1827 components && !struct_def; components--) {
1828 struct_def = LookupStruct(
1829 current_namespace_->GetFullyQualifiedName(name, components - 1));
1832 if (!struct_def && create_if_new) {
1833 struct_def = new StructDef();
1835 structs_.Add(qualified_name, struct_def);
1836 struct_def->name = name;
1837 struct_def->defined_namespace = current_namespace_;
1839 // Not a definition.
1840 // Rather than failing, we create a "pre declared" StructDef, due to
1841 // circular references, and check for errors at the end of parsing.
1842 // It is defined in the current namespace, as the best guess what the
1843 // final namespace will be.
1844 structs_.Add(name, struct_def);
1845 struct_def->name = name;
1846 struct_def->defined_namespace = current_namespace_;
1847 struct_def->original_location.reset(
1848 new std::string(file_being_parsed_ + ":" + NumToString(line_)));
1854 const EnumVal *EnumDef::MinValue() const {
1855 return vals.vec.empty() ? nullptr : vals.vec.front();
1857 const EnumVal *EnumDef::MaxValue() const {
1858 return vals.vec.empty() ? nullptr : vals.vec.back();
1861 template<typename T> static uint64_t EnumDistanceImpl(T e1, T e2) {
1862 if (e1 < e2) { std::swap(e1, e2); } // use std for scalars
1863 // Signed overflow may occur, use unsigned calculation.
1864 // The unsigned overflow is well-defined by C++ standard (modulo 2^n).
1865 return static_cast<uint64_t>(e1) - static_cast<uint64_t>(e2);
1868 uint64_t EnumDef::Distance(const EnumVal *v1, const EnumVal *v2) const {
1869 return IsUInt64() ? EnumDistanceImpl(v1->GetAsUInt64(), v2->GetAsUInt64())
1870 : EnumDistanceImpl(v1->GetAsInt64(), v2->GetAsInt64());
1873 std::string EnumDef::AllFlags() const {
1874 FLATBUFFERS_ASSERT(attributes.Lookup("bit_flags"));
1876 for (auto it = Vals().begin(); it != Vals().end(); ++it) {
1877 u64 |= (*it)->GetAsUInt64();
1879 return IsUInt64() ? NumToString(u64) : NumToString(static_cast<int64_t>(u64));
1882 EnumVal *EnumDef::ReverseLookup(int64_t enum_idx,
1883 bool skip_union_default) const {
1884 auto skip_first = static_cast<int>(is_union && skip_union_default);
1885 for (auto it = Vals().begin() + skip_first; it != Vals().end(); ++it) {
1886 if ((*it)->GetAsInt64() == enum_idx) { return *it; }
1891 EnumVal *EnumDef::FindByValue(const std::string &constant) const {
1895 uint64_t u64; // avoid reinterpret_cast of pointers
1896 done = StringToNumber(constant.c_str(), &u64);
1897 i64 = static_cast<int64_t>(u64);
1899 done = StringToNumber(constant.c_str(), &i64);
1901 FLATBUFFERS_ASSERT(done);
1902 if (!done) return nullptr;
1903 return ReverseLookup(i64, false);
1906 void EnumDef::SortByValue() {
1909 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1910 return e1->GetAsUInt64() < e2->GetAsUInt64();
1913 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1914 return e1->GetAsInt64() < e2->GetAsInt64();
1918 void EnumDef::RemoveDuplicates() {
1919 // This method depends form SymbolTable implementation!
1920 // 1) vals.vec - owner (raw pointer)
1921 // 2) vals.dict - access map
1922 auto first = vals.vec.begin();
1923 auto last = vals.vec.end();
1924 if (first == last) return;
1925 auto result = first;
1926 while (++first != last) {
1927 if ((*result)->value != (*first)->value) {
1928 *(++result) = *first;
1931 for (auto it = vals.dict.begin(); it != vals.dict.end(); ++it) {
1932 if (it->second == ev) it->second = *result; // reassign
1934 delete ev; // delete enum value
1938 vals.vec.erase(++result, last);
1941 template<typename T> void EnumDef::ChangeEnumValue(EnumVal *ev, T new_value) {
1942 ev->value = static_cast<int64_t>(new_value);
1945 namespace EnumHelper {
1946 template<BaseType E> struct EnumValType { typedef int64_t type; };
1947 template<> struct EnumValType<BASE_TYPE_ULONG> { typedef uint64_t type; };
1948 } // namespace EnumHelper
1950 struct EnumValBuilder {
1951 EnumVal *CreateEnumerator(const std::string &ev_name) {
1952 FLATBUFFERS_ASSERT(!temp);
1953 auto first = enum_def.vals.vec.empty();
1955 temp = new EnumVal(ev_name, first ? 0 : enum_def.vals.vec.back()->value);
1959 EnumVal *CreateEnumerator(const std::string &ev_name, int64_t val) {
1960 FLATBUFFERS_ASSERT(!temp);
1962 temp = new EnumVal(ev_name, val);
1966 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator(const std::string &name) {
1967 FLATBUFFERS_ASSERT(temp);
1968 ECHECK(ValidateValue(&temp->value, false == user_value));
1969 FLATBUFFERS_ASSERT((temp->union_type.enum_def == nullptr) ||
1970 (temp->union_type.enum_def == &enum_def));
1971 auto not_unique = enum_def.vals.Add(name, temp);
1973 if (not_unique) return parser.Error("enum value already exists: " + name);
1977 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator() {
1978 return AcceptEnumerator(temp->name);
1981 FLATBUFFERS_CHECKED_ERROR AssignEnumeratorValue(const std::string &value) {
1984 auto ascending = false;
1985 if (enum_def.IsUInt64()) {
1987 fit = StringToNumber(value.c_str(), &u64);
1988 ascending = u64 > temp->GetAsUInt64();
1989 temp->value = static_cast<int64_t>(u64); // well-defined since C++20.
1992 fit = StringToNumber(value.c_str(), &i64);
1993 ascending = i64 > temp->GetAsInt64();
1996 if (!fit) return parser.Error("enum value does not fit, \"" + value + "\"");
1997 if (!ascending && strict_ascending && !enum_def.vals.vec.empty())
1998 return parser.Error("enum values must be specified in ascending order");
2002 template<BaseType E, typename CTYPE>
2003 inline FLATBUFFERS_CHECKED_ERROR ValidateImpl(int64_t *ev, int m) {
2004 typedef typename EnumHelper::EnumValType<E>::type T; // int64_t or uint64_t
2005 static_assert(sizeof(T) == sizeof(int64_t), "invalid EnumValType");
2006 const auto v = static_cast<T>(*ev);
2007 auto up = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::max)());
2008 auto dn = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::lowest)());
2009 if (v < dn || v > (up - m)) {
2010 return parser.Error("enum value does not fit, \"" + NumToString(v) +
2011 (m ? " + 1\"" : "\"") + " out of " +
2012 TypeToIntervalString<CTYPE>());
2014 *ev = static_cast<int64_t>(v + m); // well-defined since C++20.
2018 FLATBUFFERS_CHECKED_ERROR ValidateValue(int64_t *ev, bool next) {
2020 switch (enum_def.underlying_type.base_type) {
2021 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
2022 PTYPE, RTYPE, KTYPE) \
2023 case BASE_TYPE_##ENUM: { \
2024 if (!IsInteger(BASE_TYPE_##ENUM)) break; \
2025 return ValidateImpl<BASE_TYPE_##ENUM, CTYPE>(ev, next ? 1 : 0); \
2027 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
2028 #undef FLATBUFFERS_TD
2032 return parser.Error("fatal: invalid enum underlying type");
2035 EnumValBuilder(Parser &_parser, EnumDef &_enum_def, bool strict_order = true)
2037 enum_def(_enum_def),
2039 strict_ascending(strict_order),
2040 user_value(false) {}
2042 ~EnumValBuilder() { delete temp; }
2047 const bool strict_ascending;
2051 CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest) {
2052 std::vector<std::string> enum_comment = doc_comment_;
2054 std::string enum_name = attribute_;
2055 EXPECT(kTokenIdentifier);
2057 ECHECK(StartEnum(enum_name, is_union, &enum_def));
2058 enum_def->doc_comment = enum_comment;
2059 if (!is_union && !opts.proto_mode) {
2060 // Give specialized error message, since this type spec used to
2061 // be optional in the first FlatBuffers release.
2064 "must specify the underlying integer type for this"
2065 " enum (e.g. \': short\', which was the default).");
2069 // Specify the integer type underlying this enum.
2070 ECHECK(ParseType(enum_def->underlying_type));
2071 if (!IsInteger(enum_def->underlying_type.base_type) ||
2072 IsBool(enum_def->underlying_type.base_type))
2073 return Error("underlying enum type must be integral");
2074 // Make this type refer back to the enum it was derived from.
2075 enum_def->underlying_type.enum_def = enum_def;
2077 ECHECK(ParseMetaData(&enum_def->attributes));
2078 const auto underlying_type = enum_def->underlying_type.base_type;
2079 if (enum_def->attributes.Lookup("bit_flags") &&
2080 !IsUnsigned(underlying_type)) {
2081 // todo: Convert to the Error in the future?
2082 Warning("underlying type of bit_flags enum must be unsigned");
2084 // Protobuf allows them to be specified in any order, so sort afterwards.
2085 const auto strict_ascending = (false == opts.proto_mode);
2086 EnumValBuilder evb(*this, *enum_def, strict_ascending);
2088 // A lot of code generatos expect that an enum is not-empty.
2089 if ((is_union || Is('}')) && !opts.proto_mode) {
2090 evb.CreateEnumerator("NONE");
2091 ECHECK(evb.AcceptEnumerator());
2093 std::set<std::pair<BaseType, StructDef *>> union_types;
2095 if (opts.proto_mode && attribute_ == "option") {
2096 ECHECK(ParseProtoOption());
2098 auto &ev = *evb.CreateEnumerator(attribute_);
2099 auto full_name = ev.name;
2100 ev.doc_comment = doc_comment_;
2101 EXPECT(kTokenIdentifier);
2103 ECHECK(ParseNamespacing(&full_name, &ev.name));
2104 if (opts.union_value_namespacing) {
2105 // Since we can't namespace the actual enum identifiers, turn
2106 // namespace parts into part of the identifier.
2107 ev.name = full_name;
2108 std::replace(ev.name.begin(), ev.name.end(), '.', '_');
2112 ECHECK(ParseType(ev.union_type));
2113 if (ev.union_type.base_type != BASE_TYPE_STRUCT &&
2114 ev.union_type.base_type != BASE_TYPE_STRING)
2115 return Error("union value type may only be table/struct/string");
2117 ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name));
2119 if (!enum_def->uses_multiple_type_instances) {
2120 auto ins = union_types.insert(std::make_pair(
2121 ev.union_type.base_type, ev.union_type.struct_def));
2122 enum_def->uses_multiple_type_instances = (false == ins.second);
2128 ECHECK(evb.AssignEnumeratorValue(attribute_));
2129 EXPECT(kTokenIntegerConstant);
2130 } else if (false == strict_ascending) {
2131 // The opts.proto_mode flag is active.
2132 return Error("Protobuf mode doesn't allow implicit enum values.");
2135 ECHECK(evb.AcceptEnumerator());
2137 if (opts.proto_mode && Is('[')) {
2139 // ignore attributes on enums.
2140 while (token_ != ']') NEXT();
2144 if (!Is(opts.proto_mode ? ';' : ',')) break;
2149 // At this point, the enum can be empty if input is invalid proto-file.
2150 if (!enum_def->size())
2151 return Error("incomplete enum declaration, values not found");
2153 if (enum_def->attributes.Lookup("bit_flags")) {
2154 const auto base_width = static_cast<uint64_t>(8 * SizeOf(underlying_type));
2155 for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
2158 const auto u = ev->GetAsUInt64();
2159 // Stop manipulations with the sign.
2160 if (!IsUnsigned(underlying_type) && u == (base_width - 1))
2161 return Error("underlying type of bit_flags enum must be unsigned");
2162 if (u >= base_width)
2163 return Error("bit flag out of range of underlying integral type");
2164 enum_def->ChangeEnumValue(ev, 1ULL << u);
2168 if (false == strict_ascending)
2169 enum_def->SortByValue(); // Must be sorted to use MinValue/MaxValue.
2171 if (dest) *dest = enum_def;
2172 types_.Add(current_namespace_->GetFullyQualifiedName(enum_def->name),
2173 new Type(BASE_TYPE_UNION, nullptr, enum_def));
2177 CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) {
2178 auto &struct_def = *LookupCreateStruct(name, true, true);
2179 if (!struct_def.predecl) return Error("datatype already exists: " + name);
2180 struct_def.predecl = false;
2181 struct_def.name = name;
2182 struct_def.file = file_being_parsed_;
2183 // Move this struct to the back of the vector just in case it was predeclared,
2184 // to preserve declaration order.
2185 *std::remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) =
2187 *dest = &struct_def;
2191 CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
2192 StructDef *struct_def, const char *suffix,
2193 BaseType basetype) {
2194 auto len = strlen(suffix);
2195 for (auto it = fields.begin(); it != fields.end(); ++it) {
2196 auto &fname = (*it)->name;
2197 if (fname.length() > len &&
2198 fname.compare(fname.length() - len, len, suffix) == 0 &&
2199 (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
2201 struct_def->fields.Lookup(fname.substr(0, fname.length() - len));
2202 if (field && field->value.type.base_type == basetype)
2203 return Error("Field " + fname +
2204 " would clash with generated functions for field " +
2211 bool Parser::SupportsAdvancedUnionFeatures() const {
2212 return opts.lang_to_generate != 0 &&
2213 (opts.lang_to_generate &
2214 ~(IDLOptions::kCpp | IDLOptions::kJs | IDLOptions::kTs |
2215 IDLOptions::kPhp | IDLOptions::kJava | IDLOptions::kCSharp |
2216 IDLOptions::kKotlin | IDLOptions::kBinary)) == 0;
2219 bool Parser::SupportsAdvancedArrayFeatures() const {
2220 return (opts.lang_to_generate &
2221 ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
2222 IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
2223 IDLOptions::kBinary)) == 0;
2226 Namespace *Parser::UniqueNamespace(Namespace *ns) {
2227 for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
2228 if (ns->components == (*it)->components) {
2233 namespaces_.push_back(ns);
2237 std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
2238 Namespace *ns = new Namespace();
2240 std::size_t current, previous = 0;
2241 current = full_qualified_name.find('.');
2242 while (current != std::string::npos) {
2243 ns->components.push_back(
2244 full_qualified_name.substr(previous, current - previous));
2245 previous = current + 1;
2246 current = full_qualified_name.find('.', previous);
2248 current_namespace_ = UniqueNamespace(ns);
2249 return full_qualified_name.substr(previous, current - previous);
2252 static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
2253 auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
2254 auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
2258 CheckedError Parser::ParseDecl() {
2259 std::vector<std::string> dc = doc_comment_;
2260 bool fixed = IsIdent("struct");
2261 if (!fixed && !IsIdent("table")) return Error("declaration expected");
2263 std::string name = attribute_;
2264 EXPECT(kTokenIdentifier);
2265 StructDef *struct_def;
2266 ECHECK(StartStruct(name, &struct_def));
2267 struct_def->doc_comment = dc;
2268 struct_def->fixed = fixed;
2269 ECHECK(ParseMetaData(&struct_def->attributes));
2270 struct_def->sortbysize =
2271 struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
2273 while (token_ != '}') ECHECK(ParseField(*struct_def));
2274 auto force_align = struct_def->attributes.Lookup("force_align");
2277 auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
2278 if (force_align->type.base_type != BASE_TYPE_INT ||
2279 align < struct_def->minalign || align > FLATBUFFERS_MAX_ALIGNMENT ||
2280 align & (align - 1))
2282 "force_align must be a power of two integer ranging from the"
2283 "struct\'s natural alignment to " +
2284 NumToString(FLATBUFFERS_MAX_ALIGNMENT));
2285 struct_def->minalign = align;
2287 if (!struct_def->bytesize) return Error("size 0 structs not allowed");
2289 struct_def->PadLastField(struct_def->minalign);
2290 // Check if this is a table that has manual id assignments
2291 auto &fields = struct_def->fields.vec;
2292 if (!fixed && fields.size()) {
2293 size_t num_id_fields = 0;
2294 for (auto it = fields.begin(); it != fields.end(); ++it) {
2295 if ((*it)->attributes.Lookup("id")) num_id_fields++;
2297 // If any fields have ids..
2298 if (num_id_fields) {
2299 // Then all fields must have them.
2300 if (num_id_fields != fields.size())
2302 "either all fields or no fields must have an 'id' attribute");
2303 // Simply sort by id, then the fields are the same as if no ids had
2305 std::sort(fields.begin(), fields.end(), compareFieldDefs);
2306 // Verify we have a contiguous set, and reassign vtable offsets.
2307 for (int i = 0; i < static_cast<int>(fields.size()); i++) {
2308 if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
2309 return Error("field id\'s must be consecutive from 0, id " +
2310 NumToString(i) + " missing or set twice");
2311 fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
2317 CheckClash(fields, struct_def, UnionTypeFieldSuffix(), BASE_TYPE_UNION));
2318 ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
2319 ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
2320 ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
2321 ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
2322 ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
2324 types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name),
2325 new Type(BASE_TYPE_STRUCT, struct_def, nullptr));
2329 CheckedError Parser::ParseService() {
2330 std::vector<std::string> service_comment = doc_comment_;
2332 auto service_name = attribute_;
2333 EXPECT(kTokenIdentifier);
2334 auto &service_def = *new ServiceDef();
2335 service_def.name = service_name;
2336 service_def.file = file_being_parsed_;
2337 service_def.doc_comment = service_comment;
2338 service_def.defined_namespace = current_namespace_;
2339 if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
2341 return Error("service already exists: " + service_name);
2342 ECHECK(ParseMetaData(&service_def.attributes));
2345 std::vector<std::string> doc_comment = doc_comment_;
2346 auto rpc_name = attribute_;
2347 EXPECT(kTokenIdentifier);
2349 Type reqtype, resptype;
2350 ECHECK(ParseTypeIdent(reqtype));
2353 ECHECK(ParseTypeIdent(resptype));
2354 if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
2355 resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
2356 return Error("rpc request and response types must be tables");
2357 auto &rpc = *new RPCCall();
2358 rpc.name = rpc_name;
2359 rpc.request = reqtype.struct_def;
2360 rpc.response = resptype.struct_def;
2361 rpc.doc_comment = doc_comment;
2362 if (service_def.calls.Add(rpc_name, &rpc))
2363 return Error("rpc already exists: " + rpc_name);
2364 ECHECK(ParseMetaData(&rpc.attributes));
2366 } while (token_ != '}');
2371 bool Parser::SetRootType(const char *name) {
2372 root_struct_def_ = LookupStruct(name);
2373 if (!root_struct_def_)
2375 LookupStruct(current_namespace_->GetFullyQualifiedName(name));
2376 return root_struct_def_ != nullptr;
2379 void Parser::MarkGenerated() {
2380 // This function marks all existing definitions as having already
2381 // been generated, which signals no code for included files should be
2383 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2384 (*it)->generated = true;
2386 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
2387 if (!(*it)->predecl) { (*it)->generated = true; }
2389 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
2390 (*it)->generated = true;
2394 CheckedError Parser::ParseNamespace() {
2396 auto ns = new Namespace();
2397 namespaces_.push_back(ns); // Store it here to not leak upon error.
2398 if (token_ != ';') {
2400 ns->components.push_back(attribute_);
2401 EXPECT(kTokenIdentifier);
2402 if (Is('.')) NEXT() else break;
2405 namespaces_.pop_back();
2406 current_namespace_ = UniqueNamespace(ns);
2411 // Best effort parsing of .proto declarations, with the aim to turn them
2412 // in the closest corresponding FlatBuffer equivalent.
2413 // We parse everything as identifiers instead of keywords, since we don't
2414 // want protobuf keywords to become invalid identifiers in FlatBuffers.
2415 CheckedError Parser::ParseProtoDecl() {
2416 bool isextend = IsIdent("extend");
2417 if (IsIdent("package")) {
2418 // These are identical in syntax to FlatBuffer's namespace decl.
2419 ECHECK(ParseNamespace());
2420 } else if (IsIdent("message") || isextend) {
2421 std::vector<std::string> struct_comment = doc_comment_;
2423 StructDef *struct_def = nullptr;
2424 Namespace *parent_namespace = nullptr;
2426 if (Is('.')) NEXT(); // qualified names may start with a . ?
2427 auto id = attribute_;
2428 EXPECT(kTokenIdentifier);
2429 ECHECK(ParseNamespacing(&id, nullptr));
2430 struct_def = LookupCreateStruct(id, false);
2432 return Error("cannot extend unknown message type: " + id);
2434 std::string name = attribute_;
2435 EXPECT(kTokenIdentifier);
2436 ECHECK(StartStruct(name, &struct_def));
2437 // Since message definitions can be nested, we create a new namespace.
2438 auto ns = new Namespace();
2439 // Copy of current namespace.
2440 *ns = *current_namespace_;
2441 // But with current message name.
2442 ns->components.push_back(name);
2444 parent_namespace = current_namespace_;
2445 current_namespace_ = UniqueNamespace(ns);
2447 struct_def->doc_comment = struct_comment;
2448 ECHECK(ParseProtoFields(struct_def, isextend, false));
2449 if (!isextend) { current_namespace_ = parent_namespace; }
2450 if (Is(';')) NEXT();
2451 } else if (IsIdent("enum")) {
2452 // These are almost the same, just with different terminator:
2454 ECHECK(ParseEnum(false, &enum_def));
2455 if (Is(';')) NEXT();
2456 // Temp: remove any duplicates, as .fbs files can't handle them.
2457 enum_def->RemoveDuplicates();
2458 } else if (IsIdent("syntax")) { // Skip these.
2461 EXPECT(kTokenStringConstant);
2463 } else if (IsIdent("option")) { // Skip these.
2464 ECHECK(ParseProtoOption());
2466 } else if (IsIdent("service")) { // Skip these.
2468 EXPECT(kTokenIdentifier);
2469 ECHECK(ParseProtoCurliesOrIdent());
2471 return Error("don\'t know how to parse .proto declaration starting with " +
2472 TokenToStringId(token_));
2477 CheckedError Parser::StartEnum(const std::string &enum_name, bool is_union,
2479 auto &enum_def = *new EnumDef();
2480 enum_def.name = enum_name;
2481 enum_def.file = file_being_parsed_;
2482 enum_def.doc_comment = doc_comment_;
2483 enum_def.is_union = is_union;
2484 enum_def.defined_namespace = current_namespace_;
2485 if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name),
2487 return Error("enum already exists: " + enum_name);
2488 enum_def.underlying_type.base_type =
2489 is_union ? BASE_TYPE_UTYPE : BASE_TYPE_INT;
2490 enum_def.underlying_type.enum_def = &enum_def;
2491 if (dest) *dest = &enum_def;
2495 CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
2496 bool inside_oneof) {
2498 while (token_ != '}') {
2499 if (IsIdent("message") || IsIdent("extend") || IsIdent("enum")) {
2500 // Nested declarations.
2501 ECHECK(ParseProtoDecl());
2502 } else if (IsIdent("extensions")) { // Skip these.
2504 EXPECT(kTokenIntegerConstant);
2505 if (Is(kTokenIdentifier)) {
2510 } else if (IsIdent("option")) { // Skip these.
2511 ECHECK(ParseProtoOption());
2513 } else if (IsIdent("reserved")) { // Skip these.
2515 while (!Is(';')) { NEXT(); } // A variety of formats, just skip.
2518 std::vector<std::string> field_comment = doc_comment_;
2519 // Parse the qualifier.
2520 bool required = false;
2521 bool repeated = false;
2523 if (!inside_oneof) {
2524 if (IsIdent("optional")) {
2525 // This is the default.
2527 } else if (IsIdent("required")) {
2530 } else if (IsIdent("repeated")) {
2533 } else if (IsIdent("oneof")) {
2537 // can't error, proto3 allows decls without any of the above.
2540 StructDef *anonymous_struct = nullptr;
2541 EnumDef *oneof_union = nullptr;
2543 if (IsIdent("group") || oneof) {
2545 if (oneof && opts.proto_oneof_union) {
2546 auto name = MakeCamel(attribute_, true) + "Union";
2547 ECHECK(StartEnum(name, true, &oneof_union));
2548 type = Type(BASE_TYPE_UNION, nullptr, oneof_union);
2550 auto name = "Anonymous" + NumToString(anonymous_counter++);
2551 ECHECK(StartStruct(name, &anonymous_struct));
2552 type = Type(BASE_TYPE_STRUCT, anonymous_struct);
2555 ECHECK(ParseTypeFromProtoType(&type));
2557 // Repeated elements get mapped to a vector.
2559 type.element = type.base_type;
2560 type.base_type = BASE_TYPE_VECTOR;
2561 if (type.element == BASE_TYPE_VECTOR) {
2562 // We have a vector or vectors, which FlatBuffers doesn't support.
2563 // For now make it a vector of string (since the source is likely
2564 // "repeated bytes").
2565 // TODO(wvo): A better solution would be to wrap this in a table.
2566 type.element = BASE_TYPE_STRING;
2569 std::string name = attribute_;
2570 EXPECT(kTokenIdentifier);
2572 // Parse the field id. Since we're just translating schemas, not
2573 // any kind of binary compatibility, we can safely ignore these, and
2576 EXPECT(kTokenIntegerConstant);
2578 FieldDef *field = nullptr;
2580 // We allow a field to be re-defined when extending.
2581 // TODO: are there situations where that is problematic?
2582 field = struct_def->fields.Lookup(name);
2584 if (!field) ECHECK(AddField(*struct_def, name, type, &field));
2585 field->doc_comment = field_comment;
2586 if (!IsScalar(type.base_type)) field->required = required;
2587 // See if there's a default specified.
2591 auto key = attribute_;
2592 ECHECK(ParseProtoKey());
2594 auto val = attribute_;
2595 ECHECK(ParseProtoCurliesOrIdent());
2596 if (key == "default") {
2597 // Temp: skip non-numeric defaults (enums).
2598 auto numeric = strpbrk(val.c_str(), "0123456789-+.");
2599 if (IsScalar(type.base_type) && numeric == val.c_str())
2600 field->value.constant = val;
2601 } else if (key == "deprecated") {
2602 field->deprecated = val == "true";
2604 if (!Is(',')) break;
2609 if (anonymous_struct) {
2610 ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
2611 if (Is(';')) NEXT();
2612 } else if (oneof_union) {
2613 // Parse into a temporary StructDef, then transfer fields into an
2614 // EnumDef describing the oneof as a union.
2615 StructDef oneof_struct;
2616 ECHECK(ParseProtoFields(&oneof_struct, false, oneof));
2617 if (Is(';')) NEXT();
2618 for (auto field_it = oneof_struct.fields.vec.begin();
2619 field_it != oneof_struct.fields.vec.end(); ++field_it) {
2620 const auto &oneof_field = **field_it;
2621 const auto &oneof_type = oneof_field.value.type;
2622 if (oneof_type.base_type != BASE_TYPE_STRUCT ||
2623 !oneof_type.struct_def || oneof_type.struct_def->fixed)
2624 return Error("oneof '" + name +
2625 "' cannot be mapped to a union because member '" +
2626 oneof_field.name + "' is not a table type.");
2627 EnumValBuilder evb(*this, *oneof_union);
2628 auto ev = evb.CreateEnumerator(oneof_type.struct_def->name);
2629 ev->union_type = oneof_type;
2630 ev->doc_comment = oneof_field.doc_comment;
2631 ECHECK(evb.AcceptEnumerator(oneof_field.name));
2642 CheckedError Parser::ParseProtoKey() {
2643 if (token_ == '(') {
2645 // Skip "(a.b)" style custom attributes.
2646 while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
2650 EXPECT(kTokenIdentifier);
2653 EXPECT(kTokenIdentifier);
2658 CheckedError Parser::ParseProtoCurliesOrIdent() {
2661 for (int nesting = 1; nesting;) {
2664 else if (token_ == '}')
2669 NEXT(); // Any single token.
2674 CheckedError Parser::ParseProtoOption() {
2676 ECHECK(ParseProtoKey());
2678 ECHECK(ParseProtoCurliesOrIdent());
2682 // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
2683 CheckedError Parser::ParseTypeFromProtoType(Type *type) {
2684 struct type_lookup {
2685 const char *proto_type;
2686 BaseType fb_type, element;
2688 static type_lookup lookup[] = {
2689 { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE },
2690 { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE },
2691 { "int32", BASE_TYPE_INT, BASE_TYPE_NONE },
2692 { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2693 { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2694 { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2695 { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE },
2696 { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2697 { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2698 { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2699 { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE },
2700 { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2701 { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE },
2702 { "string", BASE_TYPE_STRING, BASE_TYPE_NONE },
2703 { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR },
2704 { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE }
2706 for (auto tl = lookup; tl->proto_type; tl++) {
2707 if (attribute_ == tl->proto_type) {
2708 type->base_type = tl->fb_type;
2709 type->element = tl->element;
2714 if (Is('.')) NEXT(); // qualified names may start with a . ?
2715 ECHECK(ParseTypeIdent(*type));
2719 CheckedError Parser::SkipAnyJsonValue() {
2722 size_t fieldn_outer = 0;
2723 return ParseTableDelimiters(
2724 fieldn_outer, nullptr,
2725 [&](const std::string &, size_t &fieldn,
2726 const StructDef *) -> CheckedError {
2727 ECHECK(Recurse([&]() { return SkipAnyJsonValue(); }));
2733 uoffset_t count = 0;
2734 return ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2735 return Recurse([&]() { return SkipAnyJsonValue(); });
2738 case kTokenStringConstant:
2739 case kTokenIntegerConstant:
2740 case kTokenFloatConstant: NEXT(); break;
2742 if (IsIdent("true") || IsIdent("false") || IsIdent("null")) {
2745 return TokenError();
2750 CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
2753 auto start = builder->StartMap();
2754 size_t fieldn_outer = 0;
2756 ParseTableDelimiters(fieldn_outer, nullptr,
2757 [&](const std::string &name, size_t &fieldn,
2758 const StructDef *) -> CheckedError {
2760 ECHECK(ParseFlexBufferValue(builder));
2765 builder->EndMap(start);
2769 auto start = builder->StartVector();
2770 uoffset_t count = 0;
2771 ECHECK(ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2772 return ParseFlexBufferValue(builder);
2774 builder->EndVector(start, false, false);
2777 case kTokenStringConstant:
2778 builder->String(attribute_);
2779 EXPECT(kTokenStringConstant);
2781 case kTokenIntegerConstant:
2782 builder->Int(StringToInt(attribute_.c_str()));
2783 EXPECT(kTokenIntegerConstant);
2785 case kTokenFloatConstant:
2786 builder->Double(strtod(attribute_.c_str(), nullptr));
2787 EXPECT(kTokenFloatConstant);
2790 if (IsIdent("true")) {
2791 builder->Bool(true);
2793 } else if (IsIdent("false")) {
2794 builder->Bool(false);
2796 } else if (IsIdent("null")) {
2800 return TokenError();
2805 bool Parser::ParseFlexBuffer(const char *source, const char *source_filename,
2806 flexbuffers::Builder *builder) {
2807 auto ok = !StartParseFile(source, source_filename).Check() &&
2808 !ParseFlexBufferValue(builder).Check();
2809 if (ok) builder->Finish();
2813 bool Parser::Parse(const char *source, const char **include_paths,
2814 const char *source_filename) {
2815 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2818 if (opts.use_flexbuffers) {
2819 r = ParseFlexBuffer(source, source_filename, &flex_builder_);
2821 r = !ParseRoot(source, include_paths, source_filename).Check();
2823 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2827 CheckedError Parser::StartParseFile(const char *source,
2828 const char *source_filename) {
2829 file_being_parsed_ = source_filename ? source_filename : "";
2831 ResetState(source_);
2833 ECHECK(SkipByteOrderMark());
2835 if (Is(kTokenEof)) return Error("input file is empty");
2839 CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
2840 const char *source_filename) {
2841 ECHECK(DoParse(source, include_paths, source_filename, nullptr));
2843 // Check that all types were defined.
2844 for (auto it = structs_.vec.begin(); it != structs_.vec.end();) {
2845 auto &struct_def = **it;
2846 if (struct_def.predecl) {
2847 if (opts.proto_mode) {
2848 // Protos allow enums to be used before declaration, so check if that
2849 // is the case here.
2850 EnumDef *enum_def = nullptr;
2851 for (size_t components =
2852 struct_def.defined_namespace->components.size() + 1;
2853 components && !enum_def; components--) {
2854 auto qualified_name =
2855 struct_def.defined_namespace->GetFullyQualifiedName(
2856 struct_def.name, components - 1);
2857 enum_def = LookupEnum(qualified_name);
2860 // This is pretty slow, but a simple solution for now.
2861 auto initial_count = struct_def.refcount;
2862 for (auto struct_it = structs_.vec.begin();
2863 struct_it != structs_.vec.end(); ++struct_it) {
2864 auto &sd = **struct_it;
2865 for (auto field_it = sd.fields.vec.begin();
2866 field_it != sd.fields.vec.end(); ++field_it) {
2867 auto &field = **field_it;
2868 if (field.value.type.struct_def == &struct_def) {
2869 field.value.type.struct_def = nullptr;
2870 field.value.type.enum_def = enum_def;
2871 auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
2872 ? field.value.type.element
2873 : field.value.type.base_type;
2874 FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
2875 bt = enum_def->underlying_type.base_type;
2876 struct_def.refcount--;
2877 enum_def->refcount++;
2881 if (struct_def.refcount)
2882 return Error("internal: " + NumToString(struct_def.refcount) + "/" +
2883 NumToString(initial_count) +
2884 " use(s) of pre-declaration enum not accounted for: " +
2886 structs_.dict.erase(structs_.dict.find(struct_def.name));
2887 it = structs_.vec.erase(it);
2889 continue; // Skip error.
2892 auto err = "type referenced but not defined (check namespace): " +
2894 if (struct_def.original_location)
2895 err += ", originally at: " + *struct_def.original_location;
2901 // This check has to happen here and not earlier, because only now do we
2902 // know for sure what the type of these are.
2903 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2904 auto &enum_def = **it;
2905 if (enum_def.is_union) {
2906 for (auto val_it = enum_def.Vals().begin();
2907 val_it != enum_def.Vals().end(); ++val_it) {
2908 auto &val = **val_it;
2909 if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def &&
2910 val.union_type.struct_def->fixed)
2912 "only tables can be union elements in the generated language: " +
2920 CheckedError Parser::DoParse(const char *source, const char **include_paths,
2921 const char *source_filename,
2922 const char *include_filename) {
2923 if (source_filename) {
2924 if (included_files_.find(source_filename) == included_files_.end()) {
2925 included_files_[source_filename] =
2926 include_filename ? include_filename : "";
2927 files_included_per_file_[source_filename] = std::set<std::string>();
2932 if (!include_paths) {
2933 static const char *current_directory[] = { "", nullptr };
2934 include_paths = current_directory;
2936 field_stack_.clear();
2938 // Start with a blank namespace just in case this file doesn't have one.
2939 current_namespace_ = empty_namespace_;
2941 ECHECK(StartParseFile(source, source_filename));
2943 // Includes must come before type declarations:
2945 // Parse pre-include proto statements if any:
2946 if (opts.proto_mode && (attribute_ == "option" || attribute_ == "syntax" ||
2947 attribute_ == "package")) {
2948 ECHECK(ParseProtoDecl());
2949 } else if (IsIdent("native_include")) {
2951 vector_emplace_back(&native_included_files_, attribute_);
2952 EXPECT(kTokenStringConstant);
2954 } else if (IsIdent("include") || (opts.proto_mode && IsIdent("import"))) {
2956 if (opts.proto_mode && attribute_ == "public") NEXT();
2957 auto name = flatbuffers::PosixPath(attribute_.c_str());
2958 EXPECT(kTokenStringConstant);
2959 // Look for the file in include_paths.
2960 std::string filepath;
2961 for (auto paths = include_paths; paths && *paths; paths++) {
2962 filepath = flatbuffers::ConCatPathFileName(*paths, name);
2963 if (FileExists(filepath.c_str())) break;
2965 if (filepath.empty())
2966 return Error("unable to locate include file: " + name);
2967 if (source_filename)
2968 files_included_per_file_[source_filename].insert(filepath);
2969 if (included_files_.find(filepath) == included_files_.end()) {
2970 // We found an include file that we have not parsed yet.
2971 // Load it and parse it.
2972 std::string contents;
2973 if (!LoadFile(filepath.c_str(), true, &contents))
2974 return Error("unable to load include file: " + name);
2975 ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
2977 // We generally do not want to output code for any included files:
2978 if (!opts.generate_all) MarkGenerated();
2979 // Reset these just in case the included file had them, and the
2981 root_struct_def_ = nullptr;
2982 file_identifier_.clear();
2983 file_extension_.clear();
2984 // This is the easiest way to continue this file after an include:
2985 // instead of saving and restoring all the state, we simply start the
2986 // file anew. This will cause it to encounter the same include
2987 // statement again, but this time it will skip it, because it was
2988 // entered into included_files_.
2989 // This is recursive, but only go as deep as the number of include
2991 if (source_filename) { included_files_.erase(source_filename); }
2992 return DoParse(source, include_paths, source_filename,
3000 // Now parse all other kinds of declarations:
3001 while (token_ != kTokenEof) {
3002 if (opts.proto_mode) {
3003 ECHECK(ParseProtoDecl());
3004 } else if (IsIdent("namespace")) {
3005 ECHECK(ParseNamespace());
3006 } else if (token_ == '{') {
3007 if (!root_struct_def_)
3008 return Error("no root type set to parse json with");
3009 if (builder_.GetSize()) {
3010 return Error("cannot have more than one json object in a file");
3013 ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
3014 if (opts.size_prefixed) {
3015 builder_.FinishSizePrefixed(
3016 Offset<Table>(toff),
3017 file_identifier_.length() ? file_identifier_.c_str() : nullptr);
3019 builder_.Finish(Offset<Table>(toff), file_identifier_.length()
3020 ? file_identifier_.c_str()
3023 // Check that JSON file doesn't contain more objects or IDL directives.
3024 // Comments after JSON are allowed.
3026 } else if (IsIdent("enum")) {
3027 ECHECK(ParseEnum(false, nullptr));
3028 } else if (IsIdent("union")) {
3029 ECHECK(ParseEnum(true, nullptr));
3030 } else if (IsIdent("root_type")) {
3032 auto root_type = attribute_;
3033 EXPECT(kTokenIdentifier);
3034 ECHECK(ParseNamespacing(&root_type, nullptr));
3035 if (opts.root_type.empty()) {
3036 if (!SetRootType(root_type.c_str()))
3037 return Error("unknown root type: " + root_type);
3038 if (root_struct_def_->fixed) return Error("root type must be a table");
3041 } else if (IsIdent("file_identifier")) {
3043 file_identifier_ = attribute_;
3044 EXPECT(kTokenStringConstant);
3045 if (file_identifier_.length() != FlatBufferBuilder::kFileIdentifierLength)
3046 return Error("file_identifier must be exactly " +
3047 NumToString(FlatBufferBuilder::kFileIdentifierLength) +
3050 } else if (IsIdent("file_extension")) {
3052 file_extension_ = attribute_;
3053 EXPECT(kTokenStringConstant);
3055 } else if (IsIdent("include")) {
3056 return Error("includes must come before declarations");
3057 } else if (IsIdent("attribute")) {
3059 auto name = attribute_;
3060 if (Is(kTokenIdentifier)) {
3063 EXPECT(kTokenStringConstant);
3066 known_attributes_[name] = false;
3067 } else if (IsIdent("rpc_service")) {
3068 ECHECK(ParseService());
3070 ECHECK(ParseDecl());
3076 std::set<std::string> Parser::GetIncludedFilesRecursive(
3077 const std::string &file_name) const {
3078 std::set<std::string> included_files;
3079 std::list<std::string> to_process;
3081 if (file_name.empty()) return included_files;
3082 to_process.push_back(file_name);
3084 while (!to_process.empty()) {
3085 std::string current = to_process.front();
3086 to_process.pop_front();
3087 included_files.insert(current);
3089 // Workaround the lack of const accessor in C++98 maps.
3091 (*const_cast<std::map<std::string, std::set<std::string>> *>(
3092 &files_included_per_file_))[current];
3093 for (auto it = new_files.begin(); it != new_files.end(); ++it) {
3094 if (included_files.find(*it) == included_files.end())
3095 to_process.push_back(*it);
3099 return included_files;
3102 // Schema serialization functionality:
3104 template<typename T> bool compareName(const T *a, const T *b) {
3105 return a->defined_namespace->GetFullyQualifiedName(a->name) <
3106 b->defined_namespace->GetFullyQualifiedName(b->name);
3109 template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
3110 // Pre-sort these vectors, such that we can set the correct indices for them.
3112 std::sort(vec.begin(), vec.end(), compareName<T>);
3113 for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
3116 void Parser::Serialize() {
3118 AssignIndices(structs_.vec);
3119 AssignIndices(enums_.vec);
3120 std::vector<Offset<reflection::Object>> object_offsets;
3121 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
3122 auto offset = (*it)->Serialize(&builder_, *this);
3123 object_offsets.push_back(offset);
3124 (*it)->serialized_location = offset.o;
3126 std::vector<Offset<reflection::Enum>> enum_offsets;
3127 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3128 auto offset = (*it)->Serialize(&builder_, *this);
3129 enum_offsets.push_back(offset);
3130 (*it)->serialized_location = offset.o;
3132 std::vector<Offset<reflection::Service>> service_offsets;
3133 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
3134 auto offset = (*it)->Serialize(&builder_, *this);
3135 service_offsets.push_back(offset);
3136 (*it)->serialized_location = offset.o;
3138 auto objs__ = builder_.CreateVectorOfSortedTables(&object_offsets);
3139 auto enum__ = builder_.CreateVectorOfSortedTables(&enum_offsets);
3140 auto fiid__ = builder_.CreateString(file_identifier_);
3141 auto fext__ = builder_.CreateString(file_extension_);
3142 auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
3143 auto schema_offset = reflection::CreateSchema(
3144 builder_, objs__, enum__, fiid__, fext__,
3145 (root_struct_def_ ? root_struct_def_->serialized_location : 0), serv__);
3146 if (opts.size_prefixed) {
3147 builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
3149 builder_.Finish(schema_offset, reflection::SchemaIdentifier());
3153 static Namespace *GetNamespace(
3154 const std::string &qualified_name, std::vector<Namespace *> &namespaces,
3155 std::map<std::string, Namespace *> &namespaces_index) {
3156 size_t dot = qualified_name.find_last_of('.');
3157 std::string namespace_name = (dot != std::string::npos)
3158 ? std::string(qualified_name.c_str(), dot)
3160 Namespace *&ns = namespaces_index[namespace_name];
3163 ns = new Namespace();
3164 namespaces.push_back(ns);
3169 dot = qualified_name.find('.', pos);
3170 if (dot == std::string::npos) { break; }
3171 ns->components.push_back(qualified_name.substr(pos, dot - pos));
3179 Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
3180 const Parser &parser) const {
3181 std::vector<Offset<reflection::Field>> field_offsets;
3182 for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
3183 field_offsets.push_back((*it)->Serialize(
3184 builder, static_cast<uint16_t>(it - fields.vec.begin()), parser));
3186 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3187 auto name__ = builder->CreateString(qualified_name);
3188 auto flds__ = builder->CreateVectorOfSortedTables(&field_offsets);
3189 auto attr__ = SerializeAttributes(builder, parser);
3190 auto docs__ = parser.opts.binary_schema_comments
3191 ? builder->CreateVectorOfStrings(doc_comment)
3193 return reflection::CreateObject(*builder, name__, flds__, fixed,
3194 static_cast<int>(minalign),
3195 static_cast<int>(bytesize), attr__, docs__);
3198 bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
3199 if (!DeserializeAttributes(parser, object->attributes())) return false;
3200 DeserializeDoc(doc_comment, object->documentation());
3201 name = parser.UnqualifiedName(object->name()->str());
3203 sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
3204 const auto &of = *(object->fields());
3205 auto indexes = std::vector<uoffset_t>(of.size());
3206 for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
3207 size_t tmp_struct_size = 0;
3208 for (size_t i = 0; i < indexes.size(); i++) {
3209 auto field = of.Get(indexes[i]);
3210 auto field_def = new FieldDef();
3211 if (!field_def->Deserialize(parser, field) ||
3212 fields.Add(field_def->name, field_def)) {
3217 // Recompute padding since that's currently not serialized.
3218 auto size = InlineSize(field_def->value.type);
3220 i + 1 < indexes.size() ? of.Get(indexes[i + 1]) : nullptr;
3221 tmp_struct_size += size;
3222 field_def->padding =
3223 next_field ? (next_field->offset() - field_def->value.offset) - size
3224 : PaddingBytes(tmp_struct_size, minalign);
3225 tmp_struct_size += field_def->padding;
3228 FLATBUFFERS_ASSERT(static_cast<int>(tmp_struct_size) == object->bytesize());
3232 Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
3234 const Parser &parser) const {
3235 auto name__ = builder->CreateString(name);
3236 auto type__ = value.type.Serialize(builder);
3237 auto attr__ = SerializeAttributes(builder, parser);
3238 auto docs__ = parser.opts.binary_schema_comments
3239 ? builder->CreateVectorOfStrings(doc_comment)
3241 return reflection::CreateField(
3242 *builder, name__, type__, id, value.offset,
3243 // Is uint64>max(int64) tested?
3244 IsInteger(value.type.base_type) ? StringToInt(value.constant.c_str()) : 0,
3245 // result may be platform-dependent if underlying is float (not double)
3246 IsFloat(value.type.base_type) ? strtod(value.constant.c_str(), nullptr)
3248 deprecated, required, key, attr__, docs__);
3249 // TODO: value.constant is almost always "0", we could save quite a bit of
3250 // space by sharing it. Same for common values of value.type.
3253 bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
3254 name = field->name()->str();
3255 defined_namespace = parser.current_namespace_;
3256 if (!value.type.Deserialize(parser, field->type())) return false;
3257 value.offset = field->offset();
3258 if (IsInteger(value.type.base_type)) {
3259 value.constant = NumToString(field->default_integer());
3260 } else if (IsFloat(value.type.base_type)) {
3261 value.constant = FloatToString(field->default_real(), 16);
3262 size_t last_zero = value.constant.find_last_not_of('0');
3263 if (last_zero != std::string::npos && last_zero != 0) {
3264 value.constant.erase(last_zero, std::string::npos);
3267 deprecated = field->deprecated();
3268 required = field->required();
3270 if (!DeserializeAttributes(parser, field->attributes())) return false;
3271 // TODO: this should probably be handled by a separate attribute
3272 if (attributes.Lookup("flexbuffer")) {
3274 parser.uses_flexbuffers_ = true;
3275 if (value.type.base_type != BASE_TYPE_VECTOR ||
3276 value.type.element != BASE_TYPE_UCHAR)
3279 if (auto nested = attributes.Lookup("nested_flatbuffer")) {
3280 auto nested_qualified_name =
3281 parser.current_namespace_->GetFullyQualifiedName(nested->constant);
3282 nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
3283 if (!nested_flatbuffer) return false;
3285 DeserializeDoc(doc_comment, field->documentation());
3289 Offset<reflection::RPCCall> RPCCall::Serialize(FlatBufferBuilder *builder,
3290 const Parser &parser) const {
3291 auto name__ = builder->CreateString(name);
3292 auto attr__ = SerializeAttributes(builder, parser);
3293 auto docs__ = parser.opts.binary_schema_comments
3294 ? builder->CreateVectorOfStrings(doc_comment)
3296 return reflection::CreateRPCCall(
3297 *builder, name__, request->serialized_location,
3298 response->serialized_location, attr__, docs__);
3301 bool RPCCall::Deserialize(Parser &parser, const reflection::RPCCall *call) {
3302 name = call->name()->str();
3303 if (!DeserializeAttributes(parser, call->attributes())) return false;
3304 DeserializeDoc(doc_comment, call->documentation());
3305 request = parser.structs_.Lookup(call->request()->name()->str());
3306 response = parser.structs_.Lookup(call->response()->name()->str());
3307 if (!request || !response) { return false; }
3311 Offset<reflection::Service> ServiceDef::Serialize(FlatBufferBuilder *builder,
3312 const Parser &parser) const {
3313 std::vector<Offset<reflection::RPCCall>> servicecall_offsets;
3314 for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) {
3315 servicecall_offsets.push_back((*it)->Serialize(builder, parser));
3317 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3318 auto name__ = builder->CreateString(qualified_name);
3319 auto call__ = builder->CreateVector(servicecall_offsets);
3320 auto attr__ = SerializeAttributes(builder, parser);
3321 auto docs__ = parser.opts.binary_schema_comments
3322 ? builder->CreateVectorOfStrings(doc_comment)
3324 return reflection::CreateService(*builder, name__, call__, attr__, docs__);
3327 bool ServiceDef::Deserialize(Parser &parser,
3328 const reflection::Service *service) {
3329 name = parser.UnqualifiedName(service->name()->str());
3330 if (service->calls()) {
3331 for (uoffset_t i = 0; i < service->calls()->size(); ++i) {
3332 auto call = new RPCCall();
3333 if (!call->Deserialize(parser, service->calls()->Get(i)) ||
3334 calls.Add(call->name, call)) {
3340 if (!DeserializeAttributes(parser, service->attributes())) return false;
3341 DeserializeDoc(doc_comment, service->documentation());
3345 Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
3346 const Parser &parser) const {
3347 std::vector<Offset<reflection::EnumVal>> enumval_offsets;
3348 for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
3349 enumval_offsets.push_back((*it)->Serialize(builder, parser));
3351 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3352 auto name__ = builder->CreateString(qualified_name);
3353 auto vals__ = builder->CreateVector(enumval_offsets);
3354 auto type__ = underlying_type.Serialize(builder);
3355 auto attr__ = SerializeAttributes(builder, parser);
3356 auto docs__ = parser.opts.binary_schema_comments
3357 ? builder->CreateVectorOfStrings(doc_comment)
3359 return reflection::CreateEnum(*builder, name__, vals__, is_union, type__,
3363 bool EnumDef::Deserialize(Parser &parser, const reflection::Enum *_enum) {
3364 name = parser.UnqualifiedName(_enum->name()->str());
3365 for (uoffset_t i = 0; i < _enum->values()->size(); ++i) {
3366 auto val = new EnumVal();
3367 if (!val->Deserialize(parser, _enum->values()->Get(i)) ||
3368 vals.Add(val->name, val)) {
3373 is_union = _enum->is_union();
3374 if (!underlying_type.Deserialize(parser, _enum->underlying_type())) {
3377 if (!DeserializeAttributes(parser, _enum->attributes())) return false;
3378 DeserializeDoc(doc_comment, _enum->documentation());
3382 Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder,
3383 const Parser &parser) const {
3384 auto name__ = builder->CreateString(name);
3385 auto type__ = union_type.Serialize(builder);
3386 auto docs__ = parser.opts.binary_schema_comments
3387 ? builder->CreateVectorOfStrings(doc_comment)
3389 return reflection::CreateEnumVal(
3390 *builder, name__, value,
3391 union_type.struct_def ? union_type.struct_def->serialized_location : 0,
3395 bool EnumVal::Deserialize(const Parser &parser,
3396 const reflection::EnumVal *val) {
3397 name = val->name()->str();
3398 value = val->value();
3399 if (!union_type.Deserialize(parser, val->union_type())) return false;
3400 DeserializeDoc(doc_comment, val->documentation());
3404 Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
3405 return reflection::CreateType(
3406 *builder, static_cast<reflection::BaseType>(base_type),
3407 static_cast<reflection::BaseType>(element),
3408 struct_def ? struct_def->index : (enum_def ? enum_def->index : -1),
3412 bool Type::Deserialize(const Parser &parser, const reflection::Type *type) {
3413 if (type == nullptr) return true;
3414 base_type = static_cast<BaseType>(type->base_type());
3415 element = static_cast<BaseType>(type->element());
3416 fixed_length = type->fixed_length();
3417 if (type->index() >= 0) {
3418 bool is_series = type->base_type() == reflection::Vector ||
3419 type->base_type() == reflection::Array;
3420 if (type->base_type() == reflection::Obj ||
3421 (is_series && type->element() == reflection::Obj)) {
3422 if (static_cast<size_t>(type->index()) < parser.structs_.vec.size()) {
3423 struct_def = parser.structs_.vec[type->index()];
3424 struct_def->refcount++;
3429 if (static_cast<size_t>(type->index()) < parser.enums_.vec.size()) {
3430 enum_def = parser.enums_.vec[type->index()];
3439 flatbuffers::Offset<
3440 flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
3441 Definition::SerializeAttributes(FlatBufferBuilder *builder,
3442 const Parser &parser) const {
3443 std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
3444 for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
3445 auto it = parser.known_attributes_.find(kv->first);
3446 FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
3447 if (parser.opts.binary_schema_builtins || !it->second) {
3448 auto key = builder->CreateString(kv->first);
3449 auto val = builder->CreateString(kv->second->constant);
3450 attrs.push_back(reflection::CreateKeyValue(*builder, key, val));
3454 return builder->CreateVectorOfSortedTables(&attrs);
3460 bool Definition::DeserializeAttributes(
3461 Parser &parser, const Vector<Offset<reflection::KeyValue>> *attrs) {
3462 if (attrs == nullptr) return true;
3463 for (uoffset_t i = 0; i < attrs->size(); ++i) {
3464 auto kv = attrs->Get(i);
3465 auto value = new Value();
3466 if (kv->value()) { value->constant = kv->value()->str(); }
3467 if (attributes.Add(kv->key()->str(), value)) {
3471 parser.known_attributes_[kv->key()->str()];
3476 /************************************************************************/
3477 /* DESERIALIZATION */
3478 /************************************************************************/
3479 bool Parser::Deserialize(const uint8_t *buf, const size_t size) {
3480 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t *>(buf), size);
3481 bool size_prefixed = false;
3482 if (!reflection::SchemaBufferHasIdentifier(buf)) {
3483 if (!flatbuffers::BufferHasIdentifier(buf, reflection::SchemaIdentifier(),
3487 size_prefixed = true;
3489 auto verify_fn = size_prefixed ? &reflection::VerifySizePrefixedSchemaBuffer
3490 : &reflection::VerifySchemaBuffer;
3491 if (!verify_fn(verifier)) { return false; }
3492 auto schema = size_prefixed ? reflection::GetSizePrefixedSchema(buf)
3493 : reflection::GetSchema(buf);
3494 return Deserialize(schema);
3497 bool Parser::Deserialize(const reflection::Schema *schema) {
3498 file_identifier_ = schema->file_ident() ? schema->file_ident()->str() : "";
3499 file_extension_ = schema->file_ext() ? schema->file_ext()->str() : "";
3500 std::map<std::string, Namespace *> namespaces_index;
3502 // Create defs without deserializing so references from fields to structs and
3503 // enums can be resolved.
3504 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3506 auto struct_def = new StructDef();
3507 struct_def->bytesize = it->bytesize();
3508 struct_def->fixed = it->is_struct();
3509 struct_def->minalign = it->minalign();
3510 if (structs_.Add(it->name()->str(), struct_def)) {
3514 auto type = new Type(BASE_TYPE_STRUCT, struct_def, nullptr);
3515 if (types_.Add(it->name()->str(), type)) {
3520 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3521 auto enum_def = new EnumDef();
3522 if (enums_.Add(it->name()->str(), enum_def)) {
3526 auto type = new Type(BASE_TYPE_UNION, nullptr, enum_def);
3527 if (types_.Add(it->name()->str(), type)) {
3533 // Now fields can refer to structs and enums by index.
3534 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3536 std::string qualified_name = it->name()->str();
3537 auto struct_def = structs_.Lookup(qualified_name);
3538 struct_def->defined_namespace =
3539 GetNamespace(qualified_name, namespaces_, namespaces_index);
3540 if (!struct_def->Deserialize(*this, *it)) { return false; }
3541 if (schema->root_table() == *it) { root_struct_def_ = struct_def; }
3543 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3544 std::string qualified_name = it->name()->str();
3545 auto enum_def = enums_.Lookup(qualified_name);
3546 enum_def->defined_namespace =
3547 GetNamespace(qualified_name, namespaces_, namespaces_index);
3548 if (!enum_def->Deserialize(*this, *it)) { return false; }
3551 if (schema->services()) {
3552 for (auto it = schema->services()->begin(); it != schema->services()->end();
3554 std::string qualified_name = it->name()->str();
3555 auto service_def = new ServiceDef();
3556 service_def->defined_namespace =
3557 GetNamespace(qualified_name, namespaces_, namespaces_index);
3558 if (!service_def->Deserialize(*this, *it) ||
3559 services_.Add(qualified_name, service_def)) {
3569 std::string Parser::ConformTo(const Parser &base) {
3570 for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
3571 auto &struct_def = **sit;
3572 auto qualified_name =
3573 struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
3574 auto struct_def_base = base.LookupStruct(qualified_name);
3575 if (!struct_def_base) continue;
3576 for (auto fit = struct_def.fields.vec.begin();
3577 fit != struct_def.fields.vec.end(); ++fit) {
3578 auto &field = **fit;
3579 auto field_base = struct_def_base->fields.Lookup(field.name);
3581 if (field.value.offset != field_base->value.offset)
3582 return "offsets differ for field: " + field.name;
3583 if (field.value.constant != field_base->value.constant)
3584 return "defaults differ for field: " + field.name;
3585 if (!EqualByName(field.value.type, field_base->value.type))
3586 return "types differ for field: " + field.name;
3588 // Doesn't have to exist, deleting fields is fine.
3589 // But we should check if there is a field that has the same offset
3590 // but is incompatible (in the case of field renaming).
3591 for (auto fbit = struct_def_base->fields.vec.begin();
3592 fbit != struct_def_base->fields.vec.end(); ++fbit) {
3594 if (field.value.offset == field_base->value.offset) {
3595 if (!EqualByName(field.value.type, field_base->value.type))
3596 return "field renamed to different type: " + field.name;
3603 for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
3604 auto &enum_def = **eit;
3605 auto qualified_name =
3606 enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
3607 auto enum_def_base = base.enums_.Lookup(qualified_name);
3608 if (!enum_def_base) continue;
3609 for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
3611 auto &enum_val = **evit;
3612 auto enum_val_base = enum_def_base->Lookup(enum_val.name);
3613 if (enum_val_base) {
3614 if (enum_val != *enum_val_base)
3615 return "values differ for enum: " + enum_val.name;
3622 } // namespace flatbuffers