blob: f60e30c3f51add41c5107b64ae2ec8df6829c3ac [file] [log] [blame]
Austin Schuh2dd86a92022-09-14 21:19:23 -07001#ifndef FLATBUFFERS_IDL_NAMER
2#define FLATBUFFERS_IDL_NAMER
3
4#include "flatbuffers/idl.h"
5#include "namer.h"
6
7namespace flatbuffers {
8
9// Provides Namer capabilities to types defined in the flatbuffers IDL.
10class IdlNamer : public Namer {
11 public:
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
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.
32 std::string Type(const StructDef &d) const { return Type(d.name); }
33 std::string Type(const EnumDef &d) const { return Type(d.name); }
34
35 std::string Function(const Definition &s) const { return Function(s.name); }
36 std::string Function(const std::string& prefix, const Definition &s) const {
37 return Function(prefix + s.name);
38 }
39
40 std::string Field(const FieldDef &s) const { return Field(s.name); }
41 std::string Field(const FieldDef &d, const std::string &s) const {
42 return Field(d.name + "_" + s);
43 }
44
45 std::string Variable(const FieldDef &s) const { return Variable(s.name); }
46
47 std::string Variable(const StructDef &s) const { return Variable(s.name); }
48
49 std::string Variant(const EnumVal &s) const { return Variant(s.name); }
50
51 std::string EnumVariant(const EnumDef &e, const EnumVal &v) const {
52 return Type(e) + config_.enum_variant_seperator + Variant(v);
53 }
54
55 std::string ObjectType(const StructDef &d) const {
56 return ObjectType(d.name);
57 }
58 std::string ObjectType(const EnumDef &d) const { return ObjectType(d.name); }
59
60 std::string Method(const FieldDef &d, const std::string &suffix) const {
61 return Method(d.name, suffix);
62 }
63 std::string Method(const std::string &prefix, const StructDef &d) const {
64 return Method(prefix, d.name);
65 }
66 std::string Method(const std::string &prefix, const FieldDef &d) const {
67 return Method(prefix, d.name);
68 }
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
74 std::string Namespace(const struct Namespace &ns) const {
75 return Namespace(ns.components);
76 }
77
78 std::string NamespacedEnumVariant(const EnumDef &e, const EnumVal &v) const {
79 return NamespacedString(e.defined_namespace, EnumVariant(e, v));
80 }
81
82 std::string NamespacedType(const Definition &def) const {
83 return NamespacedString(def.defined_namespace, Type(def.name));
84 }
85
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) const {
92 return Directories(ns.components, skips);
93 }
94
95 // Legacy fields do not really follow the usual config and should be
96 // considered for deprecation.
97
98 std::string LegacyRustNativeVariant(const EnumVal &v) const {
99 return ConvertCase(EscapeKeyword(v.name), Case::kUpperCamel);
100 }
101
102 std::string LegacyRustFieldOffsetName(const FieldDef &field) const {
103 return "VT_" + ConvertCase(EscapeKeyword(field.name), Case::kAllUpper);
104 }
105
106 std::string LegacySwiftVariant(const EnumVal &ev) const {
107 auto name = ev.name;
108 if (isupper(name.front())) {
109 std::transform(name.begin(), name.end(), name.begin(), CharToLower);
110 }
111 return EscapeKeyword(ConvertCase(name, Case::kLowerCamel));
112 }
113
114 // Also used by Kotlin, lol.
115 std::string LegacyJavaMethod2(const std::string &prefix, const StructDef &sd,
116 const std::string &suffix) const {
117 return prefix + sd.name + suffix;
118 }
119
120 std::string LegacyKotlinVariant(EnumVal &ev) const {
121 // Namer assumes the input case is snake case which is wrong...
122 return ConvertCase(EscapeKeyword(ev.name), Case::kLowerCamel);
123 }
124 // Kotlin methods escapes keywords after case conversion but before
125 // prefixing and suffixing.
126 std::string LegacyKotlinMethod(const std::string &prefix, const FieldDef &d,
127 const std::string &suffix) const {
128 return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
129 suffix;
130 }
131 std::string LegacyKotlinMethod(const std::string &prefix, const StructDef &d,
132 const std::string &suffix) const {
133 return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
134 suffix;
135 }
136
137 // This is a mix of snake case and keep casing, when Ts should be using
138 // lower camel case.
139 std::string LegacyTsMutateMethod(const FieldDef& d) {
140 return "mutate_" + d.name;
141 }
142
143 private:
144 std::string NamespacedString(const struct Namespace *ns,
145 const std::string &str) const {
146 std::string ret;
147 if (ns != nullptr) { ret += Namespace(ns->components); }
148 if (!ret.empty()) ret += config_.namespace_seperator;
149 return ret + str;
150 }
151};
152
153// This is a temporary helper function for code generators to call until all
154// flag-overriding logic into flatc.cpp
155inline Namer::Config WithFlagOptions(const Namer::Config &input,
156 const IDLOptions &opts,
157 const std::string &path) {
158 Namer::Config result = input;
159 result.object_prefix = opts.object_prefix;
160 result.object_suffix = opts.object_suffix;
161 result.output_path = path;
162 result.filename_suffix = opts.filename_suffix;
163 return result;
164}
165
166} // namespace flatbuffers
167
168#endif // FLATBUFFERS_IDL_NAMER