• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef FLATBUFFERS_INCLUDE_CODEGEN_IDL_NAMER_H_
2 #define FLATBUFFERS_INCLUDE_CODEGEN_IDL_NAMER_H_
3 
4 #include "codegen/namer.h"
5 #include "flatbuffers/idl.h"
6 
7 namespace flatbuffers {
8 
9 // Provides Namer capabilities to types defined in the flatbuffers IDL.
10 class IdlNamer : public Namer {
11  public:
IdlNamer(Config config,std::set<std::string> keywords)12   explicit IdlNamer(Config config, std::set<std::string> keywords)
13       : Namer(config, std::move(keywords)) {}
14 
15   using Namer::Constant;
16   using Namer::Directories;
17   using Namer::Field;
18   using Namer::File;
19   using Namer::Function;
20   using Namer::Method;
21   using Namer::Namespace;
22   using Namer::NamespacedType;
23   using Namer::ObjectType;
24   using Namer::Type;
25   using Namer::Variable;
26   using Namer::Variant;
27 
Constant(const FieldDef & d)28   std::string Constant(const FieldDef &d) const { return Constant(d.name); }
29 
30   // Types are always structs or enums so we can only expose these two
31   // overloads.
Type(const StructDef & d)32   std::string Type(const StructDef &d) const { return Type(d.name); }
Type(const EnumDef & d)33   std::string Type(const EnumDef &d) const { return Type(d.name); }
34 
Function(const Definition & s)35   std::string Function(const Definition &s) const { return Function(s.name); }
Function(const std::string & prefix,const Definition & s)36   std::string Function(const std::string& prefix, const Definition &s) const {
37     return Function(prefix + s.name);
38   }
39 
Field(const FieldDef & s)40   std::string Field(const FieldDef &s) const { return Field(s.name); }
Field(const FieldDef & d,const std::string & s)41   std::string Field(const FieldDef &d, const std::string &s) const {
42     return Field(d.name + "_" + s);
43   }
44 
Variable(const FieldDef & s)45   std::string Variable(const FieldDef &s) const { return Variable(s.name); }
46 
Variable(const StructDef & s)47   std::string Variable(const StructDef &s) const { return Variable(s.name); }
48 
Variant(const EnumVal & s)49   std::string Variant(const EnumVal &s) const { return Variant(s.name); }
50 
EnumVariant(const EnumDef & e,const EnumVal & v)51   std::string EnumVariant(const EnumDef &e, const EnumVal &v) const {
52     return Type(e) + config_.enum_variant_seperator + Variant(v);
53   }
54 
ObjectType(const StructDef & d)55   std::string ObjectType(const StructDef &d) const {
56     return ObjectType(d.name);
57   }
ObjectType(const EnumDef & d)58   std::string ObjectType(const EnumDef &d) const { return ObjectType(d.name); }
59 
Method(const FieldDef & d,const std::string & suffix)60   std::string Method(const FieldDef &d, const std::string &suffix) const {
61     return Method(d.name, suffix);
62   }
Method(const std::string & prefix,const StructDef & d)63   std::string Method(const std::string &prefix, const StructDef &d) const {
64     return Method(prefix, d.name);
65   }
Method(const std::string & prefix,const FieldDef & d)66   std::string Method(const std::string &prefix, const FieldDef &d) const {
67     return Method(prefix, d.name);
68   }
Method(const std::string & prefix,const FieldDef & d,const std::string & suffix)69   std::string Method(const std::string &prefix, const FieldDef &d,
70                      const std::string &suffix) const {
71     return Method(prefix, d.name, suffix);
72   }
73 
Namespace(const struct Namespace & ns)74   std::string Namespace(const struct Namespace &ns) const {
75     return Namespace(ns.components);
76   }
77 
NamespacedEnumVariant(const EnumDef & e,const EnumVal & v)78   std::string NamespacedEnumVariant(const EnumDef &e, const EnumVal &v) const {
79     return NamespacedString(e.defined_namespace, EnumVariant(e, v));
80   }
81 
NamespacedType(const Definition & def)82   std::string NamespacedType(const Definition &def) const {
83     return NamespacedString(def.defined_namespace, Type(def.name));
84   }
85 
NamespacedObjectType(const Definition & def)86   std::string NamespacedObjectType(const Definition &def) const {
87     return NamespacedString(def.defined_namespace, ObjectType(def.name));
88   }
89 
90   std::string Directories(const struct Namespace &ns,
91                           SkipDir skips = SkipDir::None,
92                           Case input_case = Case::kUpperCamel) const {
93     return Directories(ns.components, skips, input_case);
94   }
95 
96   // Legacy fields do not really follow the usual config and should be
97   // considered for deprecation.
98 
LegacyRustNativeVariant(const EnumVal & v)99   std::string LegacyRustNativeVariant(const EnumVal &v) const {
100     return ConvertCase(EscapeKeyword(v.name), Case::kUpperCamel);
101   }
102 
LegacyRustFieldOffsetName(const FieldDef & field)103   std::string LegacyRustFieldOffsetName(const FieldDef &field) const {
104     return "VT_" + ConvertCase(EscapeKeyword(field.name), Case::kAllUpper);
105   }
LegacyRustUnionTypeOffsetName(const FieldDef & field)106     std::string LegacyRustUnionTypeOffsetName(const FieldDef &field) const {
107     return "VT_" + ConvertCase(EscapeKeyword(field.name + "_type"), Case::kAllUpper);
108   }
109 
110 
LegacySwiftVariant(const EnumVal & ev)111   std::string LegacySwiftVariant(const EnumVal &ev) const {
112     auto name = ev.name;
113     if (isupper(name.front())) {
114       std::transform(name.begin(), name.end(), name.begin(), CharToLower);
115     }
116     return EscapeKeyword(ConvertCase(name, Case::kLowerCamel));
117   }
118 
119   // Also used by Kotlin, lol.
LegacyJavaMethod2(const std::string & prefix,const StructDef & sd,const std::string & suffix)120   std::string LegacyJavaMethod2(const std::string &prefix, const StructDef &sd,
121                                 const std::string &suffix) const {
122     return prefix + sd.name + suffix;
123   }
124 
LegacyKotlinVariant(EnumVal & ev)125   std::string LegacyKotlinVariant(EnumVal &ev) const {
126     // Namer assumes the input case is snake case which is wrong...
127     return ConvertCase(EscapeKeyword(ev.name), Case::kLowerCamel);
128   }
129   // Kotlin methods escapes keywords after case conversion but before
130   // prefixing and suffixing.
LegacyKotlinMethod(const std::string & prefix,const FieldDef & d,const std::string & suffix)131   std::string LegacyKotlinMethod(const std::string &prefix, const FieldDef &d,
132                                  const std::string &suffix) const {
133     return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
134            suffix;
135   }
LegacyKotlinMethod(const std::string & prefix,const StructDef & d,const std::string & suffix)136   std::string LegacyKotlinMethod(const std::string &prefix, const StructDef &d,
137                                  const std::string &suffix) const {
138     return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
139            suffix;
140   }
141 
142   // This is a mix of snake case and keep casing, when Ts should be using
143   // lower camel case.
LegacyTsMutateMethod(const FieldDef & d)144   std::string LegacyTsMutateMethod(const FieldDef& d) {
145     return "mutate_" + d.name;
146   }
147 
LegacyRustUnionTypeMethod(const FieldDef & d)148   std::string LegacyRustUnionTypeMethod(const FieldDef &d) {
149     // assert d is a union
150     // d should convert case but not escape keywords due to historical reasons
151     return ConvertCase(d.name, config_.fields, Case::kLowerCamel) + "_type";
152   }
153 
154  private:
NamespacedString(const struct Namespace * ns,const std::string & str)155   std::string NamespacedString(const struct Namespace *ns,
156                                const std::string &str) const {
157     std::string ret;
158     if (ns != nullptr) { ret += Namespace(ns->components); }
159     if (!ret.empty()) ret += config_.namespace_seperator;
160     return ret + str;
161   }
162 };
163 
164 // This is a temporary helper function for code generators to call until all
165 // flag-overriding logic into flatc.cpp
WithFlagOptions(const Namer::Config & input,const IDLOptions & opts,const std::string & path)166 inline Namer::Config WithFlagOptions(const Namer::Config &input,
167                                      const IDLOptions &opts,
168                                      const std::string &path) {
169   Namer::Config result = input;
170   result.object_prefix = opts.object_prefix;
171   result.object_suffix = opts.object_suffix;
172   result.output_path = path;
173   result.filename_suffix = opts.filename_suffix;
174   return result;
175 }
176 
177 }  // namespace flatbuffers
178 
179 #endif  // FLATBUFFERS_INCLUDE_CODEGEN_IDL_NAMER_H_
180