• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 //  Based on original Protocol Buffers design by
10 //  Sanjay Ghemawat, Jeff Dean, and others.
11 
12 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
13 #define GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
14 
15 #include <iterator>
16 #include <string>
17 #include <tuple>
18 #include <type_traits>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/log/absl_check.h"
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/str_split.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/types/optional.h"
27 #include "absl/types/span.h"
28 #include "google/protobuf/compiler/code_generator.h"
29 #include "google/protobuf/compiler/cpp/names.h"
30 #include "google/protobuf/compiler/cpp/options.h"
31 #include "google/protobuf/compiler/scc.h"
32 #include "google/protobuf/descriptor.h"
33 #include "google/protobuf/descriptor.pb.h"
34 #include "google/protobuf/generated_message_tctable_impl.h"
35 #include "google/protobuf/io/printer.h"
36 
37 
38 // Must be included last.
39 #include "google/protobuf/port_def.inc"
40 
41 namespace google {
42 namespace protobuf {
43 namespace compiler {
44 namespace cpp {
45 enum class ArenaDtorNeeds { kNone = 0, kOnDemand = 1, kRequired = 2 };
46 
ProtobufNamespace(const Options & opts)47 inline absl::string_view ProtobufNamespace(const Options& opts) {
48   // This won't be transformed by copybara, since copybara looks for google::protobuf::.
49   constexpr absl::string_view kGoogle3Ns = "proto2";
50   constexpr absl::string_view kOssNs = "google::protobuf";
51 
52   return opts.opensource_runtime ? kOssNs : kGoogle3Ns;
53 }
54 
DeprecatedAttribute(const Options &,const FieldDescriptor * d)55 inline std::string DeprecatedAttribute(const Options&,
56                                        const FieldDescriptor* d) {
57   return d->options().deprecated() ? "[[deprecated]] " : "";
58 }
59 
DeprecatedAttribute(const Options &,const EnumValueDescriptor * d)60 inline std::string DeprecatedAttribute(const Options&,
61                                        const EnumValueDescriptor* d) {
62   return d->options().deprecated() ? "[[deprecated]] " : "";
63 }
64 
65 // Commonly-used separator comments.  Thick is a line of '=', thin is a line
66 // of '-'.
67 extern const char kThickSeparator[];
68 extern const char kThinSeparator[];
69 
70 absl::flat_hash_map<absl::string_view, std::string> MessageVars(
71     const Descriptor* desc);
72 
73 // Variables to access message data from the message scope.
74 void SetCommonMessageDataVariables(
75     const Descriptor* descriptor,
76     absl::flat_hash_map<absl::string_view, std::string>* variables);
77 
78 absl::flat_hash_map<absl::string_view, std::string> UnknownFieldsVars(
79     const Descriptor* desc, const Options& opts);
80 
81 void SetUnknownFieldsVariable(
82     const Descriptor* descriptor, const Options& options,
83     absl::flat_hash_map<absl::string_view, std::string>* variables);
84 
85 bool GetBootstrapBasename(const Options& options, absl::string_view basename,
86                           std::string* bootstrap_basename);
87 bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context,
88                     bool bootstrap_flag, std::string* basename);
89 bool IsBootstrapProto(const Options& options, const FileDescriptor* file);
90 
91 // Name space of the proto file. This namespace is such that the string
92 // "<namespace>::some_name" is the correct fully qualified namespace.
93 // This means if the package is empty the namespace is "", and otherwise
94 // the namespace is "::foo::bar::...::baz" without trailing semi-colons.
95 std::string Namespace(const FileDescriptor* d, const Options& options);
96 std::string Namespace(const Descriptor* d, const Options& options);
97 std::string Namespace(const FieldDescriptor* d, const Options& options);
98 std::string Namespace(const EnumDescriptor* d, const Options& options);
99 PROTOC_EXPORT std::string Namespace(const FileDescriptor* d);
100 PROTOC_EXPORT std::string Namespace(const Descriptor* d);
101 PROTOC_EXPORT std::string Namespace(const FieldDescriptor* d);
102 PROTOC_EXPORT std::string Namespace(const EnumDescriptor* d);
103 
104 class MessageSCCAnalyzer;
105 
106 // Returns true if it's safe to init "field" to zero.
107 bool CanInitializeByZeroing(const FieldDescriptor* field,
108                             const Options& options,
109                             MessageSCCAnalyzer* scc_analyzer);
110 // Returns true if it's safe to reset "field" to zero.
111 bool CanClearByZeroing(const FieldDescriptor* field);
112 // Determines if swap can be implemented via memcpy.
113 bool HasTrivialSwap(const FieldDescriptor* field, const Options& options,
114                     MessageSCCAnalyzer* scc_analyzer);
115 
116 PROTOC_EXPORT std::string ClassName(const Descriptor* descriptor);
117 PROTOC_EXPORT std::string ClassName(const EnumDescriptor* enum_descriptor);
118 
119 std::string QualifiedClassName(const Descriptor* d, const Options& options);
120 std::string QualifiedClassName(const EnumDescriptor* d, const Options& options);
121 
122 PROTOC_EXPORT std::string QualifiedClassName(const Descriptor* d);
123 PROTOC_EXPORT std::string QualifiedClassName(const EnumDescriptor* d);
124 
125 // DEPRECATED just use ClassName or QualifiedClassName, a boolean is very
126 // unreadable at the callsite.
127 // Returns the non-nested type name for the given type.  If "qualified" is
128 // true, prefix the type with the full namespace.  For example, if you had:
129 //   package foo.bar;
130 //   message Baz { message Moo {} }
131 // Then the qualified ClassName for Moo would be:
132 //   ::foo::bar::Baz_Moo
133 // While the non-qualified version would be:
134 //   Baz_Moo
ClassName(const Descriptor * descriptor,bool qualified)135 inline std::string ClassName(const Descriptor* descriptor, bool qualified) {
136   return qualified ? QualifiedClassName(descriptor, Options())
137                    : ClassName(descriptor);
138 }
139 
ClassName(const EnumDescriptor * descriptor,bool qualified)140 inline std::string ClassName(const EnumDescriptor* descriptor, bool qualified) {
141   return qualified ? QualifiedClassName(descriptor, Options())
142                    : ClassName(descriptor);
143 }
144 
145 // Returns the extension name prefixed with the class name if nested but without
146 // the package name.
147 std::string ExtensionName(const FieldDescriptor* d);
148 
149 std::string QualifiedExtensionName(const FieldDescriptor* d,
150                                    const Options& options);
151 std::string QualifiedExtensionName(const FieldDescriptor* d);
152 
153 // Type name of default instance.
154 std::string DefaultInstanceType(const Descriptor* descriptor,
155                                 const Options& options, bool split = false);
156 
157 // Non-qualified name of the default_instance of this message.
158 std::string DefaultInstanceName(const Descriptor* descriptor,
159                                 const Options& options, bool split = false);
160 
161 // Non-qualified name of the default instance pointer. This is used only for
162 // implicit weak fields, where we need an extra indirection.
163 std::string DefaultInstancePtr(const Descriptor* descriptor,
164                                const Options& options, bool split = false);
165 
166 // Fully qualified name of the default_instance of this message.
167 std::string QualifiedDefaultInstanceName(const Descriptor* descriptor,
168                                          const Options& options,
169                                          bool split = false);
170 
171 // Fully qualified name of the default instance pointer.
172 std::string QualifiedDefaultInstancePtr(const Descriptor* descriptor,
173                                         const Options& options,
174                                         bool split = false);
175 
176 // DescriptorTable variable name.
177 std::string DescriptorTableName(const FileDescriptor* file,
178                                 const Options& options);
179 
180 // When declaring symbol externs from another file, this macro will supply the
181 // dllexport needed for the target file, if any.
182 std::string FileDllExport(const FileDescriptor* file, const Options& options);
183 
184 // Name of the base class: google::protobuf::Message or google::protobuf::MessageLite.
185 std::string SuperClassName(const Descriptor* descriptor,
186                            const Options& options);
187 
188 // Adds an underscore if necessary to prevent conflicting with a keyword.
189 std::string ResolveKeyword(absl::string_view name);
190 
191 // Get the (unqualified) name that should be used for this field in C++ code.
192 // The name is coerced to lower-case to emulate proto1 behavior.  People
193 // should be using lowercase-with-underscores style for proto field names
194 // anyway, so normally this just returns field->name().
195 PROTOC_EXPORT std::string FieldName(const FieldDescriptor* field);
196 
197 // Returns the (unqualified) private member name for this field in C++ code.
198 std::string FieldMemberName(const FieldDescriptor* field, bool split);
199 
200 // Returns an estimate of the compiler's alignment for the field.  This
201 // can't guarantee to be correct because the generated code could be compiled on
202 // different systems with different alignment rules.  The estimates below assume
203 // 64-bit pointers.
204 int EstimateAlignmentSize(const FieldDescriptor* field);
205 
206 // Returns an estimate of the size of the field.  This
207 // can't guarantee to be correct because the generated code could be compiled on
208 // different systems with different alignment rules.  The estimates below assume
209 // 64-bit pointers.
210 int EstimateSize(const FieldDescriptor* field);
211 
212 // Get the unqualified name that should be used for a field's field
213 // number constant.
214 std::string FieldConstantName(const FieldDescriptor* field);
215 
216 // Returns the scope where the field was defined (for extensions, this is
217 // different from the message type to which the field applies).
FieldScope(const FieldDescriptor * field)218 inline const Descriptor* FieldScope(const FieldDescriptor* field) {
219   return field->is_extension() ? field->extension_scope()
220                                : field->containing_type();
221 }
222 
223 // Returns the fully-qualified type name field->message_type().  Usually this
224 // is just ClassName(field->message_type(), true);
225 std::string FieldMessageTypeName(const FieldDescriptor* field,
226                                  const Options& options);
227 
228 // Get the C++ type name for a primitive type (e.g. "double", "::int32", etc.).
229 const char* PrimitiveTypeName(FieldDescriptor::CppType type);
230 std::string PrimitiveTypeName(const Options& options,
231                               FieldDescriptor::CppType type);
232 
233 // Get the declared type name in CamelCase format, as is used e.g. for the
234 // methods of WireFormat.  For example, TYPE_INT32 becomes "Int32".
235 const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
236 
237 // Return the code that evaluates to the number when compiled.
238 std::string Int32ToString(int number);
239 
240 // Get code that evaluates to the field's default value.
241 std::string DefaultValue(const Options& options, const FieldDescriptor* field);
242 
243 // Compatibility function for callers outside proto2.
244 std::string DefaultValue(const FieldDescriptor* field);
245 
246 // Convert a file name into a valid identifier.
247 std::string FilenameIdentifier(absl::string_view filename);
248 
249 // For each .proto file generates a unique name. To prevent collisions of
250 // symbols in the global namespace
251 std::string UniqueName(absl::string_view name, absl::string_view filename,
252                        const Options& options);
UniqueName(absl::string_view name,const FileDescriptor * d,const Options & options)253 inline std::string UniqueName(absl::string_view name, const FileDescriptor* d,
254                               const Options& options) {
255   return UniqueName(name, d->name(), options);
256 }
UniqueName(absl::string_view name,const Descriptor * d,const Options & options)257 inline std::string UniqueName(absl::string_view name, const Descriptor* d,
258                               const Options& options) {
259   return UniqueName(name, d->file(), options);
260 }
UniqueName(absl::string_view name,const EnumDescriptor * d,const Options & options)261 inline std::string UniqueName(absl::string_view name, const EnumDescriptor* d,
262                               const Options& options) {
263   return UniqueName(name, d->file(), options);
264 }
UniqueName(absl::string_view name,const ServiceDescriptor * d,const Options & options)265 inline std::string UniqueName(absl::string_view name,
266                               const ServiceDescriptor* d,
267                               const Options& options) {
268   return UniqueName(name, d->file(), options);
269 }
270 
271 // Versions for call sites that only support the internal runtime (like proto1
272 // support).
InternalRuntimeOptions()273 inline Options InternalRuntimeOptions() {
274   Options options;
275   options.opensource_runtime = false;
276   return options;
277 }
UniqueName(absl::string_view name,absl::string_view filename)278 inline std::string UniqueName(absl::string_view name,
279                               absl::string_view filename) {
280   return UniqueName(name, filename, InternalRuntimeOptions());
281 }
UniqueName(absl::string_view name,const FileDescriptor * d)282 inline std::string UniqueName(absl::string_view name, const FileDescriptor* d) {
283   return UniqueName(name, d->name(), InternalRuntimeOptions());
284 }
UniqueName(absl::string_view name,const Descriptor * d)285 inline std::string UniqueName(absl::string_view name, const Descriptor* d) {
286   return UniqueName(name, d->file(), InternalRuntimeOptions());
287 }
UniqueName(absl::string_view name,const EnumDescriptor * d)288 inline std::string UniqueName(absl::string_view name, const EnumDescriptor* d) {
289   return UniqueName(name, d->file(), InternalRuntimeOptions());
290 }
UniqueName(absl::string_view name,const ServiceDescriptor * d)291 inline std::string UniqueName(absl::string_view name,
292                               const ServiceDescriptor* d) {
293   return UniqueName(name, d->file(), InternalRuntimeOptions());
294 }
295 
296 // Return the qualified C++ name for a file level symbol.
297 std::string QualifiedFileLevelSymbol(const FileDescriptor* file,
298                                      absl::string_view name,
299                                      const Options& options);
300 
301 // Escape C++ trigraphs by escaping question marks to \?
302 std::string EscapeTrigraphs(absl::string_view to_escape);
303 
304 // Escaped function name to eliminate naming conflict.
305 std::string SafeFunctionName(const Descriptor* descriptor,
306                              const FieldDescriptor* field,
307                              absl::string_view prefix);
308 
309 // Returns the optimize mode for <file>, respecting <options.enforce_lite>.
310 FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file,
311                                         const Options& options);
312 
313 // Determines whether unknown fields will be stored in an UnknownFieldSet or
314 // a string.
UseUnknownFieldSet(const FileDescriptor * file,const Options & options)315 inline bool UseUnknownFieldSet(const FileDescriptor* file,
316                                const Options& options) {
317   return GetOptimizeFor(file, options) != FileOptions::LITE_RUNTIME;
318 }
319 
IsWeak(const FieldDescriptor * field,const Options & options)320 inline bool IsWeak(const FieldDescriptor* field, const Options& options) {
321   if (field->options().weak()) {
322     ABSL_CHECK(!options.opensource_runtime);
323     return true;
324   }
325   return false;
326 }
327 
IsCord(const FieldDescriptor * field)328 inline bool IsCord(const FieldDescriptor* field) {
329   return field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
330          internal::cpp::EffectiveStringCType(field) == FieldOptions::CORD;
331 }
332 
IsString(const FieldDescriptor * field)333 inline bool IsString(const FieldDescriptor* field) {
334   return field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
335          internal::cpp::EffectiveStringCType(field) == FieldOptions::STRING;
336 }
337 
IsStringPiece(const FieldDescriptor * field)338 inline bool IsStringPiece(const FieldDescriptor* field) {
339   return field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
340          internal::cpp::EffectiveStringCType(field) ==
341              FieldOptions::STRING_PIECE;
342 }
343 
344 bool IsProfileDriven(const Options& options);
345 
346 // Returns true if `field` is unlikely to be present based on PDProto profile.
347 bool IsRarelyPresent(const FieldDescriptor* field, const Options& options);
348 
349 // Returns true if `field` is likely to be present based on PDProto profile.
350 bool IsLikelyPresent(const FieldDescriptor* field, const Options& options);
351 
352 float GetPresenceProbability(const FieldDescriptor* field,
353                              const Options& options);
354 
355 bool IsStringInliningEnabled(const Options& options);
356 
357 // Returns true if the provided field is a singular string and can be inlined.
358 bool CanStringBeInlined(const FieldDescriptor* field);
359 
360 // Returns true if `field` is a string field that can and should be inlined
361 // based on PDProto profile.
362 bool IsStringInlined(const FieldDescriptor* field, const Options& options);
363 
364 // Returns true if `field` should be inlined based on PDProto profile.
365 // Currently we only enable inlining for string fields backed by a std::string
366 // instance, but in the future we may expand this to message types.
IsFieldInlined(const FieldDescriptor * field,const Options & options)367 inline bool IsFieldInlined(const FieldDescriptor* field,
368                            const Options& options) {
369   return IsStringInlined(field, options);
370 }
371 
372 // Does the given FileDescriptor use lazy fields?
373 bool HasLazyFields(const FileDescriptor* file, const Options& options,
374                    MessageSCCAnalyzer* scc_analyzer);
375 
376 // Is the given field a supported lazy field?
377 bool IsLazy(const FieldDescriptor* field, const Options& options,
378             MessageSCCAnalyzer* scc_analyzer);
379 
380 // Is this an explicit (non-profile driven) lazy field, as denoted by
381 // lazy/unverified_lazy in the descriptor?
IsExplicitLazy(const FieldDescriptor * field)382 inline bool IsExplicitLazy(const FieldDescriptor* field) {
383   if (field->is_map() || field->is_repeated()) {
384     return false;
385   }
386 
387   if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
388     return false;
389   }
390 
391   return field->options().lazy() || field->options().unverified_lazy();
392 }
393 
394 internal::field_layout::TransformValidation GetLazyStyle(
395     const FieldDescriptor* field, const Options& options,
396     MessageSCCAnalyzer* scc_analyzer);
397 
398 bool IsEagerlyVerifiedLazy(const FieldDescriptor* field, const Options& options,
399                            MessageSCCAnalyzer* scc_analyzer);
400 
401 bool IsLazilyVerifiedLazy(const FieldDescriptor* field, const Options& options);
402 
403 bool ShouldVerify(const Descriptor* descriptor, const Options& options,
404                   MessageSCCAnalyzer* scc_analyzer);
405 bool ShouldVerify(const FileDescriptor* file, const Options& options,
406                   MessageSCCAnalyzer* scc_analyzer);
407 bool ShouldVerifyRecursively(const FieldDescriptor* field);
408 
409 // Indicates whether to use predefined verify methods for a given message. If a
410 // message is "simple" and needs no special verification per field (e.g. message
411 // field, repeated packed, UTF8 string, etc.), we can use either VerifySimple or
412 // VerifySimpleAlwaysCheckInt32 methods as all verification can be done based on
413 // the wire type.
414 //
415 // Otherwise, we need "custom" verify methods tailored to a message to pass
416 // which field needs a special verification; i.e. InternalVerify.
417 enum class VerifySimpleType {
418   kSimpleInt32Never,   // Use VerifySimple
419   kSimpleInt32Always,  // Use VerifySimpleAlwaysCheckInt32
420   kCustom,             // Use InternalVerify and check only for int32
421   kCustomInt32Never,   // Use InternalVerify but never check for int32
422   kCustomInt32Always,  // Use InternalVerify and always check for int32
423 };
424 
425 // Returns VerifySimpleType if messages can be verified by predefined methods.
426 VerifySimpleType ShouldVerifySimple(const Descriptor* descriptor);
427 
428 
429 // Is the given message being split (go/pdsplit)?
430 bool ShouldSplit(const Descriptor* desc, const Options& options);
431 
432 // Is the given field being split out?
433 bool ShouldSplit(const FieldDescriptor* field, const Options& options);
434 
435 // Should we generate code that force creating an allocation in the constructor
436 // of the given message?
437 bool ShouldForceAllocationOnConstruction(const Descriptor* desc,
438                                          const Options& options);
439 
440 // Returns true if the message is present based on PDProto profile.
441 bool IsPresentMessage(const Descriptor* descriptor, const Options& options);
442 
443 // Returns the most likely present field. Returns nullptr if not profile driven.
444 const FieldDescriptor* FindHottestField(
445     const std::vector<const FieldDescriptor*>& fields, const Options& options);
446 
447 // Does the file contain any definitions that need extension_set.h?
448 bool HasExtensionsOrExtendableMessage(const FileDescriptor* file);
449 
450 // Does the file have any repeated fields, necessitating the file to include
451 // repeated_field.h? This does not include repeated extensions, since those are
452 // all stored internally in an ExtensionSet, not a separate RepeatedField*.
453 bool HasRepeatedFields(const FileDescriptor* file);
454 
455 // Does the file have any string/bytes fields with ctype=STRING_PIECE? This
456 // does not include extensions, since ctype is ignored for extensions.
457 bool HasStringPieceFields(const FileDescriptor* file, const Options& options);
458 
459 // Does the file have any string/bytes fields with ctype=CORD? This does not
460 // include extensions, since ctype is ignored for extensions.
461 bool HasCordFields(const FileDescriptor* file, const Options& options);
462 
463 // Does the file have any map fields, necessitating the file to include
464 // map_field_inl.h and map.h.
465 bool HasMapFields(const FileDescriptor* file);
466 
467 // Does this file have any enum type definitions?
468 bool HasEnumDefinitions(const FileDescriptor* file);
469 
470 // Returns true if a message in the file can have v2 table.
471 bool HasV2Table(const FileDescriptor* file);
472 
473 // Returns true if a message (descriptor) can have v2 table.
474 bool HasV2Table(const Descriptor* descriptor);
475 
476 // Does this file have generated parsing, serialization, and other
477 // standard methods for which reflection-based fallback implementations exist?
HasGeneratedMethods(const FileDescriptor * file,const Options & options)478 inline bool HasGeneratedMethods(const FileDescriptor* file,
479                                 const Options& options) {
480   return GetOptimizeFor(file, options) != FileOptions::CODE_SIZE;
481 }
482 
483 // Do message classes in this file have descriptor and reflection methods?
HasDescriptorMethods(const FileDescriptor * file,const Options & options)484 inline bool HasDescriptorMethods(const FileDescriptor* file,
485                                  const Options& options) {
486   return GetOptimizeFor(file, options) != FileOptions::LITE_RUNTIME;
487 }
488 
489 // Should we generate generic services for this file?
HasGenericServices(const FileDescriptor * file,const Options & options)490 inline bool HasGenericServices(const FileDescriptor* file,
491                                const Options& options) {
492   return file->service_count() > 0 &&
493          GetOptimizeFor(file, options) != FileOptions::LITE_RUNTIME &&
494          file->options().cc_generic_services();
495 }
496 
IsProto2MessageSet(const Descriptor * descriptor,const Options & options)497 inline bool IsProto2MessageSet(const Descriptor* descriptor,
498                                const Options& options) {
499   return !options.opensource_runtime &&
500          options.enforce_mode != EnforceOptimizeMode::kLiteRuntime &&
501          !options.lite_implicit_weak_fields &&
502          descriptor->options().message_set_wire_format() &&
503          descriptor->full_name() == "google.protobuf.bridge.MessageSet";
504 }
505 
IsMapEntryMessage(const Descriptor * descriptor)506 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
507   return descriptor->options().map_entry();
508 }
509 
510 // Returns true if the field's CPPTYPE is string or message.
511 bool IsStringOrMessage(const FieldDescriptor* field);
512 
513 std::string UnderscoresToCamelCase(absl::string_view input,
514                                    bool cap_next_letter);
515 
IsCrossFileMessage(const FieldDescriptor * field)516 inline bool IsCrossFileMessage(const FieldDescriptor* field) {
517   return field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
518          field->message_type()->file() != field->file();
519 }
520 
MakeDefaultName(const FieldDescriptor * field)521 inline std::string MakeDefaultName(const FieldDescriptor* field) {
522   return absl::StrCat("_i_give_permission_to_break_this_code_default_",
523                       FieldName(field), "_");
524 }
525 
526 // Semantically distinct from MakeDefaultName in that it gives the C++ code
527 // referencing a default field from the message scope, rather than just the
528 // variable name.
529 // For example, declarations of default variables should always use just
530 // MakeDefaultName to produce code like:
531 //   Type _i_give_permission_to_break_this_code_default_field_;
532 //
533 // Code that references these should use MakeDefaultFieldName, in case the field
534 // exists at some nested level like:
535 //   internal_container_._i_give_permission_to_break_this_code_default_field_;
MakeDefaultFieldName(const FieldDescriptor * field)536 inline std::string MakeDefaultFieldName(const FieldDescriptor* field) {
537   return absl::StrCat("Impl_::", MakeDefaultName(field));
538 }
539 
MakeVarintCachedSizeName(const FieldDescriptor * field)540 inline std::string MakeVarintCachedSizeName(const FieldDescriptor* field) {
541   return absl::StrCat("_", FieldName(field), "_cached_byte_size_");
542 }
543 
544 // Semantically distinct from MakeVarintCachedSizeName in that it gives the C++
545 // code referencing the object from the message scope, rather than just the
546 // variable name.
547 // For example, declarations of default variables should always use just
548 // MakeVarintCachedSizeName to produce code like:
549 //   Type _field_cached_byte_size_;
550 //
551 // Code that references these variables should use
552 // MakeVarintCachedSizeFieldName, in case the field exists at some nested level
553 // like:
554 //   internal_container_._field_cached_byte_size_;
MakeVarintCachedSizeFieldName(const FieldDescriptor * field,bool split)555 inline std::string MakeVarintCachedSizeFieldName(const FieldDescriptor* field,
556                                                  bool split) {
557   return absl::StrCat("_impl_.", split ? "_split_->" : "", "_",
558                       FieldName(field), "_cached_byte_size_");
559 }
560 
561 // Note: A lot of libraries detect Any protos based on Descriptor::full_name()
562 // while the two functions below use FileDescriptor::name(). In a sane world the
563 // two approaches should be equivalent. But if you are dealing with descriptors
564 // from untrusted sources, you might need to match semantics across libraries.
565 bool IsAnyMessage(const FileDescriptor* descriptor);
566 bool IsAnyMessage(const Descriptor* descriptor);
567 
568 bool IsWellKnownMessage(const FileDescriptor* file);
569 
570 enum class GeneratedFileType : int { kPbH, kProtoH, kProtoStaticReflectionH };
571 
IncludeGuard(const FileDescriptor * file,GeneratedFileType file_type,const Options & options)572 inline std::string IncludeGuard(const FileDescriptor* file,
573                                 GeneratedFileType file_type,
574                                 const Options& options) {
575   // If we are generating a .pb.h file and the proto_h option is enabled, then
576   // the .pb.h gets an extra suffix.
577   std::string extension;
578   switch (file_type) {
579     case GeneratedFileType::kPbH:
580       extension = ".pb.h";
581       break;
582     case GeneratedFileType::kProtoH:
583       extension = ".proto.h";
584       break;
585     case GeneratedFileType::kProtoStaticReflectionH:
586       extension = ".proto.static_reflection.h";
587   }
588   return FilenameIdentifier(absl::StrCat(file->name(), extension));
589 }
590 
591 // Returns the OptimizeMode for this file, furthermore it updates a status
592 // bool if has_opt_codesize_extension is non-null. If this status bool is true
593 // it means this file contains an extension that itself is defined as
594 // optimized_for = CODE_SIZE.
595 FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file,
596                                         const Options& options,
597                                         bool* has_opt_codesize_extension);
GetOptimizeFor(const FileDescriptor * file,const Options & options)598 inline FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file,
599                                                const Options& options) {
600   return GetOptimizeFor(file, options, nullptr);
601 }
NeedsEagerDescriptorAssignment(const FileDescriptor * file,const Options & options)602 inline bool NeedsEagerDescriptorAssignment(const FileDescriptor* file,
603                                            const Options& options) {
604   bool has_opt_codesize_extension;
605   if (GetOptimizeFor(file, options, &has_opt_codesize_extension) ==
606           FileOptions::CODE_SIZE &&
607       has_opt_codesize_extension) {
608     // If this filedescriptor contains an extension from another file which
609     // is optimized_for = CODE_SIZE. We need to be careful in the ordering so
610     // we eagerly build the descriptors in the dependencies before building
611     // the descriptors of this file.
612     return true;
613   } else {
614     // If we have a generated code based parser we never need eager
615     // initialization of descriptors of our deps.
616     return false;
617   }
618 }
619 
620 // This orders the messages in a .pb.cc as it's outputted by file.cc
621 void FlattenMessagesInFile(const FileDescriptor* file,
622                            std::vector<const Descriptor*>* result);
FlattenMessagesInFile(const FileDescriptor * file)623 inline std::vector<const Descriptor*> FlattenMessagesInFile(
624     const FileDescriptor* file) {
625   std::vector<const Descriptor*> result;
626   FlattenMessagesInFile(file, &result);
627   return result;
628 }
629 
630 std::vector<const Descriptor*> TopologicalSortMessagesInFile(
631     const FileDescriptor* file, MessageSCCAnalyzer& scc_analyzer);
632 
633 bool HasWeakFields(const Descriptor* desc, const Options& options);
634 bool HasWeakFields(const FileDescriptor* file, const Options& options);
635 
636 // Returns true if the "required" restriction check should be ignored for the
637 // given field.
ShouldIgnoreRequiredFieldCheck(const FieldDescriptor * field,const Options & options)638 inline static bool ShouldIgnoreRequiredFieldCheck(const FieldDescriptor* field,
639                                                   const Options& options) {
640   // Do not check "required" for lazily verified lazy fields.
641   return IsLazilyVerifiedLazy(field, options);
642 }
643 
644 struct MessageAnalysis {
645   bool is_recursive = false;
646   bool contains_cord = false;
647   bool contains_extension = false;
648   bool contains_required = false;
649   bool contains_weak = false;  // Implicit weak as well.
650 };
651 
652 // This class is used in FileGenerator, to ensure linear instead of
653 // quadratic performance, if we do this per message we would get O(V*(V+E)).
654 // Logically this is just only used in message.cc, but in the header for
655 // FileGenerator to help share it.
656 class PROTOC_EXPORT MessageSCCAnalyzer {
657  public:
MessageSCCAnalyzer(const Options & options)658   explicit MessageSCCAnalyzer(const Options& options) : options_(options) {}
659 
660   MessageAnalysis GetSCCAnalysis(const SCC* scc);
661 
HasRequiredFields(const Descriptor * descriptor)662   bool HasRequiredFields(const Descriptor* descriptor) {
663     MessageAnalysis result = GetSCCAnalysis(GetSCC(descriptor));
664     return result.contains_required || result.contains_extension;
665   }
HasWeakField(const Descriptor * descriptor)666   bool HasWeakField(const Descriptor* descriptor) {
667     MessageAnalysis result = GetSCCAnalysis(GetSCC(descriptor));
668     return result.contains_weak;
669   }
GetSCC(const Descriptor * descriptor)670   const SCC* GetSCC(const Descriptor* descriptor) {
671     return analyzer_.GetSCC(descriptor);
672   }
673 
674  private:
675   struct DepsGenerator {
operatorDepsGenerator676     std::vector<const Descriptor*> operator()(const Descriptor* desc) const {
677       std::vector<const Descriptor*> deps;
678       for (int i = 0; i < desc->field_count(); i++) {
679         if (desc->field(i)->message_type()) {
680           deps.push_back(desc->field(i)->message_type());
681         }
682       }
683       return deps;
684     }
685   };
686   SCCAnalyzer<DepsGenerator> analyzer_;
687   Options options_;
688   absl::flat_hash_map<const SCC*, MessageAnalysis> analysis_cache_;
689 };
690 
691 void ListAllFields(const Descriptor* d,
692                    std::vector<const FieldDescriptor*>* fields);
693 void ListAllFields(const FileDescriptor* d,
694                    std::vector<const FieldDescriptor*>* fields);
695 
696 template <bool do_nested_types, class T>
ForEachField(const Descriptor * d,T && func)697 void ForEachField(const Descriptor* d, T&& func) {
698   if (do_nested_types) {
699     for (int i = 0; i < d->nested_type_count(); i++) {
700       ForEachField<true>(d->nested_type(i), std::forward<T&&>(func));
701     }
702   }
703   for (int i = 0; i < d->extension_count(); i++) {
704     func(d->extension(i));
705   }
706   for (int i = 0; i < d->field_count(); i++) {
707     func(d->field(i));
708   }
709 }
710 
711 template <class T>
ForEachField(const FileDescriptor * d,T && func)712 void ForEachField(const FileDescriptor* d, T&& func) {
713   for (int i = 0; i < d->message_type_count(); i++) {
714     ForEachField<true>(d->message_type(i), std::forward<T&&>(func));
715   }
716   for (int i = 0; i < d->extension_count(); i++) {
717     func(d->extension(i));
718   }
719 }
720 
721 void ListAllTypesForServices(const FileDescriptor* fd,
722                              std::vector<const Descriptor*>* types);
723 
724 // Whether this type should use the implicit weak feature for descriptor based
725 // objects.
726 //
727 // This feature allows tree shaking within a single translation unit by
728 // decoupling the messages from the TU-wide `file_default_instances` array.
729 // This way there are no static initializers in the TU pointing to any part of
730 // the generated classes and they can be GC'd by the linker.
731 // Instead of direct use, we have two ways to weakly refer to the default
732 // instances:
733 //  - Each default instance is located on its own section, and we use a
734 //    `&__start_section_name` pointer to access it. This is a reference that
735 //    allows GC to happen. This step is used with dynamic linking.
736 //  - We also allow merging all these sections at link time into the
737 //    `pb_defaults` section. All surviving messages will be injected back into
738 //    the `file_default_instances` when the runtime is initialized. This is
739 //    useful when doing static linking and you want to avoid having an unbounded
740 //    number of sections.
741 //
742 // Any object that gets GC'd will have a `nullptr` in the respective slot in the
743 // `file_default_instances` array. The runtime will recognize this and will
744 // dynamically generate the object if needed. This logic is in the
745 // `GeneratedMessageFactory::GetPrototype`.  It will fall back to a
746 // `DynamicMessage` for the missing objects.
747 // This allows all of reflection to keep working normally, even for types that
748 // were dropped. Note that dropping the _classes_ will not drop the descriptor
749 // information. The messages are still going to be registered in the generated
750 // `DescriptorPool` and will be available via normal `FindMessageTypeByName` and
751 // friends.
752 //
753 // A "pin" is adding dependency edge in the graph for the GC.
754 // The default instance and vtable of a message pin each other. If any one
755 // lives, they both do. This is important. The default instance of the message
756 // pins the vtable trivially by using it. The vtable pins the default instance
757 // by having a StrongPointer into it from any of the virtual functions.
758 //
759 // All parent messages pin their children.
760 // SPEED messages do this implicitly via the TcParseTable, which contain
761 // pointers to the submessages.
762 // CODE_SIZE messages explicitly add a pin via `StrongPointer` somewhere in
763 // their codegen.
764 // LITE messages do not participate at all in this feature.
765 //
766 // For extensions, the identifiers currently pin the extendee. The extended is
767 // assumed to by pinned elsewhere since we already have an instance of it when
768 // we call `.GetExtension` et al. The extension identifier itself is not
769 // automatically pinned, so it has to be used to participate in the graph.
770 // Registration of the extensions do not pin the extended or the extendee. At
771 // registration time we will eagerly create a prototype object if one is
772 // missing to insert in the extension table in ExtensionSet.
773 //
774 // For services, the TU unconditionally pins the request/response objects.
775 // This is the status quo for simplicity to avoid modifying the RPC layer. It
776 // might be improved in the future.
777 bool UsingImplicitWeakDescriptor(const FileDescriptor* file,
778                                  const Options& options);
779 
780 // Generates a strong reference to the message in `desc`, as a statement.
781 std::string StrongReferenceToType(const Descriptor* desc,
782                                   const Options& options);
783 
784 // Generates the section name to be used for a data object when using implicit
785 // weak descriptors. The prefix determines the kind of object and the section it
786 // will be merged into afterwards.
787 // See `UsingImplicitWeakDescriptor` above.
788 std::string WeakDescriptorDataSection(absl::string_view prefix,
789                                       const Descriptor* descriptor,
790                                       int index_in_file_messages,
791                                       const Options& options);
792 
793 // Section name to be used for the default instance for implicit weak descriptor
794 // objects. See `UsingImplicitWeakDescriptor` above.
WeakDefaultInstanceSection(const Descriptor * descriptor,int index_in_file_messages,const Options & options)795 inline std::string WeakDefaultInstanceSection(const Descriptor* descriptor,
796                                               int index_in_file_messages,
797                                               const Options& options) {
798   return WeakDescriptorDataSection("def", descriptor, index_in_file_messages,
799                                    options);
800 }
801 
802 // Indicates whether we should use implicit weak fields for this file.
803 bool UsingImplicitWeakFields(const FileDescriptor* file,
804                              const Options& options);
805 
806 // Indicates whether to treat this field as implicitly weak.
807 bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options,
808                          MessageSCCAnalyzer* scc_analyzer);
809 
SimpleBaseClass(const Descriptor * desc,const Options & options)810 inline std::string SimpleBaseClass(const Descriptor* desc,
811                                    const Options& options) {
812   // The only base class we have derived from `Message`.
813   if (!HasDescriptorMethods(desc->file(), options)) return "";
814   // We don't use the base class to be able to inject the weak descriptor pins.
815   if (UsingImplicitWeakDescriptor(desc->file(), options)) return "";
816   if (desc->extension_range_count() != 0) return "";
817   // Don't use a simple base class if the field tracking is enabled. This
818   // ensures generating all methods to track.
819   if (options.field_listener_options.inject_field_listener_events) return "";
820   if (desc->field_count() == 0) {
821     return "ZeroFieldsBase";
822   }
823   // TODO: Support additional common message types with only one
824   // or two fields
825   return "";
826 }
827 
HasSimpleBaseClass(const Descriptor * desc,const Options & options)828 inline bool HasSimpleBaseClass(const Descriptor* desc, const Options& options) {
829   return !SimpleBaseClass(desc, options).empty();
830 }
831 
HasSimpleBaseClasses(const FileDescriptor * file,const Options & options)832 inline bool HasSimpleBaseClasses(const FileDescriptor* file,
833                                  const Options& options) {
834   return internal::cpp::VisitDescriptorsInFileOrder(
835       file, [&](const Descriptor* desc) {
836         return HasSimpleBaseClass(desc, options);
837       });
838 }
839 
840 // Returns true if this message has a _tracker_ field.
HasTracker(const Descriptor * desc,const Options & options)841 inline bool HasTracker(const Descriptor* desc, const Options& options) {
842   return options.field_listener_options.inject_field_listener_events &&
843          desc->file()->options().optimize_for() !=
844              google::protobuf::FileOptions::LITE_RUNTIME &&
845          !IsMapEntryMessage(desc);
846 }
847 
848 // Returns true if this message needs an Impl_ struct for it's data.
HasImplData(const Descriptor * desc,const Options & options)849 inline bool HasImplData(const Descriptor* desc, const Options& options) {
850   return !HasSimpleBaseClass(desc, options);
851 }
852 
853 // DO NOT USE IN NEW CODE! Use io::Printer directly instead. See b/242326974.
854 //
855 // Formatter is a functor class which acts as a closure around printer and
856 // the variable map. It's much like printer->Print except it supports both named
857 // variables that are substituted using a key value map and direct arguments. In
858 // the format string $1$, $2$, etc... are substituted for the first, second, ...
859 // direct argument respectively in the format call, it accepts both strings and
860 // integers. The implementation verifies all arguments are used and are "first"
861 // used in order of appearance in the argument list. For example,
862 //
863 // Format("return array[$1$];", 3) -> "return array[3];"
864 // Format("array[$2$] = $1$;", "Bla", 3) -> FATAL error (wrong order)
865 // Format("array[$1$] = $2$;", 3, "Bla") -> "array[3] = Bla;"
866 //
867 // The arguments can be used more than once like
868 //
869 // Format("array[$1$] = $2$;  // Index = $1$", 3, "Bla") ->
870 //        "array[3] = Bla;  // Index = 3"
871 //
872 // If you use more arguments use the following style to help the reader,
873 //
874 // Format("int $1$() {\n"
875 //        "  array[$2$] = $3$;\n"
876 //        "  return $4$;"
877 //        "}\n",
878 //        funname, // 1
879 //        idx,  // 2
880 //        varname,  // 3
881 //        retval);  // 4
882 //
883 // but consider using named variables. Named variables like $foo$, with some
884 // identifier foo, are looked up in the map. One additional feature is that
885 // spaces are accepted between the '$' delimiters, $ foo$ will
886 // substitute to " bar" if foo stands for "bar", but in case it's empty
887 // will substitute to "". Hence, for example,
888 //
889 // Format(vars, "$dllexport $void fun();") -> "void fun();"
890 //                                            "__declspec(export) void fun();"
891 //
892 // which is convenient to prevent double, leading or trailing spaces.
893 class PROTOC_EXPORT Formatter {
894  public:
Formatter(io::Printer * printer)895   explicit Formatter(io::Printer* printer) : printer_(printer) {}
Formatter(io::Printer * printer,const absl::flat_hash_map<absl::string_view,std::string> & vars)896   Formatter(io::Printer* printer,
897             const absl::flat_hash_map<absl::string_view, std::string>& vars)
898       : printer_(printer), vars_(vars) {}
899 
900   template <typename T>
Set(absl::string_view key,const T & value)901   void Set(absl::string_view key, const T& value) {
902     vars_[key] = ToString(value);
903   }
904 
905   template <typename... Args>
operator()906   void operator()(const char* format, const Args&... args) const {
907     printer_->FormatInternal({ToString(args)...}, vars_, format);
908   }
909 
Indent()910   void Indent() const { printer_->Indent(); }
Outdent()911   void Outdent() const { printer_->Outdent(); }
printer()912   io::Printer* printer() const { return printer_; }
913 
914   class PROTOC_EXPORT ScopedIndenter {
915    public:
ScopedIndenter(Formatter * format)916     explicit ScopedIndenter(Formatter* format) : format_(format) {
917       format_->Indent();
918     }
~ScopedIndenter()919     ~ScopedIndenter() { format_->Outdent(); }
920 
921    private:
922     Formatter* format_;
923   };
924 
ScopedIndent()925   PROTOBUF_NODISCARD ScopedIndenter ScopedIndent() {
926     return ScopedIndenter(this);
927   }
928   template <typename... Args>
ScopedIndent(const char * format,const Args &&...args)929   PROTOBUF_NODISCARD ScopedIndenter ScopedIndent(const char* format,
930                                                  const Args&&... args) {
931     (*this)(format, static_cast<Args&&>(args)...);
932     return ScopedIndenter(this);
933   }
934 
935  private:
936   io::Printer* printer_;
937   absl::flat_hash_map<absl::string_view, std::string> vars_;
938 
939   // Convenience overloads to accept different types as arguments.
ToString(absl::string_view s)940   static std::string ToString(absl::string_view s) { return std::string(s); }
941   template <typename I, typename = typename std::enable_if<
942                             std::is_integral<I>::value>::type>
ToString(I x)943   static std::string ToString(I x) {
944     return absl::StrCat(x);
945   }
ToString(absl::Hex x)946   static std::string ToString(absl::Hex x) { return absl::StrCat(x); }
ToString(const FieldDescriptor * d)947   static std::string ToString(const FieldDescriptor* d) {
948     return Payload(d, GeneratedCodeInfo::Annotation::NONE);
949   }
ToString(const Descriptor * d)950   static std::string ToString(const Descriptor* d) {
951     return Payload(d, GeneratedCodeInfo::Annotation::NONE);
952   }
ToString(const EnumDescriptor * d)953   static std::string ToString(const EnumDescriptor* d) {
954     return Payload(d, GeneratedCodeInfo::Annotation::NONE);
955   }
ToString(const EnumValueDescriptor * d)956   static std::string ToString(const EnumValueDescriptor* d) {
957     return Payload(d, GeneratedCodeInfo::Annotation::NONE);
958   }
ToString(const OneofDescriptor * d)959   static std::string ToString(const OneofDescriptor* d) {
960     return Payload(d, GeneratedCodeInfo::Annotation::NONE);
961   }
962 
ToString(std::tuple<const FieldDescriptor *,GeneratedCodeInfo::Annotation::Semantic> p)963   static std::string ToString(
964       std::tuple<const FieldDescriptor*,
965                  GeneratedCodeInfo::Annotation::Semantic>
966           p) {
967     return Payload(std::get<0>(p), std::get<1>(p));
968   }
ToString(std::tuple<const Descriptor *,GeneratedCodeInfo::Annotation::Semantic> p)969   static std::string ToString(
970       std::tuple<const Descriptor*, GeneratedCodeInfo::Annotation::Semantic>
971           p) {
972     return Payload(std::get<0>(p), std::get<1>(p));
973   }
ToString(std::tuple<const EnumDescriptor *,GeneratedCodeInfo::Annotation::Semantic> p)974   static std::string ToString(
975       std::tuple<const EnumDescriptor*, GeneratedCodeInfo::Annotation::Semantic>
976           p) {
977     return Payload(std::get<0>(p), std::get<1>(p));
978   }
ToString(std::tuple<const EnumValueDescriptor *,GeneratedCodeInfo::Annotation::Semantic> p)979   static std::string ToString(
980       std::tuple<const EnumValueDescriptor*,
981                  GeneratedCodeInfo::Annotation::Semantic>
982           p) {
983     return Payload(std::get<0>(p), std::get<1>(p));
984   }
ToString(std::tuple<const OneofDescriptor *,GeneratedCodeInfo::Annotation::Semantic> p)985   static std::string ToString(
986       std::tuple<const OneofDescriptor*,
987                  GeneratedCodeInfo::Annotation::Semantic>
988           p) {
989     return Payload(std::get<0>(p), std::get<1>(p));
990   }
991 
992   template <typename Descriptor>
Payload(const Descriptor * descriptor,GeneratedCodeInfo::Annotation::Semantic semantic)993   static std::string Payload(const Descriptor* descriptor,
994                              GeneratedCodeInfo::Annotation::Semantic semantic) {
995     std::vector<int> path;
996     descriptor->GetLocationPath(&path);
997     GeneratedCodeInfo::Annotation annotation;
998     for (int index : path) {
999       annotation.add_path(index);
1000     }
1001     annotation.set_source_file(descriptor->file()->name());
1002     annotation.set_semantic(semantic);
1003     return annotation.SerializeAsString();
1004   }
1005 };
1006 
1007 template <typename T>
FieldComment(const T * field,const Options & options)1008 std::string FieldComment(const T* field, const Options& options) {
1009   if (options.strip_nonfunctional_codegen) {
1010     return std::string(field->name());
1011   }
1012   // Print the field's (or oneof's) proto-syntax definition as a comment.
1013   // We don't want to print group bodies so we cut off after the first
1014   // line.
1015   DebugStringOptions debug_options;
1016   debug_options.elide_group_body = true;
1017   debug_options.elide_oneof_body = true;
1018 
1019   for (absl::string_view chunk :
1020        absl::StrSplit(field->DebugStringWithOptions(debug_options), '\n')) {
1021     return std::string(chunk);
1022   }
1023 
1024   return "<unknown>";
1025 }
1026 
1027 template <class T>
PrintFieldComment(const Formatter & format,const T * field,const Options & options)1028 void PrintFieldComment(const Formatter& format, const T* field,
1029                        const Options& options) {
1030   format("// $1$\n", FieldComment(field, options));
1031 }
1032 
1033 class PROTOC_EXPORT NamespaceOpener {
1034  public:
1035   explicit NamespaceOpener(
1036       io::Printer* p,
1037       io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
p_(p)1038       : p_(p), loc_(loc) {}
1039 
1040   explicit NamespaceOpener(
1041       const Formatter& format,
1042       io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
1043       : NamespaceOpener(format.printer(), loc) {}
1044 
1045   NamespaceOpener(
1046       absl::string_view name, const Formatter& format,
1047       io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
1048       : NamespaceOpener(name, format.printer(), loc) {}
1049 
1050   NamespaceOpener(
1051       absl::string_view name, io::Printer* p,
1052       io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
NamespaceOpener(p,loc)1053       : NamespaceOpener(p, loc) {
1054     ChangeTo(name, loc);
1055   }
1056 
~NamespaceOpener()1057   ~NamespaceOpener() { ChangeTo("", loc_); }
1058 
1059   void ChangeTo(
1060       absl::string_view name,
1061       io::Printer::SourceLocation loc = io::Printer::SourceLocation::current());
1062 
1063  private:
1064   io::Printer* p_;
1065   io::Printer::SourceLocation loc_;
1066   std::vector<std::string> name_stack_;
1067 };
1068 
1069 void GenerateUtf8CheckCodeForString(const FieldDescriptor* field,
1070                                     const Options& options, bool for_parse,
1071                                     absl::string_view parameters,
1072                                     const Formatter& format);
1073 
1074 void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field,
1075                                   const Options& options, bool for_parse,
1076                                   absl::string_view parameters,
1077                                   const Formatter& format);
1078 
1079 void GenerateUtf8CheckCodeForString(io::Printer* p,
1080                                     const FieldDescriptor* field,
1081                                     const Options& options, bool for_parse,
1082                                     absl::string_view parameters);
1083 
1084 void GenerateUtf8CheckCodeForCord(io::Printer* p, const FieldDescriptor* field,
1085                                   const Options& options, bool for_parse,
1086                                   absl::string_view parameters);
1087 
ShouldGenerateExternSpecializations(const Options & options)1088 inline bool ShouldGenerateExternSpecializations(const Options& options) {
1089   // For OSS we omit the specializations to reduce codegen size.
1090   // Some compilers can't handle that much input in a single translation unit.
1091   // These specializations are just a link size optimization and do not affect
1092   // correctness or performance, so it is ok to omit them.
1093   return !options.opensource_runtime;
1094 }
1095 
1096 struct OneOfRangeImpl {
1097   struct Iterator {
1098     using iterator_category = std::forward_iterator_tag;
1099     using value_type = const OneofDescriptor*;
1100     using difference_type = int;
1101 
1102     value_type operator*() { return descriptor->oneof_decl(idx); }
1103 
1104     friend bool operator==(const Iterator& a, const Iterator& b) {
1105       ABSL_DCHECK(a.descriptor == b.descriptor);
1106       return a.idx == b.idx;
1107     }
1108     friend bool operator!=(const Iterator& a, const Iterator& b) {
1109       return !(a == b);
1110     }
1111 
1112     Iterator& operator++() {
1113       idx++;
1114       return *this;
1115     }
1116 
1117     int idx;
1118     const Descriptor* descriptor;
1119   };
1120 
beginOneOfRangeImpl1121   Iterator begin() const { return {0, descriptor}; }
endOneOfRangeImpl1122   Iterator end() const {
1123     return {descriptor->real_oneof_decl_count(), descriptor};
1124   }
1125 
1126   const Descriptor* descriptor;
1127 };
1128 
OneOfRange(const Descriptor * desc)1129 inline OneOfRangeImpl OneOfRange(const Descriptor* desc) { return {desc}; }
1130 
1131 // Strips ".proto" or ".protodevel" from the end of a filename.
1132 PROTOC_EXPORT std::string StripProto(absl::string_view filename);
1133 
1134 bool HasMessageFieldOrExtension(const Descriptor* desc);
1135 
1136 // Generates a vector of substitutions for use with Printer::WithVars that
1137 // contains annotated accessor names for a particular field.
1138 //
1139 // Each substitution will be named `absl::StrCat(prefix, "name")`, and will
1140 // be annotated with `field`.
1141 std::vector<io::Printer::Sub> AnnotatedAccessors(
1142     const FieldDescriptor* field, absl::Span<const absl::string_view> prefixes,
1143     absl::optional<google::protobuf::io::AnnotationCollector::Semantic> semantic =
1144         absl::nullopt);
1145 
1146 // Check whether `file` represents the .proto file FileDescriptorProto and
1147 // friends. This file needs special handling because it must be usable during
1148 // dynamic initialization.
1149 bool IsFileDescriptorProto(const FileDescriptor* file, const Options& options);
1150 
1151 // Determine if we should generate a class for the descriptor.
1152 // Some descriptors, like some map entries, are not represented as a generated
1153 // class.
1154 bool ShouldGenerateClass(const Descriptor* descriptor, const Options& options);
1155 
1156 
1157 // Determine if we are going to generate a tracker call for OnDeserialize.
1158 // This one is handled specially because we generate the PostLoopHandler for it.
1159 // We don't want to generate a handler if it is going to end up empty.
1160 bool HasOnDeserializeTracker(const Descriptor* descriptor,
1161                              const Options& options);
1162 
1163 // Determine if we need a PostLoopHandler function to inject into TcParseTable's
1164 // ParseLoop.
1165 // If this returns true, the parse table generation will use
1166 // `&ClassName::PostLoopHandler` which should be a static function of the right
1167 // signature.
1168 bool NeedsPostLoopHandler(const Descriptor* descriptor, const Options& options);
1169 
1170 // Priority used for static initializers.
1171 enum InitPriority {
1172   kInitPriority101,
1173   kInitPriority102,
1174 };
1175 
1176 }  // namespace cpp
1177 }  // namespace compiler
1178 }  // namespace protobuf
1179 }  // namespace google
1180 
1181 #include "google/protobuf/port_undef.inc"
1182 
1183 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
1184