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