• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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/cpp_helpers.h>
44 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
45 #include <google/protobuf/compiler/cpp/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/cpp_enum_field.h>
51 #include <google/protobuf/compiler/cpp/cpp_map_field.h>
52 #include <google/protobuf/compiler/cpp/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(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   (*variables)["field"] = FieldMemberName(descriptor);
248 
249   (*variables)["tag_size"] = StrCat(
250       WireFormat::TagSize(descriptor->number(), descriptor->type()));
251   (*variables)["deprecated_attr"] = DeprecatedAttribute(options, descriptor);
252 
253   (*variables)["set_hasbit"] = "";
254   (*variables)["clear_hasbit"] = "";
255   if (HasHasbit(descriptor)) {
256     (*variables)["set_hasbit_io"] =
257         StrCat("_Internal::set_has_", FieldName(descriptor), "(&",
258                      (*variables)["has_bits"], ");");
259   } else {
260     (*variables)["set_hasbit_io"] = "";
261   }
262 
263   AddAccessorAnnotations(descriptor, options, variables);
264 
265   // These variables are placeholders to pick out the beginning and ends of
266   // identifiers for annotations (when doing so with existing variables would
267   // be ambiguous or impossible). They should never be set to anything but the
268   // empty string.
269   (*variables)["{"] = "";
270   (*variables)["}"] = "";
271 }
272 
SetHasBitIndex(int32_t has_bit_index)273 void FieldGenerator::SetHasBitIndex(int32_t has_bit_index) {
274   if (!HasHasbit(descriptor_)) {
275     GOOGLE_CHECK_EQ(has_bit_index, -1);
276     return;
277   }
278   variables_["set_hasbit"] = StrCat(
279       variables_["has_bits"], "[", has_bit_index / 32, "] |= 0x",
280       strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
281   variables_["clear_hasbit"] = StrCat(
282       variables_["has_bits"], "[", has_bit_index / 32, "] &= ~0x",
283       strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
284 }
285 
SetInlinedStringIndex(int32_t inlined_string_index)286 void FieldGenerator::SetInlinedStringIndex(int32_t inlined_string_index) {
287   if (!IsStringInlined(descriptor_, options_)) {
288     GOOGLE_CHECK_EQ(inlined_string_index, -1);
289     return;
290   }
291   // The first bit is the tracking bit for on demand registering ArenaDtor.
292   GOOGLE_CHECK_GT(inlined_string_index, 0)
293       << "_inlined_string_donated_'s bit 0 is reserved for arena dtor tracking";
294   variables_["inlined_string_donated"] = StrCat(
295       "(", variables_["inlined_string_donated_array"], "[",
296       inlined_string_index / 32, "] & 0x",
297       strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8),
298       "u) != 0;");
299   variables_["donating_states_word"] =
300       StrCat(variables_["inlined_string_donated_array"], "[",
301                    inlined_string_index / 32, "]");
302   variables_["mask_for_undonate"] = StrCat(
303       "~0x", strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8),
304       "u");
305 }
306 
SetCommonOneofFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables)307 void SetCommonOneofFieldVariables(
308     const FieldDescriptor* descriptor,
309     std::map<std::string, std::string>* variables) {
310   const std::string prefix = descriptor->containing_oneof()->name() + "_.";
311   (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
312 }
313 
~FieldGenerator()314 FieldGenerator::~FieldGenerator() {}
315 
FieldGeneratorMap(const Descriptor * descriptor,const Options & options,MessageSCCAnalyzer * scc_analyzer)316 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
317                                      const Options& options,
318                                      MessageSCCAnalyzer* scc_analyzer)
319     : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
320   // Construct all the FieldGenerators.
321   for (int i = 0; i < descriptor->field_count(); i++) {
322     field_generators_[i].reset(
323         MakeGenerator(descriptor->field(i), options, scc_analyzer));
324   }
325 }
326 
MakeGoogleInternalGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)327 FieldGenerator* FieldGeneratorMap::MakeGoogleInternalGenerator(
328     const FieldDescriptor* field, const Options& options,
329     MessageSCCAnalyzer* scc_analyzer) {
330 
331   return nullptr;
332 }
333 
MakeGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)334 FieldGenerator* FieldGeneratorMap::MakeGenerator(
335     const FieldDescriptor* field, const Options& options,
336     MessageSCCAnalyzer* scc_analyzer) {
337   FieldGenerator* generator =
338       MakeGoogleInternalGenerator(field, options, scc_analyzer);
339   if (generator) {
340     return generator;
341   }
342 
343   if (field->is_repeated()) {
344     switch (field->cpp_type()) {
345       case FieldDescriptor::CPPTYPE_MESSAGE:
346         if (field->is_map()) {
347           return new MapFieldGenerator(field, options, scc_analyzer);
348         } else {
349           return new RepeatedMessageFieldGenerator(field, options,
350                                                    scc_analyzer);
351         }
352       case FieldDescriptor::CPPTYPE_STRING:
353         return new RepeatedStringFieldGenerator(field, options);
354       case FieldDescriptor::CPPTYPE_ENUM:
355         return new RepeatedEnumFieldGenerator(field, options);
356       default:
357         return new RepeatedPrimitiveFieldGenerator(field, options);
358     }
359   } else if (field->real_containing_oneof()) {
360     switch (field->cpp_type()) {
361       case FieldDescriptor::CPPTYPE_MESSAGE:
362         return new MessageOneofFieldGenerator(field, options, scc_analyzer);
363       case FieldDescriptor::CPPTYPE_STRING:
364         return new StringOneofFieldGenerator(field, options);
365       case FieldDescriptor::CPPTYPE_ENUM:
366         return new EnumOneofFieldGenerator(field, options);
367       default:
368         return new PrimitiveOneofFieldGenerator(field, options);
369     }
370   } else {
371     switch (field->cpp_type()) {
372       case FieldDescriptor::CPPTYPE_MESSAGE:
373         return new MessageFieldGenerator(field, options, scc_analyzer);
374       case FieldDescriptor::CPPTYPE_STRING:
375         return new StringFieldGenerator(field, options);
376       case FieldDescriptor::CPPTYPE_ENUM:
377         return new EnumFieldGenerator(field, options);
378       default:
379         return new PrimitiveFieldGenerator(field, options);
380     }
381   }
382 }
383 
~FieldGeneratorMap()384 FieldGeneratorMap::~FieldGeneratorMap() {}
385 
get(const FieldDescriptor * field) const386 const FieldGenerator& FieldGeneratorMap::get(
387     const FieldDescriptor* field) const {
388   GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
389   return *field_generators_[field->index()];
390 }
391 
392 }  // namespace cpp
393 }  // namespace compiler
394 }  // namespace protobuf
395 }  // namespace google
396