blob: 0b8ffa821e8c41a54e7accc694b4eae0df85e16c [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// independent from idl_parser, since this code is not needed for most clients
18
Austin Schuh272c6132020-11-14 16:37:52 -080019#include <cctype>
20#include <set>
Austin Schuhe89fa2d2019-08-14 20:24:23 -070021#include <string>
Austin Schuh272c6132020-11-14 16:37:52 -080022#include <unordered_set>
23#include <vector>
Austin Schuhe89fa2d2019-08-14 20:24:23 -070024
25#include "flatbuffers/code_generators.h"
26#include "flatbuffers/flatbuffers.h"
27#include "flatbuffers/idl.h"
28#include "flatbuffers/util.h"
Austin Schuh2dd86a92022-09-14 21:19:23 -070029#include "idl_namer.h"
Austin Schuhe89fa2d2019-08-14 20:24:23 -070030
Austin Schuhe89fa2d2019-08-14 20:24:23 -070031namespace flatbuffers {
32namespace python {
33
Austin Schuh2dd86a92022-09-14 21:19:23 -070034namespace {
35
36static std::set<std::string> PythonKeywords() {
James Kuszmaul8e62b022022-03-22 09:33:25 -070037 return { "False", "None", "True", "and", "as", "assert",
38 "break", "class", "continue", "def", "del", "elif",
39 "else", "except", "finally", "for", "from", "global",
40 "if", "import", "in", "is", "lambda", "nonlocal",
41 "not", "or", "pass", "raise", "return", "try",
42 "while", "with", "yield" };
43}
44
Austin Schuh2dd86a92022-09-14 21:19:23 -070045static Namer::Config PythonDefaultConfig() {
James Kuszmaul8e62b022022-03-22 09:33:25 -070046 return { /*types=*/Case::kKeep,
47 /*constants=*/Case::kScreamingSnake,
48 /*methods=*/Case::kUpperCamel,
49 /*functions=*/Case::kUpperCamel,
50 /*fields=*/Case::kLowerCamel,
51 /*variable=*/Case::kLowerCamel,
52 /*variants=*/Case::kKeep,
53 /*enum_variant_seperator=*/".",
Austin Schuh2dd86a92022-09-14 21:19:23 -070054 /*escape_keywords=*/Namer::Config::Escape::BeforeConvertingCase,
James Kuszmaul8e62b022022-03-22 09:33:25 -070055 /*namespaces=*/Case::kKeep, // Packages in python.
56 /*namespace_seperator=*/".",
57 /*object_prefix=*/"",
58 /*object_suffix=*/"T",
59 /*keyword_prefix=*/"",
60 /*keyword_suffix=*/"_",
61 /*filenames=*/Case::kKeep,
62 /*directories=*/Case::kKeep,
63 /*output_path=*/"",
64 /*filename_suffix=*/"",
65 /*filename_extension=*/".py" };
66}
67
Austin Schuhe89fa2d2019-08-14 20:24:23 -070068// Hardcode spaces per indentation.
Austin Schuh2dd86a92022-09-14 21:19:23 -070069static const CommentConfig def_comment = { nullptr, "#", nullptr };
70static const std::string Indent = " ";
71
72} // namespace
Austin Schuhe89fa2d2019-08-14 20:24:23 -070073
74class PythonGenerator : public BaseGenerator {
75 public:
76 PythonGenerator(const Parser &parser, const std::string &path,
77 const std::string &file_name)
78 : BaseGenerator(parser, path, file_name, "" /* not used */,
Austin Schuh272c6132020-11-14 16:37:52 -080079 "" /* not used */, "py"),
James Kuszmaul8e62b022022-03-22 09:33:25 -070080 float_const_gen_("float('nan')", "float('inf')", "float('-inf')"),
Austin Schuh2dd86a92022-09-14 21:19:23 -070081 namer_(WithFlagOptions(PythonDefaultConfig(), parser.opts, path),
82 PythonKeywords()) {}
Austin Schuhe89fa2d2019-08-14 20:24:23 -070083
84 // Most field accessors need to retrieve and test the field offset first,
85 // this is the prefix code for that.
James Kuszmaul8e62b022022-03-22 09:33:25 -070086 std::string OffsetPrefix(const FieldDef &field) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -070087 return "\n" + Indent + Indent +
Austin Schuh272c6132020-11-14 16:37:52 -080088 "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
89 "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
90 Indent + Indent + "if o != 0:\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -070091 }
92
93 // Begin a class declaration.
James Kuszmaul8e62b022022-03-22 09:33:25 -070094 void BeginClass(const StructDef &struct_def, std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -080095 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -070096 code += "class " + namer_.Type(struct_def) + "(object):\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -070097 code += Indent + "__slots__ = ['_tab']";
98 code += "\n\n";
99 }
100
101 // Begin enum code with a class declaration.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700102 void BeginEnum(const EnumDef &enum_def, std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800103 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700104 code += "class " + namer_.Type(enum_def) + "(object):\n";
Austin Schuh272c6132020-11-14 16:37:52 -0800105 }
106
107 // Starts a new line and then indents.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700108 std::string GenIndents(int num) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800109 return "\n" + std::string(num * Indent.length(), ' ');
110 }
111
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700112 // A single enum member.
113 void EnumMember(const EnumDef &enum_def, const EnumVal &ev,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700114 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800115 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700116 code += Indent;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700117 code += namer_.Variant(ev);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700118 code += " = ";
119 code += enum_def.ToString(ev) + "\n";
120 }
121
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700122 // Initialize a new struct or table from existing data.
123 void NewRootTypeFromBuffer(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700124 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800125 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700126 const std::string struct_type = namer_.Type(struct_def);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700127
128 code += Indent + "@classmethod\n";
129 code += Indent + "def GetRootAs";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700130 code += "(cls, buf, offset=0):";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700131 code += "\n";
132 code += Indent + Indent;
133 code += "n = flatbuffers.encode.Get";
134 code += "(flatbuffers.packer.uoffset, buf, offset)\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700135 code += Indent + Indent + "x = " + struct_type + "()\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700136 code += Indent + Indent + "x.Init(buf, n + offset)\n";
137 code += Indent + Indent + "return x\n";
138 code += "\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700139
140 // Add an alias with the old name
141 code += Indent + "@classmethod\n";
142 code += Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n";
143 code +=
144 Indent + Indent +
145 "\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
146 code += Indent + Indent + "return cls.GetRootAs(buf, offset)\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700147 }
148
149 // Initialize an existing object with other data, to avoid an allocation.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700150 void InitializeExisting(const StructDef &struct_def,
151 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800152 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700153
154 GenReceiver(struct_def, code_ptr);
155 code += "Init(self, buf, pos):\n";
156 code += Indent + Indent + "self._tab = flatbuffers.table.Table(buf, pos)\n";
157 code += "\n";
158 }
159
160 // Get the length of a vector.
161 void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700162 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800163 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700164
165 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700166 code += namer_.Method(field) + "Length(self";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700167 code += "):" + OffsetPrefix(field);
168 code += Indent + Indent + Indent + "return self._tab.VectorLen(o)\n";
169 code += Indent + Indent + "return 0\n\n";
170 }
171
Austin Schuh272c6132020-11-14 16:37:52 -0800172 // Determines whether a vector is none or not.
173 void GetVectorIsNone(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700174 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800175 auto &code = *code_ptr;
176
177 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700178 code += namer_.Method(field) + "IsNone(self";
Austin Schuh272c6132020-11-14 16:37:52 -0800179 code += "):";
180 code += GenIndents(2) +
181 "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
182 "(self._tab.Offset(" + NumToString(field.value.offset) + "))";
183 code += GenIndents(2) + "return o == 0";
184 code += "\n\n";
185 }
186
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700187 // Get the value of a struct's scalar.
188 void GetScalarFieldOfStruct(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700189 const FieldDef &field,
190 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800191 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700192 std::string getter = GenGetter(field.value.type);
193 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700194 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700195 code += "(self): return " + getter;
196 code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
197 code += NumToString(field.value.offset) + "))\n";
198 }
199
200 // Get the value of a table's scalar.
Austin Schuh272c6132020-11-14 16:37:52 -0800201 void GetScalarFieldOfTable(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700202 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800203 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700204 std::string getter = GenGetter(field.value.type);
205 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700206 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700207 code += "(self):";
208 code += OffsetPrefix(field);
209 getter += "o + self._tab.Pos)";
210 auto is_bool = IsBool(field.value.type.base_type);
Austin Schuh272c6132020-11-14 16:37:52 -0800211 if (is_bool) { getter = "bool(" + getter + ")"; }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700212 code += Indent + Indent + Indent + "return " + getter + "\n";
213 std::string default_value;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700214 if (field.IsScalarOptional()) {
215 default_value = "None";
216 } else if (is_bool) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700217 default_value = field.value.constant == "0" ? "False" : "True";
218 } else {
219 default_value = IsFloat(field.value.type.base_type)
220 ? float_const_gen_.GenFloatConstant(field)
221 : field.value.constant;
222 }
223 code += Indent + Indent + "return " + default_value + "\n\n";
224 }
225
226 // Get a struct by initializing an existing struct.
227 // Specific to Struct.
228 void GetStructFieldOfStruct(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700229 const FieldDef &field,
230 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800231 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700232 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700233 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700234 code += "(self, obj):\n";
235 code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
236 code += NumToString(field.value.offset) + ")";
237 code += "\n" + Indent + Indent + "return obj\n\n";
238 }
239
240 // Get the value of a fixed size array.
241 void GetArrayOfStruct(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700242 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800243 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700244 const auto vec_type = field.value.type.VectorType();
245 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700246 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700247 if (IsStruct(vec_type)) {
248 code += "(self, obj, i):\n";
249 code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
250 code += NumToString(field.value.offset) + " + i * ";
251 code += NumToString(InlineSize(vec_type));
252 code += ")\n" + Indent + Indent + "return obj\n\n";
253 } else {
254 auto getter = GenGetter(vec_type);
255 code += "(self): return [" + getter;
256 code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
257 code += NumToString(field.value.offset) + " + i * ";
258 code += NumToString(InlineSize(vec_type));
259 code += ")) for i in range(";
260 code += NumToString(field.value.type.fixed_length) + ")]\n";
261 }
262 }
263
264 // Get a struct by initializing an existing struct.
265 // Specific to Table.
Austin Schuh272c6132020-11-14 16:37:52 -0800266 void GetStructFieldOfTable(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700267 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800268 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700269 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700270 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700271 code += "(self):";
272 code += OffsetPrefix(field);
273 if (field.value.type.struct_def->fixed) {
274 code += Indent + Indent + Indent + "x = o + self._tab.Pos\n";
275 } else {
276 code += Indent + Indent + Indent;
277 code += "x = self._tab.Indirect(o + self._tab.Pos)\n";
278 }
Austin Schuh272c6132020-11-14 16:37:52 -0800279 if (parser_.opts.include_dependence_headers) {
280 code += Indent + Indent + Indent;
281 code += "from " + GenPackageReference(field.value.type) + " import " +
282 TypeName(field) + "\n";
283 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700284 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
285 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
286 code += Indent + Indent + Indent + "return obj\n";
287 code += Indent + Indent + "return None\n\n";
288 }
289
290 // Get the value of a string.
291 void GetStringField(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700292 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800293 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700294 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700295 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700296 code += "(self):";
297 code += OffsetPrefix(field);
298 code += Indent + Indent + Indent + "return " + GenGetter(field.value.type);
299 code += "o + self._tab.Pos)\n";
300 code += Indent + Indent + "return None\n\n";
301 }
302
303 // Get the value of a union from an object.
304 void GetUnionField(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700305 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800306 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700307 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700308 code += namer_.Method(field) + "(self):";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700309 code += OffsetPrefix(field);
310
311 // TODO(rw): this works and is not the good way to it:
312 bool is_native_table = TypeName(field) == "*flatbuffers.Table";
313 if (is_native_table) {
Austin Schuh272c6132020-11-14 16:37:52 -0800314 code +=
315 Indent + Indent + Indent + "from flatbuffers.table import Table\n";
316 } else if (parser_.opts.include_dependence_headers) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700317 code += Indent + Indent + Indent;
Austin Schuh272c6132020-11-14 16:37:52 -0800318 code += "from " + GenPackageReference(field.value.type) + " import " +
319 TypeName(field) + "\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700320 }
321 code += Indent + Indent + Indent + "obj = Table(bytearray(), 0)\n";
322 code += Indent + Indent + Indent + GenGetter(field.value.type);
323 code += "obj, o)\n" + Indent + Indent + Indent + "return obj\n";
324 code += Indent + Indent + "return None\n\n";
325 }
326
Austin Schuh272c6132020-11-14 16:37:52 -0800327 // Generate the package reference when importing a struct or enum from its
328 // module.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700329 std::string GenPackageReference(const Type &type) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800330 if (type.struct_def) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700331 return namer_.NamespacedType(*type.struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800332 } else if (type.enum_def) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700333 return namer_.NamespacedType(*type.enum_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800334 } else {
335 return "." + GenTypeGet(type);
336 }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700337 }
Austin Schuh272c6132020-11-14 16:37:52 -0800338
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700339 // Get the value of a vector's struct member.
340 void GetMemberOfVectorOfStruct(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700341 const FieldDef &field,
342 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800343 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700344 auto vectortype = field.value.type.VectorType();
345
346 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700347 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700348 code += "(self, j):" + OffsetPrefix(field);
349 code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n";
350 code += Indent + Indent + Indent;
351 code += "x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * ";
352 code += NumToString(InlineSize(vectortype)) + "\n";
353 if (!(vectortype.struct_def->fixed)) {
354 code += Indent + Indent + Indent + "x = self._tab.Indirect(x)\n";
355 }
Austin Schuh272c6132020-11-14 16:37:52 -0800356 if (parser_.opts.include_dependence_headers) {
357 code += Indent + Indent + Indent;
358 code += "from " + GenPackageReference(field.value.type) + " import " +
359 TypeName(field) + "\n";
360 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700361 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
362 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
363 code += Indent + Indent + Indent + "return obj\n";
364 code += Indent + Indent + "return None\n\n";
365 }
366
367 // Get the value of a vector's non-struct member. Uses a named return
368 // argument to conveniently set the zero value for the result.
369 void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
370 const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700371 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800372 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700373 auto vectortype = field.value.type.VectorType();
374
375 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700376 code += namer_.Method(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700377 code += "(self, j):";
378 code += OffsetPrefix(field);
379 code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n";
380 code += Indent + Indent + Indent;
381 code += "return " + GenGetter(field.value.type);
382 code += "a + flatbuffers.number_types.UOffsetTFlags.py_type(j * ";
383 code += NumToString(InlineSize(vectortype)) + "))\n";
Austin Schuh272c6132020-11-14 16:37:52 -0800384 if (IsString(vectortype)) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700385 code += Indent + Indent + "return \"\"\n";
386 } else {
387 code += Indent + Indent + "return 0\n";
388 }
389 code += "\n";
390 }
391
392 // Returns a non-struct vector as a numpy array. Much faster
393 // than iterating over the vector element by element.
394 void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
395 const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700396 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800397 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700398 auto vectortype = field.value.type.VectorType();
399
400 // Currently, we only support accessing as numpy array if
401 // the vector type is a scalar.
402 if (!(IsScalar(vectortype.base_type))) { return; }
403
404 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700405 code += namer_.Method(field) + "AsNumpy(self):";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700406 code += OffsetPrefix(field);
407
408 code += Indent + Indent + Indent;
409 code += "return ";
410 code += "self._tab.GetVectorAsNumpy(flatbuffers.number_types.";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700411 code += namer_.Method(GenTypeGet(field.value.type));
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700412 code += "Flags, o)\n";
413
Austin Schuh272c6132020-11-14 16:37:52 -0800414 if (IsString(vectortype)) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700415 code += Indent + Indent + "return \"\"\n";
416 } else {
417 code += Indent + Indent + "return 0\n";
418 }
419 code += "\n";
420 }
421
James Kuszmaul8e62b022022-03-22 09:33:25 -0700422 std::string NestedFlatbufferType(std::string unqualified_name) const {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700423 StructDef *nested_root = parser_.LookupStruct(unqualified_name);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700424 std::string qualified_name;
425 if (nested_root == nullptr) {
426 qualified_name = namer_.NamespacedType(
427 parser_.current_namespace_->components, unqualified_name);
428 // Double check qualified name just to be sure it exists.
429 nested_root = parser_.LookupStruct(qualified_name);
430 }
431 FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.
432 return qualified_name;
433 }
434
435 // Returns a nested flatbuffer as itself.
436 void GetVectorAsNestedFlatbuffer(const StructDef &struct_def,
437 const FieldDef &field,
438 std::string *code_ptr) const {
439 auto nested = field.attributes.Lookup("nested_flatbuffer");
440 if (!nested) { return; } // There is no nested flatbuffer.
441
442 const std::string unqualified_name = nested->constant;
443 const std::string qualified_name = NestedFlatbufferType(unqualified_name);
444
445 auto &code = *code_ptr;
446 GenReceiver(struct_def, code_ptr);
Austin Schuh2dd86a92022-09-14 21:19:23 -0700447 code += namer_.Method(field) + "NestedRoot(self):";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700448
449 code += OffsetPrefix(field);
450
451 code += Indent + Indent + Indent;
452 code += "from " + qualified_name + " import " + unqualified_name + "\n";
453 code += Indent + Indent + Indent + "return " + unqualified_name;
454 code += ".GetRootAs" + unqualified_name;
455 code += "(self._tab.Bytes, self._tab.Vector(o))\n";
456 code += Indent + Indent + "return 0\n";
457 code += "\n";
458 }
459
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700460 // Begin the creator function signature.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700461 void BeginBuilderArgs(const StructDef &struct_def,
462 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800463 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700464
465 code += "\n";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700466 code += "def Create" + namer_.Type(struct_def);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700467 code += "(builder";
468 }
469
470 // Recursively generate arguments for a constructor, to deal with nested
471 // structs.
472 void StructBuilderArgs(const StructDef &struct_def,
Austin Schuh272c6132020-11-14 16:37:52 -0800473 const std::string nameprefix,
474 const std::string namesuffix, bool has_field_name,
475 const std::string fieldname_suffix,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700476 std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700477 for (auto it = struct_def.fields.vec.begin();
Austin Schuh272c6132020-11-14 16:37:52 -0800478 it != struct_def.fields.vec.end(); ++it) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700479 auto &field = **it;
480 const auto &field_type = field.value.type;
481 const auto &type =
482 IsArray(field_type) ? field_type.VectorType() : field_type;
483 if (IsStruct(type)) {
484 // Generate arguments for a struct inside a struct. To ensure names
485 // don't clash, and to make it obvious these arguments are constructing
486 // a nested struct, prefix the name with the field name.
Austin Schuh272c6132020-11-14 16:37:52 -0800487 auto subprefix = nameprefix;
488 if (has_field_name) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700489 subprefix += namer_.Field(field) + fieldname_suffix;
Austin Schuh272c6132020-11-14 16:37:52 -0800490 }
491 StructBuilderArgs(*field.value.type.struct_def, subprefix, namesuffix,
492 has_field_name, fieldname_suffix, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700493 } else {
Austin Schuh272c6132020-11-14 16:37:52 -0800494 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700495 code += std::string(", ") + nameprefix;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700496 if (has_field_name) { code += namer_.Field(field); }
Austin Schuh272c6132020-11-14 16:37:52 -0800497 code += namesuffix;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700498 }
499 }
500 }
501
502 // End the creator function signature.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700503 void EndBuilderArgs(std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800504 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700505 code += "):\n";
506 }
507
508 // Recursively generate struct construction statements and instert manual
509 // padding.
510 void StructBuilderBody(const StructDef &struct_def, const char *nameprefix,
511 std::string *code_ptr, size_t index = 0,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700512 bool in_array = false) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800513 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700514 std::string indent(index * 4, ' ');
515 code +=
516 indent + " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
517 code += NumToString(struct_def.bytesize) + ")\n";
518 for (auto it = struct_def.fields.vec.rbegin();
Austin Schuh272c6132020-11-14 16:37:52 -0800519 it != struct_def.fields.vec.rend(); ++it) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700520 auto &field = **it;
521 const auto &field_type = field.value.type;
522 const auto &type =
523 IsArray(field_type) ? field_type.VectorType() : field_type;
524 if (field.padding)
525 code +=
526 indent + " builder.Pad(" + NumToString(field.padding) + ")\n";
527 if (IsStruct(field_type)) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700528 StructBuilderBody(*field_type.struct_def,
529 (nameprefix + (namer_.Field(field) + "_")).c_str(),
530 code_ptr, index, in_array);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700531 } else {
532 const auto index_var = "_idx" + NumToString(index);
533 if (IsArray(field_type)) {
534 code += indent + " for " + index_var + " in range(";
535 code += NumToString(field_type.fixed_length);
536 code += " , 0, -1):\n";
537 in_array = true;
538 }
539 if (IsStruct(type)) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700540 StructBuilderBody(*field_type.struct_def,
541 (nameprefix + (namer_.Field(field) + "_")).c_str(),
542 code_ptr, index + 1, in_array);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700543 } else {
544 code += IsArray(field_type) ? " " : "";
545 code += indent + " builder.Prepend" + GenMethod(field) + "(";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700546 code += nameprefix + namer_.Variable(field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700547 size_t array_cnt = index + (IsArray(field_type) ? 1 : 0);
548 for (size_t i = 0; in_array && i < array_cnt; i++) {
549 code += "[_idx" + NumToString(i) + "-1]";
550 }
551 code += ")\n";
552 }
553 }
554 }
555 }
556
James Kuszmaul8e62b022022-03-22 09:33:25 -0700557 void EndBuilderBody(std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800558 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700559 code += " return builder.Offset()\n";
560 }
561
562 // Get the value of a table's starting offset.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700563 void GetStartOfTable(const StructDef &struct_def,
564 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800565 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700566 const auto struct_type = namer_.Type(struct_def);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700567 // Generate method with struct name.
568 code += "def " + struct_type + "Start(builder): ";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700569 code += "builder.StartObject(";
570 code += NumToString(struct_def.fields.vec.size());
571 code += ")\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700572
573 if (!parser_.opts.one_file) {
574 // Generate method without struct name.
575 code += "def Start(builder):\n";
576 code += Indent + "return " + struct_type + "Start(builder)\n";
577 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700578 }
579
580 // Set the value of a table's field.
Austin Schuh272c6132020-11-14 16:37:52 -0800581 void BuildFieldOfTable(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700582 const size_t offset, std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800583 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700584 const std::string field_var = namer_.Variable(field);
585 const std::string field_method = namer_.Method(field);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700586
587 // Generate method with struct name.
Austin Schuh2dd86a92022-09-14 21:19:23 -0700588 code += "def " + namer_.Type(struct_def) + "Add" + field_method;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700589 code += "(builder, ";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700590 code += field_var;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700591 code += "): ";
592 code += "builder.Prepend";
593 code += GenMethod(field) + "Slot(";
594 code += NumToString(offset) + ", ";
595 if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
596 code += "flatbuffers.number_types.UOffsetTFlags.py_type";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700597 code += "(" + field_var + ")";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700598 } else {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700599 code += field_var;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700600 }
601 code += ", ";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700602 if (field.IsScalarOptional()) {
603 code += "None";
604 } else if (IsFloat(field.value.type.base_type)) {
605 code += float_const_gen_.GenFloatConstant(field);
606 } else {
607 code += field.value.constant;
608 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700609 code += ")\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700610
611 if (!parser_.opts.one_file) {
612 // Generate method without struct name.
613 code += "def Add" + field_method + "(builder, " + field_var + "):\n";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700614 code +=
615 Indent + "return " + namer_.Type(struct_def) + "Add" + field_method;
James Kuszmaul8e62b022022-03-22 09:33:25 -0700616 code += "(builder, ";
617 code += field_var;
618 code += ")\n";
619 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700620 }
621
622 // Set the value of one of the members of a table's vector.
Austin Schuh272c6132020-11-14 16:37:52 -0800623 void BuildVectorOfTable(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700624 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800625 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700626 const std::string struct_type = namer_.Type(struct_def);
627 const std::string field_method = namer_.Method(field);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700628
629 // Generate method with struct name.
630 code += "def " + struct_type + "Start" + field_method;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700631 code += "Vector(builder, numElems): return builder.StartVector(";
632 auto vector_type = field.value.type.VectorType();
633 auto alignment = InlineAlignment(vector_type);
634 auto elem_size = InlineSize(vector_type);
635 code += NumToString(elem_size);
636 code += ", numElems, " + NumToString(alignment);
637 code += ")\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700638
639 if (!parser_.opts.one_file) {
640 // Generate method without struct name.
641 code += "def Start" + field_method + "Vector(builder, numElems):\n";
642 code += Indent + "return " + struct_type + "Start";
643 code += field_method + "Vector(builder, numElems)\n";
644 }
645 }
646
647 // Set the value of one of the members of a table's vector and fills in the
648 // elements from a bytearray. This is for simplifying the use of nested
649 // flatbuffers.
650 void BuildVectorOfTableFromBytes(const StructDef &struct_def,
651 const FieldDef &field,
652 std::string *code_ptr) const {
653 auto nested = field.attributes.Lookup("nested_flatbuffer");
654 if (!nested) { return; } // There is no nested flatbuffer.
655
656 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700657 const std::string field_method = namer_.Method(field);
658 const std::string struct_type = namer_.Type(struct_def);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700659
660 // Generate method with struct and field name.
661 code += "def " + struct_type + "Make" + field_method;
662 code += "VectorFromBytes(builder, bytes):\n";
663 code += Indent + "builder.StartVector(";
664 auto vector_type = field.value.type.VectorType();
665 auto alignment = InlineAlignment(vector_type);
666 auto elem_size = InlineSize(vector_type);
667 code += NumToString(elem_size);
668 code += ", len(bytes), " + NumToString(alignment);
669 code += ")\n";
670 code += Indent + "builder.head = builder.head - len(bytes)\n";
671 code += Indent + "builder.Bytes[builder.head : builder.head + len(bytes)]";
672 code += " = bytes\n";
673 code += Indent + "return builder.EndVector()\n";
674
675 if (!parser_.opts.one_file) {
676 // Generate method without struct and field name.
677 code += "def Make" + field_method + "VectorFromBytes(builder, bytes):\n";
678 code += Indent + "return " + struct_type + "Make" + field_method +
679 "VectorFromBytes(builder, bytes)\n";
680 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700681 }
682
683 // Get the offset of the end of a table.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700684 void GetEndOffsetOnTable(const StructDef &struct_def,
685 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800686 auto &code = *code_ptr;
James Kuszmaul8e62b022022-03-22 09:33:25 -0700687
688 // Generate method with struct name.
Austin Schuh2dd86a92022-09-14 21:19:23 -0700689 code += "def " + namer_.Type(struct_def) + "End";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700690 code += "(builder): ";
691 code += "return builder.EndObject()\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700692
693 if (!parser_.opts.one_file) {
694 // Generate method without struct name.
695 code += "def End(builder):\n";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700696 code += Indent + "return " + namer_.Type(struct_def) + "End(builder)";
James Kuszmaul8e62b022022-03-22 09:33:25 -0700697 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700698 }
699
700 // Generate the receiver for function signatures.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700701 void GenReceiver(const StructDef &struct_def, std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800702 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700703 code += Indent + "# " + namer_.Type(struct_def) + "\n";
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700704 code += Indent + "def ";
705 }
706
707 // Generate a struct field, conditioned on its child type(s).
Austin Schuh272c6132020-11-14 16:37:52 -0800708 void GenStructAccessor(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700709 std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700710 GenComment(field.doc_comment, code_ptr, &def_comment, Indent.c_str());
711 if (IsScalar(field.value.type.base_type)) {
712 if (struct_def.fixed) {
713 GetScalarFieldOfStruct(struct_def, field, code_ptr);
714 } else {
715 GetScalarFieldOfTable(struct_def, field, code_ptr);
716 }
717 } else if (IsArray(field.value.type)) {
718 GetArrayOfStruct(struct_def, field, code_ptr);
719 } else {
720 switch (field.value.type.base_type) {
721 case BASE_TYPE_STRUCT:
722 if (struct_def.fixed) {
723 GetStructFieldOfStruct(struct_def, field, code_ptr);
724 } else {
725 GetStructFieldOfTable(struct_def, field, code_ptr);
726 }
727 break;
Austin Schuh272c6132020-11-14 16:37:52 -0800728 case BASE_TYPE_STRING:
729 GetStringField(struct_def, field, code_ptr);
730 break;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700731 case BASE_TYPE_VECTOR: {
732 auto vectortype = field.value.type.VectorType();
733 if (vectortype.base_type == BASE_TYPE_STRUCT) {
734 GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
735 } else {
736 GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
737 GetVectorOfNonStructAsNumpy(struct_def, field, code_ptr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700738 GetVectorAsNestedFlatbuffer(struct_def, field, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700739 }
740 break;
741 }
742 case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
743 default: FLATBUFFERS_ASSERT(0);
744 }
745 }
Austin Schuh272c6132020-11-14 16:37:52 -0800746 if (IsVector(field.value.type) || IsArray(field.value.type)) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700747 GetVectorLen(struct_def, field, code_ptr);
Austin Schuh272c6132020-11-14 16:37:52 -0800748 GetVectorIsNone(struct_def, field, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700749 }
750 }
751
Austin Schuh272c6132020-11-14 16:37:52 -0800752 // Generate struct sizeof.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700753 void GenStructSizeOf(const StructDef &struct_def,
754 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800755 auto &code = *code_ptr;
756 code += Indent + "@classmethod\n";
757 code += Indent + "def SizeOf(cls):\n";
758 code +=
759 Indent + Indent + "return " + NumToString(struct_def.bytesize) + "\n";
760 code += "\n";
761 }
762
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700763 // Generate table constructors, conditioned on its members' types.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700764 void GenTableBuilders(const StructDef &struct_def,
765 std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700766 GetStartOfTable(struct_def, code_ptr);
767
768 for (auto it = struct_def.fields.vec.begin();
Austin Schuh272c6132020-11-14 16:37:52 -0800769 it != struct_def.fields.vec.end(); ++it) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700770 auto &field = **it;
771 if (field.deprecated) continue;
772
773 auto offset = it - struct_def.fields.vec.begin();
774 BuildFieldOfTable(struct_def, field, offset, code_ptr);
Austin Schuh272c6132020-11-14 16:37:52 -0800775 if (IsVector(field.value.type)) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700776 BuildVectorOfTable(struct_def, field, code_ptr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700777 BuildVectorOfTableFromBytes(struct_def, field, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700778 }
779 }
780
781 GetEndOffsetOnTable(struct_def, code_ptr);
782 }
783
784 // Generate function to check for proper file identifier
785 void GenHasFileIdentifier(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700786 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800787 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700788 std::string escapedID;
789 // In the event any of file_identifier characters are special(NULL, \, etc),
790 // problems occur. To prevent this, convert all chars to their hex-escaped
791 // equivalent.
792 for (auto it = parser_.file_identifier_.begin();
793 it != parser_.file_identifier_.end(); ++it) {
794 escapedID += "\\x" + IntToStringHex(*it, 2);
795 }
796
797 code += Indent + "@classmethod\n";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700798 code += Indent + "def " + namer_.Type(struct_def);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700799 code += "BufferHasIdentifier(cls, buf, offset, size_prefixed=False):";
800 code += "\n";
801 code += Indent + Indent;
802 code += "return flatbuffers.util.BufferHasIdentifier(buf, offset, b\"";
803 code += escapedID;
804 code += "\", size_prefixed=size_prefixed)\n";
805 code += "\n";
806 }
Austin Schuh272c6132020-11-14 16:37:52 -0800807
808 // Generates struct or table methods.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700809 void GenStruct(const StructDef &struct_def, std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700810 if (struct_def.generated) return;
811
812 GenComment(struct_def.doc_comment, code_ptr, &def_comment);
813 BeginClass(struct_def, code_ptr);
814 if (!struct_def.fixed) {
815 // Generate a special accessor for the table that has been declared as
816 // the root type.
817 NewRootTypeFromBuffer(struct_def, code_ptr);
Austin Schuh272c6132020-11-14 16:37:52 -0800818 if (parser_.file_identifier_.length()) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700819 // Generate a special function to test file_identifier
820 GenHasFileIdentifier(struct_def, code_ptr);
821 }
Austin Schuh272c6132020-11-14 16:37:52 -0800822 } else {
823 // Generates the SizeOf method for all structs.
824 GenStructSizeOf(struct_def, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700825 }
Austin Schuh272c6132020-11-14 16:37:52 -0800826 // Generates the Init method that sets the field in a pre-existing
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700827 // accessor object. This is to allow object reuse.
828 InitializeExisting(struct_def, code_ptr);
829 for (auto it = struct_def.fields.vec.begin();
Austin Schuh272c6132020-11-14 16:37:52 -0800830 it != struct_def.fields.vec.end(); ++it) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700831 auto &field = **it;
832 if (field.deprecated) continue;
833
834 GenStructAccessor(struct_def, field, code_ptr);
835 }
836
837 if (struct_def.fixed) {
Austin Schuh272c6132020-11-14 16:37:52 -0800838 // creates a struct constructor function
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700839 GenStructBuilder(struct_def, code_ptr);
840 } else {
Austin Schuh272c6132020-11-14 16:37:52 -0800841 // Creates a set of functions that allow table construction.
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700842 GenTableBuilders(struct_def, code_ptr);
843 }
844 }
845
Austin Schuh272c6132020-11-14 16:37:52 -0800846 void GenReceiverForObjectAPI(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700847 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800848 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700849 code += GenIndents(1) + "# " + namer_.ObjectType(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800850 code += GenIndents(1) + "def ";
851 }
852
853 void BeginClassForObjectAPI(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700854 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800855 auto &code = *code_ptr;
856 code += "\n";
Austin Schuh2dd86a92022-09-14 21:19:23 -0700857 code += "class " + namer_.ObjectType(struct_def) + "(object):";
Austin Schuh272c6132020-11-14 16:37:52 -0800858 code += "\n";
859 }
860
861 // Gets the accoresponding python builtin type of a BaseType for scalars and
862 // string.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700863 std::string GetBasePythonTypeForScalarAndString(
864 const BaseType &base_type) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800865 if (IsBool(base_type)) {
866 return "bool";
867 } else if (IsFloat(base_type)) {
868 return "float";
869 } else if (IsInteger(base_type)) {
870 return "int";
871 } else if (base_type == BASE_TYPE_STRING) {
872 return "str";
873 } else {
874 FLATBUFFERS_ASSERT(false && "base_type is not a scalar or string type.");
875 return "";
876 }
877 }
878
James Kuszmaul8e62b022022-03-22 09:33:25 -0700879 std::string GetDefaultValue(const FieldDef &field) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800880 BaseType base_type = field.value.type.base_type;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700881 if (field.IsScalarOptional()) {
882 return "None";
883 } else if (IsBool(base_type)) {
Austin Schuh272c6132020-11-14 16:37:52 -0800884 return field.value.constant == "0" ? "False" : "True";
885 } else if (IsFloat(base_type)) {
886 return float_const_gen_.GenFloatConstant(field);
887 } else if (IsInteger(base_type)) {
888 return field.value.constant;
889 } else {
890 // For string, struct, and table.
891 return "None";
892 }
893 }
894
895 void GenUnionInit(const FieldDef &field, std::string *field_types_ptr,
896 std::set<std::string> *import_list,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700897 std::set<std::string> *import_typing_list) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800898 // Gets all possible types in the union.
899 import_typing_list->insert("Union");
900 auto &field_types = *field_types_ptr;
901 field_types = "Union[";
902
903 std::string separator_string = ", ";
904 auto enum_def = field.value.type.enum_def;
905 for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
906 ++it) {
907 auto &ev = **it;
908 // Union only supports string and table.
909 std::string field_type;
910 switch (ev.union_type.base_type) {
911 case BASE_TYPE_STRUCT:
Austin Schuh2dd86a92022-09-14 21:19:23 -0700912 field_type = namer_.ObjectType(*ev.union_type.struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800913 if (parser_.opts.include_dependence_headers) {
914 auto package_reference = GenPackageReference(ev.union_type);
915 field_type = package_reference + "." + field_type;
916 import_list->insert("import " + package_reference);
917 }
918 break;
919 case BASE_TYPE_STRING: field_type += "str"; break;
920 case BASE_TYPE_NONE: field_type += "None"; break;
921 default: break;
922 }
923 field_types += field_type + separator_string;
924 }
925
926 // Removes the last separator_string.
927 field_types.erase(field_types.length() - separator_string.size());
928 field_types += "]";
929
930 // Gets the import lists for the union.
931 if (parser_.opts.include_dependence_headers) {
Austin Schuh2dd86a92022-09-14 21:19:23 -0700932 const auto package_reference = GenPackageReference(field.value.type);
Austin Schuh272c6132020-11-14 16:37:52 -0800933 import_list->insert("import " + package_reference);
934 }
935 }
936
James Kuszmaul8e62b022022-03-22 09:33:25 -0700937 void GenStructInit(const FieldDef &field, std::string *out_ptr,
Austin Schuh272c6132020-11-14 16:37:52 -0800938 std::set<std::string> *import_list,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700939 std::set<std::string> *import_typing_list) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800940 import_typing_list->insert("Optional");
James Kuszmaul8e62b022022-03-22 09:33:25 -0700941 auto &output = *out_ptr;
942 const Type &type = field.value.type;
Austin Schuh2dd86a92022-09-14 21:19:23 -0700943 const std::string object_type = namer_.ObjectType(*type.struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800944 if (parser_.opts.include_dependence_headers) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700945 auto package_reference = GenPackageReference(type);
946 output = package_reference + "." + object_type + "]";
Austin Schuh272c6132020-11-14 16:37:52 -0800947 import_list->insert("import " + package_reference);
948 } else {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700949 output = object_type + "]";
Austin Schuh272c6132020-11-14 16:37:52 -0800950 }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700951 output = "Optional[" + output;
Austin Schuh272c6132020-11-14 16:37:52 -0800952 }
953
954 void GenVectorInit(const FieldDef &field, std::string *field_type_ptr,
955 std::set<std::string> *import_list,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700956 std::set<std::string> *import_typing_list) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800957 import_typing_list->insert("List");
958 auto &field_type = *field_type_ptr;
James Kuszmaul8e62b022022-03-22 09:33:25 -0700959 const Type &vector_type = field.value.type.VectorType();
960 const BaseType base_type = vector_type.base_type;
Austin Schuh272c6132020-11-14 16:37:52 -0800961 if (base_type == BASE_TYPE_STRUCT) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700962 const std::string object_type =
Austin Schuh2dd86a92022-09-14 21:19:23 -0700963 namer_.ObjectType(*vector_type.struct_def);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700964 field_type = object_type + "]";
Austin Schuh272c6132020-11-14 16:37:52 -0800965 if (parser_.opts.include_dependence_headers) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700966 auto package_reference = GenPackageReference(vector_type);
967 field_type = package_reference + "." + object_type + "]";
Austin Schuh272c6132020-11-14 16:37:52 -0800968 import_list->insert("import " + package_reference);
969 }
970 field_type = "List[" + field_type;
971 } else {
972 field_type =
973 "List[" + GetBasePythonTypeForScalarAndString(base_type) + "]";
974 }
975 }
976
977 void GenInitialize(const StructDef &struct_def, std::string *code_ptr,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700978 std::set<std::string> *import_list) const {
Austin Schuh272c6132020-11-14 16:37:52 -0800979 std::string code;
980 std::set<std::string> import_typing_list;
981 for (auto it = struct_def.fields.vec.begin();
982 it != struct_def.fields.vec.end(); ++it) {
983 auto &field = **it;
984 if (field.deprecated) continue;
985
986 // Determines field type, default value, and typing imports.
987 auto base_type = field.value.type.base_type;
988 std::string field_type;
989 switch (base_type) {
990 case BASE_TYPE_UNION: {
991 GenUnionInit(field, &field_type, import_list, &import_typing_list);
992 break;
993 }
994 case BASE_TYPE_STRUCT: {
995 GenStructInit(field, &field_type, import_list, &import_typing_list);
996 break;
997 }
998 case BASE_TYPE_VECTOR:
999 case BASE_TYPE_ARRAY: {
1000 GenVectorInit(field, &field_type, import_list, &import_typing_list);
1001 break;
1002 }
1003 default:
1004 // Scalar or sting fields.
1005 field_type = GetBasePythonTypeForScalarAndString(base_type);
Austin Schuh2dd86a92022-09-14 21:19:23 -07001006 if (field.IsScalarOptional()) {
1007 field_type = "Optional[" + field_type + "]";
1008 }
Austin Schuh272c6132020-11-14 16:37:52 -08001009 break;
1010 }
1011
James Kuszmaul8e62b022022-03-22 09:33:25 -07001012 const auto default_value = GetDefaultValue(field);
Austin Schuh272c6132020-11-14 16:37:52 -08001013 // Wrties the init statement.
Austin Schuh2dd86a92022-09-14 21:19:23 -07001014 const auto field_field = namer_.Field(field);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001015 code += GenIndents(2) + "self." + field_field + " = " + default_value +
1016 " # type: " + field_type;
Austin Schuh272c6132020-11-14 16:37:52 -08001017 }
1018
1019 // Writes __init__ method.
1020 auto &code_base = *code_ptr;
1021 GenReceiverForObjectAPI(struct_def, code_ptr);
1022 code_base += "__init__(self):";
1023 if (code.empty()) {
1024 code_base += GenIndents(2) + "pass";
1025 } else {
1026 code_base += code;
1027 }
1028 code_base += "\n";
1029
1030 // Merges the typing imports into import_list.
1031 if (!import_typing_list.empty()) {
1032 // Adds the try statement.
1033 std::string typing_imports = "try:";
1034 typing_imports += GenIndents(1) + "from typing import ";
1035 std::string separator_string = ", ";
1036 for (auto it = import_typing_list.begin(); it != import_typing_list.end();
1037 ++it) {
1038 const std::string &im = *it;
1039 typing_imports += im + separator_string;
1040 }
1041 // Removes the last separator_string.
1042 typing_imports.erase(typing_imports.length() - separator_string.size());
1043
1044 // Adds the except statement.
1045 typing_imports += "\n";
1046 typing_imports += "except:";
1047 typing_imports += GenIndents(1) + "pass";
1048 import_list->insert(typing_imports);
1049 }
1050
1051 // Removes the import of the struct itself, if applied.
Austin Schuh2dd86a92022-09-14 21:19:23 -07001052 auto struct_import = "import " + namer_.NamespacedType(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001053 import_list->erase(struct_import);
1054 }
1055
James Kuszmaul8e62b022022-03-22 09:33:25 -07001056 void InitializeFromBuf(const StructDef &struct_def,
1057 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001058 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001059 const auto struct_var = namer_.Variable(struct_def);
1060 const auto struct_type = namer_.Type(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001061
1062 code += GenIndents(1) + "@classmethod";
1063 code += GenIndents(1) + "def InitFromBuf(cls, buf, pos):";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001064 code += GenIndents(2) + struct_var + " = " + struct_type + "()";
1065 code += GenIndents(2) + struct_var + ".Init(buf, pos)";
1066 code += GenIndents(2) + "return cls.InitFromObj(" + struct_var + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001067 code += "\n";
1068 }
1069
1070 void InitializeFromObjForObject(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001071 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001072 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001073 const auto struct_var = namer_.Variable(struct_def);
1074 const auto struct_object = namer_.ObjectType(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001075
1076 code += GenIndents(1) + "@classmethod";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001077 code += GenIndents(1) + "def InitFromObj(cls, " + struct_var + "):";
1078 code += GenIndents(2) + "x = " + struct_object + "()";
1079 code += GenIndents(2) + "x._UnPack(" + struct_var + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001080 code += GenIndents(2) + "return x";
1081 code += "\n";
1082 }
1083
1084 void GenUnPackForStruct(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001085 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001086 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001087 const auto struct_var = namer_.Variable(struct_def);
1088 const auto field_field = namer_.Field(field);
1089 const auto field_method = namer_.Method(field);
Austin Schuh272c6132020-11-14 16:37:52 -08001090 auto field_type = TypeName(field);
1091
1092 if (parser_.opts.include_dependence_headers) {
1093 auto package_reference = GenPackageReference(field.value.type);
1094 field_type = package_reference + "." + TypeName(field);
1095 }
1096
James Kuszmaul8e62b022022-03-22 09:33:25 -07001097 code += GenIndents(2) + "if " + struct_var + "." + field_method + "(";
Austin Schuh272c6132020-11-14 16:37:52 -08001098 // if field is a struct, we need to create an instance for it first.
1099 if (struct_def.fixed && field.value.type.base_type == BASE_TYPE_STRUCT) {
1100 code += field_type + "()";
1101 }
1102 code += ") is not None:";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001103 code += GenIndents(3) + "self." + field_field + " = " + field_type +
1104 "T.InitFromObj(" + struct_var + "." + field_method + "(";
Austin Schuh272c6132020-11-14 16:37:52 -08001105 // A struct's accessor requires a struct buf instance.
1106 if (struct_def.fixed && field.value.type.base_type == BASE_TYPE_STRUCT) {
1107 code += field_type + "()";
1108 }
1109 code += "))";
1110 }
1111
1112 void GenUnPackForUnion(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001113 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001114 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001115 const auto field_field = namer_.Field(field);
1116 const auto field_method = namer_.Method(field);
1117 const auto struct_var = namer_.Variable(struct_def);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001118 const EnumDef &enum_def = *field.value.type.enum_def;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001119 auto union_type = namer_.Type(enum_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001120
1121 if (parser_.opts.include_dependence_headers) {
Austin Schuh2dd86a92022-09-14 21:19:23 -07001122 union_type = namer_.NamespacedType(enum_def) + "." + union_type;
Austin Schuh272c6132020-11-14 16:37:52 -08001123 }
James Kuszmaul8e62b022022-03-22 09:33:25 -07001124 code += GenIndents(2) + "self." + field_field + " = " + union_type +
1125 "Creator(" + "self." + field_field + "Type, " + struct_var + "." +
1126 field_method + "())";
Austin Schuh272c6132020-11-14 16:37:52 -08001127 }
1128
1129 void GenUnPackForStructVector(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001130 const FieldDef &field,
1131 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001132 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001133 const auto field_field = namer_.Field(field);
1134 const auto field_method = namer_.Method(field);
1135 const auto struct_var = namer_.Variable(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001136
James Kuszmaul8e62b022022-03-22 09:33:25 -07001137 code += GenIndents(2) + "if not " + struct_var + "." + field_method +
1138 "IsNone():";
1139 code += GenIndents(3) + "self." + field_field + " = []";
1140 code += GenIndents(3) + "for i in range(" + struct_var + "." +
1141 field_method + "Length()):";
Austin Schuh272c6132020-11-14 16:37:52 -08001142
James Kuszmaul8e62b022022-03-22 09:33:25 -07001143 auto field_type = TypeName(field);
1144 auto one_instance = field_type + "_";
Austin Schuh272c6132020-11-14 16:37:52 -08001145 one_instance[0] = CharToLower(one_instance[0]);
1146
1147 if (parser_.opts.include_dependence_headers) {
1148 auto package_reference = GenPackageReference(field.value.type);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001149 field_type = package_reference + "." + TypeName(field);
Austin Schuh272c6132020-11-14 16:37:52 -08001150 }
1151
James Kuszmaul8e62b022022-03-22 09:33:25 -07001152 code += GenIndents(4) + "if " + struct_var + "." + field_method +
1153 "(i) is None:";
1154 code += GenIndents(5) + "self." + field_field + ".append(None)";
Austin Schuh272c6132020-11-14 16:37:52 -08001155 code += GenIndents(4) + "else:";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001156 code += GenIndents(5) + one_instance + " = " + field_type +
1157 "T.InitFromObj(" + struct_var + "." + field_method + "(i))";
1158 code +=
1159 GenIndents(5) + "self." + field_field + ".append(" + one_instance + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001160 }
1161
1162 void GenUnpackforScalarVectorHelper(const StructDef &struct_def,
1163 const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001164 std::string *code_ptr,
1165 int indents) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001166 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001167 const auto field_field = namer_.Field(field);
1168 const auto field_method = namer_.Method(field);
1169 const auto struct_var = namer_.Variable(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001170
James Kuszmaul8e62b022022-03-22 09:33:25 -07001171 code += GenIndents(indents) + "self." + field_field + " = []";
1172 code += GenIndents(indents) + "for i in range(" + struct_var + "." +
1173 field_method + "Length()):";
1174 code += GenIndents(indents + 1) + "self." + field_field + ".append(" +
1175 struct_var + "." + field_method + "(i))";
Austin Schuh272c6132020-11-14 16:37:52 -08001176 }
1177
1178 void GenUnPackForScalarVector(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001179 const FieldDef &field,
1180 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001181 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001182 const auto field_field = namer_.Field(field);
1183 const auto field_method = namer_.Method(field);
1184 const auto struct_var = namer_.Variable(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001185
James Kuszmaul8e62b022022-03-22 09:33:25 -07001186 code += GenIndents(2) + "if not " + struct_var + "." + field_method +
1187 "IsNone():";
Austin Schuh272c6132020-11-14 16:37:52 -08001188
1189 // String does not have the AsNumpy method.
1190 if (!(IsScalar(field.value.type.VectorType().base_type))) {
1191 GenUnpackforScalarVectorHelper(struct_def, field, code_ptr, 3);
1192 return;
1193 }
1194
1195 code += GenIndents(3) + "if np is None:";
1196 GenUnpackforScalarVectorHelper(struct_def, field, code_ptr, 4);
1197
1198 // If numpy exists, use the AsNumpy method to optimize the unpack speed.
1199 code += GenIndents(3) + "else:";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001200 code += GenIndents(4) + "self." + field_field + " = " + struct_var + "." +
1201 field_method + "AsNumpy()";
Austin Schuh272c6132020-11-14 16:37:52 -08001202 }
1203
1204 void GenUnPackForScalar(const StructDef &struct_def, const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001205 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001206 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001207 const auto field_field = namer_.Field(field);
1208 const auto field_method = namer_.Method(field);
1209 const auto struct_var = namer_.Variable(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001210
James Kuszmaul8e62b022022-03-22 09:33:25 -07001211 code += GenIndents(2) + "self." + field_field + " = " + struct_var + "." +
1212 field_method + "()";
Austin Schuh272c6132020-11-14 16:37:52 -08001213 }
1214
1215 // Generates the UnPack method for the object class.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001216 void GenUnPack(const StructDef &struct_def, std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001217 std::string code;
1218 // Items that needs to be imported. No duplicate modules will be imported.
1219 std::set<std::string> import_list;
1220
1221 for (auto it = struct_def.fields.vec.begin();
1222 it != struct_def.fields.vec.end(); ++it) {
1223 auto &field = **it;
1224 if (field.deprecated) continue;
1225
1226 auto field_type = TypeName(field);
1227 switch (field.value.type.base_type) {
1228 case BASE_TYPE_STRUCT: {
1229 GenUnPackForStruct(struct_def, field, &code);
1230 break;
1231 }
1232 case BASE_TYPE_UNION: {
1233 GenUnPackForUnion(struct_def, field, &code);
1234 break;
1235 }
1236 case BASE_TYPE_VECTOR: {
1237 auto vectortype = field.value.type.VectorType();
1238 if (vectortype.base_type == BASE_TYPE_STRUCT) {
1239 GenUnPackForStructVector(struct_def, field, &code);
1240 } else {
1241 GenUnPackForScalarVector(struct_def, field, &code);
1242 }
1243 break;
1244 }
1245 case BASE_TYPE_ARRAY: {
1246 GenUnPackForScalarVector(struct_def, field, &code);
1247 break;
1248 }
1249 default: GenUnPackForScalar(struct_def, field, &code);
1250 }
1251 }
1252
1253 // Writes import statements and code into the generated file.
1254 auto &code_base = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001255 const auto struct_var = namer_.Variable(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001256
1257 GenReceiverForObjectAPI(struct_def, code_ptr);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001258 code_base += "_UnPack(self, " + struct_var + "):";
1259 code_base += GenIndents(2) + "if " + struct_var + " is None:";
Austin Schuh272c6132020-11-14 16:37:52 -08001260 code_base += GenIndents(3) + "return";
1261
1262 // Write the import statements.
1263 for (std::set<std::string>::iterator it = import_list.begin();
1264 it != import_list.end(); ++it) {
1265 code_base += GenIndents(2) + *it;
1266 }
1267
1268 // Write the code.
1269 code_base += code;
1270 code_base += "\n";
1271 }
1272
James Kuszmaul8e62b022022-03-22 09:33:25 -07001273 void GenPackForStruct(const StructDef &struct_def,
1274 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001275 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001276 const auto struct_fn = namer_.Function(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001277
1278 GenReceiverForObjectAPI(struct_def, code_ptr);
1279 code += "Pack(self, builder):";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001280 code += GenIndents(2) + "return Create" + struct_fn + "(builder";
Austin Schuh272c6132020-11-14 16:37:52 -08001281
1282 StructBuilderArgs(struct_def,
1283 /* nameprefix = */ "self.",
1284 /* namesuffix = */ "",
1285 /* has_field_name = */ true,
1286 /* fieldname_suffix = */ ".", code_ptr);
1287 code += ")\n";
1288 }
1289
1290 void GenPackForStructVectorField(const StructDef &struct_def,
1291 const FieldDef &field,
1292 std::string *code_prefix_ptr,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001293 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001294 auto &code_prefix = *code_prefix_ptr;
1295 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001296 const auto field_field = namer_.Field(field);
1297 const auto struct_type = namer_.Type(struct_def);
1298 const auto field_method = namer_.Method(field);
Austin Schuh272c6132020-11-14 16:37:52 -08001299
1300 // Creates the field.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001301 code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
Austin Schuh272c6132020-11-14 16:37:52 -08001302 if (field.value.type.struct_def->fixed) {
James Kuszmaul8e62b022022-03-22 09:33:25 -07001303 code_prefix += GenIndents(3) + struct_type + "Start" + field_method +
1304 "Vector(builder, len(self." + field_field + "))";
Austin Schuh272c6132020-11-14 16:37:52 -08001305 code_prefix += GenIndents(3) + "for i in reversed(range(len(self." +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001306 field_field + "))):";
Austin Schuh272c6132020-11-14 16:37:52 -08001307 code_prefix +=
James Kuszmaul8e62b022022-03-22 09:33:25 -07001308 GenIndents(4) + "self." + field_field + "[i].Pack(builder)";
1309 code_prefix += GenIndents(3) + field_field + " = builder.EndVector()";
Austin Schuh272c6132020-11-14 16:37:52 -08001310 } else {
1311 // If the vector is a struct vector, we need to first build accessor for
1312 // each struct element.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001313 code_prefix += GenIndents(3) + field_field + "list = []";
Austin Schuh272c6132020-11-14 16:37:52 -08001314 code_prefix += GenIndents(3);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001315 code_prefix += "for i in range(len(self." + field_field + ")):";
1316 code_prefix += GenIndents(4) + field_field + "list.append(self." +
1317 field_field + "[i].Pack(builder))";
Austin Schuh272c6132020-11-14 16:37:52 -08001318
James Kuszmaul8e62b022022-03-22 09:33:25 -07001319 code_prefix += GenIndents(3) + struct_type + "Start" + field_method +
1320 "Vector(builder, len(self." + field_field + "))";
Austin Schuh272c6132020-11-14 16:37:52 -08001321 code_prefix += GenIndents(3) + "for i in reversed(range(len(self." +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001322 field_field + "))):";
Austin Schuh272c6132020-11-14 16:37:52 -08001323 code_prefix += GenIndents(4) + "builder.PrependUOffsetTRelative" + "(" +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001324 field_field + "list[i])";
1325 code_prefix += GenIndents(3) + field_field + " = builder.EndVector()";
Austin Schuh272c6132020-11-14 16:37:52 -08001326 }
1327
1328 // Adds the field into the struct.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001329 code += GenIndents(2) + "if self." + field_field + " is not None:";
1330 code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " +
1331 field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001332 }
1333
1334 void GenPackForScalarVectorFieldHelper(const StructDef &struct_def,
1335 const FieldDef &field,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001336 std::string *code_ptr,
1337 int indents) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001338 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001339 const auto field_field = namer_.Field(field);
1340 const auto field_method = namer_.Method(field);
1341 const auto struct_type = namer_.Type(struct_def);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001342 const auto vectortype = field.value.type.VectorType();
Austin Schuh272c6132020-11-14 16:37:52 -08001343
James Kuszmaul8e62b022022-03-22 09:33:25 -07001344 code += GenIndents(indents) + struct_type + "Start" + field_method +
1345 "Vector(builder, len(self." + field_field + "))";
Austin Schuh272c6132020-11-14 16:37:52 -08001346 code += GenIndents(indents) + "for i in reversed(range(len(self." +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001347 field_field + "))):";
Austin Schuh272c6132020-11-14 16:37:52 -08001348 code += GenIndents(indents + 1) + "builder.Prepend";
1349
1350 std::string type_name;
1351 switch (vectortype.base_type) {
1352 case BASE_TYPE_BOOL: type_name = "Bool"; break;
1353 case BASE_TYPE_CHAR: type_name = "Byte"; break;
1354 case BASE_TYPE_UCHAR: type_name = "Uint8"; break;
1355 case BASE_TYPE_SHORT: type_name = "Int16"; break;
1356 case BASE_TYPE_USHORT: type_name = "Uint16"; break;
1357 case BASE_TYPE_INT: type_name = "Int32"; break;
1358 case BASE_TYPE_UINT: type_name = "Uint32"; break;
1359 case BASE_TYPE_LONG: type_name = "Int64"; break;
1360 case BASE_TYPE_ULONG: type_name = "Uint64"; break;
1361 case BASE_TYPE_FLOAT: type_name = "Float32"; break;
1362 case BASE_TYPE_DOUBLE: type_name = "Float64"; break;
1363 case BASE_TYPE_STRING: type_name = "UOffsetTRelative"; break;
1364 default: type_name = "VOffsetT"; break;
1365 }
1366 code += type_name;
1367 }
1368
1369 void GenPackForScalarVectorField(const StructDef &struct_def,
1370 const FieldDef &field,
1371 std::string *code_prefix_ptr,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001372 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001373 auto &code = *code_ptr;
1374 auto &code_prefix = *code_prefix_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001375 const auto field_field = namer_.Field(field);
1376 const auto field_method = namer_.Method(field);
1377 const auto struct_type = namer_.Type(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001378
1379 // Adds the field into the struct.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001380 code += GenIndents(2) + "if self." + field_field + " is not None:";
1381 code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " +
1382 field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001383
1384 // Creates the field.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001385 code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
Austin Schuh272c6132020-11-14 16:37:52 -08001386 // If the vector is a string vector, we need to first build accessor for
1387 // each string element. And this generated code, needs to be
1388 // placed ahead of code_prefix.
1389 auto vectortype = field.value.type.VectorType();
1390 if (IsString(vectortype)) {
James Kuszmaul8e62b022022-03-22 09:33:25 -07001391 code_prefix += GenIndents(3) + field_field + "list = []";
1392 code_prefix +=
1393 GenIndents(3) + "for i in range(len(self." + field_field + ")):";
1394 code_prefix += GenIndents(4) + field_field +
1395 "list.append(builder.CreateString(self." + field_field +
1396 "[i]))";
Austin Schuh272c6132020-11-14 16:37:52 -08001397 GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 3);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001398 code_prefix += "(" + field_field + "list[i])";
1399 code_prefix += GenIndents(3) + field_field + " = builder.EndVector()";
Austin Schuh272c6132020-11-14 16:37:52 -08001400 return;
1401 }
1402
1403 code_prefix += GenIndents(3) + "if np is not None and type(self." +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001404 field_field + ") is np.ndarray:";
1405 code_prefix += GenIndents(4) + field_field +
1406 " = builder.CreateNumpyVector(self." + field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001407 code_prefix += GenIndents(3) + "else:";
1408 GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 4);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001409 code_prefix += "(self." + field_field + "[i])";
1410 code_prefix += GenIndents(4) + field_field + " = builder.EndVector()";
Austin Schuh272c6132020-11-14 16:37:52 -08001411 }
1412
1413 void GenPackForStructField(const StructDef &struct_def, const FieldDef &field,
1414 std::string *code_prefix_ptr,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001415 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001416 auto &code_prefix = *code_prefix_ptr;
1417 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001418 const auto field_field = namer_.Field(field);
1419 const auto field_method = namer_.Method(field);
1420 const auto struct_type = namer_.Type(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001421
1422 if (field.value.type.struct_def->fixed) {
1423 // Pure struct fields need to be created along with their parent
1424 // structs.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001425 code += GenIndents(2) + "if self." + field_field + " is not None:";
1426 code += GenIndents(3) + field_field + " = self." + field_field +
1427 ".Pack(builder)";
Austin Schuh272c6132020-11-14 16:37:52 -08001428 } else {
1429 // Tables need to be created before their parent structs are created.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001430 code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
1431 code_prefix += GenIndents(3) + field_field + " = self." + field_field +
1432 ".Pack(builder)";
1433 code += GenIndents(2) + "if self." + field_field + " is not None:";
Austin Schuh272c6132020-11-14 16:37:52 -08001434 }
1435
James Kuszmaul8e62b022022-03-22 09:33:25 -07001436 code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " +
1437 field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001438 }
1439
1440 void GenPackForUnionField(const StructDef &struct_def, const FieldDef &field,
1441 std::string *code_prefix_ptr,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001442 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001443 auto &code_prefix = *code_prefix_ptr;
1444 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001445 const auto field_field = namer_.Field(field);
1446 const auto field_method = namer_.Method(field);
1447 const auto struct_type = namer_.Type(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001448
1449 // TODO(luwa): TypeT should be moved under the None check as well.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001450 code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
1451 code_prefix += GenIndents(3) + field_field + " = self." + field_field +
1452 ".Pack(builder)";
1453 code += GenIndents(2) + "if self." + field_field + " is not None:";
1454 code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " +
1455 field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001456 }
1457
James Kuszmaul8e62b022022-03-22 09:33:25 -07001458 void GenPackForTable(const StructDef &struct_def,
1459 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001460 auto &code_base = *code_ptr;
1461 std::string code, code_prefix;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001462 const auto struct_var = namer_.Variable(struct_def);
1463 const auto struct_type = namer_.Type(struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001464
1465 GenReceiverForObjectAPI(struct_def, code_ptr);
1466 code_base += "Pack(self, builder):";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001467 code += GenIndents(2) + struct_type + "Start(builder)";
Austin Schuh272c6132020-11-14 16:37:52 -08001468 for (auto it = struct_def.fields.vec.begin();
1469 it != struct_def.fields.vec.end(); ++it) {
1470 auto &field = **it;
1471 if (field.deprecated) continue;
1472
Austin Schuh2dd86a92022-09-14 21:19:23 -07001473 const auto field_method = namer_.Method(field);
1474 const auto field_field = namer_.Field(field);
Austin Schuh272c6132020-11-14 16:37:52 -08001475
1476 switch (field.value.type.base_type) {
1477 case BASE_TYPE_STRUCT: {
1478 GenPackForStructField(struct_def, field, &code_prefix, &code);
1479 break;
1480 }
1481 case BASE_TYPE_UNION: {
1482 GenPackForUnionField(struct_def, field, &code_prefix, &code);
1483 break;
1484 }
1485 case BASE_TYPE_VECTOR: {
1486 auto vectortype = field.value.type.VectorType();
1487 if (vectortype.base_type == BASE_TYPE_STRUCT) {
1488 GenPackForStructVectorField(struct_def, field, &code_prefix, &code);
1489 } else {
1490 GenPackForScalarVectorField(struct_def, field, &code_prefix, &code);
1491 }
1492 break;
1493 }
1494 case BASE_TYPE_ARRAY: {
1495 GenPackForScalarVectorField(struct_def, field, &code_prefix, &code);
1496 break;
1497 }
1498 case BASE_TYPE_STRING: {
James Kuszmaul8e62b022022-03-22 09:33:25 -07001499 code_prefix +=
1500 GenIndents(2) + "if self." + field_field + " is not None:";
1501 code_prefix += GenIndents(3) + field_field +
1502 " = builder.CreateString(self." + field_field + ")";
1503 code += GenIndents(2) + "if self." + field_field + " is not None:";
1504 code += GenIndents(3) + struct_type + "Add" + field_method +
1505 "(builder, " + field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001506 break;
1507 }
1508 default:
1509 // Generates code for scalar values. If the value equals to the
1510 // default value, builder will automatically ignore it. So we don't
1511 // need to check the value ahead.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001512 code += GenIndents(2) + struct_type + "Add" + field_method +
1513 "(builder, self." + field_field + ")";
Austin Schuh272c6132020-11-14 16:37:52 -08001514 break;
1515 }
1516 }
1517
James Kuszmaul8e62b022022-03-22 09:33:25 -07001518 code += GenIndents(2) + struct_var + " = " + struct_type + "End(builder)";
1519 code += GenIndents(2) + "return " + struct_var;
Austin Schuh272c6132020-11-14 16:37:52 -08001520
1521 code_base += code_prefix + code;
1522 code_base += "\n";
1523 }
1524
1525 void GenStructForObjectAPI(const StructDef &struct_def,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001526 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001527 if (struct_def.generated) return;
1528
1529 std::set<std::string> import_list;
1530 std::string code;
1531
1532 // Creates an object class for a struct or a table
1533 BeginClassForObjectAPI(struct_def, &code);
1534
1535 GenInitialize(struct_def, &code, &import_list);
1536
1537 InitializeFromBuf(struct_def, &code);
1538
1539 InitializeFromObjForObject(struct_def, &code);
1540
1541 GenUnPack(struct_def, &code);
1542
1543 if (struct_def.fixed) {
1544 GenPackForStruct(struct_def, &code);
1545 } else {
1546 GenPackForTable(struct_def, &code);
1547 }
1548
1549 // Adds the imports at top.
1550 auto &code_base = *code_ptr;
1551 code_base += "\n";
1552 for (auto it = import_list.begin(); it != import_list.end(); it++) {
1553 auto im = *it;
1554 code_base += im + "\n";
1555 }
1556 code_base += code;
1557 }
1558
1559 void GenUnionCreatorForStruct(const EnumDef &enum_def, const EnumVal &ev,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001560 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001561 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001562 const auto union_type = namer_.Type(enum_def);
1563 const auto variant = namer_.Variant(ev);
1564 auto field_type = namer_.ObjectType(*ev.union_type.struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001565
Austin Schuh2dd86a92022-09-14 21:19:23 -07001566 code +=
1567 GenIndents(1) + "if unionType == " + union_type + "()." + variant + ":";
Austin Schuh272c6132020-11-14 16:37:52 -08001568 if (parser_.opts.include_dependence_headers) {
1569 auto package_reference = GenPackageReference(ev.union_type);
1570 code += GenIndents(2) + "import " + package_reference;
1571 field_type = package_reference + "." + field_type;
1572 }
1573 code += GenIndents(2) + "return " + field_type +
1574 ".InitFromBuf(table.Bytes, table.Pos)";
1575 }
1576
1577 void GenUnionCreatorForString(const EnumDef &enum_def, const EnumVal &ev,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001578 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001579 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001580 const auto union_type = namer_.Type(enum_def);
1581 const auto variant = namer_.Variant(ev);
Austin Schuh272c6132020-11-14 16:37:52 -08001582
Austin Schuh2dd86a92022-09-14 21:19:23 -07001583 code +=
1584 GenIndents(1) + "if unionType == " + union_type + "()." + variant + ":";
Austin Schuh272c6132020-11-14 16:37:52 -08001585 code += GenIndents(2) + "tab = Table(table.Bytes, table.Pos)";
1586 code += GenIndents(2) + "union = tab.String(table.Pos)";
1587 code += GenIndents(2) + "return union";
1588 }
1589
1590 // Creates an union object based on union type.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001591 void GenUnionCreator(const EnumDef &enum_def, std::string *code_ptr) const {
1592 if (enum_def.generated) return;
1593
Austin Schuh272c6132020-11-14 16:37:52 -08001594 auto &code = *code_ptr;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001595 const auto enum_fn = namer_.Function(enum_def);
Austin Schuh272c6132020-11-14 16:37:52 -08001596
1597 code += "\n";
James Kuszmaul8e62b022022-03-22 09:33:25 -07001598 code += "def " + enum_fn + "Creator(unionType, table):";
Austin Schuh272c6132020-11-14 16:37:52 -08001599 code += GenIndents(1) + "from flatbuffers.table import Table";
1600 code += GenIndents(1) + "if not isinstance(table, Table):";
1601 code += GenIndents(2) + "return None";
1602
1603 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
1604 auto &ev = **it;
1605 // Union only supports string and table.
1606 switch (ev.union_type.base_type) {
1607 case BASE_TYPE_STRUCT:
1608 GenUnionCreatorForStruct(enum_def, ev, &code);
1609 break;
1610 case BASE_TYPE_STRING:
1611 GenUnionCreatorForString(enum_def, ev, &code);
1612 break;
1613 default: break;
1614 }
1615 }
1616 code += GenIndents(1) + "return None";
1617 code += "\n";
1618 }
1619
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001620 // Generate enum declarations.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001621 void GenEnum(const EnumDef &enum_def, std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001622 if (enum_def.generated) return;
1623
1624 GenComment(enum_def.doc_comment, code_ptr, &def_comment);
James Kuszmaul8e62b022022-03-22 09:33:25 -07001625 BeginEnum(enum_def, code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001626 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
1627 auto &ev = **it;
1628 GenComment(ev.doc_comment, code_ptr, &def_comment, Indent.c_str());
1629 EnumMember(enum_def, ev, code_ptr);
1630 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001631 }
1632
1633 // Returns the function name that is able to read a value of the given type.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001634 std::string GenGetter(const Type &type) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001635 switch (type.base_type) {
1636 case BASE_TYPE_STRING: return "self._tab.String(";
1637 case BASE_TYPE_UNION: return "self._tab.Union(";
1638 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
1639 default:
1640 return "self._tab.Get(flatbuffers.number_types." +
James Kuszmaul8e62b022022-03-22 09:33:25 -07001641 namer_.Method(GenTypeGet(type)) + "Flags, ";
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001642 }
1643 }
1644
1645 // Returns the method name for use with add/put calls.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001646 std::string GenMethod(const FieldDef &field) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001647 return (IsScalar(field.value.type.base_type) || IsArray(field.value.type))
James Kuszmaul8e62b022022-03-22 09:33:25 -07001648 ? namer_.Method(GenTypeBasic(field.value.type))
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001649 : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
1650 }
1651
James Kuszmaul8e62b022022-03-22 09:33:25 -07001652 std::string GenTypeBasic(const Type &type) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001653 // clang-format off
Austin Schuh272c6132020-11-14 16:37:52 -08001654 static const char *ctypename[] = {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001655 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
Austin Schuh272c6132020-11-14 16:37:52 -08001656 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, ...) \
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001657 #PTYPE,
1658 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
1659 #undef FLATBUFFERS_TD
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001660 };
Austin Schuh272c6132020-11-14 16:37:52 -08001661 // clang-format on
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001662 return ctypename[IsArray(type) ? type.VectorType().base_type
1663 : type.base_type];
1664 }
1665
James Kuszmaul8e62b022022-03-22 09:33:25 -07001666 std::string GenTypePointer(const Type &type) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001667 switch (type.base_type) {
1668 case BASE_TYPE_STRING: return "string";
1669 case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
1670 case BASE_TYPE_STRUCT: return type.struct_def->name;
1671 case BASE_TYPE_UNION:
1672 // fall through
1673 default: return "*flatbuffers.Table";
1674 }
1675 }
1676
James Kuszmaul8e62b022022-03-22 09:33:25 -07001677 std::string GenTypeGet(const Type &type) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001678 return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
1679 }
1680
James Kuszmaul8e62b022022-03-22 09:33:25 -07001681 std::string TypeName(const FieldDef &field) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001682 return GenTypeGet(field.value.type);
1683 }
1684
1685 // Create a struct with a builder and the struct's arguments.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001686 void GenStructBuilder(const StructDef &struct_def,
1687 std::string *code_ptr) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001688 BeginBuilderArgs(struct_def, code_ptr);
Austin Schuh272c6132020-11-14 16:37:52 -08001689 StructBuilderArgs(struct_def,
1690 /* nameprefix = */ "",
1691 /* namesuffix = */ "",
1692 /* has_field_name = */ true,
1693 /* fieldname_suffix = */ "_", code_ptr);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001694 EndBuilderArgs(code_ptr);
1695
1696 StructBuilderBody(struct_def, "", code_ptr);
1697 EndBuilderBody(code_ptr);
1698 }
1699
1700 bool generate() {
James Kuszmaul8e62b022022-03-22 09:33:25 -07001701 std::string one_file_code;
1702 if (!generateEnums(&one_file_code)) return false;
1703 if (!generateStructs(&one_file_code)) return false;
1704
1705 if (parser_.opts.one_file) {
1706 // Legacy file format uses keep casing.
1707 return SaveType(file_name_ + "_generated.py", *parser_.current_namespace_,
1708 one_file_code, true);
1709 }
1710
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001711 return true;
1712 }
1713
1714 private:
James Kuszmaul8e62b022022-03-22 09:33:25 -07001715 bool generateEnums(std::string *one_file_code) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001716 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
1717 ++it) {
1718 auto &enum_def = **it;
1719 std::string enumcode;
1720 GenEnum(enum_def, &enumcode);
Austin Schuh272c6132020-11-14 16:37:52 -08001721 if (parser_.opts.generate_object_based_api & enum_def.is_union) {
1722 GenUnionCreator(enum_def, &enumcode);
1723 }
James Kuszmaul8e62b022022-03-22 09:33:25 -07001724
1725 if (parser_.opts.one_file && !enumcode.empty()) {
1726 *one_file_code += enumcode + "\n\n";
1727 } else {
Austin Schuh2dd86a92022-09-14 21:19:23 -07001728 if (!SaveType(namer_.File(enum_def, SkipFile::Suffix),
James Kuszmaul8e62b022022-03-22 09:33:25 -07001729 *enum_def.defined_namespace, enumcode, false))
1730 return false;
1731 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001732 }
1733 return true;
1734 }
1735
James Kuszmaul8e62b022022-03-22 09:33:25 -07001736 bool generateStructs(std::string *one_file_code) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001737 for (auto it = parser_.structs_.vec.begin();
1738 it != parser_.structs_.vec.end(); ++it) {
1739 auto &struct_def = **it;
1740 std::string declcode;
1741 GenStruct(struct_def, &declcode);
Austin Schuh272c6132020-11-14 16:37:52 -08001742 if (parser_.opts.generate_object_based_api) {
1743 GenStructForObjectAPI(struct_def, &declcode);
1744 }
James Kuszmaul8e62b022022-03-22 09:33:25 -07001745
1746 if (parser_.opts.one_file && !declcode.empty()) {
1747 *one_file_code += declcode + "\n\n";
1748 } else {
Austin Schuh2dd86a92022-09-14 21:19:23 -07001749 if (!SaveType(namer_.File(struct_def, SkipFile::Suffix),
James Kuszmaul8e62b022022-03-22 09:33:25 -07001750 *struct_def.defined_namespace, declcode, true))
1751 return false;
1752 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001753 }
1754 return true;
1755 }
1756
1757 // Begin by declaring namespace and imports.
1758 void BeginFile(const std::string &name_space_name, const bool needs_imports,
James Kuszmaul8e62b022022-03-22 09:33:25 -07001759 std::string *code_ptr) const {
Austin Schuh272c6132020-11-14 16:37:52 -08001760 auto &code = *code_ptr;
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001761 code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
1762 code += "# namespace: " + name_space_name + "\n\n";
Austin Schuh272c6132020-11-14 16:37:52 -08001763 if (needs_imports) {
1764 code += "import flatbuffers\n";
1765 code += "from flatbuffers.compat import import_numpy\n";
1766 code += "np = import_numpy()\n\n";
1767 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001768 }
1769
1770 // Save out the generated code for a Python Table type.
James Kuszmaul8e62b022022-03-22 09:33:25 -07001771 bool SaveType(const std::string &defname, const Namespace &ns,
1772 const std::string &classcode, bool needs_imports) const {
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001773 if (!classcode.length()) return true;
1774
James Kuszmaul8e62b022022-03-22 09:33:25 -07001775 std::string code = "";
1776 BeginFile(LastNamespacePart(ns), needs_imports, &code);
1777 code += classcode;
1778
1779 const std::string directories =
1780 parser_.opts.one_file ? path_ : namer_.Directories(ns.components);
1781 EnsureDirExists(directories);
1782
1783 for (size_t i = path_.size() + 1; i != std::string::npos;
1784 i = directories.find(kPathSeparator, i + 1)) {
1785 const std::string init_py =
1786 directories.substr(0, i) + kPathSeparator + "__init__.py";
1787 SaveFile(init_py.c_str(), "", false);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001788 }
1789
James Kuszmaul8e62b022022-03-22 09:33:25 -07001790 const std::string filename = directories + defname;
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001791 return SaveFile(filename.c_str(), code, false);
1792 }
Austin Schuh272c6132020-11-14 16:37:52 -08001793
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001794 private:
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001795 const SimpleFloatConstantGenerator float_const_gen_;
Austin Schuh2dd86a92022-09-14 21:19:23 -07001796 const IdlNamer namer_;
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001797};
1798
1799} // namespace python
1800
1801bool GeneratePython(const Parser &parser, const std::string &path,
1802 const std::string &file_name) {
1803 python::PythonGenerator generator(parser, path, file_name);
1804 return generator.generate();
1805}
1806
1807} // namespace flatbuffers