Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 3 | // |
Hong Shin | c482a8a | 2023-11-09 09:30:34 -0800 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 7 | |
| 8 | #include "protos_generator/gen_extensions.h" |
| 9 | |
| 10 | #include "absl/strings/str_cat.h" |
| 11 | #include "protos_generator/gen_utils.h" |
| 12 | #include "protos_generator/names.h" |
| 13 | |
| 14 | namespace protos_generator { |
| 15 | |
| 16 | namespace protobuf = ::google::protobuf; |
| 17 | |
| 18 | std::string ExtensionIdentifierBase(const protobuf::FieldDescriptor* ext) { |
| 19 | assert(ext->is_extension()); |
| 20 | std::string ext_scope; |
| 21 | if (ext->extension_scope()) { |
| 22 | return MessageName(ext->extension_scope()); |
| 23 | } else { |
| 24 | return ToCIdent(ext->file()->package()); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | std::string ContainingTypeName(const protobuf::FieldDescriptor* ext) { |
| 29 | return ext->containing_type()->file() != ext->file() |
| 30 | ? QualifiedClassName(ext->containing_type()) |
| 31 | : ClassName(ext->containing_type()); |
| 32 | } |
| 33 | |
| 34 | void WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor* ext, |
| 35 | Output& output) { |
| 36 | std::string mini_table_name = |
| 37 | absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext"); |
| 38 | if (ext->extension_scope()) { |
| 39 | output( |
| 40 | R"cc( |
| 41 | static const ::protos::internal::ExtensionIdentifier<$0, $1> $2; |
| 42 | )cc", |
| 43 | ContainingTypeName(ext), CppTypeParameterName(ext), ext->name()); |
| 44 | } else { |
| 45 | output( |
| 46 | R"cc( |
| 47 | extern const ::protos::internal::ExtensionIdentifier<$0, $1> $2; |
| 48 | )cc", |
| 49 | ContainingTypeName(ext), CppTypeParameterName(ext), ext->name()); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void WriteExtensionIdentifiersHeader( |
| 54 | const std::vector<const protobuf::FieldDescriptor*>& extensions, |
| 55 | Output& output) { |
| 56 | for (const auto* ext : extensions) { |
| 57 | if (!ext->extension_scope()) { |
| 58 | WriteExtensionIdentifierHeader(ext, output); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext, |
| 64 | Output& output) { |
| 65 | std::string mini_table_name = |
| 66 | absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext"); |
| 67 | if (ext->extension_scope()) { |
| 68 | output( |
| 69 | R"cc( |
| 70 | const ::protos::internal::ExtensionIdentifier<$0, $3> $4::$2(&$1); |
| 71 | )cc", |
| 72 | ContainingTypeName(ext), mini_table_name, ext->name(), |
| 73 | CppTypeParameterName(ext), ClassName(ext->extension_scope())); |
| 74 | } else { |
| 75 | output( |
| 76 | R"cc( |
| 77 | const ::protos::internal::ExtensionIdentifier<$0, $3> $2(&$1); |
| 78 | )cc", |
| 79 | ContainingTypeName(ext), mini_table_name, ext->name(), |
| 80 | CppTypeParameterName(ext)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void WriteExtensionIdentifiers( |
| 85 | const std::vector<const protobuf::FieldDescriptor*>& extensions, |
| 86 | Output& output) { |
| 87 | for (const auto* ext : extensions) { |
| 88 | if (!ext->extension_scope()) { |
| 89 | WriteExtensionIdentifier(ext, output); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace protos_generator |