cpp_message.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
  35. #include <memory>
  36. #include <set>
  37. #include <string>
  38. #include <google/protobuf/compiler/cpp/cpp_field.h>
  39. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  40. #include <google/protobuf/compiler/cpp/cpp_message_layout_helper.h>
  41. #include <google/protobuf/compiler/cpp/cpp_options.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace io {
  45. class Printer; // printer.h
  46. }
  47. }
  48. namespace protobuf {
  49. namespace compiler {
  50. namespace cpp {
  51. class EnumGenerator; // enum.h
  52. class ExtensionGenerator; // extension.h
  53. class MessageGenerator {
  54. public:
  55. // See generator.cc for the meaning of dllexport_decl.
  56. MessageGenerator(const Descriptor* descriptor, int index_in_file_messages,
  57. const Options& options, SCCAnalyzer* scc_analyzer);
  58. ~MessageGenerator();
  59. // Append the two types of nested generators to the corresponding vector.
  60. void AddGenerators(std::vector<EnumGenerator*>* enum_generators,
  61. std::vector<ExtensionGenerator*>* extension_generators);
  62. // Header stuff.
  63. // Return names for forward declarations of this class and all its nested
  64. // types. A given key in {class,enum}_names will map from a class name to the
  65. // descriptor that was responsible for its inclusion in the map. This can be
  66. // used to associate the descriptor with the code generated for it.
  67. void FillMessageForwardDeclarations(
  68. std::map<string, const Descriptor*>* class_names);
  69. // Generate definitions for this class and all its nested types.
  70. void GenerateClassDefinition(io::Printer* printer);
  71. // Generate definitions of inline methods (placed at the end of the header
  72. // file).
  73. void GenerateInlineMethods(io::Printer* printer);
  74. // Source file stuff.
  75. // Generate extra fields
  76. void GenerateExtraDefaultFields(io::Printer* printer);
  77. // Generates code that creates default instances for fields.
  78. void GenerateFieldDefaultInstances(io::Printer* printer);
  79. // Generates code that initializes the message's default instance. This
  80. // is separate from allocating because all default instances must be
  81. // allocated before any can be initialized.
  82. void GenerateDefaultInstanceInitializer(io::Printer* printer);
  83. // Generate all non-inline methods for this class.
  84. void GenerateClassMethods(io::Printer* printer);
  85. // Generate source file code that should go outside any namespace.
  86. void GenerateSourceInProto2Namespace(io::Printer* printer);
  87. private:
  88. // Generate declarations and definitions of accessors for fields.
  89. void GenerateFieldAccessorDeclarations(io::Printer* printer);
  90. void GenerateFieldAccessorDefinitions(io::Printer* printer);
  91. // Generate the table-driven parsing array. Returns the number of entries
  92. // generated.
  93. size_t GenerateParseOffsets(io::Printer* printer);
  94. size_t GenerateParseAuxTable(io::Printer* printer);
  95. // Generates a ParseTable entry. Returns whether the proto uses table-driven
  96. // parsing.
  97. bool GenerateParseTable(io::Printer* printer, size_t offset,
  98. size_t aux_offset);
  99. // Generate the field offsets array. Returns the a pair of the total numer
  100. // of entries generated and the index of the first has_bit entry.
  101. std::pair<size_t, size_t> GenerateOffsets(io::Printer* printer);
  102. void GenerateSchema(io::Printer* printer, int offset, int has_offset);
  103. // For each field generates a table entry describing the field for the
  104. // table driven serializer.
  105. int GenerateFieldMetadata(io::Printer* printer);
  106. // Generate constructors and destructor.
  107. void GenerateStructors(io::Printer* printer);
  108. // The compiler typically generates multiple copies of each constructor and
  109. // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx
  110. // Placing common code in a separate method reduces the generated code size.
  111. //
  112. // Generate the shared constructor code.
  113. void GenerateSharedConstructorCode(io::Printer* printer);
  114. // Generate the shared destructor code.
  115. void GenerateSharedDestructorCode(io::Printer* printer);
  116. // Generate the arena-specific destructor code.
  117. void GenerateArenaDestructorCode(io::Printer* printer);
  118. // Helper for GenerateClear and others. Optionally emits a condition that
  119. // assumes the existence of the cached_has_bits variable, and returns true if
  120. // the condition was printed.
  121. bool MaybeGenerateOptionalFieldCondition(io::Printer* printer,
  122. const FieldDescriptor* field,
  123. int expected_has_bits_index);
  124. // Generate standard Message methods.
  125. void GenerateClear(io::Printer* printer);
  126. void GenerateOneofClear(io::Printer* printer);
  127. void GenerateMergeFromCodedStream(io::Printer* printer);
  128. void GenerateSerializeWithCachedSizes(io::Printer* printer);
  129. void GenerateSerializeWithCachedSizesToArray(io::Printer* printer);
  130. void GenerateSerializeWithCachedSizesBody(io::Printer* printer,
  131. bool to_array);
  132. void GenerateByteSize(io::Printer* printer);
  133. void GenerateMergeFrom(io::Printer* printer);
  134. void GenerateCopyFrom(io::Printer* printer);
  135. void GenerateSwap(io::Printer* printer);
  136. void GenerateIsInitialized(io::Printer* printer);
  137. // Helpers for GenerateSerializeWithCachedSizes().
  138. //
  139. // cached_has_bit_index maintains that:
  140. // cached_has_bits = _has_bits_[cached_has_bit_index]
  141. // for cached_has_bit_index >= 0
  142. void GenerateSerializeOneField(io::Printer* printer,
  143. const FieldDescriptor* field,
  144. bool unbounded,
  145. int cached_has_bits_index);
  146. // Generate a switch statement to serialize 2+ fields from the same oneof.
  147. // Or, if fields.size() == 1, just call GenerateSerializeOneField().
  148. void GenerateSerializeOneofFields(
  149. io::Printer* printer, const std::vector<const FieldDescriptor*>& fields,
  150. bool to_array);
  151. void GenerateSerializeOneExtensionRange(
  152. io::Printer* printer, const Descriptor::ExtensionRange* range,
  153. bool unbounded);
  154. // Generates has_foo() functions and variables for singular field has-bits.
  155. void GenerateSingularFieldHasBits(const FieldDescriptor* field,
  156. std::map<string, string> vars,
  157. io::Printer* printer);
  158. // Generates has_foo() functions and variables for oneof field has-bits.
  159. void GenerateOneofHasBits(io::Printer* printer);
  160. // Generates has_foo_bar() functions for oneof members.
  161. void GenerateOneofMemberHasBits(const FieldDescriptor* field,
  162. const std::map<string, string>& vars,
  163. io::Printer* printer);
  164. // Generates the clear_foo() method for a field.
  165. void GenerateFieldClear(const FieldDescriptor* field,
  166. const std::map<string, string>& vars,
  167. bool is_inline,
  168. io::Printer* printer);
  169. void GenerateConstructorBody(io::Printer* printer,
  170. std::vector<bool> already_processed,
  171. bool copy_constructor) const;
  172. size_t HasBitsSize() const;
  173. std::vector<uint32> RequiredFieldsBitMask() const;
  174. const Descriptor* descriptor_;
  175. int index_in_file_messages_;
  176. string classname_;
  177. Options options_;
  178. FieldGeneratorMap field_generators_;
  179. // optimized_order_ is the order we layout the message's fields in the class.
  180. // This is reused to initialize the fields in-order for cache efficiency.
  181. //
  182. // optimized_order_ excludes oneof fields and weak fields.
  183. std::vector<const FieldDescriptor *> optimized_order_;
  184. std::vector<int> has_bit_indices_;
  185. int max_has_bit_index_;
  186. std::unique_ptr<std::unique_ptr<EnumGenerator> []> enum_generators_;
  187. std::unique_ptr<std::unique_ptr<ExtensionGenerator> []> extension_generators_;
  188. int num_required_fields_;
  189. int num_weak_fields_;
  190. // table_driven_ indicates the generated message uses table-driven parsing.
  191. bool table_driven_;
  192. std::unique_ptr<MessageLayoutHelper> message_layout_helper_;
  193. SCCAnalyzer* scc_analyzer_;
  194. string scc_name_;
  195. friend class FileGenerator;
  196. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
  197. };
  198. } // namespace cpp
  199. } // namespace compiler
  200. } // namespace protobuf
  201. } // namespace google
  202. #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__