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 "flatbuffers/code_generators.h"
20 #include "flatbuffers/flatbuffers.h"
21 #include "flatbuffers/idl.h"
22 #include "flatbuffers/util.h"
23 
24 #if defined(FLATBUFFERS_CPP98_STL)
25 #  include <cctype>
26 #endif  // defined(FLATBUFFERS_CPP98_STL)
27 
28 namespace flatbuffers {
29 
30 // These arrays need to correspond to the IDLOptions::k enum.
31 
32 struct LanguageParameters {
33   IDLOptions::Language language;
34   // Whether function names in the language typically start with uppercase.
35   bool first_camel_upper;
36   std::string file_extension;
37   std::string string_type;
38   std::string bool_type;
39   std::string open_curly;
40   std::string accessor_type;
41   std::string const_decl;
42   std::string unsubclassable_decl;
43   std::string enum_decl;
44   std::string enum_separator;
45   std::string getter_prefix;
46   std::string getter_suffix;
47   std::string inheritance_marker;
48   std::string namespace_ident;
49   std::string namespace_begin;
50   std::string namespace_end;
51   std::string set_bb_byteorder;
52   std::string get_bb_position;
53   std::string get_fbb_offset;
54   std::string accessor_prefix;
55   std::string accessor_prefix_static;
56   std::string optional_suffix;
57   std::string includes;
58   std::string class_annotation;
59   std::string generated_type_annotation;
60   CommentConfig comment_config;
61 };
62 
GetLangParams(IDLOptions::Language lang)63 const LanguageParameters &GetLangParams(IDLOptions::Language lang) {
64   static const LanguageParameters language_parameters[] = {
65     {
66         IDLOptions::kJava,
67         false,
68         ".java",
69         "String",
70         "boolean ",
71         " {\n",
72         "class ",
73         " final ",
74         "final ",
75         "final class ",
76         ";\n",
77         "()",
78         "",
79         " extends ",
80         "package ",
81         ";",
82         "",
83         "_bb.order(ByteOrder.LITTLE_ENDIAN); ",
84         "position()",
85         "offset()",
86         "",
87         "",
88         "",
89         "import java.nio.*;\nimport java.lang.*;\nimport "
90         "java.util.*;\nimport com.google.flatbuffers.*;\n",
91         "\n@SuppressWarnings(\"unused\")",
92         "\n@javax.annotation.Generated(value=\"flatc\")\n",
93         {
94             "/**",
95             " *",
96             " */",
97         },
98     },
99     {
100         IDLOptions::kCSharp,
101         true,
102         ".cs",
103         "string",
104         "bool ",
105         "\n{\n",
106         "struct ",
107         " readonly ",
108         "",
109         "enum ",
110         ",\n",
111         " { get",
112         "} ",
113         " : ",
114         "namespace ",
115         "\n{",
116         "\n}\n",
117         "",
118         "Position",
119         "Offset",
120         "__p.",
121         "Table.",
122         "?",
123         "using global::System;\nusing global::FlatBuffers;\n\n",
124         "",
125         "",
126         {
127             nullptr,
128             "///",
129             nullptr,
130         },
131     },
132   };
133 
134   if (lang == IDLOptions::kJava) {
135     return language_parameters[0];
136   } else {
137     FLATBUFFERS_ASSERT(lang == IDLOptions::kCSharp);
138     return language_parameters[1];
139   }
140 }
141 
142 namespace general {
143 class GeneralGenerator : public BaseGenerator {
144  public:
GeneralGenerator(const Parser & parser,const std::string & path,const std::string & file_name)145   GeneralGenerator(const Parser &parser, const std::string &path,
146                    const std::string &file_name)
147       : BaseGenerator(parser, path, file_name, "", "."),
148         lang_(GetLangParams(parser_.opts.lang)),
149         cur_name_space_(nullptr) {}
150 
151   GeneralGenerator &operator=(const GeneralGenerator &);
generate()152   bool generate() {
153     std::string one_file_code;
154     cur_name_space_ = parser_.current_namespace_;
155 
156     for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
157          ++it) {
158       std::string enumcode;
159       auto &enum_def = **it;
160       if (!parser_.opts.one_file) cur_name_space_ = enum_def.defined_namespace;
161       GenEnum(enum_def, &enumcode);
162       if (parser_.opts.one_file) {
163         one_file_code += enumcode;
164       } else {
165         if (!SaveType(enum_def.name, *enum_def.defined_namespace, enumcode,
166                       false))
167           return false;
168       }
169     }
170 
171     for (auto it = parser_.structs_.vec.begin();
172          it != parser_.structs_.vec.end(); ++it) {
173       std::string declcode;
174       auto &struct_def = **it;
175       if (!parser_.opts.one_file)
176         cur_name_space_ = struct_def.defined_namespace;
177       GenStruct(struct_def, &declcode);
178       if (parser_.opts.one_file) {
179         one_file_code += declcode;
180       } else {
181         if (!SaveType(struct_def.name, *struct_def.defined_namespace, declcode,
182                       true))
183           return false;
184       }
185     }
186 
187     if (parser_.opts.one_file) {
188       return SaveType(file_name_, *parser_.current_namespace_, one_file_code,
189                       true);
190     }
191     return true;
192   }
193 
194   // Save out the generated code for a single class while adding
195   // declaration boilerplate.
SaveType(const std::string & defname,const Namespace & ns,const std::string & classcode,bool needs_includes) const196   bool SaveType(const std::string &defname, const Namespace &ns,
197                 const std::string &classcode, bool needs_includes) const {
198     if (!classcode.length()) return true;
199 
200     std::string code;
201     if (lang_.language == IDLOptions::kCSharp) {
202       code =
203           "// <auto-generated>\n"
204           "//  " +
205           std::string(FlatBuffersGeneratedWarning()) +
206           "\n"
207           "// </auto-generated>\n\n";
208     } else {
209       code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
210     }
211 
212     std::string namespace_name = FullNamespace(".", ns);
213     if (!namespace_name.empty()) {
214       code += lang_.namespace_ident + namespace_name + lang_.namespace_begin;
215       code += "\n\n";
216     }
217     if (needs_includes) {
218       code += lang_.includes;
219       if (parser_.opts.gen_nullable) {
220         code += "\nimport javax.annotation.Nullable;\n";
221       }
222       code += lang_.class_annotation;
223     }
224     if (parser_.opts.gen_generated) {
225       code += lang_.generated_type_annotation;
226     }
227     code += classcode;
228     if (!namespace_name.empty()) code += lang_.namespace_end;
229     auto filename = NamespaceDir(ns) + defname + lang_.file_extension;
230     return SaveFile(filename.c_str(), code, false);
231   }
232 
CurrentNameSpace() const233   const Namespace *CurrentNameSpace() const { return cur_name_space_; }
234 
FunctionStart(char upper) const235   std::string FunctionStart(char upper) const {
236     return std::string() + (lang_.language == IDLOptions::kJava
237                                 ? static_cast<char>(tolower(upper))
238                                 : upper);
239   }
240 
GenNullableAnnotation(const Type & t) const241   std::string GenNullableAnnotation(const Type &t) const {
242     return lang_.language == IDLOptions::kJava && parser_.opts.gen_nullable &&
243                    !IsScalar(DestinationType(t, true).base_type)
244                ? " @Nullable "
245                : "";
246   }
247 
IsEnum(const Type & type)248   static bool IsEnum(const Type &type) {
249     return type.enum_def != nullptr && IsInteger(type.base_type);
250   }
251 
GenTypeBasic(const Type & type,bool enableLangOverrides) const252   std::string GenTypeBasic(const Type &type, bool enableLangOverrides) const {
253     // clang-format off
254   static const char * const java_typename[] = {
255     #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
256         CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
257         #JTYPE,
258       FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
259     #undef FLATBUFFERS_TD
260   };
261 
262   static const char * const csharp_typename[] = {
263     #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
264         CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
265         #NTYPE,
266       FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
267     #undef FLATBUFFERS_TD
268   };
269     // clang-format on
270 
271     if (enableLangOverrides) {
272       if (lang_.language == IDLOptions::kCSharp) {
273         if (IsEnum(type)) return WrapInNameSpace(*type.enum_def);
274         if (type.base_type == BASE_TYPE_STRUCT) {
275           return "Offset<" + WrapInNameSpace(*type.struct_def) + ">";
276         }
277       }
278     }
279 
280     if (lang_.language == IDLOptions::kJava) {
281       return java_typename[type.base_type];
282     } else {
283       FLATBUFFERS_ASSERT(lang_.language == IDLOptions::kCSharp);
284       return csharp_typename[type.base_type];
285     }
286   }
287 
GenTypeBasic(const Type & type) const288   std::string GenTypeBasic(const Type &type) const {
289     return GenTypeBasic(type, true);
290   }
291 
GenTypePointer(const Type & type) const292   std::string GenTypePointer(const Type &type) const {
293     switch (type.base_type) {
294       case BASE_TYPE_STRING: return lang_.string_type;
295       case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
296       case BASE_TYPE_STRUCT: return WrapInNameSpace(*type.struct_def);
297       case BASE_TYPE_UNION:
298         // Unions in C# use a generic Table-derived type for better type safety
299         if (lang_.language == IDLOptions::kCSharp) return "TTable";
300         // fall through
301       default: return "Table";
302     }
303   }
304 
GenTypeGet(const Type & type) const305   std::string GenTypeGet(const Type &type) const {
306     return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
307   }
308 
309   // Find the destination type the user wants to receive the value in (e.g.
310   // one size higher signed types for unsigned serialized values in Java).
DestinationType(const Type & type,bool vectorelem) const311   Type DestinationType(const Type &type, bool vectorelem) const {
312     if (lang_.language != IDLOptions::kJava) return type;
313     switch (type.base_type) {
314       // We use int for both uchar/ushort, since that generally means less
315       // casting than using short for uchar.
316       case BASE_TYPE_UCHAR: return Type(BASE_TYPE_INT);
317       case BASE_TYPE_USHORT: return Type(BASE_TYPE_INT);
318       case BASE_TYPE_UINT: return Type(BASE_TYPE_LONG);
319       case BASE_TYPE_VECTOR:
320         if (vectorelem) return DestinationType(type.VectorType(), vectorelem);
321         // else fall thru
322       default: return type;
323     }
324   }
325 
GenOffsetType(const StructDef & struct_def) const326   std::string GenOffsetType(const StructDef &struct_def) const {
327     if (lang_.language == IDLOptions::kCSharp) {
328       return "Offset<" + WrapInNameSpace(struct_def) + ">";
329     } else {
330       return "int";
331     }
332   }
333 
GenOffsetConstruct(const StructDef & struct_def,const std::string & variable_name) const334   std::string GenOffsetConstruct(const StructDef &struct_def,
335                                  const std::string &variable_name) const {
336     if (lang_.language == IDLOptions::kCSharp) {
337       return "new Offset<" + WrapInNameSpace(struct_def) + ">(" +
338              variable_name + ")";
339     }
340     return variable_name;
341   }
342 
GenVectorOffsetType() const343   std::string GenVectorOffsetType() const {
344     if (lang_.language == IDLOptions::kCSharp) {
345       return "VectorOffset";
346     } else {
347       return "int";
348     }
349   }
350 
351   // Generate destination type name
GenTypeNameDest(const Type & type) const352   std::string GenTypeNameDest(const Type &type) const {
353     return GenTypeGet(DestinationType(type, true));
354   }
355 
356   // Mask to turn serialized value into destination type value.
DestinationMask(const Type & type,bool vectorelem) const357   std::string DestinationMask(const Type &type, bool vectorelem) const {
358     if (lang_.language != IDLOptions::kJava) return "";
359     switch (type.base_type) {
360       case BASE_TYPE_UCHAR: return " & 0xFF";
361       case BASE_TYPE_USHORT: return " & 0xFFFF";
362       case BASE_TYPE_UINT: return " & 0xFFFFFFFFL";
363       case BASE_TYPE_VECTOR:
364         if (vectorelem) return DestinationMask(type.VectorType(), vectorelem);
365         // else fall thru
366       default: return "";
367     }
368   }
369 
370   // Casts necessary to correctly read serialized data
DestinationCast(const Type & type) const371   std::string DestinationCast(const Type &type) const {
372     if (type.base_type == BASE_TYPE_VECTOR) {
373       return DestinationCast(type.VectorType());
374     } else {
375       switch (lang_.language) {
376         case IDLOptions::kJava:
377           // Cast necessary to correctly read serialized unsigned values.
378           if (type.base_type == BASE_TYPE_UINT) return "(long)";
379           break;
380 
381         case IDLOptions::kCSharp:
382           // Cast from raw integral types to enum.
383           if (IsEnum(type)) return "(" + WrapInNameSpace(*type.enum_def) + ")";
384           break;
385 
386         default: break;
387       }
388     }
389     return "";
390   }
391 
392   // Cast statements for mutator method parameters.
393   // In Java, parameters representing unsigned numbers need to be cast down to
394   // their respective type. For example, a long holding an unsigned int value
395   // would be cast down to int before being put onto the buffer. In C#, one cast
396   // directly cast an Enum to its underlying type, which is essential before
397   // putting it onto the buffer.
SourceCast(const Type & type,bool castFromDest) const398   std::string SourceCast(const Type &type, bool castFromDest) const {
399     if (type.base_type == BASE_TYPE_VECTOR) {
400       return SourceCast(type.VectorType(), castFromDest);
401     } else {
402       switch (lang_.language) {
403         case IDLOptions::kJava:
404           if (castFromDest) {
405             if (type.base_type == BASE_TYPE_UINT)
406               return "(int)";
407             else if (type.base_type == BASE_TYPE_USHORT)
408               return "(short)";
409             else if (type.base_type == BASE_TYPE_UCHAR)
410               return "(byte)";
411           }
412           break;
413         case IDLOptions::kCSharp:
414           if (IsEnum(type)) return "(" + GenTypeBasic(type, false) + ")";
415           break;
416         default: break;
417       }
418     }
419     return "";
420   }
421 
SourceCast(const Type & type) const422   std::string SourceCast(const Type &type) const { return SourceCast(type, true); }
423 
SourceCastBasic(const Type & type,bool castFromDest) const424   std::string SourceCastBasic(const Type &type, bool castFromDest) const {
425     return IsScalar(type.base_type) ? SourceCast(type, castFromDest) : "";
426   }
427 
SourceCastBasic(const Type & type) const428   std::string SourceCastBasic(const Type &type) const {
429     return SourceCastBasic(type, true);
430   }
431 
GenEnumDefaultValue(const Value & value) const432   std::string GenEnumDefaultValue(const Value &value) const {
433     auto enum_def = value.type.enum_def;
434     auto vec = enum_def->vals.vec;
435     auto default_value = StringToInt(value.constant.c_str());
436 
437     auto result = value.constant;
438     for (auto it = vec.begin(); it != vec.end(); ++it) {
439       auto enum_val = **it;
440       if (enum_val.value == default_value) {
441         result = WrapInNameSpace(*enum_def) + "." + enum_val.name;
442         break;
443       }
444     }
445 
446     return result;
447   }
448 
GenDefaultValue(const Value & value,bool enableLangOverrides) const449   std::string GenDefaultValue(const Value &value, bool enableLangOverrides) const {
450     if (enableLangOverrides) {
451       // handles both enum case and vector of enum case
452       if (lang_.language == IDLOptions::kCSharp &&
453           value.type.enum_def != nullptr &&
454           value.type.base_type != BASE_TYPE_UNION) {
455         return GenEnumDefaultValue(value);
456       }
457     }
458 
459     auto longSuffix = lang_.language == IDLOptions::kJava ? "L" : "";
460     switch (value.type.base_type) {
461       case BASE_TYPE_FLOAT: return value.constant + "f";
462       case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true";
463       case BASE_TYPE_ULONG: {
464         if (lang_.language != IDLOptions::kJava) return value.constant;
465         // Converts the ulong into its bits signed equivalent
466         uint64_t defaultValue = StringToUInt(value.constant.c_str());
467         return NumToString(static_cast<int64_t>(defaultValue)) + longSuffix;
468       }
469       case BASE_TYPE_UINT:
470       case BASE_TYPE_LONG: return value.constant + longSuffix;
471       default: return value.constant;
472     }
473   }
474 
GenDefaultValue(const Value & value) const475   std::string GenDefaultValue(const Value &value) const {
476     return GenDefaultValue(value, true);
477   }
478 
GenDefaultValueBasic(const Value & value,bool enableLangOverrides) const479   std::string GenDefaultValueBasic(const Value &value,
480                                    bool enableLangOverrides) const {
481     if (!IsScalar(value.type.base_type)) {
482       if (enableLangOverrides) {
483         if (lang_.language == IDLOptions::kCSharp) {
484           switch (value.type.base_type) {
485             case BASE_TYPE_STRING: return "default(StringOffset)";
486             case BASE_TYPE_STRUCT:
487               return "default(Offset<" +
488                      WrapInNameSpace(*value.type.struct_def) + ">)";
489             case BASE_TYPE_VECTOR: return "default(VectorOffset)";
490             default: break;
491           }
492         }
493       }
494       return "0";
495     }
496     return GenDefaultValue(value, enableLangOverrides);
497   }
498 
GenDefaultValueBasic(const Value & value) const499   std::string GenDefaultValueBasic(const Value &value) const {
500     return GenDefaultValueBasic(value, true);
501   }
502 
GenEnum(EnumDef & enum_def,std::string * code_ptr) const503   void GenEnum(EnumDef &enum_def, std::string *code_ptr) const {
504     std::string &code = *code_ptr;
505     if (enum_def.generated) return;
506 
507     // Generate enum definitions of the form:
508     // public static (final) int name = value;
509     // In Java, we use ints rather than the Enum feature, because we want them
510     // to map directly to how they're used in C/C++ and file formats.
511     // That, and Java Enums are expensive, and not universally liked.
512     GenComment(enum_def.doc_comment, code_ptr, &lang_.comment_config);
513     if (enum_def.attributes.Lookup("private")) {
514       // For Java, we leave the enum unmarked to indicate package-private
515       // For C# we mark the enum as internal
516       if (lang_.language == IDLOptions::kCSharp) {
517         code += "internal ";
518       }
519     } else {
520       code += "public ";
521     }
522     code += lang_.enum_decl + enum_def.name;
523     if (lang_.language == IDLOptions::kCSharp) {
524       code += lang_.inheritance_marker +
525               GenTypeBasic(enum_def.underlying_type, false);
526     }
527     code += lang_.open_curly;
528     if (lang_.language == IDLOptions::kJava) {
529       code += "  private " + enum_def.name + "() { }\n";
530     }
531     for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
532          ++it) {
533       auto &ev = **it;
534       GenComment(ev.doc_comment, code_ptr, &lang_.comment_config, "  ");
535       if (lang_.language != IDLOptions::kCSharp) {
536         code += "  public static";
537         code += lang_.const_decl;
538         code += GenTypeBasic(enum_def.underlying_type, false);
539       }
540       code += " " + ev.name + " = ";
541       code += NumToString(ev.value);
542       code += lang_.enum_separator;
543     }
544 
545     // Generate a generate string table for enum values.
546     // We do not do that for C# where this functionality is native.
547     if (lang_.language != IDLOptions::kCSharp) {
548       // Problem is, if values are very sparse that could generate really big
549       // tables. Ideally in that case we generate a map lookup instead, but for
550       // the moment we simply don't output a table at all.
551       auto range = enum_def.vals.vec.back()->value -
552                    enum_def.vals.vec.front()->value + 1;
553       // Average distance between values above which we consider a table
554       // "too sparse". Change at will.
555       static const int kMaxSparseness = 5;
556       if (range / static_cast<int64_t>(enum_def.vals.vec.size()) <
557           kMaxSparseness) {
558         code += "\n  public static";
559         code += lang_.const_decl;
560         code += lang_.string_type;
561         code += "[] names = { ";
562         auto val = enum_def.vals.vec.front()->value;
563         for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
564              ++it) {
565           while (val++ != (*it)->value) code += "\"\", ";
566           code += "\"" + (*it)->name + "\", ";
567         }
568         code += "};\n\n";
569         code += "  public static ";
570         code += lang_.string_type;
571         code += " " + MakeCamel("name", lang_.first_camel_upper);
572         code += "(int e) { return names[e";
573         if (enum_def.vals.vec.front()->value)
574           code += " - " + enum_def.vals.vec.front()->name;
575         code += "]; }\n";
576       }
577     }
578 
579     // Close the class
580     code += "}";
581     // Java does not need the closing semi-colon on class definitions.
582     code += (lang_.language != IDLOptions::kJava) ? ";" : "";
583     code += "\n\n";
584   }
585 
586   // Returns the function name that is able to read a value of the given type.
GenGetter(const Type & type) const587   std::string GenGetter(const Type &type) const {
588     switch (type.base_type) {
589       case BASE_TYPE_STRING: return lang_.accessor_prefix + "__string";
590       case BASE_TYPE_STRUCT: return lang_.accessor_prefix + "__struct";
591       case BASE_TYPE_UNION: return lang_.accessor_prefix + "__union";
592       case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
593       default: {
594         std::string getter =
595             lang_.accessor_prefix + "bb." + FunctionStart('G') + "et";
596         if (type.base_type == BASE_TYPE_BOOL) {
597           getter = "0!=" + getter;
598         } else if (GenTypeBasic(type, false) != "byte") {
599           getter += MakeCamel(GenTypeBasic(type, false));
600         }
601         return getter;
602       }
603     }
604   }
605 
606   // Returns the function name that is able to read a value of the given type.
GenGetterForLookupByKey(flatbuffers::FieldDef * key_field,const std::string & data_buffer,const char * num=nullptr) const607   std::string GenGetterForLookupByKey(flatbuffers::FieldDef *key_field,
608                                       const std::string &data_buffer,
609                                       const char *num = nullptr) const {
610     auto type = key_field->value.type;
611     auto dest_mask = DestinationMask(type, true);
612     auto dest_cast = DestinationCast(type);
613     auto getter = data_buffer + "." + FunctionStart('G') + "et";
614     if (GenTypeBasic(type, false) != "byte") {
615       getter += MakeCamel(GenTypeBasic(type, false));
616     }
617     getter = dest_cast + getter + "(" + GenOffsetGetter(key_field, num) + ")" +
618              dest_mask;
619     return getter;
620   }
621 
622   // Direct mutation is only allowed for scalar fields.
623   // Hence a setter method will only be generated for such fields.
GenSetter(const Type & type) const624   std::string GenSetter(const Type &type) const {
625     if (IsScalar(type.base_type)) {
626       std::string setter =
627           lang_.accessor_prefix + "bb." + FunctionStart('P') + "ut";
628       if (GenTypeBasic(type, false) != "byte" &&
629           type.base_type != BASE_TYPE_BOOL) {
630         setter += MakeCamel(GenTypeBasic(type, false));
631       }
632       return setter;
633     } else {
634       return "";
635     }
636   }
637 
638   // Returns the method name for use with add/put calls.
GenMethod(const Type & type) const639   std::string GenMethod(const Type &type) const {
640     return IsScalar(type.base_type) ? MakeCamel(GenTypeBasic(type, false))
641                                     : (IsStruct(type) ? "Struct" : "Offset");
642   }
643 
644   // Recursively generate arguments for a constructor, to deal with nested
645   // structs.
GenStructArgs(const StructDef & struct_def,std::string * code_ptr,const char * nameprefix) const646   void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
647                      const char *nameprefix) const {
648     std::string &code = *code_ptr;
649     for (auto it = struct_def.fields.vec.begin();
650          it != struct_def.fields.vec.end(); ++it) {
651       auto &field = **it;
652       if (IsStruct(field.value.type)) {
653         // Generate arguments for a struct inside a struct. To ensure names
654         // don't clash, and to make it obvious these arguments are constructing
655         // a nested struct, prefix the name with the field name.
656         GenStructArgs(*field.value.type.struct_def, code_ptr,
657                       (nameprefix + (field.name + "_")).c_str());
658       } else {
659         code += ", ";
660         code += GenTypeBasic(DestinationType(field.value.type, false));
661         code += " ";
662         code += nameprefix;
663         code += MakeCamel(field.name, lang_.first_camel_upper);
664       }
665     }
666   }
667 
668   // Recusively generate struct construction statements of the form:
669   // builder.putType(name);
670   // and insert manual padding.
GenStructBody(const StructDef & struct_def,std::string * code_ptr,const char * nameprefix) const671   void GenStructBody(const StructDef &struct_def, std::string *code_ptr,
672                      const char *nameprefix) const {
673     std::string &code = *code_ptr;
674     code += "    builder." + FunctionStart('P') + "rep(";
675     code += NumToString(struct_def.minalign) + ", ";
676     code += NumToString(struct_def.bytesize) + ");\n";
677     for (auto it = struct_def.fields.vec.rbegin();
678          it != struct_def.fields.vec.rend(); ++it) {
679       auto &field = **it;
680       if (field.padding) {
681         code += "    builder." + FunctionStart('P') + "ad(";
682         code += NumToString(field.padding) + ");\n";
683       }
684       if (IsStruct(field.value.type)) {
685         GenStructBody(*field.value.type.struct_def, code_ptr,
686                       (nameprefix + (field.name + "_")).c_str());
687       } else {
688         code += "    builder." + FunctionStart('P') + "ut";
689         code += GenMethod(field.value.type) + "(";
690         code += SourceCast(field.value.type);
691         auto argname =
692             nameprefix + MakeCamel(field.name, lang_.first_camel_upper);
693         code += argname;
694         code += ");\n";
695       }
696     }
697   }
698 
GenByteBufferLength(const char * bb_name) const699   std::string GenByteBufferLength(const char *bb_name) const {
700     std::string bb_len = bb_name;
701     if (lang_.language == IDLOptions::kCSharp)
702       bb_len += ".Length";
703     else
704       bb_len += ".capacity()";
705     return bb_len;
706   }
707 
GenOffsetGetter(flatbuffers::FieldDef * key_field,const char * num=nullptr) const708   std::string GenOffsetGetter(flatbuffers::FieldDef *key_field,
709                               const char *num = nullptr) const {
710     std::string key_offset = "";
711     key_offset += lang_.accessor_prefix_static + "__offset(" +
712                   NumToString(key_field->value.offset) + ", ";
713     if (num) {
714       key_offset += num;
715       key_offset +=
716           (lang_.language == IDLOptions::kCSharp ? ".Value, builder.DataBuffer)"
717                                                  : ", _bb)");
718     } else {
719       key_offset += GenByteBufferLength("bb");
720       key_offset += " - tableOffset, bb)";
721     }
722     return key_offset;
723   }
724 
GenLookupKeyGetter(flatbuffers::FieldDef * key_field) const725   std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) const {
726     std::string key_getter = "      ";
727     key_getter += "int tableOffset = " + lang_.accessor_prefix_static;
728     key_getter += "__indirect(vectorLocation + 4 * (start + middle)";
729     key_getter += ", bb);\n      ";
730     if (key_field->value.type.base_type == BASE_TYPE_STRING) {
731       key_getter += "int comp = " + lang_.accessor_prefix_static;
732       key_getter += FunctionStart('C') + "ompareStrings(";
733       key_getter += GenOffsetGetter(key_field);
734       key_getter += ", byteKey, bb);\n";
735     } else {
736       auto get_val = GenGetterForLookupByKey(key_field, "bb");
737       if (lang_.language == IDLOptions::kCSharp) {
738         key_getter += "int comp = " + get_val + ".CompareTo(key);\n";
739       } else {
740         key_getter += GenTypeNameDest(key_field->value.type) + " val = ";
741         key_getter += get_val + ";\n";
742         key_getter += "      int comp = val > key ? 1 : val < key ? -1 : 0;\n";
743       }
744     }
745     return key_getter;
746   }
747 
GenKeyGetter(flatbuffers::FieldDef * key_field) const748   std::string GenKeyGetter(flatbuffers::FieldDef *key_field) const {
749     std::string key_getter = "";
750     auto data_buffer =
751         (lang_.language == IDLOptions::kCSharp) ? "builder.DataBuffer" : "_bb";
752     if (key_field->value.type.base_type == BASE_TYPE_STRING) {
753       if (lang_.language == IDLOptions::kJava) key_getter += " return ";
754       key_getter += lang_.accessor_prefix_static;
755       key_getter += FunctionStart('C') + "ompareStrings(";
756       key_getter += GenOffsetGetter(key_field, "o1") + ", ";
757       key_getter += GenOffsetGetter(key_field, "o2") + ", " + data_buffer + ")";
758       if (lang_.language == IDLOptions::kJava) key_getter += ";";
759     } else {
760       auto field_getter = GenGetterForLookupByKey(key_field, data_buffer, "o1");
761       if (lang_.language == IDLOptions::kCSharp) {
762         key_getter += field_getter;
763         field_getter = GenGetterForLookupByKey(key_field, data_buffer, "o2");
764         key_getter += ".CompareTo(" + field_getter + ")";
765       } else {
766         key_getter +=
767             "\n    " + GenTypeNameDest(key_field->value.type) + " val_1 = ";
768         key_getter +=
769             field_getter + ";\n    " + GenTypeNameDest(key_field->value.type);
770         key_getter += " val_2 = ";
771         field_getter = GenGetterForLookupByKey(key_field, data_buffer, "o2");
772         key_getter += field_getter + ";\n";
773         key_getter +=
774             "    return val_1 > val_2 ? 1 : val_1 < val_2 ? -1 : 0;\n ";
775       }
776     }
777     return key_getter;
778   }
779 
GenStruct(StructDef & struct_def,std::string * code_ptr) const780   void GenStruct(StructDef &struct_def, std::string *code_ptr) const {
781     if (struct_def.generated) return;
782     std::string &code = *code_ptr;
783 
784     // Generate a struct accessor class, with methods of the form:
785     // public type name() { return bb.getType(i + offset); }
786     // or for tables of the form:
787     // public type name() {
788     //   int o = __offset(offset); return o != 0 ? bb.getType(o + i) : default;
789     // }
790     GenComment(struct_def.doc_comment, code_ptr, &lang_.comment_config);
791     if (struct_def.attributes.Lookup("private")) {
792       // For Java, we leave the struct unmarked to indicate package-private
793       // For C# we mark the struct as internal
794       if (lang_.language == IDLOptions::kCSharp) {
795         code += "internal ";
796       }
797     } else {
798       code += "public ";
799     }
800     if (lang_.language == IDLOptions::kCSharp &&
801         struct_def.attributes.Lookup("csharp_partial")) {
802       // generate a partial class for this C# struct/table
803       code += "partial ";
804     } else {
805       code += lang_.unsubclassable_decl;
806     }
807     code += lang_.accessor_type + struct_def.name;
808     if (lang_.language == IDLOptions::kCSharp) {
809       code += " : IFlatbufferObject";
810       code += lang_.open_curly;
811       code += "  private ";
812       code += struct_def.fixed ? "Struct" : "Table";
813       code += " __p;\n";
814 
815       if (lang_.language == IDLOptions::kCSharp) {
816         code += "  public ByteBuffer ByteBuffer { get { return __p.bb; } }\n";
817       }
818 
819     } else {
820       code += lang_.inheritance_marker;
821       code += struct_def.fixed ? "Struct" : "Table";
822       code += lang_.open_curly;
823     }
824     if (!struct_def.fixed) {
825       // Generate a special accessor for the table that when used as the root
826       // of a FlatBuffer
827       std::string method_name =
828           FunctionStart('G') + "etRootAs" + struct_def.name;
829       std::string method_signature =
830           "  public static " + struct_def.name + " " + method_name;
831 
832       // create convenience method that doesn't require an existing object
833       code += method_signature + "(ByteBuffer _bb) ";
834       code += "{ return " + method_name + "(_bb, new " + struct_def.name +
835               "()); }\n";
836 
837       // create method that allows object reuse
838       code +=
839           method_signature + "(ByteBuffer _bb, " + struct_def.name + " obj) { ";
840       code += lang_.set_bb_byteorder;
841       code += "return (obj.__assign(_bb." + FunctionStart('G') + "etInt(_bb.";
842       code += lang_.get_bb_position;
843       code += ") + _bb.";
844       code += lang_.get_bb_position;
845       code += ", _bb)); }\n";
846       if (parser_.root_struct_def_ == &struct_def) {
847         if (parser_.file_identifier_.length()) {
848           // Check if a buffer has the identifier.
849           code += "  public static ";
850           code += lang_.bool_type + struct_def.name;
851           code += "BufferHasIdentifier(ByteBuffer _bb) { return ";
852           code += lang_.accessor_prefix_static + "__has_identifier(_bb, \"";
853           code += parser_.file_identifier_;
854           code += "\"); }\n";
855         }
856       }
857     }
858     // Generate the __init method that sets the field in a pre-existing
859     // accessor object. This is to allow object reuse.
860     code += "  public void __init(int _i, ByteBuffer _bb) ";
861     code += "{ " + lang_.accessor_prefix + "bb_pos = _i; ";
862     code += lang_.accessor_prefix + "bb = _bb; }\n";
863     code +=
864         "  public " + struct_def.name + " __assign(int _i, ByteBuffer _bb) ";
865     code += "{ __init(_i, _bb); return this; }\n\n";
866     for (auto it = struct_def.fields.vec.begin();
867          it != struct_def.fields.vec.end(); ++it) {
868       auto &field = **it;
869       if (field.deprecated) continue;
870       GenComment(field.doc_comment, code_ptr, &lang_.comment_config, "  ");
871       std::string type_name = GenTypeGet(field.value.type);
872       std::string type_name_dest = GenTypeNameDest(field.value.type);
873       std::string conditional_cast = "";
874       std::string optional = "";
875       if (lang_.language == IDLOptions::kCSharp && !struct_def.fixed &&
876           (field.value.type.base_type == BASE_TYPE_STRUCT ||
877            field.value.type.base_type == BASE_TYPE_UNION ||
878            (field.value.type.base_type == BASE_TYPE_VECTOR &&
879             (field.value.type.element == BASE_TYPE_STRUCT ||
880              field.value.type.element == BASE_TYPE_UNION)))) {
881         optional = lang_.optional_suffix;
882         conditional_cast = "(" + type_name_dest + optional + ")";
883       }
884       std::string dest_mask = DestinationMask(field.value.type, true);
885       std::string dest_cast = DestinationCast(field.value.type);
886       std::string src_cast = SourceCast(field.value.type);
887       std::string method_start = "  public " +
888                                  (field.required ? "" : GenNullableAnnotation(field.value.type)) +
889                                  type_name_dest + optional + " " +
890                                  MakeCamel(field.name, lang_.first_camel_upper);
891       std::string obj = lang_.language == IDLOptions::kCSharp
892                             ? "(new " + type_name + "())"
893                             : "obj";
894 
895       // Most field accessors need to retrieve and test the field offset first,
896       // this is the prefix code for that:
897       auto offset_prefix = " { int o = " + lang_.accessor_prefix + "__offset(" +
898                            NumToString(field.value.offset) +
899                            "); return o != 0 ? ";
900       // Generate the accessors that don't do object reuse.
901       if (field.value.type.base_type == BASE_TYPE_STRUCT) {
902         // Calls the accessor that takes an accessor object with a new object.
903         if (lang_.language != IDLOptions::kCSharp) {
904           code += method_start + "() { return ";
905           code += MakeCamel(field.name, lang_.first_camel_upper);
906           code += "(new ";
907           code += type_name + "()); }\n";
908         }
909       } else if (field.value.type.base_type == BASE_TYPE_VECTOR &&
910                  field.value.type.element == BASE_TYPE_STRUCT) {
911         // Accessors for vectors of structs also take accessor objects, this
912         // generates a variant without that argument.
913         if (lang_.language != IDLOptions::kCSharp) {
914           code += method_start + "(int j) { return ";
915           code += MakeCamel(field.name, lang_.first_camel_upper);
916           code += "(new " + type_name + "(), j); }\n";
917         }
918       } else if (field.value.type.base_type == BASE_TYPE_UNION ||
919           (field.value.type.base_type == BASE_TYPE_VECTOR &&
920            field.value.type.VectorType().base_type == BASE_TYPE_UNION)) {
921         if (lang_.language == IDLOptions::kCSharp) {
922           // Union types in C# use generic Table-derived type for better type
923           // safety.
924           method_start += "<TTable>";
925           type_name = type_name_dest;
926         }
927       }
928       std::string getter = dest_cast + GenGetter(field.value.type);
929       code += method_start;
930       std::string default_cast = "";
931       // only create default casts for c# scalars or vectors of scalars
932       if (lang_.language == IDLOptions::kCSharp &&
933           (IsScalar(field.value.type.base_type) ||
934            (field.value.type.base_type == BASE_TYPE_VECTOR &&
935             IsScalar(field.value.type.element)))) {
936         // For scalars, default value will be returned by GetDefaultValue().
937         // If the scalar is an enum, GetDefaultValue() returns an actual c# enum
938         // that doesn't need to be casted. However, default values for enum
939         // elements of vectors are integer literals ("0") and are still casted
940         // for clarity.
941         if (field.value.type.enum_def == nullptr ||
942             field.value.type.base_type == BASE_TYPE_VECTOR) {
943           default_cast = "(" + type_name_dest + ")";
944         }
945       }
946       std::string member_suffix = "; ";
947       if (IsScalar(field.value.type.base_type)) {
948         code += lang_.getter_prefix;
949         member_suffix += lang_.getter_suffix;
950         if (struct_def.fixed) {
951           code += " { return " + getter;
952           code += "(" + lang_.accessor_prefix + "bb_pos + ";
953           code += NumToString(field.value.offset) + ")";
954           code += dest_mask;
955         } else {
956           code += offset_prefix + getter;
957           code += "(o + " + lang_.accessor_prefix + "bb_pos)" + dest_mask;
958           code += " : " + default_cast;
959           code += GenDefaultValue(field.value);
960         }
961       } else {
962         switch (field.value.type.base_type) {
963           case BASE_TYPE_STRUCT:
964             if (lang_.language != IDLOptions::kCSharp) {
965               code += "(" + type_name + " obj" + ")";
966             } else {
967               code += lang_.getter_prefix;
968               member_suffix += lang_.getter_suffix;
969             }
970             if (struct_def.fixed) {
971               code += " { return " + obj + ".__assign(" + lang_.accessor_prefix;
972               code += "bb_pos + " + NumToString(field.value.offset) + ", ";
973               code += lang_.accessor_prefix + "bb)";
974             } else {
975               code += offset_prefix + conditional_cast;
976               code += obj + ".__assign(";
977               code += field.value.type.struct_def->fixed
978                           ? "o + " + lang_.accessor_prefix + "bb_pos"
979                           : lang_.accessor_prefix + "__indirect(o + " +
980                                 lang_.accessor_prefix + "bb_pos)";
981               code += ", " + lang_.accessor_prefix + "bb) : null";
982             }
983             break;
984           case BASE_TYPE_STRING:
985             code += lang_.getter_prefix;
986             member_suffix += lang_.getter_suffix;
987             code += offset_prefix + getter + "(o + " + lang_.accessor_prefix;
988             code += "bb_pos) : null";
989             break;
990           case BASE_TYPE_VECTOR: {
991             auto vectortype = field.value.type.VectorType();
992             if (vectortype.base_type == BASE_TYPE_UNION &&
993                 lang_.language == IDLOptions::kCSharp) {
994                   conditional_cast = "(TTable?)";
995                   getter += "<TTable>";
996             }
997             code += "(";
998             if (vectortype.base_type == BASE_TYPE_STRUCT) {
999               if (lang_.language != IDLOptions::kCSharp)
1000                 code += type_name + " obj, ";
1001               getter = obj + ".__assign";
1002             } else if (vectortype.base_type == BASE_TYPE_UNION) {
1003               if (lang_.language != IDLOptions::kCSharp)
1004                 code += type_name + " obj, ";
1005             }
1006             code += "int j)";
1007             const auto body = offset_prefix + conditional_cast + getter + "(";
1008             if (vectortype.base_type == BASE_TYPE_UNION) {
1009               if (lang_.language != IDLOptions::kCSharp)
1010                 code += body + "obj, ";
1011               else
1012                 code += " where TTable : struct, IFlatbufferObject" + body;
1013             } else {
1014               code += body;
1015             }
1016             auto index = lang_.accessor_prefix + "__vector(o) + j * " +
1017                          NumToString(InlineSize(vectortype));
1018             if (vectortype.base_type == BASE_TYPE_STRUCT) {
1019               code += vectortype.struct_def->fixed
1020                           ? index
1021                           : lang_.accessor_prefix + "__indirect(" + index + ")";
1022               code += ", " + lang_.accessor_prefix + "bb";
1023             } else {
1024               code += index;
1025             }
1026             code += ")" + dest_mask + " : ";
1027 
1028             code +=
1029                 field.value.type.element == BASE_TYPE_BOOL
1030                     ? "false"
1031                     : (IsScalar(field.value.type.element) ? default_cast + "0"
1032                                                           : "null");
1033             break;
1034           }
1035           case BASE_TYPE_UNION:
1036             if (lang_.language == IDLOptions::kCSharp) {
1037               code += "() where TTable : struct, IFlatbufferObject";
1038               code += offset_prefix + "(TTable?)" + getter;
1039               code += "<TTable>(o) : null";
1040             } else {
1041               code += "(" + type_name + " obj)" + offset_prefix + getter;
1042               code += "(obj, o) : null";
1043             }
1044             break;
1045           default: FLATBUFFERS_ASSERT(0);
1046         }
1047       }
1048       code += member_suffix;
1049       code += "}\n";
1050       if (field.value.type.base_type == BASE_TYPE_VECTOR) {
1051         code +=
1052             "  public int " + MakeCamel(field.name, lang_.first_camel_upper);
1053         code += "Length";
1054         code += lang_.getter_prefix;
1055         code += offset_prefix;
1056         code += lang_.accessor_prefix + "__vector_len(o) : 0; ";
1057         code += lang_.getter_suffix;
1058         code += "}\n";
1059         // See if we should generate a by-key accessor.
1060         if (field.value.type.element == BASE_TYPE_STRUCT &&
1061             !field.value.type.struct_def->fixed) {
1062           auto &sd = *field.value.type.struct_def;
1063           auto &fields = sd.fields.vec;
1064           for (auto kit = fields.begin(); kit != fields.end(); ++kit) {
1065             auto &key_field = **kit;
1066             if (key_field.key) {
1067               auto qualified_name = WrapInNameSpace(sd);
1068               code += "  public " + qualified_name + lang_.optional_suffix + " ";
1069               code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey(";
1070               code += GenTypeNameDest(key_field.value.type) + " key)";
1071               code += offset_prefix;
1072               code += qualified_name + ".__lookup_by_key(";
1073               if (lang_.language == IDLOptions::kJava)
1074                 code += "null, ";
1075               code += lang_.accessor_prefix + "__vector(o), key, ";
1076               code += lang_.accessor_prefix + "bb) : null; ";
1077               code += "}\n";
1078               if (lang_.language == IDLOptions::kJava) {
1079                 code += "  public " + qualified_name + lang_.optional_suffix + " ";
1080                 code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey(";
1081                 code += qualified_name + lang_.optional_suffix + " obj, ";
1082                 code += GenTypeNameDest(key_field.value.type) + " key)";
1083                 code += offset_prefix;
1084                 code += qualified_name + ".__lookup_by_key(obj, ";
1085                 code += lang_.accessor_prefix + "__vector(o), key, ";
1086                 code += lang_.accessor_prefix + "bb) : null; ";
1087                 code += "}\n";
1088               }
1089               break;
1090             }
1091           }
1092         }
1093       }
1094       // Generate a ByteBuffer accessor for strings & vectors of scalars.
1095       if ((field.value.type.base_type == BASE_TYPE_VECTOR &&
1096            IsScalar(field.value.type.VectorType().base_type)) ||
1097           field.value.type.base_type == BASE_TYPE_STRING) {
1098         switch (lang_.language) {
1099           case IDLOptions::kJava:
1100             code += "  public ByteBuffer ";
1101             code += MakeCamel(field.name, lang_.first_camel_upper);
1102             code += "AsByteBuffer() { return ";
1103             code += lang_.accessor_prefix + "__vector_as_bytebuffer(";
1104             code += NumToString(field.value.offset) + ", ";
1105             code +=
1106                 NumToString(field.value.type.base_type == BASE_TYPE_STRING
1107                                 ? 1
1108                                 : InlineSize(field.value.type.VectorType()));
1109             code += "); }\n";
1110             code += "  public ByteBuffer ";
1111             code += MakeCamel(field.name, lang_.first_camel_upper);
1112             code += "InByteBuffer(ByteBuffer _bb) { return ";
1113             code += lang_.accessor_prefix + "__vector_in_bytebuffer(_bb, ";
1114             code += NumToString(field.value.offset) + ", ";
1115             code +=
1116                 NumToString(field.value.type.base_type == BASE_TYPE_STRING
1117                                 ? 1
1118                                 : InlineSize(field.value.type.VectorType()));
1119             code += "); }\n";
1120             break;
1121           case IDLOptions::kCSharp:
1122             code += "#if ENABLE_SPAN_T\n";
1123             code += "  public Span<byte> Get";
1124             code += MakeCamel(field.name, lang_.first_camel_upper);
1125             code += "Bytes() { return ";
1126             code += lang_.accessor_prefix + "__vector_as_span(";
1127             code += NumToString(field.value.offset);
1128             code += "); }\n";
1129             code += "#else\n";
1130             code += "  public ArraySegment<byte>? Get";
1131             code += MakeCamel(field.name, lang_.first_camel_upper);
1132             code += "Bytes() { return ";
1133             code += lang_.accessor_prefix + "__vector_as_arraysegment(";
1134             code += NumToString(field.value.offset);
1135             code += "); }\n";
1136             code += "#endif\n";
1137 
1138             // For direct blockcopying the data into a typed array
1139             code += "  public ";
1140             code += GenTypeBasic(field.value.type.VectorType());
1141             code += "[] Get";
1142             code += MakeCamel(field.name, lang_.first_camel_upper);
1143             code += "Array() { return ";
1144             code += lang_.accessor_prefix + "__vector_as_array<";
1145             code += GenTypeBasic(field.value.type.VectorType());
1146             code += ">(";
1147             code += NumToString(field.value.offset);
1148             code += "); }\n";
1149             break;
1150           default: break;
1151         }
1152       }
1153       // generate object accessors if is nested_flatbuffer
1154       if (field.nested_flatbuffer) {
1155         auto nested_type_name = WrapInNameSpace(*field.nested_flatbuffer);
1156         auto nested_method_name =
1157             MakeCamel(field.name, lang_.first_camel_upper) + "As" +
1158             nested_type_name;
1159         auto get_nested_method_name = nested_method_name;
1160         if (lang_.language == IDLOptions::kCSharp) {
1161           get_nested_method_name = "Get" + nested_method_name;
1162           conditional_cast =
1163               "(" + nested_type_name + lang_.optional_suffix + ")";
1164         }
1165         if (lang_.language != IDLOptions::kCSharp) {
1166           code += "  public " + nested_type_name + lang_.optional_suffix + " ";
1167           code += nested_method_name + "() { return ";
1168           code +=
1169               get_nested_method_name + "(new " + nested_type_name + "()); }\n";
1170         } else {
1171           obj = "(new " + nested_type_name + "())";
1172         }
1173         code += "  public " + nested_type_name + lang_.optional_suffix + " ";
1174         code += get_nested_method_name + "(";
1175         if (lang_.language != IDLOptions::kCSharp)
1176           code += nested_type_name + " obj";
1177         code += ") { int o = " + lang_.accessor_prefix + "__offset(";
1178         code += NumToString(field.value.offset) + "); ";
1179         code += "return o != 0 ? " + conditional_cast + obj + ".__assign(";
1180         code += lang_.accessor_prefix;
1181         code += "__indirect(" + lang_.accessor_prefix + "__vector(o)), ";
1182         code += lang_.accessor_prefix + "bb) : null; }\n";
1183       }
1184       // Generate mutators for scalar fields or vectors of scalars.
1185       if (parser_.opts.mutable_buffer) {
1186         auto underlying_type = field.value.type.base_type == BASE_TYPE_VECTOR
1187                                    ? field.value.type.VectorType()
1188                                    : field.value.type;
1189         // Boolean parameters have to be explicitly converted to byte
1190         // representation.
1191         auto setter_parameter = underlying_type.base_type == BASE_TYPE_BOOL
1192                                     ? "(byte)(" + field.name + " ? 1 : 0)"
1193                                     : field.name;
1194         auto mutator_prefix = MakeCamel("mutate", lang_.first_camel_upper);
1195         // A vector mutator also needs the index of the vector element it should
1196         // mutate.
1197         auto mutator_params =
1198             (field.value.type.base_type == BASE_TYPE_VECTOR ? "(int j, "
1199                                                             : "(") +
1200             GenTypeNameDest(underlying_type) + " " + field.name + ") { ";
1201         auto setter_index =
1202             field.value.type.base_type == BASE_TYPE_VECTOR
1203                 ? lang_.accessor_prefix + "__vector(o) + j * " +
1204                       NumToString(InlineSize(underlying_type))
1205                 : (struct_def.fixed
1206                        ? lang_.accessor_prefix + "bb_pos + " +
1207                              NumToString(field.value.offset)
1208                        : "o + " + lang_.accessor_prefix + "bb_pos");
1209         if (IsScalar(field.value.type.base_type) ||
1210             (field.value.type.base_type == BASE_TYPE_VECTOR &&
1211              IsScalar(field.value.type.VectorType().base_type))) {
1212           code += "  public ";
1213           code += struct_def.fixed ? "void " : lang_.bool_type;
1214           code += mutator_prefix + MakeCamel(field.name, true);
1215           code += mutator_params;
1216           if (struct_def.fixed) {
1217             code += GenSetter(underlying_type) + "(" + setter_index + ", ";
1218             code += src_cast + setter_parameter + "); }\n";
1219           } else {
1220             code += "int o = " + lang_.accessor_prefix + "__offset(";
1221             code += NumToString(field.value.offset) + ");";
1222             code += " if (o != 0) { " + GenSetter(underlying_type);
1223             code += "(" + setter_index + ", " + src_cast + setter_parameter +
1224                     "); return true; } else { return false; } }\n";
1225           }
1226         }
1227       }
1228     }
1229     code += "\n";
1230     flatbuffers::FieldDef *key_field = nullptr;
1231     if (struct_def.fixed) {
1232       // create a struct constructor function
1233       code += "  public static " + GenOffsetType(struct_def) + " ";
1234       code += FunctionStart('C') + "reate";
1235       code += struct_def.name + "(FlatBufferBuilder builder";
1236       GenStructArgs(struct_def, code_ptr, "");
1237       code += ") {\n";
1238       GenStructBody(struct_def, code_ptr, "");
1239       code += "    return ";
1240       code += GenOffsetConstruct(
1241           struct_def, "builder." + std::string(lang_.get_fbb_offset));
1242       code += ";\n  }\n";
1243     } else {
1244       // Generate a method that creates a table in one go. This is only possible
1245       // when the table has no struct fields, since those have to be created
1246       // inline, and there's no way to do so in Java.
1247       bool has_no_struct_fields = true;
1248       int num_fields = 0;
1249       for (auto it = struct_def.fields.vec.begin();
1250            it != struct_def.fields.vec.end(); ++it) {
1251         auto &field = **it;
1252         if (field.deprecated) continue;
1253         if (IsStruct(field.value.type)) {
1254           has_no_struct_fields = false;
1255         } else {
1256           num_fields++;
1257         }
1258       }
1259       // JVM specifications restrict default constructor params to be < 255.
1260       // Longs and doubles take up 2 units, so we set the limit to be < 127.
1261       if (has_no_struct_fields && num_fields && num_fields < 127) {
1262         // Generate a table constructor of the form:
1263         // public static int createName(FlatBufferBuilder builder, args...)
1264         code += "  public static " + GenOffsetType(struct_def) + " ";
1265         code += FunctionStart('C') + "reate" + struct_def.name;
1266         code += "(FlatBufferBuilder builder";
1267         for (auto it = struct_def.fields.vec.begin();
1268              it != struct_def.fields.vec.end(); ++it) {
1269           auto &field = **it;
1270           if (field.deprecated) continue;
1271           code += ",\n      ";
1272           code += GenTypeBasic(DestinationType(field.value.type, false));
1273           code += " ";
1274           code += field.name;
1275           if (!IsScalar(field.value.type.base_type)) code += "Offset";
1276 
1277           // Java doesn't have defaults, which means this method must always
1278           // supply all arguments, and thus won't compile when fields are added.
1279           if (lang_.language != IDLOptions::kJava) {
1280             code += " = ";
1281             code += GenDefaultValueBasic(field.value);
1282           }
1283         }
1284         code += ") {\n    builder.";
1285         code += FunctionStart('S') + "tartObject(";
1286         code += NumToString(struct_def.fields.vec.size()) + ");\n";
1287         for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
1288              size; size /= 2) {
1289           for (auto it = struct_def.fields.vec.rbegin();
1290                it != struct_def.fields.vec.rend(); ++it) {
1291             auto &field = **it;
1292             if (!field.deprecated &&
1293                 (!struct_def.sortbysize ||
1294                  size == SizeOf(field.value.type.base_type))) {
1295               code += "    " + struct_def.name + ".";
1296               code += FunctionStart('A') + "dd";
1297               code += MakeCamel(field.name) + "(builder, " + field.name;
1298               if (!IsScalar(field.value.type.base_type)) code += "Offset";
1299               code += ");\n";
1300             }
1301           }
1302         }
1303         code += "    return " + struct_def.name + ".";
1304         code += FunctionStart('E') + "nd" + struct_def.name;
1305         code += "(builder);\n  }\n\n";
1306       }
1307       // Generate a set of static methods that allow table construction,
1308       // of the form:
1309       // public static void addName(FlatBufferBuilder builder, short name)
1310       // { builder.addShort(id, name, default); }
1311       // Unlike the Create function, these always work.
1312       code += "  public static void " + FunctionStart('S') + "tart";
1313       code += struct_def.name;
1314       code += "(FlatBufferBuilder builder) { builder.";
1315       code += FunctionStart('S') + "tartObject(";
1316       code += NumToString(struct_def.fields.vec.size()) + "); }\n";
1317       for (auto it = struct_def.fields.vec.begin();
1318            it != struct_def.fields.vec.end(); ++it) {
1319         auto &field = **it;
1320         if (field.deprecated) continue;
1321         if (field.key) key_field = &field;
1322         code += "  public static void " + FunctionStart('A') + "dd";
1323         code += MakeCamel(field.name);
1324         code += "(FlatBufferBuilder builder, ";
1325         code += GenTypeBasic(DestinationType(field.value.type, false));
1326         auto argname = MakeCamel(field.name, false);
1327         if (!IsScalar(field.value.type.base_type)) argname += "Offset";
1328         code += " " + argname + ") { builder." + FunctionStart('A') + "dd";
1329         code += GenMethod(field.value.type) + "(";
1330         code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
1331         code += SourceCastBasic(field.value.type);
1332         code += argname;
1333         if (!IsScalar(field.value.type.base_type) &&
1334             field.value.type.base_type != BASE_TYPE_UNION &&
1335             lang_.language == IDLOptions::kCSharp) {
1336           code += ".Value";
1337         }
1338         code += ", ";
1339         if (lang_.language == IDLOptions::kJava)
1340           code += SourceCastBasic(field.value.type);
1341         code += GenDefaultValue(field.value, false);
1342         code += "); }\n";
1343         if (field.value.type.base_type == BASE_TYPE_VECTOR) {
1344           auto vector_type = field.value.type.VectorType();
1345           auto alignment = InlineAlignment(vector_type);
1346           auto elem_size = InlineSize(vector_type);
1347           if (!IsStruct(vector_type)) {
1348             // Generate a method to create a vector from a Java array.
1349             code += "  public static " + GenVectorOffsetType() + " ";
1350             code += FunctionStart('C') + "reate";
1351             code += MakeCamel(field.name);
1352             code += "Vector(FlatBufferBuilder builder, ";
1353             code += GenTypeBasic(vector_type) + "[] data) ";
1354             code += "{ builder." + FunctionStart('S') + "tartVector(";
1355             code += NumToString(elem_size);
1356             code += ", data." + FunctionStart('L') + "ength, ";
1357             code += NumToString(alignment);
1358             code += "); for (int i = data.";
1359             code += FunctionStart('L') + "ength - 1; i >= 0; i--) builder.";
1360             code += FunctionStart('A') + "dd";
1361             code += GenMethod(vector_type);
1362             code += "(";
1363             code += SourceCastBasic(vector_type, false);
1364             code += "data[i]";
1365             if (lang_.language == IDLOptions::kCSharp &&
1366                 (vector_type.base_type == BASE_TYPE_STRUCT ||
1367                  vector_type.base_type == BASE_TYPE_STRING))
1368               code += ".Value";
1369             code += "); return ";
1370             code += "builder." + FunctionStart('E') + "ndVector(); }\n";
1371             // For C#, include a block copy method signature.
1372             if (lang_.language == IDLOptions::kCSharp) {
1373               code += "  public static " + GenVectorOffsetType() + " ";
1374               code += FunctionStart('C') + "reate";
1375               code += MakeCamel(field.name);
1376               code += "VectorBlock(FlatBufferBuilder builder, ";
1377               code += GenTypeBasic(vector_type) + "[] data) ";
1378               code += "{ builder." + FunctionStart('S') + "tartVector(";
1379               code += NumToString(elem_size);
1380               code += ", data." + FunctionStart('L') + "ength, ";
1381               code += NumToString(alignment);
1382               code += "); builder.Add(data); return builder.EndVector(); }\n";
1383             }
1384           }
1385           // Generate a method to start a vector, data to be added manually
1386           // after.
1387           code += "  public static void " + FunctionStart('S') + "tart";
1388           code += MakeCamel(field.name);
1389           code += "Vector(FlatBufferBuilder builder, int numElems) ";
1390           code += "{ builder." + FunctionStart('S') + "tartVector(";
1391           code += NumToString(elem_size);
1392           code += ", numElems, " + NumToString(alignment);
1393           code += "); }\n";
1394         }
1395       }
1396       code += "  public static " + GenOffsetType(struct_def) + " ";
1397       code += FunctionStart('E') + "nd" + struct_def.name;
1398       code += "(FlatBufferBuilder builder) {\n    int o = builder.";
1399       code += FunctionStart('E') + "ndObject();\n";
1400       for (auto it = struct_def.fields.vec.begin();
1401            it != struct_def.fields.vec.end(); ++it) {
1402         auto &field = **it;
1403         if (!field.deprecated && field.required) {
1404           code += "    builder." + FunctionStart('R') + "equired(o, ";
1405           code += NumToString(field.value.offset);
1406           code += ");  // " + field.name + "\n";
1407         }
1408       }
1409       code += "    return " + GenOffsetConstruct(struct_def, "o") + ";\n  }\n";
1410       if (parser_.root_struct_def_ == &struct_def) {
1411         std::string size_prefix[] = { "", "SizePrefixed" };
1412         for (int i = 0; i < 2; ++i) {
1413           code += "  public static void ";
1414           code += FunctionStart('F') + "inish" + size_prefix[i] +
1415                   struct_def.name;
1416           code += "Buffer(FlatBufferBuilder builder, " +
1417                   GenOffsetType(struct_def);
1418           code += " offset) {";
1419           code += " builder." + FunctionStart('F') + "inish" + size_prefix[i] +
1420                   "(offset";
1421           if (lang_.language == IDLOptions::kCSharp) { code += ".Value"; }
1422 
1423           if (parser_.file_identifier_.length())
1424             code += ", \"" + parser_.file_identifier_ + "\"";
1425           code += "); }\n";
1426         }
1427       }
1428     }
1429     // Only generate key compare function for table,
1430     // because `key_field` is not set for struct
1431     if (struct_def.has_key && !struct_def.fixed) {
1432       if (lang_.language == IDLOptions::kJava) {
1433         code += "\n  @Override\n  protected int keysCompare(";
1434         code += "Integer o1, Integer o2, ByteBuffer _bb) {";
1435         code += GenKeyGetter(key_field);
1436         code += " }\n";
1437       } else {
1438         code += "\n  public static VectorOffset ";
1439         code += "CreateSortedVectorOf" + struct_def.name;
1440         code += "(FlatBufferBuilder builder, ";
1441         code += "Offset<" + struct_def.name + ">";
1442         code += "[] offsets) {\n";
1443         code += "    Array.Sort(offsets, (Offset<" + struct_def.name +
1444                 "> o1, Offset<" + struct_def.name + "> o2) => " +
1445                 GenKeyGetter(key_field);
1446         code += ");\n";
1447         code += "    return builder.CreateVectorOfTables(offsets);\n  }\n";
1448       }
1449 
1450       code += "\n  public static " + struct_def.name + lang_.optional_suffix;
1451       code += " __lookup_by_key(";
1452       if (lang_.language == IDLOptions::kJava)
1453         code +=  struct_def.name + " obj, ";
1454       code += "int vectorLocation, ";
1455       code += GenTypeNameDest(key_field->value.type);
1456       code += " key, ByteBuffer bb) {\n";
1457       if (key_field->value.type.base_type == BASE_TYPE_STRING) {
1458         code += "    byte[] byteKey = ";
1459         if (lang_.language == IDLOptions::kJava)
1460           code += "key.getBytes(Table.UTF8_CHARSET.get());\n";
1461         else
1462           code += "System.Text.Encoding.UTF8.GetBytes(key);\n";
1463       }
1464       code += "    int span = ";
1465       code += "bb." + FunctionStart('G') + "etInt(vectorLocation - 4);\n";
1466       code += "    int start = 0;\n";
1467       code += "    while (span != 0) {\n";
1468       code += "      int middle = span / 2;\n";
1469       code += GenLookupKeyGetter(key_field);
1470       code += "      if (comp > 0) {\n";
1471       code += "        span = middle;\n";
1472       code += "      } else if (comp < 0) {\n";
1473       code += "        middle++;\n";
1474       code += "        start += middle;\n";
1475       code += "        span -= middle;\n";
1476       code += "      } else {\n";
1477       code += "        return ";
1478       if (lang_.language == IDLOptions::kJava)
1479         code += "(obj == null ? new " + struct_def.name + "() : obj)";
1480       else
1481         code += "new " + struct_def.name + "()";
1482       code += ".__assign(tableOffset, bb);\n";
1483       code += "      }\n    }\n";
1484       code += "    return null;\n";
1485       code += "  }\n";
1486     }
1487     code += "}";
1488     // Java does not need the closing semi-colon on class definitions.
1489     code += (lang_.language != IDLOptions::kJava) ? ";" : "";
1490     code += "\n\n";
1491   }
1492   const LanguageParameters &lang_;
1493   // This tracks the current namespace used to determine if a type need to be
1494   // prefixed by its namespace
1495   const Namespace *cur_name_space_;
1496 };
1497 }  // namespace general
1498 
GenerateGeneral(const Parser & parser,const std::string & path,const std::string & file_name)1499 bool GenerateGeneral(const Parser &parser, const std::string &path,
1500                      const std::string &file_name) {
1501   general::GeneralGenerator generator(parser, path, file_name);
1502   return generator.generate();
1503 }
1504 
GeneralMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)1505 std::string GeneralMakeRule(const Parser &parser, const std::string &path,
1506                             const std::string &file_name) {
1507   FLATBUFFERS_ASSERT(parser.opts.lang <= IDLOptions::kMAX);
1508   const auto &lang = GetLangParams(parser.opts.lang);
1509 
1510   std::string make_rule;
1511 
1512   for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end();
1513        ++it) {
1514     auto &enum_def = **it;
1515     if (make_rule != "") make_rule += " ";
1516     std::string directory =
1517         BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace);
1518     make_rule += directory + enum_def.name + lang.file_extension;
1519   }
1520 
1521   for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
1522        ++it) {
1523     auto &struct_def = **it;
1524     if (make_rule != "") make_rule += " ";
1525     std::string directory = BaseGenerator::NamespaceDir(
1526         parser, path, *struct_def.defined_namespace);
1527     make_rule += directory + struct_def.name + lang.file_extension;
1528   }
1529 
1530   make_rule += ": ";
1531   auto included_files = parser.GetIncludedFilesRecursive(file_name);
1532   for (auto it = included_files.begin(); it != included_files.end(); ++it) {
1533     make_rule += " " + *it;
1534   }
1535   return make_rule;
1536 }
1537 
BinaryFileName(const Parser & parser,const std::string & path,const std::string & file_name)1538 std::string BinaryFileName(const Parser &parser, const std::string &path,
1539                            const std::string &file_name) {
1540   auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin";
1541   return path + file_name + "." + ext;
1542 }
1543 
GenerateBinary(const Parser & parser,const std::string & path,const std::string & file_name)1544 bool GenerateBinary(const Parser &parser, const std::string &path,
1545                     const std::string &file_name) {
1546   return !parser.builder_.GetSize() ||
1547          flatbuffers::SaveFile(
1548              BinaryFileName(parser, path, file_name).c_str(),
1549              reinterpret_cast<char *>(parser.builder_.GetBufferPointer()),
1550              parser.builder_.GetSize(), true);
1551 }
1552 
BinaryMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)1553 std::string BinaryMakeRule(const Parser &parser, const std::string &path,
1554                            const std::string &file_name) {
1555   if (!parser.builder_.GetSize()) return "";
1556   std::string filebase =
1557       flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
1558   std::string make_rule =
1559       BinaryFileName(parser, path, filebase) + ": " + file_name;
1560   auto included_files =
1561       parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
1562   for (auto it = included_files.begin(); it != included_files.end(); ++it) {
1563     make_rule += " " + *it;
1564   }
1565   return make_rule;
1566 }
1567 
1568 }  // namespace flatbuffers
1569