Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Dan Field |
| 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 | // independent from idl_parser, since this code is not needed for most clients |
| 18 | #include <cassert> |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 19 | #include <cmath> |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 20 | |
| 21 | #include "flatbuffers/code_generators.h" |
| 22 | #include "flatbuffers/flatbuffers.h" |
| 23 | #include "flatbuffers/idl.h" |
| 24 | #include "flatbuffers/util.h" |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 25 | #include "idl_namer.h" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 26 | |
| 27 | namespace flatbuffers { |
| 28 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 29 | namespace dart { |
| 30 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | static Namer::Config DartDefaultConfig() { |
| 34 | return { /*types=*/Case::kUpperCamel, |
| 35 | /*constants=*/Case::kScreamingSnake, |
| 36 | /*methods=*/Case::kLowerCamel, |
| 37 | /*functions=*/Case::kUnknown, // unused. |
| 38 | /*fields=*/Case::kLowerCamel, |
| 39 | /*variables=*/Case::kLowerCamel, |
| 40 | /*variants=*/Case::kKeep, |
| 41 | /*enum_variant_seperator=*/".", |
| 42 | /*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase, |
| 43 | /*namespaces=*/Case::kSnake2, |
| 44 | /*namespace_seperator=*/".", |
| 45 | /*object_prefix=*/"", |
| 46 | /*object_suffix=*/"T", |
| 47 | /*keyword_prefix=*/"$", |
| 48 | /*keyword_suffix=*/"", |
| 49 | /*filenames=*/Case::kKeep, |
| 50 | /*directories=*/Case::kKeep, |
| 51 | /*output_path=*/"", |
| 52 | /*filename_suffix=*/"_generated", |
| 53 | /*filename_extension=*/".dart" }; |
| 54 | } |
| 55 | |
| 56 | static std::set<std::string> DartKeywords() { |
| 57 | // see https://www.dartlang.org/guides/language/language-tour#keywords |
| 58 | // yield*, async*, and sync* shouldn't be proble |
| 59 | return { |
| 60 | "abstract", "else", "import", "show", "as", "enum", |
| 61 | "in", "static", "assert", "export", "interface", "super", |
| 62 | "async", "extends", "is", "switch", "await", "extension", |
| 63 | "late", "sync", "break", "external", "library", "this", |
| 64 | "case", "factory", "mixin", "throw", "catch", "false", |
| 65 | "new", "true", "class", "final", "null", "try", |
| 66 | "const", "finally", "on", "typedef", "continue", "for", |
| 67 | "operator", "var", "covariant", "Function", "part", "void", |
| 68 | "default", "get", "required", "while", "deferred", "hide", |
| 69 | "rethrow", "with", "do", "if", "return", "yield", |
| 70 | "dynamic", "implements", "set", |
| 71 | }; |
| 72 | } |
| 73 | } // namespace |
| 74 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 75 | const std::string _kFb = "fb"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 76 | |
| 77 | // Iterate through all definitions we haven't generate code for (enums, structs, |
| 78 | // and tables) and output them to a single file. |
| 79 | class DartGenerator : public BaseGenerator { |
| 80 | public: |
| 81 | typedef std::map<std::string, std::string> namespace_code_map; |
| 82 | |
| 83 | DartGenerator(const Parser &parser, const std::string &path, |
| 84 | const std::string &file_name) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 85 | : BaseGenerator(parser, path, file_name, "", ".", "dart"), |
| 86 | namer_(WithFlagOptions(DartDefaultConfig(), parser.opts, path), |
| 87 | DartKeywords()) {} |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 88 | // Iterate through all definitions we haven't generate code for (enums, |
| 89 | // structs, and tables) and output them to a single file. |
| 90 | bool generate() { |
| 91 | std::string code; |
| 92 | namespace_code_map namespace_code; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 93 | GenerateEnums(namespace_code); |
| 94 | GenerateStructs(namespace_code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 95 | |
| 96 | for (auto kv = namespace_code.begin(); kv != namespace_code.end(); ++kv) { |
| 97 | code.clear(); |
| 98 | code = code + "// " + FlatBuffersGeneratedWarning() + "\n"; |
| 99 | code = code + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 100 | "// ignore_for_file: unused_import, unused_field, unused_element, " |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 101 | "unused_local_variable\n\n"; |
| 102 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 103 | if (!kv->first.empty()) { code += "library " + kv->first + ";\n\n"; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 104 | |
| 105 | code += "import 'dart:typed_data' show Uint8List;\n"; |
| 106 | code += "import 'package:flat_buffers/flat_buffers.dart' as " + _kFb + |
| 107 | ";\n\n"; |
| 108 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 109 | for (auto kv2 = namespace_code.begin(); kv2 != namespace_code.end(); |
| 110 | ++kv2) { |
| 111 | if (kv2->first != kv->first) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 112 | code += "import './" + Filename(kv2->first, /*path=*/false) + |
| 113 | "' as " + ImportAliasName(kv2->first) + ";\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | code += "\n"; |
| 117 | code += kv->second; |
| 118 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 119 | if (!SaveFile(Filename(kv->first).c_str(), code, false)) { return false; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 124 | std::string Filename(const std::string &suffix, bool path = true) const { |
| 125 | return (path ? path_ : "") + |
| 126 | namer_.File(file_name_ + (suffix.empty() ? "" : "_" + suffix)); |
| 127 | } |
| 128 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 129 | private: |
| 130 | static std::string ImportAliasName(const std::string &ns) { |
| 131 | std::string ret; |
| 132 | ret.assign(ns); |
| 133 | size_t pos = ret.find('.'); |
| 134 | while (pos != std::string::npos) { |
| 135 | ret.replace(pos, 1, "_"); |
| 136 | pos = ret.find('.', pos + 1); |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 142 | void GenerateEnums(namespace_code_map &namespace_code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 143 | for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); |
| 144 | ++it) { |
| 145 | auto &enum_def = **it; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 146 | GenEnum(enum_def, namespace_code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 150 | void GenerateStructs(namespace_code_map &namespace_code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 151 | for (auto it = parser_.structs_.vec.begin(); |
| 152 | it != parser_.structs_.vec.end(); ++it) { |
| 153 | auto &struct_def = **it; |
| 154 | GenStruct(struct_def, namespace_code); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Generate a documentation comment, if available. |
| 159 | static void GenDocComment(const std::vector<std::string> &dc, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 160 | const char *indent, std::string &code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 161 | for (auto it = dc.begin(); it != dc.end(); ++it) { |
| 162 | if (indent) code += indent; |
| 163 | code += "/// " + *it + "\n"; |
| 164 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Generate an enum declaration and an enum string lookup table. |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 168 | void GenEnum(EnumDef &enum_def, namespace_code_map &namespace_code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 169 | if (enum_def.generated) return; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 170 | std::string &code = |
| 171 | namespace_code[namer_.Namespace(*enum_def.defined_namespace)]; |
| 172 | GenDocComment(enum_def.doc_comment, "", code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 173 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 174 | const std::string enum_type = |
| 175 | namer_.Type(enum_def) + (enum_def.is_union ? "TypeId" : ""); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 176 | const bool is_bit_flags = |
| 177 | enum_def.attributes.Lookup("bit_flags") != nullptr; |
| 178 | // The flatbuffer schema language allows bit flag enums to potentially have |
| 179 | // a default value of zero, even if it's not a valid enum value... |
| 180 | const bool permit_zero = is_bit_flags; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 181 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 182 | code += "class " + enum_type + " {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 183 | code += " final int value;\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 184 | code += " const " + enum_type + "._(this.value);\n\n"; |
| 185 | code += " factory " + enum_type + ".fromValue(int value) {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 186 | code += " final result = values[value];\n"; |
| 187 | code += " if (result == null) {\n"; |
| 188 | if (permit_zero) { |
| 189 | code += " if (value == 0) {\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 190 | code += " return " + enum_type + "._(0);\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 191 | code += " } else {\n"; |
| 192 | } |
| 193 | code += " throw StateError('Invalid value $value for bit flag enum "; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 194 | code += enum_type + "');\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 195 | if (permit_zero) { code += " }\n"; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 196 | code += " }\n"; |
| 197 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 198 | code += " return result;\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 199 | code += " }\n\n"; |
| 200 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 201 | code += " static " + enum_type + "? _createOrNull(int? value) => \n"; |
| 202 | code += |
| 203 | " value == null ? null : " + enum_type + ".fromValue(value);\n\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 204 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 205 | // this is meaningless for bit_flags |
| 206 | // however, note that unlike "regular" dart enums this enum can still have |
| 207 | // holes. |
| 208 | if (!is_bit_flags) { |
| 209 | code += " static const int minValue = " + |
| 210 | enum_def.ToString(*enum_def.MinValue()) + ";\n"; |
| 211 | code += " static const int maxValue = " + |
| 212 | enum_def.ToString(*enum_def.MaxValue()) + ";\n"; |
| 213 | } |
| 214 | |
| 215 | code += |
| 216 | " static bool containsValue(int value) =>" |
| 217 | " values.containsKey(value);\n\n"; |
| 218 | |
| 219 | for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { |
| 220 | auto &ev = **it; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 221 | const auto enum_var = namer_.Variant(ev); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 222 | |
| 223 | if (!ev.doc_comment.empty()) { |
| 224 | if (it != enum_def.Vals().begin()) { code += '\n'; } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 225 | GenDocComment(ev.doc_comment, " ", code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 226 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 227 | code += " static const " + enum_type + " " + enum_var + " = " + |
| 228 | enum_type + "._(" + enum_def.ToString(ev) + ");\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 231 | code += " static const Map<int, " + enum_type + "> values = {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 232 | for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { |
| 233 | auto &ev = **it; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 234 | const auto enum_var = namer_.Variant(ev); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 235 | if (it != enum_def.Vals().begin()) code += ",\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 236 | code += " " + enum_def.ToString(ev) + ": " + enum_var; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 237 | } |
| 238 | code += "};\n\n"; |
| 239 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 240 | code += " static const " + _kFb + ".Reader<" + enum_type + "> reader = _" + |
| 241 | enum_type + "Reader();\n\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 242 | code += " @override\n"; |
| 243 | code += " String toString() {\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 244 | code += " return '" + enum_type + "{value: $value}';\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 245 | code += " }\n"; |
| 246 | code += "}\n\n"; |
| 247 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 248 | GenEnumReader(enum_def, enum_type, code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 251 | void GenEnumReader(EnumDef &enum_def, const std::string &enum_type, |
| 252 | std::string &code) { |
| 253 | code += "class _" + enum_type + "Reader extends " + _kFb + ".Reader<" + |
| 254 | enum_type + "> {\n"; |
| 255 | code += " const _" + enum_type + "Reader();\n\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 256 | code += " @override\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 257 | code += " int get size => " + EnumSize(enum_def.underlying_type) + ";\n\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 258 | code += " @override\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 259 | code += " " + enum_type + " read(" + _kFb + |
| 260 | ".BufferContext bc, int offset) =>\n"; |
| 261 | code += " " + enum_type + ".fromValue(const " + _kFb + "." + |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 262 | GenType(enum_def.underlying_type) + "Reader().read(bc, offset));\n"; |
| 263 | code += "}\n\n"; |
| 264 | } |
| 265 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 266 | std::string GenType(const Type &type) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 267 | switch (type.base_type) { |
| 268 | case BASE_TYPE_BOOL: return "Bool"; |
| 269 | case BASE_TYPE_CHAR: return "Int8"; |
| 270 | case BASE_TYPE_UTYPE: |
| 271 | case BASE_TYPE_UCHAR: return "Uint8"; |
| 272 | case BASE_TYPE_SHORT: return "Int16"; |
| 273 | case BASE_TYPE_USHORT: return "Uint16"; |
| 274 | case BASE_TYPE_INT: return "Int32"; |
| 275 | case BASE_TYPE_UINT: return "Uint32"; |
| 276 | case BASE_TYPE_LONG: return "Int64"; |
| 277 | case BASE_TYPE_ULONG: return "Uint64"; |
| 278 | case BASE_TYPE_FLOAT: return "Float32"; |
| 279 | case BASE_TYPE_DOUBLE: return "Float64"; |
| 280 | case BASE_TYPE_STRING: return "String"; |
| 281 | case BASE_TYPE_VECTOR: return GenType(type.VectorType()); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 282 | case BASE_TYPE_STRUCT: return namer_.Type(*type.struct_def); |
| 283 | case BASE_TYPE_UNION: return namer_.Type(*type.enum_def) + "TypeId"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 284 | default: return "Table"; |
| 285 | } |
| 286 | } |
| 287 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 288 | static std::string EnumSize(const Type &type) { |
| 289 | switch (type.base_type) { |
| 290 | case BASE_TYPE_BOOL: |
| 291 | case BASE_TYPE_CHAR: |
| 292 | case BASE_TYPE_UTYPE: |
| 293 | case BASE_TYPE_UCHAR: return "1"; |
| 294 | case BASE_TYPE_SHORT: |
| 295 | case BASE_TYPE_USHORT: return "2"; |
| 296 | case BASE_TYPE_INT: |
| 297 | case BASE_TYPE_UINT: |
| 298 | case BASE_TYPE_FLOAT: return "4"; |
| 299 | case BASE_TYPE_LONG: |
| 300 | case BASE_TYPE_ULONG: |
| 301 | case BASE_TYPE_DOUBLE: return "8"; |
| 302 | default: return "1"; |
| 303 | } |
| 304 | } |
| 305 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 306 | std::string GenReaderTypeName(const Type &type, Namespace *current_namespace, |
| 307 | const FieldDef &def, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 308 | bool parent_is_vector = false, bool lazy = true, |
| 309 | bool constConstruct = true) { |
| 310 | std::string prefix = (constConstruct ? "const " : "") + _kFb; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 311 | if (type.base_type == BASE_TYPE_BOOL) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 312 | return prefix + ".BoolReader()"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 313 | } else if (IsVector(type)) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 314 | if (!type.VectorType().enum_def) { |
| 315 | if (type.VectorType().base_type == BASE_TYPE_CHAR) { |
| 316 | return prefix + ".Int8ListReader(" + (lazy ? ")" : "lazy: false)"); |
| 317 | } |
| 318 | if (type.VectorType().base_type == BASE_TYPE_UCHAR) { |
| 319 | return prefix + ".Uint8ListReader(" + (lazy ? ")" : "lazy: false)"); |
| 320 | } |
| 321 | } |
| 322 | return prefix + ".ListReader<" + |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 323 | GenDartTypeName(type.VectorType(), current_namespace, def) + ">(" + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 324 | GenReaderTypeName(type.VectorType(), current_namespace, def, true, |
| 325 | true, false) + |
| 326 | (lazy ? ")" : ", lazy: false)"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 327 | } else if (IsString(type)) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 328 | return prefix + ".StringReader()"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 329 | } |
| 330 | if (IsScalar(type.base_type)) { |
| 331 | if (type.enum_def && parent_is_vector) { |
| 332 | return GenDartTypeName(type, current_namespace, def) + ".reader"; |
| 333 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 334 | return prefix + "." + GenType(type) + "Reader()"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 335 | } else { |
| 336 | return GenDartTypeName(type, current_namespace, def) + ".reader"; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | std::string GenDartTypeName(const Type &type, Namespace *current_namespace, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 341 | const FieldDef &def, |
| 342 | std::string struct_type_suffix = "") { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 343 | if (type.enum_def) { |
| 344 | if (type.enum_def->is_union && type.base_type != BASE_TYPE_UNION) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 345 | return namer_.Type(*type.enum_def) + "TypeId"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 346 | } else if (type.enum_def->is_union) { |
| 347 | return "dynamic"; |
| 348 | } else if (type.base_type != BASE_TYPE_VECTOR) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 349 | return namer_.Type(*type.enum_def); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | switch (type.base_type) { |
| 354 | case BASE_TYPE_BOOL: return "bool"; |
| 355 | case BASE_TYPE_LONG: |
| 356 | case BASE_TYPE_ULONG: |
| 357 | case BASE_TYPE_INT: |
| 358 | case BASE_TYPE_UINT: |
| 359 | case BASE_TYPE_SHORT: |
| 360 | case BASE_TYPE_USHORT: |
| 361 | case BASE_TYPE_CHAR: |
| 362 | case BASE_TYPE_UCHAR: return "int"; |
| 363 | case BASE_TYPE_FLOAT: |
| 364 | case BASE_TYPE_DOUBLE: return "double"; |
| 365 | case BASE_TYPE_STRING: return "String"; |
| 366 | case BASE_TYPE_STRUCT: |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 367 | return MaybeWrapNamespace( |
| 368 | namer_.Type(*type.struct_def) + struct_type_suffix, |
| 369 | current_namespace, def); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 370 | case BASE_TYPE_VECTOR: |
| 371 | return "List<" + |
| 372 | GenDartTypeName(type.VectorType(), current_namespace, def, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 373 | struct_type_suffix) + |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 374 | ">"; |
| 375 | default: assert(0); return "dynamic"; |
| 376 | } |
| 377 | } |
| 378 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 379 | std::string GenDartTypeName(const Type &type, Namespace *current_namespace, |
| 380 | const FieldDef &def, bool nullable, |
| 381 | std::string struct_type_suffix) { |
| 382 | std::string typeName = |
| 383 | GenDartTypeName(type, current_namespace, def, struct_type_suffix); |
| 384 | if (nullable && typeName != "dynamic") typeName += "?"; |
| 385 | return typeName; |
| 386 | } |
| 387 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 388 | std::string MaybeWrapNamespace(const std::string &type_name, |
| 389 | Namespace *current_ns, |
| 390 | const FieldDef &field) const { |
| 391 | const std::string current_namespace = namer_.Namespace(*current_ns); |
| 392 | const std::string field_namespace = |
| 393 | field.value.type.struct_def |
| 394 | ? namer_.Namespace(*field.value.type.struct_def->defined_namespace) |
| 395 | : field.value.type.enum_def |
| 396 | ? namer_.Namespace(*field.value.type.enum_def->defined_namespace) |
| 397 | : ""; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 398 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 399 | if (field_namespace != "" && field_namespace != current_namespace) { |
| 400 | return ImportAliasName(field_namespace) + "." + type_name; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 401 | } else { |
| 402 | return type_name; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Generate an accessor struct with constructor for a flatbuffers struct. |
| 407 | void GenStruct(const StructDef &struct_def, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 408 | namespace_code_map &namespace_code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 409 | if (struct_def.generated) return; |
| 410 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 411 | std::string &code = |
| 412 | namespace_code[namer_.Namespace(*struct_def.defined_namespace)]; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 413 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 414 | const auto &struct_type = namer_.Type(struct_def); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 415 | |
| 416 | // Emit constructor |
| 417 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 418 | GenDocComment(struct_def.doc_comment, "", code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 419 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 420 | auto reader_name = "_" + struct_type + "Reader"; |
| 421 | auto builder_name = struct_type + "Builder"; |
| 422 | auto object_builder_name = struct_type + "ObjectBuilder"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 423 | |
| 424 | std::string reader_code, builder_code; |
| 425 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 426 | code += "class " + struct_type + " {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 427 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 428 | code += " " + struct_type + "._(this._bc, this._bcOffset);\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 429 | if (!struct_def.fixed) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 430 | code += " factory " + struct_type + "(List<int> bytes) {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 431 | code += |
| 432 | " final rootRef = " + _kFb + ".BufferContext.fromBytes(bytes);\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 433 | code += " return reader.read(rootRef, 0);\n"; |
| 434 | code += " }\n"; |
| 435 | } |
| 436 | |
| 437 | code += "\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 438 | code += " static const " + _kFb + ".Reader<" + struct_type + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 439 | "> reader = " + reader_name + "();\n\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 440 | |
| 441 | code += " final " + _kFb + ".BufferContext _bc;\n"; |
| 442 | code += " final int _bcOffset;\n\n"; |
| 443 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 444 | std::vector<std::pair<int, FieldDef *>> non_deprecated_fields; |
| 445 | for (auto it = struct_def.fields.vec.begin(); |
| 446 | it != struct_def.fields.vec.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 447 | FieldDef &field = **it; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 448 | if (field.deprecated) continue; |
| 449 | auto offset = static_cast<int>(it - struct_def.fields.vec.begin()); |
| 450 | non_deprecated_fields.push_back(std::make_pair(offset, &field)); |
| 451 | } |
| 452 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 453 | GenImplementationGetters(struct_def, non_deprecated_fields, code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 454 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 455 | if (parser_.opts.generate_object_based_api) { |
| 456 | code += |
| 457 | "\n" + GenStructObjectAPIUnpack(struct_def, non_deprecated_fields); |
| 458 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 459 | code += "\n static int pack(fb.Builder fbBuilder, " + |
| 460 | namer_.ObjectType(struct_def) + "? object) {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 461 | code += " if (object == null) return 0;\n"; |
| 462 | code += " return object.pack(fbBuilder);\n"; |
| 463 | code += " }\n"; |
| 464 | } |
| 465 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 466 | code += "}\n\n"; |
| 467 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 468 | if (parser_.opts.generate_object_based_api) { |
| 469 | code += GenStructObjectAPI(struct_def, non_deprecated_fields); |
| 470 | } |
| 471 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 472 | GenReader(struct_def, reader_name, reader_code); |
| 473 | GenBuilder(struct_def, non_deprecated_fields, builder_name, builder_code); |
| 474 | GenObjectBuilder(struct_def, non_deprecated_fields, object_builder_name, |
| 475 | builder_code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 476 | |
| 477 | code += reader_code; |
| 478 | code += builder_code; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 479 | } |
| 480 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 481 | // Generate an accessor struct with constructor for a flatbuffers struct. |
| 482 | std::string GenStructObjectAPI( |
| 483 | const StructDef &struct_def, |
| 484 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields) { |
| 485 | std::string code; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 486 | GenDocComment(struct_def.doc_comment, "", code); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 487 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 488 | std::string object_type = namer_.ObjectType(struct_def); |
| 489 | code += "class " + object_type + " implements " + _kFb + ".Packable {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 490 | |
| 491 | std::string constructor_args; |
| 492 | for (auto it = non_deprecated_fields.begin(); |
| 493 | it != non_deprecated_fields.end(); ++it) { |
| 494 | const FieldDef &field = *it->second; |
| 495 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 496 | const std::string field_name = namer_.Field(field); |
| 497 | const std::string defaultValue = getDefaultValue(field.value); |
| 498 | const std::string type_name = |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 499 | GenDartTypeName(field.value.type, struct_def.defined_namespace, field, |
| 500 | defaultValue.empty() && !struct_def.fixed, "T"); |
| 501 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 502 | GenDocComment(field.doc_comment, " ", code); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 503 | code += " " + type_name + " " + field_name + ";\n"; |
| 504 | |
| 505 | if (!constructor_args.empty()) constructor_args += ",\n"; |
| 506 | constructor_args += " "; |
| 507 | constructor_args += (struct_def.fixed ? "required " : ""); |
| 508 | constructor_args += "this." + field_name; |
| 509 | if (!struct_def.fixed && !defaultValue.empty()) { |
| 510 | if (IsEnum(field.value.type)) { |
| 511 | auto &enum_def = *field.value.type.enum_def; |
| 512 | if (auto val = enum_def.FindByValue(defaultValue)) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 513 | constructor_args += " = " + namer_.EnumVariant(enum_def, *val); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 514 | } else { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 515 | constructor_args += " = const " + namer_.Type(enum_def) + "._(" + |
| 516 | defaultValue + ")"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 517 | } |
| 518 | } else { |
| 519 | constructor_args += " = " + defaultValue; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (!constructor_args.empty()) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 525 | code += "\n " + object_type + "({\n" + constructor_args + "});\n\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | code += GenStructObjectAPIPack(struct_def, non_deprecated_fields); |
| 529 | code += "\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 530 | code += GenToString(object_type, non_deprecated_fields); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 531 | |
| 532 | code += "}\n\n"; |
| 533 | return code; |
| 534 | } |
| 535 | |
| 536 | // Generate function `StructNameT unpack()` |
| 537 | std::string GenStructObjectAPIUnpack( |
| 538 | const StructDef &struct_def, |
| 539 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields) { |
| 540 | std::string constructor_args; |
| 541 | for (auto it = non_deprecated_fields.begin(); |
| 542 | it != non_deprecated_fields.end(); ++it) { |
| 543 | const FieldDef &field = *it->second; |
| 544 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 545 | const std::string field_name = namer_.Field(field); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 546 | if (!constructor_args.empty()) constructor_args += ",\n"; |
| 547 | constructor_args += " " + field_name + ": "; |
| 548 | |
| 549 | const Type &type = field.value.type; |
| 550 | std::string defaultValue = getDefaultValue(field.value); |
| 551 | bool isNullable = defaultValue.empty() && !struct_def.fixed; |
| 552 | std::string nullableValueAccessOperator = isNullable ? "?" : ""; |
| 553 | if (type.base_type == BASE_TYPE_STRUCT) { |
| 554 | constructor_args += |
| 555 | field_name + nullableValueAccessOperator + ".unpack()"; |
| 556 | } else if (type.base_type == BASE_TYPE_VECTOR) { |
| 557 | if (type.VectorType().base_type == BASE_TYPE_STRUCT) { |
| 558 | constructor_args += field_name + nullableValueAccessOperator + |
| 559 | ".map((e) => e.unpack()).toList()"; |
| 560 | } else { |
| 561 | constructor_args += |
| 562 | GenReaderTypeName(field.value.type, struct_def.defined_namespace, |
| 563 | field, false, false); |
| 564 | constructor_args += ".vTableGet"; |
| 565 | std::string offset = NumToString(field.value.offset); |
| 566 | constructor_args += |
| 567 | isNullable |
| 568 | ? "Nullable(_bc, _bcOffset, " + offset + ")" |
| 569 | : "(_bc, _bcOffset, " + offset + ", " + defaultValue + ")"; |
| 570 | } |
| 571 | } else { |
| 572 | constructor_args += field_name; |
| 573 | } |
| 574 | } |
| 575 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 576 | const std::string object_type = namer_.ObjectType(struct_def); |
| 577 | std::string code = " " + object_type + " unpack() => " + object_type + "("; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 578 | if (!constructor_args.empty()) code += "\n" + constructor_args; |
| 579 | code += ");\n"; |
| 580 | return code; |
| 581 | } |
| 582 | |
| 583 | // Generate function `StructNameT pack()` |
| 584 | std::string GenStructObjectAPIPack( |
| 585 | const StructDef &struct_def, |
| 586 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields) { |
| 587 | std::string code; |
| 588 | |
| 589 | code += " @override\n"; |
| 590 | code += " int pack(fb.Builder fbBuilder) {\n"; |
| 591 | code += GenObjectBuilderImplementation(struct_def, non_deprecated_fields, |
| 592 | false, true); |
| 593 | code += " }\n"; |
| 594 | return code; |
| 595 | } |
| 596 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 597 | std::string NamespaceAliasFromUnionType(Namespace *root_namespace, |
| 598 | const Type &type) { |
| 599 | const std::vector<std::string> qualified_name_parts = |
| 600 | type.struct_def->defined_namespace->components; |
| 601 | if (std::equal(root_namespace->components.begin(), |
| 602 | root_namespace->components.end(), |
| 603 | qualified_name_parts.begin())) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 604 | return namer_.Type(*type.struct_def); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 605 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 606 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 607 | std::string ns; |
| 608 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 609 | for (auto it = qualified_name_parts.begin(); |
| 610 | it != qualified_name_parts.end(); ++it) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 611 | auto &part = *it; |
| 612 | |
| 613 | for (size_t i = 0; i < part.length(); i++) { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 614 | if (i && !isdigit(part[i]) && part[i] == CharToUpper(part[i])) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 615 | ns += "_"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 616 | ns += CharToLower(part[i]); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 617 | } else { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 618 | ns += CharToLower(part[i]); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 621 | if (it != qualified_name_parts.end() - 1) { ns += "_"; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 624 | return ns + "." + namer_.Type(*type.struct_def); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 627 | void GenImplementationGetters( |
| 628 | const StructDef &struct_def, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 629 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 630 | std::string &code) { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 631 | for (auto it = non_deprecated_fields.begin(); |
| 632 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 633 | const FieldDef &field = *it->second; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 634 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 635 | const std::string field_name = namer_.Field(field); |
| 636 | const std::string defaultValue = getDefaultValue(field.value); |
| 637 | const bool isNullable = defaultValue.empty() && !struct_def.fixed; |
| 638 | const std::string type_name = |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 639 | GenDartTypeName(field.value.type, struct_def.defined_namespace, field, |
| 640 | isNullable, ""); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 641 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 642 | GenDocComment(field.doc_comment, " ", code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 643 | |
| 644 | code += " " + type_name + " get " + field_name; |
| 645 | if (field.value.type.base_type == BASE_TYPE_UNION) { |
| 646 | code += " {\n"; |
| 647 | code += " switch (" + field_name + "Type?.value) {\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 648 | const auto &enum_def = *field.value.type.enum_def; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 649 | for (auto en_it = enum_def.Vals().begin() + 1; |
| 650 | en_it != enum_def.Vals().end(); ++en_it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 651 | const auto &ev = **en_it; |
| 652 | const auto enum_name = NamespaceAliasFromUnionType( |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 653 | enum_def.defined_namespace, ev.union_type); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 654 | code += " case " + enum_def.ToString(ev) + ": return " + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 655 | enum_name + ".reader.vTableGetNullable(_bc, _bcOffset, " + |
| 656 | NumToString(field.value.offset) + ");\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 657 | } |
| 658 | code += " default: return null;\n"; |
| 659 | code += " }\n"; |
| 660 | code += " }\n"; |
| 661 | } else { |
| 662 | code += " => "; |
| 663 | if (field.value.type.enum_def && |
| 664 | field.value.type.base_type != BASE_TYPE_VECTOR) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 665 | code += GenDartTypeName(field.value.type, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 666 | struct_def.defined_namespace, field) + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 667 | (isNullable ? "._createOrNull(" : ".fromValue("); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | code += GenReaderTypeName(field.value.type, |
| 671 | struct_def.defined_namespace, field); |
| 672 | if (struct_def.fixed) { |
| 673 | code += |
| 674 | ".read(_bc, _bcOffset + " + NumToString(field.value.offset) + ")"; |
| 675 | } else { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 676 | code += ".vTableGet"; |
| 677 | std::string offset = NumToString(field.value.offset); |
| 678 | if (isNullable) { |
| 679 | code += "Nullable(_bc, _bcOffset, " + offset + ")"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 680 | } else { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 681 | code += "(_bc, _bcOffset, " + offset + ", " + defaultValue + ")"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 682 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 683 | } |
| 684 | if (field.value.type.enum_def && |
| 685 | field.value.type.base_type != BASE_TYPE_VECTOR) { |
| 686 | code += ")"; |
| 687 | } |
| 688 | code += ";\n"; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | code += "\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 693 | code += GenToString(namer_.Type(struct_def), non_deprecated_fields); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 694 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 695 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 696 | std::string GenToString( |
| 697 | const std::string &object_name, |
| 698 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields) { |
| 699 | std::string code; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 700 | code += " @override\n"; |
| 701 | code += " String toString() {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 702 | code += " return '" + object_name + "{"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 703 | for (auto it = non_deprecated_fields.begin(); |
| 704 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 705 | const std::string field = namer_.Field(*it->second); |
| 706 | // We need to escape the fact that some fields have $ in the name which is |
| 707 | // also used in symbol/string substitution. |
| 708 | std::string escaped_field; |
| 709 | for (size_t i = 0; i < field.size(); i++) { |
| 710 | if (field[i] == '$') escaped_field.push_back('\\'); |
| 711 | escaped_field.push_back(field[i]); |
| 712 | } |
| 713 | code += escaped_field + ": ${" + field + "}"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 714 | if (it != non_deprecated_fields.end() - 1) { code += ", "; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 715 | } |
| 716 | code += "}';\n"; |
| 717 | code += " }\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 718 | return code; |
| 719 | } |
| 720 | |
| 721 | std::string getDefaultValue(const Value &value) const { |
| 722 | if (!value.constant.empty() && value.constant != "0") { |
| 723 | if (IsBool(value.type.base_type)) { |
| 724 | return "true"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 725 | } |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame^] | 726 | if (IsScalar(value.type.base_type)) { |
| 727 | if (StringIsFlatbufferNan(value.constant)) { |
| 728 | return "double.nan"; |
| 729 | } else if (StringIsFlatbufferPositiveInfinity(value.constant)) { |
| 730 | return "double.infinity"; |
| 731 | } else if (StringIsFlatbufferNegativeInfinity(value.constant)) { |
| 732 | return "double.negativeInfinity"; |
| 733 | } |
| 734 | } |
| 735 | return value.constant; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 736 | } else if (IsBool(value.type.base_type)) { |
| 737 | return "false"; |
| 738 | } else if (IsScalar(value.type.base_type) && !IsUnion(value.type)) { |
| 739 | return "0"; |
| 740 | } else { |
| 741 | return ""; |
| 742 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 745 | void GenReader(const StructDef &struct_def, const std::string &reader_name, |
| 746 | std::string &code) { |
| 747 | const auto struct_type = namer_.Type(struct_def); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 748 | |
| 749 | code += "class " + reader_name + " extends " + _kFb; |
| 750 | if (struct_def.fixed) { |
| 751 | code += ".StructReader<"; |
| 752 | } else { |
| 753 | code += ".TableReader<"; |
| 754 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 755 | code += struct_type + "> {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 756 | code += " const " + reader_name + "();\n\n"; |
| 757 | |
| 758 | if (struct_def.fixed) { |
| 759 | code += " @override\n"; |
| 760 | code += " int get size => " + NumToString(struct_def.bytesize) + ";\n\n"; |
| 761 | } |
| 762 | code += " @override\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 763 | code += " " + struct_type + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 764 | " createObject(fb.BufferContext bc, int offset) => \n " + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 765 | struct_type + "._(bc, offset);\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 766 | code += "}\n\n"; |
| 767 | } |
| 768 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 769 | void GenBuilder( |
| 770 | const StructDef &struct_def, |
| 771 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 772 | const std::string &builder_name, std::string &code) { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 773 | if (non_deprecated_fields.size() == 0) { return; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 774 | |
| 775 | code += "class " + builder_name + " {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 776 | code += " " + builder_name + "(this.fbBuilder);\n\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 777 | code += " final " + _kFb + ".Builder fbBuilder;\n\n"; |
| 778 | |
| 779 | if (struct_def.fixed) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 780 | StructBuilderBody(struct_def, non_deprecated_fields, code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 781 | } else { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 782 | TableBuilderBody(struct_def, non_deprecated_fields, code); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | code += "}\n\n"; |
| 786 | } |
| 787 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 788 | void StructBuilderBody( |
| 789 | const StructDef &struct_def, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 790 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 791 | std::string &code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 792 | code += " int finish("; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 793 | for (auto it = non_deprecated_fields.begin(); |
| 794 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 795 | const FieldDef &field = *it->second; |
| 796 | const std::string field_name = namer_.Field(field); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 797 | |
| 798 | if (IsStruct(field.value.type)) { |
| 799 | code += "fb.StructBuilder"; |
| 800 | } else { |
| 801 | code += GenDartTypeName(field.value.type, struct_def.defined_namespace, |
| 802 | field); |
| 803 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 804 | code += " " + field_name; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 805 | if (it != non_deprecated_fields.end() - 1) { code += ", "; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 806 | } |
| 807 | code += ") {\n"; |
| 808 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 809 | for (auto it = non_deprecated_fields.rbegin(); |
| 810 | it != non_deprecated_fields.rend(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 811 | const FieldDef &field = *it->second; |
| 812 | const std::string field_name = namer_.Field(field); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 813 | |
| 814 | if (field.padding) { |
| 815 | code += " fbBuilder.pad(" + NumToString(field.padding) + ");\n"; |
| 816 | } |
| 817 | |
| 818 | if (IsStruct(field.value.type)) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 819 | code += " " + field_name + "();\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 820 | } else { |
| 821 | code += " fbBuilder.put" + GenType(field.value.type) + "("; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 822 | code += field_name; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 823 | if (field.value.type.enum_def) { code += ".value"; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 824 | code += ");\n"; |
| 825 | } |
| 826 | } |
| 827 | code += " return fbBuilder.offset;\n"; |
| 828 | code += " }\n\n"; |
| 829 | } |
| 830 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 831 | void TableBuilderBody( |
| 832 | const StructDef &struct_def, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 833 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 834 | std::string &code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 835 | code += " void begin() {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 836 | code += " fbBuilder.startTable(" + |
| 837 | NumToString(struct_def.fields.vec.size()) + ");\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 838 | code += " }\n\n"; |
| 839 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 840 | for (auto it = non_deprecated_fields.begin(); |
| 841 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 842 | const auto &field = *it->second; |
| 843 | const auto offset = it->first; |
| 844 | const std::string add_field = namer_.Method("add", field); |
| 845 | const std::string field_var = namer_.Variable(field); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 846 | |
| 847 | if (IsScalar(field.value.type.base_type)) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 848 | code += " int " + add_field + "("; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 849 | code += GenDartTypeName(field.value.type, struct_def.defined_namespace, |
| 850 | field); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 851 | code += "? " + field_var + ") {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 852 | code += " fbBuilder.add" + GenType(field.value.type) + "(" + |
| 853 | NumToString(offset) + ", "; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 854 | code += field_var; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 855 | if (field.value.type.enum_def) { code += "?.value"; } |
| 856 | code += ");\n"; |
| 857 | } else if (IsStruct(field.value.type)) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 858 | code += " int " + add_field + "(int offset) {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 859 | code += |
| 860 | " fbBuilder.addStruct(" + NumToString(offset) + ", offset);\n"; |
| 861 | } else { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 862 | code += " int " + add_field + "Offset(int? offset) {\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 863 | code += |
| 864 | " fbBuilder.addOffset(" + NumToString(offset) + ", offset);\n"; |
| 865 | } |
| 866 | code += " return fbBuilder.offset;\n"; |
| 867 | code += " }\n"; |
| 868 | } |
| 869 | |
| 870 | code += "\n"; |
| 871 | code += " int finish() {\n"; |
| 872 | code += " return fbBuilder.endTable();\n"; |
| 873 | code += " }\n"; |
| 874 | } |
| 875 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 876 | void GenObjectBuilder( |
| 877 | const StructDef &struct_def, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 878 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 879 | const std::string &builder_name, std::string &code) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 880 | code += "class " + builder_name + " extends " + _kFb + ".ObjectBuilder {\n"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 881 | for (auto it = non_deprecated_fields.begin(); |
| 882 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 883 | const FieldDef &field = *it->second; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 884 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 885 | code += " final " + |
| 886 | GenDartTypeName(field.value.type, struct_def.defined_namespace, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 887 | field, !struct_def.fixed, "ObjectBuilder") + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 888 | " _" + namer_.Variable(field) + ";\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 889 | } |
| 890 | code += "\n"; |
| 891 | code += " " + builder_name + "("; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 892 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 893 | if (non_deprecated_fields.size() != 0) { |
| 894 | code += "{\n"; |
| 895 | for (auto it = non_deprecated_fields.begin(); |
| 896 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 897 | const FieldDef &field = *it->second; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 898 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 899 | code += " "; |
| 900 | code += (struct_def.fixed ? "required " : "") + |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 901 | GenDartTypeName(field.value.type, struct_def.defined_namespace, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 902 | field, !struct_def.fixed, "ObjectBuilder") + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 903 | " " + namer_.Variable(field) + ",\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 904 | } |
| 905 | code += " })\n"; |
| 906 | code += " : "; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 907 | for (auto it = non_deprecated_fields.begin(); |
| 908 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 909 | const FieldDef &field = *it->second; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 910 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 911 | code += "_" + namer_.Variable(field) + " = " + namer_.Variable(field); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 912 | if (it == non_deprecated_fields.end() - 1) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 913 | code += ";\n\n"; |
| 914 | } else { |
| 915 | code += ",\n "; |
| 916 | } |
| 917 | } |
| 918 | } else { |
| 919 | code += ");\n\n"; |
| 920 | } |
| 921 | |
| 922 | code += " /// Finish building, and store into the [fbBuilder].\n"; |
| 923 | code += " @override\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 924 | code += " int finish(" + _kFb + ".Builder fbBuilder) {\n"; |
| 925 | code += GenObjectBuilderImplementation(struct_def, non_deprecated_fields); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 926 | code += " }\n\n"; |
| 927 | |
| 928 | code += " /// Convenience method to serialize to byte list.\n"; |
| 929 | code += " @override\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 930 | code += " Uint8List toBytes([String? fileIdentifier]) {\n"; |
| 931 | code += " final fbBuilder = " + _kFb + |
| 932 | ".Builder(deduplicateTables: false);\n"; |
| 933 | code += " fbBuilder.finish(finish(fbBuilder), fileIdentifier);\n"; |
| 934 | code += " return fbBuilder.buffer;\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 935 | code += " }\n"; |
| 936 | code += "}\n"; |
| 937 | } |
| 938 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 939 | std::string GenObjectBuilderImplementation( |
| 940 | const StructDef &struct_def, |
| 941 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 942 | bool prependUnderscore = true, bool pack = false) { |
| 943 | std::string code; |
| 944 | for (auto it = non_deprecated_fields.begin(); |
| 945 | it != non_deprecated_fields.end(); ++it) { |
| 946 | const FieldDef &field = *it->second; |
| 947 | |
| 948 | if (IsScalar(field.value.type.base_type) || IsStruct(field.value.type)) |
| 949 | continue; |
| 950 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 951 | std::string offset_name = namer_.Variable(field) + "Offset"; |
| 952 | std::string field_name = |
| 953 | (prependUnderscore ? "_" : "") + namer_.Variable(field); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 954 | // custom handling for fixed-sized struct in pack() |
| 955 | if (pack && IsVector(field.value.type) && |
| 956 | field.value.type.VectorType().base_type == BASE_TYPE_STRUCT && |
| 957 | field.value.type.struct_def->fixed) { |
| 958 | code += " int? " + offset_name + ";\n"; |
| 959 | code += " if (" + field_name + " != null) {\n"; |
| 960 | code += |
| 961 | " for (var e in " + field_name + "!) { e.pack(fbBuilder); }\n"; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 962 | code += " " + namer_.Variable(field) + |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 963 | "Offset = fbBuilder.endStructVector(" + field_name + |
| 964 | "!.length);\n"; |
| 965 | code += " }\n"; |
| 966 | continue; |
| 967 | } |
| 968 | |
| 969 | code += " final int? " + offset_name; |
| 970 | if (IsVector(field.value.type)) { |
| 971 | code += " = " + field_name + " == null ? null\n"; |
| 972 | code += " : fbBuilder.writeList"; |
| 973 | switch (field.value.type.VectorType().base_type) { |
| 974 | case BASE_TYPE_STRING: |
| 975 | code += |
| 976 | "(" + field_name + "!.map(fbBuilder.writeString).toList());\n"; |
| 977 | break; |
| 978 | case BASE_TYPE_STRUCT: |
| 979 | if (field.value.type.struct_def->fixed) { |
| 980 | code += "OfStructs(" + field_name + "!);\n"; |
| 981 | } else { |
| 982 | code += "(" + field_name + "!.map((b) => b." + |
| 983 | (pack ? "pack" : "getOrCreateOffset") + |
| 984 | "(fbBuilder)).toList());\n"; |
| 985 | } |
| 986 | break; |
| 987 | default: |
| 988 | code += |
| 989 | GenType(field.value.type.VectorType()) + "(" + field_name + "!"; |
| 990 | if (field.value.type.enum_def) { |
| 991 | code += ".map((f) => f.value).toList()"; |
| 992 | } |
| 993 | code += ");\n"; |
| 994 | } |
| 995 | } else if (IsString(field.value.type)) { |
| 996 | code += " = " + field_name + " == null ? null\n"; |
| 997 | code += " : fbBuilder.writeString(" + field_name + "!);\n"; |
| 998 | } else { |
| 999 | code += " = " + field_name + "?." + |
| 1000 | (pack ? "pack" : "getOrCreateOffset") + "(fbBuilder);\n"; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if (struct_def.fixed) { |
| 1005 | code += StructObjectBuilderBody(non_deprecated_fields, prependUnderscore, |
| 1006 | pack); |
| 1007 | } else { |
| 1008 | code += TableObjectBuilderBody(struct_def, non_deprecated_fields, |
| 1009 | prependUnderscore, pack); |
| 1010 | } |
| 1011 | return code; |
| 1012 | } |
| 1013 | |
| 1014 | std::string StructObjectBuilderBody( |
| 1015 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 1016 | bool prependUnderscore = true, bool pack = false) { |
| 1017 | std::string code; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1018 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 1019 | for (auto it = non_deprecated_fields.rbegin(); |
| 1020 | it != non_deprecated_fields.rend(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1021 | const FieldDef &field = *it->second; |
| 1022 | const std::string field_name = namer_.Field(field); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1023 | |
| 1024 | if (field.padding) { |
| 1025 | code += " fbBuilder.pad(" + NumToString(field.padding) + ");\n"; |
| 1026 | } |
| 1027 | |
| 1028 | if (IsStruct(field.value.type)) { |
| 1029 | code += " "; |
| 1030 | if (prependUnderscore) { code += "_"; } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1031 | code += field_name + (pack ? ".pack" : ".finish") + "(fbBuilder);\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1032 | } else { |
| 1033 | code += " fbBuilder.put" + GenType(field.value.type) + "("; |
| 1034 | if (prependUnderscore) { code += "_"; } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1035 | code += field_name; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1036 | if (field.value.type.enum_def) { code += ".value"; } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1037 | code += ");\n"; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | code += " return fbBuilder.offset;\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1042 | return code; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1045 | std::string TableObjectBuilderBody( |
| 1046 | const StructDef &struct_def, |
| 1047 | const std::vector<std::pair<int, FieldDef *>> &non_deprecated_fields, |
| 1048 | bool prependUnderscore = true, bool pack = false) { |
| 1049 | std::string code; |
| 1050 | code += " fbBuilder.startTable(" + |
| 1051 | NumToString(struct_def.fields.vec.size()) + ");\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1052 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 1053 | for (auto it = non_deprecated_fields.begin(); |
| 1054 | it != non_deprecated_fields.end(); ++it) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1055 | const FieldDef &field = *it->second; |
| 1056 | auto offset = it->first; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1057 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1058 | std::string field_var = |
| 1059 | (prependUnderscore ? "_" : "") + namer_.Variable(field); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1060 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1061 | if (IsScalar(field.value.type.base_type)) { |
| 1062 | code += " fbBuilder.add" + GenType(field.value.type) + "(" + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1063 | NumToString(offset) + ", " + field_var; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1064 | if (field.value.type.enum_def) { |
| 1065 | bool isNullable = getDefaultValue(field.value).empty(); |
| 1066 | code += (isNullable || !pack) ? "?.value" : ".value"; |
| 1067 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1068 | code += ");\n"; |
| 1069 | } else if (IsStruct(field.value.type)) { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1070 | code += " if (" + field_var + " != null) {\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1071 | code += " fbBuilder.addStruct(" + NumToString(offset) + ", " + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1072 | field_var + (pack ? "!.pack" : "!.finish") + "(fbBuilder));\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1073 | code += " }\n"; |
| 1074 | } else { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1075 | code += " fbBuilder.addOffset(" + NumToString(offset) + ", " + |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1076 | namer_.Variable(field) + "Offset);\n"; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | code += " return fbBuilder.endTable();\n"; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1080 | return code; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1081 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1082 | |
| 1083 | const IdlNamer namer_; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1084 | }; |
| 1085 | } // namespace dart |
| 1086 | |
| 1087 | bool GenerateDart(const Parser &parser, const std::string &path, |
| 1088 | const std::string &file_name) { |
| 1089 | dart::DartGenerator generator(parser, path, file_name); |
| 1090 | return generator.generate(); |
| 1091 | } |
| 1092 | |
| 1093 | std::string DartMakeRule(const Parser &parser, const std::string &path, |
| 1094 | const std::string &file_name) { |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1095 | auto filebase = |
| 1096 | flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 1097 | dart::DartGenerator generator(parser, path, file_name); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 1098 | auto make_rule = generator.Filename("") + ": "; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1099 | |
| 1100 | auto included_files = parser.GetIncludedFilesRecursive(file_name); |
| 1101 | for (auto it = included_files.begin(); it != included_files.end(); ++it) { |
| 1102 | make_rule += " " + *it; |
| 1103 | } |
| 1104 | return make_rule; |
| 1105 | } |
| 1106 | |
| 1107 | } // namespace flatbuffers |