2 * Copyright 2014 Google Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "flatbuffers/idl.h"
24 #include "flatbuffers/util.h"
26 namespace flatbuffers {
28 // Reflects the version at the compiling time of binary(lib/dll/so).
29 const char *FLATBUFFERS_VERSION() {
32 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
33 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
34 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
38 const double kPi = 3.14159265358979323846;
41 const char *const kTypeNames[] = {
42 #define FLATBUFFERS_TD(ENUM, IDLTYPE, ...) \
44 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
49 const char kTypeSizes[] = {
50 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
52 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
57 // The enums in the reflection schema should match the ones we use internally.
58 // Compare the last element to check if these go out of sync.
59 static_assert(BASE_TYPE_UNION == static_cast<BaseType>(reflection::Union),
62 // Any parsing calls have to be wrapped in this macro, which automates
63 // handling of recursive error checking a bit. It will check the received
64 // CheckedError object, and return straight away on error.
65 #define ECHECK(call) \
68 if (ce.Check()) return ce; \
71 // These two functions are called hundreds of times below, so define a short
73 #define NEXT() ECHECK(Next())
74 #define EXPECT(tok) ECHECK(Expect(tok))
76 static bool ValidateUTF8(const std::string &str) {
77 const char *s = &str[0];
78 const char *const sEnd = s + str.length();
80 if (FromUTF8(&s) < 0) { return false; }
85 static bool IsLowerSnakeCase(const std::string &str) {
86 for (size_t i = 0; i < str.length(); i++) {
88 if (!check_ascii_range(c, 'a', 'z') && !is_digit(c) && c != '_') {
95 // Convert an underscore_based_identifier in to camelCase.
96 // Also uppercases the first character if first is true.
97 std::string MakeCamel(const std::string &in, bool first) {
99 for (size_t i = 0; i < in.length(); i++) {
101 s += CharToUpper(in[0]);
102 else if (in[i] == '_' && i + 1 < in.length())
103 s += CharToUpper(in[++i]);
110 // Convert an underscore_based_identifier in to screaming snake case.
111 std::string MakeScreamingCamel(const std::string &in) {
113 for (size_t i = 0; i < in.length(); i++) {
115 s += CharToUpper(in[i]);
122 void DeserializeDoc(std::vector<std::string> &doc,
123 const Vector<Offset<String>> *documentation) {
124 if (documentation == nullptr) return;
125 for (uoffset_t index = 0; index < documentation->size(); index++)
126 doc.push_back(documentation->Get(index)->str());
129 void Parser::Message(const std::string &msg) {
130 if (!error_.empty()) error_ += "\n"; // log all warnings and errors
131 error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
134 #ifdef _WIN32 // MSVC alike
136 "(" + NumToString(line_) + ", " + NumToString(CursorPosition()) + ")";
138 if (file_being_parsed_.length()) error_ += ":";
139 error_ += NumToString(line_) + ": " + NumToString(CursorPosition());
142 error_ += ": " + msg;
145 void Parser::Warning(const std::string &msg) {
146 if (!opts.no_warnings) Message("warning: " + msg);
149 CheckedError Parser::Error(const std::string &msg) {
150 Message("error: " + msg);
151 return CheckedError(true);
154 inline CheckedError NoError() { return CheckedError(false); }
156 CheckedError Parser::RecurseError() {
157 return Error("maximum parsing depth " + NumToString(parse_depth_counter_) +
161 class Parser::ParseDepthGuard {
163 explicit ParseDepthGuard(Parser *parser_not_null)
164 : parser_(*parser_not_null), caller_depth_(parser_.parse_depth_counter_) {
165 FLATBUFFERS_ASSERT(caller_depth_ <= (FLATBUFFERS_MAX_PARSING_DEPTH) &&
166 "Check() must be called to prevent stack overflow");
167 parser_.parse_depth_counter_ += 1;
170 ~ParseDepthGuard() { parser_.parse_depth_counter_ -= 1; }
172 CheckedError Check() {
173 return caller_depth_ >= (FLATBUFFERS_MAX_PARSING_DEPTH)
174 ? parser_.RecurseError()
175 : CheckedError(false);
178 FLATBUFFERS_DELETE_FUNC(ParseDepthGuard(const ParseDepthGuard &));
179 FLATBUFFERS_DELETE_FUNC(ParseDepthGuard &operator=(const ParseDepthGuard &));
183 const int caller_depth_;
186 template<typename T> std::string TypeToIntervalString() {
187 return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
188 NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
191 // atot: template version of atoi/atof: convert a string to an instance of T.
193 bool atot_scalar(const char *s, T *val, bool_constant<false>) {
194 return StringToNumber(s, val);
198 bool atot_scalar(const char *s, T *val, bool_constant<true>) {
199 // Normalize NaN parsed from fbs or json to unsigned NaN.
200 if (false == StringToNumber(s, val)) return false;
201 *val = (*val != *val) ? std::fabs(*val) : *val;
205 template<typename T> CheckedError atot(const char *s, Parser &parser, T *val) {
206 auto done = atot_scalar(s, val, bool_constant<is_floating_point<T>::value>());
207 if (done) return NoError();
209 return parser.Error("invalid number: \"" + std::string(s) + "\"");
211 return parser.Error("invalid number: \"" + std::string(s) + "\"" +
212 ", constant does not fit " + TypeToIntervalString<T>());
215 inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
218 *val = Offset<void>(atoi(s));
222 std::string Namespace::GetFullyQualifiedName(const std::string &name,
223 size_t max_components) const {
224 // Early exit if we don't have a defined namespace.
225 if (components.empty() || !max_components) { return name; }
226 std::string stream_str;
227 for (size_t i = 0; i < std::min(components.size(), max_components); i++) {
228 stream_str += components[i];
231 if (!stream_str.empty()) stream_str.pop_back();
240 T *LookupTableByName(const SymbolTable<T> &table, const std::string &name,
241 const Namespace ¤t_namespace, size_t skip_top) {
242 const auto &components = current_namespace.components;
243 if (table.dict.empty()) return nullptr;
244 if (components.size() < skip_top) return nullptr;
245 const auto N = components.size() - skip_top;
246 std::string full_name;
247 for (size_t i = 0; i < N; i++) {
248 full_name += components[i];
251 for (size_t i = N; i > 0; i--) {
253 auto obj = table.Lookup(full_name);
255 auto len = full_name.size() - components[i - 1].size() - 1 - name.size();
256 full_name.resize(len);
258 FLATBUFFERS_ASSERT(full_name.empty());
259 return table.Lookup(name); // lookup in global namespace
262 // Declare tokens we'll use. Single character tokens are represented by their
263 // ascii character code (e.g. '{'), others above 256.
265 #define FLATBUFFERS_GEN_TOKENS(TD) \
266 TD(Eof, 256, "end of file") \
267 TD(StringConstant, 257, "string constant") \
268 TD(IntegerConstant, 258, "integer constant") \
269 TD(FloatConstant, 259, "float constant") \
270 TD(Identifier, 260, "identifier")
272 __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
275 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
276 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
277 #undef FLATBUFFERS_TOKEN
280 static std::string TokenToString(int t) {
281 static const char * const tokens[] = {
282 #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
283 FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
284 #undef FLATBUFFERS_TOKEN
285 #define FLATBUFFERS_TD(ENUM, IDLTYPE, ...) \
287 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
288 #undef FLATBUFFERS_TD
290 if (t < 256) { // A single ascii char token.
292 s.append(1, static_cast<char>(t));
294 } else { // Other tokens.
295 return tokens[t - 256];
300 std::string Parser::TokenToStringId(int t) const {
301 return t == kTokenIdentifier ? attribute_ : TokenToString(t);
304 // Parses exactly nibbles worth of hex digits into a number, or error.
305 CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
306 FLATBUFFERS_ASSERT(nibbles > 0);
307 for (int i = 0; i < nibbles; i++)
308 if (!is_xdigit(cursor_[i]))
309 return Error("escape code must be followed by " + NumToString(nibbles) +
311 std::string target(cursor_, cursor_ + nibbles);
312 *val = StringToUInt(target.c_str(), 16);
317 CheckedError Parser::SkipByteOrderMark() {
318 if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
320 if (static_cast<unsigned char>(*cursor_) != 0xbb)
321 return Error("invalid utf-8 byte order mark");
323 if (static_cast<unsigned char>(*cursor_) != 0xbf)
324 return Error("invalid utf-8 byte order mark");
329 static inline bool IsIdentifierStart(char c) {
330 return is_alpha(c) || (c == '_');
333 CheckedError Parser::Next() {
334 doc_comment_.clear();
335 bool seen_newline = cursor_ == source_;
337 attr_is_trivial_ascii_string_ = true;
362 case '=': return NoError();
365 int unicode_high_surrogate = -1;
367 while (*cursor_ != c) {
368 if (*cursor_ < ' ' && static_cast<signed char>(*cursor_) >= 0)
369 return Error("illegal character in string constant");
370 if (*cursor_ == '\\') {
371 attr_is_trivial_ascii_string_ = false; // has escape sequence
373 if (unicode_high_surrogate != -1 && *cursor_ != 'u') {
375 "illegal Unicode sequence (unpaired high surrogate)");
414 case 'x': { // Not in the JSON standard
417 ECHECK(ParseHexNum(2, &val));
418 attribute_ += static_cast<char>(val);
424 ECHECK(ParseHexNum(4, &val));
425 if (val >= 0xD800 && val <= 0xDBFF) {
426 if (unicode_high_surrogate != -1) {
428 "illegal Unicode sequence (multiple high surrogates)");
430 unicode_high_surrogate = static_cast<int>(val);
432 } else if (val >= 0xDC00 && val <= 0xDFFF) {
433 if (unicode_high_surrogate == -1) {
435 "illegal Unicode sequence (unpaired low surrogate)");
437 int code_point = 0x10000 +
438 ((unicode_high_surrogate & 0x03FF) << 10) +
440 ToUTF8(code_point, &attribute_);
441 unicode_high_surrogate = -1;
444 if (unicode_high_surrogate != -1) {
446 "illegal Unicode sequence (unpaired high surrogate)");
448 ToUTF8(static_cast<int>(val), &attribute_);
452 default: return Error("unknown escape code in string constant");
454 } else { // printable chars + UTF-8 bytes
455 if (unicode_high_surrogate != -1) {
457 "illegal Unicode sequence (unpaired high surrogate)");
459 // reset if non-printable
460 attr_is_trivial_ascii_string_ &=
461 check_ascii_range(*cursor_, ' ', '~');
463 attribute_ += *cursor_++;
466 if (unicode_high_surrogate != -1) {
467 return Error("illegal Unicode sequence (unpaired high surrogate)");
470 if (!attr_is_trivial_ascii_string_ && !opts.allow_non_utf8 &&
471 !ValidateUTF8(attribute_)) {
472 return Error("illegal UTF-8 sequence");
474 token_ = kTokenStringConstant;
478 if (*cursor_ == '/') {
479 const char *start = ++cursor_;
480 while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++;
481 if (*start == '/') { // documentation comment
484 "a documentation comment should be on a line on its own");
485 doc_comment_.push_back(std::string(start + 1, cursor_));
488 } else if (*cursor_ == '*') {
490 // TODO: make nested.
491 while (*cursor_ != '*' || cursor_[1] != '/') {
492 if (*cursor_ == '\n') MarkNewLine();
493 if (!*cursor_) return Error("end of file in comment");
499 FLATBUFFERS_FALLTHROUGH(); // else fall thru
501 const auto has_sign = (c == '+') || (c == '-');
502 // '-'/'+' and following identifier - can be a predefined constant like:
503 // NAN, INF, PI, etc or it can be a function name like cos/sin/deg.
504 if (IsIdentifierStart(c) || (has_sign && IsIdentifierStart(*cursor_))) {
505 // Collect all chars of an identifier:
506 const char *start = cursor_ - 1;
507 while (IsIdentifierStart(*cursor_) || is_digit(*cursor_)) cursor_++;
508 attribute_.append(start, cursor_);
509 token_ = has_sign ? kTokenStringConstant : kTokenIdentifier;
514 (c == '.') ? 0 : 1; // dot_lvl==0 <=> exactly one '.' seen
515 if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum?
516 // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4).
517 if (is_digit(c) || has_sign || !dot_lvl) {
518 const auto start = cursor_ - 1;
519 auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1;
520 if (!is_digit(c) && is_digit(*cursor_)) {
521 start_digits = cursor_; // see digit in cursor_ position
524 // hex-float can't begind with '.'
525 auto use_hex = dot_lvl && (c == '0') && is_alpha_char(*cursor_, 'X');
526 if (use_hex) start_digits = ++cursor_; // '0x' is the prefix, skip it
527 // Read an integer number or mantisa of float-point number.
530 while (is_xdigit(*cursor_)) cursor_++;
532 while (is_digit(*cursor_)) cursor_++;
534 } while ((*cursor_ == '.') && (++cursor_) && (--dot_lvl >= 0));
535 // Exponent of float-point number.
536 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
537 // The exponent suffix of hexadecimal float number is mandatory.
538 if (use_hex && !dot_lvl) start_digits = cursor_;
539 if ((use_hex && is_alpha_char(*cursor_, 'P')) ||
540 is_alpha_char(*cursor_, 'E')) {
541 dot_lvl = 0; // Emulate dot to signal about float-point number.
543 if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
544 start_digits = cursor_; // the exponent-part has to have digits
545 // Exponent is decimal integer number
546 while (is_digit(*cursor_)) cursor_++;
547 if (*cursor_ == '.') {
548 cursor_++; // If see a dot treat it as part of invalid number.
549 dot_lvl = -1; // Fall thru to Error().
554 if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
555 attribute_.append(start, cursor_);
556 token_ = dot_lvl ? kTokenIntegerConstant : kTokenFloatConstant;
559 return Error("invalid number: " + std::string(start, cursor_));
564 if (false == check_ascii_range(c, ' ', '~'))
565 ch = "code: " + NumToString(c);
566 return Error("illegal character: " + ch);
571 // Check if a given token is next.
572 bool Parser::Is(int t) const { return t == token_; }
574 bool Parser::IsIdent(const char *id) const {
575 return token_ == kTokenIdentifier && attribute_ == id;
578 // Expect a given token to be next, consume it, or error if not present.
579 CheckedError Parser::Expect(int t) {
581 return Error("expecting: " + TokenToString(t) +
582 " instead got: " + TokenToStringId(token_));
588 CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
593 if (last) *last = attribute_;
594 EXPECT(kTokenIdentifier);
599 EnumDef *Parser::LookupEnum(const std::string &id) {
600 // Search thru parent namespaces.
601 return LookupTableByName(enums_, id, *current_namespace_, 0);
604 StructDef *Parser::LookupStruct(const std::string &id) const {
605 auto sd = structs_.Lookup(id);
606 if (sd) sd->refcount++;
610 StructDef *Parser::LookupStructThruParentNamespaces(
611 const std::string &id) const {
612 auto sd = LookupTableByName(structs_, id, *current_namespace_, 1);
613 if (sd) sd->refcount++;
617 CheckedError Parser::ParseTypeIdent(Type &type) {
618 std::string id = attribute_;
619 EXPECT(kTokenIdentifier);
620 ECHECK(ParseNamespacing(&id, nullptr));
621 auto enum_def = LookupEnum(id);
623 type = enum_def->underlying_type;
624 if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
626 type.base_type = BASE_TYPE_STRUCT;
627 type.struct_def = LookupCreateStruct(id);
632 // Parse any IDL type.
633 CheckedError Parser::ParseType(Type &type) {
634 if (token_ == kTokenIdentifier) {
635 if (IsIdent("bool")) {
636 type.base_type = BASE_TYPE_BOOL;
638 } else if (IsIdent("byte") || IsIdent("int8")) {
639 type.base_type = BASE_TYPE_CHAR;
641 } else if (IsIdent("ubyte") || IsIdent("uint8")) {
642 type.base_type = BASE_TYPE_UCHAR;
644 } else if (IsIdent("short") || IsIdent("int16")) {
645 type.base_type = BASE_TYPE_SHORT;
647 } else if (IsIdent("ushort") || IsIdent("uint16")) {
648 type.base_type = BASE_TYPE_USHORT;
650 } else if (IsIdent("int") || IsIdent("int32")) {
651 type.base_type = BASE_TYPE_INT;
653 } else if (IsIdent("uint") || IsIdent("uint32")) {
654 type.base_type = BASE_TYPE_UINT;
656 } else if (IsIdent("long") || IsIdent("int64")) {
657 type.base_type = BASE_TYPE_LONG;
659 } else if (IsIdent("ulong") || IsIdent("uint64")) {
660 type.base_type = BASE_TYPE_ULONG;
662 } else if (IsIdent("float") || IsIdent("float32")) {
663 type.base_type = BASE_TYPE_FLOAT;
665 } else if (IsIdent("double") || IsIdent("float64")) {
666 type.base_type = BASE_TYPE_DOUBLE;
668 } else if (IsIdent("string")) {
669 type.base_type = BASE_TYPE_STRING;
672 ECHECK(ParseTypeIdent(type));
674 } else if (token_ == '[') {
675 ParseDepthGuard depth_guard(this);
676 ECHECK(depth_guard.Check());
679 ECHECK(ParseType(subtype));
680 if (IsSeries(subtype)) {
681 // We could support this, but it will complicate things, and it's
682 // easier to work around with a struct around the inner vector.
683 return Error("nested vector types not supported (wrap in table first)");
687 if (token_ != kTokenIntegerConstant) {
688 return Error("length of fixed-length array must be an integer value");
690 uint16_t fixed_length = 0;
691 bool check = StringToNumber(attribute_.c_str(), &fixed_length);
692 if (!check || fixed_length < 1) {
694 "length of fixed-length array must be positive and fit to "
697 type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def,
701 type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
703 type.element = subtype.base_type;
706 return Error("illegal type syntax");
711 CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
712 const Type &type, FieldDef **dest) {
713 auto &field = *new FieldDef();
715 FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
717 field.file = struct_def.file;
718 field.value.type = type;
719 if (struct_def.fixed) { // statically compute the field offset
720 auto size = InlineSize(type);
721 auto alignment = InlineAlignment(type);
722 // structs_ need to have a predictable format, so we need to align to
723 // the largest scalar
724 struct_def.minalign = std::max(struct_def.minalign, alignment);
725 struct_def.PadLastField(alignment);
726 field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
727 struct_def.bytesize += size;
729 if (struct_def.fields.Add(name, &field))
730 return Error("field already exists: " + name);
735 CheckedError Parser::ParseField(StructDef &struct_def) {
736 std::string name = attribute_;
738 if (LookupCreateStruct(name, false, false))
739 return Error("field name can not be the same as table/struct name");
741 if (!IsLowerSnakeCase(name)) {
742 Warning("field names should be lowercase snake_case, got: " + name);
745 std::vector<std::string> dc = doc_comment_;
746 EXPECT(kTokenIdentifier);
749 ECHECK(ParseType(type));
751 if (struct_def.fixed) {
752 auto valid = IsScalar(type.base_type) || IsStruct(type);
753 if (!valid && IsArray(type)) {
754 const auto &elem_type = type.VectorType();
755 valid |= IsScalar(elem_type.base_type) || IsStruct(elem_type);
758 return Error("structs may contain only scalar or struct fields");
761 if (!struct_def.fixed && IsArray(type))
762 return Error("fixed-length array in table must be wrapped in struct");
764 if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
766 "Arrays are not yet supported in all "
767 "the specified programming languages.");
770 FieldDef *typefield = nullptr;
771 if (type.base_type == BASE_TYPE_UNION) {
772 // For union fields, add a second auto-generated field to hold the type,
773 // with a special suffix.
774 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
775 type.enum_def->underlying_type, &typefield));
776 } else if (IsVector(type) && type.element == BASE_TYPE_UNION) {
777 // Only cpp, js and ts supports the union vector feature so far.
778 if (!SupportsAdvancedUnionFeatures()) {
780 "Vectors of unions are not yet supported in at least one of "
781 "the specified programming languages.");
783 // For vector of union fields, add a second auto-generated vector field to
784 // hold the types, with a special suffix.
785 Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def);
786 union_vector.element = BASE_TYPE_UTYPE;
787 ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector,
792 ECHECK(AddField(struct_def, name, type, &field));
796 ECHECK(ParseSingleValue(&field->name, field->value, true));
797 if (IsStruct(type) || (struct_def.fixed && field->value.constant != "0"))
799 "default values are not supported for struct fields, table fields, "
801 if ((IsString(type) || IsVector(type)) && field->value.constant != "0" &&
802 field->value.constant != "null" && !SupportsDefaultVectorsAndStrings())
804 "Default values for strings and vectors are not supported in one of "
805 "the specified programming languages");
806 if (IsVector(type) && field->value.constant != "0" &&
807 field->value.constant != "[]") {
808 return Error("The only supported default for vectors is `[]`.");
812 // Append .0 if the value has not it (skip hex and scientific floats).
813 // This suffix needed for generated C++ code.
814 if (IsFloat(type.base_type)) {
815 auto &text = field->value.constant;
816 FLATBUFFERS_ASSERT(false == text.empty());
817 auto s = text.c_str();
818 while (*s == ' ') s++;
819 if (*s == '-' || *s == '+') s++;
820 // 1) A float constants (nan, inf, pi, etc) is a kind of identifier.
821 // 2) A float number needn't ".0" at the end if it has exponent.
822 if ((false == IsIdentifierStart(*s)) &&
823 (std::string::npos == field->value.constant.find_first_of(".eEpP"))) {
824 field->value.constant += ".0";
828 field->doc_comment = dc;
829 ECHECK(ParseMetaData(&field->attributes));
830 field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
831 auto hash_name = field->attributes.Lookup("hash");
833 switch ((IsVector(type)) ? type.element : type.base_type) {
834 case BASE_TYPE_SHORT:
835 case BASE_TYPE_USHORT: {
836 if (FindHashFunction16(hash_name->constant.c_str()) == nullptr)
837 return Error("Unknown hashing algorithm for 16 bit types: " +
838 hash_name->constant);
842 case BASE_TYPE_UINT: {
843 if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
844 return Error("Unknown hashing algorithm for 32 bit types: " +
845 hash_name->constant);
849 case BASE_TYPE_ULONG: {
850 if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
851 return Error("Unknown hashing algorithm for 64 bit types: " +
852 hash_name->constant);
857 "only short, ushort, int, uint, long and ulong data types support "
862 // For historical convenience reasons, string keys are assumed required.
863 // Scalars are kDefault unless otherwise specified.
864 // Nonscalars are kOptional unless required;
865 field->key = field->attributes.Lookup("key") != nullptr;
866 const bool required = field->attributes.Lookup("required") != nullptr ||
867 (IsString(type) && field->key);
868 const bool default_str_or_vec =
869 ((IsString(type) || IsVector(type)) && field->value.constant != "0");
870 const bool optional = IsScalar(type.base_type)
871 ? (field->value.constant == "null")
872 : !(required || default_str_or_vec);
873 if (required && optional) {
874 return Error("Fields cannot be both optional and required.");
876 field->presence = FieldDef::MakeFieldPresence(optional, required);
878 if (required && (struct_def.fixed || IsScalar(type.base_type))) {
879 return Error("only non-scalar fields in tables may be 'required'");
882 if (struct_def.has_key) return Error("only one field may be set as 'key'");
883 struct_def.has_key = true;
884 if (!IsScalar(type.base_type) && !IsString(type)) {
885 return Error("'key' field must be string or scalar type");
889 if (field->IsScalarOptional()) {
890 if (type.enum_def && type.enum_def->Lookup("null")) {
891 FLATBUFFERS_ASSERT(IsInteger(type.base_type));
893 "the default 'null' is reserved for declaring optional scalar "
894 "fields, it conflicts with declaration of enum '" +
895 type.enum_def->name + "'.");
897 if (field->attributes.Lookup("key")) {
899 "only a non-optional scalar field can be used as a 'key' field");
901 if (!SupportsOptionalScalars()) {
903 "Optional scalars are not yet supported in at least one the of "
904 "the specified programming languages.");
909 // The type.base_type can only be scalar, union, array or vector.
910 // Table, struct or string can't have enum_def.
911 // Default value of union and vector in NONE, NULL translated to "0".
912 FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
913 (type.base_type == BASE_TYPE_UNION) || IsVector(type) ||
915 if (IsVector(type)) {
916 // Vector can't use initialization list.
917 FLATBUFFERS_ASSERT(field->value.constant == "0");
919 // All unions should have the NONE ("0") enum value.
920 auto in_enum = field->IsOptional() ||
921 type.enum_def->attributes.Lookup("bit_flags") ||
922 type.enum_def->FindByValue(field->value.constant);
923 if (false == in_enum)
924 return Error("default value of " + field->value.constant +
925 " for field " + name + " is not part of enum " +
926 type.enum_def->name);
930 if (field->deprecated && struct_def.fixed)
931 return Error("can't deprecate fields in a struct");
933 auto cpp_type = field->attributes.Lookup("cpp_type");
936 return Error("cpp_type can only be used with a hashed field");
937 /// forcing cpp_ptr_type to 'naked' if unset
938 auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
940 auto val = new Value();
941 val->type = cpp_type->type;
942 val->constant = "naked";
943 field->attributes.Add("cpp_ptr_type", val);
947 field->shared = field->attributes.Lookup("shared") != nullptr;
948 if (field->shared && field->value.type.base_type != BASE_TYPE_STRING)
949 return Error("shared can only be defined on strings");
951 auto field_native_custom_alloc =
952 field->attributes.Lookup("native_custom_alloc");
953 if (field_native_custom_alloc)
955 "native_custom_alloc can only be used with a table or struct "
958 field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
959 if (field->native_inline && !IsStruct(field->value.type))
960 return Error("native_inline can only be defined on structs");
962 auto nested = field->attributes.Lookup("nested_flatbuffer");
964 if (nested->type.base_type != BASE_TYPE_STRING)
966 "nested_flatbuffer attribute must be a string (the root type)");
967 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
969 "nested_flatbuffer attribute may only apply to a vector of ubyte");
970 // This will cause an error if the root type of the nested flatbuffer
971 // wasn't defined elsewhere.
972 field->nested_flatbuffer = LookupCreateStruct(nested->constant);
975 if (field->attributes.Lookup("flexbuffer")) {
976 field->flexbuffer = true;
977 uses_flexbuffers_ = true;
978 if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
979 return Error("flexbuffer attribute may only apply to a vector of ubyte");
983 if (!IsScalar(typefield->value.type.base_type)) {
984 // this is a union vector field
985 typefield->presence = field->presence;
987 // If this field is a union, and it has a manually assigned id,
988 // the automatically added type field should have an id as well (of N - 1).
989 auto attr = field->attributes.Lookup("id");
991 const auto &id_str = attr->constant;
993 const auto done = !atot(id_str.c_str(), *this, &id).Check();
994 if (done && id > 0) {
995 auto val = new Value();
996 val->type = attr->type;
997 val->constant = NumToString(id - 1);
998 typefield->attributes.Add("id", val);
1001 "a union type effectively adds two fields with non-negative ids, "
1002 "its id must be that of the second field (the first field is "
1003 "the type field and not explicitly declared in the schema);\n"
1005 field->name + ", id: " + id_str);
1008 // if this field is a union that is deprecated,
1009 // the automatically added type field should be deprecated as well
1010 if (field->deprecated) { typefield->deprecated = true; }
1017 CheckedError Parser::ParseString(Value &val, bool use_string_pooling) {
1018 auto s = attribute_;
1019 EXPECT(kTokenStringConstant);
1020 if (use_string_pooling) {
1021 val.constant = NumToString(builder_.CreateSharedString(s).o);
1023 val.constant = NumToString(builder_.CreateString(s).o);
1028 CheckedError Parser::ParseComma() {
1029 if (!opts.protobuf_ascii_alike) EXPECT(',');
1033 CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
1034 size_t parent_fieldn,
1035 const StructDef *parent_struct_def,
1036 uoffset_t count, bool inside_vector) {
1037 switch (val.type.base_type) {
1038 case BASE_TYPE_UNION: {
1039 FLATBUFFERS_ASSERT(field);
1040 std::string constant;
1041 Vector<uint8_t> *vector_of_union_types = nullptr;
1042 // Find corresponding type field we may have already parsed.
1043 for (auto elem = field_stack_.rbegin() + count;
1044 elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) {
1045 auto &type = elem->second->value.type;
1046 if (type.enum_def == val.type.enum_def) {
1047 if (inside_vector) {
1048 if (IsVector(type) && type.element == BASE_TYPE_UTYPE) {
1049 // Vector of union type field.
1051 ECHECK(atot(elem->first.constant.c_str(), *this, &offset));
1052 vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>(
1053 builder_.GetCurrentBufferPointer() + builder_.GetSize() -
1058 if (type.base_type == BASE_TYPE_UTYPE) {
1059 // Union type field.
1060 constant = elem->first.constant;
1066 if (constant.empty() && !inside_vector) {
1067 // We haven't seen the type field yet. Sadly a lot of JSON writers
1068 // output these in alphabetical order, meaning it comes after this
1069 // value. So we scan past the value to find it, then come back here.
1070 // We currently don't do this for vectors of unions because the
1071 // scanning/serialization logic would get very complicated.
1072 auto type_name = field->name + UnionTypeFieldSuffix();
1073 FLATBUFFERS_ASSERT(parent_struct_def);
1074 auto type_field = parent_struct_def->fields.Lookup(type_name);
1075 FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField().
1076 // Remember where we are in the source file, so we can come back here.
1077 auto backup = *static_cast<ParserState *>(this);
1078 ECHECK(SkipAnyJsonValue()); // The table.
1079 ECHECK(ParseComma());
1080 auto next_name = attribute_;
1081 if (Is(kTokenStringConstant)) {
1084 EXPECT(kTokenIdentifier);
1086 if (next_name == type_name) {
1088 ParseDepthGuard depth_guard(this);
1089 ECHECK(depth_guard.Check());
1090 Value type_val = type_field->value;
1091 ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr, 0));
1092 constant = type_val.constant;
1093 // Got the information we needed, now rewind:
1094 *static_cast<ParserState *>(this) = backup;
1097 if (constant.empty() && !vector_of_union_types) {
1098 return Error("missing type field for this union value: " + field->name);
1101 if (vector_of_union_types) {
1102 enum_idx = vector_of_union_types->Get(count);
1104 ECHECK(atot(constant.c_str(), *this, &enum_idx));
1106 auto enum_val = val.type.enum_def->ReverseLookup(enum_idx, true);
1107 if (!enum_val) return Error("illegal type id for: " + field->name);
1108 if (enum_val->union_type.base_type == BASE_TYPE_STRUCT) {
1109 ECHECK(ParseTable(*enum_val->union_type.struct_def, &val.constant,
1111 if (enum_val->union_type.struct_def->fixed) {
1112 // All BASE_TYPE_UNION values are offsets, so turn this into one.
1113 SerializeStruct(*enum_val->union_type.struct_def, val);
1114 builder_.ClearOffsets();
1115 val.constant = NumToString(builder_.GetSize());
1117 } else if (IsString(enum_val->union_type)) {
1118 ECHECK(ParseString(val, field->shared));
1120 FLATBUFFERS_ASSERT(false);
1124 case BASE_TYPE_STRUCT:
1125 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1127 case BASE_TYPE_STRING: {
1128 ECHECK(ParseString(val, field->shared));
1131 case BASE_TYPE_VECTOR: {
1133 ECHECK(ParseVector(val.type.VectorType(), &off, field, parent_fieldn));
1134 val.constant = NumToString(off);
1137 case BASE_TYPE_ARRAY: {
1138 ECHECK(ParseArray(val));
1142 case BASE_TYPE_UINT:
1143 case BASE_TYPE_LONG:
1144 case BASE_TYPE_ULONG: {
1145 if (field && field->attributes.Lookup("hash") &&
1146 (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
1147 ECHECK(ParseHash(val, field));
1149 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1154 ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1160 void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
1161 SerializeStruct(builder_, struct_def, val);
1164 void Parser::SerializeStruct(FlatBufferBuilder &builder,
1165 const StructDef &struct_def, const Value &val) {
1166 FLATBUFFERS_ASSERT(val.constant.length() == struct_def.bytesize);
1167 builder.Align(struct_def.minalign);
1168 builder.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
1169 struct_def.bytesize);
1170 builder.AddStructOffset(val.offset, builder.GetSize());
1173 template<typename F>
1174 CheckedError Parser::ParseTableDelimiters(size_t &fieldn,
1175 const StructDef *struct_def, F body) {
1176 // We allow tables both as JSON object{ .. } with field names
1177 // or vector[..] with all fields in order
1178 char terminator = '}';
1179 bool is_nested_vector = struct_def && Is('[');
1180 if (is_nested_vector) {
1187 if ((!opts.strict_json || !fieldn) && Is(terminator)) break;
1189 if (is_nested_vector) {
1190 if (fieldn >= struct_def->fields.vec.size()) {
1191 return Error("too many unnamed fields in nested array");
1193 name = struct_def->fields.vec[fieldn]->name;
1196 if (Is(kTokenStringConstant)) {
1199 EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
1201 if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
1203 ECHECK(body(name, fieldn, struct_def));
1204 if (Is(terminator)) break;
1205 ECHECK(ParseComma());
1208 if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
1209 return Error("wrong number of unnamed fields in table vector");
1214 CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
1215 uoffset_t *ovalue) {
1216 ParseDepthGuard depth_guard(this);
1217 ECHECK(depth_guard.Check());
1219 size_t fieldn_outer = 0;
1220 auto err = ParseTableDelimiters(
1221 fieldn_outer, &struct_def,
1222 [&](const std::string &name, size_t &fieldn,
1223 const StructDef *struct_def_inner) -> CheckedError {
1224 if (name == "$schema") {
1225 ECHECK(Expect(kTokenStringConstant));
1228 auto field = struct_def_inner->fields.Lookup(name);
1230 if (!opts.skip_unexpected_fields_in_json) {
1231 return Error("unknown field: " + name);
1233 ECHECK(SkipAnyJsonValue());
1236 if (IsIdent("null") && !IsScalar(field->value.type.base_type)) {
1237 ECHECK(Next()); // Ignore this field.
1239 Value val = field->value;
1240 if (field->flexbuffer) {
1241 flexbuffers::Builder builder(1024,
1242 flexbuffers::BUILDER_FLAG_SHARE_ALL);
1243 ECHECK(ParseFlexBufferValue(&builder));
1245 // Force alignment for nested flexbuffer
1246 builder_.ForceVectorAlignment(builder.GetSize(), sizeof(uint8_t),
1247 sizeof(largest_scalar_t));
1248 auto off = builder_.CreateVector(builder.GetBuffer());
1249 val.constant = NumToString(off.o);
1250 } else if (field->nested_flatbuffer) {
1252 ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner));
1254 ECHECK(ParseAnyValue(val, field, fieldn, struct_def_inner, 0));
1256 // Hardcoded insertion-sort with error-check.
1257 // If fields are specified in order, then this loop exits
1259 auto elem = field_stack_.rbegin();
1260 for (; elem != field_stack_.rbegin() + fieldn; ++elem) {
1261 auto existing_field = elem->second;
1262 if (existing_field == field)
1263 return Error("field set more than once: " + field->name);
1264 if (existing_field->value.offset < field->value.offset) break;
1266 // Note: elem points to before the insertion point, thus .base()
1267 // points to the correct spot.
1268 field_stack_.insert(elem.base(), std::make_pair(val, field));
1276 // Check if all required fields are parsed.
1277 for (auto field_it = struct_def.fields.vec.begin();
1278 field_it != struct_def.fields.vec.end(); ++field_it) {
1279 auto required_field = *field_it;
1280 if (!required_field->IsRequired()) { continue; }
1282 for (auto pf_it = field_stack_.end() - fieldn_outer;
1283 pf_it != field_stack_.end(); ++pf_it) {
1284 auto parsed_field = pf_it->second;
1285 if (parsed_field == required_field) {
1291 return Error("required field is missing: " + required_field->name +
1292 " in " + struct_def.name);
1296 if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size())
1297 return Error("struct: wrong number of initializers: " + struct_def.name);
1299 auto start = struct_def.fixed ? builder_.StartStruct(struct_def.minalign)
1300 : builder_.StartTable();
1302 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size;
1304 // Go through elements in reverse, since we're building the data backwards.
1305 for (auto it = field_stack_.rbegin();
1306 it != field_stack_.rbegin() + fieldn_outer; ++it) {
1307 auto &field_value = it->first;
1308 auto field = it->second;
1309 if (!struct_def.sortbysize ||
1310 size == SizeOf(field_value.type.base_type)) {
1311 switch (field_value.type.base_type) {
1313 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
1314 case BASE_TYPE_ ## ENUM: \
1315 builder_.Pad(field->padding); \
1316 if (struct_def.fixed) { \
1318 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1319 builder_.PushElement(val); \
1321 CTYPE val, valdef; \
1322 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1323 ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
1324 builder_.AddElement(field_value.offset, val, valdef); \
1327 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
1328 #undef FLATBUFFERS_TD
1329 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
1330 case BASE_TYPE_ ## ENUM: \
1331 builder_.Pad(field->padding); \
1332 if (IsStruct(field->value.type)) { \
1333 SerializeStruct(*field->value.type.struct_def, field_value); \
1336 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1337 builder_.AddOffset(field_value.offset, val); \
1340 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
1341 #undef FLATBUFFERS_TD
1342 case BASE_TYPE_ARRAY:
1343 builder_.Pad(field->padding);
1345 reinterpret_cast<const uint8_t*>(field_value.constant.c_str()),
1346 InlineSize(field_value.type));
1353 for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back();
1355 if (struct_def.fixed) {
1356 builder_.ClearOffsets();
1357 builder_.EndStruct();
1358 FLATBUFFERS_ASSERT(value);
1359 // Temporarily store this struct in the value string, since it is to
1360 // be serialized in-place elsewhere.
1362 reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
1363 struct_def.bytesize);
1364 builder_.PopBytes(struct_def.bytesize);
1365 FLATBUFFERS_ASSERT(!ovalue);
1367 auto val = builder_.EndTable(start);
1368 if (ovalue) *ovalue = val;
1369 if (value) *value = NumToString(val);
1374 template<typename F>
1375 CheckedError Parser::ParseVectorDelimiters(uoffset_t &count, F body) {
1378 if ((!opts.strict_json || !count) && Is(']')) break;
1379 ECHECK(body(count));
1382 ECHECK(ParseComma());
1388 static bool CompareSerializedScalars(const uint8_t *a, const uint8_t *b,
1389 const FieldDef &key) {
1390 switch (key.value.type.base_type) {
1391 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
1392 case BASE_TYPE_##ENUM: { \
1393 CTYPE def = static_cast<CTYPE>(0); \
1394 if (!a || !b) { StringToNumber(key.value.constant.c_str(), &def); } \
1395 const auto av = a ? ReadScalar<CTYPE>(a) : def; \
1396 const auto bv = b ? ReadScalar<CTYPE>(b) : def; \
1399 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
1400 #undef FLATBUFFERS_TD
1402 FLATBUFFERS_ASSERT(false && "scalar type expected");
1408 static bool CompareTablesByScalarKey(const Offset<Table> *_a,
1409 const Offset<Table> *_b,
1410 const FieldDef &key) {
1411 const voffset_t offset = key.value.offset;
1412 // Indirect offset pointer to table pointer.
1413 auto a = reinterpret_cast<const uint8_t *>(_a) + ReadScalar<uoffset_t>(_a);
1414 auto b = reinterpret_cast<const uint8_t *>(_b) + ReadScalar<uoffset_t>(_b);
1415 // Fetch field address from table.
1416 a = reinterpret_cast<const Table *>(a)->GetAddressOf(offset);
1417 b = reinterpret_cast<const Table *>(b)->GetAddressOf(offset);
1418 return CompareSerializedScalars(a, b, key);
1421 static bool CompareTablesByStringKey(const Offset<Table> *_a,
1422 const Offset<Table> *_b,
1423 const FieldDef &key) {
1424 const voffset_t offset = key.value.offset;
1425 // Indirect offset pointer to table pointer.
1426 auto a = reinterpret_cast<const uint8_t *>(_a) + ReadScalar<uoffset_t>(_a);
1427 auto b = reinterpret_cast<const uint8_t *>(_b) + ReadScalar<uoffset_t>(_b);
1428 // Fetch field address from table.
1429 a = reinterpret_cast<const Table *>(a)->GetAddressOf(offset);
1430 b = reinterpret_cast<const Table *>(b)->GetAddressOf(offset);
1432 // Indirect offset pointer to string pointer.
1433 a += ReadScalar<uoffset_t>(a);
1434 b += ReadScalar<uoffset_t>(b);
1435 return *reinterpret_cast<const String *>(a) <
1436 *reinterpret_cast<const String *>(b);
1438 return a ? true : false;
1442 static void SwapSerializedTables(Offset<Table> *a, Offset<Table> *b) {
1443 // These are serialized offsets, so are relative where they are
1444 // stored in memory, so compute the distance between these pointers:
1445 ptrdiff_t diff = (b - a) * sizeof(Offset<Table>);
1446 FLATBUFFERS_ASSERT(diff >= 0); // Guaranteed by SimpleQsort.
1447 auto udiff = static_cast<uoffset_t>(diff);
1448 a->o = EndianScalar(ReadScalar<uoffset_t>(a) - udiff);
1449 b->o = EndianScalar(ReadScalar<uoffset_t>(b) + udiff);
1453 // See below for why we need our own sort :(
1454 template<typename T, typename F, typename S>
1455 void SimpleQsort(T *begin, T *end, size_t width, F comparator, S swapper) {
1456 if (end - begin <= static_cast<ptrdiff_t>(width)) return;
1457 auto l = begin + width;
1460 if (comparator(begin, l)) {
1469 SimpleQsort(begin, l, width, comparator, swapper);
1470 SimpleQsort(r, end, width, comparator, swapper);
1473 CheckedError Parser::ParseAlignAttribute(const std::string &align_constant,
1474 size_t min_align, size_t *align) {
1475 // Use uint8_t to avoid problems with size_t==`unsigned long` on LP64.
1476 uint8_t align_value;
1477 if (StringToNumber(align_constant.c_str(), &align_value) &&
1478 VerifyAlignmentRequirements(static_cast<size_t>(align_value),
1480 *align = align_value;
1483 return Error("unexpected force_align value '" + align_constant +
1484 "', alignment must be a power of two integer ranging from the "
1485 "type\'s natural alignment " +
1486 NumToString(min_align) + " to " +
1487 NumToString(FLATBUFFERS_MAX_ALIGNMENT));
1490 CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
1491 FieldDef *field, size_t fieldn) {
1492 uoffset_t count = 0;
1493 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1496 ECHECK(ParseAnyValue(val, field, fieldn, nullptr, count, true));
1497 field_stack_.push_back(std::make_pair(val, nullptr));
1502 const size_t len = count * InlineSize(type) / InlineAlignment(type);
1503 const size_t elemsize = InlineAlignment(type);
1504 const auto force_align = field->attributes.Lookup("force_align");
1507 ECHECK(ParseAlignAttribute(force_align->constant, 1, &align));
1508 if (align > 1) { builder_.ForceVectorAlignment(len, elemsize, align); }
1511 builder_.StartVector(len, elemsize);
1512 for (uoffset_t i = 0; i < count; i++) {
1513 // start at the back, since we're building the data backwards.
1514 auto &val = field_stack_.back().first;
1515 switch (val.type.base_type) {
1517 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE,...) \
1518 case BASE_TYPE_ ## ENUM: \
1519 if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
1522 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1523 builder_.PushElement(elem); \
1526 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1527 #undef FLATBUFFERS_TD
1530 field_stack_.pop_back();
1533 builder_.ClearOffsets();
1534 *ovalue = builder_.EndVector(count);
1536 if (type.base_type == BASE_TYPE_STRUCT && type.struct_def->has_key) {
1537 // We should sort this vector. Find the key first.
1538 const FieldDef *key = nullptr;
1539 for (auto it = type.struct_def->fields.vec.begin();
1540 it != type.struct_def->fields.vec.end(); ++it) {
1546 FLATBUFFERS_ASSERT(key);
1548 // We can't use std::sort because for structs the size is not known at
1549 // compile time, and for tables our iterators dereference offsets, so can't
1550 // be used to swap elements.
1551 // And we can't use C qsort either, since that would force use to use
1552 // globals, making parsing thread-unsafe.
1553 // So for now, we use SimpleQsort above.
1554 // TODO: replace with something better, preferably not recursive.
1556 if (type.struct_def->fixed) {
1557 const voffset_t offset = key->value.offset;
1558 const size_t struct_size = type.struct_def->bytesize;
1560 reinterpret_cast<VectorOfAny *>(builder_.GetCurrentBufferPointer());
1561 SimpleQsort<uint8_t>(
1562 v->Data(), v->Data() + v->size() * type.struct_def->bytesize,
1563 type.struct_def->bytesize,
1564 [offset, key](const uint8_t *a, const uint8_t *b) -> bool {
1565 return CompareSerializedScalars(a + offset, b + offset, *key);
1567 [struct_size](uint8_t *a, uint8_t *b) {
1569 for (size_t i = 0; i < struct_size; i++) { std::swap(a[i], b[i]); }
1572 auto v = reinterpret_cast<Vector<Offset<Table>> *>(
1573 builder_.GetCurrentBufferPointer());
1574 // Here also can't use std::sort. We do have an iterator type for it,
1575 // but it is non-standard as it will dereference the offsets, and thus
1576 // can't be used to swap elements.
1577 if (key->value.type.base_type == BASE_TYPE_STRING) {
1578 SimpleQsort<Offset<Table>>(
1579 v->data(), v->data() + v->size(), 1,
1580 [key](const Offset<Table> *_a, const Offset<Table> *_b) -> bool {
1581 return CompareTablesByStringKey(_a, _b, *key);
1583 SwapSerializedTables);
1585 SimpleQsort<Offset<Table>>(
1586 v->data(), v->data() + v->size(), 1,
1587 [key](const Offset<Table> *_a, const Offset<Table> *_b) -> bool {
1588 return CompareTablesByScalarKey(_a, _b, *key);
1590 SwapSerializedTables);
1597 CheckedError Parser::ParseArray(Value &array) {
1598 std::vector<Value> stack;
1599 FlatBufferBuilder builder;
1600 const auto &type = array.type.VectorType();
1601 auto length = array.type.fixed_length;
1602 uoffset_t count = 0;
1603 auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1604 vector_emplace_back(&stack, Value());
1605 auto &val = stack.back();
1607 if (IsStruct(type)) {
1608 ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1610 ECHECK(ParseSingleValue(nullptr, val, false));
1615 if (length != count) return Error("Fixed-length array size is incorrect.");
1617 for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
1620 switch (val.type.base_type) {
1621 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
1622 case BASE_TYPE_ ## ENUM: \
1623 if (IsStruct(val.type)) { \
1624 SerializeStruct(builder, *val.type.struct_def, val); \
1627 ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1628 builder.PushElement(elem); \
1631 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1632 #undef FLATBUFFERS_TD
1633 default: FLATBUFFERS_ASSERT(0);
1638 array.constant.assign(
1639 reinterpret_cast<const char *>(builder.GetCurrentBufferPointer()),
1640 InlineSize(array.type));
1644 CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
1646 const StructDef *parent_struct_def) {
1647 if (token_ == '[') { // backwards compat for 'legacy' ubyte buffers
1648 ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
1650 auto cursor_at_value_begin = cursor_;
1651 ECHECK(SkipAnyJsonValue());
1652 std::string substring(cursor_at_value_begin - 1, cursor_ - 1);
1654 // Create and initialize new parser
1655 Parser nested_parser;
1656 FLATBUFFERS_ASSERT(field->nested_flatbuffer);
1657 nested_parser.root_struct_def_ = field->nested_flatbuffer;
1658 nested_parser.enums_ = enums_;
1659 nested_parser.opts = opts;
1660 nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
1661 nested_parser.parse_depth_counter_ = parse_depth_counter_;
1662 // Parse JSON substring into new flatbuffer builder using nested_parser
1663 bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
1665 // Clean nested_parser to avoid deleting the elements in
1666 // the SymbolTables on destruction
1667 nested_parser.enums_.dict.clear();
1668 nested_parser.enums_.vec.clear();
1670 if (!ok) { ECHECK(Error(nested_parser.error_)); }
1671 // Force alignment for nested flatbuffer
1672 builder_.ForceVectorAlignment(
1673 nested_parser.builder_.GetSize(), sizeof(uint8_t),
1674 nested_parser.builder_.GetBufferMinAlignment());
1676 auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
1677 nested_parser.builder_.GetSize());
1678 val.constant = NumToString(off.o);
1683 CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) {
1687 auto name = attribute_;
1688 if (false == (Is(kTokenIdentifier) || Is(kTokenStringConstant)))
1689 return Error("attribute name must be either identifier or string: " +
1691 if (known_attributes_.find(name) == known_attributes_.end())
1692 return Error("user define attributes must be declared before use: " +
1695 auto e = new Value();
1696 if (attributes->Add(name, e)) Warning("attribute already found: " + name);
1699 ECHECK(ParseSingleValue(&name, *e, true));
1711 CheckedError Parser::ParseEnumFromString(const Type &type,
1712 std::string *result) {
1713 const auto base_type =
1714 type.enum_def ? type.enum_def->underlying_type.base_type : type.base_type;
1715 if (!IsInteger(base_type)) return Error("not a valid value for this field");
1717 for (size_t pos = 0; pos != std::string::npos;) {
1718 const auto delim = attribute_.find_first_of(' ', pos);
1719 const auto last = (std::string::npos == delim);
1720 auto word = attribute_.substr(pos, !last ? delim - pos : std::string::npos);
1721 pos = !last ? delim + 1 : std::string::npos;
1722 const EnumVal *ev = nullptr;
1723 if (type.enum_def) {
1724 ev = type.enum_def->Lookup(word);
1726 auto dot = word.find_first_of('.');
1727 if (std::string::npos == dot)
1728 return Error("enum values need to be qualified by an enum type");
1729 auto enum_def_str = word.substr(0, dot);
1730 const auto enum_def = LookupEnum(enum_def_str);
1731 if (!enum_def) return Error("unknown enum: " + enum_def_str);
1732 auto enum_val_str = word.substr(dot + 1);
1733 ev = enum_def->Lookup(enum_val_str);
1735 if (!ev) return Error("unknown enum value: " + word);
1736 u64 |= ev->GetAsUInt64();
1738 *result = IsUnsigned(base_type) ? NumToString(u64)
1739 : NumToString(static_cast<int64_t>(u64));
1743 CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
1744 FLATBUFFERS_ASSERT(field);
1745 Value *hash_name = field->attributes.Lookup("hash");
1746 switch (e.type.base_type) {
1747 case BASE_TYPE_SHORT: {
1748 auto hash = FindHashFunction16(hash_name->constant.c_str());
1749 int16_t hashed_value = static_cast<int16_t>(hash(attribute_.c_str()));
1750 e.constant = NumToString(hashed_value);
1753 case BASE_TYPE_USHORT: {
1754 auto hash = FindHashFunction16(hash_name->constant.c_str());
1755 uint16_t hashed_value = hash(attribute_.c_str());
1756 e.constant = NumToString(hashed_value);
1759 case BASE_TYPE_INT: {
1760 auto hash = FindHashFunction32(hash_name->constant.c_str());
1761 int32_t hashed_value = static_cast<int32_t>(hash(attribute_.c_str()));
1762 e.constant = NumToString(hashed_value);
1765 case BASE_TYPE_UINT: {
1766 auto hash = FindHashFunction32(hash_name->constant.c_str());
1767 uint32_t hashed_value = hash(attribute_.c_str());
1768 e.constant = NumToString(hashed_value);
1771 case BASE_TYPE_LONG: {
1772 auto hash = FindHashFunction64(hash_name->constant.c_str());
1773 int64_t hashed_value = static_cast<int64_t>(hash(attribute_.c_str()));
1774 e.constant = NumToString(hashed_value);
1777 case BASE_TYPE_ULONG: {
1778 auto hash = FindHashFunction64(hash_name->constant.c_str());
1779 uint64_t hashed_value = hash(attribute_.c_str());
1780 e.constant = NumToString(hashed_value);
1783 default: FLATBUFFERS_ASSERT(0);
1789 CheckedError Parser::TokenError() {
1790 return Error("cannot parse value starting with: " + TokenToStringId(token_));
1793 // Re-pack helper (ParseSingleValue) to normalize defaults of scalars.
1794 template<typename T> inline void SingleValueRepack(Value &e, T val) {
1795 // Remove leading zeros.
1796 if (IsInteger(e.type.base_type)) { e.constant = NumToString(val); }
1798 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
1799 // Normalize defaults NaN to unsigned quiet-NaN(0) if value was parsed from
1800 // hex-float literal.
1801 static inline void SingleValueRepack(Value &e, float val) {
1802 if (val != val) e.constant = "nan";
1804 static inline void SingleValueRepack(Value &e, double val) {
1805 if (val != val) e.constant = "nan";
1809 CheckedError Parser::ParseFunction(const std::string *name, Value &e) {
1810 ParseDepthGuard depth_guard(this);
1811 ECHECK(depth_guard.Check());
1813 // Copy name, attribute will be changed on NEXT().
1814 const auto functionname = attribute_;
1815 if (!IsFloat(e.type.base_type)) {
1816 return Error(functionname + ": type of argument mismatch, expecting: " +
1817 kTypeNames[BASE_TYPE_DOUBLE] +
1818 ", found: " + kTypeNames[e.type.base_type] +
1819 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1823 ECHECK(ParseSingleValue(name, e, false));
1825 // calculate with double precision
1827 ECHECK(atot(e.constant.c_str(), *this, &x));
1829 auto func_match = false;
1830 #define FLATBUFFERS_FN_DOUBLE(name, op) \
1831 if (!func_match && functionname == name) { y = op; func_match = true; }
1832 FLATBUFFERS_FN_DOUBLE("deg", x / kPi * 180);
1833 FLATBUFFERS_FN_DOUBLE("rad", x * kPi / 180);
1834 FLATBUFFERS_FN_DOUBLE("sin", sin(x));
1835 FLATBUFFERS_FN_DOUBLE("cos", cos(x));
1836 FLATBUFFERS_FN_DOUBLE("tan", tan(x));
1837 FLATBUFFERS_FN_DOUBLE("asin", asin(x));
1838 FLATBUFFERS_FN_DOUBLE("acos", acos(x));
1839 FLATBUFFERS_FN_DOUBLE("atan", atan(x));
1840 // TODO(wvo): add more useful conversion functions here.
1841 #undef FLATBUFFERS_FN_DOUBLE
1843 if (true != func_match) {
1844 return Error(std::string("Unknown conversion function: ") + functionname +
1845 ", field name: " + (name ? *name : "") +
1846 ", value: " + e.constant);
1848 e.constant = NumToString(y);
1852 CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
1853 bool check, Value &e, BaseType req,
1855 bool match = dtoken == token_;
1857 FLATBUFFERS_ASSERT(*destmatch == false);
1859 e.constant = attribute_;
1860 // Check token match
1862 if (e.type.base_type == BASE_TYPE_NONE) {
1863 e.type.base_type = req;
1866 std::string("type mismatch: expecting: ") +
1867 kTypeNames[e.type.base_type] + ", found: " + kTypeNames[req] +
1868 ", name: " + (name ? *name : "") + ", value: " + e.constant);
1871 // The exponent suffix of hexadecimal float-point number is mandatory.
1872 // A hex-integer constant is forbidden as an initializer of float number.
1873 if ((kTokenFloatConstant != dtoken) && IsFloat(e.type.base_type)) {
1874 const auto &s = e.constant;
1875 const auto k = s.find_first_of("0123456789.");
1876 if ((std::string::npos != k) && (s.length() > (k + 1)) &&
1877 (s[k] == '0' && is_alpha_char(s[k + 1], 'X')) &&
1878 (std::string::npos == s.find_first_of("pP", k + 2))) {
1880 "invalid number, the exponent suffix of hexadecimal "
1881 "floating-point literals is mandatory: \"" +
1890 CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
1892 const auto in_type = e.type.base_type;
1893 const auto is_tok_ident = (token_ == kTokenIdentifier);
1894 const auto is_tok_string = (token_ == kTokenStringConstant);
1896 // First see if this could be a conversion function:
1897 if (is_tok_ident && *cursor_ == '(') { return ParseFunction(name, e); }
1902 #define IF_ECHECK_(force, dtoken, check, req) \
1903 if (!match && ((check) || IsConstTrue(force))) \
1904 ECHECK(TryTypedValue(name, dtoken, check, e, req, &match))
1905 #define TRY_ECHECK(dtoken, check, req) IF_ECHECK_(false, dtoken, check, req)
1906 #define FORCE_ECHECK(dtoken, check, req) IF_ECHECK_(true, dtoken, check, req)
1909 if (is_tok_ident || is_tok_string) {
1910 const auto kTokenStringOrIdent = token_;
1911 // The string type is a most probable type, check it first.
1912 TRY_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1915 // avoid escaped and non-ascii in the string
1916 if (!match && is_tok_string && IsScalar(in_type) &&
1917 !attr_is_trivial_ascii_string_) {
1919 std::string("type mismatch or invalid value, an initializer of "
1920 "non-string field must be trivial ASCII string: type: ") +
1921 kTypeNames[in_type] + ", name: " + (name ? *name : "") +
1922 ", value: " + attribute_);
1925 // A boolean as true/false. Boolean as Integer check below.
1926 if (!match && IsBool(in_type)) {
1927 auto is_true = attribute_ == "true";
1928 if (is_true || attribute_ == "false") {
1929 attribute_ = is_true ? "1" : "0";
1930 // accepts both kTokenStringConstant and kTokenIdentifier
1931 TRY_ECHECK(kTokenStringOrIdent, IsBool(in_type), BASE_TYPE_BOOL);
1934 // Check for optional scalars.
1935 if (!match && IsScalar(in_type) && attribute_ == "null") {
1936 e.constant = "null";
1940 // Check if this could be a string/identifier enum value.
1941 // Enum can have only true integer base type.
1942 if (!match && IsInteger(in_type) && !IsBool(in_type) &&
1943 IsIdentifierStart(*attribute_.c_str())) {
1944 ECHECK(ParseEnumFromString(e.type, &e.constant));
1948 // Parse a float/integer number from the string.
1949 // A "scalar-in-string" value needs extra checks.
1950 if (!match && is_tok_string && IsScalar(in_type)) {
1951 // Strip trailing whitespaces from attribute_.
1952 auto last_non_ws = attribute_.find_last_not_of(' ');
1953 if (std::string::npos != last_non_ws) attribute_.resize(last_non_ws + 1);
1954 if (IsFloat(e.type.base_type)) {
1955 // The functions strtod() and strtof() accept both 'nan' and
1956 // 'nan(number)' literals. While 'nan(number)' is rejected by the parser
1957 // as an unsupported function if is_tok_ident is true.
1958 if (attribute_.find_last_of(')') != std::string::npos) {
1959 return Error("invalid number: " + attribute_);
1963 // Float numbers or nan, inf, pi, etc.
1964 TRY_ECHECK(kTokenStringOrIdent, IsFloat(in_type), BASE_TYPE_FLOAT);
1965 // An integer constant in string.
1966 TRY_ECHECK(kTokenStringOrIdent, IsInteger(in_type), BASE_TYPE_INT);
1967 // Unknown tokens will be interpreted as string type.
1968 // An attribute value may be a scalar or string constant.
1969 FORCE_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1972 // Try a float number.
1973 TRY_ECHECK(kTokenFloatConstant, IsFloat(in_type), BASE_TYPE_FLOAT);
1974 // Integer token can init any scalar (integer of float).
1975 FORCE_ECHECK(kTokenIntegerConstant, IsScalar(in_type), BASE_TYPE_INT);
1977 // Match empty vectors for default-empty-vectors.
1978 if (!match && IsVector(e.type) && token_ == '[') {
1980 if (token_ != ']') { return Error("Expected `]` in vector default"); }
1992 msg += "Cannot assign token starting with '" + TokenToStringId(token_) +
1993 "' to value of <" + std::string(kTypeNames[in_type]) + "> type.";
1996 const auto match_type = e.type.base_type; // may differ from in_type
1997 // The check_now flag must be true when parse a fbs-schema.
1998 // This flag forces to check default scalar values or metadata of field.
1999 // For JSON parser the flag should be false.
2000 // If it is set for JSON each value will be checked twice (see ParseTable).
2001 // Special case 'null' since atot can't handle that.
2002 if (check_now && IsScalar(match_type) && e.constant != "null") {
2004 switch (match_type) {
2005 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
2006 case BASE_TYPE_ ## ENUM: {\
2008 ECHECK(atot(e.constant.c_str(), *this, &val)); \
2009 SingleValueRepack(e, val); \
2011 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
2012 #undef FLATBUFFERS_TD
2020 StructDef *Parser::LookupCreateStruct(const std::string &name,
2021 bool create_if_new, bool definition) {
2022 std::string qualified_name = current_namespace_->GetFullyQualifiedName(name);
2023 // See if it exists pre-declared by an unqualified use.
2024 auto struct_def = LookupStruct(name);
2025 if (struct_def && struct_def->predecl) {
2027 // Make sure it has the current namespace, and is registered under its
2029 struct_def->defined_namespace = current_namespace_;
2030 structs_.Move(name, qualified_name);
2034 // See if it exists pre-declared by an qualified use.
2035 struct_def = LookupStruct(qualified_name);
2036 if (struct_def && struct_def->predecl) {
2038 // Make sure it has the current namespace.
2039 struct_def->defined_namespace = current_namespace_;
2043 if (!definition && !struct_def) {
2044 struct_def = LookupStructThruParentNamespaces(name);
2046 if (!struct_def && create_if_new) {
2047 struct_def = new StructDef();
2049 structs_.Add(qualified_name, struct_def);
2050 struct_def->name = name;
2051 struct_def->defined_namespace = current_namespace_;
2053 // Not a definition.
2054 // Rather than failing, we create a "pre declared" StructDef, due to
2055 // circular references, and check for errors at the end of parsing.
2056 // It is defined in the current namespace, as the best guess what the
2057 // final namespace will be.
2058 structs_.Add(name, struct_def);
2059 struct_def->name = name;
2060 struct_def->defined_namespace = current_namespace_;
2061 struct_def->original_location.reset(
2062 new std::string(file_being_parsed_ + ":" + NumToString(line_)));
2068 const EnumVal *EnumDef::MinValue() const {
2069 return vals.vec.empty() ? nullptr : vals.vec.front();
2071 const EnumVal *EnumDef::MaxValue() const {
2072 return vals.vec.empty() ? nullptr : vals.vec.back();
2075 template<typename T> static uint64_t EnumDistanceImpl(T e1, T e2) {
2076 if (e1 < e2) { std::swap(e1, e2); } // use std for scalars
2077 // Signed overflow may occur, use unsigned calculation.
2078 // The unsigned overflow is well-defined by C++ standard (modulo 2^n).
2079 return static_cast<uint64_t>(e1) - static_cast<uint64_t>(e2);
2082 uint64_t EnumDef::Distance(const EnumVal *v1, const EnumVal *v2) const {
2083 return IsUInt64() ? EnumDistanceImpl(v1->GetAsUInt64(), v2->GetAsUInt64())
2084 : EnumDistanceImpl(v1->GetAsInt64(), v2->GetAsInt64());
2087 std::string EnumDef::AllFlags() const {
2088 FLATBUFFERS_ASSERT(attributes.Lookup("bit_flags"));
2090 for (auto it = Vals().begin(); it != Vals().end(); ++it) {
2091 u64 |= (*it)->GetAsUInt64();
2093 return IsUInt64() ? NumToString(u64) : NumToString(static_cast<int64_t>(u64));
2096 EnumVal *EnumDef::ReverseLookup(int64_t enum_idx,
2097 bool skip_union_default) const {
2098 auto skip_first = static_cast<int>(is_union && skip_union_default);
2099 for (auto it = Vals().begin() + skip_first; it != Vals().end(); ++it) {
2100 if ((*it)->GetAsInt64() == enum_idx) { return *it; }
2105 EnumVal *EnumDef::FindByValue(const std::string &constant) const {
2109 uint64_t u64; // avoid reinterpret_cast of pointers
2110 done = StringToNumber(constant.c_str(), &u64);
2111 i64 = static_cast<int64_t>(u64);
2113 done = StringToNumber(constant.c_str(), &i64);
2115 FLATBUFFERS_ASSERT(done);
2116 if (!done) return nullptr;
2117 return ReverseLookup(i64, false);
2120 void EnumDef::SortByValue() {
2123 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
2124 return e1->GetAsUInt64() < e2->GetAsUInt64();
2127 std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
2128 return e1->GetAsInt64() < e2->GetAsInt64();
2132 void EnumDef::RemoveDuplicates() {
2133 // This method depends form SymbolTable implementation!
2134 // 1) vals.vec - owner (raw pointer)
2135 // 2) vals.dict - access map
2136 auto first = vals.vec.begin();
2137 auto last = vals.vec.end();
2138 if (first == last) return;
2139 auto result = first;
2140 while (++first != last) {
2141 if ((*result)->value != (*first)->value) {
2142 *(++result) = *first;
2145 for (auto it = vals.dict.begin(); it != vals.dict.end(); ++it) {
2146 if (it->second == ev) it->second = *result; // reassign
2148 delete ev; // delete enum value
2152 vals.vec.erase(++result, last);
2155 template<typename T> void EnumDef::ChangeEnumValue(EnumVal *ev, T new_value) {
2156 ev->value = static_cast<int64_t>(new_value);
2159 namespace EnumHelper {
2160 template<BaseType E> struct EnumValType { typedef int64_t type; };
2161 template<> struct EnumValType<BASE_TYPE_ULONG> { typedef uint64_t type; };
2162 } // namespace EnumHelper
2164 struct EnumValBuilder {
2165 EnumVal *CreateEnumerator(const std::string &ev_name) {
2166 FLATBUFFERS_ASSERT(!temp);
2167 auto first = enum_def.vals.vec.empty();
2169 temp = new EnumVal(ev_name, first ? 0 : enum_def.vals.vec.back()->value);
2173 EnumVal *CreateEnumerator(const std::string &ev_name, int64_t val) {
2174 FLATBUFFERS_ASSERT(!temp);
2176 temp = new EnumVal(ev_name, val);
2180 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator(const std::string &name) {
2181 FLATBUFFERS_ASSERT(temp);
2182 ECHECK(ValidateValue(&temp->value, false == user_value));
2183 FLATBUFFERS_ASSERT((temp->union_type.enum_def == nullptr) ||
2184 (temp->union_type.enum_def == &enum_def));
2185 auto not_unique = enum_def.vals.Add(name, temp);
2187 if (not_unique) return parser.Error("enum value already exists: " + name);
2191 FLATBUFFERS_CHECKED_ERROR AcceptEnumerator() {
2192 return AcceptEnumerator(temp->name);
2195 FLATBUFFERS_CHECKED_ERROR AssignEnumeratorValue(const std::string &value) {
2198 if (enum_def.IsUInt64()) {
2200 fit = StringToNumber(value.c_str(), &u64);
2201 temp->value = static_cast<int64_t>(u64); // well-defined since C++20.
2204 fit = StringToNumber(value.c_str(), &i64);
2207 if (!fit) return parser.Error("enum value does not fit, \"" + value + "\"");
2211 template<BaseType E, typename CTYPE>
2212 inline FLATBUFFERS_CHECKED_ERROR ValidateImpl(int64_t *ev, int m) {
2213 typedef typename EnumHelper::EnumValType<E>::type T; // int64_t or uint64_t
2214 static_assert(sizeof(T) == sizeof(int64_t), "invalid EnumValType");
2215 const auto v = static_cast<T>(*ev);
2216 auto up = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::max)());
2217 auto dn = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::lowest)());
2218 if (v < dn || v > (up - m)) {
2219 return parser.Error("enum value does not fit, \"" + NumToString(v) +
2220 (m ? " + 1\"" : "\"") + " out of " +
2221 TypeToIntervalString<CTYPE>());
2223 *ev = static_cast<int64_t>(v + m); // well-defined since C++20.
2227 FLATBUFFERS_CHECKED_ERROR ValidateValue(int64_t *ev, bool next) {
2229 switch (enum_def.underlying_type.base_type) {
2230 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
2231 case BASE_TYPE_##ENUM: { \
2232 if (!IsInteger(BASE_TYPE_##ENUM)) break; \
2233 return ValidateImpl<BASE_TYPE_##ENUM, CTYPE>(ev, next ? 1 : 0); \
2235 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
2236 #undef FLATBUFFERS_TD
2240 return parser.Error("fatal: invalid enum underlying type");
2243 EnumValBuilder(Parser &_parser, EnumDef &_enum_def)
2245 enum_def(_enum_def),
2247 user_value(false) {}
2249 ~EnumValBuilder() { delete temp; }
2257 CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest) {
2258 std::vector<std::string> enum_comment = doc_comment_;
2260 std::string enum_name = attribute_;
2261 EXPECT(kTokenIdentifier);
2263 ECHECK(StartEnum(enum_name, is_union, &enum_def));
2264 enum_def->doc_comment = enum_comment;
2265 if (!is_union && !opts.proto_mode) {
2266 // Give specialized error message, since this type spec used to
2267 // be optional in the first FlatBuffers release.
2270 "must specify the underlying integer type for this"
2271 " enum (e.g. \': short\', which was the default).");
2275 // Specify the integer type underlying this enum.
2276 ECHECK(ParseType(enum_def->underlying_type));
2277 if (!IsInteger(enum_def->underlying_type.base_type) ||
2278 IsBool(enum_def->underlying_type.base_type))
2279 return Error("underlying enum type must be integral");
2280 // Make this type refer back to the enum it was derived from.
2281 enum_def->underlying_type.enum_def = enum_def;
2283 ECHECK(ParseMetaData(&enum_def->attributes));
2284 const auto underlying_type = enum_def->underlying_type.base_type;
2285 if (enum_def->attributes.Lookup("bit_flags") &&
2286 !IsUnsigned(underlying_type)) {
2287 // todo: Convert to the Error in the future?
2288 Warning("underlying type of bit_flags enum must be unsigned");
2290 EnumValBuilder evb(*this, *enum_def);
2292 // A lot of code generatos expect that an enum is not-empty.
2293 if ((is_union || Is('}')) && !opts.proto_mode) {
2294 evb.CreateEnumerator("NONE");
2295 ECHECK(evb.AcceptEnumerator());
2297 std::set<std::pair<BaseType, StructDef *>> union_types;
2299 if (opts.proto_mode && attribute_ == "option") {
2300 ECHECK(ParseProtoOption());
2302 auto &ev = *evb.CreateEnumerator(attribute_);
2303 auto full_name = ev.name;
2304 ev.doc_comment = doc_comment_;
2305 EXPECT(kTokenIdentifier);
2307 ECHECK(ParseNamespacing(&full_name, &ev.name));
2308 if (opts.union_value_namespacing) {
2309 // Since we can't namespace the actual enum identifiers, turn
2310 // namespace parts into part of the identifier.
2311 ev.name = full_name;
2312 std::replace(ev.name.begin(), ev.name.end(), '.', '_');
2316 ECHECK(ParseType(ev.union_type));
2317 if (ev.union_type.base_type != BASE_TYPE_STRUCT &&
2318 ev.union_type.base_type != BASE_TYPE_STRING)
2319 return Error("union value type may only be table/struct/string");
2321 ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name));
2323 if (!enum_def->uses_multiple_type_instances) {
2324 auto ins = union_types.insert(std::make_pair(
2325 ev.union_type.base_type, ev.union_type.struct_def));
2326 enum_def->uses_multiple_type_instances = (false == ins.second);
2332 ECHECK(evb.AssignEnumeratorValue(attribute_));
2333 EXPECT(kTokenIntegerConstant);
2336 ECHECK(evb.AcceptEnumerator());
2338 if (opts.proto_mode && Is('[')) {
2340 // ignore attributes on enums.
2341 while (token_ != ']') NEXT();
2345 if (!Is(opts.proto_mode ? ';' : ',')) break;
2350 // At this point, the enum can be empty if input is invalid proto-file.
2351 if (!enum_def->size())
2352 return Error("incomplete enum declaration, values not found");
2354 if (enum_def->attributes.Lookup("bit_flags")) {
2355 const auto base_width = static_cast<uint64_t>(8 * SizeOf(underlying_type));
2356 for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
2359 const auto u = ev->GetAsUInt64();
2360 // Stop manipulations with the sign.
2361 if (!IsUnsigned(underlying_type) && u == (base_width - 1))
2362 return Error("underlying type of bit_flags enum must be unsigned");
2363 if (u >= base_width)
2364 return Error("bit flag out of range of underlying integral type");
2365 enum_def->ChangeEnumValue(ev, 1ULL << u);
2369 enum_def->SortByValue(); // Must be sorted to use MinValue/MaxValue.
2371 // Ensure enum value uniqueness.
2372 auto prev_it = enum_def->Vals().begin();
2373 for (auto it = prev_it + 1; it != enum_def->Vals().end(); ++it) {
2374 auto prev_ev = *prev_it;
2376 if (prev_ev->GetAsUInt64() == ev->GetAsUInt64())
2377 return Error("all enum values must be unique: " + prev_ev->name +
2378 " and " + ev->name + " are both " +
2379 NumToString(ev->GetAsInt64()));
2382 if (dest) *dest = enum_def;
2383 types_.Add(current_namespace_->GetFullyQualifiedName(enum_def->name),
2384 new Type(BASE_TYPE_UNION, nullptr, enum_def));
2388 CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) {
2389 auto &struct_def = *LookupCreateStruct(name, true, true);
2390 if (!struct_def.predecl) return Error("datatype already exists: " + name);
2391 struct_def.predecl = false;
2392 struct_def.name = name;
2393 struct_def.file = file_being_parsed_;
2394 // Move this struct to the back of the vector just in case it was predeclared,
2395 // to preserve declaration order.
2396 *std::remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) =
2398 *dest = &struct_def;
2402 CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
2403 StructDef *struct_def, const char *suffix,
2404 BaseType basetype) {
2405 auto len = strlen(suffix);
2406 for (auto it = fields.begin(); it != fields.end(); ++it) {
2407 auto &fname = (*it)->name;
2408 if (fname.length() > len &&
2409 fname.compare(fname.length() - len, len, suffix) == 0 &&
2410 (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
2412 struct_def->fields.Lookup(fname.substr(0, fname.length() - len));
2413 if (field && field->value.type.base_type == basetype)
2414 return Error("Field " + fname +
2415 " would clash with generated functions for field " +
2422 bool Parser::SupportsOptionalScalars(const flatbuffers::IDLOptions &opts) {
2423 static FLATBUFFERS_CONSTEXPR unsigned long supported_langs =
2424 IDLOptions::kRust | IDLOptions::kSwift | IDLOptions::kLobster |
2425 IDLOptions::kKotlin | IDLOptions::kCpp | IDLOptions::kJava |
2426 IDLOptions::kCSharp | IDLOptions::kTs | IDLOptions::kBinary;
2427 unsigned long langs = opts.lang_to_generate;
2428 return (langs > 0 && langs < IDLOptions::kMAX) && !(langs & ~supported_langs);
2430 bool Parser::SupportsOptionalScalars() const {
2431 // Check in general if a language isn't specified.
2432 return opts.lang_to_generate == 0 || SupportsOptionalScalars(opts);
2435 bool Parser::SupportsDefaultVectorsAndStrings() const {
2436 static FLATBUFFERS_CONSTEXPR unsigned long supported_langs =
2437 IDLOptions::kRust | IDLOptions::kSwift;
2438 return !(opts.lang_to_generate & ~supported_langs);
2441 bool Parser::SupportsAdvancedUnionFeatures() const {
2442 return opts.lang_to_generate != 0 &&
2443 (opts.lang_to_generate &
2444 ~(IDLOptions::kCpp | IDLOptions::kTs | IDLOptions::kPhp |
2445 IDLOptions::kJava | IDLOptions::kCSharp | IDLOptions::kKotlin |
2446 IDLOptions::kBinary | IDLOptions::kSwift)) == 0;
2449 bool Parser::SupportsAdvancedArrayFeatures() const {
2450 return (opts.lang_to_generate &
2451 ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
2452 IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
2453 IDLOptions::kBinary)) == 0;
2456 Namespace *Parser::UniqueNamespace(Namespace *ns) {
2457 for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
2458 if (ns->components == (*it)->components) {
2463 namespaces_.push_back(ns);
2467 std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
2468 Namespace *ns = new Namespace();
2470 std::size_t current, previous = 0;
2471 current = full_qualified_name.find('.');
2472 while (current != std::string::npos) {
2473 ns->components.push_back(
2474 full_qualified_name.substr(previous, current - previous));
2475 previous = current + 1;
2476 current = full_qualified_name.find('.', previous);
2478 current_namespace_ = UniqueNamespace(ns);
2479 return full_qualified_name.substr(previous, current - previous);
2482 static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
2483 auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
2484 auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
2488 CheckedError Parser::ParseDecl() {
2489 std::vector<std::string> dc = doc_comment_;
2490 bool fixed = IsIdent("struct");
2491 if (!fixed && !IsIdent("table")) return Error("declaration expected");
2493 std::string name = attribute_;
2494 EXPECT(kTokenIdentifier);
2495 StructDef *struct_def;
2496 ECHECK(StartStruct(name, &struct_def));
2497 struct_def->doc_comment = dc;
2498 struct_def->fixed = fixed;
2499 ECHECK(ParseMetaData(&struct_def->attributes));
2500 struct_def->sortbysize =
2501 struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
2503 while (token_ != '}') ECHECK(ParseField(*struct_def));
2505 const auto force_align = struct_def->attributes.Lookup("force_align");
2508 ECHECK(ParseAlignAttribute(force_align->constant, struct_def->minalign,
2510 struct_def->minalign = align;
2512 if (!struct_def->bytesize) return Error("size 0 structs not allowed");
2514 struct_def->PadLastField(struct_def->minalign);
2515 // Check if this is a table that has manual id assignments
2516 auto &fields = struct_def->fields.vec;
2517 if (!fixed && fields.size()) {
2518 size_t num_id_fields = 0;
2519 for (auto it = fields.begin(); it != fields.end(); ++it) {
2520 if ((*it)->attributes.Lookup("id")) num_id_fields++;
2522 // If any fields have ids..
2523 if (num_id_fields || opts.require_explicit_ids) {
2524 // Then all fields must have them.
2525 if (num_id_fields != fields.size()) {
2526 if (opts.require_explicit_ids) {
2528 "all fields must have an 'id' attribute when "
2529 "--require-explicit-ids is used");
2532 "either all fields or no fields must have an 'id' attribute");
2535 // Simply sort by id, then the fields are the same as if no ids had
2537 std::sort(fields.begin(), fields.end(), compareFieldDefs);
2538 // Verify we have a contiguous set, and reassign vtable offsets.
2539 FLATBUFFERS_ASSERT(fields.size() <=
2540 flatbuffers::numeric_limits<voffset_t>::max());
2541 for (voffset_t i = 0; i < static_cast<voffset_t>(fields.size()); i++) {
2542 auto &field = *fields[i];
2543 const auto &id_str = field.attributes.Lookup("id")->constant;
2544 // Metadata values have a dynamic type, they can be `float`, 'int', or
2546 // The FieldIndexToOffset(i) expects the voffset_t so `id` is limited by
2549 const auto done = !atot(id_str.c_str(), *this, &id).Check();
2551 return Error("field id\'s must be non-negative number, field: " +
2552 field.name + ", id: " + id_str);
2554 return Error("field id\'s must be consecutive from 0, id " +
2555 NumToString(i) + " missing or set twice, field: " +
2556 field.name + ", id: " + id_str);
2557 field.value.offset = FieldIndexToOffset(i);
2563 CheckClash(fields, struct_def, UnionTypeFieldSuffix(), BASE_TYPE_UNION));
2564 ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
2565 ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
2566 ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
2567 ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
2568 ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
2570 types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name),
2571 new Type(BASE_TYPE_STRUCT, struct_def, nullptr));
2575 CheckedError Parser::ParseService() {
2576 std::vector<std::string> service_comment = doc_comment_;
2578 auto service_name = attribute_;
2579 EXPECT(kTokenIdentifier);
2580 auto &service_def = *new ServiceDef();
2581 service_def.name = service_name;
2582 service_def.file = file_being_parsed_;
2583 service_def.doc_comment = service_comment;
2584 service_def.defined_namespace = current_namespace_;
2585 if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
2587 return Error("service already exists: " + service_name);
2588 ECHECK(ParseMetaData(&service_def.attributes));
2591 std::vector<std::string> doc_comment = doc_comment_;
2592 auto rpc_name = attribute_;
2593 EXPECT(kTokenIdentifier);
2595 Type reqtype, resptype;
2596 ECHECK(ParseTypeIdent(reqtype));
2599 ECHECK(ParseTypeIdent(resptype));
2600 if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
2601 resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
2602 return Error("rpc request and response types must be tables");
2603 auto &rpc = *new RPCCall();
2604 rpc.name = rpc_name;
2605 rpc.request = reqtype.struct_def;
2606 rpc.response = resptype.struct_def;
2607 rpc.doc_comment = doc_comment;
2608 if (service_def.calls.Add(rpc_name, &rpc))
2609 return Error("rpc already exists: " + rpc_name);
2610 ECHECK(ParseMetaData(&rpc.attributes));
2612 } while (token_ != '}');
2617 bool Parser::SetRootType(const char *name) {
2618 root_struct_def_ = LookupStruct(name);
2619 if (!root_struct_def_)
2621 LookupStruct(current_namespace_->GetFullyQualifiedName(name));
2622 return root_struct_def_ != nullptr;
2625 void Parser::MarkGenerated() {
2626 // This function marks all existing definitions as having already
2627 // been generated, which signals no code for included files should be
2629 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2630 (*it)->generated = true;
2632 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
2633 if (!(*it)->predecl) { (*it)->generated = true; }
2635 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
2636 (*it)->generated = true;
2640 CheckedError Parser::ParseNamespace() {
2642 auto ns = new Namespace();
2643 namespaces_.push_back(ns); // Store it here to not leak upon error.
2644 if (token_ != ';') {
2646 ns->components.push_back(attribute_);
2647 EXPECT(kTokenIdentifier);
2648 if (Is('.')) NEXT() else break;
2651 namespaces_.pop_back();
2652 current_namespace_ = UniqueNamespace(ns);
2657 // Best effort parsing of .proto declarations, with the aim to turn them
2658 // in the closest corresponding FlatBuffer equivalent.
2659 // We parse everything as identifiers instead of keywords, since we don't
2660 // want protobuf keywords to become invalid identifiers in FlatBuffers.
2661 CheckedError Parser::ParseProtoDecl() {
2662 bool isextend = IsIdent("extend");
2663 if (IsIdent("package")) {
2664 // These are identical in syntax to FlatBuffer's namespace decl.
2665 ECHECK(ParseNamespace());
2666 } else if (IsIdent("message") || isextend) {
2667 std::vector<std::string> struct_comment = doc_comment_;
2669 StructDef *struct_def = nullptr;
2670 Namespace *parent_namespace = nullptr;
2672 if (Is('.')) NEXT(); // qualified names may start with a . ?
2673 auto id = attribute_;
2674 EXPECT(kTokenIdentifier);
2675 ECHECK(ParseNamespacing(&id, nullptr));
2676 struct_def = LookupCreateStruct(id, false);
2678 return Error("cannot extend unknown message type: " + id);
2680 std::string name = attribute_;
2681 EXPECT(kTokenIdentifier);
2682 ECHECK(StartStruct(name, &struct_def));
2683 // Since message definitions can be nested, we create a new namespace.
2684 auto ns = new Namespace();
2685 // Copy of current namespace.
2686 *ns = *current_namespace_;
2687 // But with current message name.
2688 ns->components.push_back(name);
2690 parent_namespace = current_namespace_;
2691 current_namespace_ = UniqueNamespace(ns);
2693 struct_def->doc_comment = struct_comment;
2694 ECHECK(ParseProtoFields(struct_def, isextend, false));
2695 if (!isextend) { current_namespace_ = parent_namespace; }
2696 if (Is(';')) NEXT();
2697 } else if (IsIdent("enum")) {
2698 // These are almost the same, just with different terminator:
2700 ECHECK(ParseEnum(false, &enum_def));
2701 if (Is(';')) NEXT();
2702 // Temp: remove any duplicates, as .fbs files can't handle them.
2703 enum_def->RemoveDuplicates();
2704 } else if (IsIdent("syntax")) { // Skip these.
2707 EXPECT(kTokenStringConstant);
2709 } else if (IsIdent("option")) { // Skip these.
2710 ECHECK(ParseProtoOption());
2712 } else if (IsIdent("service")) { // Skip these.
2714 EXPECT(kTokenIdentifier);
2715 ECHECK(ParseProtoCurliesOrIdent());
2717 return Error("don\'t know how to parse .proto declaration starting with " +
2718 TokenToStringId(token_));
2723 CheckedError Parser::StartEnum(const std::string &enum_name, bool is_union,
2725 auto &enum_def = *new EnumDef();
2726 enum_def.name = enum_name;
2727 enum_def.file = file_being_parsed_;
2728 enum_def.doc_comment = doc_comment_;
2729 enum_def.is_union = is_union;
2730 enum_def.defined_namespace = current_namespace_;
2731 if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name),
2733 return Error("enum already exists: " + enum_name);
2734 enum_def.underlying_type.base_type =
2735 is_union ? BASE_TYPE_UTYPE : BASE_TYPE_INT;
2736 enum_def.underlying_type.enum_def = &enum_def;
2737 if (dest) *dest = &enum_def;
2741 CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
2742 bool inside_oneof) {
2744 while (token_ != '}') {
2745 if (IsIdent("message") || IsIdent("extend") || IsIdent("enum")) {
2746 // Nested declarations.
2747 ECHECK(ParseProtoDecl());
2748 } else if (IsIdent("extensions")) { // Skip these.
2750 EXPECT(kTokenIntegerConstant);
2751 if (Is(kTokenIdentifier)) {
2756 } else if (IsIdent("option")) { // Skip these.
2757 ECHECK(ParseProtoOption());
2759 } else if (IsIdent("reserved")) { // Skip these.
2761 while (!Is(';')) { NEXT(); } // A variety of formats, just skip.
2764 std::vector<std::string> field_comment = doc_comment_;
2765 // Parse the qualifier.
2766 bool required = false;
2767 bool repeated = false;
2769 if (!inside_oneof) {
2770 if (IsIdent("optional")) {
2771 // This is the default.
2773 } else if (IsIdent("required")) {
2776 } else if (IsIdent("repeated")) {
2779 } else if (IsIdent("oneof")) {
2783 // can't error, proto3 allows decls without any of the above.
2786 StructDef *anonymous_struct = nullptr;
2787 EnumDef *oneof_union = nullptr;
2789 if (IsIdent("group") || oneof) {
2791 if (oneof && opts.proto_oneof_union) {
2792 auto name = MakeCamel(attribute_, true) + "Union";
2793 ECHECK(StartEnum(name, true, &oneof_union));
2794 type = Type(BASE_TYPE_UNION, nullptr, oneof_union);
2796 auto name = "Anonymous" + NumToString(anonymous_counter_++);
2797 ECHECK(StartStruct(name, &anonymous_struct));
2798 type = Type(BASE_TYPE_STRUCT, anonymous_struct);
2801 ECHECK(ParseTypeFromProtoType(&type));
2803 // Repeated elements get mapped to a vector.
2805 type.element = type.base_type;
2806 type.base_type = BASE_TYPE_VECTOR;
2807 if (type.element == BASE_TYPE_VECTOR) {
2808 // We have a vector or vectors, which FlatBuffers doesn't support.
2809 // For now make it a vector of string (since the source is likely
2810 // "repeated bytes").
2811 // TODO(wvo): A better solution would be to wrap this in a table.
2812 type.element = BASE_TYPE_STRING;
2815 std::string name = attribute_;
2816 EXPECT(kTokenIdentifier);
2818 // Parse the field id. Since we're just translating schemas, not
2819 // any kind of binary compatibility, we can safely ignore these, and
2822 EXPECT(kTokenIntegerConstant);
2824 FieldDef *field = nullptr;
2826 // We allow a field to be re-defined when extending.
2827 // TODO: are there situations where that is problematic?
2828 field = struct_def->fields.Lookup(name);
2830 if (!field) ECHECK(AddField(*struct_def, name, type, &field));
2831 field->doc_comment = field_comment;
2832 if (!IsScalar(type.base_type) && required) {
2833 field->presence = FieldDef::kRequired;
2835 // See if there's a default specified.
2839 auto key = attribute_;
2840 ECHECK(ParseProtoKey());
2842 auto val = attribute_;
2843 ECHECK(ParseProtoCurliesOrIdent());
2844 if (key == "default") {
2845 // Temp: skip non-numeric and non-boolean defaults (enums).
2846 auto numeric = strpbrk(val.c_str(), "0123456789-+.");
2847 if (IsScalar(type.base_type) && numeric == val.c_str()) {
2848 field->value.constant = val;
2849 } else if (val == "true") {
2850 field->value.constant = val;
2851 } // "false" is default, no need to handle explicitly.
2852 } else if (key == "deprecated") {
2853 field->deprecated = val == "true";
2855 if (!Is(',')) break;
2860 if (anonymous_struct) {
2861 ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
2862 if (Is(';')) NEXT();
2863 } else if (oneof_union) {
2864 // Parse into a temporary StructDef, then transfer fields into an
2865 // EnumDef describing the oneof as a union.
2866 StructDef oneof_struct;
2867 ECHECK(ParseProtoFields(&oneof_struct, false, oneof));
2868 if (Is(';')) NEXT();
2869 for (auto field_it = oneof_struct.fields.vec.begin();
2870 field_it != oneof_struct.fields.vec.end(); ++field_it) {
2871 const auto &oneof_field = **field_it;
2872 const auto &oneof_type = oneof_field.value.type;
2873 if (oneof_type.base_type != BASE_TYPE_STRUCT ||
2874 !oneof_type.struct_def || oneof_type.struct_def->fixed)
2875 return Error("oneof '" + name +
2876 "' cannot be mapped to a union because member '" +
2877 oneof_field.name + "' is not a table type.");
2878 EnumValBuilder evb(*this, *oneof_union);
2879 auto ev = evb.CreateEnumerator(oneof_type.struct_def->name);
2880 ev->union_type = oneof_type;
2881 ev->doc_comment = oneof_field.doc_comment;
2882 ECHECK(evb.AcceptEnumerator(oneof_field.name));
2893 CheckedError Parser::ParseProtoKey() {
2894 if (token_ == '(') {
2896 // Skip "(a.b)" style custom attributes.
2897 while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
2901 EXPECT(kTokenIdentifier);
2904 EXPECT(kTokenIdentifier);
2909 CheckedError Parser::ParseProtoCurliesOrIdent() {
2912 for (int nesting = 1; nesting;) {
2915 else if (token_ == '}')
2920 NEXT(); // Any single token.
2925 CheckedError Parser::ParseProtoOption() {
2927 ECHECK(ParseProtoKey());
2929 ECHECK(ParseProtoCurliesOrIdent());
2933 // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
2934 CheckedError Parser::ParseTypeFromProtoType(Type *type) {
2935 struct type_lookup {
2936 const char *proto_type;
2937 BaseType fb_type, element;
2939 static type_lookup lookup[] = {
2940 { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE },
2941 { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE },
2942 { "int32", BASE_TYPE_INT, BASE_TYPE_NONE },
2943 { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2944 { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2945 { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2946 { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE },
2947 { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2948 { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2949 { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2950 { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE },
2951 { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2952 { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE },
2953 { "string", BASE_TYPE_STRING, BASE_TYPE_NONE },
2954 { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR },
2955 { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE }
2957 for (auto tl = lookup; tl->proto_type; tl++) {
2958 if (attribute_ == tl->proto_type) {
2959 type->base_type = tl->fb_type;
2960 type->element = tl->element;
2965 if (Is('.')) NEXT(); // qualified names may start with a . ?
2966 ECHECK(ParseTypeIdent(*type));
2970 CheckedError Parser::SkipAnyJsonValue() {
2971 ParseDepthGuard depth_guard(this);
2972 ECHECK(depth_guard.Check());
2976 size_t fieldn_outer = 0;
2977 return ParseTableDelimiters(fieldn_outer, nullptr,
2978 [&](const std::string &, size_t &fieldn,
2979 const StructDef *) -> CheckedError {
2980 ECHECK(SkipAnyJsonValue());
2986 uoffset_t count = 0;
2987 return ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2988 return SkipAnyJsonValue();
2991 case kTokenStringConstant:
2992 case kTokenIntegerConstant:
2993 case kTokenFloatConstant: NEXT(); break;
2995 if (IsIdent("true") || IsIdent("false") || IsIdent("null")) {
2998 return TokenError();
3003 CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
3004 ParseDepthGuard depth_guard(this);
3005 ECHECK(depth_guard.Check());
3009 auto start = builder->StartMap();
3010 size_t fieldn_outer = 0;
3012 ParseTableDelimiters(fieldn_outer, nullptr,
3013 [&](const std::string &name, size_t &fieldn,
3014 const StructDef *) -> CheckedError {
3016 ECHECK(ParseFlexBufferValue(builder));
3021 builder->EndMap(start);
3022 if (builder->HasDuplicateKeys())
3023 return Error("FlexBuffers map has duplicate keys");
3027 auto start = builder->StartVector();
3028 uoffset_t count = 0;
3029 ECHECK(ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
3030 return ParseFlexBufferValue(builder);
3032 builder->EndVector(start, false, false);
3035 case kTokenStringConstant:
3036 builder->String(attribute_);
3037 EXPECT(kTokenStringConstant);
3039 case kTokenIntegerConstant:
3040 builder->Int(StringToInt(attribute_.c_str()));
3041 EXPECT(kTokenIntegerConstant);
3043 case kTokenFloatConstant: {
3045 StringToNumber(attribute_.c_str(), &d);
3047 EXPECT(kTokenFloatConstant);
3051 if (IsIdent("true")) {
3052 builder->Bool(true);
3054 } else if (IsIdent("false")) {
3055 builder->Bool(false);
3057 } else if (IsIdent("null")) {
3061 return TokenError();
3066 bool Parser::ParseFlexBuffer(const char *source, const char *source_filename,
3067 flexbuffers::Builder *builder) {
3068 const auto initial_depth = parse_depth_counter_;
3069 (void)initial_depth;
3070 auto ok = !StartParseFile(source, source_filename).Check() &&
3071 !ParseFlexBufferValue(builder).Check();
3072 if (ok) builder->Finish();
3073 FLATBUFFERS_ASSERT(initial_depth == parse_depth_counter_);
3077 bool Parser::Parse(const char *source, const char **include_paths,
3078 const char *source_filename) {
3079 const auto initial_depth = parse_depth_counter_;
3080 (void)initial_depth;
3083 if (opts.use_flexbuffers) {
3084 r = ParseFlexBuffer(source, source_filename, &flex_builder_);
3086 r = !ParseRoot(source, include_paths, source_filename).Check();
3088 FLATBUFFERS_ASSERT(initial_depth == parse_depth_counter_);
3092 bool Parser::ParseJson(const char *json, const char *json_filename) {
3093 const auto initial_depth = parse_depth_counter_;
3094 (void)initial_depth;
3097 !StartParseFile(json, json_filename).Check() && !DoParseJson().Check();
3098 FLATBUFFERS_ASSERT(initial_depth == parse_depth_counter_);
3102 CheckedError Parser::StartParseFile(const char *source,
3103 const char *source_filename) {
3104 file_being_parsed_ = source_filename ? source_filename : "";
3106 ResetState(source_);
3108 ECHECK(SkipByteOrderMark());
3110 if (Is(kTokenEof)) return Error("input file is empty");
3114 CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
3115 const char *source_filename) {
3116 ECHECK(DoParse(source, include_paths, source_filename, nullptr));
3118 // Check that all types were defined.
3119 for (auto it = structs_.vec.begin(); it != structs_.vec.end();) {
3120 auto &struct_def = **it;
3121 if (struct_def.predecl) {
3122 if (opts.proto_mode) {
3123 // Protos allow enums to be used before declaration, so check if that
3124 // is the case here.
3125 EnumDef *enum_def = nullptr;
3126 for (size_t components =
3127 struct_def.defined_namespace->components.size() + 1;
3128 components && !enum_def; components--) {
3129 auto qualified_name =
3130 struct_def.defined_namespace->GetFullyQualifiedName(
3131 struct_def.name, components - 1);
3132 enum_def = LookupEnum(qualified_name);
3135 // This is pretty slow, but a simple solution for now.
3136 auto initial_count = struct_def.refcount;
3137 for (auto struct_it = structs_.vec.begin();
3138 struct_it != structs_.vec.end(); ++struct_it) {
3139 auto &sd = **struct_it;
3140 for (auto field_it = sd.fields.vec.begin();
3141 field_it != sd.fields.vec.end(); ++field_it) {
3142 auto &field = **field_it;
3143 if (field.value.type.struct_def == &struct_def) {
3144 field.value.type.struct_def = nullptr;
3145 field.value.type.enum_def = enum_def;
3146 auto &bt = IsVector(field.value.type)
3147 ? field.value.type.element
3148 : field.value.type.base_type;
3149 FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
3150 bt = enum_def->underlying_type.base_type;
3151 struct_def.refcount--;
3152 enum_def->refcount++;
3156 if (struct_def.refcount)
3157 return Error("internal: " + NumToString(struct_def.refcount) + "/" +
3158 NumToString(initial_count) +
3159 " use(s) of pre-declaration enum not accounted for: " +
3161 structs_.dict.erase(structs_.dict.find(struct_def.name));
3162 it = structs_.vec.erase(it);
3164 continue; // Skip error.
3167 auto err = "type referenced but not defined (check namespace): " +
3169 if (struct_def.original_location)
3170 err += ", originally at: " + *struct_def.original_location;
3176 // This check has to happen here and not earlier, because only now do we
3177 // know for sure what the type of these are.
3178 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3179 auto &enum_def = **it;
3180 if (enum_def.is_union) {
3181 for (auto val_it = enum_def.Vals().begin();
3182 val_it != enum_def.Vals().end(); ++val_it) {
3183 auto &val = **val_it;
3184 if (!SupportsAdvancedUnionFeatures() &&
3185 (IsStruct(val.union_type) || IsString(val.union_type)))
3187 "only tables can be union elements in the generated language: " +
3192 // Parse JSON object only if the scheme has been parsed.
3193 if (token_ == '{') { ECHECK(DoParseJson()); }
3198 // Generate a unique hash for a file based on its name and contents (if any).
3199 static uint64_t HashFile(const char *source_filename, const char *source) {
3202 if (source_filename)
3203 hash = HashFnv1a<uint64_t>(StripPath(source_filename).c_str());
3205 if (source && *source) hash ^= HashFnv1a<uint64_t>(source);
3210 CheckedError Parser::DoParse(const char *source, const char **include_paths,
3211 const char *source_filename,
3212 const char *include_filename) {
3213 uint64_t source_hash = 0;
3214 if (source_filename) {
3215 // If the file is in-memory, don't include its contents in the hash as we
3216 // won't be able to load them later.
3217 if (FileExists(source_filename))
3218 source_hash = HashFile(source_filename, source);
3220 source_hash = HashFile(source_filename, nullptr);
3222 if (included_files_.find(source_hash) == included_files_.end()) {
3223 included_files_[source_hash] = include_filename ? include_filename : "";
3224 files_included_per_file_[source_filename] = std::set<std::string>();
3229 if (!include_paths) {
3230 static const char *current_directory[] = { "", nullptr };
3231 include_paths = current_directory;
3233 field_stack_.clear();
3235 // Start with a blank namespace just in case this file doesn't have one.
3236 current_namespace_ = empty_namespace_;
3238 ECHECK(StartParseFile(source, source_filename));
3240 // Includes must come before type declarations:
3242 // Parse pre-include proto statements if any:
3243 if (opts.proto_mode && (attribute_ == "option" || attribute_ == "syntax" ||
3244 attribute_ == "package")) {
3245 ECHECK(ParseProtoDecl());
3246 } else if (IsIdent("native_include")) {
3248 vector_emplace_back(&native_included_files_, attribute_);
3249 EXPECT(kTokenStringConstant);
3251 } else if (IsIdent("include") || (opts.proto_mode && IsIdent("import"))) {
3253 if (opts.proto_mode && attribute_ == "public") NEXT();
3254 auto name = flatbuffers::PosixPath(attribute_.c_str());
3255 EXPECT(kTokenStringConstant);
3256 // Look for the file relative to the directory of the current file.
3257 std::string filepath;
3258 if (source_filename) {
3259 auto source_file_directory =
3260 flatbuffers::StripFileName(source_filename);
3261 filepath = flatbuffers::ConCatPathFileName(source_file_directory, name);
3263 if (filepath.empty() || !FileExists(filepath.c_str())) {
3264 // Look for the file in include_paths.
3265 for (auto paths = include_paths; paths && *paths; paths++) {
3266 filepath = flatbuffers::ConCatPathFileName(*paths, name);
3267 if (FileExists(filepath.c_str())) break;
3270 if (filepath.empty())
3271 return Error("unable to locate include file: " + name);
3272 if (source_filename)
3273 files_included_per_file_[source_filename].insert(filepath);
3275 std::string contents;
3276 bool file_loaded = LoadFile(filepath.c_str(), true, &contents);
3277 if (included_files_.find(HashFile(filepath.c_str(), contents.c_str())) ==
3278 included_files_.end()) {
3279 // We found an include file that we have not parsed yet.
3281 if (!file_loaded) return Error("unable to load include file: " + name);
3282 ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
3284 // We generally do not want to output code for any included files:
3285 if (!opts.generate_all) MarkGenerated();
3286 // Reset these just in case the included file had them, and the
3288 root_struct_def_ = nullptr;
3289 file_identifier_.clear();
3290 file_extension_.clear();
3291 // This is the easiest way to continue this file after an include:
3292 // instead of saving and restoring all the state, we simply start the
3293 // file anew. This will cause it to encounter the same include
3294 // statement again, but this time it will skip it, because it was
3295 // entered into included_files_.
3296 // This is recursive, but only go as deep as the number of include
3298 included_files_.erase(source_hash);
3299 return DoParse(source, include_paths, source_filename,
3307 // Now parse all other kinds of declarations:
3308 while (token_ != kTokenEof) {
3309 if (opts.proto_mode) {
3310 ECHECK(ParseProtoDecl());
3311 } else if (IsIdent("namespace")) {
3312 ECHECK(ParseNamespace());
3313 } else if (token_ == '{') {
3315 } else if (IsIdent("enum")) {
3316 ECHECK(ParseEnum(false, nullptr));
3317 } else if (IsIdent("union")) {
3318 ECHECK(ParseEnum(true, nullptr));
3319 } else if (IsIdent("root_type")) {
3321 auto root_type = attribute_;
3322 EXPECT(kTokenIdentifier);
3323 ECHECK(ParseNamespacing(&root_type, nullptr));
3324 if (opts.root_type.empty()) {
3325 if (!SetRootType(root_type.c_str()))
3326 return Error("unknown root type: " + root_type);
3327 if (root_struct_def_->fixed) return Error("root type must be a table");
3330 } else if (IsIdent("file_identifier")) {
3332 file_identifier_ = attribute_;
3333 EXPECT(kTokenStringConstant);
3334 if (file_identifier_.length() != FlatBufferBuilder::kFileIdentifierLength)
3335 return Error("file_identifier must be exactly " +
3336 NumToString(FlatBufferBuilder::kFileIdentifierLength) +
3339 } else if (IsIdent("file_extension")) {
3341 file_extension_ = attribute_;
3342 EXPECT(kTokenStringConstant);
3344 } else if (IsIdent("include")) {
3345 return Error("includes must come before declarations");
3346 } else if (IsIdent("attribute")) {
3348 auto name = attribute_;
3349 if (Is(kTokenIdentifier)) {
3352 EXPECT(kTokenStringConstant);
3355 known_attributes_[name] = false;
3356 } else if (IsIdent("rpc_service")) {
3357 ECHECK(ParseService());
3359 ECHECK(ParseDecl());
3365 CheckedError Parser::DoParseJson() {
3366 if (token_ != '{') {
3369 if (!root_struct_def_) return Error("no root type set to parse json with");
3370 if (builder_.GetSize()) {
3371 return Error("cannot have more than one json object in a file");
3374 ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
3375 if (opts.size_prefixed) {
3376 builder_.FinishSizePrefixed(
3377 Offset<Table>(toff),
3378 file_identifier_.length() ? file_identifier_.c_str() : nullptr);
3380 builder_.Finish(Offset<Table>(toff), file_identifier_.length()
3381 ? file_identifier_.c_str()
3385 // Check that JSON file doesn't contain more objects or IDL directives.
3386 // Comments after JSON are allowed.
3391 std::set<std::string> Parser::GetIncludedFilesRecursive(
3392 const std::string &file_name) const {
3393 std::set<std::string> included_files;
3394 std::list<std::string> to_process;
3396 if (file_name.empty()) return included_files;
3397 to_process.push_back(file_name);
3399 while (!to_process.empty()) {
3400 std::string current = to_process.front();
3401 to_process.pop_front();
3402 included_files.insert(current);
3404 // Workaround the lack of const accessor in C++98 maps.
3406 (*const_cast<std::map<std::string, std::set<std::string>> *>(
3407 &files_included_per_file_))[current];
3408 for (auto it = new_files.begin(); it != new_files.end(); ++it) {
3409 if (included_files.find(*it) == included_files.end())
3410 to_process.push_back(*it);
3414 return included_files;
3417 // Schema serialization functionality:
3419 template<typename T> bool compareName(const T *a, const T *b) {
3420 return a->defined_namespace->GetFullyQualifiedName(a->name) <
3421 b->defined_namespace->GetFullyQualifiedName(b->name);
3424 template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
3425 // Pre-sort these vectors, such that we can set the correct indices for them.
3427 std::sort(vec.begin(), vec.end(), compareName<T>);
3428 for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
3431 void Parser::Serialize() {
3433 AssignIndices(structs_.vec);
3434 AssignIndices(enums_.vec);
3435 std::vector<Offset<reflection::Object>> object_offsets;
3436 for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
3437 auto offset = (*it)->Serialize(&builder_, *this);
3438 object_offsets.push_back(offset);
3439 (*it)->serialized_location = offset.o;
3441 std::vector<Offset<reflection::Enum>> enum_offsets;
3442 for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3443 auto offset = (*it)->Serialize(&builder_, *this);
3444 enum_offsets.push_back(offset);
3445 (*it)->serialized_location = offset.o;
3447 std::vector<Offset<reflection::Service>> service_offsets;
3448 for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
3449 auto offset = (*it)->Serialize(&builder_, *this);
3450 service_offsets.push_back(offset);
3451 (*it)->serialized_location = offset.o;
3453 auto objs__ = builder_.CreateVectorOfSortedTables(&object_offsets);
3454 auto enum__ = builder_.CreateVectorOfSortedTables(&enum_offsets);
3455 auto fiid__ = builder_.CreateString(file_identifier_);
3456 auto fext__ = builder_.CreateString(file_extension_);
3457 auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
3458 auto schema_offset = reflection::CreateSchema(
3459 builder_, objs__, enum__, fiid__, fext__,
3460 (root_struct_def_ ? root_struct_def_->serialized_location : 0), serv__);
3461 if (opts.size_prefixed) {
3462 builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
3464 builder_.Finish(schema_offset, reflection::SchemaIdentifier());
3468 static Namespace *GetNamespace(
3469 const std::string &qualified_name, std::vector<Namespace *> &namespaces,
3470 std::map<std::string, Namespace *> &namespaces_index) {
3471 size_t dot = qualified_name.find_last_of('.');
3472 std::string namespace_name = (dot != std::string::npos)
3473 ? std::string(qualified_name.c_str(), dot)
3475 Namespace *&ns = namespaces_index[namespace_name];
3478 ns = new Namespace();
3479 namespaces.push_back(ns);
3484 dot = qualified_name.find('.', pos);
3485 if (dot == std::string::npos) { break; }
3486 ns->components.push_back(qualified_name.substr(pos, dot - pos));
3494 Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
3495 const Parser &parser) const {
3496 std::vector<Offset<reflection::Field>> field_offsets;
3497 for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
3498 field_offsets.push_back((*it)->Serialize(
3499 builder, static_cast<uint16_t>(it - fields.vec.begin()), parser));
3501 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3502 auto name__ = builder->CreateString(qualified_name);
3503 auto flds__ = builder->CreateVectorOfSortedTables(&field_offsets);
3504 auto attr__ = SerializeAttributes(builder, parser);
3505 auto docs__ = parser.opts.binary_schema_comments
3506 ? builder->CreateVectorOfStrings(doc_comment)
3508 return reflection::CreateObject(*builder, name__, flds__, fixed,
3509 static_cast<int>(minalign),
3510 static_cast<int>(bytesize), attr__, docs__);
3513 bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
3514 if (!DeserializeAttributes(parser, object->attributes())) return false;
3515 DeserializeDoc(doc_comment, object->documentation());
3516 name = parser.UnqualifiedName(object->name()->str());
3518 sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
3519 const auto &of = *(object->fields());
3520 auto indexes = std::vector<uoffset_t>(of.size());
3521 for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
3522 size_t tmp_struct_size = 0;
3523 for (size_t i = 0; i < indexes.size(); i++) {
3524 auto field = of.Get(indexes[i]);
3525 auto field_def = new FieldDef();
3526 if (!field_def->Deserialize(parser, field) ||
3527 fields.Add(field_def->name, field_def)) {
3532 // Recompute padding since that's currently not serialized.
3533 auto size = InlineSize(field_def->value.type);
3535 i + 1 < indexes.size() ? of.Get(indexes[i + 1]) : nullptr;
3536 tmp_struct_size += size;
3537 field_def->padding =
3538 next_field ? (next_field->offset() - field_def->value.offset) - size
3539 : PaddingBytes(tmp_struct_size, minalign);
3540 tmp_struct_size += field_def->padding;
3543 FLATBUFFERS_ASSERT(static_cast<int>(tmp_struct_size) == object->bytesize());
3547 Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
3549 const Parser &parser) const {
3550 auto name__ = builder->CreateString(name);
3551 auto type__ = value.type.Serialize(builder);
3552 auto attr__ = SerializeAttributes(builder, parser);
3553 auto docs__ = parser.opts.binary_schema_comments
3554 ? builder->CreateVectorOfStrings(doc_comment)
3557 StringToNumber(value.constant.c_str(), &d);
3558 return reflection::CreateField(
3559 *builder, name__, type__, id, value.offset,
3560 // Is uint64>max(int64) tested?
3561 IsInteger(value.type.base_type) ? StringToInt(value.constant.c_str()) : 0,
3562 // result may be platform-dependent if underlying is float (not double)
3563 IsFloat(value.type.base_type) ? d : 0.0, deprecated, IsRequired(), key,
3564 attr__, docs__, IsOptional());
3565 // TODO: value.constant is almost always "0", we could save quite a bit of
3566 // space by sharing it. Same for common values of value.type.
3569 bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
3570 name = field->name()->str();
3571 defined_namespace = parser.current_namespace_;
3572 if (!value.type.Deserialize(parser, field->type())) return false;
3573 value.offset = field->offset();
3574 if (IsInteger(value.type.base_type)) {
3575 value.constant = NumToString(field->default_integer());
3576 } else if (IsFloat(value.type.base_type)) {
3577 value.constant = FloatToString(field->default_real(), 16);
3579 presence = FieldDef::MakeFieldPresence(field->optional(), field->required());
3581 if (!DeserializeAttributes(parser, field->attributes())) return false;
3582 // TODO: this should probably be handled by a separate attribute
3583 if (attributes.Lookup("flexbuffer")) {
3585 parser.uses_flexbuffers_ = true;
3586 if (value.type.base_type != BASE_TYPE_VECTOR ||
3587 value.type.element != BASE_TYPE_UCHAR)
3590 if (auto nested = attributes.Lookup("nested_flatbuffer")) {
3591 auto nested_qualified_name =
3592 parser.current_namespace_->GetFullyQualifiedName(nested->constant);
3593 nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
3594 if (!nested_flatbuffer) return false;
3596 shared = attributes.Lookup("shared") != nullptr;
3597 DeserializeDoc(doc_comment, field->documentation());
3601 Offset<reflection::RPCCall> RPCCall::Serialize(FlatBufferBuilder *builder,
3602 const Parser &parser) const {
3603 auto name__ = builder->CreateString(name);
3604 auto attr__ = SerializeAttributes(builder, parser);
3605 auto docs__ = parser.opts.binary_schema_comments
3606 ? builder->CreateVectorOfStrings(doc_comment)
3608 return reflection::CreateRPCCall(
3609 *builder, name__, request->serialized_location,
3610 response->serialized_location, attr__, docs__);
3613 bool RPCCall::Deserialize(Parser &parser, const reflection::RPCCall *call) {
3614 name = call->name()->str();
3615 if (!DeserializeAttributes(parser, call->attributes())) return false;
3616 DeserializeDoc(doc_comment, call->documentation());
3617 request = parser.structs_.Lookup(call->request()->name()->str());
3618 response = parser.structs_.Lookup(call->response()->name()->str());
3619 if (!request || !response) { return false; }
3623 Offset<reflection::Service> ServiceDef::Serialize(FlatBufferBuilder *builder,
3624 const Parser &parser) const {
3625 std::vector<Offset<reflection::RPCCall>> servicecall_offsets;
3626 for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) {
3627 servicecall_offsets.push_back((*it)->Serialize(builder, parser));
3629 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3630 auto name__ = builder->CreateString(qualified_name);
3631 auto call__ = builder->CreateVector(servicecall_offsets);
3632 auto attr__ = SerializeAttributes(builder, parser);
3633 auto docs__ = parser.opts.binary_schema_comments
3634 ? builder->CreateVectorOfStrings(doc_comment)
3636 return reflection::CreateService(*builder, name__, call__, attr__, docs__);
3639 bool ServiceDef::Deserialize(Parser &parser,
3640 const reflection::Service *service) {
3641 name = parser.UnqualifiedName(service->name()->str());
3642 if (service->calls()) {
3643 for (uoffset_t i = 0; i < service->calls()->size(); ++i) {
3644 auto call = new RPCCall();
3645 if (!call->Deserialize(parser, service->calls()->Get(i)) ||
3646 calls.Add(call->name, call)) {
3652 if (!DeserializeAttributes(parser, service->attributes())) return false;
3653 DeserializeDoc(doc_comment, service->documentation());
3657 Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
3658 const Parser &parser) const {
3659 std::vector<Offset<reflection::EnumVal>> enumval_offsets;
3660 for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
3661 enumval_offsets.push_back((*it)->Serialize(builder, parser));
3663 auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3664 auto name__ = builder->CreateString(qualified_name);
3665 auto vals__ = builder->CreateVector(enumval_offsets);
3666 auto type__ = underlying_type.Serialize(builder);
3667 auto attr__ = SerializeAttributes(builder, parser);
3668 auto docs__ = parser.opts.binary_schema_comments
3669 ? builder->CreateVectorOfStrings(doc_comment)
3671 return reflection::CreateEnum(*builder, name__, vals__, is_union, type__,
3675 bool EnumDef::Deserialize(Parser &parser, const reflection::Enum *_enum) {
3676 name = parser.UnqualifiedName(_enum->name()->str());
3677 for (uoffset_t i = 0; i < _enum->values()->size(); ++i) {
3678 auto val = new EnumVal();
3679 if (!val->Deserialize(parser, _enum->values()->Get(i)) ||
3680 vals.Add(val->name, val)) {
3685 is_union = _enum->is_union();
3686 if (!underlying_type.Deserialize(parser, _enum->underlying_type())) {
3689 if (!DeserializeAttributes(parser, _enum->attributes())) return false;
3690 DeserializeDoc(doc_comment, _enum->documentation());
3694 Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder,
3695 const Parser &parser) const {
3696 auto name__ = builder->CreateString(name);
3697 auto type__ = union_type.Serialize(builder);
3698 auto docs__ = parser.opts.binary_schema_comments
3699 ? builder->CreateVectorOfStrings(doc_comment)
3701 return reflection::CreateEnumVal(
3702 *builder, name__, value,
3703 union_type.struct_def ? union_type.struct_def->serialized_location : 0,
3707 bool EnumVal::Deserialize(const Parser &parser,
3708 const reflection::EnumVal *val) {
3709 name = val->name()->str();
3710 value = val->value();
3711 if (!union_type.Deserialize(parser, val->union_type())) return false;
3712 DeserializeDoc(doc_comment, val->documentation());
3716 Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
3717 return reflection::CreateType(
3718 *builder, static_cast<reflection::BaseType>(base_type),
3719 static_cast<reflection::BaseType>(element),
3720 struct_def ? struct_def->index : (enum_def ? enum_def->index : -1),
3724 bool Type::Deserialize(const Parser &parser, const reflection::Type *type) {
3725 if (type == nullptr) return true;
3726 base_type = static_cast<BaseType>(type->base_type());
3727 element = static_cast<BaseType>(type->element());
3728 fixed_length = type->fixed_length();
3729 if (type->index() >= 0) {
3730 bool is_series = type->base_type() == reflection::Vector ||
3731 type->base_type() == reflection::Array;
3732 if (type->base_type() == reflection::Obj ||
3733 (is_series && type->element() == reflection::Obj)) {
3734 if (static_cast<size_t>(type->index()) < parser.structs_.vec.size()) {
3735 struct_def = parser.structs_.vec[type->index()];
3736 struct_def->refcount++;
3741 if (static_cast<size_t>(type->index()) < parser.enums_.vec.size()) {
3742 enum_def = parser.enums_.vec[type->index()];
3751 flatbuffers::Offset<
3752 flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
3753 Definition::SerializeAttributes(FlatBufferBuilder *builder,
3754 const Parser &parser) const {
3755 std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
3756 for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
3757 auto it = parser.known_attributes_.find(kv->first);
3758 FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
3759 if (parser.opts.binary_schema_builtins || !it->second) {
3760 auto key = builder->CreateString(kv->first);
3761 auto val = builder->CreateString(kv->second->constant);
3762 attrs.push_back(reflection::CreateKeyValue(*builder, key, val));
3766 return builder->CreateVectorOfSortedTables(&attrs);
3772 bool Definition::DeserializeAttributes(
3773 Parser &parser, const Vector<Offset<reflection::KeyValue>> *attrs) {
3774 if (attrs == nullptr) return true;
3775 for (uoffset_t i = 0; i < attrs->size(); ++i) {
3776 auto kv = attrs->Get(i);
3777 auto value = new Value();
3778 if (kv->value()) { value->constant = kv->value()->str(); }
3779 if (attributes.Add(kv->key()->str(), value)) {
3783 parser.known_attributes_[kv->key()->str()];
3788 /************************************************************************/
3789 /* DESERIALIZATION */
3790 /************************************************************************/
3791 bool Parser::Deserialize(const uint8_t *buf, const size_t size) {
3792 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t *>(buf), size);
3793 bool size_prefixed = false;
3794 if (!reflection::SchemaBufferHasIdentifier(buf)) {
3795 if (!flatbuffers::BufferHasIdentifier(buf, reflection::SchemaIdentifier(),
3799 size_prefixed = true;
3801 auto verify_fn = size_prefixed ? &reflection::VerifySizePrefixedSchemaBuffer
3802 : &reflection::VerifySchemaBuffer;
3803 if (!verify_fn(verifier)) { return false; }
3804 auto schema = size_prefixed ? reflection::GetSizePrefixedSchema(buf)
3805 : reflection::GetSchema(buf);
3806 return Deserialize(schema);
3809 bool Parser::Deserialize(const reflection::Schema *schema) {
3810 file_identifier_ = schema->file_ident() ? schema->file_ident()->str() : "";
3811 file_extension_ = schema->file_ext() ? schema->file_ext()->str() : "";
3812 std::map<std::string, Namespace *> namespaces_index;
3814 // Create defs without deserializing so references from fields to structs and
3815 // enums can be resolved.
3816 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3818 auto struct_def = new StructDef();
3819 struct_def->bytesize = it->bytesize();
3820 struct_def->fixed = it->is_struct();
3821 struct_def->minalign = it->minalign();
3822 if (structs_.Add(it->name()->str(), struct_def)) {
3826 auto type = new Type(BASE_TYPE_STRUCT, struct_def, nullptr);
3827 if (types_.Add(it->name()->str(), type)) {
3832 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3833 auto enum_def = new EnumDef();
3834 if (enums_.Add(it->name()->str(), enum_def)) {
3838 auto type = new Type(BASE_TYPE_UNION, nullptr, enum_def);
3839 if (types_.Add(it->name()->str(), type)) {
3845 // Now fields can refer to structs and enums by index.
3846 for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3848 std::string qualified_name = it->name()->str();
3849 auto struct_def = structs_.Lookup(qualified_name);
3850 struct_def->defined_namespace =
3851 GetNamespace(qualified_name, namespaces_, namespaces_index);
3852 if (!struct_def->Deserialize(*this, *it)) { return false; }
3853 if (schema->root_table() == *it) { root_struct_def_ = struct_def; }
3855 for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3856 std::string qualified_name = it->name()->str();
3857 auto enum_def = enums_.Lookup(qualified_name);
3858 enum_def->defined_namespace =
3859 GetNamespace(qualified_name, namespaces_, namespaces_index);
3860 if (!enum_def->Deserialize(*this, *it)) { return false; }
3863 if (schema->services()) {
3864 for (auto it = schema->services()->begin(); it != schema->services()->end();
3866 std::string qualified_name = it->name()->str();
3867 auto service_def = new ServiceDef();
3868 service_def->defined_namespace =
3869 GetNamespace(qualified_name, namespaces_, namespaces_index);
3870 if (!service_def->Deserialize(*this, *it) ||
3871 services_.Add(qualified_name, service_def)) {
3881 std::string Parser::ConformTo(const Parser &base) {
3882 for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
3883 auto &struct_def = **sit;
3884 auto qualified_name =
3885 struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
3886 auto struct_def_base = base.LookupStruct(qualified_name);
3887 if (!struct_def_base) continue;
3888 for (auto fit = struct_def.fields.vec.begin();
3889 fit != struct_def.fields.vec.end(); ++fit) {
3890 auto &field = **fit;
3891 auto field_base = struct_def_base->fields.Lookup(field.name);
3893 if (field.value.offset != field_base->value.offset)
3894 return "offsets differ for field: " + field.name;
3895 if (field.value.constant != field_base->value.constant)
3896 return "defaults differ for field: " + field.name;
3897 if (!EqualByName(field.value.type, field_base->value.type))
3898 return "types differ for field: " + field.name;
3900 // Doesn't have to exist, deleting fields is fine.
3901 // But we should check if there is a field that has the same offset
3902 // but is incompatible (in the case of field renaming).
3903 for (auto fbit = struct_def_base->fields.vec.begin();
3904 fbit != struct_def_base->fields.vec.end(); ++fbit) {
3906 if (field.value.offset == field_base->value.offset) {
3907 if (!EqualByName(field.value.type, field_base->value.type))
3908 return "field renamed to different type: " + field.name;
3915 for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
3916 auto &enum_def = **eit;
3917 auto qualified_name =
3918 enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
3919 auto enum_def_base = base.enums_.Lookup(qualified_name);
3920 if (!enum_def_base) continue;
3921 for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
3923 auto &enum_val = **evit;
3924 auto enum_val_base = enum_def_base->Lookup(enum_val.name);
3925 if (enum_val_base) {
3926 if (enum_val != *enum_val_base)
3927 return "values differ for enum: " + enum_val.name;
3934 } // namespace flatbuffers