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: [email protected] (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #include <google/protobuf/compiler/cpp/field.h>
36
37 #include <cstdint>
38 #include <memory>
39 #include <string>
40
41 #include <google/protobuf/stubs/strutil.h>
42 #include <google/protobuf/stubs/substitute.h>
43 #include <google/protobuf/compiler/cpp/helpers.h>
44 #include <google/protobuf/compiler/cpp/primitive_field.h>
45 #include <google/protobuf/compiler/cpp/string_field.h>
46 #include <google/protobuf/stubs/logging.h>
47 #include <google/protobuf/stubs/common.h>
48 #include <google/protobuf/io/printer.h>
49 #include <google/protobuf/wire_format.h>
50 #include <google/protobuf/compiler/cpp/enum_field.h>
51 #include <google/protobuf/compiler/cpp/map_field.h>
52 #include <google/protobuf/compiler/cpp/message_field.h>
53 #include <google/protobuf/descriptor.pb.h>
54
55 namespace google {
56 namespace protobuf {
57 namespace compiler {
58 namespace cpp {
59
60 using internal::WireFormat;
61
62 namespace {
63
MaySetAnnotationVariable(const Options & options,StringPiece annotation_name,StringPiece substitute_template_prefix,StringPiece prepared_template,int field_index,StringPiece access_type,std::map<std::string,std::string> * variables)64 void MaySetAnnotationVariable(const Options& options,
65 StringPiece annotation_name,
66 StringPiece substitute_template_prefix,
67 StringPiece prepared_template,
68 int field_index, StringPiece access_type,
69 std::map<std::string, std::string>* variables) {
70 if (options.field_listener_options.forbidden_field_listener_events.count(
71 std::string(annotation_name)))
72 return;
73 (*variables)[StrCat("annotate_", annotation_name)] = strings::Substitute(
74 StrCat(substitute_template_prefix, prepared_template, ");\n"),
75 field_index, access_type);
76 }
77
GenerateTemplateForOneofString(const FieldDescriptor * descriptor,StringPiece proto_ns,StringPiece field_member)78 std::string GenerateTemplateForOneofString(const FieldDescriptor* descriptor,
79 StringPiece proto_ns,
80 StringPiece field_member) {
81 std::string field_name = google::protobuf::compiler::cpp::FieldName(descriptor);
82 std::string field_pointer =
83 descriptor->options().ctype() == google::protobuf::FieldOptions::STRING
84 ? "$0.UnsafeGetPointer()"
85 : "$0";
86
87 if (descriptor->default_value_string().empty()) {
88 return strings::Substitute(StrCat("_internal_has_", field_name, "() ? ",
89 field_pointer, ": nullptr"),
90 field_member);
91 }
92
93 if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING_PIECE) {
94 return strings::Substitute(StrCat("_internal_has_", field_name, "() ? ",
95 field_pointer, ": nullptr"),
96 field_member);
97 }
98
99 std::string default_value_pointer =
100 descriptor->options().ctype() == google::protobuf::FieldOptions::STRING
101 ? "&$1.get()"
102 : "&$1";
103 return strings::Substitute(
104 StrCat("_internal_has_", field_name, "() ? ", field_pointer, " : ",
105 default_value_pointer),
106 field_member, MakeDefaultFieldName(descriptor));
107 }
108
GenerateTemplateForSingleString(const FieldDescriptor * descriptor,StringPiece field_member)109 std::string GenerateTemplateForSingleString(const FieldDescriptor* descriptor,
110 StringPiece field_member) {
111 if (descriptor->default_value_string().empty()) {
112 return StrCat("&", field_member);
113 }
114
115 if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING) {
116 return strings::Substitute(
117 "$0.IsDefault() ? &$1.get() : $0.UnsafeGetPointer()", field_member,
118 MakeDefaultFieldName(descriptor));
119 }
120
121 return StrCat("&", field_member);
122 }
123
124 } // namespace
125
AddAccessorAnnotations(const FieldDescriptor * descriptor,const Options & options,std::map<std::string,std::string> * variables)126 void AddAccessorAnnotations(const FieldDescriptor* descriptor,
127 const Options& options,
128 std::map<std::string, std::string>* variables) {
129 // Can be expanded to include more specific calls, for example, for arena or
130 // clear calls.
131 static constexpr const char* kAccessorsAnnotations[] = {
132 "annotate_add", "annotate_get", "annotate_has",
133 "annotate_list", "annotate_mutable", "annotate_mutable_list",
134 "annotate_release", "annotate_set", "annotate_size",
135 "annotate_clear", "annotate_add_mutable",
136 };
137 for (size_t i = 0; i < GOOGLE_ARRAYSIZE(kAccessorsAnnotations); ++i) {
138 (*variables)[kAccessorsAnnotations[i]] = "";
139 }
140 if (options.annotate_accessor) {
141 for (size_t i = 0; i < GOOGLE_ARRAYSIZE(kAccessorsAnnotations); ++i) {
142 (*variables)[kAccessorsAnnotations[i]] = StrCat(
143 " ", FieldName(descriptor), "_AccessedNoStrip = true;\n");
144 }
145 }
146 if (!options.field_listener_options.inject_field_listener_events) {
147 return;
148 }
149 if (descriptor->file()->options().optimize_for() ==
150 google::protobuf::FileOptions::LITE_RUNTIME) {
151 return;
152 }
153 std::string field_member = (*variables)["field"];
154 const google::protobuf::OneofDescriptor* oneof_member =
155 descriptor->real_containing_oneof();
156 const std::string proto_ns = (*variables)["proto_ns"];
157 const std::string substitute_template_prefix =
158 StrCat(" ", (*variables)["tracker"], ".$1<$0>(this, ");
159 std::string prepared_template;
160
161 // Flat template is needed if the prepared one is introspecting the values
162 // inside the returned values, for example, for repeated fields and maps.
163 std::string prepared_flat_template;
164 std::string prepared_add_template;
165 // TODO(b/190614678): Support fields with type Message or Map.
166 if (descriptor->is_repeated() && !descriptor->is_map()) {
167 if (descriptor->type() != FieldDescriptor::TYPE_MESSAGE &&
168 descriptor->type() != FieldDescriptor::TYPE_GROUP) {
169 prepared_template = strings::Substitute("&$0.Get(index)", field_member);
170 prepared_add_template =
171 strings::Substitute("&$0.Get($0.size() - 1)", field_member);
172 } else {
173 prepared_template = "nullptr";
174 prepared_add_template = "nullptr";
175 }
176 } else if (descriptor->is_map()) {
177 prepared_template = "nullptr";
178 } else if (descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
179 !IsExplicitLazy(descriptor)) {
180 prepared_template = "nullptr";
181 } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
182 if (oneof_member) {
183 prepared_template = GenerateTemplateForOneofString(
184 descriptor, (*variables)["proto_ns"], field_member);
185 } else {
186 prepared_template =
187 GenerateTemplateForSingleString(descriptor, field_member);
188 }
189 } else {
190 prepared_template = StrCat("&", field_member);
191 }
192 if (descriptor->is_repeated() && !descriptor->is_map() &&
193 descriptor->type() != FieldDescriptor::TYPE_MESSAGE &&
194 descriptor->type() != FieldDescriptor::TYPE_GROUP) {
195 prepared_flat_template = StrCat("&", field_member);
196 } else {
197 prepared_flat_template = prepared_template;
198 }
199
200 MaySetAnnotationVariable(options, "get", substitute_template_prefix,
201 prepared_template, descriptor->index(), "OnGet",
202 variables);
203 MaySetAnnotationVariable(options, "set", substitute_template_prefix,
204 prepared_template, descriptor->index(), "OnSet",
205 variables);
206 MaySetAnnotationVariable(options, "has", substitute_template_prefix,
207 prepared_template, descriptor->index(), "OnHas",
208 variables);
209 MaySetAnnotationVariable(options, "mutable", substitute_template_prefix,
210 prepared_template, descriptor->index(), "OnMutable",
211 variables);
212 MaySetAnnotationVariable(options, "release", substitute_template_prefix,
213 prepared_template, descriptor->index(), "OnRelease",
214 variables);
215 MaySetAnnotationVariable(options, "clear", substitute_template_prefix,
216 prepared_flat_template, descriptor->index(),
217 "OnClear", variables);
218 MaySetAnnotationVariable(options, "size", substitute_template_prefix,
219 prepared_flat_template, descriptor->index(),
220 "OnSize", variables);
221 MaySetAnnotationVariable(options, "list", substitute_template_prefix,
222 prepared_flat_template, descriptor->index(),
223 "OnList", variables);
224 MaySetAnnotationVariable(options, "mutable_list", substitute_template_prefix,
225 prepared_flat_template, descriptor->index(),
226 "OnMutableList", variables);
227 MaySetAnnotationVariable(options, "add", substitute_template_prefix,
228 prepared_add_template, descriptor->index(), "OnAdd",
229 variables);
230 MaySetAnnotationVariable(options, "add_mutable", substitute_template_prefix,
231 prepared_add_template, descriptor->index(),
232 "OnAddMutable", variables);
233 }
234
SetCommonFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables,const Options & options)235 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
236 std::map<std::string, std::string>* variables,
237 const Options& options) {
238 SetCommonVars(options, variables);
239 SetCommonMessageDataVariables(descriptor->containing_type(), variables);
240
241 (*variables)["ns"] = Namespace(descriptor, options);
242 (*variables)["name"] = FieldName(descriptor);
243 (*variables)["index"] = StrCat(descriptor->index());
244 (*variables)["number"] = StrCat(descriptor->number());
245 (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
246 (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
247 bool split = ShouldSplit(descriptor, options);
248 (*variables)["field"] = FieldMemberName(descriptor, split);
249
250 (*variables)["tag_size"] = StrCat(
251 WireFormat::TagSize(descriptor->number(), descriptor->type()));
252 (*variables)["deprecated_attr"] = DeprecatedAttribute(options, descriptor);
253
254 (*variables)["set_hasbit"] = "";
255 (*variables)["clear_hasbit"] = "";
256 (*variables)["maybe_prepare_split_message"] =
257 split ? " PrepareSplitMessageForWrite();\n" : "";
258
259 AddAccessorAnnotations(descriptor, options, variables);
260
261 // These variables are placeholders to pick out the beginning and ends of
262 // identifiers for annotations (when doing so with existing variables would
263 // be ambiguous or impossible). They should never be set to anything but the
264 // empty string.
265 (*variables)["{"] = "";
266 (*variables)["}"] = "";
267 }
268
SetHasBitIndex(int32_t has_bit_index)269 void FieldGenerator::SetHasBitIndex(int32_t has_bit_index) {
270 if (!HasHasbit(descriptor_)) {
271 GOOGLE_CHECK_EQ(has_bit_index, -1);
272 return;
273 }
274 variables_["set_hasbit"] = StrCat(
275 variables_["has_bits"], "[", has_bit_index / 32, "] |= 0x",
276 strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
277 variables_["clear_hasbit"] = StrCat(
278 variables_["has_bits"], "[", has_bit_index / 32, "] &= ~0x",
279 strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
280 }
281
SetInlinedStringIndex(int32_t inlined_string_index)282 void FieldGenerator::SetInlinedStringIndex(int32_t inlined_string_index) {
283 if (!IsStringInlined(descriptor_, options_)) {
284 GOOGLE_CHECK_EQ(inlined_string_index, -1);
285 return;
286 }
287 // The first bit is the tracking bit for on demand registering ArenaDtor.
288 GOOGLE_CHECK_GT(inlined_string_index, 0)
289 << "_inlined_string_donated_'s bit 0 is reserved for arena dtor tracking";
290 variables_["inlined_string_donated"] = StrCat(
291 "(", variables_["inlined_string_donated_array"], "[",
292 inlined_string_index / 32, "] & 0x",
293 strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8),
294 "u) != 0;");
295 variables_["donating_states_word"] =
296 StrCat(variables_["inlined_string_donated_array"], "[",
297 inlined_string_index / 32, "]");
298 variables_["mask_for_undonate"] = StrCat(
299 "~0x", strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8),
300 "u");
301 }
302
GenerateAggregateInitializer(io::Printer * printer) const303 void FieldGenerator::GenerateAggregateInitializer(io::Printer* printer) const {
304 Formatter format(printer, variables_);
305 if (ShouldSplit(descriptor_, options_)) {
306 format("decltype(Impl_::Split::$name$_){arena}");
307 return;
308 }
309 format("decltype($field$){arena}");
310 }
311
GenerateConstexprAggregateInitializer(io::Printer * printer) const312 void FieldGenerator::GenerateConstexprAggregateInitializer(
313 io::Printer* printer) const {
314 Formatter format(printer, variables_);
315 format("/*decltype($field$)*/{}");
316 }
317
GenerateCopyAggregateInitializer(io::Printer * printer) const318 void FieldGenerator::GenerateCopyAggregateInitializer(
319 io::Printer* printer) const {
320 Formatter format(printer, variables_);
321 format("decltype($field$){from.$field$}");
322 }
323
GenerateCopyConstructorCode(io::Printer * printer) const324 void FieldGenerator::GenerateCopyConstructorCode(io::Printer* printer) const {
325 if (ShouldSplit(descriptor_, options_)) {
326 // There is no copy constructor for the `Split` struct, so we need to copy
327 // the value here.
328 Formatter format(printer, variables_);
329 format("$field$ = from.$field$;\n");
330 }
331 }
332
SetCommonOneofFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables)333 void SetCommonOneofFieldVariables(
334 const FieldDescriptor* descriptor,
335 std::map<std::string, std::string>* variables) {
336 const std::string prefix = descriptor->containing_oneof()->name() + "_.";
337 (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
338 }
339
~FieldGenerator()340 FieldGenerator::~FieldGenerator() {}
341
FieldGeneratorMap(const Descriptor * descriptor,const Options & options,MessageSCCAnalyzer * scc_analyzer)342 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
343 const Options& options,
344 MessageSCCAnalyzer* scc_analyzer)
345 : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
346 // Construct all the FieldGenerators.
347 for (int i = 0; i < descriptor->field_count(); i++) {
348 field_generators_[i].reset(
349 MakeGenerator(descriptor->field(i), options, scc_analyzer));
350 }
351 }
352
MakeGoogleInternalGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)353 FieldGenerator* FieldGeneratorMap::MakeGoogleInternalGenerator(
354 const FieldDescriptor* field, const Options& options,
355 MessageSCCAnalyzer* scc_analyzer) {
356
357 return nullptr;
358 }
359
MakeGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)360 FieldGenerator* FieldGeneratorMap::MakeGenerator(
361 const FieldDescriptor* field, const Options& options,
362 MessageSCCAnalyzer* scc_analyzer) {
363 FieldGenerator* generator =
364 MakeGoogleInternalGenerator(field, options, scc_analyzer);
365 if (generator) {
366 return generator;
367 }
368
369 if (field->is_repeated()) {
370 switch (field->cpp_type()) {
371 case FieldDescriptor::CPPTYPE_MESSAGE:
372 if (field->is_map()) {
373 return new MapFieldGenerator(field, options, scc_analyzer);
374 } else {
375 return new RepeatedMessageFieldGenerator(field, options,
376 scc_analyzer);
377 }
378 case FieldDescriptor::CPPTYPE_STRING:
379 return new RepeatedStringFieldGenerator(field, options);
380 case FieldDescriptor::CPPTYPE_ENUM:
381 return new RepeatedEnumFieldGenerator(field, options);
382 default:
383 return new RepeatedPrimitiveFieldGenerator(field, options);
384 }
385 } else if (field->real_containing_oneof()) {
386 switch (field->cpp_type()) {
387 case FieldDescriptor::CPPTYPE_MESSAGE:
388 return new MessageOneofFieldGenerator(field, options, scc_analyzer);
389 case FieldDescriptor::CPPTYPE_STRING:
390 return new StringOneofFieldGenerator(field, options);
391 case FieldDescriptor::CPPTYPE_ENUM:
392 return new EnumOneofFieldGenerator(field, options);
393 default:
394 return new PrimitiveOneofFieldGenerator(field, options);
395 }
396 } else {
397 switch (field->cpp_type()) {
398 case FieldDescriptor::CPPTYPE_MESSAGE:
399 return new MessageFieldGenerator(field, options, scc_analyzer);
400 case FieldDescriptor::CPPTYPE_STRING:
401 return new StringFieldGenerator(field, options);
402 case FieldDescriptor::CPPTYPE_ENUM:
403 return new EnumFieldGenerator(field, options);
404 default:
405 return new PrimitiveFieldGenerator(field, options);
406 }
407 }
408 }
409
~FieldGeneratorMap()410 FieldGeneratorMap::~FieldGeneratorMap() {}
411
get(const FieldDescriptor * field) const412 const FieldGenerator& FieldGeneratorMap::get(
413 const FieldDescriptor* field) const {
414 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
415 return *field_generators_[field->index()];
416 }
417
418 } // namespace cpp
419 } // namespace compiler
420 } // namespace protobuf
421 } // namespace google
422