forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
names.cc
181 lines (153 loc) · 6.44 KB
/
names.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/hpb/names.h"
#include <string>
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/hpb/keywords.h"
namespace google::protobuf::hpb_generator {
namespace protobuf = ::proto2;
namespace {
// TODO: b/346865271 append ::hpb instead of ::protos after namespace swap
std::string NamespaceFromPackageName(absl::string_view package_name) {
return absl::StrCat(absl::StrReplaceAll(package_name, {{".", "::"}}),
"::protos");
}
std::string DotsToColons(const absl::string_view name) {
return absl::StrReplaceAll(name, {{".", "::"}});
}
std::string Namespace(const absl::string_view package) {
if (package.empty()) return "";
return "::" + DotsToColons(package);
}
// Return the qualified C++ name for a file level symbol.
std::string QualifiedFileLevelSymbol(const protobuf::FileDescriptor* file,
const std::string& name) {
if (file->package().empty()) {
return absl::StrCat("::", name);
}
// Append ::protos postfix to package name.
return absl::StrCat(Namespace(file->package()), "::protos::", name);
}
std::string CppTypeInternal(const protobuf::FieldDescriptor* field,
bool is_const, bool is_type_parameter) {
std::string maybe_const = is_const ? "const " : "";
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
if (is_type_parameter) {
return absl::StrCat(maybe_const,
QualifiedClassName(field->message_type()));
} else {
return absl::StrCat(maybe_const,
QualifiedClassName(field->message_type()), "*");
}
}
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
return "bool";
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return "float";
case protobuf::FieldDescriptor::CPPTYPE_INT32:
case protobuf::FieldDescriptor::CPPTYPE_ENUM:
return "int32_t";
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
return "uint32_t";
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return "double";
case protobuf::FieldDescriptor::CPPTYPE_INT64:
return "int64_t";
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
return "uint64_t";
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return "absl::string_view";
default:
ABSL_LOG(FATAL) << "Unexpected type: " << field->cpp_type();
}
}
} // namespace
std::string ClassName(const protobuf::Descriptor* descriptor) {
const protobuf::Descriptor* parent = descriptor->containing_type();
std::string res;
// Classes in global namespace without package names are prefixed
// by hpb_ to avoid collision with C compiler structs defined in
// proto.upb.h.
if ((parent && parent->file()->package().empty()) ||
descriptor->file()->package().empty()) {
res = std::string(kNoPackageNamePrefix);
}
if (parent) res += ClassName(parent) + "_";
absl::StrAppend(&res, descriptor->name());
return ResolveKeywordConflict(res);
}
std::string QualifiedClassName(const protobuf::Descriptor* descriptor) {
return QualifiedFileLevelSymbol(descriptor->file(), ClassName(descriptor));
}
std::string QualifiedInternalClassName(const protobuf::Descriptor* descriptor) {
return QualifiedFileLevelSymbol(
descriptor->file(), absl::StrCat("internal::", ClassName(descriptor)));
}
std::string CppSourceFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.proto.cc";
}
std::string ForwardingHeaderFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.fwd.h";
}
std::string UpbCFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.h";
}
std::string CppHeaderFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.proto.h";
}
void WriteStartNamespace(const protobuf::FileDescriptor* file, Output& output) {
// Skip namespace generation if package name is not specified.
if (file->package().empty()) {
return;
}
output("namespace $0 {\n\n", NamespaceFromPackageName(file->package()));
}
void WriteEndNamespace(const protobuf::FileDescriptor* file, Output& output) {
if (file->package().empty()) {
return;
}
output("} // namespace $0\n\n", NamespaceFromPackageName(file->package()));
}
std::string CppConstType(const protobuf::FieldDescriptor* field) {
return CppTypeInternal(field, /* is_const= */ true,
/* is_type_parameter= */ false);
}
std::string CppTypeParameterName(const protobuf::FieldDescriptor* field) {
return CppTypeInternal(field, /* is_const= */ false,
/* is_type_parameter= */ true);
}
std::string MessageBaseType(const protobuf::FieldDescriptor* field,
bool is_const) {
ABSL_DCHECK(field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE);
std::string maybe_const = is_const ? "const " : "";
return maybe_const + QualifiedClassName(field->message_type());
}
std::string MessagePtrConstType(const protobuf::FieldDescriptor* field,
bool is_const) {
ABSL_DCHECK(field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE);
std::string maybe_const = is_const ? "const " : "";
return "::hpb::Ptr<" + maybe_const +
QualifiedClassName(field->message_type()) + ">";
}
std::string MessageCProxyType(const protobuf::FieldDescriptor* field,
bool is_const) {
ABSL_DCHECK(field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE);
std::string maybe_const = is_const ? "const " : "";
return maybe_const + QualifiedInternalClassName(field->message_type()) +
"CProxy";
}
std::string MessageProxyType(const protobuf::FieldDescriptor* field,
bool is_const) {
ABSL_DCHECK(field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE);
std::string maybe_const = is_const ? "const " : "";
return maybe_const + QualifiedInternalClassName(field->message_type()) +
"Proxy";
}
} // namespace protobuf
} // namespace google::hpb_generator