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