Coverage Report

Created: 2025-12-14 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flatbuffers/include/codegen/idl_namer.h
Line
Count
Source
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:
12
  explicit IdlNamer(Config config, std::set<std::string> keywords)
13
0
      : 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
0
  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
0
  std::string Type(const StructDef& d) const { return Type(d.name); }
33
0
  std::string Type(const EnumDef& d) const { return Type(d.name); }
34
35
0
  std::string Function(const Definition& s) const { return Function(s.name); }
36
0
  std::string Function(const std::string& prefix, const Definition& s) const {
37
0
    return Function(prefix + s.name);
38
0
  }
39
40
0
  std::string Field(const FieldDef& s) const { return Field(s.name); }
41
0
  std::string Field(const FieldDef& d, const std::string& s) const {
42
0
    return Field(d.name + "_" + s);
43
0
  }
44
45
0
  std::string Variable(const FieldDef& s) const { return Variable(s.name); }
46
47
0
  std::string Variable(const StructDef& s) const { return Variable(s.name); }
48
49
0
  std::string Variant(const EnumVal& s) const { return Variant(s.name); }
50
51
0
  std::string EnumVariant(const EnumDef& e, const EnumVal& v) const {
52
0
    return Type(e) + config_.enum_variant_seperator + Variant(v);
53
0
  }
54
55
0
  std::string ObjectType(const StructDef& d) const {
56
0
    return ObjectType(d.name);
57
0
  }
58
0
  std::string ObjectType(const EnumDef& d) const { return ObjectType(d.name); }
59
60
0
  std::string Method(const FieldDef& d, const std::string& suffix) const {
61
0
    return Method(d.name, suffix);
62
0
  }
63
0
  std::string Method(const std::string& prefix, const StructDef& d) const {
64
0
    return Method(prefix, d.name);
65
0
  }
66
0
  std::string Method(const std::string& prefix, const FieldDef& d) const {
67
0
    return Method(prefix, d.name);
68
0
  }
69
  std::string Method(const std::string& prefix, const FieldDef& d,
70
0
                     const std::string& suffix) const {
71
0
    return Method(prefix, d.name, suffix);
72
0
  }
73
74
0
  std::string Namespace(const struct Namespace& ns) const {
75
0
    return Namespace(ns.components);
76
0
  }
77
78
0
  std::string NamespacedEnumVariant(const EnumDef& e, const EnumVal& v) const {
79
0
    return NamespacedString(e.defined_namespace, EnumVariant(e, v));
80
0
  }
81
82
0
  std::string NamespacedType(const Definition& def) const {
83
0
    return NamespacedString(def.defined_namespace, Type(def.name));
84
0
  }
85
86
0
  std::string NamespacedObjectType(const Definition& def) const {
87
0
    return NamespacedString(def.defined_namespace, ObjectType(def.name));
88
0
  }
89
90
  std::string Directories(const struct Namespace& ns,
91
                          SkipDir skips = SkipDir::None,
92
0
                          Case input_case = Case::kUpperCamel) const {
93
0
    return Directories(ns.components, skips, input_case);
94
0
  }
95
96
  // Legacy fields do not really follow the usual config and should be
97
  // considered for deprecation.
98
99
0
  std::string LegacyRustNativeVariant(const EnumVal& v) const {
100
0
    return ConvertCase(EscapeKeyword(v.name), Case::kUpperCamel);
101
0
  }
102
103
0
  std::string LegacyRustFieldOffsetName(const FieldDef& field) const {
104
0
    return "VT_" + ConvertCase(EscapeKeyword(field.name), Case::kAllUpper);
105
0
  }
106
0
  std::string LegacyRustUnionTypeOffsetName(const FieldDef& field) const {
107
0
    return "VT_" +
108
0
           ConvertCase(EscapeKeyword(field.name + "_type"), Case::kAllUpper);
109
0
  }
110
111
0
  std::string LegacySwiftVariant(const EnumVal& ev) const {
112
0
    auto name = ev.name;
113
0
    if (isupper(name.front())) {
114
0
      std::transform(name.begin(), name.end(), name.begin(), CharToLower);
115
0
    }
116
0
    return EscapeKeyword(ConvertCase(name, Case::kLowerCamel));
117
0
  }
118
119
  // Also used by Kotlin, lol.
120
  std::string LegacyJavaMethod2(const std::string& prefix, const StructDef& sd,
121
0
                                const std::string& suffix) const {
122
0
    return prefix + sd.name + suffix;
123
0
  }
124
125
0
  std::string LegacyKotlinVariant(EnumVal& ev) const {
126
0
    // Namer assumes the input case is snake case which is wrong...
127
0
    return ConvertCase(EscapeKeyword(ev.name), Case::kLowerCamel);
128
0
  }
129
  // Kotlin methods escapes keywords after case conversion but before
130
  // prefixing and suffixing.
131
  std::string LegacyKotlinMethod(const std::string& prefix, const FieldDef& d,
132
0
                                 const std::string& suffix) const {
133
0
    return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
134
0
           suffix;
135
0
  }
136
  std::string LegacyKotlinMethod(const std::string& prefix, const StructDef& d,
137
0
                                 const std::string& suffix) const {
138
0
    return prefix + ConvertCase(EscapeKeyword(d.name), Case::kUpperCamel) +
139
0
           suffix;
140
0
  }
141
142
  // This is a mix of snake case and keep casing, when Ts should be using
143
  // lower camel case.
144
0
  std::string LegacyTsMutateMethod(const FieldDef& d) {
145
0
    return "mutate_" + d.name;
146
0
  }
147
148
0
  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
0
    return ConvertCase(d.name, config_.fields, Case::kLowerCamel) + "_type";
152
0
  }
153
154
 private:
155
  std::string NamespacedString(const struct Namespace* ns,
156
0
                               const std::string& str) const {
157
0
    std::string ret;
158
0
    if (ns != nullptr) {
159
0
      ret += Namespace(ns->components);
160
0
    }
161
0
    if (!ret.empty()) ret += config_.namespace_seperator;
162
0
    return ret + str;
163
0
  }
164
};
165
166
// This is a temporary helper function for code generators to call until all
167
// flag-overriding logic into flatc.cpp
168
inline Namer::Config WithFlagOptions(const Namer::Config& input,
169
                                     const IDLOptions& opts,
170
0
                                     const std::string& path) {
171
0
  Namer::Config result = input;
172
0
  result.object_prefix = opts.object_prefix;
173
0
  result.object_suffix = opts.object_suffix;
174
0
  result.output_path = path;
175
0
  result.filename_suffix = opts.filename_suffix;
176
0
  return result;
177
0
}
178
179
}  // namespace flatbuffers
180
181
#endif  // FLATBUFFERS_INCLUDE_CODEGEN_IDL_NAMER_H_