cpp_field.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 <memory>
37 
41 
51 
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace cpp {
56 
57 using internal::WireFormat;
58 
60  std::map<std::string, std::string>* variables,
61  const Options& options) {
62  SetCommonVars(options, variables);
63  (*variables)["ns"] = Namespace(descriptor, options);
64  (*variables)["name"] = FieldName(descriptor);
65  (*variables)["index"] = StrCat(descriptor->index());
66  (*variables)["number"] = StrCat(descriptor->number());
67  (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
68  (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
69  (*variables)["field_member"] = FieldName(descriptor) + "_";
70 
71  (*variables)["tag_size"] = StrCat(
72  WireFormat::TagSize(descriptor->number(), descriptor->type()));
73  (*variables)["deprecated_attr"] =
74  DeprecatedAttribute(options, descriptor->options().deprecated());
75 
76  (*variables)["set_hasbit"] = "";
77  (*variables)["clear_hasbit"] = "";
78  if (HasFieldPresence(descriptor->file())) {
79  (*variables)["set_hasbit_io"] =
80  "_Internal::set_has_" + FieldName(descriptor) + "(&_has_bits_);";
81  } else {
82  (*variables)["set_hasbit_io"] = "";
83  }
84 
85  // These variables are placeholders to pick out the beginning and ends of
86  // identifiers for annotations (when doing so with existing variables would
87  // be ambiguous or impossible). They should never be set to anything but the
88  // empty string.
89  (*variables)["{"] = "";
90  (*variables)["}"] = "";
91 }
92 
94  if (!HasFieldPresence(descriptor_->file()) || has_bit_index == -1) {
95  return;
96  }
97  variables_["set_hasbit"] = StrCat(
98  "_has_bits_[", has_bit_index / 32, "] |= 0x",
99  strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
100  variables_["clear_hasbit"] = StrCat(
101  "_has_bits_[", has_bit_index / 32, "] &= ~0x",
102  strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
103 }
104 
107  std::map<std::string, std::string>* variables) {
108  const std::string prefix = descriptor->containing_oneof()->name() + "_.";
109  (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
110  (*variables)["field_member"] =
111  StrCat(prefix, (*variables)["name"], "_");
112 }
113 
115 
117  io::Printer* printer) const {
118  // Reaching here indicates a bug. Cases are:
119  // - This FieldGenerator should support packing, but this method should be
120  // overridden.
121  // - This FieldGenerator doesn't support packing, and this method should
122  // never have been called.
123  GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() "
124  << "called on field generator that does not support packing.";
125 }
126 
128  const Options& options,
129  MessageSCCAnalyzer* scc_analyzer)
130  : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
131  // Construct all the FieldGenerators.
132  for (int i = 0; i < descriptor->field_count(); i++) {
133  field_generators_[i].reset(
134  MakeGenerator(descriptor->field(i), options, scc_analyzer));
135  }
136 }
137 
139  const FieldDescriptor* field, const Options& options,
140  MessageSCCAnalyzer* scc_analyzer) {
141 
142  return nullptr;
143 }
144 
146  const FieldDescriptor* field, const Options& options,
147  MessageSCCAnalyzer* scc_analyzer) {
148  FieldGenerator* generator =
149  MakeGoogleInternalGenerator(field, options, scc_analyzer);
150  if (generator) {
151  return generator;
152  }
153 
154  if (field->is_repeated()) {
155  switch (field->cpp_type()) {
157  if (field->is_map()) {
158  return new MapFieldGenerator(field, options);
159  } else {
161  scc_analyzer);
162  }
167  default:
169  }
170  } else if (field->containing_oneof()) {
171  switch (field->cpp_type()) {
173  return new MessageOneofFieldGenerator(field, options, scc_analyzer);
178  default:
180  }
181  } else {
182  switch (field->cpp_type()) {
184  return new MessageFieldGenerator(field, options, scc_analyzer);
186  return new StringFieldGenerator(field, options);
188  return new EnumFieldGenerator(field, options);
189  default:
191  }
192  }
193 }
194 
196 
198  const FieldDescriptor* field) const {
199  GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
200  return *field_generators_[field->index()];
201 }
202 
203 } // namespace cpp
204 } // namespace compiler
205 } // namespace protobuf
206 } // namespace google
GOOGLE_CHECK_EQ
#define GOOGLE_CHECK_EQ(A, B)
Definition: logging.h:156
google::protobuf::FieldDescriptor::CPPTYPE_ENUM
@ CPPTYPE_ENUM
Definition: src/google/protobuf/descriptor.h:561
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.internal::WireFormat::TagSize
static size_t TagSize(int field_number, FieldDescriptor::Type type)
Definition: wire_format.h:293
cpp_message_field.h
google::protobuf::compiler::cpp::FieldName
std::string FieldName(const FieldDescriptor *field)
Definition: cpp_helpers.cc:410
google::protobuf::compiler::cpp::FieldGenerator::SetHasBitIndex
void SetHasBitIndex(int32 has_bit_index)
Definition: cpp_field.cc:93
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGenerator
static FieldGenerator * MakeGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:145
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
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::compiler::cpp::SetCommonFieldVariables
void SetCommonFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables, const Options &options)
Definition: cpp_field.cc:59
google::protobuf::compiler::cpp::FieldGeneratorMap::descriptor_
const Descriptor * descriptor_
Definition: cpp_field.h:229
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::compiler::cpp::RepeatedEnumFieldGenerator
Definition: cpp_enum_field.h:86
cpp_string_field.h
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::cpp::DeclaredTypeMethodName
const char * DeclaredTypeMethodName(FieldDescriptor::Type type)
Definition: cpp_helpers.cc:545
google::protobuf::compiler::cpp::StringOneofFieldGenerator
Definition: cpp_string_field.h:85
google::protobuf::compiler::cpp::FieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: cpp_field.h:205
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGoogleInternalGenerator
static FieldGenerator * MakeGoogleInternalGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:138
cpp_helpers.h
google::protobuf::compiler::cpp::FieldGenerator::variables_
std::map< std::string, std::string > variables_
Definition: cpp_field.h:207
google::protobuf::compiler::cpp::FieldGenerator::GenerateMergeFromCodedStreamWithPacking
virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer *printer) const
Definition: cpp_field.cc:116
google::protobuf::compiler::cpp::FieldGeneratorMap::~FieldGeneratorMap
~FieldGeneratorMap()
Definition: cpp_field.cc:195
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::compiler::cpp::StringFieldGenerator
Definition: cpp_string_field.h:47
google::protobuf::compiler::cpp::MapFieldGenerator
Definition: cpp_map_field.h:44
google::protobuf::compiler::cpp::FieldGenerator
Definition: cpp_field.h:71
strutil.h
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::cpp::EnumOneofFieldGenerator
Definition: cpp_enum_field.h:70
prefix
static const char prefix[]
Definition: test_pair_ipc.cpp:26
google::protobuf::compiler::cpp::FieldGeneratorMap::get
const FieldGenerator & get(const FieldDescriptor *field) const
Definition: cpp_field.cc:197
google::protobuf::compiler::cpp::EnumFieldGenerator
Definition: cpp_enum_field.h:47
google::protobuf::compiler::cpp::FieldScope
const Descriptor * FieldScope(const FieldDescriptor *field)
Definition: cpp_helpers.h:182
google::protobuf::compiler::cpp::FieldGeneratorMap::FieldGeneratorMap
FieldGeneratorMap(const Descriptor *descriptor, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:127
printer.h
google::protobuf::compiler::cpp::MessageOneofFieldGenerator
Definition: cpp_message_field.h:81
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::compiler::cpp::DeprecatedAttribute
std::string DeprecatedAttribute(const Options &options, bool deprecated)
Definition: cpp_helpers.h:68
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
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::compiler::cpp::PrimitiveFieldGenerator
Definition: cpp_primitive_field.h:47
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::cpp::FieldGeneratorMap::field_generators_
std::vector< std::unique_ptr< FieldGenerator > > field_generators_
Definition: cpp_field.h:230
google::protobuf::compiler::cpp::RepeatedStringFieldGenerator
Definition: cpp_string_field.h:107
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::compiler::cpp::Namespace
std::string Namespace(const std::string &package)
Definition: cpp_helpers.cc:336
google::protobuf::compiler::cpp::RepeatedMessageFieldGenerator
Definition: cpp_message_field.h:104
cpp_primitive_field.h
common.h
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: cpp_helpers.h:524
google::protobuf::compiler::cpp::FieldGenerator::~FieldGenerator
virtual ~FieldGenerator()
Definition: cpp_field.cc:114
cpp_enum_field.h
wire_format.h
logging.h
google::protobuf::compiler::cpp::ClassName
std::string ClassName(const Descriptor *descriptor)
Definition: cpp_helpers.cc:301
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
cpp_map_field.h
google::protobuf::compiler::cpp::HasFieldPresence
bool HasFieldPresence(const FileDescriptor *file)
Definition: cpp_helpers.h:413
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
descriptor.pb.h
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE
@ CPPTYPE_MESSAGE
Definition: src/google/protobuf/descriptor.h:563
cpp_field.h
google::protobuf::compiler::cpp::PrimitiveOneofFieldGenerator
Definition: cpp_primitive_field.h:71
google::protobuf::compiler::cpp::MessageFieldGenerator
Definition: cpp_message_field.h:48
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::strings::ZERO_PAD_8
@ ZERO_PAD_8
Definition: strutil.h:583
google::protobuf::compiler::cpp::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables)
Definition: cpp_field.cc:105
google::protobuf::compiler::cpp::RepeatedPrimitiveFieldGenerator
Definition: cpp_primitive_field.h:88


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