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