2 * Copyright 2014 Google Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "flatbuffers/idl.h"
25 #include "flatbuffers/util.h"
27 namespace flatbuffers {
29 // Reflects the version at the compiling time of binary(lib/dll/so).
30 const char *FLATBUFFERS_VERSION() {
33 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
34 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
35 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
39 const double kPi = 3.14159265358979323846;
41 const char *const kTypeNames[] = {
43 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
44 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
46 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
52 const char kTypeSizes[] = {
54 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
55 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
57 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
62 // The enums in the reflection schema should match the ones we use internally.
63 // Compare the last element to check if these go out of sync.
64 static_assert(BASE_TYPE_UNION == static_cast<BaseType>(reflection::Union),
67 // Any parsing calls have to be wrapped in this macro, which automates
68 // handling of recursive error checking a bit. It will check the received
69 // CheckedError object, and return straight away on error.
70 #define ECHECK(call) \
73 if (ce.Check()) return ce; \
76 // These two functions are called hundreds of times below, so define a short
78 #define NEXT() ECHECK(Next())
79 #define EXPECT(tok) ECHECK(Expect(tok))
81 static bool ValidateUTF8(const std::string &str) {
82 const char *s = &str[0];
83 const char *const sEnd = s + str.length();
85 if (FromUTF8(&s) < 0) { return false; }
90 // Convert an underscore_based_indentifier in to camelCase.
91 // Also uppercases the first character if first is true.
92 std::string MakeCamel(const std::string &in, bool first) {
94 for (size_t i = 0; i < in.length(); i++) {
96 s += static_cast<char>(toupper(in[0]));
97 else if (in[i] == '_' && i + 1 < in.length())
98 s += static_cast<char>(toupper(in[++i]));
105 // Convert an underscore_based_identifier in to screaming snake case.
106 std::string MakeScreamingCamel(const std::string &in) {
108 for (size_t i = 0; i < in.length(); i++) {
110 s += static_cast<char>(toupper(in[i]));
117 void DeserializeDoc( std::vector<std::string> &doc,
118 const Vector<Offset<String>> *documentation) {
119 if (documentation == nullptr) return;
120 for (uoffset_t index = 0; index < documentation->size(); index++)
121 doc.push_back(documentation->Get(index)->str());
124 void Parser::Message(const std::string &msg) {
125 if (!error_.empty()) error_ += "\n"; // log all warnings and errors
126 error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
129 #ifdef _WIN32 // MSVC alike
131 "(" + NumToString(line_) + ", " + NumToString(CursorPosition()) + ")";
133 if (file_being_parsed_.length()) error_ += ":";
134 error_ += NumToString(line_) + ": " + NumToString(CursorPosition());
137 error_ += ": " + msg;
140 void Parser::Warning(const std::string &msg) { Message("warning: " + msg); }
142 CheckedError Parser::Error(const std::string &msg) {
143 Message("error: " + msg);
144 return CheckedError(true);
147 inline CheckedError NoError() { return CheckedError(false); }
149 CheckedError Parser::RecurseError() {
150 return Error("maximum parsing recursion of " +
151 NumToString(FLATBUFFERS_MAX_PARSING_DEPTH) + " reached");
154 template<typename F> CheckedError Parser::Recurse(F f) {
155 if (recurse_protection_counter >= (FLATBUFFERS_MAX_PARSING_DEPTH))
156 return RecurseError();
157 recurse_protection_counter++;
159 recurse_protection_counter--;
163 template<typename T> std::string TypeToIntervalString() {
164 return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
165 NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
168 // atot: template version of atoi/atof: convert a string to an instance of T.
170 inline CheckedError atot(const char *s, Parser &parser, T *val) {
171 auto done = StringToNumber(s, val);
172 if (done) return NoError();
174 return parser.Error("invalid number: \"" + std::string(s) + "\"");
176 return parser.Error("invalid number: \"" + std::string(s) + "\"" +
177 ", constant does not fit " + TypeToIntervalString<T>());
180 inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
183 *val = Offset<void>(atoi(s));
187 std::string Namespace::GetFullyQualifiedName(const std::string &name,
188 size_t max_components) const {
189 // Early exit if we don't have a defined namespace.
190 if (components.empty() || !max_components) { return name; }
191 std::string stream_str;
192 for (size_t i = 0; i < std::min(components.size(), max_components); i++) {
193 if (i) { stream_str += '.'; }
194 stream_str += std::string(components[i]);
203 // Declare tokens we'll use. Single character tokens are represented by their
204 // ascii character code (e.g. '{'), others above 256.
206 #define FLATBUFFERS_GEN_TOKENS(TD) \
207 TD(Eof, 256, "end of file") \
208 TD(StringConstant, 257, "string constant") \
209 TD(IntegerConstant, 258, "integer constant") \
210 TD(FloatConstant, 259, "float constant") \
211 TD(Identifier, 260, "identifier")
213 __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
216 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
217 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
218 #undef FLATBUFFERS_TOKEN
221 static std::string TokenToString(int t) {
222 static const char * const tokens[] = {
223 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
224 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
225 #undef FLATBUFFERS_TOKEN
226 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
227 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
229 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
230 #undef FLATBUFFERS_TD
232 if (t < 256) { // A single ascii char token.
234 s.append(1, static_cast<char>(t));
236 } else { // Other tokens.
237 return tokens[t - 256];
242 std::string Parser::TokenToStringId(int t) const {
243 return t == kTokenIdentifier ? attribute_ : TokenToString(t);
246 // Parses exactly nibbles worth of hex digits into a number, or error.
247 CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
248 FLATBUFFERS_ASSERT(nibbles > 0);
249 for (int i = 0; i < nibbles; i++)
250 if (!is_xdigit(cursor_[i]))
251 return Error("escape code must be followed by " + NumToString(nibbles) +
253 std::string target(cursor_, cursor_ + nibbles);
254 *val = StringToUInt(target.c_str(), 16);
259 CheckedError Parser::SkipByteOrderMark() {
260 if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
262 if (static_cast<unsigned char>(*cursor_) != 0xbb)
263 return Error("invalid utf-8 byte order mark");
265 if (static_cast<unsigned char>(*cursor_) != 0xbf)
266 return Error("invalid utf-8 byte order mark");
271 static inline bool IsIdentifierStart(char c) {
272 return is_alpha(c) || (c == '_');
275 CheckedError Parser::Next() {
276 doc_comment_.clear();
277 bool seen_newline = cursor_ == source_;
279 attr_is_trivial_ascii_string_ = true;
304 case '=': return NoError();
307 int unicode_high_surrogate = -1;
309 while (*cursor_ != c) {
310 if (*cursor_ < ' ' && static_cast<signed char>(*cursor_) >= 0)
311 return Error("illegal character in string constant");
312 if (*cursor_ == '\\') {
313 attr_is_trivial_ascii_string_ = false; // has escape sequence
315 if (unicode_high_surrogate != -1 && *cursor_ != 'u') {
317 "illegal Unicode sequence (unpaired high surrogate)");
356 case 'x': { // Not in the JSON standard
359 ECHECK(ParseHexNum(2, &val));
360 attribute_ += static_cast<char>(val);
366 ECHECK(ParseHexNum(4, &val));
367 if (val >= 0xD800 && val <= 0xDBFF) {
368 if (unicode_high_surrogate != -1) {
370 "illegal Unicode sequence (multiple high surrogates)");
372 unicode_high_surrogate = static_cast<int>(val);
374 } else if (val >= 0xDC00 && val <= 0xDFFF) {
375 if (unicode_high_surrogate == -1) {
377 "illegal Unicode sequence (unpaired low surrogate)");
379 int code_point = 0x10000 +
380 ((unicode_high_surrogate & 0x03FF) << 10) +
382 ToUTF8(code_point, &attribute_);
383 unicode_high_surrogate = -1;
386 if (unicode_high_surrogate != -1) {
388 "illegal Unicode sequence (unpaired high surrogate)");
390 ToUTF8(static_cast<int>(val), &attribute_);
394 default: return Error("unknown escape code in string constant");
396 } else { // printable chars + UTF-8 bytes
397 if (unicode_high_surrogate != -1) {
399 "illegal Unicode sequence (unpaired high surrogate)");
401 // reset if non-printable
402 attr_is_trivial_ascii_string_ &= check_ascii_range(*cursor_, ' ', '~');
404 attribute_ += *cursor_++;
407 if (unicode_high_surrogate != -1) {
408 return Error("illegal Unicode sequence (unpaired high surrogate)");
411 if (!attr_is_trivial_ascii_string_ && !opts.allow_non_utf8 &&
412 !ValidateUTF8(attribute_)) {
413 return Error("illegal UTF-8 sequence");
415 token_ = kTokenStringConstant;
419 if (*cursor_ == '/') {
420 const char *start = ++cursor_;
421 while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++;
422 if (*start == '/') { // documentation comment
425 "a documentation comment should be on a line on its own");
426 doc_comment_.push_back(std::string(start + 1, cursor_));
429 } else if (*cursor_ == '*') {
431 // TODO: make nested.
432 while (*cursor_ != '*' || cursor_[1] != '/') {
433 if (*cursor_ == '\n') MarkNewLine();
434 if (!*cursor_) return Error("end of file in comment");
440 FLATBUFFERS_FALLTHROUGH(); // else fall thru
442 const auto has_sign = (c == '+') || (c == '-');
443 // '-'/'+' and following identifier - can be a predefined constant like:
444 // NAN, INF, PI, etc.
445 if (IsIdentifierStart(c) || (has_sign && IsIdentifierStart(*cursor_))) {
446 // Collect all chars of an identifier:
447 const char *start = cursor_ - 1;
448 while (IsIdentifierStart(*cursor_) || is_digit(*cursor_)) cursor_++;
449 attribute_.append(start, cursor_);
450 token_ = has_sign ? kTokenStringConstant : kTokenIdentifier;
454 auto dot_lvl = (c == '.') ? 0 : 1; // dot_lvl==0 <=> exactly one '.' seen
455 if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum?
456 // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4).
457 if (is_digit(c) || has_sign || !dot_lvl) {
458 const auto start = cursor_ - 1;
459 auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1;
460 if (!is_digit(c) && is_digit(*cursor_)){
461 start_digits = cursor_; // see digit in cursor_ position
464 // hex-float can't begind with '.'
465 auto use_hex = dot_lvl && (c == '0') && is_alpha_char(*cursor_, 'X');
466 if (use_hex) start_digits = ++cursor_; // '0x' is the prefix, skip it
467 // Read an integer number or mantisa of float-point number.
470 while (is_xdigit(*cursor_)) cursor_++;
472 while (is_digit(*cursor_)) cursor_++;
474 } while ((*cursor_ == '.') && (++cursor_) && (--dot_lvl >= 0));
475 // Exponent of float-point number.
476 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
477 // The exponent suffix of hexadecimal float number is mandatory.
478 if (use_hex && !dot_lvl) start_digits = cursor_;
479 if ((use_hex && is_alpha_char(*cursor_, 'P')) ||
480 is_alpha_char(*cursor_, 'E')) {
481 dot_lvl = 0; // Emulate dot to signal about float-point number.
483 if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
484 start_digits = cursor_; // the exponent-part has to have digits
485 // Exponent is decimal integer number
486 while (is_digit(*cursor_)) cursor_++;
487 if (*cursor_ == '.') {
488 cursor_++; // If see a dot treat it as part of invalid number.
489 dot_lvl = -1; // Fall thru to Error().
494 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
495 attribute_.append(start, cursor_);
496 token_ = dot_lvl ? kTokenIntegerConstant : kTokenFloatConstant;
499 return Error("invalid number: " + std::string(start, cursor_));
504 if (false == check_ascii_range(c, ' ', '~')) ch = "code: " + NumToString(c);
505 return Error("illegal character: " + ch);
510 // Check if a given token is next.
511 bool Parser::Is(int t) const { return t == token_; }
513 bool Parser::IsIdent(const char *id) const {
514 return token_ == kTokenIdentifier && attribute_ == id;
517 // Expect a given token to be next, consume it, or error if not present.
518 CheckedError Parser::Expect(int t) {
520 return Error("expecting: " + TokenToString(t) +
521 " instead got: " + TokenToStringId(token_));
527 CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
532 if (last) *last = attribute_;
533 EXPECT(kTokenIdentifier);
538 EnumDef *Parser::LookupEnum(const std::string &id) {
539 // Search thru parent namespaces.
540 for (int components = static_cast<int>(current_namespace_->components.size());
541 components >= 0; components--) {
542 auto ed = enums_.Lookup(
543 current_namespace_->GetFullyQualifiedName(id, components));
549 StructDef *Parser::LookupStruct(const std::string &id) const {
550 auto sd = structs_.Lookup(id);
551 if (sd) sd->refcount++;
555 CheckedError Parser::ParseTypeIdent(Type &type) {
556 std::string id = attribute_;
557 EXPECT(kTokenIdentifier);
558 ECHECK(ParseNamespacing(&id, nullptr));
559 auto enum_def = LookupEnum(id);
561 type = enum_def->underlying_type;
562 if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
564 type.base_type = BASE_TYPE_STRUCT;
565 type.struct_def = LookupCreateStruct(id);
570 // Parse any IDL type.
571 CheckedError Parser::ParseType(Type &type) {
572 if (token_ == kTokenIdentifier) {
573 if (IsIdent("bool")) {
574 type.base_type = BASE_TYPE_BOOL;
576 } else if (IsIdent("byte") || IsIdent("int8")) {
577 type.base_type = BASE_TYPE_CHAR;
579 } else if (IsIdent("ubyte") || IsIdent("uint8")) {
580 type.base_type = BASE_TYPE_UCHAR;
582 } else if (IsIdent("short") || IsIdent("int16")) {
583 type.base_type = BASE_TYPE_SHORT;
585 } else if (IsIdent("ushort") || IsIdent("uint16")) {
586 type.base_type = BASE_TYPE_USHORT;
588 } else if (IsIdent("int") || IsIdent("int32")) {
589 type.base_type = BASE_TYPE_INT;
591 } else if (IsIdent("uint") || IsIdent("uint32")) {
592 type.base_type = BASE_TYPE_UINT;
594 } else if (IsIdent("long") || IsIdent("int64")) {
595 type.base_type = BASE_TYPE_LONG;
597 } else if (IsIdent("ulong") || IsIdent("uint64")) {
598 type.base_type = BASE_TYPE_ULONG;
600 } else if (IsIdent("float") || IsIdent("float32")) {
601 type.base_type = BASE_TYPE_FLOAT;
603 } else if (IsIdent("double") || IsIdent("float64")) {
604 type.base_type = BASE_TYPE_DOUBLE;
606 } else if (IsIdent("string")) {
607 type.base_type = BASE_TYPE_STRING;
610 ECHECK(ParseTypeIdent(type));
612 } else if (token_ == '[') {
615 ECHECK(Recurse([&]() { return ParseType(subtype); }));
616 if (IsSeries(subtype)) {
617 // We could support this, but it will complicate things, and it's
618 // easier to work around with a struct around the inner vector.
619 return Error("nested vector types not supported (wrap in table first)");
623 if (token_ != kTokenIntegerConstant) {
624 return Error("length of fixed-length array must be an integer value");
626 uint16_t fixed_length = 0;
627 bool check = StringToNumber(attribute_.c_str(), &fixed_length);
628 if (!check || fixed_length < 1) {
630 "length of fixed-length array must be positive and fit to "
633 // Check if enum arrays are used in C++ without specifying --scoped-enums
634 if ((opts.lang_to_generate & IDLOptions::kCpp) && !opts.scoped_enums &&
637 "--scoped-enums must be enabled to use enum arrays in C++\n");
639 type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def,
643 type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
645 type.element = subtype.base_type;
648 return Error("illegal type syntax");
653 CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
654 const Type &type, FieldDef **dest) {
655 auto &field = *new FieldDef();
657 FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
659 field.file = struct_def.file;
660 field.value.type = type;
661 if (struct_def.fixed) { // statically compute the field offset
662 auto size = InlineSize(type);
663 auto alignment = InlineAlignment(type);
664 // structs_ need to have a predictable format, so we need to align to
665 // the largest scalar
666 struct_def.minalign = std::max(struct_def.minalign, alignment);
667 struct_def.PadLastField(alignment);
668 field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
669 struct_def.bytesize += size;
671 if (struct_def.fields.Add(name, &field))
672 return Error("field already exists: " + name);
677 CheckedError Parser::ParseField(StructDef &struct_def) {
678 std::string name = attribute_;
680 if (LookupCreateStruct(name, false, false))
681 return Error("field name can not be the same as table/struct name");
683 std::vector<std::string> dc = doc_comment_;
684 EXPECT(kTokenIdentifier);
687 ECHECK(ParseType(type));
689 if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type) &&
691 return Error("structs_ may contain only scalar or struct fields");
693 if (!struct_def.fixed && IsArray(type))
694 return Error("fixed-length array in table must be wrapped in struct");
696 if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
698 "Arrays are not yet supported in all "
699 "the specified programming languages.");
702 FieldDef *typefield = nullptr;
703 if (type.base_type == BASE_TYPE_UNION) {
704 // For union fields, add a second auto-generated field to hold the type,
705 // with a special suffix.
706 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
707 type.enum_def->underlying_type, &typefield));
708 } else if (type.base_type == BASE_TYPE_VECTOR &&
709 type.element == BASE_TYPE_UNION) {
710 // Only cpp, js and ts supports the union vector feature so far.
711 if (!SupportsAdvancedUnionFeatures()) {
713 "Vectors of unions are not yet supported in all "
714 "the specified programming languages.");
716 // For vector of union fields, add a second auto-generated vector field to
717 // hold the types, with a special suffix.
718 Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def);
719 union_vector.element = BASE_TYPE_UTYPE;
720 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector,
725 ECHECK(AddField(struct_def, name, type, &field));
729 ECHECK(ParseSingleValue(&field->name, field->value, true));
730 if (!IsScalar(type.base_type) ||
731 (struct_def.fixed && field->value.constant != "0"))
733 "default values currently only supported for scalars in tables");
735 // Append .0 if the value has not it (skip hex and scientific floats).
736 // This suffix needed for generated C++ code.
737 if (IsFloat(type.base_type)) {
738 auto &text = field->value.constant;
739 FLATBUFFERS_ASSERT(false == text.empty());
740 auto s = text.c_str();
741 while(*s == ' ') s++;
742 if (*s == '-' || *s == '+') s++;
743 // 1) A float constants (nan, inf, pi, etc) is a kind of identifier.
744 // 2) A float number needn't ".0" at the end if it has exponent.
745 if ((false == IsIdentifierStart(*s)) &&
746 (std::string::npos == field->value.constant.find_first_of(".eEpP"))) {
747 field->value.constant += ".0";
751 // The type.base_type can only be scalar, union, array or vector.
752 // Table, struct or string can't have enum_def.
753 // Default value of union and vector in NONE, NULL translated to "0".
754 FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
755 (type.base_type == BASE_TYPE_UNION) ||
756 (type.base_type == BASE_TYPE_VECTOR) ||
757 (type.base_type == BASE_TYPE_ARRAY));
758 if (type.base_type == BASE_TYPE_VECTOR) {
759 // Vector can't use initialization list.
760 FLATBUFFERS_ASSERT(field->value.constant == "0");
762 // All unions should have the NONE ("0") enum value.
763 auto in_enum = type.enum_def->attributes.Lookup("bit_flags") ||
764 type.enum_def->FindByValue(field->value.constant);
765 if (false == in_enum)
766 return Error("default value of " + field->value.constant +
767 " for field " + name + " is not part of enum " +
768 type.enum_def->name);
772 field->doc_comment = dc;
773 ECHECK(ParseMetaData(&field->attributes));
774 field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
775 auto hash_name = field->attributes.Lookup("hash");
777 switch ((type.base_type == BASE_TYPE_VECTOR) ? type.element : type.base_type) {
778 case BASE_TYPE_SHORT:
779 case BASE_TYPE_USHORT: {
780 if (FindHashFunction16(hash_name->constant.c_str()) == nullptr)
781 return Error("Unknown hashing algorithm for 16 bit types: " +
782 hash_name->constant);
786 case BASE_TYPE_UINT: {
787 if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
788 return Error("Unknown hashing algorithm for 32 bit types: " +
789 hash_name->constant);
793 case BASE_TYPE_ULONG: {
794 if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
795 return Error("Unknown hashing algorithm for 64 bit types: " +
796 hash_name->constant);
801 "only short, ushort, int, uint, long and ulong data types support hashing.");
804 auto cpp_type = field->attributes.Lookup("cpp_type");
807 return Error("cpp_type can only be used with a hashed field");
808 /// forcing cpp_ptr_type to 'naked' if unset
809 auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
811 auto val = new Value();
812 val->type = cpp_type->type;
813 val->constant = "naked";
814 field->attributes.Add("cpp_ptr_type", val);
817 if (field->deprecated && struct_def.fixed)
818 return Error("can't deprecate fields in a struct");
819 field->required = field->attributes.Lookup("required") != nullptr;
820 if (field->required &&
821 (struct_def.fixed || IsScalar(type.base_type)))
822 return Error("only non-scalar fields in tables may be 'required'");
823 field->key = field->attributes.Lookup("key") != nullptr;
825 if (struct_def.has_key) return Error("only one field may be set as 'key'");
826 struct_def.has_key = true;
827 if (!IsScalar(type.base_type)) {
828 field->required = true;
829 if (type.base_type != BASE_TYPE_STRING)
830 return Error("'key' field must be string or scalar type");
833 field->shared = field->attributes.Lookup("shared") != nullptr;
834 if (field->shared && field->value.type.base_type != BASE_TYPE_STRING)
835 return Error("shared can only be defined on strings");
837 auto field_native_custom_alloc =
838 field->attributes.Lookup("native_custom_alloc");
839 if (field_native_custom_alloc)
841 "native_custom_alloc can only be used with a table or struct "
844 field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
845 if (field->native_inline && !IsStruct(field->value.type))
846 return Error("native_inline can only be defined on structs");
848 auto nested = field->attributes.Lookup("nested_flatbuffer");
850 if (nested->type.base_type != BASE_TYPE_STRING)
852 "nested_flatbuffer attribute must be a string (the root type)");
853 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
855 "nested_flatbuffer attribute may only apply to a vector of ubyte");
856 // This will cause an error if the root type of the nested flatbuffer
857 // wasn't defined elsewhere.
858 field->nested_flatbuffer = LookupCreateStruct(nested->constant);
861 if (field->attributes.Lookup("flexbuffer")) {
862 field->flexbuffer = true;
863 uses_flexbuffers_ = true;
864 if (type.base_type != BASE_TYPE_VECTOR ||
865 type.element != BASE_TYPE_UCHAR)
866 return Error("flexbuffer attribute may only apply to a vector of ubyte");
870 if (!IsScalar(typefield->value.type.base_type)) {
871 // this is a union vector field
872 typefield->required = field->required;
874 // If this field is a union, and it has a manually assigned id,
875 // the automatically added type field should have an id as well (of N - 1).
876 auto attr = field->attributes.Lookup("id");
878 auto id = atoi(attr->constant.c_str());
879 auto val = new Value();
880 val->type = attr->type;
881 val->constant = NumToString(id - 1);
882 typefield->attributes.Add("id", val);
890 CheckedError Parser::ParseString(Value &val) {
892 EXPECT(kTokenStringConstant);
893 val.constant = NumToString(builder_.CreateString(s).o);
897 CheckedError Parser::ParseComma() {
898 if (!opts.protobuf_ascii_alike) EXPECT(',');
902 CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
903 size_t parent_fieldn,
904 const StructDef *parent_struct_def,
906 bool inside_vector) {
907 switch (val.type.base_type) {
908 case BASE_TYPE_UNION: {
909 FLATBUFFERS_ASSERT(field);
910 std::string constant;
911 Vector<uint8_t> *vector_of_union_types = nullptr;
912 // Find corresponding type field we may have already parsed.
913 for (auto elem = field_stack_.rbegin() + count;
914 elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) {
915 auto &type = elem->second->value.type;
916 if (type.enum_def == val.type.enum_def) {
918 if (type.base_type == BASE_TYPE_VECTOR &&
919 type.element == BASE_TYPE_UTYPE) {
920 // Vector of union type field.
922 ECHECK(atot(elem->first.constant.c_str(), *this, &offset));
923 vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>(
924 builder_.GetCurrentBufferPointer() +
925 builder_.GetSize() - offset);
929 if (type.base_type == BASE_TYPE_UTYPE) {
931 constant = elem->first.constant;
937 if (constant.empty() && !inside_vector) {
938 // We haven't seen the type field yet. Sadly a lot of JSON writers
939 // output these in alphabetical order, meaning it comes after this
940 // value. So we scan past the value to find it, then come back here.
941 // We currently don't do this for vectors of unions because the
942 // scanning/serialization logic would get very complicated.
943 auto type_name = field->name + UnionTypeFieldSuffix();
944 FLATBUFFERS_ASSERT(parent_struct_def);
945 auto type_field = parent_struct_def->fields.Lookup(type_name);
946 FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField().
947 // Remember where we are in the source file, so we can come back here.
948 auto backup = *static_cast<ParserState *>(this);
949 ECHECK(SkipAnyJsonValue()); // The table.
950 ECHECK(ParseComma());
951 auto next_name = attribute_;
952 if (Is(kTokenStringConstant)) {
955 EXPECT(kTokenIdentifier);
957 if (next_name == type_name) {
959 Value type_val = type_field->value;
960 ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr, 0));
961 constant = type_val.constant;
962 // Got the information we needed, now rewind:
963 *static_cast<ParserState *>(this) = backup;
966 if (constant.empty() && !vector_of_union_types) {
967 return Error("missing type field for this union value: " +
971 if (vector_of_union_types) {
972 enum_idx = vector_of_union_types->Get(count);
974 ECHECK(atot(constant.c_str(), *this, &enum_idx));
976 auto enum_val = val.type.enum_def->ReverseLookup(enum_idx, true);
977 if (!enum_val) return Error("illegal type id for: " + field->name);
978 if (enum_val->union_type.base_type == BASE_TYPE_STRUCT) {
979 ECHECK(ParseTable(*enum_val->union_type.struct_def, &val.constant,
981 if (enum_val->union_type.struct_def->fixed) {
982 // All BASE_TYPE_UNION values are offsets, so turn this into one.
983 SerializeStruct(*enum_val->union_type.struct_def, val);
984 builder_.ClearOffsets();
985 val.constant = NumToString(builder_.GetSize());
987 } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
988 ECHECK(ParseString(val));
990 FLATBUFFERS_ASSERT(false);
994 case BASE_TYPE_STRUCT:
995 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
997 case BASE_TYPE_STRING: {
998 ECHECK(ParseString(val));
1001 case BASE_TYPE_VECTOR: {
1003 ECHECK(ParseVector(val.type.VectorType(), &off, field, parent_fieldn));
1004 val.constant = NumToString(off);
1007 case BASE_TYPE_ARRAY: {
1008 ECHECK(ParseArray(val));
1012 case BASE_TYPE_UINT:
1013 case BASE_TYPE_LONG:
1014 case BASE_TYPE_ULONG: {
1015 if (field && field->attributes.Lookup("hash") &&
1016 (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
1017 ECHECK(ParseHash(val, field));
1019 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1024 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1030 void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
1031 SerializeStruct(builder_, struct_def, val);
1034 void Parser::SerializeStruct(FlatBufferBuilder &builder,
1035 const StructDef &struct_def, const Value &val) {
1036 FLATBUFFERS_ASSERT(val.constant.length() == struct_def.bytesize);
1037 builder.Align(struct_def.minalign);
1038 builder.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
1039 struct_def.bytesize);
1040 builder.AddStructOffset(val.offset, builder.GetSize());
1043 template <typename F>
1044 CheckedError Parser::ParseTableDelimiters(size_t &fieldn,
1045 const StructDef *struct_def,
1047 // We allow tables both as JSON object{ .. } with field names
1048 // or vector[..] with all fields in order
1049 char terminator = '}';
1050 bool is_nested_vector = struct_def && Is('[');
1051 if (is_nested_vector) {
1058 if ((!opts.strict_json || !fieldn) && Is(terminator)) break;
1060 if (is_nested_vector) {
1061 if (fieldn >= struct_def->fields.vec.size()) {
1062 return Error("too many unnamed fields in nested array");
1064 name = struct_def->fields.vec[fieldn]->name;
1067 if (Is(kTokenStringConstant)) {
1070 EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
1072 if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
1074 ECHECK(body(name, fieldn, struct_def));
1075 if (Is(terminator)) break;
1076 ECHECK(ParseComma());
1079 if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
1080 return Error("wrong number of unnamed fields in table vector");
1085 CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
1086 uoffset_t *ovalue) {
1087 size_t fieldn_outer = 0;
1088 auto err = ParseTableDelimiters(
1089 fieldn_outer, &struct_def,
1090 [&](const std::string &name, size_t &fieldn,
1091 const StructDef *struct_def_inner) -> CheckedError {
1092 if (name == "$schema") {
1093 ECHECK(Expect(kTokenStringConstant));
1096 auto field = struct_def_inner->fields.Lookup(name);
1098 if (!opts.skip_unexpected_fields_in_json) {
1099 return Error("unknown field: " + name);
1101 ECHECK(SkipAnyJsonValue());
1104 if (IsIdent("null") && !IsScalar(field->value.type.base_type)) {
1105 ECHECK(Next()); // Ignore this field.
1107 Value val = field->value;
1108 if (field->flexbuffer) {
1109 flexbuffers::Builder builder(1024,
1110 flexbuffers::BUILDER_FLAG_SHARE_ALL);
1111 ECHECK(ParseFlexBufferValue(&builder));
1113 // Force alignment for nested flexbuffer
1114 builder_.ForceVectorAlignment(builder.GetSize(), sizeof(uint8_t),
1115 sizeof(largest_scalar_t));
1116 auto off = builder_.CreateVector(builder.GetBuffer());
1117 val.constant = NumToString(off.o);
1118 } else if (field->nested_flatbuffer) {
1120 ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner));
1122 ECHECK(Recurse([&]() {
1123 return ParseAnyValue(val, field, fieldn, struct_def_inner, 0);
1126 // Hardcoded insertion-sort with error-check.
1127 // If fields are specified in order, then this loop exits
1129 auto elem = field_stack_.rbegin();
1130 for (; elem != field_stack_.rbegin() + fieldn; ++elem) {
1131 auto existing_field = elem->second;
1132 if (existing_field == field)
1133 return Error("field set more than once: " + field->name);
1134 if (existing_field->value.offset < field->value.offset) break;
1136 // Note: elem points to before the insertion point, thus .base()
1137 // points to the correct spot.
1138 field_stack_.insert(elem.base(), std::make_pair(val, field));
1146 // Check if all required fields are parsed.
1147 for (auto field_it = struct_def.fields.vec.begin();
1148 field_it != struct_def.fields.vec.end(); ++field_it) {
1149 auto required_field = *field_it;
1150 if (!required_field->required) { continue; }
1152 for (auto pf_it = field_stack_.end() - fieldn_outer;
1153 pf_it != field_stack_.end(); ++pf_it) {
1154 auto parsed_field = pf_it->second;
1155 if (parsed_field == required_field) {
1161 return Error("required field is missing: " + required_field->name +
1162 " in " + struct_def.name);
1166 if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size())
1167 return Error("struct: wrong number of initializers: " + struct_def.name);
1169 auto start = struct_def.fixed ? builder_.StartStruct(struct_def.minalign)
1170 : builder_.StartTable();
1172 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size;
1174 // Go through elements in reverse, since we're building the data backwards.
1175 for (auto it = field_stack_.rbegin();
1176 it != field_stack_.rbegin() + fieldn_outer; ++it) {
1177 auto &field_value = it->first;
1178 auto field = it->second;
1179 if (!struct_def.sortbysize ||
1180 size == SizeOf(field_value.type.base_type)) {
1181 switch (field_value.type.base_type) {
1183 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1184 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1185 case BASE_TYPE_ ## ENUM: \
1186 builder_.Pad(field->padding); \
1187 if (struct_def.fixed) { \
1189 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1190 builder_.PushElement(val); \
1192 CTYPE val, valdef; \
1193 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1194 ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
1195 builder_.AddElement(field_value.offset, val, valdef); \
1198 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1199 #undef FLATBUFFERS_TD
1200 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1201 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1202 case BASE_TYPE_ ## ENUM: \
1203 builder_.Pad(field->padding); \
1204 if (IsStruct(field->value.type)) { \
1205 SerializeStruct(*field->value.type.struct_def, field_value); \
1208 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1209 builder_.AddOffset(field_value.offset, val); \
1212 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
1213 #undef FLATBUFFERS_TD
1214 case BASE_TYPE_ARRAY:
1215 builder_.Pad(field->padding);
1217 reinterpret_cast<const uint8_t*>(field_value.constant.c_str()),
1218 InlineSize(field_value.type));
1225 for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back();
1227 if (struct_def.fixed) {
1228 builder_.ClearOffsets();
1229 builder_.EndStruct();
1230 FLATBUFFERS_ASSERT(value);
1231 // Temporarily store this struct in the value string, since it is to
1232 // be serialized in-place elsewhere.
1234 reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
1235 struct_def.bytesize);
1236 builder_.PopBytes(struct_def.bytesize);
1237 FLATBUFFERS_ASSERT(!ovalue);
1239 auto val = builder_.EndTable(start);
1240 if (ovalue) *ovalue = val;
1241 if (value) *value = NumToString(val);
1246 template <typename F>
1247 CheckedError Parser::ParseVectorDelimiters(uoffset_t &count, F body) {
1250 if ((!opts.strict_json || !count) && Is(']')) break;
1251 ECHECK(body(count));
1254 ECHECK(ParseComma());
1260 static bool CompareType(const uint8_t *a, const uint8_t *b, BaseType ftype) {
1262 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
1263 PTYPE, RTYPE, KTYPE) \
1264 case BASE_TYPE_ ## ENUM: \
1265 return ReadScalar<CTYPE>(a) < ReadScalar<CTYPE>(b);
1266 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
1267 #undef FLATBUFFERS_TD
1268 case BASE_TYPE_STRING:
1269 // Indirect offset pointer to string pointer.
1270 a += ReadScalar<uoffset_t>(a);
1271 b += ReadScalar<uoffset_t>(b);
1272 return *reinterpret_cast<const String *>(a) <
1273 *reinterpret_cast<const String *>(b);
1274 default: return false;
1278 // See below for why we need our own sort :(
1279 template<typename T, typename F, typename S>
1280 void SimpleQsort(T *begin, T *end, size_t width, F comparator, S swapper) {
1281 if (end - begin <= static_cast<ptrdiff_t>(width)) return;
1282 auto l = begin + width;
1285 if (comparator(begin, l)) {
1294 SimpleQsort(begin, l, width, comparator, swapper);
1295 SimpleQsort(r, end, width, comparator, swapper);
1298 CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
1299 FieldDef *field, size_t fieldn) {
1300 uoffset_t count = 0;
1301 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1304 ECHECK(Recurse([&]() {
1305 return ParseAnyValue(val, field, fieldn, nullptr, count, true);
1307 field_stack_.push_back(std::make_pair(val, nullptr));
1312 builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
1313 InlineAlignment(type));
1314 for (uoffset_t i = 0; i < count; i++) {
1315 // start at the back, since we're building the data backwards.
1316 auto &val = field_stack_.back().first;
1317 switch (val.type.base_type) {
1319 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1320 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1321 case BASE_TYPE_ ## ENUM: \
1322 if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
1325 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1326 builder_.PushElement(elem); \
1329 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1330 #undef FLATBUFFERS_TD
1333 field_stack_.pop_back();
1336 builder_.ClearOffsets();
1337 *ovalue = builder_.EndVector(count);
1339 if (type.base_type == BASE_TYPE_STRUCT && type.struct_def->has_key) {
1340 // We should sort this vector. Find the key first.
1341 const FieldDef *key = nullptr;
1342 for (auto it = type.struct_def->fields.vec.begin();
1343 it != type.struct_def->fields.vec.end(); ++it) {
1351 // We can't use std::sort because for structs the size is not known at
1352 // compile time, and for tables our iterators dereference offsets, so can't
1353 // be used to swap elements.
1354 // And we can't use C qsort either, since that would force use to use
1355 // globals, making parsing thread-unsafe.
1356 // So for now, we use SimpleQsort above.
1357 // TODO: replace with something better, preferably not recursive.
1358 static voffset_t offset = key->value.offset;
1359 static BaseType ftype = key->value.type.base_type;
1361 if (type.struct_def->fixed) {
1362 auto v = reinterpret_cast<VectorOfAny *>(
1363 builder_.GetCurrentBufferPointer());
1364 SimpleQsort<uint8_t>(v->Data(),
1365 v->Data() + v->size() * type.struct_def->bytesize,
1366 type.struct_def->bytesize,
1367 [](const uint8_t *a, const uint8_t *b) -> bool {
1368 return CompareType(a + offset, b + offset, ftype);
1369 }, [&](uint8_t *a, uint8_t *b) {
1371 for (size_t i = 0; i < type.struct_def->bytesize; i++) {
1372 std::swap(a[i], b[i]);
1376 auto v = reinterpret_cast<Vector<Offset<Table>> *>(
1377 builder_.GetCurrentBufferPointer());
1378 // Here also can't use std::sort. We do have an iterator type for it,
1379 // but it is non-standard as it will dereference the offsets, and thus
1380 // can't be used to swap elements.
1381 SimpleQsort<Offset<Table>>(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);
1392 }, [&](Offset<Table> *a, Offset<Table> *b) {
1393 // These are serialized offsets, so are relative where they are
1394 // stored in memory, so compute the distance between these pointers:
1395 ptrdiff_t diff = (b - a) * sizeof(Offset<Table>);
1396 assert(diff >= 0); // Guaranteed by SimpleQsort.
1397 auto udiff = static_cast<uoffset_t>(diff);
1398 a->o = EndianScalar(ReadScalar<uoffset_t>(a) - udiff);
1399 b->o = EndianScalar(ReadScalar<uoffset_t>(b) + udiff);
1407 CheckedError Parser::ParseArray(Value &array) {
1408 std::vector<Value> stack;
1409 FlatBufferBuilder builder;
1410 const auto &type = array.type.VectorType();
1411 auto length = array.type.fixed_length;
1412 uoffset_t count = 0;
1413 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1414 vector_emplace_back(&stack, Value());
1415 auto &val = stack.back();
1417 if (IsStruct(type)) {
1418 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1420 ECHECK(ParseSingleValue(nullptr, val, false));
1425 if (length != count) return Error("Fixed-length array size is incorrect.");
1427 for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
1430 switch (val.type.base_type) {
1431 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1432 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1433 case BASE_TYPE_ ## ENUM: \
1434 if (IsStruct(val.type)) { \
1435 SerializeStruct(builder, *val.type.struct_def, val); \
1438 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1439 builder.PushElement(elem); \
1442 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1443 #undef FLATBUFFERS_TD
1444 default: FLATBUFFERS_ASSERT(0);
1449 array.constant.assign(
1450 reinterpret_cast<const char *>(builder.GetCurrentBufferPointer()),
1451 InlineSize(array.type));
1455 CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
1457 const StructDef *parent_struct_def) {
1458 if (token_ == '[') { // backwards compat for 'legacy' ubyte buffers
1459 ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
1461 auto cursor_at_value_begin = cursor_;
1462 ECHECK(SkipAnyJsonValue());
1463 std::string substring(cursor_at_value_begin - 1, cursor_ - 1);
1465 // Create and initialize new parser
1466 Parser nested_parser;
1467 FLATBUFFERS_ASSERT(field->nested_flatbuffer);
1468 nested_parser.root_struct_def_ = field->nested_flatbuffer;
1469 nested_parser.enums_ = enums_;
1470 nested_parser.opts = opts;
1471 nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
1473 // Parse JSON substring into new flatbuffer builder using nested_parser
1474 bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
1476 // Clean nested_parser to avoid deleting the elements in
1477 // the SymbolTables on destruction
1478 nested_parser.enums_.dict.clear();
1479 nested_parser.enums_.vec.clear();
1482 ECHECK(Error(nested_parser.error_));
1484 // Force alignment for nested flatbuffer
1485 builder_.ForceVectorAlignment(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 & ~(IDLOptions::kCpp | IDLOptions::kJs |
2214 IDLOptions::kTs | IDLOptions::kPhp |
2215 IDLOptions::kJava | IDLOptions::kCSharp |
2216 IDLOptions::kKotlin |
2217 IDLOptions::kBinary)) == 0;
2220 bool Parser::SupportsAdvancedArrayFeatures() const {
2221 return (opts.lang_to_generate &
2222 ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
2223 IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
2224 IDLOptions::kBinary)) == 0;
2227 Namespace *Parser::UniqueNamespace(Namespace *ns) {
2228 for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
2229 if (ns->components == (*it)->components) {
2234 namespaces_.push_back(ns);
2238 std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
2239 Namespace *ns = new Namespace();
2241 std::size_t current, previous = 0;
2242 current = full_qualified_name.find('.');
2243 while (current != std::string::npos) {
2244 ns->components.push_back(
2245 full_qualified_name.substr(previous, current - previous));
2246 previous = current + 1;
2247 current = full_qualified_name.find('.', previous);
2249 current_namespace_ = UniqueNamespace(ns);
2250 return full_qualified_name.substr(previous, current - previous);
2253 static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
2254 auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
2255 auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
2259 CheckedError Parser::ParseDecl() {
2260 std::vector<std::string> dc = doc_comment_;
2261 bool fixed = IsIdent("struct");
2262 if (!fixed && !IsIdent("table")) return Error("declaration expected");
2264 std::string name = attribute_;
2265 EXPECT(kTokenIdentifier);
2266 StructDef *struct_def;
2267 ECHECK(StartStruct(name, &struct_def));
2268 struct_def->doc_comment = dc;
2269 struct_def->fixed = fixed;
2270 ECHECK(ParseMetaData(&struct_def->attributes));
2271 struct_def->sortbysize =
2272 struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
2274 while (token_ != '}') ECHECK(ParseField(*struct_def));
2275 auto force_align = struct_def->attributes.Lookup("force_align");
2278 auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
2279 if (force_align->type.base_type != BASE_TYPE_INT ||
2280 align < struct_def->minalign || align > FLATBUFFERS_MAX_ALIGNMENT ||
2281 align & (align - 1))
2283 "force_align must be a power of two integer ranging from the"
2284 "struct\'s natural alignment to " +
2285 NumToString(FLATBUFFERS_MAX_ALIGNMENT));
2286 struct_def->minalign = align;
2288 if (!struct_def->bytesize) return Error("size 0 structs not allowed");
2290 struct_def->PadLastField(struct_def->minalign);
2291 // Check if this is a table that has manual id assignments
2292 auto &fields = struct_def->fields.vec;
2293 if (!fixed && fields.size()) {
2294 size_t num_id_fields = 0;
2295 for (auto it = fields.begin(); it != fields.end(); ++it) {
2296 if ((*it)->attributes.Lookup("id")) num_id_fields++;
2298 // If any fields have ids..
2299 if (num_id_fields) {
2300 // Then all fields must have them.
2301 if (num_id_fields != fields.size())
2303 "either all fields or no fields must have an 'id' attribute");
2304 // Simply sort by id, then the fields are the same as if no ids had
2306 std::sort(fields.begin(), fields.end(), compareFieldDefs);
2307 // Verify we have a contiguous set, and reassign vtable offsets.
2308 for (int i = 0; i < static_cast<int>(fields.size()); i++) {
2309 if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
2310 return Error("field id\'s must be consecutive from 0, id " +
2311 NumToString(i) + " missing or set twice");
2312 fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
2318 CheckClash(fields, struct_def, UnionTypeFieldSuffix(), BASE_TYPE_UNION));
2319 ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
2320 ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
2321 ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
2322 ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
2323 ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
2325 types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name),
2326 new Type(BASE_TYPE_STRUCT, struct_def, nullptr));
2330 CheckedError Parser::ParseService() {
2331 std::vector<std::string> service_comment = doc_comment_;
2333 auto service_name = attribute_;
2334 EXPECT(kTokenIdentifier);
2335 auto &service_def = *new ServiceDef();
2336 service_def.name = service_name;
2337 service_def.file = file_being_parsed_;
2338 service_def.doc_comment = service_comment;
2339 service_def.defined_namespace = current_namespace_;
2340 if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
2342 return Error("service already exists: " + service_name);
2343 ECHECK(ParseMetaData(&service_def.attributes));
2346 std::vector<std::string> doc_comment = doc_comment_;
2347 auto rpc_name = attribute_;
2348 EXPECT(kTokenIdentifier);
2350 Type reqtype, resptype;
2351 ECHECK(ParseTypeIdent(reqtype));
2354 ECHECK(ParseTypeIdent(resptype));
2355 if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
2356 resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
2357 return Error("rpc request and response types must be tables");
2358 auto &rpc = *new RPCCall();
2359 rpc.name = rpc_name;
2360 rpc.request = reqtype.struct_def;
2361 rpc.response = resptype.struct_def;
2362 rpc.doc_comment = doc_comment;
2363 if (service_def.calls.Add(rpc_name, &rpc))
2364 return Error("rpc already exists: " + rpc_name);
2365 ECHECK(ParseMetaData(&rpc.attributes));
2367 } while (token_ != '}');
2372 bool Parser::SetRootType(const char *name) {
2373 root_struct_def_ = LookupStruct(name);
2374 if (!root_struct_def_)
2376 LookupStruct(current_namespace_->GetFullyQualifiedName(name));
2377 return root_struct_def_ != nullptr;
2380 void Parser::MarkGenerated() {
2381 // This function marks all existing definitions as having already
2382 // been generated, which signals no code for included files should be
2384 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2385 (*it)->generated = true;
2387 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
2388 if (!(*it)->predecl) { (*it)->generated = true; }
2390 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
2391 (*it)->generated = true;
2395 CheckedError Parser::ParseNamespace() {
2397 auto ns = new Namespace();
2398 namespaces_.push_back(ns); // Store it here to not leak upon error.
2399 if (token_ != ';') {
2401 ns->components.push_back(attribute_);
2402 EXPECT(kTokenIdentifier);
2403 if (Is('.')) NEXT() else break;
2406 namespaces_.pop_back();
2407 current_namespace_ = UniqueNamespace(ns);
2412 // Best effort parsing of .proto declarations, with the aim to turn them
2413 // in the closest corresponding FlatBuffer equivalent.
2414 // We parse everything as identifiers instead of keywords, since we don't
2415 // want protobuf keywords to become invalid identifiers in FlatBuffers.
2416 CheckedError Parser::ParseProtoDecl() {
2417 bool isextend = IsIdent("extend");
2418 if (IsIdent("package")) {
2419 // These are identical in syntax to FlatBuffer's namespace decl.
2420 ECHECK(ParseNamespace());
2421 } else if (IsIdent("message") || isextend) {
2422 std::vector<std::string> struct_comment = doc_comment_;
2424 StructDef *struct_def = nullptr;
2425 Namespace *parent_namespace = nullptr;
2427 if (Is('.')) NEXT(); // qualified names may start with a . ?
2428 auto id = attribute_;
2429 EXPECT(kTokenIdentifier);
2430 ECHECK(ParseNamespacing(&id, nullptr));
2431 struct_def = LookupCreateStruct(id, false);
2433 return Error("cannot extend unknown message type: " + id);
2435 std::string name = attribute_;
2436 EXPECT(kTokenIdentifier);
2437 ECHECK(StartStruct(name, &struct_def));
2438 // Since message definitions can be nested, we create a new namespace.
2439 auto ns = new Namespace();
2440 // Copy of current namespace.
2441 *ns = *current_namespace_;
2442 // But with current message name.
2443 ns->components.push_back(name);
2445 parent_namespace = current_namespace_;
2446 current_namespace_ = UniqueNamespace(ns);
2448 struct_def->doc_comment = struct_comment;
2449 ECHECK(ParseProtoFields(struct_def, isextend, false));
2450 if (!isextend) { current_namespace_ = parent_namespace; }
2451 if (Is(';')) NEXT();
2452 } else if (IsIdent("enum")) {
2453 // These are almost the same, just with different terminator:
2455 ECHECK(ParseEnum(false, &enum_def));
2456 if (Is(';')) NEXT();
2457 // Temp: remove any duplicates, as .fbs files can't handle them.
2458 enum_def->RemoveDuplicates();
2459 } else if (IsIdent("syntax")) { // Skip these.
2462 EXPECT(kTokenStringConstant);
2464 } else if (IsIdent("option")) { // Skip these.
2465 ECHECK(ParseProtoOption());
2467 } else if (IsIdent("service")) { // Skip these.
2469 EXPECT(kTokenIdentifier);
2470 ECHECK(ParseProtoCurliesOrIdent());
2472 return Error("don\'t know how to parse .proto declaration starting with " +
2473 TokenToStringId(token_));
2478 CheckedError Parser::StartEnum(const std::string &enum_name, bool is_union,
2480 auto &enum_def = *new EnumDef();
2481 enum_def.name = enum_name;
2482 enum_def.file = file_being_parsed_;
2483 enum_def.doc_comment = doc_comment_;
2484 enum_def.is_union = is_union;
2485 enum_def.defined_namespace = current_namespace_;
2486 if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name),
2488 return Error("enum already exists: " + enum_name);
2489 enum_def.underlying_type.base_type = is_union ? BASE_TYPE_UTYPE
2491 enum_def.underlying_type.enum_def = &enum_def;
2492 if (dest) *dest = &enum_def;
2496 CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
2497 bool inside_oneof) {
2499 while (token_ != '}') {
2500 if (IsIdent("message") || IsIdent("extend") || IsIdent("enum")) {
2501 // Nested declarations.
2502 ECHECK(ParseProtoDecl());
2503 } else if (IsIdent("extensions")) { // Skip these.
2505 EXPECT(kTokenIntegerConstant);
2506 if (Is(kTokenIdentifier)) {
2511 } else if (IsIdent("option")) { // Skip these.
2512 ECHECK(ParseProtoOption());
2514 } else if (IsIdent("reserved")) { // Skip these.
2516 while (!Is(';')) { NEXT(); } // A variety of formats, just skip.
2519 std::vector<std::string> field_comment = doc_comment_;
2520 // Parse the qualifier.
2521 bool required = false;
2522 bool repeated = false;
2524 if (!inside_oneof) {
2525 if (IsIdent("optional")) {
2526 // This is the default.
2528 } else if (IsIdent("required")) {
2531 } else if (IsIdent("repeated")) {
2534 } else if (IsIdent("oneof")) {
2538 // can't error, proto3 allows decls without any of the above.
2541 StructDef *anonymous_struct = nullptr;
2542 EnumDef *oneof_union = nullptr;
2544 if (IsIdent("group") || oneof) {
2546 if (oneof && opts.proto_oneof_union) {
2547 auto name = MakeCamel(attribute_, true) + "Union";
2548 ECHECK(StartEnum(name, true, &oneof_union));
2549 type = Type(BASE_TYPE_UNION, nullptr, oneof_union);
2551 auto name = "Anonymous" + NumToString(anonymous_counter++);
2552 ECHECK(StartStruct(name, &anonymous_struct));
2553 type = Type(BASE_TYPE_STRUCT, anonymous_struct);
2556 ECHECK(ParseTypeFromProtoType(&type));
2558 // Repeated elements get mapped to a vector.
2560 type.element = type.base_type;
2561 type.base_type = BASE_TYPE_VECTOR;
2562 if (type.element == BASE_TYPE_VECTOR) {
2563 // We have a vector or vectors, which FlatBuffers doesn't support.
2564 // For now make it a vector of string (since the source is likely
2565 // "repeated bytes").
2566 // TODO(wvo): A better solution would be to wrap this in a table.
2567 type.element = BASE_TYPE_STRING;
2570 std::string name = attribute_;
2571 EXPECT(kTokenIdentifier);
2573 // Parse the field id. Since we're just translating schemas, not
2574 // any kind of binary compatibility, we can safely ignore these, and
2577 EXPECT(kTokenIntegerConstant);
2579 FieldDef *field = nullptr;
2581 // We allow a field to be re-defined when extending.
2582 // TODO: are there situations where that is problematic?
2583 field = struct_def->fields.Lookup(name);
2585 if (!field) ECHECK(AddField(*struct_def, name, type, &field));
2586 field->doc_comment = field_comment;
2587 if (!IsScalar(type.base_type)) field->required = required;
2588 // See if there's a default specified.
2592 auto key = attribute_;
2593 ECHECK(ParseProtoKey());
2595 auto val = attribute_;
2596 ECHECK(ParseProtoCurliesOrIdent());
2597 if (key == "default") {
2598 // Temp: skip non-numeric defaults (enums).
2599 auto numeric = strpbrk(val.c_str(), "0123456789-+.");
2600 if (IsScalar(type.base_type) && numeric == val.c_str())
2601 field->value.constant = val;
2602 } else if (key == "deprecated") {
2603 field->deprecated = val == "true";
2605 if (!Is(',')) break;
2610 if (anonymous_struct) {
2611 ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
2612 if (Is(';')) NEXT();
2613 } else if (oneof_union) {
2614 // Parse into a temporary StructDef, then transfer fields into an
2615 // EnumDef describing the oneof as a union.
2616 StructDef oneof_struct;
2617 ECHECK(ParseProtoFields(&oneof_struct, false, oneof));
2618 if (Is(';')) NEXT();
2619 for (auto field_it = oneof_struct.fields.vec.begin();
2620 field_it != oneof_struct.fields.vec.end(); ++field_it) {
2621 const auto &oneof_field = **field_it;
2622 const auto &oneof_type = oneof_field.value.type;
2623 if (oneof_type.base_type != BASE_TYPE_STRUCT ||
2624 !oneof_type.struct_def || oneof_type.struct_def->fixed)
2625 return Error("oneof '" + name +
2626 "' cannot be mapped to a union because member '" +
2627 oneof_field.name + "' is not a table type.");
2628 EnumValBuilder evb(*this, *oneof_union);
2629 auto ev = evb.CreateEnumerator(oneof_type.struct_def->name);
2630 ev->union_type = oneof_type;
2631 ev->doc_comment = oneof_field.doc_comment;
2632 ECHECK(evb.AcceptEnumerator(oneof_field.name));
2643 CheckedError Parser::ParseProtoKey() {
2644 if (token_ == '(') {
2646 // Skip "(a.b)" style custom attributes.
2647 while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
2651 EXPECT(kTokenIdentifier);
2654 EXPECT(kTokenIdentifier);
2659 CheckedError Parser::ParseProtoCurliesOrIdent() {
2662 for (int nesting = 1; nesting;) {
2665 else if (token_ == '}')
2670 NEXT(); // Any single token.
2675 CheckedError Parser::ParseProtoOption() {
2677 ECHECK(ParseProtoKey());
2679 ECHECK(ParseProtoCurliesOrIdent());
2683 // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
2684 CheckedError Parser::ParseTypeFromProtoType(Type *type) {
2685 struct type_lookup {
2686 const char *proto_type;
2687 BaseType fb_type, element;
2689 static type_lookup lookup[] = {
2690 { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE },
2691 { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE },
2692 { "int32", BASE_TYPE_INT, BASE_TYPE_NONE },
2693 { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2694 { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2695 { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2696 { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE },
2697 { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2698 { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2699 { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2700 { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE },
2701 { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2702 { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE },
2703 { "string", BASE_TYPE_STRING, BASE_TYPE_NONE },
2704 { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR },
2705 { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE }
2707 for (auto tl = lookup; tl->proto_type; tl++) {
2708 if (attribute_ == tl->proto_type) {
2709 type->base_type = tl->fb_type;
2710 type->element = tl->element;
2715 if (Is('.')) NEXT(); // qualified names may start with a . ?
2716 ECHECK(ParseTypeIdent(*type));
2720 CheckedError Parser::SkipAnyJsonValue() {
2723 size_t fieldn_outer = 0;
2724 return ParseTableDelimiters(
2725 fieldn_outer, nullptr,
2726 [&](const std::string &, size_t &fieldn,
2727 const StructDef *) -> CheckedError {
2728 ECHECK(Recurse([&]() { return SkipAnyJsonValue(); }));
2734 uoffset_t count = 0;
2735 return ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2736 return Recurse([&]() { return SkipAnyJsonValue(); });
2739 case kTokenStringConstant:
2740 case kTokenIntegerConstant:
2741 case kTokenFloatConstant: NEXT(); break;
2743 if (IsIdent("true") || IsIdent("false") || IsIdent("null")) {
2746 return TokenError();
2751 CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
2754 auto start = builder->StartMap();
2755 size_t fieldn_outer = 0;
2757 ParseTableDelimiters(fieldn_outer, nullptr,
2758 [&](const std::string &name, size_t &fieldn,
2759 const StructDef *) -> CheckedError {
2761 ECHECK(ParseFlexBufferValue(builder));
2766 builder->EndMap(start);
2770 auto start = builder->StartVector();
2771 uoffset_t count = 0;
2772 ECHECK(ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2773 return ParseFlexBufferValue(builder);
2775 builder->EndVector(start, false, false);
2778 case kTokenStringConstant:
2779 builder->String(attribute_);
2780 EXPECT(kTokenStringConstant);
2782 case kTokenIntegerConstant:
2783 builder->Int(StringToInt(attribute_.c_str()));
2784 EXPECT(kTokenIntegerConstant);
2786 case kTokenFloatConstant:
2787 builder->Double(strtod(attribute_.c_str(), nullptr));
2788 EXPECT(kTokenFloatConstant);
2791 if (IsIdent("true")) {
2792 builder->Bool(true);
2794 } else if (IsIdent("false")) {
2795 builder->Bool(false);
2797 } else if (IsIdent("null")) {
2801 return TokenError();
2806 bool Parser::ParseFlexBuffer(const char *source, const char *source_filename,
2807 flexbuffers::Builder *builder) {
2808 auto ok = !StartParseFile(source, source_filename).Check() &&
2809 !ParseFlexBufferValue(builder).Check();
2810 if (ok) builder->Finish();
2814 bool Parser::Parse(const char *source, const char **include_paths,
2815 const char *source_filename) {
2816 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2819 if (opts.use_flexbuffers) {
2820 r = ParseFlexBuffer(source, source_filename, &flex_builder_);
2822 r = !ParseRoot(source, include_paths, source_filename).Check();
2824 FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2828 CheckedError Parser::StartParseFile(const char *source,
2829 const char *source_filename) {
2830 file_being_parsed_ = source_filename ? source_filename : "";
2832 ResetState(source_);
2834 ECHECK(SkipByteOrderMark());
2836 if (Is(kTokenEof)) return Error("input file is empty");
2840 CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
2841 const char *source_filename) {
2842 ECHECK(DoParse(source, include_paths, source_filename, nullptr));
2844 // Check that all types were defined.
2845 for (auto it = structs_.vec.begin(); it != structs_.vec.end();) {
2846 auto &struct_def = **it;
2847 if (struct_def.predecl) {
2848 if (opts.proto_mode) {
2849 // Protos allow enums to be used before declaration, so check if that
2850 // is the case here.
2851 EnumDef *enum_def = nullptr;
2852 for (size_t components =
2853 struct_def.defined_namespace->components.size() + 1;
2854 components && !enum_def; components--) {
2855 auto qualified_name =
2856 struct_def.defined_namespace->GetFullyQualifiedName(
2857 struct_def.name, components - 1);
2858 enum_def = LookupEnum(qualified_name);
2861 // This is pretty slow, but a simple solution for now.
2862 auto initial_count = struct_def.refcount;
2863 for (auto struct_it = structs_.vec.begin();
2864 struct_it != structs_.vec.end(); ++struct_it) {
2865 auto &sd = **struct_it;
2866 for (auto field_it = sd.fields.vec.begin();
2867 field_it != sd.fields.vec.end(); ++field_it) {
2868 auto &field = **field_it;
2869 if (field.value.type.struct_def == &struct_def) {
2870 field.value.type.struct_def = nullptr;
2871 field.value.type.enum_def = enum_def;
2872 auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
2873 ? field.value.type.element
2874 : field.value.type.base_type;
2875 FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
2876 bt = enum_def->underlying_type.base_type;
2877 struct_def.refcount--;
2878 enum_def->refcount++;
2882 if (struct_def.refcount)
2883 return Error("internal: " + NumToString(struct_def.refcount) + "/" +
2884 NumToString(initial_count) +
2885 " use(s) of pre-declaration enum not accounted for: " +
2887 structs_.dict.erase(structs_.dict.find(struct_def.name));
2888 it = structs_.vec.erase(it);
2890 continue; // Skip error.
2893 auto err = "type referenced but not defined (check namespace): " +
2895 if (struct_def.original_location)
2896 err += ", originally at: " + *struct_def.original_location;
2902 // This check has to happen here and not earlier, because only now do we
2903 // know for sure what the type of these are.
2904 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2905 auto &enum_def = **it;
2906 if (enum_def.is_union) {
2907 for (auto val_it = enum_def.Vals().begin();
2908 val_it != enum_def.Vals().end(); ++val_it) {
2909 auto &val = **val_it;
2910 if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def &&
2911 val.union_type.struct_def->fixed)
2913 "only tables can be union elements in the generated language: " +
2921 CheckedError Parser::DoParse(const char *source, const char **include_paths,
2922 const char *source_filename,
2923 const char *include_filename) {
2924 if (source_filename) {
2925 if (included_files_.find(source_filename) == included_files_.end()) {
2926 included_files_[source_filename] =
2927 include_filename ? include_filename : "";
2928 files_included_per_file_[source_filename] = std::set<std::string>();
2933 if (!include_paths) {
2934 static const char *current_directory[] = { "", nullptr };
2935 include_paths = current_directory;
2937 field_stack_.clear();
2939 // Start with a blank namespace just in case this file doesn't have one.
2940 current_namespace_ = empty_namespace_;
2942 ECHECK(StartParseFile(source, source_filename));
2944 // Includes must come before type declarations:
2946 // Parse pre-include proto statements if any:
2947 if (opts.proto_mode && (attribute_ == "option" || attribute_ == "syntax" ||
2948 attribute_ == "package")) {
2949 ECHECK(ParseProtoDecl());
2950 } else if (IsIdent("native_include")) {
2952 vector_emplace_back(&native_included_files_, attribute_);
2953 EXPECT(kTokenStringConstant);
2955 } else if (IsIdent("include") || (opts.proto_mode && IsIdent("import"))) {
2957 if (opts.proto_mode && attribute_ == "public") NEXT();
2958 auto name = flatbuffers::PosixPath(attribute_.c_str());
2959 EXPECT(kTokenStringConstant);
2960 // Look for the file in include_paths.
2961 std::string filepath;
2962 for (auto paths = include_paths; paths && *paths; paths++) {
2963 filepath = flatbuffers::ConCatPathFileName(*paths, name);
2964 if (FileExists(filepath.c_str())) break;
2966 if (filepath.empty())
2967 return Error("unable to locate include file: " + name);
2968 if (source_filename)
2969 files_included_per_file_[source_filename].insert(filepath);
2970 if (included_files_.find(filepath) == included_files_.end()) {
2971 // We found an include file that we have not parsed yet.
2972 // Load it and parse it.
2973 std::string contents;
2974 if (!LoadFile(filepath.c_str(), true, &contents))
2975 return Error("unable to load include file: " + name);
2976 ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
2978 // We generally do not want to output code for any included files:
2979 if (!opts.generate_all) MarkGenerated();
2980 // Reset these just in case the included file had them, and the
2982 root_struct_def_ = nullptr;
2983 file_identifier_.clear();
2984 file_extension_.clear();
2985 // This is the easiest way to continue this file after an include:
2986 // instead of saving and restoring all the state, we simply start the
2987 // file anew. This will cause it to encounter the same include
2988 // statement again, but this time it will skip it, because it was
2989 // entered into included_files_.
2990 // This is recursive, but only go as deep as the number of include
2992 if (source_filename) {
2993 included_files_.erase(source_filename);
2995 return DoParse(source, include_paths, source_filename,
3003 // Now parse all other kinds of declarations:
3004 while (token_ != kTokenEof) {
3005 if (opts.proto_mode) {
3006 ECHECK(ParseProtoDecl());
3007 } else if (IsIdent("namespace")) {
3008 ECHECK(ParseNamespace());
3009 } else if (token_ == '{') {
3010 if (!root_struct_def_)
3011 return Error("no root type set to parse json with");
3012 if (builder_.GetSize()) {
3013 return Error("cannot have more than one json object in a file");
3016 ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
3017 if (opts.size_prefixed) {
3018 builder_.FinishSizePrefixed(Offset<Table>(toff), file_identifier_.length()
3019 ? file_identifier_.c_str()
3022 builder_.Finish(Offset<Table>(toff), file_identifier_.length()
3023 ? file_identifier_.c_str()
3026 // Check that JSON file doesn't contain more objects or IDL directives.
3027 // Comments after JSON are allowed.
3029 } else if (IsIdent("enum")) {
3030 ECHECK(ParseEnum(false, nullptr));
3031 } else if (IsIdent("union")) {
3032 ECHECK(ParseEnum(true, nullptr));
3033 } else if (IsIdent("root_type")) {
3035 auto root_type = attribute_;
3036 EXPECT(kTokenIdentifier);
3037 ECHECK(ParseNamespacing(&root_type, nullptr));
3038 if (opts.root_type.empty()) {
3039 if (!SetRootType(root_type.c_str()))
3040 return Error("unknown root type: " + root_type);
3041 if (root_struct_def_->fixed)
3042 return Error("root type must be a table");
3045 } else if (IsIdent("file_identifier")) {
3047 file_identifier_ = attribute_;
3048 EXPECT(kTokenStringConstant);
3049 if (file_identifier_.length() != FlatBufferBuilder::kFileIdentifierLength)
3050 return Error("file_identifier must be exactly " +
3051 NumToString(FlatBufferBuilder::kFileIdentifierLength) +
3054 } else if (IsIdent("file_extension")) {
3056 file_extension_ = attribute_;
3057 EXPECT(kTokenStringConstant);
3059 } else if (IsIdent("include")) {
3060 return Error("includes must come before declarations");
3061 } else if (IsIdent("attribute")) {
3063 auto name = attribute_;
3064 if (Is(kTokenIdentifier)) {
3067 EXPECT(kTokenStringConstant);
3070 known_attributes_[name] = false;
3071 } else if (IsIdent("rpc_service")) {
3072 ECHECK(ParseService());
3074 ECHECK(ParseDecl());
3080 std::set<std::string> Parser::GetIncludedFilesRecursive(
3081 const std::string &file_name) const {
3082 std::set<std::string> included_files;
3083 std::list<std::string> to_process;
3085 if (file_name.empty()) return included_files;
3086 to_process.push_back(file_name);
3088 while (!to_process.empty()) {
3089 std::string current = to_process.front();
3090 to_process.pop_front();
3091 included_files.insert(current);
3093 // Workaround the lack of const accessor in C++98 maps.
3095 (*const_cast<std::map<std::string, std::set<std::string>> *>(
3096 &files_included_per_file_))[current];
3097 for (auto it = new_files.begin(); it != new_files.end(); ++it) {
3098 if (included_files.find(*it) == included_files.end())
3099 to_process.push_back(*it);
3103 return included_files;
3106 // Schema serialization functionality:
3108 template<typename T> bool compareName(const T *a, const T *b) {
3109 return a->defined_namespace->GetFullyQualifiedName(a->name) <
3110 b->defined_namespace->GetFullyQualifiedName(b->name);
3113 template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
3114 // Pre-sort these vectors, such that we can set the correct indices for them.
3116 std::sort(vec.begin(), vec.end(), compareName<T>);
3117 for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
3120 void Parser::Serialize() {
3122 AssignIndices(structs_.vec);
3123 AssignIndices(enums_.vec);
3124 std::vector<Offset<reflection::Object>> object_offsets;
3125 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
3126 auto offset = (*it)->Serialize(&builder_, *this);
3127 object_offsets.push_back(offset);
3128 (*it)->serialized_location = offset.o;
3130 std::vector<Offset<reflection::Enum>> enum_offsets;
3131 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3132 auto offset = (*it)->Serialize(&builder_, *this);
3133 enum_offsets.push_back(offset);
3134 (*it)->serialized_location = offset.o;
3136 std::vector<Offset<reflection::Service>> service_offsets;
3137 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
3138 auto offset = (*it)->Serialize(&builder_, *this);
3139 service_offsets.push_back(offset);
3140 (*it)->serialized_location = offset.o;
3142 auto objs__ = builder_.CreateVectorOfSortedTables(&object_offsets);
3143 auto enum__ = builder_.CreateVectorOfSortedTables(&enum_offsets);
3144 auto fiid__ = builder_.CreateString(file_identifier_);
3145 auto fext__ = builder_.CreateString(file_extension_);
3146 auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
3147 auto schema_offset =
3148 reflection::CreateSchema(builder_, objs__, enum__, fiid__, fext__,
3149 (root_struct_def_ ? root_struct_def_->serialized_location : 0),
3151 if (opts.size_prefixed) {
3152 builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
3154 builder_.Finish(schema_offset, reflection::SchemaIdentifier());
3158 static Namespace *GetNamespace(
3159 const std::string &qualified_name, std::vector<Namespace *> &namespaces,
3160 std::map<std::string, Namespace *> &namespaces_index) {
3161 size_t dot = qualified_name.find_last_of('.');
3162 std::string namespace_name = (dot != std::string::npos)
3163 ? std::string(qualified_name.c_str(), dot)
3165 Namespace *&ns = namespaces_index[namespace_name];
3168 ns = new Namespace();
3169 namespaces.push_back(ns);
3174 dot = qualified_name.find('.', pos);
3175 if (dot == std::string::npos) { break; }
3176 ns->components.push_back(qualified_name.substr(pos, dot - pos));
3184 Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
3185 const Parser &parser) const {
3186 std::vector<Offset<reflection::Field>> field_offsets;
3187 for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
3188 field_offsets.push_back((*it)->Serialize(
3189 builder, static_cast<uint16_t>(it - fields.vec.begin()), parser));
3191 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3192 auto name__ = builder->CreateString(qualified_name);
3193 auto flds__ = builder->CreateVectorOfSortedTables(&field_offsets);
3194 auto attr__ = SerializeAttributes(builder, parser);
3195 auto docs__ = parser.opts.binary_schema_comments
3196 ? builder->CreateVectorOfStrings(doc_comment)
3198 return reflection::CreateObject(*builder, name__, flds__, fixed,
3199 static_cast<int>(minalign),
3200 static_cast<int>(bytesize),
3204 bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
3205 if (!DeserializeAttributes(parser, object->attributes()))
3207 DeserializeDoc(doc_comment, object->documentation());
3208 name = parser.UnqualifiedName(object->name()->str());
3210 sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
3211 const auto& of = *(object->fields());
3212 auto indexes = std::vector<uoffset_t>(of.size());
3213 for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
3214 size_t tmp_struct_size = 0;
3215 for (size_t i = 0; i < indexes.size(); i++) {
3216 auto field = of.Get(indexes[i]);
3217 auto field_def = new FieldDef();
3218 if (!field_def->Deserialize(parser, field) ||
3219 fields.Add(field_def->name, field_def)) {
3224 // Recompute padding since that's currently not serialized.
3225 auto size = InlineSize(field_def->value.type);
3227 i + 1 < indexes.size()
3228 ? of.Get(indexes[i+1])
3230 tmp_struct_size += size;
3231 field_def->padding =
3232 next_field ? (next_field->offset() - field_def->value.offset) - size
3233 : PaddingBytes(tmp_struct_size, minalign);
3234 tmp_struct_size += field_def->padding;
3237 FLATBUFFERS_ASSERT(static_cast<int>(tmp_struct_size) == object->bytesize());
3241 Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
3243 const Parser &parser) const {
3244 auto name__ = builder->CreateString(name);
3245 auto type__ = value.type.Serialize(builder);
3246 auto attr__ = SerializeAttributes(builder, parser);
3247 auto docs__ = parser.opts.binary_schema_comments
3248 ? builder->CreateVectorOfStrings(doc_comment)
3250 return reflection::CreateField(*builder, name__, type__, id, value.offset,
3251 // Is uint64>max(int64) tested?
3252 IsInteger(value.type.base_type) ? StringToInt(value.constant.c_str()) : 0,
3253 // result may be platform-dependent if underlying is float (not double)
3254 IsFloat(value.type.base_type) ? strtod(value.constant.c_str(), nullptr)
3256 deprecated, required, key, attr__, docs__);
3257 // TODO: value.constant is almost always "0", we could save quite a bit of
3258 // space by sharing it. Same for common values of value.type.
3261 bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
3262 name = field->name()->str();
3263 defined_namespace = parser.current_namespace_;
3264 if (!value.type.Deserialize(parser, field->type()))
3266 value.offset = field->offset();
3267 if (IsInteger(value.type.base_type)) {
3268 value.constant = NumToString(field->default_integer());
3269 } else if (IsFloat(value.type.base_type)) {
3270 value.constant = FloatToString(field->default_real(), 16);
3271 size_t last_zero = value.constant.find_last_not_of('0');
3272 if (last_zero != std::string::npos && last_zero != 0) {
3273 value.constant.erase(last_zero, std::string::npos);
3276 deprecated = field->deprecated();
3277 required = field->required();
3279 if (!DeserializeAttributes(parser, field->attributes()))
3281 // TODO: this should probably be handled by a separate attribute
3282 if (attributes.Lookup("flexbuffer")) {
3284 parser.uses_flexbuffers_ = true;
3285 if (value.type.base_type != BASE_TYPE_VECTOR ||
3286 value.type.element != BASE_TYPE_UCHAR)
3289 if (auto nested = attributes.Lookup("nested_flatbuffer")) {
3290 auto nested_qualified_name =
3291 parser.current_namespace_->GetFullyQualifiedName(nested->constant);
3292 nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
3293 if (!nested_flatbuffer) return false;
3295 DeserializeDoc(doc_comment, field->documentation());
3299 Offset<reflection::RPCCall> RPCCall::Serialize(FlatBufferBuilder *builder,
3300 const Parser &parser) const {
3301 auto name__ = builder->CreateString(name);
3302 auto attr__ = SerializeAttributes(builder, parser);
3303 auto docs__ = parser.opts.binary_schema_comments
3304 ? builder->CreateVectorOfStrings(doc_comment)
3306 return reflection::CreateRPCCall(*builder, name__,
3307 request->serialized_location,
3308 response->serialized_location,
3312 bool RPCCall::Deserialize(Parser &parser, const reflection::RPCCall *call) {
3313 name = call->name()->str();
3314 if (!DeserializeAttributes(parser, call->attributes()))
3316 DeserializeDoc(doc_comment, call->documentation());
3317 request = parser.structs_.Lookup(call->request()->name()->str());
3318 response = parser.structs_.Lookup(call->response()->name()->str());
3319 if (!request || !response) { return false; }
3323 Offset<reflection::Service> ServiceDef::Serialize(FlatBufferBuilder *builder,
3324 const Parser &parser) const {
3325 std::vector<Offset<reflection::RPCCall>> servicecall_offsets;
3326 for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) {
3327 servicecall_offsets.push_back((*it)->Serialize(builder, parser));
3329 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3330 auto name__ = builder->CreateString(qualified_name);
3331 auto call__ = builder->CreateVector(servicecall_offsets);
3332 auto attr__ = SerializeAttributes(builder, parser);
3333 auto docs__ = parser.opts.binary_schema_comments
3334 ? builder->CreateVectorOfStrings(doc_comment)
3336 return reflection::CreateService(*builder, name__, call__, attr__, docs__);
3339 bool ServiceDef::Deserialize(Parser &parser,
3340 const reflection::Service *service) {
3341 name = parser.UnqualifiedName(service->name()->str());
3342 if (service->calls()) {
3343 for (uoffset_t i = 0; i < service->calls()->size(); ++i) {
3344 auto call = new RPCCall();
3345 if (!call->Deserialize(parser, service->calls()->Get(i)) ||
3346 calls.Add(call->name, call)) {
3352 if (!DeserializeAttributes(parser, service->attributes()))
3354 DeserializeDoc(doc_comment, service->documentation());
3358 Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
3359 const Parser &parser) const {
3360 std::vector<Offset<reflection::EnumVal>> enumval_offsets;
3361 for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
3362 enumval_offsets.push_back((*it)->Serialize(builder, parser));
3364 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3365 auto name__ = builder->CreateString(qualified_name);
3366 auto vals__ = builder->CreateVector(enumval_offsets);
3367 auto type__ = underlying_type.Serialize(builder);
3368 auto attr__ = SerializeAttributes(builder, parser);
3369 auto docs__ = parser.opts.binary_schema_comments
3370 ? builder->CreateVectorOfStrings(doc_comment)
3372 return reflection::CreateEnum(*builder, name__, vals__, is_union, type__,
3376 bool EnumDef::Deserialize(Parser &parser, const reflection::Enum *_enum) {
3377 name = parser.UnqualifiedName(_enum->name()->str());
3378 for (uoffset_t i = 0; i < _enum->values()->size(); ++i) {
3379 auto val = new EnumVal();
3380 if (!val->Deserialize(parser, _enum->values()->Get(i)) ||
3381 vals.Add(val->name, val)) {
3386 is_union = _enum->is_union();
3387 if (!underlying_type.Deserialize(parser, _enum->underlying_type())) {
3390 if (!DeserializeAttributes(parser, _enum->attributes()))
3392 DeserializeDoc(doc_comment, _enum->documentation());
3396 Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder,
3397 const Parser &parser) const {
3398 auto name__ = builder->CreateString(name);
3399 auto type__ = union_type.Serialize(builder);
3400 auto docs__ = parser.opts.binary_schema_comments
3401 ? builder->CreateVectorOfStrings(doc_comment)
3403 return reflection::CreateEnumVal(*builder, name__, value,
3404 union_type.struct_def ? union_type.struct_def->serialized_location : 0,
3408 bool EnumVal::Deserialize(const Parser &parser,
3409 const reflection::EnumVal *val) {
3410 name = val->name()->str();
3411 value = val->value();
3412 if (!union_type.Deserialize(parser, val->union_type()))
3414 DeserializeDoc(doc_comment, val->documentation());
3418 Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
3419 return reflection::CreateType(
3420 *builder, static_cast<reflection::BaseType>(base_type),
3421 static_cast<reflection::BaseType>(element),
3422 struct_def ? struct_def->index : (enum_def ? enum_def->index : -1),
3426 bool Type::Deserialize(const Parser &parser, const reflection::Type *type) {
3427 if (type == nullptr) return true;
3428 base_type = static_cast<BaseType>(type->base_type());
3429 element = static_cast<BaseType>(type->element());
3430 fixed_length = type->fixed_length();
3431 if (type->index() >= 0) {
3432 bool is_series = type->base_type() == reflection::Vector ||
3433 type->base_type() == reflection::Array;
3434 if (type->base_type() == reflection::Obj ||
3436 type->element() == reflection::Obj)) {
3437 if (static_cast<size_t>(type->index()) < parser.structs_.vec.size()) {
3438 struct_def = parser.structs_.vec[type->index()];
3439 struct_def->refcount++;
3444 if (static_cast<size_t>(type->index()) < parser.enums_.vec.size()) {
3445 enum_def = parser.enums_.vec[type->index()];
3454 flatbuffers::Offset<
3455 flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
3456 Definition::SerializeAttributes(FlatBufferBuilder *builder,
3457 const Parser &parser) const {
3458 std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
3459 for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
3460 auto it = parser.known_attributes_.find(kv->first);
3461 FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
3462 if (parser.opts.binary_schema_builtins || !it->second) {
3463 auto key = builder->CreateString(kv->first);
3464 auto val = builder->CreateString(kv->second->constant);
3465 attrs.push_back(reflection::CreateKeyValue(*builder, key, val));
3469 return builder->CreateVectorOfSortedTables(&attrs);
3475 bool Definition::DeserializeAttributes(
3476 Parser &parser, const Vector<Offset<reflection::KeyValue>> *attrs) {
3477 if (attrs == nullptr)
3479 for (uoffset_t i = 0; i < attrs->size(); ++i) {
3480 auto kv = attrs->Get(i);
3481 auto value = new Value();
3482 if (kv->value()) { value->constant = kv->value()->str(); }
3483 if (attributes.Add(kv->key()->str(), value)) {
3487 parser.known_attributes_[kv->key()->str()];
3492 /************************************************************************/
3493 /* DESERIALIZATION */
3494 /************************************************************************/
3495 bool Parser::Deserialize(const uint8_t *buf, const size_t size) {
3496 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t *>(buf), size);
3497 bool size_prefixed = false;
3498 if(!reflection::SchemaBufferHasIdentifier(buf)) {
3499 if (!flatbuffers::BufferHasIdentifier(buf, reflection::SchemaIdentifier(),
3503 size_prefixed = true;
3505 auto verify_fn = size_prefixed ? &reflection::VerifySizePrefixedSchemaBuffer
3506 : &reflection::VerifySchemaBuffer;
3507 if (!verify_fn(verifier)) {
3510 auto schema = size_prefixed ? reflection::GetSizePrefixedSchema(buf)
3511 : reflection::GetSchema(buf);
3512 return Deserialize(schema);
3515 bool Parser::Deserialize(const reflection::Schema *schema) {
3516 file_identifier_ = schema->file_ident() ? schema->file_ident()->str() : "";
3517 file_extension_ = schema->file_ext() ? schema->file_ext()->str() : "";
3518 std::map<std::string, Namespace *> namespaces_index;
3520 // Create defs without deserializing so references from fields to structs and
3521 // enums can be resolved.
3522 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3524 auto struct_def = new StructDef();
3525 struct_def->bytesize = it->bytesize();
3526 struct_def->fixed = it->is_struct();
3527 struct_def->minalign = it->minalign();
3528 if (structs_.Add(it->name()->str(), struct_def)) {
3532 auto type = new Type(BASE_TYPE_STRUCT, struct_def, nullptr);
3533 if (types_.Add(it->name()->str(), type)) {
3538 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3539 auto enum_def = new EnumDef();
3540 if (enums_.Add(it->name()->str(), enum_def)) {
3544 auto type = new Type(BASE_TYPE_UNION, nullptr, enum_def);
3545 if (types_.Add(it->name()->str(), type)) {
3551 // Now fields can refer to structs and enums by index.
3552 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3554 std::string qualified_name = it->name()->str();
3555 auto struct_def = structs_.Lookup(qualified_name);
3556 struct_def->defined_namespace =
3557 GetNamespace(qualified_name, namespaces_, namespaces_index);
3558 if (!struct_def->Deserialize(*this, * it)) { return false; }
3559 if (schema->root_table() == *it) { root_struct_def_ = struct_def; }
3561 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3562 std::string qualified_name = it->name()->str();
3563 auto enum_def = enums_.Lookup(qualified_name);
3564 enum_def->defined_namespace =
3565 GetNamespace(qualified_name, namespaces_, namespaces_index);
3566 if (!enum_def->Deserialize(*this, *it)) { return false; }
3569 if (schema->services()) {
3570 for (auto it = schema->services()->begin(); it != schema->services()->end();
3572 std::string qualified_name = it->name()->str();
3573 auto service_def = new ServiceDef();
3574 service_def->defined_namespace =
3575 GetNamespace(qualified_name, namespaces_, namespaces_index);
3576 if (!service_def->Deserialize(*this, *it) ||
3577 services_.Add(qualified_name, service_def)) {
3587 std::string Parser::ConformTo(const Parser &base) {
3588 for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
3589 auto &struct_def = **sit;
3590 auto qualified_name =
3591 struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
3592 auto struct_def_base = base.LookupStruct(qualified_name);
3593 if (!struct_def_base) continue;
3594 for (auto fit = struct_def.fields.vec.begin();
3595 fit != struct_def.fields.vec.end(); ++fit) {
3596 auto &field = **fit;
3597 auto field_base = struct_def_base->fields.Lookup(field.name);
3599 if (field.value.offset != field_base->value.offset)
3600 return "offsets differ for field: " + field.name;
3601 if (field.value.constant != field_base->value.constant)
3602 return "defaults differ for field: " + field.name;
3603 if (!EqualByName(field.value.type, field_base->value.type))
3604 return "types differ for field: " + field.name;
3606 // Doesn't have to exist, deleting fields is fine.
3607 // But we should check if there is a field that has the same offset
3608 // but is incompatible (in the case of field renaming).
3609 for (auto fbit = struct_def_base->fields.vec.begin();
3610 fbit != struct_def_base->fields.vec.end(); ++fbit) {
3612 if (field.value.offset == field_base->value.offset) {
3613 if (!EqualByName(field.value.type, field_base->value.type))
3614 return "field renamed to different type: " + field.name;
3621 for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
3622 auto &enum_def = **eit;
3623 auto qualified_name =
3624 enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
3625 auto enum_def_base = base.enums_.Lookup(qualified_name);
3626 if (!enum_def_base) continue;
3627 for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
3629 auto &enum_val = **evit;
3630 auto enum_val_base = enum_def_base->Lookup(enum_val.name);
3631 if (enum_val_base) {
3632 if (enum_val != *enum_val_base)
3633 return "values differ for enum: " + enum_val.name;
3640 } // namespace flatbuffers