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