1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above copyright
9 //       notice, this list of conditions and the following disclaimer in the
10 //       documentation and/or other materials provided with the distribution.
11 //     * Neither the name of Google LLC nor the
12 //       names of its contributors may be used to endorse or promote products
13 //       derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "protos_generator/gen_extensions.h"
27 
28 #include "absl/strings/str_cat.h"
29 #include "protos_generator/gen_utils.h"
30 
31 namespace protos_generator {
32 
33 namespace protobuf = ::google::protobuf;
34 
ExtensionIdentifierBase(const protobuf::FieldDescriptor * ext)35 std::string ExtensionIdentifierBase(const protobuf::FieldDescriptor* ext) {
36   assert(ext->is_extension());
37   std::string ext_scope;
38   if (ext->extension_scope()) {
39     return MessageName(ext->extension_scope());
40   } else {
41     return ToCIdent(ext->file()->package());
42   }
43 }
44 
ContainingTypeName(const protobuf::FieldDescriptor * ext)45 std::string ContainingTypeName(const protobuf::FieldDescriptor* ext) {
46   return ext->containing_type()->file() != ext->file()
47              ? QualifiedClassName(ext->containing_type())
48              : ClassName(ext->containing_type());
49 }
50 
WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor * ext,Output & output)51 void WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor* ext,
52                                     Output& output) {
53   std::string mini_table_name =
54       absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext");
55   if (ext->extension_scope()) {
56     output(
57         R"cc(
58           static const ::protos::internal::ExtensionIdentifier<$0, $1> $2;
59         )cc",
60         ContainingTypeName(ext), CppTypeParameterName(ext), ext->name());
61   } else {
62     output(
63         R"cc(
64           extern const ::protos::internal::ExtensionIdentifier<$0, $1> $2;
65         )cc",
66         ContainingTypeName(ext), CppTypeParameterName(ext), ext->name());
67   }
68 }
69 
WriteExtensionIdentifiersHeader(const std::vector<const protobuf::FieldDescriptor * > & extensions,Output & output)70 void WriteExtensionIdentifiersHeader(
71     const std::vector<const protobuf::FieldDescriptor*>& extensions,
72     Output& output) {
73   for (const auto* ext : extensions) {
74     if (!ext->extension_scope()) {
75       WriteExtensionIdentifierHeader(ext, output);
76     }
77   }
78 }
79 
WriteExtensionIdentifier(const protobuf::FieldDescriptor * ext,Output & output)80 void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext,
81                               Output& output) {
82   std::string mini_table_name =
83       absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext");
84   if (ext->extension_scope()) {
85     output(
86         R"cc(
87           const ::protos::internal::ExtensionIdentifier<$0, $3> $4::$2(&$1);
88         )cc",
89         ContainingTypeName(ext), mini_table_name, ext->name(),
90         CppTypeParameterName(ext), ClassName(ext->extension_scope()));
91   } else {
92     output(
93         R"cc(
94           const ::protos::internal::ExtensionIdentifier<$0, $3> $2(&$1);
95         )cc",
96         ContainingTypeName(ext), mini_table_name, ext->name(),
97         CppTypeParameterName(ext));
98   }
99 }
100 
WriteExtensionIdentifiers(const std::vector<const protobuf::FieldDescriptor * > & extensions,Output & output)101 void WriteExtensionIdentifiers(
102     const std::vector<const protobuf::FieldDescriptor*>& extensions,
103     Output& output) {
104   for (const auto* ext : extensions) {
105     if (!ext->extension_scope()) {
106       WriteExtensionIdentifier(ext, output);
107     }
108   }
109 }
110 
111 }  // namespace protos_generator
112