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