37692f9818f75fcd442065b4bd89e934bbb4088c
[platform/upstream/flatbuffers.git] / src / idl_parser.cpp
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <algorithm>
18 #include <list>
19 #include <string>
20 #include <utility>
21
22 #include <cmath>
23
24 #include "flatbuffers/idl.h"
25 #include "flatbuffers/util.h"
26
27 namespace flatbuffers {
28
29 // Reflects the version at the compiling time of binary(lib/dll/so).
30 const char *FLATBUFFERS_VERSION() {
31   // clang-format off
32   return
33       FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
34       FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
35       FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
36   // clang-format on
37 }
38
39 const double kPi = 3.14159265358979323846;
40
41 const char *const kTypeNames[] = {
42 // clang-format off
43   #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
44     CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
45     IDLTYPE,
46     FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
47   #undef FLATBUFFERS_TD
48   // clang-format on
49   nullptr
50 };
51
52 const char kTypeSizes[] = {
53 // clang-format off
54   #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
55       CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
56       sizeof(CTYPE),
57     FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
58   #undef FLATBUFFERS_TD
59   // clang-format on
60 };
61
62 // The enums in the reflection schema should match the ones we use internally.
63 // Compare the last element to check if these go out of sync.
64 static_assert(BASE_TYPE_UNION == static_cast<BaseType>(reflection::Union),
65               "enums don't match");
66
67 // Any parsing calls have to be wrapped in this macro, which automates
68 // handling of recursive error checking a bit. It will check the received
69 // CheckedError object, and return straight away on error.
70 #define ECHECK(call)           \
71   {                            \
72     auto ce = (call);          \
73     if (ce.Check()) return ce; \
74   }
75
76 // These two functions are called hundreds of times below, so define a short
77 // form:
78 #define NEXT() ECHECK(Next())
79 #define EXPECT(tok) ECHECK(Expect(tok))
80
81 static bool ValidateUTF8(const std::string &str) {
82   const char *s = &str[0];
83   const char *const sEnd = s + str.length();
84   while (s < sEnd) {
85     if (FromUTF8(&s) < 0) { return false; }
86   }
87   return true;
88 }
89
90 // Convert an underscore_based_indentifier in to camelCase.
91 // Also uppercases the first character if first is true.
92 std::string MakeCamel(const std::string &in, bool first) {
93   std::string s;
94   for (size_t i = 0; i < in.length(); i++) {
95     if (!i && first)
96       s += static_cast<char>(toupper(in[0]));
97     else if (in[i] == '_' && i + 1 < in.length())
98       s += static_cast<char>(toupper(in[++i]));
99     else
100       s += in[i];
101   }
102   return s;
103 }
104
105 // Convert an underscore_based_identifier in to screaming snake case.
106 std::string MakeScreamingCamel(const std::string &in) {
107   std::string s;
108   for (size_t i = 0; i < in.length(); i++) {
109     if (in[i] != '_')
110       s += static_cast<char>(toupper(in[i]));
111     else
112       s += in[i];
113   }
114   return s;
115 }
116
117 void DeserializeDoc( std::vector<std::string> &doc,
118                      const Vector<Offset<String>> *documentation) {
119   if (documentation == nullptr) return;
120   for (uoffset_t index = 0; index < documentation->size(); index++)
121     doc.push_back(documentation->Get(index)->str());
122 }
123
124 void Parser::Message(const std::string &msg) {
125   if (!error_.empty()) error_ += "\n";  // log all warnings and errors
126   error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
127   // clang-format off
128
129   #ifdef _WIN32  // MSVC alike
130     error_ +=
131         "(" + NumToString(line_) + ", " + NumToString(CursorPosition()) + ")";
132   #else  // gcc alike
133     if (file_being_parsed_.length()) error_ += ":";
134     error_ += NumToString(line_) + ": " + NumToString(CursorPosition());
135   #endif
136   // clang-format on
137   error_ += ": " + msg;
138 }
139
140 void Parser::Warning(const std::string &msg) { Message("warning: " + msg); }
141
142 CheckedError Parser::Error(const std::string &msg) {
143   Message("error: " + msg);
144   return CheckedError(true);
145 }
146
147 inline CheckedError NoError() { return CheckedError(false); }
148
149 CheckedError Parser::RecurseError() {
150   return Error("maximum parsing recursion of " +
151                NumToString(FLATBUFFERS_MAX_PARSING_DEPTH) + " reached");
152 }
153
154 template<typename F> CheckedError Parser::Recurse(F f) {
155   if (recurse_protection_counter >= (FLATBUFFERS_MAX_PARSING_DEPTH))
156     return RecurseError();
157   recurse_protection_counter++;
158   auto ce = f();
159   recurse_protection_counter--;
160   return ce;
161 }
162
163 template<typename T> std::string TypeToIntervalString() {
164   return "[" + NumToString((flatbuffers::numeric_limits<T>::lowest)()) + "; " +
165          NumToString((flatbuffers::numeric_limits<T>::max)()) + "]";
166 }
167
168 // atot: template version of atoi/atof: convert a string to an instance of T.
169 template<typename T>
170 inline CheckedError atot(const char *s, Parser &parser, T *val) {
171   auto done = StringToNumber(s, val);
172   if (done) return NoError();
173   if (0 == *val)
174     return parser.Error("invalid number: \"" + std::string(s) + "\"");
175   else
176     return parser.Error("invalid number: \"" + std::string(s) + "\"" +
177                         ", constant does not fit " + TypeToIntervalString<T>());
178 }
179 template<>
180 inline CheckedError atot<Offset<void>>(const char *s, Parser &parser,
181                                        Offset<void> *val) {
182   (void)parser;
183   *val = Offset<void>(atoi(s));
184   return NoError();
185 }
186
187 std::string Namespace::GetFullyQualifiedName(const std::string &name,
188                                              size_t max_components) const {
189   // Early exit if we don't have a defined namespace.
190   if (components.empty() || !max_components) { return name; }
191   std::string stream_str;
192   for (size_t i = 0; i < std::min(components.size(), max_components); i++) {
193     if (i) { stream_str += '.'; }
194     stream_str += std::string(components[i]);
195   }
196   if (name.length()) {
197     stream_str += '.';
198     stream_str += name;
199   }
200   return stream_str;
201 }
202
203 // Declare tokens we'll use. Single character tokens are represented by their
204 // ascii character code (e.g. '{'), others above 256.
205 // clang-format off
206 #define FLATBUFFERS_GEN_TOKENS(TD) \
207   TD(Eof, 256, "end of file") \
208   TD(StringConstant, 257, "string constant") \
209   TD(IntegerConstant, 258, "integer constant") \
210   TD(FloatConstant, 259, "float constant") \
211   TD(Identifier, 260, "identifier")
212 #ifdef __GNUC__
213 __extension__  // Stop GCC complaining about trailing comma with -Wpendantic.
214 #endif
215 enum {
216   #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE,
217     FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
218   #undef FLATBUFFERS_TOKEN
219 };
220
221 static std::string TokenToString(int t) {
222   static const char * const tokens[] = {
223     #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
224       FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
225     #undef FLATBUFFERS_TOKEN
226     #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
227       CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
228       IDLTYPE,
229       FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
230     #undef FLATBUFFERS_TD
231   };
232   if (t < 256) {  // A single ascii char token.
233     std::string s;
234     s.append(1, static_cast<char>(t));
235     return s;
236   } else {       // Other tokens.
237     return tokens[t - 256];
238   }
239 }
240 // clang-format on
241
242 std::string Parser::TokenToStringId(int t) const {
243   return t == kTokenIdentifier ? attribute_ : TokenToString(t);
244 }
245
246 // Parses exactly nibbles worth of hex digits into a number, or error.
247 CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
248   FLATBUFFERS_ASSERT(nibbles > 0);
249   for (int i = 0; i < nibbles; i++)
250     if (!is_xdigit(cursor_[i]))
251       return Error("escape code must be followed by " + NumToString(nibbles) +
252                    " hex digits");
253   std::string target(cursor_, cursor_ + nibbles);
254   *val = StringToUInt(target.c_str(), 16);
255   cursor_ += nibbles;
256   return NoError();
257 }
258
259 CheckedError Parser::SkipByteOrderMark() {
260   if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError();
261   cursor_++;
262   if (static_cast<unsigned char>(*cursor_) != 0xbb)
263     return Error("invalid utf-8 byte order mark");
264   cursor_++;
265   if (static_cast<unsigned char>(*cursor_) != 0xbf)
266     return Error("invalid utf-8 byte order mark");
267   cursor_++;
268   return NoError();
269 }
270
271 static inline bool IsIdentifierStart(char c) {
272   return is_alpha(c) || (c == '_');
273 }
274
275 CheckedError Parser::Next() {
276   doc_comment_.clear();
277   bool seen_newline = cursor_ == source_;
278   attribute_.clear();
279   attr_is_trivial_ascii_string_ = true;
280   for (;;) {
281     char c = *cursor_++;
282     token_ = c;
283     switch (c) {
284       case '\0':
285         cursor_--;
286         token_ = kTokenEof;
287         return NoError();
288       case ' ':
289       case '\r':
290       case '\t': break;
291       case '\n':
292         MarkNewLine();
293         seen_newline = true;
294         break;
295       case '{':
296       case '}':
297       case '(':
298       case ')':
299       case '[':
300       case ']':
301       case ',':
302       case ':':
303       case ';':
304       case '=': return NoError();
305       case '\"':
306       case '\'': {
307         int unicode_high_surrogate = -1;
308
309         while (*cursor_ != c) {
310           if (*cursor_ < ' ' && static_cast<signed char>(*cursor_) >= 0)
311             return Error("illegal character in string constant");
312           if (*cursor_ == '\\') {
313             attr_is_trivial_ascii_string_ = false;  // has escape sequence
314             cursor_++;
315             if (unicode_high_surrogate != -1 && *cursor_ != 'u') {
316               return Error(
317                   "illegal Unicode sequence (unpaired high surrogate)");
318             }
319             switch (*cursor_) {
320               case 'n':
321                 attribute_ += '\n';
322                 cursor_++;
323                 break;
324               case 't':
325                 attribute_ += '\t';
326                 cursor_++;
327                 break;
328               case 'r':
329                 attribute_ += '\r';
330                 cursor_++;
331                 break;
332               case 'b':
333                 attribute_ += '\b';
334                 cursor_++;
335                 break;
336               case 'f':
337                 attribute_ += '\f';
338                 cursor_++;
339                 break;
340               case '\"':
341                 attribute_ += '\"';
342                 cursor_++;
343                 break;
344               case '\'':
345                 attribute_ += '\'';
346                 cursor_++;
347                 break;
348               case '\\':
349                 attribute_ += '\\';
350                 cursor_++;
351                 break;
352               case '/':
353                 attribute_ += '/';
354                 cursor_++;
355                 break;
356               case 'x': {  // Not in the JSON standard
357                 cursor_++;
358                 uint64_t val;
359                 ECHECK(ParseHexNum(2, &val));
360                 attribute_ += static_cast<char>(val);
361                 break;
362               }
363               case 'u': {
364                 cursor_++;
365                 uint64_t val;
366                 ECHECK(ParseHexNum(4, &val));
367                 if (val >= 0xD800 && val <= 0xDBFF) {
368                   if (unicode_high_surrogate != -1) {
369                     return Error(
370                         "illegal Unicode sequence (multiple high surrogates)");
371                   } else {
372                     unicode_high_surrogate = static_cast<int>(val);
373                   }
374                 } else if (val >= 0xDC00 && val <= 0xDFFF) {
375                   if (unicode_high_surrogate == -1) {
376                     return Error(
377                         "illegal Unicode sequence (unpaired low surrogate)");
378                   } else {
379                     int code_point = 0x10000 +
380                                      ((unicode_high_surrogate & 0x03FF) << 10) +
381                                      (val & 0x03FF);
382                     ToUTF8(code_point, &attribute_);
383                     unicode_high_surrogate = -1;
384                   }
385                 } else {
386                   if (unicode_high_surrogate != -1) {
387                     return Error(
388                         "illegal Unicode sequence (unpaired high surrogate)");
389                   }
390                   ToUTF8(static_cast<int>(val), &attribute_);
391                 }
392                 break;
393               }
394               default: return Error("unknown escape code in string constant");
395             }
396           } else {  // printable chars + UTF-8 bytes
397             if (unicode_high_surrogate != -1) {
398               return Error(
399                   "illegal Unicode sequence (unpaired high surrogate)");
400             }
401             // reset if non-printable
402             attr_is_trivial_ascii_string_ &= check_ascii_range(*cursor_, ' ', '~');
403
404             attribute_ += *cursor_++;
405           }
406         }
407         if (unicode_high_surrogate != -1) {
408           return Error("illegal Unicode sequence (unpaired high surrogate)");
409         }
410         cursor_++;
411         if (!attr_is_trivial_ascii_string_ && !opts.allow_non_utf8 &&
412             !ValidateUTF8(attribute_)) {
413           return Error("illegal UTF-8 sequence");
414         }
415         token_ = kTokenStringConstant;
416         return NoError();
417       }
418       case '/':
419         if (*cursor_ == '/') {
420           const char *start = ++cursor_;
421           while (*cursor_ && *cursor_ != '\n' && *cursor_ != '\r') cursor_++;
422           if (*start == '/') {  // documentation comment
423             if (!seen_newline)
424               return Error(
425                   "a documentation comment should be on a line on its own");
426             doc_comment_.push_back(std::string(start + 1, cursor_));
427           }
428           break;
429         } else if (*cursor_ == '*') {
430           cursor_++;
431           // TODO: make nested.
432           while (*cursor_ != '*' || cursor_[1] != '/') {
433             if (*cursor_ == '\n') MarkNewLine();
434             if (!*cursor_) return Error("end of file in comment");
435             cursor_++;
436           }
437           cursor_ += 2;
438           break;
439         }
440         FLATBUFFERS_FALLTHROUGH(); // else fall thru
441       default:
442         const auto has_sign = (c == '+') || (c == '-');
443         // '-'/'+' and following identifier - can be a predefined constant like:
444         // NAN, INF, PI, etc.
445         if (IsIdentifierStart(c) || (has_sign && IsIdentifierStart(*cursor_))) {
446           // Collect all chars of an identifier:
447           const char *start = cursor_ - 1;
448           while (IsIdentifierStart(*cursor_) || is_digit(*cursor_)) cursor_++;
449           attribute_.append(start, cursor_);
450           token_ = has_sign ? kTokenStringConstant : kTokenIdentifier;
451           return NoError();
452         }
453
454         auto dot_lvl = (c == '.') ? 0 : 1;  // dot_lvl==0 <=> exactly one '.' seen
455         if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum?
456         // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4).
457         if (is_digit(c) || has_sign || !dot_lvl) {
458           const auto start = cursor_ - 1;
459           auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1;
460           if (!is_digit(c) && is_digit(*cursor_)){
461             start_digits = cursor_; // see digit in cursor_ position
462             c = *cursor_++;
463           }
464           // hex-float can't begind with '.'
465           auto use_hex = dot_lvl && (c == '0') && is_alpha_char(*cursor_, 'X');
466           if (use_hex) start_digits = ++cursor_;  // '0x' is the prefix, skip it
467           // Read an integer number or mantisa of float-point number.
468           do {
469             if (use_hex) {
470               while (is_xdigit(*cursor_)) cursor_++;
471             } else {
472               while (is_digit(*cursor_)) cursor_++;
473             }
474           } while ((*cursor_ == '.') && (++cursor_) && (--dot_lvl >= 0));
475           // Exponent of float-point number.
476           if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
477             // The exponent suffix of hexadecimal float number is mandatory.
478             if (use_hex && !dot_lvl) start_digits = cursor_;
479             if ((use_hex && is_alpha_char(*cursor_, 'P')) ||
480                 is_alpha_char(*cursor_, 'E')) {
481               dot_lvl = 0;  // Emulate dot to signal about float-point number.
482               cursor_++;
483               if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
484               start_digits = cursor_;  // the exponent-part has to have digits
485               // Exponent is decimal integer number
486               while (is_digit(*cursor_)) cursor_++;
487               if (*cursor_ == '.') {
488                 cursor_++;  // If see a dot treat it as part of invalid number.
489                 dot_lvl = -1;  // Fall thru to Error().
490               }
491             }
492           }
493           // Finalize.
494           if ((dot_lvl >= 0) && (cursor_ > start_digits)) {
495             attribute_.append(start, cursor_);
496             token_ = dot_lvl ? kTokenIntegerConstant : kTokenFloatConstant;
497             return NoError();
498           } else {
499             return Error("invalid number: " + std::string(start, cursor_));
500           }
501         }
502         std::string ch;
503         ch = c;
504         if (false == check_ascii_range(c, ' ', '~')) ch = "code: " + NumToString(c);
505         return Error("illegal character: " + ch);
506     }
507   }
508 }
509
510 // Check if a given token is next.
511 bool Parser::Is(int t) const { return t == token_; }
512
513 bool Parser::IsIdent(const char *id) const {
514   return token_ == kTokenIdentifier && attribute_ == id;
515 }
516
517 // Expect a given token to be next, consume it, or error if not present.
518 CheckedError Parser::Expect(int t) {
519   if (t != token_) {
520     return Error("expecting: " + TokenToString(t) +
521                  " instead got: " + TokenToStringId(token_));
522   }
523   NEXT();
524   return NoError();
525 }
526
527 CheckedError Parser::ParseNamespacing(std::string *id, std::string *last) {
528   while (Is('.')) {
529     NEXT();
530     *id += ".";
531     *id += attribute_;
532     if (last) *last = attribute_;
533     EXPECT(kTokenIdentifier);
534   }
535   return NoError();
536 }
537
538 EnumDef *Parser::LookupEnum(const std::string &id) {
539   // Search thru parent namespaces.
540   for (int components = static_cast<int>(current_namespace_->components.size());
541        components >= 0; components--) {
542     auto ed = enums_.Lookup(
543         current_namespace_->GetFullyQualifiedName(id, components));
544     if (ed) return ed;
545   }
546   return nullptr;
547 }
548
549 StructDef *Parser::LookupStruct(const std::string &id) const {
550   auto sd = structs_.Lookup(id);
551   if (sd) sd->refcount++;
552   return sd;
553 }
554
555 CheckedError Parser::ParseTypeIdent(Type &type) {
556   std::string id = attribute_;
557   EXPECT(kTokenIdentifier);
558   ECHECK(ParseNamespacing(&id, nullptr));
559   auto enum_def = LookupEnum(id);
560   if (enum_def) {
561     type = enum_def->underlying_type;
562     if (enum_def->is_union) type.base_type = BASE_TYPE_UNION;
563   } else {
564     type.base_type = BASE_TYPE_STRUCT;
565     type.struct_def = LookupCreateStruct(id);
566   }
567   return NoError();
568 }
569
570 // Parse any IDL type.
571 CheckedError Parser::ParseType(Type &type) {
572   if (token_ == kTokenIdentifier) {
573     if (IsIdent("bool")) {
574       type.base_type = BASE_TYPE_BOOL;
575       NEXT();
576     } else if (IsIdent("byte") || IsIdent("int8")) {
577       type.base_type = BASE_TYPE_CHAR;
578       NEXT();
579     } else if (IsIdent("ubyte") || IsIdent("uint8")) {
580       type.base_type = BASE_TYPE_UCHAR;
581       NEXT();
582     } else if (IsIdent("short") || IsIdent("int16")) {
583       type.base_type = BASE_TYPE_SHORT;
584       NEXT();
585     } else if (IsIdent("ushort") || IsIdent("uint16")) {
586       type.base_type = BASE_TYPE_USHORT;
587       NEXT();
588     } else if (IsIdent("int") || IsIdent("int32")) {
589       type.base_type = BASE_TYPE_INT;
590       NEXT();
591     } else if (IsIdent("uint") || IsIdent("uint32")) {
592       type.base_type = BASE_TYPE_UINT;
593       NEXT();
594     } else if (IsIdent("long") || IsIdent("int64")) {
595       type.base_type = BASE_TYPE_LONG;
596       NEXT();
597     } else if (IsIdent("ulong") || IsIdent("uint64")) {
598       type.base_type = BASE_TYPE_ULONG;
599       NEXT();
600     } else if (IsIdent("float") || IsIdent("float32")) {
601       type.base_type = BASE_TYPE_FLOAT;
602       NEXT();
603     } else if (IsIdent("double") || IsIdent("float64")) {
604       type.base_type = BASE_TYPE_DOUBLE;
605       NEXT();
606     } else if (IsIdent("string")) {
607       type.base_type = BASE_TYPE_STRING;
608       NEXT();
609     } else {
610       ECHECK(ParseTypeIdent(type));
611     }
612   } else if (token_ == '[') {
613     NEXT();
614     Type subtype;
615     ECHECK(Recurse([&]() { return ParseType(subtype); }));
616     if (IsSeries(subtype)) {
617       // We could support this, but it will complicate things, and it's
618       // easier to work around with a struct around the inner vector.
619       return Error("nested vector types not supported (wrap in table first)");
620     }
621     if (token_ == ':') {
622       NEXT();
623       if (token_ != kTokenIntegerConstant) {
624         return Error("length of fixed-length array must be an integer value");
625       }
626       uint16_t fixed_length = 0;
627       bool check = StringToNumber(attribute_.c_str(), &fixed_length);
628       if (!check || fixed_length < 1) {
629         return Error(
630             "length of fixed-length array must be positive and fit to "
631             "uint16_t type");
632       }
633       // Check if enum arrays are used in C++ without specifying --scoped-enums
634       if ((opts.lang_to_generate & IDLOptions::kCpp) && !opts.scoped_enums &&
635           IsEnum(subtype)) {
636         return Error(
637             "--scoped-enums must be enabled to use enum arrays in C++\n");
638       }
639       type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def,
640                   fixed_length);
641       NEXT();
642     } else {
643       type = Type(BASE_TYPE_VECTOR, subtype.struct_def, subtype.enum_def);
644     }
645     type.element = subtype.base_type;
646     EXPECT(']');
647   } else {
648     return Error("illegal type syntax");
649   }
650   return NoError();
651 }
652
653 CheckedError Parser::AddField(StructDef &struct_def, const std::string &name,
654                               const Type &type, FieldDef **dest) {
655   auto &field = *new FieldDef();
656   field.value.offset =
657       FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
658   field.name = name;
659   field.file = struct_def.file;
660   field.value.type = type;
661   if (struct_def.fixed) {  // statically compute the field offset
662     auto size = InlineSize(type);
663     auto alignment = InlineAlignment(type);
664     // structs_ need to have a predictable format, so we need to align to
665     // the largest scalar
666     struct_def.minalign = std::max(struct_def.minalign, alignment);
667     struct_def.PadLastField(alignment);
668     field.value.offset = static_cast<voffset_t>(struct_def.bytesize);
669     struct_def.bytesize += size;
670   }
671   if (struct_def.fields.Add(name, &field))
672     return Error("field already exists: " + name);
673   *dest = &field;
674   return NoError();
675 }
676
677 CheckedError Parser::ParseField(StructDef &struct_def) {
678   std::string name = attribute_;
679
680   if (LookupCreateStruct(name, false, false))
681     return Error("field name can not be the same as table/struct name");
682
683   std::vector<std::string> dc = doc_comment_;
684   EXPECT(kTokenIdentifier);
685   EXPECT(':');
686   Type type;
687   ECHECK(ParseType(type));
688
689   if (struct_def.fixed && !IsScalar(type.base_type) && !IsStruct(type) &&
690       !IsArray(type))
691     return Error("structs_ may contain only scalar or struct fields");
692
693   if (!struct_def.fixed && IsArray(type))
694     return Error("fixed-length array in table must be wrapped in struct");
695
696   if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
697     return Error(
698         "Arrays are not yet supported in all "
699         "the specified programming languages.");
700   }
701
702   FieldDef *typefield = nullptr;
703   if (type.base_type == BASE_TYPE_UNION) {
704     // For union fields, add a second auto-generated field to hold the type,
705     // with a special suffix.
706     ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
707                     type.enum_def->underlying_type, &typefield));
708   } else if (type.base_type == BASE_TYPE_VECTOR &&
709              type.element == BASE_TYPE_UNION) {
710     // Only cpp, js and ts supports the union vector feature so far.
711     if (!SupportsAdvancedUnionFeatures()) {
712       return Error(
713           "Vectors of unions are not yet supported in all "
714           "the specified programming languages.");
715     }
716     // For vector of union fields, add a second auto-generated vector field to
717     // hold the types, with a special suffix.
718     Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def);
719     union_vector.element = BASE_TYPE_UTYPE;
720     ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector,
721                     &typefield));
722   }
723
724   FieldDef *field;
725   ECHECK(AddField(struct_def, name, type, &field));
726
727   if (token_ == '=') {
728     NEXT();
729     ECHECK(ParseSingleValue(&field->name, field->value, true));
730     if (!IsScalar(type.base_type) ||
731         (struct_def.fixed && field->value.constant != "0"))
732       return Error(
733             "default values currently only supported for scalars in tables");
734   }
735   // Append .0 if the value has not it (skip hex and scientific floats).
736   // This suffix needed for generated C++ code.
737   if (IsFloat(type.base_type)) {
738     auto &text = field->value.constant;
739     FLATBUFFERS_ASSERT(false == text.empty());
740     auto s = text.c_str();
741     while(*s == ' ') s++;
742     if (*s == '-' || *s == '+') s++;
743     // 1) A float constants (nan, inf, pi, etc) is a kind of identifier.
744     // 2) A float number needn't ".0" at the end if it has exponent.
745     if ((false == IsIdentifierStart(*s)) &&
746         (std::string::npos == field->value.constant.find_first_of(".eEpP"))) {
747       field->value.constant += ".0";
748     }
749   }
750   if (type.enum_def) {
751     // The type.base_type can only be scalar, union, array or vector.
752     // Table, struct or string can't have enum_def.
753     // Default value of union and vector in NONE, NULL translated to "0".
754     FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
755                        (type.base_type == BASE_TYPE_UNION) ||
756                        (type.base_type == BASE_TYPE_VECTOR) ||
757                        (type.base_type == BASE_TYPE_ARRAY));
758     if (type.base_type == BASE_TYPE_VECTOR) {
759       // Vector can't use initialization list.
760       FLATBUFFERS_ASSERT(field->value.constant == "0");
761     } else {
762       // All unions should have the NONE ("0") enum value.
763       auto in_enum = type.enum_def->attributes.Lookup("bit_flags") ||
764                      type.enum_def->FindByValue(field->value.constant);
765       if (false == in_enum)
766         return Error("default value of " + field->value.constant +
767                      " for field " + name + " is not part of enum " +
768                      type.enum_def->name);
769     }
770   }
771
772   field->doc_comment = dc;
773   ECHECK(ParseMetaData(&field->attributes));
774   field->deprecated = field->attributes.Lookup("deprecated") != nullptr;
775   auto hash_name = field->attributes.Lookup("hash");
776   if (hash_name) {
777     switch ((type.base_type == BASE_TYPE_VECTOR) ? type.element : type.base_type) {
778       case BASE_TYPE_SHORT:
779       case BASE_TYPE_USHORT: {
780         if (FindHashFunction16(hash_name->constant.c_str()) == nullptr)
781           return Error("Unknown hashing algorithm for 16 bit types: " +
782                        hash_name->constant);
783         break;
784       }
785       case BASE_TYPE_INT:
786       case BASE_TYPE_UINT: {
787         if (FindHashFunction32(hash_name->constant.c_str()) == nullptr)
788           return Error("Unknown hashing algorithm for 32 bit types: " +
789                        hash_name->constant);
790         break;
791       }
792       case BASE_TYPE_LONG:
793       case BASE_TYPE_ULONG: {
794         if (FindHashFunction64(hash_name->constant.c_str()) == nullptr)
795           return Error("Unknown hashing algorithm for 64 bit types: " +
796                        hash_name->constant);
797         break;
798       }
799       default:
800         return Error(
801             "only short, ushort, int, uint, long and ulong data types support hashing.");
802     }
803   }
804   auto cpp_type = field->attributes.Lookup("cpp_type");
805   if (cpp_type) {
806     if (!hash_name)
807       return Error("cpp_type can only be used with a hashed field");
808     /// forcing cpp_ptr_type to 'naked' if unset
809     auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
810     if (!cpp_ptr_type) {
811       auto val = new Value();
812       val->type = cpp_type->type;
813       val->constant = "naked";
814       field->attributes.Add("cpp_ptr_type", val);
815     }
816   }
817   if (field->deprecated && struct_def.fixed)
818     return Error("can't deprecate fields in a struct");
819   field->required = field->attributes.Lookup("required") != nullptr;
820   if (field->required &&
821       (struct_def.fixed || IsScalar(type.base_type)))
822     return Error("only non-scalar fields in tables may be 'required'");
823   field->key = field->attributes.Lookup("key") != nullptr;
824   if (field->key) {
825     if (struct_def.has_key) return Error("only one field may be set as 'key'");
826     struct_def.has_key = true;
827     if (!IsScalar(type.base_type)) {
828       field->required = true;
829       if (type.base_type != BASE_TYPE_STRING)
830         return Error("'key' field must be string or scalar type");
831     }
832   }
833   field->shared = field->attributes.Lookup("shared") != nullptr;
834   if (field->shared && field->value.type.base_type != BASE_TYPE_STRING)
835     return Error("shared can only be defined on strings");
836
837   auto field_native_custom_alloc =
838       field->attributes.Lookup("native_custom_alloc");
839   if (field_native_custom_alloc)
840     return Error(
841         "native_custom_alloc can only be used with a table or struct "
842         "definition");
843
844   field->native_inline = field->attributes.Lookup("native_inline") != nullptr;
845   if (field->native_inline && !IsStruct(field->value.type))
846     return Error("native_inline can only be defined on structs");
847
848   auto nested = field->attributes.Lookup("nested_flatbuffer");
849   if (nested) {
850     if (nested->type.base_type != BASE_TYPE_STRING)
851       return Error(
852           "nested_flatbuffer attribute must be a string (the root type)");
853     if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
854       return Error(
855           "nested_flatbuffer attribute may only apply to a vector of ubyte");
856     // This will cause an error if the root type of the nested flatbuffer
857     // wasn't defined elsewhere.
858     field->nested_flatbuffer = LookupCreateStruct(nested->constant);
859   }
860
861   if (field->attributes.Lookup("flexbuffer")) {
862     field->flexbuffer = true;
863     uses_flexbuffers_ = true;
864     if (type.base_type != BASE_TYPE_VECTOR ||
865         type.element != BASE_TYPE_UCHAR)
866       return Error("flexbuffer attribute may only apply to a vector of ubyte");
867   }
868
869   if (typefield) {
870     if (!IsScalar(typefield->value.type.base_type)) {
871       // this is a union vector field
872       typefield->required = field->required;
873     }
874     // If this field is a union, and it has a manually assigned id,
875     // the automatically added type field should have an id as well (of N - 1).
876     auto attr = field->attributes.Lookup("id");
877     if (attr) {
878       auto id = atoi(attr->constant.c_str());
879       auto val = new Value();
880       val->type = attr->type;
881       val->constant = NumToString(id - 1);
882       typefield->attributes.Add("id", val);
883     }
884   }
885
886   EXPECT(';');
887   return NoError();
888 }
889
890 CheckedError Parser::ParseString(Value &val) {
891   auto s = attribute_;
892   EXPECT(kTokenStringConstant);
893   val.constant = NumToString(builder_.CreateString(s).o);
894   return NoError();
895 }
896
897 CheckedError Parser::ParseComma() {
898   if (!opts.protobuf_ascii_alike) EXPECT(',');
899   return NoError();
900 }
901
902 CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
903                                    size_t parent_fieldn,
904                                    const StructDef *parent_struct_def,
905                                    uoffset_t count,
906                                    bool inside_vector) {
907   switch (val.type.base_type) {
908     case BASE_TYPE_UNION: {
909       FLATBUFFERS_ASSERT(field);
910       std::string constant;
911       Vector<uint8_t> *vector_of_union_types = nullptr;
912       // Find corresponding type field we may have already parsed.
913       for (auto elem = field_stack_.rbegin() + count;
914            elem != field_stack_.rbegin() + parent_fieldn + count; ++elem) {
915         auto &type = elem->second->value.type;
916         if (type.enum_def == val.type.enum_def) {
917           if (inside_vector) {
918             if (type.base_type == BASE_TYPE_VECTOR &&
919                 type.element == BASE_TYPE_UTYPE) {
920               // Vector of union type field.
921               uoffset_t offset;
922               ECHECK(atot(elem->first.constant.c_str(), *this, &offset));
923               vector_of_union_types = reinterpret_cast<Vector<uint8_t> *>(
924                                         builder_.GetCurrentBufferPointer() +
925                                         builder_.GetSize() - offset);
926               break;
927             }
928           } else {
929             if (type.base_type == BASE_TYPE_UTYPE) {
930               // Union type field.
931               constant = elem->first.constant;
932               break;
933             }
934           }
935         }
936       }
937       if (constant.empty() && !inside_vector) {
938         // We haven't seen the type field yet. Sadly a lot of JSON writers
939         // output these in alphabetical order, meaning it comes after this
940         // value. So we scan past the value to find it, then come back here.
941         // We currently don't do this for vectors of unions because the
942         // scanning/serialization logic would get very complicated.
943         auto type_name = field->name + UnionTypeFieldSuffix();
944         FLATBUFFERS_ASSERT(parent_struct_def);
945         auto type_field = parent_struct_def->fields.Lookup(type_name);
946         FLATBUFFERS_ASSERT(type_field);  // Guaranteed by ParseField().
947         // Remember where we are in the source file, so we can come back here.
948         auto backup = *static_cast<ParserState *>(this);
949         ECHECK(SkipAnyJsonValue());  // The table.
950         ECHECK(ParseComma());
951         auto next_name = attribute_;
952         if (Is(kTokenStringConstant)) {
953           NEXT();
954         } else {
955           EXPECT(kTokenIdentifier);
956         }
957         if (next_name == type_name) {
958           EXPECT(':');
959           Value type_val = type_field->value;
960           ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr, 0));
961           constant = type_val.constant;
962           // Got the information we needed, now rewind:
963           *static_cast<ParserState *>(this) = backup;
964         }
965       }
966       if (constant.empty() && !vector_of_union_types) {
967         return Error("missing type field for this union value: " +
968                      field->name);
969       }
970       uint8_t enum_idx;
971       if (vector_of_union_types) {
972         enum_idx = vector_of_union_types->Get(count);
973       } else {
974         ECHECK(atot(constant.c_str(), *this, &enum_idx));
975       }
976       auto enum_val = val.type.enum_def->ReverseLookup(enum_idx, true);
977       if (!enum_val) return Error("illegal type id for: " + field->name);
978       if (enum_val->union_type.base_type == BASE_TYPE_STRUCT) {
979         ECHECK(ParseTable(*enum_val->union_type.struct_def, &val.constant,
980                           nullptr));
981         if (enum_val->union_type.struct_def->fixed) {
982           // All BASE_TYPE_UNION values are offsets, so turn this into one.
983           SerializeStruct(*enum_val->union_type.struct_def, val);
984           builder_.ClearOffsets();
985           val.constant = NumToString(builder_.GetSize());
986         }
987       } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
988         ECHECK(ParseString(val));
989       } else {
990         FLATBUFFERS_ASSERT(false);
991       }
992       break;
993     }
994     case BASE_TYPE_STRUCT:
995       ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
996       break;
997     case BASE_TYPE_STRING: {
998       ECHECK(ParseString(val));
999       break;
1000     }
1001     case BASE_TYPE_VECTOR: {
1002       uoffset_t off;
1003       ECHECK(ParseVector(val.type.VectorType(), &off, field, parent_fieldn));
1004       val.constant = NumToString(off);
1005       break;
1006     }
1007     case BASE_TYPE_ARRAY: {
1008       ECHECK(ParseArray(val));
1009       break;
1010     }
1011     case BASE_TYPE_INT:
1012     case BASE_TYPE_UINT:
1013     case BASE_TYPE_LONG:
1014     case BASE_TYPE_ULONG: {
1015       if (field && field->attributes.Lookup("hash") &&
1016           (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) {
1017         ECHECK(ParseHash(val, field));
1018       } else {
1019         ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1020       }
1021       break;
1022     }
1023     default:
1024       ECHECK(ParseSingleValue(field ? &field->name : nullptr, val, false));
1025       break;
1026   }
1027   return NoError();
1028 }
1029
1030 void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
1031   SerializeStruct(builder_, struct_def, val);
1032 }
1033
1034 void Parser::SerializeStruct(FlatBufferBuilder &builder,
1035                              const StructDef &struct_def, const Value &val) {
1036   FLATBUFFERS_ASSERT(val.constant.length() == struct_def.bytesize);
1037   builder.Align(struct_def.minalign);
1038   builder.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
1039                     struct_def.bytesize);
1040   builder.AddStructOffset(val.offset, builder.GetSize());
1041 }
1042
1043 template <typename F>
1044 CheckedError Parser::ParseTableDelimiters(size_t &fieldn,
1045                                           const StructDef *struct_def,
1046                                           F body) {
1047   // We allow tables both as JSON object{ .. } with field names
1048   // or vector[..] with all fields in order
1049   char terminator = '}';
1050   bool is_nested_vector = struct_def && Is('[');
1051   if (is_nested_vector) {
1052     NEXT();
1053     terminator = ']';
1054   } else {
1055     EXPECT('{');
1056   }
1057   for (;;) {
1058     if ((!opts.strict_json || !fieldn) && Is(terminator)) break;
1059     std::string name;
1060     if (is_nested_vector) {
1061       if (fieldn >= struct_def->fields.vec.size()) {
1062         return Error("too many unnamed fields in nested array");
1063       }
1064       name = struct_def->fields.vec[fieldn]->name;
1065     } else {
1066       name = attribute_;
1067       if (Is(kTokenStringConstant)) {
1068         NEXT();
1069       } else {
1070         EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
1071       }
1072       if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
1073     }
1074     ECHECK(body(name, fieldn, struct_def));
1075     if (Is(terminator)) break;
1076     ECHECK(ParseComma());
1077   }
1078   NEXT();
1079   if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
1080     return Error("wrong number of unnamed fields in table vector");
1081   }
1082   return NoError();
1083 }
1084
1085 CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
1086                                 uoffset_t *ovalue) {
1087   size_t fieldn_outer = 0;
1088   auto err = ParseTableDelimiters(
1089       fieldn_outer, &struct_def,
1090       [&](const std::string &name, size_t &fieldn,
1091           const StructDef *struct_def_inner) -> CheckedError {
1092         if (name == "$schema") {
1093           ECHECK(Expect(kTokenStringConstant));
1094           return NoError();
1095         }
1096         auto field = struct_def_inner->fields.Lookup(name);
1097         if (!field) {
1098           if (!opts.skip_unexpected_fields_in_json) {
1099             return Error("unknown field: " + name);
1100           } else {
1101             ECHECK(SkipAnyJsonValue());
1102           }
1103         } else {
1104           if (IsIdent("null") && !IsScalar(field->value.type.base_type)) {
1105             ECHECK(Next());  // Ignore this field.
1106           } else {
1107             Value val = field->value;
1108             if (field->flexbuffer) {
1109               flexbuffers::Builder builder(1024,
1110                                            flexbuffers::BUILDER_FLAG_SHARE_ALL);
1111               ECHECK(ParseFlexBufferValue(&builder));
1112               builder.Finish();
1113               // Force alignment for nested flexbuffer
1114               builder_.ForceVectorAlignment(builder.GetSize(), sizeof(uint8_t),
1115                                             sizeof(largest_scalar_t));
1116               auto off = builder_.CreateVector(builder.GetBuffer());
1117               val.constant = NumToString(off.o);
1118             } else if (field->nested_flatbuffer) {
1119               ECHECK(
1120                   ParseNestedFlatbuffer(val, field, fieldn, struct_def_inner));
1121             } else {
1122               ECHECK(Recurse([&]() {
1123                 return ParseAnyValue(val, field, fieldn, struct_def_inner, 0);
1124               }));
1125             }
1126             // Hardcoded insertion-sort with error-check.
1127             // If fields are specified in order, then this loop exits
1128             // immediately.
1129             auto elem = field_stack_.rbegin();
1130             for (; elem != field_stack_.rbegin() + fieldn; ++elem) {
1131               auto existing_field = elem->second;
1132               if (existing_field == field)
1133                 return Error("field set more than once: " + field->name);
1134               if (existing_field->value.offset < field->value.offset) break;
1135             }
1136             // Note: elem points to before the insertion point, thus .base()
1137             // points to the correct spot.
1138             field_stack_.insert(elem.base(), std::make_pair(val, field));
1139             fieldn++;
1140           }
1141         }
1142         return NoError();
1143       });
1144   ECHECK(err);
1145
1146   // Check if all required fields are parsed.
1147   for (auto field_it = struct_def.fields.vec.begin();
1148        field_it != struct_def.fields.vec.end(); ++field_it) {
1149     auto required_field = *field_it;
1150     if (!required_field->required) { continue; }
1151     bool found = false;
1152     for (auto pf_it = field_stack_.end() - fieldn_outer;
1153          pf_it != field_stack_.end(); ++pf_it) {
1154       auto parsed_field = pf_it->second;
1155       if (parsed_field == required_field) {
1156         found = true;
1157         break;
1158       }
1159     }
1160     if (!found) {
1161       return Error("required field is missing: " + required_field->name +
1162                    " in " + struct_def.name);
1163     }
1164   }
1165
1166   if (struct_def.fixed && fieldn_outer != struct_def.fields.vec.size())
1167     return Error("struct: wrong number of initializers: " + struct_def.name);
1168
1169   auto start = struct_def.fixed ? builder_.StartStruct(struct_def.minalign)
1170                                 : builder_.StartTable();
1171
1172   for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size;
1173        size /= 2) {
1174     // Go through elements in reverse, since we're building the data backwards.
1175     for (auto it = field_stack_.rbegin();
1176          it != field_stack_.rbegin() + fieldn_outer; ++it) {
1177       auto &field_value = it->first;
1178       auto field = it->second;
1179       if (!struct_def.sortbysize ||
1180           size == SizeOf(field_value.type.base_type)) {
1181         switch (field_value.type.base_type) {
1182           // clang-format off
1183           #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1184             CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1185             case BASE_TYPE_ ## ENUM: \
1186               builder_.Pad(field->padding); \
1187               if (struct_def.fixed) { \
1188                 CTYPE val; \
1189                 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1190                 builder_.PushElement(val); \
1191               } else { \
1192                 CTYPE val, valdef; \
1193                 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1194                 ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
1195                 builder_.AddElement(field_value.offset, val, valdef); \
1196               } \
1197               break;
1198             FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1199           #undef FLATBUFFERS_TD
1200           #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1201             CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1202             case BASE_TYPE_ ## ENUM: \
1203               builder_.Pad(field->padding); \
1204               if (IsStruct(field->value.type)) { \
1205                 SerializeStruct(*field->value.type.struct_def, field_value); \
1206               } else { \
1207                 CTYPE val; \
1208                 ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
1209                 builder_.AddOffset(field_value.offset, val); \
1210               } \
1211               break;
1212             FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD);
1213           #undef FLATBUFFERS_TD
1214             case BASE_TYPE_ARRAY:
1215               builder_.Pad(field->padding);
1216               builder_.PushBytes(
1217                 reinterpret_cast<const uint8_t*>(field_value.constant.c_str()),
1218                 InlineSize(field_value.type));
1219               break;
1220               // clang-format on
1221         }
1222       }
1223     }
1224   }
1225   for (size_t i = 0; i < fieldn_outer; i++) field_stack_.pop_back();
1226
1227   if (struct_def.fixed) {
1228     builder_.ClearOffsets();
1229     builder_.EndStruct();
1230     FLATBUFFERS_ASSERT(value);
1231     // Temporarily store this struct in the value string, since it is to
1232     // be serialized in-place elsewhere.
1233     value->assign(
1234         reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
1235         struct_def.bytesize);
1236     builder_.PopBytes(struct_def.bytesize);
1237     FLATBUFFERS_ASSERT(!ovalue);
1238   } else {
1239     auto val = builder_.EndTable(start);
1240     if (ovalue) *ovalue = val;
1241     if (value) *value = NumToString(val);
1242   }
1243   return NoError();
1244 }
1245
1246 template <typename F>
1247 CheckedError Parser::ParseVectorDelimiters(uoffset_t &count, F body) {
1248   EXPECT('[');
1249   for (;;) {
1250     if ((!opts.strict_json || !count) && Is(']')) break;
1251     ECHECK(body(count));
1252     count++;
1253     if (Is(']')) break;
1254     ECHECK(ParseComma());
1255   }
1256   NEXT();
1257   return NoError();
1258 }
1259
1260 static bool CompareType(const uint8_t *a, const uint8_t *b, BaseType ftype) {
1261   switch (ftype) {
1262     #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
1263                            PTYPE, RTYPE, KTYPE) \
1264     case BASE_TYPE_ ## ENUM: \
1265       return ReadScalar<CTYPE>(a) < ReadScalar<CTYPE>(b);
1266     FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
1267     #undef FLATBUFFERS_TD
1268     case BASE_TYPE_STRING:
1269       // Indirect offset pointer to string pointer.
1270       a += ReadScalar<uoffset_t>(a);
1271       b += ReadScalar<uoffset_t>(b);
1272       return *reinterpret_cast<const String *>(a) <
1273              *reinterpret_cast<const String *>(b);
1274     default: return false;
1275   }
1276 }
1277
1278 // See below for why we need our own sort :(
1279 template<typename T, typename F, typename S>
1280 void SimpleQsort(T *begin, T *end, size_t width, F comparator, S swapper) {
1281     if (end - begin <= static_cast<ptrdiff_t>(width)) return;
1282     auto l = begin + width;
1283     auto r = end;
1284     while (l < r) {
1285       if (comparator(begin, l)) {
1286         r -= width;
1287         swapper(l, r);
1288       } else {
1289         l++;
1290       }
1291     }
1292     l -= width;
1293     swapper(begin, l);
1294     SimpleQsort(begin, l, width, comparator, swapper);
1295     SimpleQsort(r, end, width, comparator, swapper);
1296 }
1297
1298 CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
1299                                  FieldDef *field, size_t fieldn) {
1300   uoffset_t count = 0;
1301   auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1302     Value val;
1303     val.type = type;
1304     ECHECK(Recurse([&]() {
1305       return ParseAnyValue(val, field, fieldn, nullptr, count, true);
1306     }));
1307     field_stack_.push_back(std::make_pair(val, nullptr));
1308     return NoError();
1309   });
1310   ECHECK(err);
1311
1312   builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
1313                        InlineAlignment(type));
1314   for (uoffset_t i = 0; i < count; i++) {
1315     // start at the back, since we're building the data backwards.
1316     auto &val = field_stack_.back().first;
1317     switch (val.type.base_type) {
1318       // clang-format off
1319       #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1320         CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1321         case BASE_TYPE_ ## ENUM: \
1322           if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
1323           else { \
1324              CTYPE elem; \
1325              ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1326              builder_.PushElement(elem); \
1327           } \
1328           break;
1329         FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1330       #undef FLATBUFFERS_TD
1331       // clang-format on
1332     }
1333     field_stack_.pop_back();
1334   }
1335
1336   builder_.ClearOffsets();
1337   *ovalue = builder_.EndVector(count);
1338
1339   if (type.base_type == BASE_TYPE_STRUCT && type.struct_def->has_key) {
1340     // We should sort this vector. Find the key first.
1341     const FieldDef *key = nullptr;
1342     for (auto it = type.struct_def->fields.vec.begin();
1343          it != type.struct_def->fields.vec.end(); ++it) {
1344       if ((*it)->key) {
1345         key = (*it);
1346         break;
1347       }
1348     }
1349     assert(key);
1350     // Now sort it.
1351     // We can't use std::sort because for structs the size is not known at
1352     // compile time, and for tables our iterators dereference offsets, so can't
1353     // be used to swap elements.
1354     // And we can't use C qsort either, since that would force use to use
1355     // globals, making parsing thread-unsafe.
1356     // So for now, we use SimpleQsort above.
1357     // TODO: replace with something better, preferably not recursive.
1358     static voffset_t offset = key->value.offset;
1359     static BaseType ftype = key->value.type.base_type;
1360
1361     if (type.struct_def->fixed) {
1362       auto v = reinterpret_cast<VectorOfAny *>(
1363                                   builder_.GetCurrentBufferPointer());
1364       SimpleQsort<uint8_t>(v->Data(),
1365                            v->Data() + v->size() * type.struct_def->bytesize,
1366                            type.struct_def->bytesize,
1367                  [](const uint8_t *a, const uint8_t *b) -> bool {
1368         return CompareType(a + offset, b + offset, ftype);
1369       }, [&](uint8_t *a, uint8_t *b) {
1370         // FIXME: faster?
1371         for (size_t i = 0; i < type.struct_def->bytesize; i++) {
1372           std::swap(a[i], b[i]);
1373         }
1374       });
1375     } else {
1376       auto v = reinterpret_cast<Vector<Offset<Table>> *>(
1377                                   builder_.GetCurrentBufferPointer());
1378       // Here also can't use std::sort. We do have an iterator type for it,
1379       // but it is non-standard as it will dereference the offsets, and thus
1380       // can't be used to swap elements.
1381       SimpleQsort<Offset<Table>>(v->data(), v->data() + v->size(), 1,
1382                  [](const Offset<Table> *_a, const Offset<Table> *_b) -> bool {
1383         // Indirect offset pointer to table pointer.
1384         auto a = reinterpret_cast<const uint8_t *>(_a) +
1385                  ReadScalar<uoffset_t>(_a);
1386         auto b = reinterpret_cast<const uint8_t *>(_b) +
1387                  ReadScalar<uoffset_t>(_b);
1388         // Fetch field address from table.
1389         a = reinterpret_cast<const Table *>(a)->GetAddressOf(offset);
1390         b = reinterpret_cast<const Table *>(b)->GetAddressOf(offset);
1391         return CompareType(a, b, ftype);
1392       }, [&](Offset<Table> *a, Offset<Table> *b) {
1393         // These are serialized offsets, so are relative where they are
1394         // stored in memory, so compute the distance between these pointers:
1395         ptrdiff_t diff = (b - a) * sizeof(Offset<Table>);
1396         assert(diff >= 0);  // Guaranteed by SimpleQsort.
1397         auto udiff = static_cast<uoffset_t>(diff);
1398         a->o = EndianScalar(ReadScalar<uoffset_t>(a) - udiff);
1399         b->o = EndianScalar(ReadScalar<uoffset_t>(b) + udiff);
1400         std::swap(*a, *b);
1401       });
1402     }
1403   }
1404   return NoError();
1405 }
1406
1407 CheckedError Parser::ParseArray(Value &array) {
1408   std::vector<Value> stack;
1409   FlatBufferBuilder builder;
1410   const auto &type = array.type.VectorType();
1411   auto length = array.type.fixed_length;
1412   uoffset_t count = 0;
1413   auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
1414     vector_emplace_back(&stack, Value());
1415     auto &val = stack.back();
1416     val.type = type;
1417     if (IsStruct(type)) {
1418       ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
1419     } else {
1420       ECHECK(ParseSingleValue(nullptr, val, false));
1421     }
1422     return NoError();
1423   });
1424   ECHECK(err);
1425   if (length != count) return Error("Fixed-length array size is incorrect.");
1426
1427   for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
1428     auto &val = *it;
1429     // clang-format off
1430     switch (val.type.base_type) {
1431       #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1432         CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1433         case BASE_TYPE_ ## ENUM: \
1434           if (IsStruct(val.type)) { \
1435             SerializeStruct(builder, *val.type.struct_def, val); \
1436           } else { \
1437             CTYPE elem; \
1438             ECHECK(atot(val.constant.c_str(), *this, &elem)); \
1439             builder.PushElement(elem); \
1440           } \
1441         break;
1442         FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1443       #undef FLATBUFFERS_TD
1444       default: FLATBUFFERS_ASSERT(0);
1445     }
1446     // clang-format on
1447   }
1448
1449   array.constant.assign(
1450       reinterpret_cast<const char *>(builder.GetCurrentBufferPointer()),
1451       InlineSize(array.type));
1452   return NoError();
1453 }
1454
1455 CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
1456                                            size_t fieldn,
1457                                            const StructDef *parent_struct_def) {
1458   if (token_ == '[') {  // backwards compat for 'legacy' ubyte buffers
1459     ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
1460   } else {
1461     auto cursor_at_value_begin = cursor_;
1462     ECHECK(SkipAnyJsonValue());
1463     std::string substring(cursor_at_value_begin - 1, cursor_ - 1);
1464
1465     // Create and initialize new parser
1466     Parser nested_parser;
1467     FLATBUFFERS_ASSERT(field->nested_flatbuffer);
1468     nested_parser.root_struct_def_ = field->nested_flatbuffer;
1469     nested_parser.enums_ = enums_;
1470     nested_parser.opts = opts;
1471     nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
1472
1473     // Parse JSON substring into new flatbuffer builder using nested_parser
1474     bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
1475
1476     // Clean nested_parser to avoid deleting the elements in
1477     // the SymbolTables on destruction
1478     nested_parser.enums_.dict.clear();
1479     nested_parser.enums_.vec.clear();
1480
1481     if (!ok) {
1482       ECHECK(Error(nested_parser.error_));
1483     }
1484     // Force alignment for nested flatbuffer
1485     builder_.ForceVectorAlignment(nested_parser.builder_.GetSize(), sizeof(uint8_t),
1486                                   nested_parser.builder_.GetBufferMinAlignment());
1487
1488     auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
1489                                      nested_parser.builder_.GetSize());
1490     val.constant = NumToString(off.o);
1491   }
1492   return NoError();
1493 }
1494
1495 CheckedError Parser::ParseMetaData(SymbolTable<Value> *attributes) {
1496   if (Is('(')) {
1497     NEXT();
1498     for (;;) {
1499       auto name = attribute_;
1500       if (false == (Is(kTokenIdentifier) || Is(kTokenStringConstant)))
1501         return Error("attribute name must be either identifier or string: " +
1502           name);
1503       if (known_attributes_.find(name) == known_attributes_.end())
1504         return Error("user define attributes must be declared before use: " +
1505                      name);
1506       NEXT();
1507       auto e = new Value();
1508       attributes->Add(name, e);
1509       if (Is(':')) {
1510         NEXT();
1511         ECHECK(ParseSingleValue(&name, *e, true));
1512       }
1513       if (Is(')')) {
1514         NEXT();
1515         break;
1516       }
1517       EXPECT(',');
1518     }
1519   }
1520   return NoError();
1521 }
1522
1523 CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
1524                                    bool check, Value &e, BaseType req,
1525                                    bool *destmatch) {
1526   bool match = dtoken == token_;
1527   if (match) {
1528     FLATBUFFERS_ASSERT(*destmatch == false);
1529     *destmatch = true;
1530     e.constant = attribute_;
1531     // Check token match
1532     if (!check) {
1533       if (e.type.base_type == BASE_TYPE_NONE) {
1534         e.type.base_type = req;
1535       } else {
1536         return Error(
1537             std::string("type mismatch: expecting: ") +
1538             kTypeNames[e.type.base_type] + ", found: " + kTypeNames[req] +
1539             ", name: " + (name ? *name : "") + ", value: " + e.constant);
1540       }
1541     }
1542     // The exponent suffix of hexadecimal float-point number is mandatory.
1543     // A hex-integer constant is forbidden as an initializer of float number.
1544     if ((kTokenFloatConstant != dtoken) && IsFloat(e.type.base_type)) {
1545       const auto &s = e.constant;
1546       const auto k = s.find_first_of("0123456789.");
1547       if ((std::string::npos != k) && (s.length() > (k + 1)) &&
1548           (s[k] == '0' && is_alpha_char(s[k + 1], 'X')) &&
1549           (std::string::npos == s.find_first_of("pP", k + 2))) {
1550         return Error(
1551             "invalid number, the exponent suffix of hexadecimal "
1552             "floating-point literals is mandatory: \"" +
1553             s + "\"");
1554       }
1555     }
1556
1557     NEXT();
1558   }
1559   return NoError();
1560 }
1561
1562 CheckedError Parser::ParseEnumFromString(const Type &type,
1563                                          std::string *result) {
1564   const auto base_type =
1565       type.enum_def ? type.enum_def->underlying_type.base_type : type.base_type;
1566   if (!IsInteger(base_type)) return Error("not a valid value for this field");
1567   uint64_t u64 = 0;
1568   for (size_t pos = 0; pos != std::string::npos;) {
1569     const auto delim = attribute_.find_first_of(' ', pos);
1570     const auto last = (std::string::npos == delim);
1571     auto word = attribute_.substr(pos, !last ? delim - pos : std::string::npos);
1572     pos = !last ? delim + 1 : std::string::npos;
1573     const EnumVal *ev = nullptr;
1574     if (type.enum_def) {
1575       ev = type.enum_def->Lookup(word);
1576     } else {
1577       auto dot = word.find_first_of('.');
1578       if (std::string::npos == dot)
1579         return Error("enum values need to be qualified by an enum type");
1580       auto enum_def_str = word.substr(0, dot);
1581       const auto enum_def = LookupEnum(enum_def_str);
1582       if (!enum_def) return Error("unknown enum: " + enum_def_str);
1583       auto enum_val_str = word.substr(dot + 1);
1584       ev = enum_def->Lookup(enum_val_str);
1585     }
1586     if (!ev) return Error("unknown enum value: " + word);
1587     u64 |= ev->GetAsUInt64();
1588   }
1589   *result = IsUnsigned(base_type) ? NumToString(u64)
1590                                   : NumToString(static_cast<int64_t>(u64));
1591   return NoError();
1592 }
1593
1594 CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
1595   FLATBUFFERS_ASSERT(field);
1596   Value *hash_name = field->attributes.Lookup("hash");
1597   switch (e.type.base_type) {
1598     case BASE_TYPE_SHORT: {
1599       auto hash = FindHashFunction16(hash_name->constant.c_str());
1600       int16_t hashed_value = static_cast<int16_t>(hash(attribute_.c_str()));
1601       e.constant = NumToString(hashed_value);
1602       break;
1603     }
1604     case BASE_TYPE_USHORT: {
1605       auto hash = FindHashFunction16(hash_name->constant.c_str());
1606       uint16_t hashed_value = hash(attribute_.c_str());
1607       e.constant = NumToString(hashed_value);
1608       break;
1609     }
1610     case BASE_TYPE_INT: {
1611       auto hash = FindHashFunction32(hash_name->constant.c_str());
1612       int32_t hashed_value = static_cast<int32_t>(hash(attribute_.c_str()));
1613       e.constant = NumToString(hashed_value);
1614       break;
1615     }
1616     case BASE_TYPE_UINT: {
1617       auto hash = FindHashFunction32(hash_name->constant.c_str());
1618       uint32_t hashed_value = hash(attribute_.c_str());
1619       e.constant = NumToString(hashed_value);
1620       break;
1621     }
1622     case BASE_TYPE_LONG: {
1623       auto hash = FindHashFunction64(hash_name->constant.c_str());
1624       int64_t hashed_value = static_cast<int64_t>(hash(attribute_.c_str()));
1625       e.constant = NumToString(hashed_value);
1626       break;
1627     }
1628     case BASE_TYPE_ULONG: {
1629       auto hash = FindHashFunction64(hash_name->constant.c_str());
1630       uint64_t hashed_value = hash(attribute_.c_str());
1631       e.constant = NumToString(hashed_value);
1632       break;
1633     }
1634     default: FLATBUFFERS_ASSERT(0);
1635   }
1636   NEXT();
1637   return NoError();
1638 }
1639
1640 CheckedError Parser::TokenError() {
1641   return Error("cannot parse value starting with: " + TokenToStringId(token_));
1642 }
1643
1644 // Re-pack helper (ParseSingleValue) to normalize defaults of scalars.
1645 template<typename T> inline void SingleValueRepack(Value &e, T val) {
1646   // Remove leading zeros.
1647   if (IsInteger(e.type.base_type)) { e.constant = NumToString(val); }
1648 }
1649 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
1650 // Normilaze defaults NaN to unsigned quiet-NaN(0).
1651 static inline void SingleValueRepack(Value& e, float val) {
1652   if (val != val) e.constant = "nan";
1653 }
1654 static inline void SingleValueRepack(Value& e, double val) {
1655   if (val != val) e.constant = "nan";
1656 }
1657 #endif
1658
1659 CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
1660                                       bool check_now) {
1661   // First see if this could be a conversion function:
1662   if (token_ == kTokenIdentifier && *cursor_ == '(') {
1663     // todo: Extract processing of conversion functions to ParseFunction.
1664     const auto functionname = attribute_;
1665     if (!IsFloat(e.type.base_type)) {
1666       return Error(functionname + ": type of argument mismatch, expecting: " +
1667                    kTypeNames[BASE_TYPE_DOUBLE] +
1668                    ", found: " + kTypeNames[e.type.base_type] +
1669                    ", name: " + (name ? *name : "") + ", value: " + e.constant);
1670     }
1671     NEXT();
1672     EXPECT('(');
1673     ECHECK(Recurse([&]() { return ParseSingleValue(name, e, false); }));
1674     EXPECT(')');
1675     // calculate with double precision
1676     double x, y = 0.0;
1677     ECHECK(atot(e.constant.c_str(), *this, &x));
1678     auto func_match = false;
1679     // clang-format off
1680     #define FLATBUFFERS_FN_DOUBLE(name, op) \
1681       if (!func_match && functionname == name) { y = op; func_match = true; }
1682     FLATBUFFERS_FN_DOUBLE("deg", x / kPi * 180);
1683     FLATBUFFERS_FN_DOUBLE("rad", x * kPi / 180);
1684     FLATBUFFERS_FN_DOUBLE("sin", sin(x));
1685     FLATBUFFERS_FN_DOUBLE("cos", cos(x));
1686     FLATBUFFERS_FN_DOUBLE("tan", tan(x));
1687     FLATBUFFERS_FN_DOUBLE("asin", asin(x));
1688     FLATBUFFERS_FN_DOUBLE("acos", acos(x));
1689     FLATBUFFERS_FN_DOUBLE("atan", atan(x));
1690     // TODO(wvo): add more useful conversion functions here.
1691     #undef FLATBUFFERS_FN_DOUBLE
1692     // clang-format on
1693     if (true != func_match) {
1694       return Error(std::string("Unknown conversion function: ") + functionname +
1695                    ", field name: " + (name ? *name : "") +
1696                    ", value: " + e.constant);
1697     }
1698     e.constant = NumToString(y);
1699     return NoError();
1700   }
1701
1702   auto match = false;
1703   const auto in_type = e.type.base_type;
1704   // clang-format off
1705   #define IF_ECHECK_(force, dtoken, check, req)    \
1706     if (!match && ((check) || IsConstTrue(force))) \
1707     ECHECK(TryTypedValue(name, dtoken, check, e, req, &match))
1708   #define TRY_ECHECK(dtoken, check, req) IF_ECHECK_(false, dtoken, check, req)
1709   #define FORCE_ECHECK(dtoken, check, req) IF_ECHECK_(true, dtoken, check, req)
1710   // clang-format on
1711
1712   if (token_ == kTokenStringConstant || token_ == kTokenIdentifier) {
1713     const auto kTokenStringOrIdent = token_;
1714     // The string type is a most probable type, check it first.
1715     TRY_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1716                BASE_TYPE_STRING);
1717
1718     // avoid escaped and non-ascii in the string
1719     if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type) &&
1720         !attr_is_trivial_ascii_string_) {
1721       return Error(
1722           std::string("type mismatch or invalid value, an initializer of "
1723                       "non-string field must be trivial ASCII string: type: ") +
1724           kTypeNames[in_type] + ", name: " + (name ? *name : "") +
1725           ", value: " + attribute_);
1726     }
1727
1728     // A boolean as true/false. Boolean as Integer check below.
1729     if (!match && IsBool(in_type)) {
1730       auto is_true = attribute_ == "true";
1731       if (is_true || attribute_ == "false") {
1732         attribute_ = is_true ? "1" : "0";
1733         // accepts both kTokenStringConstant and kTokenIdentifier
1734         TRY_ECHECK(kTokenStringOrIdent, IsBool(in_type), BASE_TYPE_BOOL);
1735       }
1736     }
1737     // Check if this could be a string/identifier enum value.
1738     // Enum can have only true integer base type.
1739     if (!match && IsInteger(in_type) && !IsBool(in_type) &&
1740         IsIdentifierStart(*attribute_.c_str())) {
1741       ECHECK(ParseEnumFromString(e.type, &e.constant));
1742       NEXT();
1743       match = true;
1744     }
1745     // Parse a float/integer number from the string.
1746     if (!match) check_now = true;  // Re-pack if parsed from string literal.
1747     if (!match && (token_ == kTokenStringConstant) && IsScalar(in_type)) {
1748       // remove trailing whitespaces from attribute_
1749       auto last = attribute_.find_last_not_of(' ');
1750       if (std::string::npos != last)  // has non-whitespace
1751         attribute_.resize(last + 1);
1752     }
1753     // Float numbers or nan, inf, pi, etc.
1754     TRY_ECHECK(kTokenStringOrIdent, IsFloat(in_type), BASE_TYPE_FLOAT);
1755     // An integer constant in string.
1756     TRY_ECHECK(kTokenStringOrIdent, IsInteger(in_type), BASE_TYPE_INT);
1757     // Unknown tokens will be interpreted as string type.
1758     // An attribute value may be a scalar or string constant.
1759     FORCE_ECHECK(kTokenStringConstant, in_type == BASE_TYPE_STRING,
1760                  BASE_TYPE_STRING);
1761   } else {
1762     // Try a float number.
1763     TRY_ECHECK(kTokenFloatConstant, IsFloat(in_type), BASE_TYPE_FLOAT);
1764     // Integer token can init any scalar (integer of float).
1765     FORCE_ECHECK(kTokenIntegerConstant, IsScalar(in_type), BASE_TYPE_INT);
1766   }
1767 #undef FORCE_ECHECK
1768 #undef TRY_ECHECK
1769 #undef IF_ECHECK_
1770
1771   if (!match) {
1772     std::string msg;
1773     msg += "Cannot assign token starting with '" + TokenToStringId(token_) +
1774            "' to value of <" + std::string(kTypeNames[in_type]) + "> type.";
1775     return Error(msg);
1776   }
1777   const auto match_type = e.type.base_type; // may differ from in_type
1778   // The check_now flag must be true when parse a fbs-schema.
1779   // This flag forces to check default scalar values or metadata of field.
1780   // For JSON parser the flag should be false.
1781   // If it is set for JSON each value will be checked twice (see ParseTable).
1782   if (check_now && IsScalar(match_type)) {
1783     // clang-format off
1784     switch (match_type) {
1785     #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
1786             CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
1787             case BASE_TYPE_ ## ENUM: {\
1788                 CTYPE val; \
1789                 ECHECK(atot(e.constant.c_str(), *this, &val)); \
1790                 SingleValueRepack(e, val); \
1791               break; }
1792     FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
1793     #undef FLATBUFFERS_TD
1794     default: break;
1795     }
1796     // clang-format on
1797   }
1798   return NoError();
1799 }
1800
1801 StructDef *Parser::LookupCreateStruct(const std::string &name,
1802                                       bool create_if_new, bool definition) {
1803   std::string qualified_name = current_namespace_->GetFullyQualifiedName(name);
1804   // See if it exists pre-declared by an unqualified use.
1805   auto struct_def = LookupStruct(name);
1806   if (struct_def && struct_def->predecl) {
1807     if (definition) {
1808       // Make sure it has the current namespace, and is registered under its
1809       // qualified name.
1810       struct_def->defined_namespace = current_namespace_;
1811       structs_.Move(name, qualified_name);
1812     }
1813     return struct_def;
1814   }
1815   // See if it exists pre-declared by an qualified use.
1816   struct_def = LookupStruct(qualified_name);
1817   if (struct_def && struct_def->predecl) {
1818     if (definition) {
1819       // Make sure it has the current namespace.
1820       struct_def->defined_namespace = current_namespace_;
1821     }
1822     return struct_def;
1823   }
1824   if (!definition) {
1825     // Search thru parent namespaces.
1826     for (size_t components = current_namespace_->components.size();
1827          components && !struct_def; components--) {
1828       struct_def = LookupStruct(
1829           current_namespace_->GetFullyQualifiedName(name, components - 1));
1830     }
1831   }
1832   if (!struct_def && create_if_new) {
1833     struct_def = new StructDef();
1834     if (definition) {
1835       structs_.Add(qualified_name, struct_def);
1836       struct_def->name = name;
1837       struct_def->defined_namespace = current_namespace_;
1838     } else {
1839       // Not a definition.
1840       // Rather than failing, we create a "pre declared" StructDef, due to
1841       // circular references, and check for errors at the end of parsing.
1842       // It is defined in the current namespace, as the best guess what the
1843       // final namespace will be.
1844       structs_.Add(name, struct_def);
1845       struct_def->name = name;
1846       struct_def->defined_namespace = current_namespace_;
1847       struct_def->original_location.reset(
1848           new std::string(file_being_parsed_ + ":" + NumToString(line_)));
1849     }
1850   }
1851   return struct_def;
1852 }
1853
1854 const EnumVal *EnumDef::MinValue() const {
1855   return vals.vec.empty() ? nullptr : vals.vec.front();
1856 }
1857 const EnumVal *EnumDef::MaxValue() const {
1858   return vals.vec.empty() ? nullptr : vals.vec.back();
1859 }
1860
1861 template<typename T> static uint64_t EnumDistanceImpl(T e1, T e2) {
1862   if (e1 < e2) { std::swap(e1, e2); }  // use std for scalars
1863   // Signed overflow may occur, use unsigned calculation.
1864   // The unsigned overflow is well-defined by C++ standard (modulo 2^n).
1865   return static_cast<uint64_t>(e1) - static_cast<uint64_t>(e2);
1866 }
1867
1868 uint64_t EnumDef::Distance(const EnumVal *v1, const EnumVal *v2) const {
1869   return IsUInt64() ? EnumDistanceImpl(v1->GetAsUInt64(), v2->GetAsUInt64())
1870                     : EnumDistanceImpl(v1->GetAsInt64(), v2->GetAsInt64());
1871 }
1872
1873 std::string EnumDef::AllFlags() const {
1874   FLATBUFFERS_ASSERT(attributes.Lookup("bit_flags"));
1875   uint64_t u64 = 0;
1876   for (auto it = Vals().begin(); it != Vals().end(); ++it) {
1877     u64 |= (*it)->GetAsUInt64();
1878   }
1879   return IsUInt64() ? NumToString(u64) : NumToString(static_cast<int64_t>(u64));
1880 }
1881
1882 EnumVal *EnumDef::ReverseLookup(int64_t enum_idx,
1883                                 bool skip_union_default) const {
1884   auto skip_first = static_cast<int>(is_union && skip_union_default);
1885   for (auto it = Vals().begin() + skip_first; it != Vals().end(); ++it) {
1886     if ((*it)->GetAsInt64() == enum_idx) { return *it; }
1887   }
1888   return nullptr;
1889 }
1890
1891 EnumVal *EnumDef::FindByValue(const std::string &constant) const {
1892   int64_t i64;
1893   auto done = false;
1894   if (IsUInt64()) {
1895     uint64_t u64;  // avoid reinterpret_cast of pointers
1896     done = StringToNumber(constant.c_str(), &u64);
1897     i64 = static_cast<int64_t>(u64);
1898   } else {
1899     done = StringToNumber(constant.c_str(), &i64);
1900   }
1901   FLATBUFFERS_ASSERT(done);
1902   if (!done) return nullptr;
1903   return ReverseLookup(i64, false);
1904 }
1905
1906 void EnumDef::SortByValue() {
1907   auto &v = vals.vec;
1908   if (IsUInt64())
1909     std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1910       return e1->GetAsUInt64() < e2->GetAsUInt64();
1911     });
1912   else
1913     std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
1914       return e1->GetAsInt64() < e2->GetAsInt64();
1915     });
1916 }
1917
1918 void EnumDef::RemoveDuplicates() {
1919   // This method depends form SymbolTable implementation!
1920   // 1) vals.vec - owner (raw pointer)
1921   // 2) vals.dict - access map
1922   auto first = vals.vec.begin();
1923   auto last = vals.vec.end();
1924   if (first == last) return;
1925   auto result = first;
1926   while (++first != last) {
1927     if ((*result)->value != (*first)->value) {
1928       *(++result) = *first;
1929     } else {
1930       auto ev = *first;
1931       for (auto it = vals.dict.begin(); it != vals.dict.end(); ++it) {
1932         if (it->second == ev) it->second = *result;  // reassign
1933       }
1934       delete ev;  // delete enum value
1935       *first = nullptr;
1936     }
1937   }
1938   vals.vec.erase(++result, last);
1939 }
1940
1941 template<typename T> void EnumDef::ChangeEnumValue(EnumVal *ev, T new_value) {
1942   ev->value = static_cast<int64_t>(new_value);
1943 }
1944
1945 namespace EnumHelper {
1946 template<BaseType E> struct EnumValType { typedef int64_t type; };
1947 template<> struct EnumValType<BASE_TYPE_ULONG> { typedef uint64_t type; };
1948 }  // namespace EnumHelper
1949
1950 struct EnumValBuilder {
1951   EnumVal *CreateEnumerator(const std::string &ev_name) {
1952     FLATBUFFERS_ASSERT(!temp);
1953     auto first = enum_def.vals.vec.empty();
1954     user_value = first;
1955     temp = new EnumVal(ev_name, first ? 0 : enum_def.vals.vec.back()->value);
1956     return temp;
1957   }
1958
1959   EnumVal *CreateEnumerator(const std::string &ev_name, int64_t val) {
1960     FLATBUFFERS_ASSERT(!temp);
1961     user_value = true;
1962     temp = new EnumVal(ev_name, val);
1963     return temp;
1964   }
1965
1966   FLATBUFFERS_CHECKED_ERROR AcceptEnumerator(const std::string &name) {
1967     FLATBUFFERS_ASSERT(temp);
1968     ECHECK(ValidateValue(&temp->value, false == user_value));
1969     FLATBUFFERS_ASSERT((temp->union_type.enum_def == nullptr) ||
1970                        (temp->union_type.enum_def == &enum_def));
1971     auto not_unique = enum_def.vals.Add(name, temp);
1972     temp = nullptr;
1973     if (not_unique) return parser.Error("enum value already exists: " + name);
1974     return NoError();
1975   }
1976
1977   FLATBUFFERS_CHECKED_ERROR AcceptEnumerator() {
1978     return AcceptEnumerator(temp->name);
1979   }
1980
1981   FLATBUFFERS_CHECKED_ERROR AssignEnumeratorValue(const std::string &value) {
1982     user_value = true;
1983     auto fit = false;
1984     auto ascending = false;
1985     if (enum_def.IsUInt64()) {
1986       uint64_t u64;
1987       fit = StringToNumber(value.c_str(), &u64);
1988       ascending = u64 > temp->GetAsUInt64();
1989       temp->value = static_cast<int64_t>(u64);  // well-defined since C++20.
1990     } else {
1991       int64_t i64;
1992       fit = StringToNumber(value.c_str(), &i64);
1993       ascending = i64 > temp->GetAsInt64();
1994       temp->value = i64;
1995     }
1996     if (!fit) return parser.Error("enum value does not fit, \"" + value + "\"");
1997     if (!ascending && strict_ascending && !enum_def.vals.vec.empty())
1998       return parser.Error("enum values must be specified in ascending order");
1999     return NoError();
2000   }
2001
2002   template<BaseType E, typename CTYPE>
2003   inline FLATBUFFERS_CHECKED_ERROR ValidateImpl(int64_t *ev, int m) {
2004     typedef typename EnumHelper::EnumValType<E>::type T;  // int64_t or uint64_t
2005     static_assert(sizeof(T) == sizeof(int64_t), "invalid EnumValType");
2006     const auto v = static_cast<T>(*ev);
2007     auto up = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::max)());
2008     auto dn = static_cast<T>((flatbuffers::numeric_limits<CTYPE>::lowest)());
2009     if (v < dn || v > (up - m)) {
2010       return parser.Error("enum value does not fit, \"" + NumToString(v) +
2011                           (m ? " + 1\"" : "\"") + " out of " +
2012                           TypeToIntervalString<CTYPE>());
2013     }
2014     *ev = static_cast<int64_t>(v + m);  // well-defined since C++20.
2015     return NoError();
2016   }
2017
2018   FLATBUFFERS_CHECKED_ERROR ValidateValue(int64_t *ev, bool next) {
2019     // clang-format off
2020     switch (enum_def.underlying_type.base_type) {
2021     #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE,   \
2022                            PTYPE, RTYPE, KTYPE)                         \
2023       case BASE_TYPE_##ENUM: {                                          \
2024         if (!IsInteger(BASE_TYPE_##ENUM)) break;                        \
2025         return ValidateImpl<BASE_TYPE_##ENUM, CTYPE>(ev, next ? 1 : 0); \
2026       }
2027       FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
2028     #undef FLATBUFFERS_TD
2029     default: break;
2030     }
2031     // clang-format on
2032     return parser.Error("fatal: invalid enum underlying type");
2033   }
2034
2035   EnumValBuilder(Parser &_parser, EnumDef &_enum_def, bool strict_order = true)
2036       : parser(_parser),
2037         enum_def(_enum_def),
2038         temp(nullptr),
2039         strict_ascending(strict_order),
2040         user_value(false) {}
2041
2042   ~EnumValBuilder() { delete temp; }
2043
2044   Parser &parser;
2045   EnumDef &enum_def;
2046   EnumVal *temp;
2047   const bool strict_ascending;
2048   bool user_value;
2049 };
2050
2051 CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest) {
2052   std::vector<std::string> enum_comment = doc_comment_;
2053   NEXT();
2054   std::string enum_name = attribute_;
2055   EXPECT(kTokenIdentifier);
2056   EnumDef *enum_def;
2057   ECHECK(StartEnum(enum_name, is_union, &enum_def));
2058   enum_def->doc_comment = enum_comment;
2059   if (!is_union && !opts.proto_mode) {
2060     // Give specialized error message, since this type spec used to
2061     // be optional in the first FlatBuffers release.
2062     if (!Is(':')) {
2063       return Error(
2064           "must specify the underlying integer type for this"
2065           " enum (e.g. \': short\', which was the default).");
2066     } else {
2067       NEXT();
2068     }
2069     // Specify the integer type underlying this enum.
2070     ECHECK(ParseType(enum_def->underlying_type));
2071     if (!IsInteger(enum_def->underlying_type.base_type) ||
2072         IsBool(enum_def->underlying_type.base_type))
2073       return Error("underlying enum type must be integral");
2074     // Make this type refer back to the enum it was derived from.
2075     enum_def->underlying_type.enum_def = enum_def;
2076   }
2077   ECHECK(ParseMetaData(&enum_def->attributes));
2078   const auto underlying_type = enum_def->underlying_type.base_type;
2079   if (enum_def->attributes.Lookup("bit_flags") &&
2080       !IsUnsigned(underlying_type)) {
2081     // todo: Convert to the Error in the future?
2082     Warning("underlying type of bit_flags enum must be unsigned");
2083   }
2084   // Protobuf allows them to be specified in any order, so sort afterwards.
2085   const auto strict_ascending = (false == opts.proto_mode);
2086   EnumValBuilder evb(*this, *enum_def, strict_ascending);
2087   EXPECT('{');
2088   // A lot of code generatos expect that an enum is not-empty.
2089   if ((is_union || Is('}')) && !opts.proto_mode) {
2090     evb.CreateEnumerator("NONE");
2091     ECHECK(evb.AcceptEnumerator());
2092   }
2093   std::set<std::pair<BaseType, StructDef *>> union_types;
2094   while (!Is('}')) {
2095     if (opts.proto_mode && attribute_ == "option") {
2096       ECHECK(ParseProtoOption());
2097     } else {
2098       auto &ev = *evb.CreateEnumerator(attribute_);
2099       auto full_name = ev.name;
2100       ev.doc_comment = doc_comment_;
2101       EXPECT(kTokenIdentifier);
2102       if (is_union) {
2103         ECHECK(ParseNamespacing(&full_name, &ev.name));
2104         if (opts.union_value_namespacing) {
2105           // Since we can't namespace the actual enum identifiers, turn
2106           // namespace parts into part of the identifier.
2107           ev.name = full_name;
2108           std::replace(ev.name.begin(), ev.name.end(), '.', '_');
2109         }
2110         if (Is(':')) {
2111           NEXT();
2112           ECHECK(ParseType(ev.union_type));
2113           if (ev.union_type.base_type != BASE_TYPE_STRUCT &&
2114               ev.union_type.base_type != BASE_TYPE_STRING)
2115             return Error("union value type may only be table/struct/string");
2116         } else {
2117           ev.union_type = Type(BASE_TYPE_STRUCT, LookupCreateStruct(full_name));
2118         }
2119         if (!enum_def->uses_multiple_type_instances) {
2120           auto ins = union_types.insert(std::make_pair(
2121               ev.union_type.base_type, ev.union_type.struct_def));
2122           enum_def->uses_multiple_type_instances = (false == ins.second);
2123         }
2124       }
2125
2126       if (Is('=')) {
2127         NEXT();
2128         ECHECK(evb.AssignEnumeratorValue(attribute_));
2129         EXPECT(kTokenIntegerConstant);
2130       } else if (false == strict_ascending) {
2131         // The opts.proto_mode flag is active.
2132         return Error("Protobuf mode doesn't allow implicit enum values.");
2133       }
2134
2135       ECHECK(evb.AcceptEnumerator());
2136
2137       if (opts.proto_mode && Is('[')) {
2138         NEXT();
2139         // ignore attributes on enums.
2140         while (token_ != ']') NEXT();
2141         NEXT();
2142       }
2143     }
2144     if (!Is(opts.proto_mode ? ';' : ',')) break;
2145     NEXT();
2146   }
2147   EXPECT('}');
2148
2149   // At this point, the enum can be empty if input is invalid proto-file.
2150   if (!enum_def->size())
2151     return Error("incomplete enum declaration, values not found");
2152
2153   if (enum_def->attributes.Lookup("bit_flags")) {
2154     const auto base_width = static_cast<uint64_t>(8 * SizeOf(underlying_type));
2155     for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
2156          ++it) {
2157       auto ev = *it;
2158       const auto u = ev->GetAsUInt64();
2159       // Stop manipulations with the sign.
2160       if (!IsUnsigned(underlying_type) && u == (base_width - 1))
2161         return Error("underlying type of bit_flags enum must be unsigned");
2162       if (u >= base_width)
2163         return Error("bit flag out of range of underlying integral type");
2164       enum_def->ChangeEnumValue(ev, 1ULL << u);
2165     }
2166   }
2167
2168   if (false == strict_ascending)
2169     enum_def->SortByValue();  // Must be sorted to use MinValue/MaxValue.
2170
2171   if (dest) *dest = enum_def;
2172   types_.Add(current_namespace_->GetFullyQualifiedName(enum_def->name),
2173              new Type(BASE_TYPE_UNION, nullptr, enum_def));
2174   return NoError();
2175 }
2176
2177 CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) {
2178   auto &struct_def = *LookupCreateStruct(name, true, true);
2179   if (!struct_def.predecl) return Error("datatype already exists: " + name);
2180   struct_def.predecl = false;
2181   struct_def.name = name;
2182   struct_def.file = file_being_parsed_;
2183   // Move this struct to the back of the vector just in case it was predeclared,
2184   // to preserve declaration order.
2185   *std::remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) =
2186       &struct_def;
2187   *dest = &struct_def;
2188   return NoError();
2189 }
2190
2191 CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
2192                                 StructDef *struct_def, const char *suffix,
2193                                 BaseType basetype) {
2194   auto len = strlen(suffix);
2195   for (auto it = fields.begin(); it != fields.end(); ++it) {
2196     auto &fname = (*it)->name;
2197     if (fname.length() > len &&
2198         fname.compare(fname.length() - len, len, suffix) == 0 &&
2199         (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
2200       auto field =
2201           struct_def->fields.Lookup(fname.substr(0, fname.length() - len));
2202       if (field && field->value.type.base_type == basetype)
2203         return Error("Field " + fname +
2204                      " would clash with generated functions for field " +
2205                      field->name);
2206     }
2207   }
2208   return NoError();
2209 }
2210
2211 bool Parser::SupportsAdvancedUnionFeatures() const {
2212   return opts.lang_to_generate != 0 &&
2213          (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kJs |
2214                                     IDLOptions::kTs | IDLOptions::kPhp |
2215                                     IDLOptions::kJava | IDLOptions::kCSharp |
2216                                     IDLOptions::kKotlin |
2217                                     IDLOptions::kBinary)) == 0;
2218 }
2219
2220 bool Parser::SupportsAdvancedArrayFeatures() const {
2221   return (opts.lang_to_generate &
2222           ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
2223             IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
2224             IDLOptions::kBinary)) == 0;
2225 }
2226
2227 Namespace *Parser::UniqueNamespace(Namespace *ns) {
2228   for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
2229     if (ns->components == (*it)->components) {
2230       delete ns;
2231       return *it;
2232     }
2233   }
2234   namespaces_.push_back(ns);
2235   return ns;
2236 }
2237
2238 std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
2239   Namespace *ns = new Namespace();
2240
2241   std::size_t current, previous = 0;
2242   current = full_qualified_name.find('.');
2243   while (current != std::string::npos) {
2244     ns->components.push_back(
2245         full_qualified_name.substr(previous, current - previous));
2246     previous = current + 1;
2247     current = full_qualified_name.find('.', previous);
2248   }
2249   current_namespace_ = UniqueNamespace(ns);
2250   return full_qualified_name.substr(previous, current - previous);
2251 }
2252
2253 static bool compareFieldDefs(const FieldDef *a, const FieldDef *b) {
2254   auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
2255   auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
2256   return a_id < b_id;
2257 }
2258
2259 CheckedError Parser::ParseDecl() {
2260   std::vector<std::string> dc = doc_comment_;
2261   bool fixed = IsIdent("struct");
2262   if (!fixed && !IsIdent("table")) return Error("declaration expected");
2263   NEXT();
2264   std::string name = attribute_;
2265   EXPECT(kTokenIdentifier);
2266   StructDef *struct_def;
2267   ECHECK(StartStruct(name, &struct_def));
2268   struct_def->doc_comment = dc;
2269   struct_def->fixed = fixed;
2270   ECHECK(ParseMetaData(&struct_def->attributes));
2271   struct_def->sortbysize =
2272       struct_def->attributes.Lookup("original_order") == nullptr && !fixed;
2273   EXPECT('{');
2274   while (token_ != '}') ECHECK(ParseField(*struct_def));
2275   auto force_align = struct_def->attributes.Lookup("force_align");
2276   if (fixed) {
2277     if (force_align) {
2278       auto align = static_cast<size_t>(atoi(force_align->constant.c_str()));
2279       if (force_align->type.base_type != BASE_TYPE_INT ||
2280           align < struct_def->minalign || align > FLATBUFFERS_MAX_ALIGNMENT ||
2281           align & (align - 1))
2282         return Error(
2283             "force_align must be a power of two integer ranging from the"
2284             "struct\'s natural alignment to " +
2285             NumToString(FLATBUFFERS_MAX_ALIGNMENT));
2286       struct_def->minalign = align;
2287     }
2288     if (!struct_def->bytesize) return Error("size 0 structs not allowed");
2289   }
2290   struct_def->PadLastField(struct_def->minalign);
2291   // Check if this is a table that has manual id assignments
2292   auto &fields = struct_def->fields.vec;
2293   if (!fixed && fields.size()) {
2294     size_t num_id_fields = 0;
2295     for (auto it = fields.begin(); it != fields.end(); ++it) {
2296       if ((*it)->attributes.Lookup("id")) num_id_fields++;
2297     }
2298     // If any fields have ids..
2299     if (num_id_fields) {
2300       // Then all fields must have them.
2301       if (num_id_fields != fields.size())
2302         return Error(
2303             "either all fields or no fields must have an 'id' attribute");
2304       // Simply sort by id, then the fields are the same as if no ids had
2305       // been specified.
2306       std::sort(fields.begin(), fields.end(), compareFieldDefs);
2307       // Verify we have a contiguous set, and reassign vtable offsets.
2308       for (int i = 0; i < static_cast<int>(fields.size()); i++) {
2309         if (i != atoi(fields[i]->attributes.Lookup("id")->constant.c_str()))
2310           return Error("field id\'s must be consecutive from 0, id " +
2311                        NumToString(i) + " missing or set twice");
2312         fields[i]->value.offset = FieldIndexToOffset(static_cast<voffset_t>(i));
2313       }
2314     }
2315   }
2316
2317   ECHECK(
2318       CheckClash(fields, struct_def, UnionTypeFieldSuffix(), BASE_TYPE_UNION));
2319   ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION));
2320   ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR));
2321   ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR));
2322   ECHECK(CheckClash(fields, struct_def, "_byte_vector", BASE_TYPE_STRING));
2323   ECHECK(CheckClash(fields, struct_def, "ByteVector", BASE_TYPE_STRING));
2324   EXPECT('}');
2325   types_.Add(current_namespace_->GetFullyQualifiedName(struct_def->name),
2326              new Type(BASE_TYPE_STRUCT, struct_def, nullptr));
2327   return NoError();
2328 }
2329
2330 CheckedError Parser::ParseService() {
2331   std::vector<std::string> service_comment = doc_comment_;
2332   NEXT();
2333   auto service_name = attribute_;
2334   EXPECT(kTokenIdentifier);
2335   auto &service_def = *new ServiceDef();
2336   service_def.name = service_name;
2337   service_def.file = file_being_parsed_;
2338   service_def.doc_comment = service_comment;
2339   service_def.defined_namespace = current_namespace_;
2340   if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
2341                     &service_def))
2342     return Error("service already exists: " + service_name);
2343   ECHECK(ParseMetaData(&service_def.attributes));
2344   EXPECT('{');
2345   do {
2346     std::vector<std::string> doc_comment = doc_comment_;
2347     auto rpc_name = attribute_;
2348     EXPECT(kTokenIdentifier);
2349     EXPECT('(');
2350     Type reqtype, resptype;
2351     ECHECK(ParseTypeIdent(reqtype));
2352     EXPECT(')');
2353     EXPECT(':');
2354     ECHECK(ParseTypeIdent(resptype));
2355     if (reqtype.base_type != BASE_TYPE_STRUCT || reqtype.struct_def->fixed ||
2356         resptype.base_type != BASE_TYPE_STRUCT || resptype.struct_def->fixed)
2357       return Error("rpc request and response types must be tables");
2358     auto &rpc = *new RPCCall();
2359     rpc.name = rpc_name;
2360     rpc.request = reqtype.struct_def;
2361     rpc.response = resptype.struct_def;
2362     rpc.doc_comment = doc_comment;
2363     if (service_def.calls.Add(rpc_name, &rpc))
2364       return Error("rpc already exists: " + rpc_name);
2365     ECHECK(ParseMetaData(&rpc.attributes));
2366     EXPECT(';');
2367   } while (token_ != '}');
2368   NEXT();
2369   return NoError();
2370 }
2371
2372 bool Parser::SetRootType(const char *name) {
2373   root_struct_def_ = LookupStruct(name);
2374   if (!root_struct_def_)
2375     root_struct_def_ =
2376         LookupStruct(current_namespace_->GetFullyQualifiedName(name));
2377   return root_struct_def_ != nullptr;
2378 }
2379
2380 void Parser::MarkGenerated() {
2381   // This function marks all existing definitions as having already
2382   // been generated, which signals no code for included files should be
2383   // generated.
2384   for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2385     (*it)->generated = true;
2386   }
2387   for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
2388     if (!(*it)->predecl) { (*it)->generated = true; }
2389   }
2390   for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
2391     (*it)->generated = true;
2392   }
2393 }
2394
2395 CheckedError Parser::ParseNamespace() {
2396   NEXT();
2397   auto ns = new Namespace();
2398   namespaces_.push_back(ns);  // Store it here to not leak upon error.
2399   if (token_ != ';') {
2400     for (;;) {
2401       ns->components.push_back(attribute_);
2402       EXPECT(kTokenIdentifier);
2403       if (Is('.')) NEXT() else break;
2404     }
2405   }
2406   namespaces_.pop_back();
2407   current_namespace_ = UniqueNamespace(ns);
2408   EXPECT(';');
2409   return NoError();
2410 }
2411
2412 // Best effort parsing of .proto declarations, with the aim to turn them
2413 // in the closest corresponding FlatBuffer equivalent.
2414 // We parse everything as identifiers instead of keywords, since we don't
2415 // want protobuf keywords to become invalid identifiers in FlatBuffers.
2416 CheckedError Parser::ParseProtoDecl() {
2417   bool isextend = IsIdent("extend");
2418   if (IsIdent("package")) {
2419     // These are identical in syntax to FlatBuffer's namespace decl.
2420     ECHECK(ParseNamespace());
2421   } else if (IsIdent("message") || isextend) {
2422     std::vector<std::string> struct_comment = doc_comment_;
2423     NEXT();
2424     StructDef *struct_def = nullptr;
2425     Namespace *parent_namespace = nullptr;
2426     if (isextend) {
2427       if (Is('.')) NEXT();  // qualified names may start with a . ?
2428       auto id = attribute_;
2429       EXPECT(kTokenIdentifier);
2430       ECHECK(ParseNamespacing(&id, nullptr));
2431       struct_def = LookupCreateStruct(id, false);
2432       if (!struct_def)
2433         return Error("cannot extend unknown message type: " + id);
2434     } else {
2435       std::string name = attribute_;
2436       EXPECT(kTokenIdentifier);
2437       ECHECK(StartStruct(name, &struct_def));
2438       // Since message definitions can be nested, we create a new namespace.
2439       auto ns = new Namespace();
2440       // Copy of current namespace.
2441       *ns = *current_namespace_;
2442       // But with current message name.
2443       ns->components.push_back(name);
2444       ns->from_table++;
2445       parent_namespace = current_namespace_;
2446       current_namespace_ = UniqueNamespace(ns);
2447     }
2448     struct_def->doc_comment = struct_comment;
2449     ECHECK(ParseProtoFields(struct_def, isextend, false));
2450     if (!isextend) { current_namespace_ = parent_namespace; }
2451     if (Is(';')) NEXT();
2452   } else if (IsIdent("enum")) {
2453     // These are almost the same, just with different terminator:
2454     EnumDef *enum_def;
2455     ECHECK(ParseEnum(false, &enum_def));
2456     if (Is(';')) NEXT();
2457     // Temp: remove any duplicates, as .fbs files can't handle them.
2458     enum_def->RemoveDuplicates();
2459   } else if (IsIdent("syntax")) {  // Skip these.
2460     NEXT();
2461     EXPECT('=');
2462     EXPECT(kTokenStringConstant);
2463     EXPECT(';');
2464   } else if (IsIdent("option")) {  // Skip these.
2465     ECHECK(ParseProtoOption());
2466     EXPECT(';');
2467   } else if (IsIdent("service")) {  // Skip these.
2468     NEXT();
2469     EXPECT(kTokenIdentifier);
2470     ECHECK(ParseProtoCurliesOrIdent());
2471   } else {
2472     return Error("don\'t know how to parse .proto declaration starting with " +
2473                  TokenToStringId(token_));
2474   }
2475   return NoError();
2476 }
2477
2478 CheckedError Parser::StartEnum(const std::string &enum_name, bool is_union,
2479                                EnumDef **dest) {
2480   auto &enum_def = *new EnumDef();
2481   enum_def.name = enum_name;
2482   enum_def.file = file_being_parsed_;
2483   enum_def.doc_comment = doc_comment_;
2484   enum_def.is_union = is_union;
2485   enum_def.defined_namespace = current_namespace_;
2486   if (enums_.Add(current_namespace_->GetFullyQualifiedName(enum_name),
2487                  &enum_def))
2488     return Error("enum already exists: " + enum_name);
2489   enum_def.underlying_type.base_type = is_union ? BASE_TYPE_UTYPE
2490                                                 : BASE_TYPE_INT;
2491   enum_def.underlying_type.enum_def = &enum_def;
2492   if (dest) *dest = &enum_def;
2493   return NoError();
2494 }
2495
2496 CheckedError Parser::ParseProtoFields(StructDef *struct_def, bool isextend,
2497                                       bool inside_oneof) {
2498   EXPECT('{');
2499   while (token_ != '}') {
2500     if (IsIdent("message") || IsIdent("extend") || IsIdent("enum")) {
2501       // Nested declarations.
2502       ECHECK(ParseProtoDecl());
2503     } else if (IsIdent("extensions")) {  // Skip these.
2504       NEXT();
2505       EXPECT(kTokenIntegerConstant);
2506       if (Is(kTokenIdentifier)) {
2507         NEXT();  // to
2508         NEXT();  // num
2509       }
2510       EXPECT(';');
2511     } else if (IsIdent("option")) {  // Skip these.
2512       ECHECK(ParseProtoOption());
2513       EXPECT(';');
2514     } else if (IsIdent("reserved")) {  // Skip these.
2515       NEXT();
2516       while (!Is(';')) { NEXT(); }  // A variety of formats, just skip.
2517       NEXT();
2518     } else {
2519       std::vector<std::string> field_comment = doc_comment_;
2520       // Parse the qualifier.
2521       bool required = false;
2522       bool repeated = false;
2523       bool oneof = false;
2524       if (!inside_oneof) {
2525         if (IsIdent("optional")) {
2526           // This is the default.
2527           NEXT();
2528         } else if (IsIdent("required")) {
2529           required = true;
2530           NEXT();
2531         } else if (IsIdent("repeated")) {
2532           repeated = true;
2533           NEXT();
2534         } else if (IsIdent("oneof")) {
2535           oneof = true;
2536           NEXT();
2537         } else {
2538           // can't error, proto3 allows decls without any of the above.
2539         }
2540       }
2541       StructDef *anonymous_struct = nullptr;
2542       EnumDef *oneof_union = nullptr;
2543       Type type;
2544       if (IsIdent("group") || oneof) {
2545         if (!oneof) NEXT();
2546         if (oneof && opts.proto_oneof_union) {
2547           auto name = MakeCamel(attribute_, true) + "Union";
2548           ECHECK(StartEnum(name, true, &oneof_union));
2549           type = Type(BASE_TYPE_UNION, nullptr, oneof_union);
2550         } else {
2551           auto name = "Anonymous" + NumToString(anonymous_counter++);
2552           ECHECK(StartStruct(name, &anonymous_struct));
2553           type = Type(BASE_TYPE_STRUCT, anonymous_struct);
2554         }
2555       } else {
2556         ECHECK(ParseTypeFromProtoType(&type));
2557       }
2558       // Repeated elements get mapped to a vector.
2559       if (repeated) {
2560         type.element = type.base_type;
2561         type.base_type = BASE_TYPE_VECTOR;
2562         if (type.element == BASE_TYPE_VECTOR) {
2563           // We have a vector or vectors, which FlatBuffers doesn't support.
2564           // For now make it a vector of string (since the source is likely
2565           // "repeated bytes").
2566           // TODO(wvo): A better solution would be to wrap this in a table.
2567           type.element = BASE_TYPE_STRING;
2568         }
2569       }
2570       std::string name = attribute_;
2571       EXPECT(kTokenIdentifier);
2572       if (!oneof) {
2573         // Parse the field id. Since we're just translating schemas, not
2574         // any kind of binary compatibility, we can safely ignore these, and
2575         // assign our own.
2576         EXPECT('=');
2577         EXPECT(kTokenIntegerConstant);
2578       }
2579       FieldDef *field = nullptr;
2580       if (isextend) {
2581         // We allow a field to be re-defined when extending.
2582         // TODO: are there situations where that is problematic?
2583         field = struct_def->fields.Lookup(name);
2584       }
2585       if (!field) ECHECK(AddField(*struct_def, name, type, &field));
2586       field->doc_comment = field_comment;
2587       if (!IsScalar(type.base_type)) field->required = required;
2588       // See if there's a default specified.
2589       if (Is('[')) {
2590         NEXT();
2591         for (;;) {
2592           auto key = attribute_;
2593           ECHECK(ParseProtoKey());
2594           EXPECT('=');
2595           auto val = attribute_;
2596           ECHECK(ParseProtoCurliesOrIdent());
2597           if (key == "default") {
2598             // Temp: skip non-numeric defaults (enums).
2599             auto numeric = strpbrk(val.c_str(), "0123456789-+.");
2600             if (IsScalar(type.base_type) && numeric == val.c_str())
2601               field->value.constant = val;
2602           } else if (key == "deprecated") {
2603             field->deprecated = val == "true";
2604           }
2605           if (!Is(',')) break;
2606           NEXT();
2607         }
2608         EXPECT(']');
2609       }
2610       if (anonymous_struct) {
2611         ECHECK(ParseProtoFields(anonymous_struct, false, oneof));
2612         if (Is(';')) NEXT();
2613       } else if (oneof_union) {
2614         // Parse into a temporary StructDef, then transfer fields into an
2615         // EnumDef describing the oneof as a union.
2616         StructDef oneof_struct;
2617         ECHECK(ParseProtoFields(&oneof_struct, false, oneof));
2618         if (Is(';')) NEXT();
2619         for (auto field_it = oneof_struct.fields.vec.begin();
2620              field_it != oneof_struct.fields.vec.end(); ++field_it) {
2621           const auto &oneof_field = **field_it;
2622           const auto &oneof_type = oneof_field.value.type;
2623           if (oneof_type.base_type != BASE_TYPE_STRUCT ||
2624               !oneof_type.struct_def || oneof_type.struct_def->fixed)
2625             return Error("oneof '" + name +
2626                 "' cannot be mapped to a union because member '" +
2627                 oneof_field.name + "' is not a table type.");
2628           EnumValBuilder evb(*this, *oneof_union);
2629           auto ev = evb.CreateEnumerator(oneof_type.struct_def->name);
2630           ev->union_type = oneof_type;
2631           ev->doc_comment = oneof_field.doc_comment;
2632           ECHECK(evb.AcceptEnumerator(oneof_field.name));
2633         }
2634       } else {
2635         EXPECT(';');
2636       }
2637     }
2638   }
2639   NEXT();
2640   return NoError();
2641 }
2642
2643 CheckedError Parser::ParseProtoKey() {
2644   if (token_ == '(') {
2645     NEXT();
2646     // Skip "(a.b)" style custom attributes.
2647     while (token_ == '.' || token_ == kTokenIdentifier) NEXT();
2648     EXPECT(')');
2649     while (Is('.')) {
2650       NEXT();
2651       EXPECT(kTokenIdentifier);
2652     }
2653   } else {
2654     EXPECT(kTokenIdentifier);
2655   }
2656   return NoError();
2657 }
2658
2659 CheckedError Parser::ParseProtoCurliesOrIdent() {
2660   if (Is('{')) {
2661     NEXT();
2662     for (int nesting = 1; nesting;) {
2663       if (token_ == '{')
2664         nesting++;
2665       else if (token_ == '}')
2666         nesting--;
2667       NEXT();
2668     }
2669   } else {
2670     NEXT();  // Any single token.
2671   }
2672   return NoError();
2673 }
2674
2675 CheckedError Parser::ParseProtoOption() {
2676   NEXT();
2677   ECHECK(ParseProtoKey());
2678   EXPECT('=');
2679   ECHECK(ParseProtoCurliesOrIdent());
2680   return NoError();
2681 }
2682
2683 // Parse a protobuf type, and map it to the corresponding FlatBuffer one.
2684 CheckedError Parser::ParseTypeFromProtoType(Type *type) {
2685   struct type_lookup {
2686     const char *proto_type;
2687     BaseType fb_type, element;
2688   };
2689   static type_lookup lookup[] = {
2690     { "float", BASE_TYPE_FLOAT, BASE_TYPE_NONE },
2691     { "double", BASE_TYPE_DOUBLE, BASE_TYPE_NONE },
2692     { "int32", BASE_TYPE_INT, BASE_TYPE_NONE },
2693     { "int64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2694     { "uint32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2695     { "uint64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2696     { "sint32", BASE_TYPE_INT, BASE_TYPE_NONE },
2697     { "sint64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2698     { "fixed32", BASE_TYPE_UINT, BASE_TYPE_NONE },
2699     { "fixed64", BASE_TYPE_ULONG, BASE_TYPE_NONE },
2700     { "sfixed32", BASE_TYPE_INT, BASE_TYPE_NONE },
2701     { "sfixed64", BASE_TYPE_LONG, BASE_TYPE_NONE },
2702     { "bool", BASE_TYPE_BOOL, BASE_TYPE_NONE },
2703     { "string", BASE_TYPE_STRING, BASE_TYPE_NONE },
2704     { "bytes", BASE_TYPE_VECTOR, BASE_TYPE_UCHAR },
2705     { nullptr, BASE_TYPE_NONE, BASE_TYPE_NONE }
2706   };
2707   for (auto tl = lookup; tl->proto_type; tl++) {
2708     if (attribute_ == tl->proto_type) {
2709       type->base_type = tl->fb_type;
2710       type->element = tl->element;
2711       NEXT();
2712       return NoError();
2713     }
2714   }
2715   if (Is('.')) NEXT();  // qualified names may start with a . ?
2716   ECHECK(ParseTypeIdent(*type));
2717   return NoError();
2718 }
2719
2720 CheckedError Parser::SkipAnyJsonValue() {
2721   switch (token_) {
2722     case '{': {
2723       size_t fieldn_outer = 0;
2724       return ParseTableDelimiters(
2725           fieldn_outer, nullptr,
2726           [&](const std::string &, size_t &fieldn,
2727               const StructDef *) -> CheckedError {
2728             ECHECK(Recurse([&]() { return SkipAnyJsonValue(); }));
2729             fieldn++;
2730             return NoError();
2731           });
2732     }
2733     case '[': {
2734       uoffset_t count = 0;
2735       return ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2736         return Recurse([&]() { return SkipAnyJsonValue(); });
2737       });
2738     }
2739     case kTokenStringConstant:
2740     case kTokenIntegerConstant:
2741     case kTokenFloatConstant: NEXT(); break;
2742     default:
2743       if (IsIdent("true") || IsIdent("false") || IsIdent("null")) {
2744         NEXT();
2745       } else
2746         return TokenError();
2747   }
2748   return NoError();
2749 }
2750
2751 CheckedError Parser::ParseFlexBufferValue(flexbuffers::Builder *builder) {
2752   switch (token_) {
2753     case '{': {
2754       auto start = builder->StartMap();
2755       size_t fieldn_outer = 0;
2756       auto err =
2757           ParseTableDelimiters(fieldn_outer, nullptr,
2758                                [&](const std::string &name, size_t &fieldn,
2759                                    const StructDef *) -> CheckedError {
2760                                  builder->Key(name);
2761                                  ECHECK(ParseFlexBufferValue(builder));
2762                                  fieldn++;
2763                                  return NoError();
2764                                });
2765       ECHECK(err);
2766       builder->EndMap(start);
2767       break;
2768     }
2769     case '[': {
2770       auto start = builder->StartVector();
2771       uoffset_t count = 0;
2772       ECHECK(ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
2773         return ParseFlexBufferValue(builder);
2774       }));
2775       builder->EndVector(start, false, false);
2776       break;
2777     }
2778     case kTokenStringConstant:
2779       builder->String(attribute_);
2780       EXPECT(kTokenStringConstant);
2781       break;
2782     case kTokenIntegerConstant:
2783       builder->Int(StringToInt(attribute_.c_str()));
2784       EXPECT(kTokenIntegerConstant);
2785       break;
2786     case kTokenFloatConstant:
2787       builder->Double(strtod(attribute_.c_str(), nullptr));
2788       EXPECT(kTokenFloatConstant);
2789       break;
2790     default:
2791       if (IsIdent("true")) {
2792         builder->Bool(true);
2793         NEXT();
2794       } else if (IsIdent("false")) {
2795         builder->Bool(false);
2796         NEXT();
2797       } else if (IsIdent("null")) {
2798         builder->Null();
2799         NEXT();
2800       } else
2801         return TokenError();
2802   }
2803   return NoError();
2804 }
2805
2806 bool Parser::ParseFlexBuffer(const char *source, const char *source_filename,
2807                              flexbuffers::Builder *builder) {
2808   auto ok = !StartParseFile(source, source_filename).Check() &&
2809             !ParseFlexBufferValue(builder).Check();
2810   if (ok) builder->Finish();
2811   return ok;
2812 }
2813
2814 bool Parser::Parse(const char *source, const char **include_paths,
2815                    const char *source_filename) {
2816   FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2817   bool r;
2818
2819   if (opts.use_flexbuffers) {
2820     r = ParseFlexBuffer(source, source_filename, &flex_builder_);
2821   } else {
2822     r = !ParseRoot(source, include_paths, source_filename).Check();
2823   }
2824   FLATBUFFERS_ASSERT(0 == recurse_protection_counter);
2825   return r;
2826 }
2827
2828 CheckedError Parser::StartParseFile(const char *source,
2829                                     const char *source_filename) {
2830   file_being_parsed_ = source_filename ? source_filename : "";
2831   source_ = source;
2832   ResetState(source_);
2833   error_.clear();
2834   ECHECK(SkipByteOrderMark());
2835   NEXT();
2836   if (Is(kTokenEof)) return Error("input file is empty");
2837   return NoError();
2838 }
2839
2840 CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
2841                                const char *source_filename) {
2842   ECHECK(DoParse(source, include_paths, source_filename, nullptr));
2843
2844   // Check that all types were defined.
2845   for (auto it = structs_.vec.begin(); it != structs_.vec.end();) {
2846     auto &struct_def = **it;
2847     if (struct_def.predecl) {
2848       if (opts.proto_mode) {
2849         // Protos allow enums to be used before declaration, so check if that
2850         // is the case here.
2851         EnumDef *enum_def = nullptr;
2852         for (size_t components =
2853                  struct_def.defined_namespace->components.size() + 1;
2854              components && !enum_def; components--) {
2855           auto qualified_name =
2856               struct_def.defined_namespace->GetFullyQualifiedName(
2857                   struct_def.name, components - 1);
2858           enum_def = LookupEnum(qualified_name);
2859         }
2860         if (enum_def) {
2861           // This is pretty slow, but a simple solution for now.
2862           auto initial_count = struct_def.refcount;
2863           for (auto struct_it = structs_.vec.begin();
2864                struct_it != structs_.vec.end(); ++struct_it) {
2865             auto &sd = **struct_it;
2866             for (auto field_it = sd.fields.vec.begin();
2867                  field_it != sd.fields.vec.end(); ++field_it) {
2868               auto &field = **field_it;
2869               if (field.value.type.struct_def == &struct_def) {
2870                 field.value.type.struct_def = nullptr;
2871                 field.value.type.enum_def = enum_def;
2872                 auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
2873                                ? field.value.type.element
2874                                : field.value.type.base_type;
2875                 FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
2876                 bt = enum_def->underlying_type.base_type;
2877                 struct_def.refcount--;
2878                 enum_def->refcount++;
2879               }
2880             }
2881           }
2882           if (struct_def.refcount)
2883             return Error("internal: " + NumToString(struct_def.refcount) + "/" +
2884                          NumToString(initial_count) +
2885                          " use(s) of pre-declaration enum not accounted for: " +
2886                          enum_def->name);
2887           structs_.dict.erase(structs_.dict.find(struct_def.name));
2888           it = structs_.vec.erase(it);
2889           delete &struct_def;
2890           continue;  // Skip error.
2891         }
2892       }
2893       auto err = "type referenced but not defined (check namespace): " +
2894                  struct_def.name;
2895       if (struct_def.original_location)
2896         err += ", originally at: " + *struct_def.original_location;
2897       return Error(err);
2898     }
2899     ++it;
2900   }
2901
2902   // This check has to happen here and not earlier, because only now do we
2903   // know for sure what the type of these are.
2904   for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
2905     auto &enum_def = **it;
2906     if (enum_def.is_union) {
2907       for (auto val_it = enum_def.Vals().begin();
2908            val_it != enum_def.Vals().end(); ++val_it) {
2909         auto &val = **val_it;
2910         if (!SupportsAdvancedUnionFeatures() && val.union_type.struct_def &&
2911             val.union_type.struct_def->fixed)
2912           return Error(
2913               "only tables can be union elements in the generated language: " +
2914               val.name);
2915       }
2916     }
2917   }
2918   return NoError();
2919 }
2920
2921 CheckedError Parser::DoParse(const char *source, const char **include_paths,
2922                              const char *source_filename,
2923                              const char *include_filename) {
2924   if (source_filename) {
2925     if (included_files_.find(source_filename) == included_files_.end()) {
2926       included_files_[source_filename] =
2927           include_filename ? include_filename : "";
2928       files_included_per_file_[source_filename] = std::set<std::string>();
2929     } else {
2930       return NoError();
2931     }
2932   }
2933   if (!include_paths) {
2934     static const char *current_directory[] = { "", nullptr };
2935     include_paths = current_directory;
2936   }
2937   field_stack_.clear();
2938   builder_.Clear();
2939   // Start with a blank namespace just in case this file doesn't have one.
2940   current_namespace_ = empty_namespace_;
2941
2942   ECHECK(StartParseFile(source, source_filename));
2943
2944   // Includes must come before type declarations:
2945   for (;;) {
2946     // Parse pre-include proto statements if any:
2947     if (opts.proto_mode && (attribute_ == "option" || attribute_ == "syntax" ||
2948                             attribute_ == "package")) {
2949       ECHECK(ParseProtoDecl());
2950     } else if (IsIdent("native_include")) {
2951       NEXT();
2952       vector_emplace_back(&native_included_files_, attribute_);
2953       EXPECT(kTokenStringConstant);
2954       EXPECT(';');
2955     } else if (IsIdent("include") || (opts.proto_mode && IsIdent("import"))) {
2956       NEXT();
2957       if (opts.proto_mode && attribute_ == "public") NEXT();
2958       auto name = flatbuffers::PosixPath(attribute_.c_str());
2959       EXPECT(kTokenStringConstant);
2960       // Look for the file in include_paths.
2961       std::string filepath;
2962       for (auto paths = include_paths; paths && *paths; paths++) {
2963         filepath = flatbuffers::ConCatPathFileName(*paths, name);
2964         if (FileExists(filepath.c_str())) break;
2965       }
2966       if (filepath.empty())
2967         return Error("unable to locate include file: " + name);
2968       if (source_filename)
2969         files_included_per_file_[source_filename].insert(filepath);
2970       if (included_files_.find(filepath) == included_files_.end()) {
2971         // We found an include file that we have not parsed yet.
2972         // Load it and parse it.
2973         std::string contents;
2974         if (!LoadFile(filepath.c_str(), true, &contents))
2975           return Error("unable to load include file: " + name);
2976         ECHECK(DoParse(contents.c_str(), include_paths, filepath.c_str(),
2977                        name.c_str()));
2978         // We generally do not want to output code for any included files:
2979         if (!opts.generate_all) MarkGenerated();
2980         // Reset these just in case the included file had them, and the
2981         // parent doesn't.
2982         root_struct_def_ = nullptr;
2983         file_identifier_.clear();
2984         file_extension_.clear();
2985         // This is the easiest way to continue this file after an include:
2986         // instead of saving and restoring all the state, we simply start the
2987         // file anew. This will cause it to encounter the same include
2988         // statement again, but this time it will skip it, because it was
2989         // entered into included_files_.
2990         // This is recursive, but only go as deep as the number of include
2991         // statements.
2992         if (source_filename) {
2993           included_files_.erase(source_filename);
2994         }
2995         return DoParse(source, include_paths, source_filename,
2996                        include_filename);
2997       }
2998       EXPECT(';');
2999     } else {
3000       break;
3001     }
3002   }
3003   // Now parse all other kinds of declarations:
3004   while (token_ != kTokenEof) {
3005     if (opts.proto_mode) {
3006       ECHECK(ParseProtoDecl());
3007     } else if (IsIdent("namespace")) {
3008       ECHECK(ParseNamespace());
3009     } else if (token_ == '{') {
3010       if (!root_struct_def_)
3011         return Error("no root type set to parse json with");
3012       if (builder_.GetSize()) {
3013         return Error("cannot have more than one json object in a file");
3014       }
3015       uoffset_t toff;
3016       ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
3017       if (opts.size_prefixed) {
3018         builder_.FinishSizePrefixed(Offset<Table>(toff), file_identifier_.length()
3019                                                              ? file_identifier_.c_str()
3020                                                              : nullptr);
3021       } else {
3022         builder_.Finish(Offset<Table>(toff), file_identifier_.length()
3023                                                  ? file_identifier_.c_str()
3024                                                  : nullptr);
3025       }
3026       // Check that JSON file doesn't contain more objects or IDL directives.
3027       // Comments after JSON are allowed.
3028       EXPECT(kTokenEof);
3029     } else if (IsIdent("enum")) {
3030       ECHECK(ParseEnum(false, nullptr));
3031     } else if (IsIdent("union")) {
3032       ECHECK(ParseEnum(true, nullptr));
3033     } else if (IsIdent("root_type")) {
3034       NEXT();
3035       auto root_type = attribute_;
3036       EXPECT(kTokenIdentifier);
3037       ECHECK(ParseNamespacing(&root_type, nullptr));
3038       if (opts.root_type.empty()) {
3039         if (!SetRootType(root_type.c_str()))
3040           return Error("unknown root type: " + root_type);
3041         if (root_struct_def_->fixed)
3042           return Error("root type must be a table");
3043       }
3044       EXPECT(';');
3045     } else if (IsIdent("file_identifier")) {
3046       NEXT();
3047       file_identifier_ = attribute_;
3048       EXPECT(kTokenStringConstant);
3049       if (file_identifier_.length() != FlatBufferBuilder::kFileIdentifierLength)
3050         return Error("file_identifier must be exactly " +
3051                      NumToString(FlatBufferBuilder::kFileIdentifierLength) +
3052                      " characters");
3053       EXPECT(';');
3054     } else if (IsIdent("file_extension")) {
3055       NEXT();
3056       file_extension_ = attribute_;
3057       EXPECT(kTokenStringConstant);
3058       EXPECT(';');
3059     } else if (IsIdent("include")) {
3060       return Error("includes must come before declarations");
3061     } else if (IsIdent("attribute")) {
3062       NEXT();
3063       auto name = attribute_;
3064       if (Is(kTokenIdentifier)) {
3065         NEXT();
3066       } else {
3067         EXPECT(kTokenStringConstant);
3068       }
3069       EXPECT(';');
3070       known_attributes_[name] = false;
3071     } else if (IsIdent("rpc_service")) {
3072       ECHECK(ParseService());
3073     } else {
3074       ECHECK(ParseDecl());
3075     }
3076   }
3077   return NoError();
3078 }
3079
3080 std::set<std::string> Parser::GetIncludedFilesRecursive(
3081     const std::string &file_name) const {
3082   std::set<std::string> included_files;
3083   std::list<std::string> to_process;
3084
3085   if (file_name.empty()) return included_files;
3086   to_process.push_back(file_name);
3087
3088   while (!to_process.empty()) {
3089     std::string current = to_process.front();
3090     to_process.pop_front();
3091     included_files.insert(current);
3092
3093     // Workaround the lack of const accessor in C++98 maps.
3094     auto &new_files =
3095         (*const_cast<std::map<std::string, std::set<std::string>> *>(
3096             &files_included_per_file_))[current];
3097     for (auto it = new_files.begin(); it != new_files.end(); ++it) {
3098       if (included_files.find(*it) == included_files.end())
3099         to_process.push_back(*it);
3100     }
3101   }
3102
3103   return included_files;
3104 }
3105
3106 // Schema serialization functionality:
3107
3108 template<typename T> bool compareName(const T *a, const T *b) {
3109   return a->defined_namespace->GetFullyQualifiedName(a->name) <
3110          b->defined_namespace->GetFullyQualifiedName(b->name);
3111 }
3112
3113 template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
3114   // Pre-sort these vectors, such that we can set the correct indices for them.
3115   auto vec = defvec;
3116   std::sort(vec.begin(), vec.end(), compareName<T>);
3117   for (int i = 0; i < static_cast<int>(vec.size()); i++) vec[i]->index = i;
3118 }
3119
3120 void Parser::Serialize() {
3121   builder_.Clear();
3122   AssignIndices(structs_.vec);
3123   AssignIndices(enums_.vec);
3124   std::vector<Offset<reflection::Object>> object_offsets;
3125   for (auto it = structs_.vec.begin(); it != structs_.vec.end(); ++it) {
3126     auto offset = (*it)->Serialize(&builder_, *this);
3127     object_offsets.push_back(offset);
3128     (*it)->serialized_location = offset.o;
3129   }
3130   std::vector<Offset<reflection::Enum>> enum_offsets;
3131   for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
3132     auto offset = (*it)->Serialize(&builder_, *this);
3133     enum_offsets.push_back(offset);
3134     (*it)->serialized_location = offset.o;
3135   }
3136   std::vector<Offset<reflection::Service>> service_offsets;
3137   for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) {
3138     auto offset = (*it)->Serialize(&builder_, *this);
3139     service_offsets.push_back(offset);
3140     (*it)->serialized_location = offset.o;
3141   }
3142   auto objs__ = builder_.CreateVectorOfSortedTables(&object_offsets);
3143   auto enum__ = builder_.CreateVectorOfSortedTables(&enum_offsets);
3144   auto fiid__ = builder_.CreateString(file_identifier_);
3145   auto fext__ = builder_.CreateString(file_extension_);
3146   auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
3147   auto schema_offset =
3148       reflection::CreateSchema(builder_, objs__, enum__, fiid__, fext__,
3149         (root_struct_def_ ? root_struct_def_->serialized_location : 0),
3150         serv__);
3151   if (opts.size_prefixed) {
3152     builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
3153   } else {
3154     builder_.Finish(schema_offset, reflection::SchemaIdentifier());
3155   }
3156 }
3157
3158 static Namespace *GetNamespace(
3159     const std::string &qualified_name, std::vector<Namespace *> &namespaces,
3160     std::map<std::string, Namespace *> &namespaces_index) {
3161   size_t dot = qualified_name.find_last_of('.');
3162   std::string namespace_name = (dot != std::string::npos)
3163                                    ? std::string(qualified_name.c_str(), dot)
3164                                    : "";
3165   Namespace *&ns = namespaces_index[namespace_name];
3166
3167   if (!ns) {
3168     ns = new Namespace();
3169     namespaces.push_back(ns);
3170
3171     size_t pos = 0;
3172
3173     for (;;) {
3174       dot = qualified_name.find('.', pos);
3175       if (dot == std::string::npos) { break; }
3176       ns->components.push_back(qualified_name.substr(pos, dot - pos));
3177       pos = dot + 1;
3178     }
3179   }
3180
3181   return ns;
3182 }
3183
3184 Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
3185                                                 const Parser &parser) const {
3186   std::vector<Offset<reflection::Field>> field_offsets;
3187   for (auto it = fields.vec.begin(); it != fields.vec.end(); ++it) {
3188     field_offsets.push_back((*it)->Serialize(
3189         builder, static_cast<uint16_t>(it - fields.vec.begin()), parser));
3190   }
3191   auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3192   auto name__ = builder->CreateString(qualified_name);
3193   auto flds__ = builder->CreateVectorOfSortedTables(&field_offsets);
3194   auto attr__ = SerializeAttributes(builder, parser);
3195   auto docs__ = parser.opts.binary_schema_comments
3196                 ? builder->CreateVectorOfStrings(doc_comment)
3197                 : 0;
3198   return reflection::CreateObject(*builder, name__, flds__, fixed,
3199                                   static_cast<int>(minalign),
3200                                   static_cast<int>(bytesize),
3201                                   attr__, docs__);
3202 }
3203
3204 bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
3205   if (!DeserializeAttributes(parser, object->attributes()))
3206     return false;
3207   DeserializeDoc(doc_comment, object->documentation());
3208   name = parser.UnqualifiedName(object->name()->str());
3209   predecl = false;
3210   sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
3211   const auto& of = *(object->fields());
3212   auto indexes = std::vector<uoffset_t>(of.size());
3213   for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
3214   size_t tmp_struct_size = 0;
3215   for (size_t i = 0; i < indexes.size(); i++) {
3216     auto field = of.Get(indexes[i]);
3217     auto field_def = new FieldDef();
3218     if (!field_def->Deserialize(parser, field) ||
3219         fields.Add(field_def->name, field_def)) {
3220       delete field_def;
3221       return false;
3222     }
3223     if (fixed) {
3224       // Recompute padding since that's currently not serialized.
3225       auto size = InlineSize(field_def->value.type);
3226       auto next_field =
3227           i + 1 < indexes.size()
3228           ? of.Get(indexes[i+1])
3229           : nullptr;
3230       tmp_struct_size += size;
3231       field_def->padding =
3232           next_field ? (next_field->offset() - field_def->value.offset) - size
3233                      : PaddingBytes(tmp_struct_size, minalign);
3234       tmp_struct_size += field_def->padding;
3235     }
3236   }
3237   FLATBUFFERS_ASSERT(static_cast<int>(tmp_struct_size) == object->bytesize());
3238   return true;
3239 }
3240
3241 Offset<reflection::Field> FieldDef::Serialize(FlatBufferBuilder *builder,
3242                                               uint16_t id,
3243                                               const Parser &parser) const {
3244   auto name__ = builder->CreateString(name);
3245   auto type__ = value.type.Serialize(builder);
3246   auto attr__ = SerializeAttributes(builder, parser);
3247   auto docs__ = parser.opts.binary_schema_comments
3248                 ? builder->CreateVectorOfStrings(doc_comment)
3249                 : 0;
3250   return reflection::CreateField(*builder, name__, type__, id, value.offset,
3251       // Is uint64>max(int64) tested?
3252       IsInteger(value.type.base_type) ? StringToInt(value.constant.c_str()) : 0,
3253       // result may be platform-dependent if underlying is float (not double)
3254       IsFloat(value.type.base_type) ? strtod(value.constant.c_str(), nullptr)
3255                                     : 0.0,
3256       deprecated, required, key, attr__, docs__);
3257   // TODO: value.constant is almost always "0", we could save quite a bit of
3258   // space by sharing it. Same for common values of value.type.
3259 }
3260
3261 bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) {
3262   name = field->name()->str();
3263   defined_namespace = parser.current_namespace_;
3264   if (!value.type.Deserialize(parser, field->type()))
3265     return false;
3266   value.offset = field->offset();
3267   if (IsInteger(value.type.base_type)) {
3268     value.constant = NumToString(field->default_integer());
3269   } else if (IsFloat(value.type.base_type)) {
3270     value.constant = FloatToString(field->default_real(), 16);
3271     size_t last_zero = value.constant.find_last_not_of('0');
3272     if (last_zero != std::string::npos && last_zero != 0) {
3273       value.constant.erase(last_zero, std::string::npos);
3274     }
3275   }
3276   deprecated = field->deprecated();
3277   required = field->required();
3278   key = field->key();
3279   if (!DeserializeAttributes(parser, field->attributes()))
3280     return false;
3281   // TODO: this should probably be handled by a separate attribute
3282   if (attributes.Lookup("flexbuffer")) {
3283     flexbuffer = true;
3284     parser.uses_flexbuffers_ = true;
3285     if (value.type.base_type != BASE_TYPE_VECTOR ||
3286         value.type.element != BASE_TYPE_UCHAR)
3287       return false;
3288   }
3289   if (auto nested = attributes.Lookup("nested_flatbuffer")) {
3290     auto nested_qualified_name =
3291         parser.current_namespace_->GetFullyQualifiedName(nested->constant);
3292     nested_flatbuffer = parser.LookupStruct(nested_qualified_name);
3293     if (!nested_flatbuffer) return false;
3294   }
3295   DeserializeDoc(doc_comment, field->documentation());
3296   return true;
3297 }
3298
3299 Offset<reflection::RPCCall> RPCCall::Serialize(FlatBufferBuilder *builder,
3300                                                const Parser &parser) const {
3301   auto name__ = builder->CreateString(name);
3302   auto attr__ = SerializeAttributes(builder, parser);
3303   auto docs__ = parser.opts.binary_schema_comments
3304                 ? builder->CreateVectorOfStrings(doc_comment)
3305                 : 0;
3306   return reflection::CreateRPCCall(*builder, name__,
3307                                    request->serialized_location,
3308                                    response->serialized_location,
3309                                    attr__, docs__);
3310 }
3311
3312 bool RPCCall::Deserialize(Parser &parser, const reflection::RPCCall *call) {
3313   name = call->name()->str();
3314   if (!DeserializeAttributes(parser, call->attributes()))
3315     return false;
3316   DeserializeDoc(doc_comment, call->documentation());
3317   request = parser.structs_.Lookup(call->request()->name()->str());
3318   response = parser.structs_.Lookup(call->response()->name()->str());
3319   if (!request || !response) { return false; }
3320   return true;
3321 }
3322
3323 Offset<reflection::Service> ServiceDef::Serialize(FlatBufferBuilder *builder,
3324                                                   const Parser &parser) const {
3325   std::vector<Offset<reflection::RPCCall>> servicecall_offsets;
3326   for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) {
3327     servicecall_offsets.push_back((*it)->Serialize(builder, parser));
3328   }
3329   auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3330   auto name__ = builder->CreateString(qualified_name);
3331   auto call__ = builder->CreateVector(servicecall_offsets);
3332   auto attr__ = SerializeAttributes(builder, parser);
3333   auto docs__ = parser.opts.binary_schema_comments
3334                 ? builder->CreateVectorOfStrings(doc_comment)
3335                 : 0;
3336   return reflection::CreateService(*builder, name__, call__, attr__, docs__);
3337 }
3338
3339 bool ServiceDef::Deserialize(Parser &parser,
3340                              const reflection::Service *service) {
3341   name = parser.UnqualifiedName(service->name()->str());
3342   if (service->calls()) {
3343     for (uoffset_t i = 0; i < service->calls()->size(); ++i) {
3344       auto call = new RPCCall();
3345       if (!call->Deserialize(parser, service->calls()->Get(i)) ||
3346           calls.Add(call->name, call)) {
3347         delete call;
3348         return false;
3349       }
3350     }
3351   }
3352   if (!DeserializeAttributes(parser, service->attributes()))
3353     return false;
3354   DeserializeDoc(doc_comment, service->documentation());
3355   return true;
3356 }
3357
3358 Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
3359                                             const Parser &parser) const {
3360   std::vector<Offset<reflection::EnumVal>> enumval_offsets;
3361   for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
3362     enumval_offsets.push_back((*it)->Serialize(builder, parser));
3363   }
3364   auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
3365   auto name__ = builder->CreateString(qualified_name);
3366   auto vals__ = builder->CreateVector(enumval_offsets);
3367   auto type__ = underlying_type.Serialize(builder);
3368   auto attr__ = SerializeAttributes(builder, parser);
3369   auto docs__ = parser.opts.binary_schema_comments
3370                 ? builder->CreateVectorOfStrings(doc_comment)
3371                 : 0;
3372   return reflection::CreateEnum(*builder, name__, vals__, is_union, type__,
3373                                 attr__, docs__);
3374 }
3375
3376 bool EnumDef::Deserialize(Parser &parser, const reflection::Enum *_enum) {
3377   name = parser.UnqualifiedName(_enum->name()->str());
3378   for (uoffset_t i = 0; i < _enum->values()->size(); ++i) {
3379     auto val = new EnumVal();
3380     if (!val->Deserialize(parser, _enum->values()->Get(i)) ||
3381         vals.Add(val->name, val)) {
3382       delete val;
3383       return false;
3384     }
3385   }
3386   is_union = _enum->is_union();
3387   if (!underlying_type.Deserialize(parser, _enum->underlying_type())) {
3388     return false;
3389   }
3390   if (!DeserializeAttributes(parser, _enum->attributes()))
3391     return false;
3392   DeserializeDoc(doc_comment, _enum->documentation());
3393   return true;
3394 }
3395
3396 Offset<reflection::EnumVal> EnumVal::Serialize(FlatBufferBuilder *builder,
3397                                                const Parser &parser) const {
3398   auto name__ = builder->CreateString(name);
3399   auto type__ = union_type.Serialize(builder);
3400   auto docs__ = parser.opts.binary_schema_comments
3401                 ? builder->CreateVectorOfStrings(doc_comment)
3402                 : 0;
3403   return reflection::CreateEnumVal(*builder, name__, value,
3404       union_type.struct_def ? union_type.struct_def->serialized_location : 0,
3405       type__, docs__);
3406 }
3407
3408 bool EnumVal::Deserialize(const Parser &parser,
3409                           const reflection::EnumVal *val) {
3410   name = val->name()->str();
3411   value = val->value();
3412   if (!union_type.Deserialize(parser, val->union_type()))
3413     return false;
3414   DeserializeDoc(doc_comment, val->documentation());
3415   return true;
3416 }
3417
3418 Offset<reflection::Type> Type::Serialize(FlatBufferBuilder *builder) const {
3419   return reflection::CreateType(
3420       *builder, static_cast<reflection::BaseType>(base_type),
3421       static_cast<reflection::BaseType>(element),
3422       struct_def ? struct_def->index : (enum_def ? enum_def->index : -1),
3423       fixed_length);
3424 }
3425
3426 bool Type::Deserialize(const Parser &parser, const reflection::Type *type) {
3427   if (type == nullptr) return true;
3428   base_type = static_cast<BaseType>(type->base_type());
3429   element = static_cast<BaseType>(type->element());
3430   fixed_length = type->fixed_length();
3431   if (type->index() >= 0) {
3432     bool is_series = type->base_type() == reflection::Vector ||
3433                      type->base_type() == reflection::Array;
3434     if (type->base_type() == reflection::Obj ||
3435         (is_series &&
3436          type->element() == reflection::Obj)) {
3437       if (static_cast<size_t>(type->index()) < parser.structs_.vec.size()) {
3438         struct_def = parser.structs_.vec[type->index()];
3439         struct_def->refcount++;
3440       } else {
3441         return false;
3442       }
3443     } else {
3444       if (static_cast<size_t>(type->index()) < parser.enums_.vec.size()) {
3445         enum_def = parser.enums_.vec[type->index()];
3446       } else {
3447         return false;
3448       }
3449     }
3450   }
3451   return true;
3452 }
3453
3454 flatbuffers::Offset<
3455     flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
3456 Definition::SerializeAttributes(FlatBufferBuilder *builder,
3457                                 const Parser &parser) const {
3458   std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
3459   for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
3460     auto it = parser.known_attributes_.find(kv->first);
3461     FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
3462     if (parser.opts.binary_schema_builtins || !it->second) {
3463       auto key = builder->CreateString(kv->first);
3464       auto val = builder->CreateString(kv->second->constant);
3465       attrs.push_back(reflection::CreateKeyValue(*builder, key, val));
3466     }
3467   }
3468   if (attrs.size()) {
3469     return builder->CreateVectorOfSortedTables(&attrs);
3470   } else {
3471     return 0;
3472   }
3473 }
3474
3475 bool Definition::DeserializeAttributes(
3476     Parser &parser, const Vector<Offset<reflection::KeyValue>> *attrs) {
3477   if (attrs == nullptr)
3478     return true;
3479   for (uoffset_t i = 0; i < attrs->size(); ++i) {
3480     auto kv = attrs->Get(i);
3481     auto value = new Value();
3482     if (kv->value()) { value->constant = kv->value()->str(); }
3483     if (attributes.Add(kv->key()->str(), value)) {
3484       delete value;
3485       return false;
3486     }
3487     parser.known_attributes_[kv->key()->str()];
3488   }
3489   return true;
3490 }
3491
3492 /************************************************************************/
3493 /* DESERIALIZATION                                                      */
3494 /************************************************************************/
3495 bool Parser::Deserialize(const uint8_t *buf, const size_t size) {
3496   flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t *>(buf), size);
3497   bool size_prefixed = false;
3498   if(!reflection::SchemaBufferHasIdentifier(buf)) {
3499     if (!flatbuffers::BufferHasIdentifier(buf, reflection::SchemaIdentifier(),
3500                                           true))
3501       return false;
3502     else
3503       size_prefixed = true;
3504   }
3505   auto verify_fn = size_prefixed ? &reflection::VerifySizePrefixedSchemaBuffer
3506                                  : &reflection::VerifySchemaBuffer;
3507   if (!verify_fn(verifier)) {
3508     return false;
3509   }
3510   auto schema = size_prefixed ? reflection::GetSizePrefixedSchema(buf)
3511                               : reflection::GetSchema(buf);
3512   return Deserialize(schema);
3513 }
3514
3515 bool Parser::Deserialize(const reflection::Schema *schema) {
3516   file_identifier_ = schema->file_ident() ? schema->file_ident()->str() : "";
3517   file_extension_ = schema->file_ext() ? schema->file_ext()->str() : "";
3518   std::map<std::string, Namespace *> namespaces_index;
3519
3520   // Create defs without deserializing so references from fields to structs and
3521   // enums can be resolved.
3522   for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3523        ++it) {
3524     auto struct_def = new StructDef();
3525     struct_def->bytesize = it->bytesize();
3526     struct_def->fixed = it->is_struct();
3527     struct_def->minalign = it->minalign();
3528     if (structs_.Add(it->name()->str(), struct_def)) {
3529       delete struct_def;
3530       return false;
3531     }
3532     auto type = new Type(BASE_TYPE_STRUCT, struct_def, nullptr);
3533     if (types_.Add(it->name()->str(), type)) {
3534       delete type;
3535       return false;
3536     }
3537   }
3538   for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3539     auto enum_def = new EnumDef();
3540     if (enums_.Add(it->name()->str(), enum_def)) {
3541       delete enum_def;
3542       return false;
3543     }
3544     auto type = new Type(BASE_TYPE_UNION, nullptr, enum_def);
3545     if (types_.Add(it->name()->str(), type)) {
3546       delete type;
3547       return false;
3548     }
3549   }
3550
3551   // Now fields can refer to structs and enums by index.
3552   for (auto it = schema->objects()->begin(); it != schema->objects()->end();
3553        ++it) {
3554     std::string qualified_name = it->name()->str();
3555     auto struct_def = structs_.Lookup(qualified_name);
3556     struct_def->defined_namespace =
3557         GetNamespace(qualified_name, namespaces_, namespaces_index);
3558     if (!struct_def->Deserialize(*this, * it)) { return false; }
3559     if (schema->root_table() == *it) { root_struct_def_ = struct_def; }
3560   }
3561   for (auto it = schema->enums()->begin(); it != schema->enums()->end(); ++it) {
3562     std::string qualified_name = it->name()->str();
3563     auto enum_def = enums_.Lookup(qualified_name);
3564     enum_def->defined_namespace =
3565         GetNamespace(qualified_name, namespaces_, namespaces_index);
3566     if (!enum_def->Deserialize(*this, *it)) { return false; }
3567   }
3568
3569   if (schema->services()) {
3570     for (auto it = schema->services()->begin(); it != schema->services()->end();
3571          ++it) {
3572       std::string qualified_name = it->name()->str();
3573       auto service_def = new ServiceDef();
3574       service_def->defined_namespace =
3575           GetNamespace(qualified_name, namespaces_, namespaces_index);
3576       if (!service_def->Deserialize(*this, *it) ||
3577           services_.Add(qualified_name, service_def)) {
3578         delete service_def;
3579         return false;
3580       }
3581     }
3582   }
3583
3584   return true;
3585 }
3586
3587 std::string Parser::ConformTo(const Parser &base) {
3588   for (auto sit = structs_.vec.begin(); sit != structs_.vec.end(); ++sit) {
3589     auto &struct_def = **sit;
3590     auto qualified_name =
3591         struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name);
3592     auto struct_def_base = base.LookupStruct(qualified_name);
3593     if (!struct_def_base) continue;
3594     for (auto fit = struct_def.fields.vec.begin();
3595          fit != struct_def.fields.vec.end(); ++fit) {
3596       auto &field = **fit;
3597       auto field_base = struct_def_base->fields.Lookup(field.name);
3598       if (field_base) {
3599         if (field.value.offset != field_base->value.offset)
3600           return "offsets differ for field: " + field.name;
3601         if (field.value.constant != field_base->value.constant)
3602           return "defaults differ for field: " + field.name;
3603         if (!EqualByName(field.value.type, field_base->value.type))
3604           return "types differ for field: " + field.name;
3605       } else {
3606         // Doesn't have to exist, deleting fields is fine.
3607         // But we should check if there is a field that has the same offset
3608         // but is incompatible (in the case of field renaming).
3609         for (auto fbit = struct_def_base->fields.vec.begin();
3610              fbit != struct_def_base->fields.vec.end(); ++fbit) {
3611           field_base = *fbit;
3612           if (field.value.offset == field_base->value.offset) {
3613             if (!EqualByName(field.value.type, field_base->value.type))
3614               return "field renamed to different type: " + field.name;
3615             break;
3616           }
3617         }
3618       }
3619     }
3620   }
3621   for (auto eit = enums_.vec.begin(); eit != enums_.vec.end(); ++eit) {
3622     auto &enum_def = **eit;
3623     auto qualified_name =
3624         enum_def.defined_namespace->GetFullyQualifiedName(enum_def.name);
3625     auto enum_def_base = base.enums_.Lookup(qualified_name);
3626     if (!enum_def_base) continue;
3627     for (auto evit = enum_def.Vals().begin(); evit != enum_def.Vals().end();
3628          ++evit) {
3629       auto &enum_val = **evit;
3630       auto enum_val_base = enum_def_base->Lookup(enum_val.name);
3631       if (enum_val_base) {
3632         if (enum_val != *enum_val_base)
3633           return "values differ for enum: " + enum_val.name;
3634       }
3635     }
3636   }
3637   return "";
3638 }
3639
3640 }  // namespace flatbuffers