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