cpp_extension.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
36 #include <map>
41 
42 
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace cpp {
48 
49 namespace {
50 
51 // Returns the fully-qualified class name of the message that this field
52 // extends. This function is used in the Google-internal code to handle some
53 // legacy cases.
54 std::string ExtendeeClassName(const FieldDescriptor* descriptor) {
55  const Descriptor* extendee = descriptor->containing_type();
56  return ClassName(extendee, true);
57 }
58 
59 } // anonymous namespace
60 
62  const Options& options)
64  // Construct type_traits_.
65  if (descriptor_->is_repeated()) {
66  type_traits_ = "Repeated";
67  }
68 
69  switch (descriptor_->cpp_type()) {
71  type_traits_.append("EnumTypeTraits< ");
72  type_traits_.append(ClassName(descriptor_->enum_type(), true));
73  type_traits_.append(", ");
74  type_traits_.append(ClassName(descriptor_->enum_type(), true));
75  type_traits_.append("_IsValid>");
76  break;
78  type_traits_.append("StringTypeTraits");
79  break;
81  type_traits_.append("MessageTypeTraits< ");
83  type_traits_.append(" >");
84  break;
85  default:
86  type_traits_.append("PrimitiveTypeTraits< ");
88  type_traits_.append(" >");
89  break;
90  }
92  variables_["extendee"] = ExtendeeClassName(descriptor_);
93  variables_["type_traits"] = type_traits_;
95  variables_["name"] = ResolveKeyword(name);
96  variables_["constant_name"] = FieldConstantName(descriptor_);
97  variables_["field_type"] =
98  StrCat(static_cast<int>(descriptor_->type()));
99  variables_["packed"] = descriptor_->options().packed() ? "true" : "false";
100 
101  std::string scope =
102  IsScoped() ? ClassName(descriptor_->extension_scope(), false) + "::" : "";
103  variables_["scope"] = scope;
104  std::string scoped_name = scope + ResolveKeyword(name);
105  variables_["scoped_name"] = scoped_name;
106  variables_["number"] = StrCat(descriptor_->number());
107 }
108 
110 
112  return descriptor_->extension_scope() != nullptr;
113 }
114 
116  Formatter format(printer, variables_);
117 
118  // If this is a class member, it needs to be declared "static". Otherwise,
119  // it needs to be "extern". In the latter case, it also needs the DLL
120  // export/import specifier.
121  std::string qualifier;
122  if (!IsScoped()) {
123  qualifier = "extern";
124  if (!options_.dllexport_decl.empty()) {
125  qualifier = options_.dllexport_decl + " " + qualifier;
126  }
127  } else {
128  qualifier = "static";
129  }
130 
131  format(
132  "static const int $constant_name$ = $number$;\n"
133  "$1$ ::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
134  " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
135  " ${2$$name$$}$;\n",
136  qualifier, descriptor_);
137 }
138 
140  // If we are building for lite with implicit weak fields, we want to skip over
141  // any custom options (i.e. extensions of messages from descriptor.proto).
142  // This prevents the creation of any unnecessary linker references to the
143  // descriptor messages.
146  "net/proto2/proto/descriptor.proto") {
147  return;
148  }
149 
150  Formatter format(printer, variables_);
151  std::string default_str;
152  // If this is a class member, it needs to be declared in its class scope.
154  // We need to declare a global string which will contain the default value.
155  // We cannot declare it at class scope because that would require exposing
156  // it in the header which would be annoying for other reasons. So we
157  // replace :: with _ in the name and declare it as a global.
158  default_str =
159  StringReplace(variables_["scoped_name"], "::", "_", true) + "_default";
160  format("const std::string $1$($2$);\n", default_str,
162  } else {
163  default_str = DefaultValue(options_, descriptor_);
164  }
165 
166  // Likewise, class members need to declare the field constant variable.
167  if (IsScoped()) {
168  format(
169  "#if !defined(_MSC_VER) || _MSC_VER >= 1900\n"
170  "const int $scope$$constant_name$;\n"
171  "#endif\n");
172  }
173 
174  format(
175  "::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
176  " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
177  " $scoped_name$($constant_name$, $1$);\n",
178  default_str);
179 }
180 
181 } // namespace cpp
182 } // namespace compiler
183 } // namespace protobuf
184 } // namespace google
google::protobuf::compiler::cpp::ExtensionGenerator::~ExtensionGenerator
~ExtensionGenerator()
Definition: cpp_extension.cc:109
google::protobuf::compiler::cpp::ExtensionGenerator::type_traits_
std::string type_traits_
Definition: cpp_extension.h:78
google::protobuf::FieldDescriptor::CPPTYPE_ENUM
@ CPPTYPE_ENUM
Definition: src/google/protobuf/descriptor.h:561
google::protobuf::FieldDescriptor::options
const FieldOptions & options() const
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::FieldDescriptor::enum_type
const EnumDescriptor * enum_type() const
Definition: src/google/protobuf/descriptor.cc:7235
google::protobuf::FieldDescriptor::CPPTYPE_STRING
@ CPPTYPE_STRING
Definition: src/google/protobuf/descriptor.h:562
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::cpp::FieldConstantName
std::string FieldConstantName(const FieldDescriptor *field)
Definition: cpp_helpers.cc:451
google::protobuf::FieldDescriptor::containing_type
const Descriptor * containing_type() const
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
Descriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:113
cpp_helpers.h
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
strutil.h
google::protobuf::compiler::cpp::Options::lite_implicit_weak_fields
bool lite_implicit_weak_fields
Definition: cpp_options.h:61
google::protobuf::FieldDescriptor::extension_scope
const Descriptor * extension_scope() const
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
google::protobuf::compiler::cpp::ResolveKeyword
std::string ResolveKeyword(const string &name)
Definition: cpp_helpers.cc:403
printer.h
google::protobuf::FieldDescriptor::number
int number() const
google::protobuf::compiler::cpp::ExtensionGenerator::ExtensionGenerator
ExtensionGenerator(const FieldDescriptor *descriptor, const Options &options)
Definition: cpp_extension.cc:61
google::protobuf::compiler::cpp::DefaultValue
std::string DefaultValue(const FieldDescriptor *field)
Definition: cpp_helpers.cc:616
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::SetCommonVars
void SetCommonVars(const Options &options, std::map< std::string, std::string > *variables)
Definition: cpp_helpers.cc:209
google::protobuf::compiler::cpp::Options
Definition: cpp_options.h:52
google::protobuf::compiler::cpp::ExtensionGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: cpp_extension.h:77
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::FileDescriptor::name
const std::string & name() const
FieldOptions::packed
bool packed() const
Definition: descriptor.pb.h:10585
google::protobuf::FieldDescriptor::type
Type type() const
Definition: src/google/protobuf/descriptor.h:2052
google::protobuf::StringReplace
void StringReplace(const string &s, const string &oldsub, const string &newsub, bool replace_all, string *res)
Definition: strutil.cc:148
google::protobuf::FieldDescriptor::name
const std::string & name() const
google::protobuf::compiler::cpp::ExtensionGenerator::GenerateDefinition
void GenerateDefinition(io::Printer *printer)
Definition: cpp_extension.cc:139
google::protobuf::compiler::cpp::ExtensionGenerator::options_
Options options_
Definition: cpp_extension.h:79
google::protobuf::Descriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::cpp::ClassName
std::string ClassName(const Descriptor *descriptor)
Definition: cpp_helpers.cc:301
google::protobuf::compiler::cpp::PrimitiveTypeName
const char * PrimitiveTypeName(FieldDescriptor::CppType type)
Definition: cpp_helpers.cc:482
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
cpp_extension.h
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
google::protobuf::compiler::cpp::Options::dllexport_decl
std::string dllexport_decl
Definition: cpp_options.h:53
descriptor.pb.h
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE
@ CPPTYPE_MESSAGE
Definition: src/google/protobuf/descriptor.h:563
google::protobuf::compiler::cpp::ExtensionGenerator::GenerateDeclaration
void GenerateDeclaration(io::Printer *printer) const
Definition: cpp_extension.cc:115
google::protobuf::compiler::cpp::ExtensionGenerator::variables_
std::map< std::string, std::string > variables_
Definition: cpp_extension.h:81
google::protobuf::FieldDescriptor::is_repeated
bool is_repeated() const
Definition: src/google/protobuf/descriptor.h:2067
google::protobuf::FieldDescriptor::cpp_type
CppType cpp_type() const
Definition: src/google/protobuf/descriptor.h:2139
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::Formatter
Definition: cpp_helpers.h:637
options_
DebugStringOptions options_
Definition: src/google/protobuf/descriptor.cc:2410
google::protobuf::compiler::cpp::ExtensionGenerator::IsScoped
bool IsScoped() const
Definition: cpp_extension.cc:111


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48