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