1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // independent from idl_parser, since this code is not needed for most clients
18
19 #include <string>
20
21 #include "flatbuffers/code_generators.h"
22 #include "flatbuffers/flatbuffers.h"
23 #include "flatbuffers/idl.h"
24 #include "flatbuffers/util.h"
25
26 #include <unordered_set>
27
28 namespace flatbuffers {
29 namespace python {
30
31 // Hardcode spaces per indentation.
32 const std::string Indent = " ";
33
34 class PythonGenerator : public BaseGenerator {
35 public:
PythonGenerator(const Parser & parser,const std::string & path,const std::string & file_name)36 PythonGenerator(const Parser &parser, const std::string &path,
37 const std::string &file_name)
38 : BaseGenerator(parser, path, file_name, "" /* not used */,
39 "" /* not used */),
40 float_const_gen_("float('nan')", "float('inf')", "float('-inf')") {
41 static const char * const keywords[] = {
42 "False",
43 "None",
44 "True",
45 "and",
46 "as",
47 "assert",
48 "break",
49 "class",
50 "continue",
51 "def",
52 "del",
53 "elif",
54 "else",
55 "except",
56 "finally",
57 "for",
58 "from",
59 "global",
60 "if",
61 "import",
62 "in",
63 "is",
64 "lambda",
65 "nonlocal",
66 "not",
67 "or",
68 "pass",
69 "raise",
70 "return",
71 "try",
72 "while",
73 "with",
74 "yield"
75 };
76 keywords_.insert(std::begin(keywords), std::end(keywords));
77 }
78
79 // Most field accessors need to retrieve and test the field offset first,
80 // this is the prefix code for that.
OffsetPrefix(const FieldDef & field)81 std::string OffsetPrefix(const FieldDef &field) {
82 return "\n" + Indent + Indent +
83 "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
84 "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
85 Indent + Indent + "if o != 0:\n";
86 }
87
88 // Begin a class declaration.
BeginClass(const StructDef & struct_def,std::string * code_ptr)89 void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
90 std::string &code = *code_ptr;
91 code += "class " + NormalizedName(struct_def) + "(object):\n";
92 code += Indent + "__slots__ = ['_tab']";
93 code += "\n\n";
94 }
95
96 // Begin enum code with a class declaration.
BeginEnum(const std::string class_name,std::string * code_ptr)97 void BeginEnum(const std::string class_name, std::string *code_ptr) {
98 std::string &code = *code_ptr;
99 code += "class " + class_name + "(object):\n";
100 }
101
EscapeKeyword(const std::string & name) const102 std::string EscapeKeyword(const std::string &name) const {
103 return keywords_.find(name) == keywords_.end() ? name : name + "_";
104 }
105
NormalizedName(const Definition & definition) const106 std::string NormalizedName(const Definition &definition) const {
107 return EscapeKeyword(definition.name);
108 }
109
NormalizedName(const EnumVal & ev) const110 std::string NormalizedName(const EnumVal &ev) const {
111 return EscapeKeyword(ev.name);
112 }
113
114 // A single enum member.
EnumMember(const EnumVal ev,std::string * code_ptr)115 void EnumMember(const EnumVal ev, std::string *code_ptr) {
116 std::string &code = *code_ptr;
117 code += Indent;
118 code += NormalizedName(ev);
119 code += " = ";
120 code += NumToString(ev.value) + "\n";
121 }
122
123 // End enum code.
EndEnum(std::string * code_ptr)124 void EndEnum(std::string *code_ptr) {
125 std::string &code = *code_ptr;
126 code += "\n";
127 }
128
129 // Initialize a new struct or table from existing data.
NewRootTypeFromBuffer(const StructDef & struct_def,std::string * code_ptr)130 void NewRootTypeFromBuffer(const StructDef &struct_def,
131 std::string *code_ptr) {
132 std::string &code = *code_ptr;
133
134 code += Indent + "@classmethod\n";
135 code += Indent + "def GetRootAs";
136 code += NormalizedName(struct_def);
137 code += "(cls, buf, offset):";
138 code += "\n";
139 code += Indent + Indent;
140 code += "n = flatbuffers.encode.Get";
141 code += "(flatbuffers.packer.uoffset, buf, offset)\n";
142 code += Indent + Indent + "x = " + NormalizedName(struct_def) + "()\n";
143 code += Indent + Indent + "x.Init(buf, n + offset)\n";
144 code += Indent + Indent + "return x\n";
145 code += "\n";
146 }
147
148 // Initialize an existing object with other data, to avoid an allocation.
InitializeExisting(const StructDef & struct_def,std::string * code_ptr)149 void InitializeExisting(const StructDef &struct_def,
150 std::string *code_ptr) {
151 std::string &code = *code_ptr;
152
153 GenReceiver(struct_def, code_ptr);
154 code += "Init(self, buf, pos):\n";
155 code += Indent + Indent + "self._tab = flatbuffers.table.Table(buf, pos)\n";
156 code += "\n";
157 }
158
159 // Get the length of a vector.
GetVectorLen(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)160 void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
161 std::string *code_ptr) {
162 std::string &code = *code_ptr;
163
164 GenReceiver(struct_def, code_ptr);
165 code += MakeCamel(NormalizedName(field)) + "Length(self";
166 code += "):" + OffsetPrefix(field);
167 code += Indent + Indent + Indent + "return self._tab.VectorLen(o)\n";
168 code += Indent + Indent + "return 0\n\n";
169 }
170
171 // Get the value of a struct's scalar.
GetScalarFieldOfStruct(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)172 void GetScalarFieldOfStruct(const StructDef &struct_def,
173 const FieldDef &field,
174 std::string *code_ptr) {
175 std::string &code = *code_ptr;
176 std::string getter = GenGetter(field.value.type);
177 GenReceiver(struct_def, code_ptr);
178 code += MakeCamel(NormalizedName(field));
179 code += "(self): return " + getter;
180 code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
181 code += NumToString(field.value.offset) + "))\n";
182 }
183
184 // Get the value of a table's scalar.
GetScalarFieldOfTable(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)185 void GetScalarFieldOfTable(const StructDef &struct_def,
186 const FieldDef &field,
187 std::string *code_ptr) {
188 std::string &code = *code_ptr;
189 std::string getter = GenGetter(field.value.type);
190 GenReceiver(struct_def, code_ptr);
191 code += MakeCamel(NormalizedName(field));
192 code += "(self):";
193 code += OffsetPrefix(field);
194 getter += "o + self._tab.Pos)";
195 auto is_bool = IsBool(field.value.type.base_type);
196 if (is_bool) {
197 getter = "bool(" + getter + ")";
198 }
199 code += Indent + Indent + Indent + "return " + getter + "\n";
200 std::string default_value;
201 if (is_bool) {
202 default_value = field.value.constant == "0" ? "False" : "True";
203 } else {
204 default_value = IsFloat(field.value.type.base_type)
205 ? float_const_gen_.GenFloatConstant(field)
206 : field.value.constant;
207 }
208 code += Indent + Indent + "return " + default_value + "\n\n";
209 }
210
211 // Get a struct by initializing an existing struct.
212 // Specific to Struct.
GetStructFieldOfStruct(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)213 void GetStructFieldOfStruct(const StructDef &struct_def,
214 const FieldDef &field,
215 std::string *code_ptr) {
216 std::string &code = *code_ptr;
217 GenReceiver(struct_def, code_ptr);
218 code += MakeCamel(NormalizedName(field));
219 code += "(self, obj):\n";
220 code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
221 code += NumToString(field.value.offset) + ")";
222 code += "\n" + Indent + Indent + "return obj\n\n";
223 }
224
225 // Get a struct by initializing an existing struct.
226 // Specific to Table.
GetStructFieldOfTable(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)227 void GetStructFieldOfTable(const StructDef &struct_def,
228 const FieldDef &field,
229 std::string *code_ptr) {
230 std::string &code = *code_ptr;
231 GenReceiver(struct_def, code_ptr);
232 code += MakeCamel(NormalizedName(field));
233 code += "(self):";
234 code += OffsetPrefix(field);
235 if (field.value.type.struct_def->fixed) {
236 code += Indent + Indent + Indent + "x = o + self._tab.Pos\n";
237 } else {
238 code += Indent + Indent + Indent;
239 code += "x = self._tab.Indirect(o + self._tab.Pos)\n";
240 }
241 code += Indent + Indent + Indent;
242 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
243 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
244 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
245 code += Indent + Indent + Indent + "return obj\n";
246 code += Indent + Indent + "return None\n\n";
247 }
248
249 // Get the value of a string.
GetStringField(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)250 void GetStringField(const StructDef &struct_def, const FieldDef &field,
251 std::string *code_ptr) {
252 std::string &code = *code_ptr;
253 GenReceiver(struct_def, code_ptr);
254 code += MakeCamel(NormalizedName(field));
255 code += "(self):";
256 code += OffsetPrefix(field);
257 code += Indent + Indent + Indent + "return " + GenGetter(field.value.type);
258 code += "o + self._tab.Pos)\n";
259 code += Indent + Indent + "return None\n\n";
260 }
261
262 // Get the value of a union from an object.
GetUnionField(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)263 void GetUnionField(const StructDef &struct_def, const FieldDef &field,
264 std::string *code_ptr) {
265 std::string &code = *code_ptr;
266 GenReceiver(struct_def, code_ptr);
267 code += MakeCamel(NormalizedName(field)) + "(self):";
268 code += OffsetPrefix(field);
269
270 // TODO(rw): this works and is not the good way to it:
271 bool is_native_table = TypeName(field) == "*flatbuffers.Table";
272 if (is_native_table) {
273 code += Indent + Indent + Indent + "from flatbuffers.table import Table\n";
274 } else {
275 code += Indent + Indent + Indent;
276 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
277 }
278 code += Indent + Indent + Indent + "obj = Table(bytearray(), 0)\n";
279 code += Indent + Indent + Indent + GenGetter(field.value.type);
280 code += "obj, o)\n" + Indent + Indent + Indent + "return obj\n";
281 code += Indent + Indent + "return None\n\n";
282 }
283
284 // Get the value of a vector's struct member.
GetMemberOfVectorOfStruct(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)285 void GetMemberOfVectorOfStruct(const StructDef &struct_def,
286 const FieldDef &field,
287 std::string *code_ptr) {
288 std::string &code = *code_ptr;
289 auto vectortype = field.value.type.VectorType();
290
291 GenReceiver(struct_def, code_ptr);
292 code += MakeCamel(NormalizedName(field));
293 code += "(self, j):" + OffsetPrefix(field);
294 code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n";
295 code += Indent + Indent + Indent;
296 code += "x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * ";
297 code += NumToString(InlineSize(vectortype)) + "\n";
298 if (!(vectortype.struct_def->fixed)) {
299 code += Indent + Indent + Indent + "x = self._tab.Indirect(x)\n";
300 }
301 code += Indent + Indent + Indent;
302 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
303 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
304 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
305 code += Indent + Indent + Indent + "return obj\n";
306 code += Indent + Indent + "return None\n\n";
307 }
308
309 // Get the value of a vector's non-struct member. Uses a named return
310 // argument to conveniently set the zero value for the result.
GetMemberOfVectorOfNonStruct(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)311 void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
312 const FieldDef &field,
313 std::string *code_ptr) {
314 std::string &code = *code_ptr;
315 auto vectortype = field.value.type.VectorType();
316
317 GenReceiver(struct_def, code_ptr);
318 code += MakeCamel(NormalizedName(field));
319 code += "(self, j):";
320 code += OffsetPrefix(field);
321 code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n";
322 code += Indent + Indent + Indent;
323 code += "return " + GenGetter(field.value.type);
324 code += "a + flatbuffers.number_types.UOffsetTFlags.py_type(j * ";
325 code += NumToString(InlineSize(vectortype)) + "))\n";
326 if (vectortype.base_type == BASE_TYPE_STRING) {
327 code += Indent + Indent + "return \"\"\n";
328 } else {
329 code += Indent + Indent + "return 0\n";
330 }
331 code += "\n";
332 }
333
334 // Returns a non-struct vector as a numpy array. Much faster
335 // than iterating over the vector element by element.
GetVectorOfNonStructAsNumpy(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)336 void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
337 const FieldDef &field,
338 std::string *code_ptr) {
339 std::string &code = *code_ptr;
340 auto vectortype = field.value.type.VectorType();
341
342 // Currently, we only support accessing as numpy array if
343 // the vector type is a scalar.
344 if (!(IsScalar(vectortype.base_type))) { return; }
345
346 GenReceiver(struct_def, code_ptr);
347 code += MakeCamel(NormalizedName(field)) + "AsNumpy(self):";
348 code += OffsetPrefix(field);
349
350 code += Indent + Indent + Indent;
351 code += "return ";
352 code += "self._tab.GetVectorAsNumpy(flatbuffers.number_types.";
353 code += MakeCamel(GenTypeGet(field.value.type));
354 code += "Flags, o)\n";
355
356 if (vectortype.base_type == BASE_TYPE_STRING) {
357 code += Indent + Indent + "return \"\"\n";
358 } else {
359 code += Indent + Indent + "return 0\n";
360 }
361 code += "\n";
362 }
363
364 // Begin the creator function signature.
BeginBuilderArgs(const StructDef & struct_def,std::string * code_ptr)365 void BeginBuilderArgs(const StructDef &struct_def,
366 std::string *code_ptr) {
367 std::string &code = *code_ptr;
368
369 code += "\n";
370 code += "def Create" + NormalizedName(struct_def);
371 code += "(builder";
372 }
373
374 // Recursively generate arguments for a constructor, to deal with nested
375 // structs.
StructBuilderArgs(const StructDef & struct_def,const char * nameprefix,std::string * code_ptr)376 void StructBuilderArgs(const StructDef &struct_def,
377 const char *nameprefix, std::string *code_ptr) {
378 for (auto it = struct_def.fields.vec.begin();
379 it != struct_def.fields.vec.end(); ++it) {
380 auto &field = **it;
381 if (IsStruct(field.value.type)) {
382 // Generate arguments for a struct inside a struct. To ensure names
383 // don't clash, and to make it obvious these arguments are constructing
384 // a nested struct, prefix the name with the field name.
385 StructBuilderArgs(*field.value.type.struct_def,
386 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
387 } else {
388 std::string &code = *code_ptr;
389 code += std::string(", ") + nameprefix;
390 code += MakeCamel(NormalizedName(field), false);
391 }
392 }
393 }
394
395 // End the creator function signature.
EndBuilderArgs(std::string * code_ptr)396 void EndBuilderArgs(std::string *code_ptr) {
397 std::string &code = *code_ptr;
398 code += "):\n";
399 }
400
401 // Recursively generate struct construction statements and instert manual
402 // padding.
StructBuilderBody(const StructDef & struct_def,const char * nameprefix,std::string * code_ptr)403 void StructBuilderBody(const StructDef &struct_def,
404 const char *nameprefix, std::string *code_ptr) {
405 std::string &code = *code_ptr;
406 code += " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
407 code += NumToString(struct_def.bytesize) + ")\n";
408 for (auto it = struct_def.fields.vec.rbegin();
409 it != struct_def.fields.vec.rend(); ++it) {
410 auto &field = **it;
411 if (field.padding)
412 code += " builder.Pad(" + NumToString(field.padding) + ")\n";
413 if (IsStruct(field.value.type)) {
414 StructBuilderBody(*field.value.type.struct_def,
415 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
416 } else {
417 code += " builder.Prepend" + GenMethod(field) + "(";
418 code += nameprefix + MakeCamel(NormalizedName(field), false) + ")\n";
419 }
420 }
421 }
422
EndBuilderBody(std::string * code_ptr)423 void EndBuilderBody(std::string *code_ptr) {
424 std::string &code = *code_ptr;
425 code += " return builder.Offset()\n";
426 }
427
428 // Get the value of a table's starting offset.
GetStartOfTable(const StructDef & struct_def,std::string * code_ptr)429 void GetStartOfTable(const StructDef &struct_def,
430 std::string *code_ptr) {
431 std::string &code = *code_ptr;
432 code += "def " + NormalizedName(struct_def) + "Start";
433 code += "(builder): ";
434 code += "builder.StartObject(";
435 code += NumToString(struct_def.fields.vec.size());
436 code += ")\n";
437 }
438
439 // Set the value of a table's field.
BuildFieldOfTable(const StructDef & struct_def,const FieldDef & field,const size_t offset,std::string * code_ptr)440 void BuildFieldOfTable(const StructDef &struct_def,
441 const FieldDef &field, const size_t offset,
442 std::string *code_ptr) {
443 std::string &code = *code_ptr;
444 code += "def " + NormalizedName(struct_def) + "Add" + MakeCamel(NormalizedName(field));
445 code += "(builder, ";
446 code += MakeCamel(NormalizedName(field), false);
447 code += "): ";
448 code += "builder.Prepend";
449 code += GenMethod(field) + "Slot(";
450 code += NumToString(offset) + ", ";
451 if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
452 code += "flatbuffers.number_types.UOffsetTFlags.py_type";
453 code += "(";
454 code += MakeCamel(NormalizedName(field), false) + ")";
455 } else {
456 code += MakeCamel(NormalizedName(field), false);
457 }
458 code += ", ";
459 code += IsFloat(field.value.type.base_type)
460 ? float_const_gen_.GenFloatConstant(field)
461 : field.value.constant;
462 code += ")\n";
463 }
464
465 // Set the value of one of the members of a table's vector.
BuildVectorOfTable(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)466 void BuildVectorOfTable(const StructDef &struct_def,
467 const FieldDef &field, std::string *code_ptr) {
468 std::string &code = *code_ptr;
469 code += "def " + NormalizedName(struct_def) + "Start";
470 code += MakeCamel(NormalizedName(field));
471 code += "Vector(builder, numElems): return builder.StartVector(";
472 auto vector_type = field.value.type.VectorType();
473 auto alignment = InlineAlignment(vector_type);
474 auto elem_size = InlineSize(vector_type);
475 code += NumToString(elem_size);
476 code += ", numElems, " + NumToString(alignment);
477 code += ")\n";
478 }
479
480 // Get the offset of the end of a table.
GetEndOffsetOnTable(const StructDef & struct_def,std::string * code_ptr)481 void GetEndOffsetOnTable(const StructDef &struct_def,
482 std::string *code_ptr) {
483 std::string &code = *code_ptr;
484 code += "def " + NormalizedName(struct_def) + "End";
485 code += "(builder): ";
486 code += "return builder.EndObject()\n";
487 }
488
489 // Generate the receiver for function signatures.
GenReceiver(const StructDef & struct_def,std::string * code_ptr)490 void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
491 std::string &code = *code_ptr;
492 code += Indent + "# " + NormalizedName(struct_def) + "\n";
493 code += Indent + "def ";
494 }
495
496 // Generate a struct field, conditioned on its child type(s).
GenStructAccessor(const StructDef & struct_def,const FieldDef & field,std::string * code_ptr)497 void GenStructAccessor(const StructDef &struct_def,
498 const FieldDef &field, std::string *code_ptr) {
499 GenComment(field.doc_comment, code_ptr, nullptr, "# ");
500 if (IsScalar(field.value.type.base_type)) {
501 if (struct_def.fixed) {
502 GetScalarFieldOfStruct(struct_def, field, code_ptr);
503 } else {
504 GetScalarFieldOfTable(struct_def, field, code_ptr);
505 }
506 } else {
507 switch (field.value.type.base_type) {
508 case BASE_TYPE_STRUCT:
509 if (struct_def.fixed) {
510 GetStructFieldOfStruct(struct_def, field, code_ptr);
511 } else {
512 GetStructFieldOfTable(struct_def, field, code_ptr);
513 }
514 break;
515 case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
516 case BASE_TYPE_VECTOR: {
517 auto vectortype = field.value.type.VectorType();
518 if (vectortype.base_type == BASE_TYPE_STRUCT) {
519 GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
520 } else {
521 GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
522 GetVectorOfNonStructAsNumpy(struct_def, field, code_ptr);
523 }
524 break;
525 }
526 case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
527 default: FLATBUFFERS_ASSERT(0);
528 }
529 }
530 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
531 GetVectorLen(struct_def, field, code_ptr);
532 }
533 }
534
535 // Generate table constructors, conditioned on its members' types.
GenTableBuilders(const StructDef & struct_def,std::string * code_ptr)536 void GenTableBuilders(const StructDef &struct_def,
537 std::string *code_ptr) {
538 GetStartOfTable(struct_def, code_ptr);
539
540 for (auto it = struct_def.fields.vec.begin();
541 it != struct_def.fields.vec.end(); ++it) {
542 auto &field = **it;
543 if (field.deprecated) continue;
544
545 auto offset = it - struct_def.fields.vec.begin();
546 BuildFieldOfTable(struct_def, field, offset, code_ptr);
547 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
548 BuildVectorOfTable(struct_def, field, code_ptr);
549 }
550 }
551
552 GetEndOffsetOnTable(struct_def, code_ptr);
553 }
554
555 // Generate struct or table methods.
GenStruct(const StructDef & struct_def,std::string * code_ptr)556 void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
557 if (struct_def.generated) return;
558
559 GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
560 BeginClass(struct_def, code_ptr);
561 if (!struct_def.fixed) {
562 // Generate a special accessor for the table that has been declared as
563 // the root type.
564 NewRootTypeFromBuffer(struct_def, code_ptr);
565 }
566 // Generate the Init method that sets the field in a pre-existing
567 // accessor object. This is to allow object reuse.
568 InitializeExisting(struct_def, code_ptr);
569 for (auto it = struct_def.fields.vec.begin();
570 it != struct_def.fields.vec.end(); ++it) {
571 auto &field = **it;
572 if (field.deprecated) continue;
573
574 GenStructAccessor(struct_def, field, code_ptr);
575 }
576
577 if (struct_def.fixed) {
578 // create a struct constructor function
579 GenStructBuilder(struct_def, code_ptr);
580 } else {
581 // Create a set of functions that allow table construction.
582 GenTableBuilders(struct_def, code_ptr);
583 }
584 }
585
586 // Generate enum declarations.
GenEnum(const EnumDef & enum_def,std::string * code_ptr)587 void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
588 if (enum_def.generated) return;
589
590 GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
591 BeginEnum(NormalizedName(enum_def), code_ptr);
592 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
593 ++it) {
594 auto &ev = **it;
595 GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
596 EnumMember(ev, code_ptr);
597 }
598 EndEnum(code_ptr);
599 }
600
601 // Returns the function name that is able to read a value of the given type.
GenGetter(const Type & type)602 std::string GenGetter(const Type &type) {
603 switch (type.base_type) {
604 case BASE_TYPE_STRING: return "self._tab.String(";
605 case BASE_TYPE_UNION: return "self._tab.Union(";
606 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
607 default:
608 return "self._tab.Get(flatbuffers.number_types." +
609 MakeCamel(GenTypeGet(type)) + "Flags, ";
610 }
611 }
612
613 // Returns the method name for use with add/put calls.
GenMethod(const FieldDef & field)614 std::string GenMethod(const FieldDef &field) {
615 return IsScalar(field.value.type.base_type)
616 ? MakeCamel(GenTypeBasic(field.value.type))
617 : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
618 }
619
GenTypeBasic(const Type & type)620 std::string GenTypeBasic(const Type &type) {
621 static const char *ctypename[] = {
622 // clang-format off
623 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
624 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
625 #PTYPE,
626 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
627 #undef FLATBUFFERS_TD
628 // clang-format on
629 };
630 return ctypename[type.base_type];
631 }
632
GenTypePointer(const Type & type)633 std::string GenTypePointer(const Type &type) {
634 switch (type.base_type) {
635 case BASE_TYPE_STRING: return "string";
636 case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
637 case BASE_TYPE_STRUCT: return type.struct_def->name;
638 case BASE_TYPE_UNION:
639 // fall through
640 default: return "*flatbuffers.Table";
641 }
642 }
643
GenTypeGet(const Type & type)644 std::string GenTypeGet(const Type &type) {
645 return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
646 }
647
TypeName(const FieldDef & field)648 std::string TypeName(const FieldDef &field) {
649 return GenTypeGet(field.value.type);
650 }
651
652 // Create a struct with a builder and the struct's arguments.
GenStructBuilder(const StructDef & struct_def,std::string * code_ptr)653 void GenStructBuilder(const StructDef &struct_def,
654 std::string *code_ptr) {
655 BeginBuilderArgs(struct_def, code_ptr);
656 StructBuilderArgs(struct_def, "", code_ptr);
657 EndBuilderArgs(code_ptr);
658
659 StructBuilderBody(struct_def, "", code_ptr);
660 EndBuilderBody(code_ptr);
661 }
662
generate()663 bool generate() {
664 if (!generateEnums()) return false;
665 if (!generateStructs()) return false;
666 return true;
667 }
668
669 private:
generateEnums()670 bool generateEnums() {
671 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
672 ++it) {
673 auto &enum_def = **it;
674 std::string enumcode;
675 GenEnum(enum_def, &enumcode);
676 if (!SaveType(enum_def, enumcode, false)) return false;
677 }
678 return true;
679 }
680
generateStructs()681 bool generateStructs() {
682 for (auto it = parser_.structs_.vec.begin();
683 it != parser_.structs_.vec.end(); ++it) {
684 auto &struct_def = **it;
685 std::string declcode;
686 GenStruct(struct_def, &declcode);
687 if (!SaveType(struct_def, declcode, true)) return false;
688 }
689 return true;
690 }
691
692 // Begin by declaring namespace and imports.
BeginFile(const std::string name_space_name,const bool needs_imports,std::string * code_ptr)693 void BeginFile(const std::string name_space_name, const bool needs_imports,
694 std::string *code_ptr) {
695 std::string &code = *code_ptr;
696 code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
697 code += "# namespace: " + name_space_name + "\n\n";
698 if (needs_imports) { code += "import flatbuffers\n\n"; }
699 }
700
701 // Save out the generated code for a Python Table type.
SaveType(const Definition & def,const std::string & classcode,bool needs_imports)702 bool SaveType(const Definition &def, const std::string &classcode,
703 bool needs_imports) {
704 if (!classcode.length()) return true;
705
706 std::string namespace_dir = path_;
707 auto &namespaces = def.defined_namespace->components;
708 for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
709 if (it != namespaces.begin()) namespace_dir += kPathSeparator;
710 namespace_dir += *it;
711 std::string init_py_filename = namespace_dir + "/__init__.py";
712 SaveFile(init_py_filename.c_str(), "", false);
713 }
714
715 std::string code = "";
716 BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
717 code += classcode;
718 std::string filename =
719 NamespaceDir(*def.defined_namespace) + NormalizedName(def) + ".py";
720 return SaveFile(filename.c_str(), code, false);
721 }
722 private:
723 std::unordered_set<std::string> keywords_;
724 const SimpleFloatConstantGenerator float_const_gen_;
725 };
726
727 } // namespace python
728
GeneratePython(const Parser & parser,const std::string & path,const std::string & file_name)729 bool GeneratePython(const Parser &parser, const std::string &path,
730 const std::string &file_name) {
731 python::PythonGenerator generator(parser, path, file_name);
732 return generator.generate();
733 }
734
735 } // namespace flatbuffers
736