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
35 #include <google/protobuf/compiler/cpp/cpp_field.h>
36 #include <memory>
37
38 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
39 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
40 #include <google/protobuf/compiler/cpp/cpp_string_field.h>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/compiler/cpp/cpp_enum_field.h>
45 #include <google/protobuf/compiler/cpp/cpp_map_field.h>
46 #include <google/protobuf/compiler/cpp/cpp_message_field.h>
47 #include <google/protobuf/descriptor.pb.h>
48 #include <google/protobuf/io/printer.h>
49 #include <google/protobuf/wire_format.h>
50 #include <google/protobuf/stubs/strutil.h>
51
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace cpp {
56
57 using internal::WireFormat;
58
SetCommonFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables,const Options & options)59 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
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
SetHasBitIndex(int32 has_bit_index)93 void FieldGenerator::SetHasBitIndex(int32 has_bit_index) {
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
SetCommonOneofFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables)105 void SetCommonOneofFieldVariables(
106 const FieldDescriptor* descriptor,
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
~FieldGenerator()114 FieldGenerator::~FieldGenerator() {}
115
GenerateMergeFromCodedStreamWithPacking(io::Printer * printer) const116 void FieldGenerator::GenerateMergeFromCodedStreamWithPacking(
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
FieldGeneratorMap(const Descriptor * descriptor,const Options & options,MessageSCCAnalyzer * scc_analyzer)127 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
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
MakeGoogleInternalGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)138 FieldGenerator* FieldGeneratorMap::MakeGoogleInternalGenerator(
139 const FieldDescriptor* field, const Options& options,
140 MessageSCCAnalyzer* scc_analyzer) {
141
142 return nullptr;
143 }
144
MakeGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)145 FieldGenerator* FieldGeneratorMap::MakeGenerator(
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()) {
156 case FieldDescriptor::CPPTYPE_MESSAGE:
157 if (field->is_map()) {
158 return new MapFieldGenerator(field, options);
159 } else {
160 return new RepeatedMessageFieldGenerator(field, options,
161 scc_analyzer);
162 }
163 case FieldDescriptor::CPPTYPE_STRING:
164 return new RepeatedStringFieldGenerator(field, options);
165 case FieldDescriptor::CPPTYPE_ENUM:
166 return new RepeatedEnumFieldGenerator(field, options);
167 default:
168 return new RepeatedPrimitiveFieldGenerator(field, options);
169 }
170 } else if (field->containing_oneof()) {
171 switch (field->cpp_type()) {
172 case FieldDescriptor::CPPTYPE_MESSAGE:
173 return new MessageOneofFieldGenerator(field, options, scc_analyzer);
174 case FieldDescriptor::CPPTYPE_STRING:
175 return new StringOneofFieldGenerator(field, options);
176 case FieldDescriptor::CPPTYPE_ENUM:
177 return new EnumOneofFieldGenerator(field, options);
178 default:
179 return new PrimitiveOneofFieldGenerator(field, options);
180 }
181 } else {
182 switch (field->cpp_type()) {
183 case FieldDescriptor::CPPTYPE_MESSAGE:
184 return new MessageFieldGenerator(field, options, scc_analyzer);
185 case FieldDescriptor::CPPTYPE_STRING:
186 return new StringFieldGenerator(field, options);
187 case FieldDescriptor::CPPTYPE_ENUM:
188 return new EnumFieldGenerator(field, options);
189 default:
190 return new PrimitiveFieldGenerator(field, options);
191 }
192 }
193 }
194
~FieldGeneratorMap()195 FieldGeneratorMap::~FieldGeneratorMap() {}
196
get(const FieldDescriptor * field) const197 const FieldGenerator& FieldGeneratorMap::get(
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
207