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/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 | |
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 | x: std::string(annotation_name))) |
72 | return; |
73 | (*variables)[StrCat(a: "annotate_" , b: annotation_name)] = strings::Substitute( |
74 | format: StrCat(a: substitute_template_prefix, b: prepared_template, c: ");\n" ), |
75 | arg0: field_index, arg1: access_type); |
76 | } |
77 | |
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(field: 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(format: StrCat(a: "_internal_has_" , b: field_name, c: "() ? " , |
89 | d: field_pointer, e: ": nullptr" ), |
90 | arg0: field_member); |
91 | } |
92 | |
93 | if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING_PIECE) { |
94 | return strings::Substitute(format: StrCat(a: "_internal_has_" , b: field_name, c: "() ? " , |
95 | d: field_pointer, e: ": nullptr" ), |
96 | arg0: 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 | format: StrCat(a: "_internal_has_" , b: field_name, c: "() ? " , d: field_pointer, e: " : " , |
105 | f: default_value_pointer), |
106 | arg0: field_member, arg1: MakeDefaultFieldName(field: descriptor)); |
107 | } |
108 | |
109 | std::string GenerateTemplateForSingleString(const FieldDescriptor* descriptor, |
110 | StringPiece field_member) { |
111 | if (descriptor->default_value_string().empty()) { |
112 | return StrCat(a: "&" , b: field_member); |
113 | } |
114 | |
115 | if (descriptor->options().ctype() == google::protobuf::FieldOptions::STRING) { |
116 | return strings::Substitute( |
117 | format: "$0.IsDefault() ? &$1.get() : $0.UnsafeGetPointer()" , arg0: field_member, |
118 | arg1: MakeDefaultFieldName(field: descriptor)); |
119 | } |
120 | |
121 | return StrCat(a: "&" , b: field_member); |
122 | } |
123 | |
124 | } // namespace |
125 | |
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 | a: " " , b: FieldName(field: descriptor), c: "_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(a: " " , b: (*variables)["tracker" ], c: ".$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(format: "&$0.Get(index)" , arg0: field_member); |
170 | prepared_add_template = |
171 | strings::Substitute(format: "&$0.Get($0.size() - 1)" , arg0: 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(field: descriptor)) { |
180 | prepared_template = "nullptr" ; |
181 | } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { |
182 | if (oneof_member) { |
183 | prepared_template = GenerateTemplateForOneofString( |
184 | descriptor, proto_ns: (*variables)["proto_ns" ], field_member); |
185 | } else { |
186 | prepared_template = |
187 | GenerateTemplateForSingleString(descriptor, field_member); |
188 | } |
189 | } else { |
190 | prepared_template = StrCat(a: "&" , b: 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(a: "&" , b: field_member); |
196 | } else { |
197 | prepared_flat_template = prepared_template; |
198 | } |
199 | |
200 | MaySetAnnotationVariable(options, annotation_name: "get" , substitute_template_prefix, |
201 | prepared_template, field_index: descriptor->index(), access_type: "OnGet" , |
202 | variables); |
203 | MaySetAnnotationVariable(options, annotation_name: "set" , substitute_template_prefix, |
204 | prepared_template, field_index: descriptor->index(), access_type: "OnSet" , |
205 | variables); |
206 | MaySetAnnotationVariable(options, annotation_name: "has" , substitute_template_prefix, |
207 | prepared_template, field_index: descriptor->index(), access_type: "OnHas" , |
208 | variables); |
209 | MaySetAnnotationVariable(options, annotation_name: "mutable" , substitute_template_prefix, |
210 | prepared_template, field_index: descriptor->index(), access_type: "OnMutable" , |
211 | variables); |
212 | MaySetAnnotationVariable(options, annotation_name: "release" , substitute_template_prefix, |
213 | prepared_template, field_index: descriptor->index(), access_type: "OnRelease" , |
214 | variables); |
215 | MaySetAnnotationVariable(options, annotation_name: "clear" , substitute_template_prefix, |
216 | prepared_template: prepared_flat_template, field_index: descriptor->index(), |
217 | access_type: "OnClear" , variables); |
218 | MaySetAnnotationVariable(options, annotation_name: "size" , substitute_template_prefix, |
219 | prepared_template: prepared_flat_template, field_index: descriptor->index(), |
220 | access_type: "OnSize" , variables); |
221 | MaySetAnnotationVariable(options, annotation_name: "list" , substitute_template_prefix, |
222 | prepared_template: prepared_flat_template, field_index: descriptor->index(), |
223 | access_type: "OnList" , variables); |
224 | MaySetAnnotationVariable(options, annotation_name: "mutable_list" , substitute_template_prefix, |
225 | prepared_template: prepared_flat_template, field_index: descriptor->index(), |
226 | access_type: "OnMutableList" , variables); |
227 | MaySetAnnotationVariable(options, annotation_name: "add" , substitute_template_prefix, |
228 | prepared_template: prepared_add_template, field_index: descriptor->index(), access_type: "OnAdd" , |
229 | variables); |
230 | MaySetAnnotationVariable(options, annotation_name: "add_mutable" , substitute_template_prefix, |
231 | prepared_template: prepared_add_template, field_index: descriptor->index(), |
232 | access_type: "OnAddMutable" , variables); |
233 | } |
234 | |
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: descriptor->containing_type(), variables); |
240 | |
241 | (*variables)["ns" ] = Namespace(d: descriptor, options); |
242 | (*variables)["name" ] = FieldName(field: descriptor); |
243 | (*variables)["index" ] = StrCat(a: descriptor->index()); |
244 | (*variables)["number" ] = StrCat(a: descriptor->number()); |
245 | (*variables)["classname" ] = ClassName(descriptor: FieldScope(field: descriptor), qualified: false); |
246 | (*variables)["declared_type" ] = DeclaredTypeMethodName(type: descriptor->type()); |
247 | bool split = ShouldSplit(field: descriptor, options); |
248 | (*variables)["field" ] = FieldMemberName(field: descriptor, split); |
249 | |
250 | (*variables)["tag_size" ] = StrCat( |
251 | a: WireFormat::TagSize(field_number: descriptor->number(), type: descriptor->type())); |
252 | (*variables)["deprecated_attr" ] = DeprecatedAttribute(options, d: 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 | |
269 | void FieldGenerator::SetHasBitIndex(int32_t has_bit_index) { |
270 | if (!HasHasbit(field: descriptor_)) { |
271 | GOOGLE_CHECK_EQ(has_bit_index, -1); |
272 | return; |
273 | } |
274 | variables_["set_hasbit" ] = StrCat( |
275 | a: variables_["has_bits" ], b: "[" , c: has_bit_index / 32, d: "] |= 0x" , |
276 | e: strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), f: "u;" ); |
277 | variables_["clear_hasbit" ] = StrCat( |
278 | a: variables_["has_bits" ], b: "[" , c: has_bit_index / 32, d: "] &= ~0x" , |
279 | e: strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), f: "u;" ); |
280 | } |
281 | |
282 | void FieldGenerator::SetInlinedStringIndex(int32_t inlined_string_index) { |
283 | if (!IsStringInlined(descriptor: descriptor_, options: 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 | a: "(" , b: variables_["inlined_string_donated_array" ], c: "[" , |
292 | d: inlined_string_index / 32, e: "] & 0x" , |
293 | f: strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8), |
294 | g: "u) != 0;" ); |
295 | variables_["donating_states_word" ] = |
296 | StrCat(a: variables_["inlined_string_donated_array" ], b: "[" , |
297 | c: inlined_string_index / 32, d: "]" ); |
298 | variables_["mask_for_undonate" ] = StrCat( |
299 | a: "~0x" , b: strings::Hex(1u << (inlined_string_index % 32), strings::ZERO_PAD_8), |
300 | c: "u" ); |
301 | } |
302 | |
303 | void FieldGenerator::GenerateAggregateInitializer(io::Printer* printer) const { |
304 | Formatter format(printer, variables_); |
305 | if (ShouldSplit(field: descriptor_, options: options_)) { |
306 | format("decltype(Impl_::Split::$name$_){arena}" ); |
307 | return; |
308 | } |
309 | format("decltype($field$){arena}" ); |
310 | } |
311 | |
312 | void FieldGenerator::GenerateConstexprAggregateInitializer( |
313 | io::Printer* printer) const { |
314 | Formatter format(printer, variables_); |
315 | format("/*decltype($field$)*/{}" ); |
316 | } |
317 | |
318 | void FieldGenerator::GenerateCopyAggregateInitializer( |
319 | io::Printer* printer) const { |
320 | Formatter format(printer, variables_); |
321 | format("decltype($field$){from.$field$}" ); |
322 | } |
323 | |
324 | void FieldGenerator::GenerateCopyConstructorCode(io::Printer* printer) const { |
325 | if (ShouldSplit(field: descriptor_, options: 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 | |
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 | |
340 | FieldGenerator::~FieldGenerator() {} |
341 | |
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 | p: MakeGenerator(field: descriptor->field(index: i), options, scc_analyzer)); |
350 | } |
351 | } |
352 | |
353 | FieldGenerator* FieldGeneratorMap::MakeGoogleInternalGenerator( |
354 | const FieldDescriptor* field, const Options& options, |
355 | MessageSCCAnalyzer* scc_analyzer) { |
356 | |
357 | return nullptr; |
358 | } |
359 | |
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 | |
410 | FieldGeneratorMap::~FieldGeneratorMap() {} |
411 | |
412 | 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 | |