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

source code of flutter_engine/third_party/flatbuffers/src/idl_parser.cpp