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