generated_message_reflection.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. //
  34. // This header is logically internal, but is made public because it is used
  35. // from protocol-compiler-generated code, which may reside in other components.
  36. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
  37. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
  38. #include <string>
  39. #include <vector>
  40. #include <google/protobuf/stubs/casts.h>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/generated_enum_reflection.h>
  44. #include <google/protobuf/stubs/once.h>
  45. #include <google/protobuf/port.h>
  46. #include <google/protobuf/unknown_field_set.h>
  47. #include <google/protobuf/port_def.inc>
  48. #ifdef SWIG
  49. #error "You cannot SWIG proto headers"
  50. #endif
  51. namespace google {
  52. namespace protobuf {
  53. class MapKey;
  54. class MapValueRef;
  55. class MessageLayoutInspector;
  56. class Message;
  57. struct Metadata;
  58. } // namespace protobuf
  59. } // namespace google
  60. namespace google {
  61. namespace protobuf {
  62. namespace internal {
  63. class DefaultEmptyOneof;
  64. // Defined in other files.
  65. class ExtensionSet; // extension_set.h
  66. class WeakFieldMap; // weak_field_map.h
  67. // This struct describes the internal layout of the message, hence this is
  68. // used to act on the message reflectively.
  69. // default_instance: The default instance of the message. This is only
  70. // used to obtain pointers to default instances of embedded
  71. // messages, which GetMessage() will return if the particular
  72. // sub-message has not been initialized yet. (Thus, all
  73. // embedded message fields *must* have non-null pointers
  74. // in the default instance.)
  75. // offsets: An array of ints giving the byte offsets.
  76. // For each oneof or weak field, the offset is relative to the
  77. // default_instance. These can be computed at compile time
  78. // using the
  79. // PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
  80. // macro. For each none oneof field, the offset is related to
  81. // the start of the message object. These can be computed at
  82. // compile time using the
  83. // PROTO2_GENERATED_MESSAGE_FIELD_OFFSET() macro.
  84. // Besides offsets for all fields, this array also contains
  85. // offsets for oneof unions. The offset of the i-th oneof union
  86. // is offsets[descriptor->field_count() + i].
  87. // has_bit_indices: Mapping from field indexes to their index in the has
  88. // bit array.
  89. // has_bits_offset: Offset in the message of an array of uint32s of size
  90. // descriptor->field_count()/32, rounded up. This is a
  91. // bitfield where each bit indicates whether or not the
  92. // corresponding field of the message has been initialized.
  93. // The bit for field index i is obtained by the expression:
  94. // has_bits[i / 32] & (1 << (i % 32))
  95. // unknown_fields_offset: Offset in the message of the UnknownFieldSet for
  96. // the message.
  97. // extensions_offset: Offset in the message of the ExtensionSet for the
  98. // message, or -1 if the message type has no extension
  99. // ranges.
  100. // oneof_case_offset: Offset in the message of an array of uint32s of
  101. // size descriptor->oneof_decl_count(). Each uint32
  102. // indicates what field is set for each oneof.
  103. // object_size: The size of a message object of this type, as measured
  104. // by sizeof().
  105. // arena_offset: If a message doesn't have a unknown_field_set that stores
  106. // the arena, it must have a direct pointer to the arena.
  107. // weak_field_map_offset: If the message proto has weak fields, this is the
  108. // offset of _weak_field_map_ in the generated proto. Otherwise
  109. // -1.
  110. struct ReflectionSchema {
  111. public:
  112. // Size of a google::protobuf::Message object of this type.
  113. uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }
  114. bool InRealOneof(const FieldDescriptor* field) const {
  115. return field->containing_oneof() &&
  116. !field->containing_oneof()->is_synthetic();
  117. }
  118. // Offset of a non-oneof field. Getting a field offset is slightly more
  119. // efficient when we know statically that it is not a oneof field.
  120. uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const {
  121. GOOGLE_DCHECK(!InRealOneof(field));
  122. return OffsetValue(offsets_[field->index()], field->type());
  123. }
  124. // Offset of any field.
  125. uint32 GetFieldOffset(const FieldDescriptor* field) const {
  126. if (InRealOneof(field)) {
  127. size_t offset =
  128. static_cast<size_t>(field->containing_type()->field_count() +
  129. field->containing_oneof()->index());
  130. return OffsetValue(offsets_[offset], field->type());
  131. } else {
  132. return GetFieldOffsetNonOneof(field);
  133. }
  134. }
  135. uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
  136. return static_cast<uint32>(oneof_case_offset_) +
  137. static_cast<uint32>(static_cast<size_t>(oneof_descriptor->index()) *
  138. sizeof(uint32));
  139. }
  140. bool HasHasbits() const { return has_bits_offset_ != -1; }
  141. // Bit index within the bit array of hasbits. Bit order is low-to-high.
  142. uint32 HasBitIndex(const FieldDescriptor* field) const {
  143. if (has_bits_offset_ == -1) return static_cast<uint32>(-1);
  144. GOOGLE_DCHECK(HasHasbits());
  145. return has_bit_indices_[field->index()];
  146. }
  147. // Byte offset of the hasbits array.
  148. uint32 HasBitsOffset() const {
  149. GOOGLE_DCHECK(HasHasbits());
  150. return static_cast<uint32>(has_bits_offset_);
  151. }
  152. // The offset of the InternalMetadataWithArena member.
  153. // For Lite this will actually be an InternalMetadataWithArenaLite.
  154. // The schema doesn't contain enough information to distinguish between
  155. // these two cases.
  156. uint32 GetMetadataOffset() const {
  157. return static_cast<uint32>(metadata_offset_);
  158. }
  159. // Whether this message has an ExtensionSet.
  160. bool HasExtensionSet() const { return extensions_offset_ != -1; }
  161. // The offset of the ExtensionSet in this message.
  162. uint32 GetExtensionSetOffset() const {
  163. GOOGLE_DCHECK(HasExtensionSet());
  164. return static_cast<uint32>(extensions_offset_);
  165. }
  166. // The off set of WeakFieldMap when the message contains weak fields.
  167. // The default is 0 for now.
  168. int GetWeakFieldMapOffset() const { return weak_field_map_offset_; }
  169. bool IsDefaultInstance(const Message& message) const {
  170. return &message == default_instance_;
  171. }
  172. // Returns a pointer to the default value for this field. The size and type
  173. // of the underlying data depends on the field's type.
  174. const void* GetFieldDefault(const FieldDescriptor* field) const {
  175. return reinterpret_cast<const uint8*>(default_instance_) +
  176. OffsetValue(offsets_[field->index()], field->type());
  177. }
  178. // Returns true if the field is implicitly backed by LazyField.
  179. bool IsEagerlyVerifiedLazyField(const FieldDescriptor* field) const {
  180. GOOGLE_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_MESSAGE);
  181. (void)field;
  182. return false;
  183. }
  184. // Returns true if the field's accessor is called by any external code (aka,
  185. // non proto library code).
  186. bool IsFieldUsed(const FieldDescriptor* field) const {
  187. (void)field;
  188. return true;
  189. }
  190. bool IsFieldStripped(const FieldDescriptor* field) const {
  191. (void)field;
  192. return false;
  193. }
  194. bool IsMessageStripped(const Descriptor* descriptor) const {
  195. (void)descriptor;
  196. return false;
  197. }
  198. bool HasWeakFields() const { return weak_field_map_offset_ > 0; }
  199. // These members are intended to be private, but we cannot actually make them
  200. // private because this prevents us from using aggregate initialization of
  201. // them, ie.
  202. //
  203. // ReflectionSchema schema = {a, b, c, d, e, ...};
  204. // private:
  205. const Message* default_instance_;
  206. const uint32* offsets_;
  207. const uint32* has_bit_indices_;
  208. int has_bits_offset_;
  209. int metadata_offset_;
  210. int extensions_offset_;
  211. int oneof_case_offset_;
  212. int object_size_;
  213. int weak_field_map_offset_;
  214. // We tag offset values to provide additional data about fields (such as
  215. // "unused" or "lazy").
  216. static uint32 OffsetValue(uint32 v, FieldDescriptor::Type type) {
  217. if (type == FieldDescriptor::TYPE_MESSAGE) {
  218. return v & 0x7FFFFFFEu;
  219. }
  220. return v & 0x7FFFFFFFu;
  221. }
  222. };
  223. // Structs that the code generator emits directly to describe a message.
  224. // These should never used directly except to build a ReflectionSchema
  225. // object.
  226. //
  227. // EXPERIMENTAL: these are changing rapidly, and may completely disappear
  228. // or merge with ReflectionSchema.
  229. struct MigrationSchema {
  230. int32 offsets_index;
  231. int32 has_bit_indices_index;
  232. int object_size;
  233. };
  234. // This struct tries to reduce unnecessary padding.
  235. // The num_xxx might not be close to their respective pointer, but this saves
  236. // padding.
  237. struct PROTOBUF_EXPORT DescriptorTable {
  238. mutable bool is_initialized;
  239. bool is_eager;
  240. int size; // of serialized descriptor
  241. const char* descriptor;
  242. const char* filename;
  243. once_flag* once;
  244. const DescriptorTable* const* deps;
  245. int num_deps;
  246. int num_messages;
  247. const MigrationSchema* schemas;
  248. const Message* const* default_instances;
  249. const uint32* offsets;
  250. // update the following descriptor arrays.
  251. Metadata* file_level_metadata;
  252. const EnumDescriptor** file_level_enum_descriptors;
  253. const ServiceDescriptor** file_level_service_descriptors;
  254. };
  255. enum {
  256. // Tag used on offsets for fields that don't have a real offset.
  257. // For example, weak message fields go into the WeakFieldMap and not in an
  258. // actual field.
  259. kInvalidFieldOffsetTag = 0x40000000u,
  260. };
  261. // AssignDescriptors() pulls the compiled FileDescriptor from the DescriptorPool
  262. // and uses it to populate all of the global variables which store pointers to
  263. // the descriptor objects. It also constructs the reflection objects. It is
  264. // called the first time anyone calls descriptor() or GetReflection() on one of
  265. // the types defined in the file. AssignDescriptors() is thread-safe.
  266. void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table,
  267. bool eager = false);
  268. // Overload used to implement GetMetadataStatic in the generated code.
  269. // See comments in compiler/cpp/internal/file.cc as to why.
  270. // It takes a `Metadata` and returns it to allow for tail calls and reduce
  271. // binary size.
  272. Metadata PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* (*table)(),
  273. internal::once_flag* once,
  274. const Metadata& metadata);
  275. // These cannot be in lite so we put them in the reflection.
  276. PROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset,
  277. uint32 tag, uint32 has_offset,
  278. io::CodedOutputStream* output);
  279. struct PROTOBUF_EXPORT AddDescriptorsRunner {
  280. explicit AddDescriptorsRunner(const DescriptorTable* table);
  281. };
  282. } // namespace internal
  283. } // namespace protobuf
  284. } // namespace google
  285. #include <google/protobuf/port_undef.inc>
  286. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__