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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
// 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/gen_messages.h"
#include <cstddef>
#include <string>
#include <vector>
#include "google/protobuf/descriptor.pb.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/hpb/context.h"
#include "google/protobuf/compiler/hpb/gen_accessors.h"
#include "google/protobuf/compiler/hpb/gen_enums.h"
#include "google/protobuf/compiler/hpb/gen_extensions.h"
#include "google/protobuf/compiler/hpb/gen_utils.h"
#include "google/protobuf/compiler/hpb/names.h"
#include "google/protobuf/descriptor.h"
#include "upb_generator/c/names.h"
#include "upb_generator/minitable/names.h"
namespace google::protobuf::hpb_generator {
namespace protobuf = ::proto2;
using Sub = protobuf::io::Printer::Sub;
void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx);
void WriteModelPublicDeclaration(
const protobuf::Descriptor* descriptor,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
const std::vector<const protobuf::EnumDescriptor*>& file_enums,
Context& ctx);
void WriteExtensionIdentifiersInClassHeader(
const protobuf::Descriptor* message,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
Context& ctx);
void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx);
void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx);
void WriteInternalForwardDeclarationsInHeader(
const protobuf::Descriptor* message, Context& ctx);
void WriteDefaultInstanceHeader(const protobuf::Descriptor* message,
Context& ctx);
void WriteUsingEnumsInHeader(
const protobuf::Descriptor* message,
const std::vector<const protobuf::EnumDescriptor*>& file_enums,
Context& ctx);
// Writes message class declarations into .hpb.h.
//
// For each proto Foo, FooAccess and FooProxy/FooCProxy are generated
// that are exposed to users as Foo , Ptr<Foo> and Ptr<const Foo>.
void WriteMessageClassDeclarations(
const protobuf::Descriptor* descriptor,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
const std::vector<const protobuf::EnumDescriptor*>& file_enums,
Context& ctx) {
if (IsMapEntryMessage(descriptor)) {
// Skip map entry generation. Low level accessors for maps are
// generated that don't require a separate map type.
return;
}
// Forward declaration of Proto Class for GCC handling of free friend method.
ctx.Emit(
{Sub("class_name", ClassName(descriptor)),
Sub("model_access",
[&] { WriteModelAccessDeclaration(descriptor, ctx); })
.WithSuffix(";"),
Sub("fwd_decl",
[&] { WriteInternalForwardDeclarationsInHeader(descriptor, ctx); })
.WithSuffix(";"),
Sub("public_decl",
[&] {
WriteModelPublicDeclaration(descriptor, file_exts, file_enums,
ctx);
})
.WithSuffix(";"),
Sub("cproxy_decl", [&] { WriteModelCProxyDeclaration(descriptor, ctx); })
.WithSuffix(";"),
Sub("proxy_decl", [&] { WriteModelProxyDeclaration(descriptor, ctx); })
.WithSuffix(";")},
R"cc(
class $class_name$;
namespace internal {
$model_access$;
$fwd_decl$;
} // namespace internal
$public_decl$;
namespace internal {
$cproxy_decl$;
$proxy_decl$;
} // namespace internal
)cc");
}
void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx) {
ctx.Emit({Sub("class_name", ClassName(descriptor)),
Sub("qualified_class_name", QualifiedClassName(descriptor)),
Sub("upb_msg_name",
upb::generator::CApiMessageType(descriptor->full_name())),
Sub("field_accessors",
[&] { WriteFieldAccessorsInHeader(descriptor, ctx); })
.WithSuffix(";"),
Sub("oneof_accessors",
[&] { WriteOneofAccessorsInHeader(descriptor, ctx); })
.WithSuffix(";")},
R"cc(
class $class_name$Access {
public:
$class_name$Access() {}
$class_name$Access($upb_msg_name$* msg, upb_Arena* arena)
: msg_(msg), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
$class_name$Access(const $upb_msg_name$* msg, upb_Arena* arena)
: msg_(const_cast<$upb_msg_name$*>(msg)), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
$field_accessors$;
$oneof_accessors$;
private:
friend class $qualified_class_name$;
friend class $class_name$Proxy;
friend class $class_name$CProxy;
friend struct ::hpb::internal::PrivateAccess;
$upb_msg_name$* msg_;
upb_Arena* arena_;
};
)cc");
}
std::string UnderscoresToCamelCase(absl::string_view input,
bool cap_next_letter) {
std::string result;
for (size_t i = 0; i < input.size(); i++) {
if (absl::ascii_islower(input[i])) {
if (cap_next_letter) {
result += absl::ascii_toupper(input[i]);
} else {
result += input[i];
}
cap_next_letter = false;
} else if (absl::ascii_isupper(input[i])) {
// Capital letters are left as-is.
result += input[i];
cap_next_letter = false;
} else if (absl::ascii_isdigit(input[i])) {
result += input[i];
cap_next_letter = true;
} else {
cap_next_letter = true;
}
}
return result;
}
std::string FieldConstantName(const protobuf::FieldDescriptor* field) {
std::string field_name = UnderscoresToCamelCase(field->name(), true);
std::string result = absl::StrCat("k", field_name, "FieldNumber");
if (!field->is_extension() &&
field->containing_type()->FindFieldByCamelcaseName(
field->camelcase_name()) != field) {
// This field's camelcase name is not unique, add field number to make it
// unique.
absl::StrAppend(&result, "_", field->number());
}
return result;
}
void WriteConstFieldNumbers(Context& ctx,
const protobuf::Descriptor* descriptor) {
for (auto field : FieldRange(descriptor)) {
ctx.EmitLegacy("static constexpr ::uint32_t $0 = $1;\n",
FieldConstantName(field), field->number());
}
ctx.Emit("\n\n");
}
void WriteModelPublicDeclaration(
const protobuf::Descriptor* descriptor,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
const std::vector<const protobuf::EnumDescriptor*>& file_enums,
Context& ctx) {
ctx.EmitLegacy(
R"cc(
class $0 final : private internal::$0Access {
public:
using Access = internal::$0Access;
using Proxy = internal::$0Proxy;
using CProxy = internal::$0CProxy;
$0();
$0(const $0& from);
$0& operator=(const $3& from);
$0(const CProxy& from);
$0(const Proxy& from);
$0& operator=(const CProxy& from);
$0($0&& m)
: Access(std::exchange(m.msg_, nullptr),
std::exchange(m.arena_, nullptr)),
owned_arena_(std::move(m.owned_arena_)) {}
$0& operator=($0&& m) {
msg_ = std::exchange(m.msg_, nullptr);
arena_ = std::exchange(m.arena_, nullptr);
owned_arena_ = std::move(m.owned_arena_);
return *this;
}
)cc",
ClassName(descriptor),
::upb::generator::MiniTableMessageVarName(descriptor->full_name()),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessage, ctx);
WriteUsingEnumsInHeader(descriptor, file_enums, ctx);
WriteDefaultInstanceHeader(descriptor, ctx);
WriteExtensionIdentifiersInClassHeader(descriptor, file_exts, ctx);
if (descriptor->extension_range_count()) {
// for typetrait checking
ctx.EmitLegacy("using ExtendableType = $0;\n", ClassName(descriptor));
}
// Note: free function friends that are templates such as ::hpb::Parse
// require explicit <$2> type parameter in declaration to be able to compile
// with gcc otherwise the compiler will fail with
// "has not been declared within namespace" error. Even though there is a
// namespace qualifier, cross namespace matching fails.
ctx.EmitLegacy(
R"cc(
static const upb_MiniTable* minitable();
)cc",
ClassName(descriptor));
ctx.Emit("\n");
WriteConstFieldNumbers(ctx, descriptor);
ctx.EmitLegacy(
R"cc(
private:
const upb_Message* msg() const { return UPB_UPCAST(msg_); }
upb_Message* msg() { return UPB_UPCAST(msg_); }
upb_Arena* arena() const { return arena_; }
$0(upb_Message* msg, upb_Arena* arena) : $0Access() {
msg_ = ($1*)msg;
arena_ = owned_arena_.ptr();
upb_Arena_Fuse(arena_, arena);
}
::hpb::Arena owned_arena_;
friend struct ::hpb::internal::PrivateAccess;
friend Proxy;
friend CProxy;
friend absl::StatusOr<$2>(::hpb::Parse<$2>(
absl::string_view bytes,
const ::hpb::ExtensionRegistry& extension_registry));
friend upb_Arena* hpb::interop::upb::GetArena<$0>($0* message);
friend upb_Arena* hpb::interop::upb::GetArena<$0>(::hpb::Ptr<$0> message);
friend $0(hpb::interop::upb::MoveMessage<$0>(upb_Message* msg,
upb_Arena* arena));
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n\n");
}
void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx) {
// Foo::Proxy.
ctx.EmitLegacy(
R"cc(
class $0Proxy final : private internal::$0Access {
public:
$0Proxy() = delete;
$0Proxy(const $0Proxy& m) : internal::$0Access() {
msg_ = m.msg_;
arena_ = m.arena_;
}
$0Proxy($0* m) : internal::$0Access() {
msg_ = m->msg_;
arena_ = m->arena_;
}
$0Proxy operator=(const $0Proxy& m) {
msg_ = m.msg_;
arena_ = m.arena_;
return *this;
}
)cc",
ClassName(descriptor));
WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessageProxy, ctx);
ctx.Emit("\n");
ctx.EmitLegacy(
R"cc(
private:
upb_Message* msg() const { return UPB_UPCAST(msg_); }
upb_Arena* arena() const { return arena_; }
$0Proxy(upb_Message* msg, upb_Arena* arena)
: internal::$0Access(($1*)msg, arena) {}
friend $0::Proxy(::hpb::CreateMessage<$0>(::hpb::Arena& arena));
friend $0::Proxy(hpb::interop::upb::MakeHandle<$0>(upb_Message*, upb_Arena*));
friend struct ::hpb::internal::PrivateAccess;
friend class RepeatedFieldProxy;
friend class $0CProxy;
friend class $0Access;
friend class ::hpb::Ptr<$0>;
friend class ::hpb::Ptr<const $0>;
static const upb_MiniTable* minitable() { return $0::minitable(); }
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>(
const $0Proxy* message);
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>(
::hpb::Ptr<$0Proxy> message);
friend upb_Arena* hpb::interop::upb::GetArena<$2>($2* message);
friend upb_Arena* hpb::interop::upb::GetArena<$2>(::hpb::Ptr<$2> message);
static void Rebind($0Proxy& lhs, const $0Proxy& rhs) {
lhs.msg_ = rhs.msg_;
lhs.arena_ = rhs.arena_;
}
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n\n");
}
void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx) {
// Foo::CProxy.
ctx.EmitLegacy(
R"cc(
class $0CProxy final : private internal::$0Access {
public:
$0CProxy() = delete;
$0CProxy(const $0* m)
: internal::$0Access(m->msg_, hpb::interop::upb::GetArena(m)) {}
$0CProxy($0Proxy m);
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessageCProxy,
ctx);
ctx.EmitLegacy(
R"cc(
private:
using AsNonConst = $0Proxy;
const upb_Message* msg() const { return UPB_UPCAST(msg_); }
upb_Arena* arena() const { return arena_; }
$0CProxy(const upb_Message* msg, upb_Arena* arena)
: internal::$0Access(($1*)msg, arena){};
friend struct ::hpb::internal::PrivateAccess;
friend class RepeatedFieldProxy;
friend class ::hpb::Ptr<$0>;
friend class ::hpb::Ptr<const $0>;
static const upb_MiniTable* minitable() { return $0::minitable(); }
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>(
const $0CProxy* message);
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>(
::hpb::Ptr<$0CProxy> message);
static void Rebind($0CProxy& lhs, const $0CProxy& rhs) {
lhs.msg_ = rhs.msg_;
lhs.arena_ = rhs.arena_;
}
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
ctx.Emit("};\n\n");
}
void WriteDefaultInstanceHeader(const protobuf::Descriptor* message,
Context& ctx) {
ctx.EmitLegacy(" static ::hpb::Ptr<const $0> default_instance();\n",
ClassName(message));
}
void WriteMessageImplementation(
const protobuf::Descriptor* descriptor,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
Context& ctx) {
bool message_is_map_entry = descriptor->options().map_entry();
if (!message_is_map_entry) {
// Constructor.
ctx.EmitLegacy(
R"cc(
$0::$0() : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = $1_new(arena_);
}
$0::$0(const $0& from) : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
}
$0::$0(const CProxy& from) : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::interop::upb::GetMessage(&from), &$2, arena_);
}
$0::$0(const Proxy& from) : $0(static_cast<const CProxy&>(from)) {}
internal::$0CProxy::$0CProxy($0Proxy m) : $0Access() {
arena_ = m.arena_;
msg_ = ($1*)::hpb::interop::upb::GetMessage(&m);
}
$0& $0::operator=(const $3& from) {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
return *this;
}
$0& $0::operator=(const CProxy& from) {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::interop::upb::GetMessage(&from), &$2, arena_);
return *this;
}
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
::upb::generator::MiniTableMessageVarName(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("\n");
// Minitable
ctx.EmitLegacy(
R"cc(
const upb_MiniTable* $0::minitable() { return &$1; }
)cc",
ClassName(descriptor),
::upb::generator::MiniTableMessageVarName(descriptor->full_name()));
ctx.Emit("\n");
}
WriteAccessorsInSource(descriptor, ctx);
if (!message_is_map_entry) {
ctx.EmitLegacy(
R"cc(
struct $0DefaultTypeInternal {
$1* msg;
upb_Arena* arena;
};
static $0DefaultTypeInternal _$0DefaultTypeBuilder() {
upb_Arena* arena = upb_Arena_New();
return $0DefaultTypeInternal{$1_new(arena), arena};
}
$0DefaultTypeInternal _$0_default_instance_ = _$0DefaultTypeBuilder();
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
ctx.EmitLegacy(
R"cc(
::hpb::Ptr<const $0> $0::default_instance() {
return ::hpb::interop::upb::MakeCHandle<$0>(
(upb_Message *)_$0_default_instance_.msg,
_$0_default_instance_.arena);
}
)cc",
ClassName(descriptor));
}
}
void WriteInternalForwardDeclarationsInHeader(
const protobuf::Descriptor* message, Context& ctx) {
// Write declaration for internal re-usable default_instance without
// leaking implementation.
ctx.EmitLegacy(
R"cc(
struct $0DefaultTypeInternal;
extern $0DefaultTypeInternal _$0_default_instance_;
)cc",
ClassName(message));
}
void WriteExtensionIdentifiersInClassHeader(
const protobuf::Descriptor* message,
const std::vector<const protobuf::FieldDescriptor*>& file_exts,
Context& ctx) {
for (auto* ext : file_exts) {
if (ext->extension_scope() &&
ext->extension_scope()->full_name() == message->full_name()) {
WriteExtensionIdentifierHeader(ext, ctx);
}
}
}
void WriteUsingEnumsInHeader(
const protobuf::Descriptor* message,
const std::vector<const protobuf::EnumDescriptor*>& file_enums,
Context& ctx) {
for (auto* enum_descriptor : file_enums) {
std::string enum_type_name = EnumTypeName(enum_descriptor);
std::string enum_resolved_type_name =
enum_descriptor->file()->package().empty() &&
enum_descriptor->containing_type() == nullptr
? absl::StrCat(kNoPackageNamePrefix,
ToCIdent(enum_descriptor->name()))
: enum_type_name;
if (enum_descriptor->containing_type() == nullptr ||
enum_descriptor->containing_type()->full_name() !=
message->full_name()) {
continue;
}
ctx.EmitLegacy("using $0", enum_descriptor->name());
if (enum_descriptor->options().deprecated()) {
ctx.EmitLegacy(" ABSL_DEPRECATED(\"Proto enum $0\")",
enum_descriptor->name());
}
ctx.EmitLegacy(" = $0;", enum_resolved_type_name);
ctx.Emit("\n");
int value_count = enum_descriptor->value_count();
for (int i = 0; i < value_count; i++) {
ctx.EmitLegacy("static constexpr $0 $1", enum_descriptor->name(),
enum_descriptor->value(i)->name());
if (enum_descriptor->options().deprecated() ||
enum_descriptor->value(i)->options().deprecated()) {
ctx.EmitLegacy(" ABSL_DEPRECATED(\"Proto enum value $0\") ",
enum_descriptor->value(i)->name());
}
ctx.EmitLegacy(" = $0;\n",
EnumValueSymbolInNameSpace(enum_descriptor,
enum_descriptor->value(i)));
}
}
}
} // namespace protobuf
} // namespace google::hpb_generator
|