• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This header is logically internal, but is made public because it is used
36 // from protocol-compiler-generated code, which may reside in other components.
37 
38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
40 
41 #include <string>
42 #include <vector>
43 #include <google/protobuf/stubs/casts.h>
44 #include <google/protobuf/stubs/common.h>
45 // TODO(jasonh): Remove this once the compiler change to directly include this
46 // is released to components.
47 #include <google/protobuf/descriptor.h>
48 #include <google/protobuf/generated_enum_reflection.h>
49 #include <google/protobuf/stubs/once.h>
50 #include <google/protobuf/port.h>
51 #include <google/protobuf/unknown_field_set.h>
52 
53 
54 #include <google/protobuf/port_def.inc>
55 
56 #ifdef SWIG
57 #error "You cannot SWIG proto headers"
58 #endif
59 
60 namespace google {
61 namespace protobuf {
62 class DescriptorPool;
63 class MapKey;
64 class MapValueRef;
65 class MessageLayoutInspector;
66 class Message;
67 struct Metadata;
68 }  // namespace protobuf
69 }  // namespace google
70 
71 namespace google {
72 namespace protobuf {
73 namespace internal {
74 class DefaultEmptyOneof;
75 // Defined in other files.
76 class ExtensionSet;  // extension_set.h
77 class WeakFieldMap;  // weak_field_map.h
78 
79 // This struct describes the internal layout of the message, hence this is
80 // used to act on the message reflectively.
81 //   default_instance:  The default instance of the message.  This is only
82 //                  used to obtain pointers to default instances of embedded
83 //                  messages, which GetMessage() will return if the particular
84 //                  sub-message has not been initialized yet.  (Thus, all
85 //                  embedded message fields *must* have non-null pointers
86 //                  in the default instance.)
87 //   offsets:       An array of ints giving the byte offsets.
88 //                  For each oneof or weak field, the offset is relative to the
89 //                  default_instance. These can be computed at compile time
90 //                  using the
91 //                  PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
92 //                  macro. For each none oneof field, the offset is related to
93 //                  the start of the message object.  These can be computed at
94 //                  compile time using the
95 //                  PROTO2_GENERATED_MESSAGE_FIELD_OFFSET() macro.
96 //                  Besides offsets for all fields, this array also contains
97 //                  offsets for oneof unions. The offset of the i-th oneof union
98 //                  is offsets[descriptor->field_count() + i].
99 //   has_bit_indices:  Mapping from field indexes to their index in the has
100 //                  bit array.
101 //   has_bits_offset:  Offset in the message of an array of uint32s of size
102 //                  descriptor->field_count()/32, rounded up.  This is a
103 //                  bitfield where each bit indicates whether or not the
104 //                  corresponding field of the message has been initialized.
105 //                  The bit for field index i is obtained by the expression:
106 //                    has_bits[i / 32] & (1 << (i % 32))
107 //   unknown_fields_offset:  Offset in the message of the UnknownFieldSet for
108 //                  the message.
109 //   extensions_offset:  Offset in the message of the ExtensionSet for the
110 //                  message, or -1 if the message type has no extension
111 //                  ranges.
112 //   oneof_case_offset:  Offset in the message of an array of uint32s of
113 //                  size descriptor->oneof_decl_count().  Each uint32
114 //                  indicates what field is set for each oneof.
115 //   object_size:   The size of a message object of this type, as measured
116 //                  by sizeof().
117 //   arena_offset:  If a message doesn't have a unknown_field_set that stores
118 //                  the arena, it must have a direct pointer to the arena.
119 //   weak_field_map_offset: If the message proto has weak fields, this is the
120 //                  offset of _weak_field_map_ in the generated proto. Otherwise
121 //                  -1.
122 struct ReflectionSchema {
123  public:
124   // Size of a google::protobuf::Message object of this type.
GetObjectSizeReflectionSchema125   uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }
126 
InRealOneofReflectionSchema127   bool InRealOneof(const FieldDescriptor* field) const {
128     return field->containing_oneof() &&
129            !field->containing_oneof()->is_synthetic();
130   }
131 
132   // Offset of a non-oneof field.  Getting a field offset is slightly more
133   // efficient when we know statically that it is not a oneof field.
GetFieldOffsetNonOneofReflectionSchema134   uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const {
135     GOOGLE_DCHECK(!InRealOneof(field));
136     return OffsetValue(offsets_[field->index()], field->type());
137   }
138 
139   // Offset of any field.
GetFieldOffsetReflectionSchema140   uint32 GetFieldOffset(const FieldDescriptor* field) const {
141     if (InRealOneof(field)) {
142       size_t offset =
143           static_cast<size_t>(field->containing_type()->field_count() +
144                               field->containing_oneof()->index());
145       return OffsetValue(offsets_[offset], field->type());
146     } else {
147       return GetFieldOffsetNonOneof(field);
148     }
149   }
150 
IsFieldInlinedReflectionSchema151   bool IsFieldInlined(const FieldDescriptor* field) const {
152     if (InRealOneof(field)) {
153       size_t offset =
154           static_cast<size_t>(field->containing_type()->field_count() +
155                               field->containing_oneof()->index());
156       return Inlined(offsets_[offset], field->type());
157     } else {
158       return Inlined(offsets_[field->index()], field->type());
159     }
160   }
161 
GetOneofCaseOffsetReflectionSchema162   uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
163     return static_cast<uint32>(oneof_case_offset_) +
164            static_cast<uint32>(static_cast<size_t>(oneof_descriptor->index()) *
165                                sizeof(uint32));
166   }
167 
HasHasbitsReflectionSchema168   bool HasHasbits() const { return has_bits_offset_ != -1; }
169 
170   // Bit index within the bit array of hasbits.  Bit order is low-to-high.
HasBitIndexReflectionSchema171   uint32 HasBitIndex(const FieldDescriptor* field) const {
172     if (has_bits_offset_ == -1) return static_cast<uint32>(-1);
173     GOOGLE_DCHECK(HasHasbits());
174     return has_bit_indices_[field->index()];
175   }
176 
177   // Byte offset of the hasbits array.
HasBitsOffsetReflectionSchema178   uint32 HasBitsOffset() const {
179     GOOGLE_DCHECK(HasHasbits());
180     return static_cast<uint32>(has_bits_offset_);
181   }
182 
183   // The offset of the InternalMetadataWithArena member.
184   // For Lite this will actually be an InternalMetadataWithArenaLite.
185   // The schema doesn't contain enough information to distinguish between
186   // these two cases.
GetMetadataOffsetReflectionSchema187   uint32 GetMetadataOffset() const {
188     return static_cast<uint32>(metadata_offset_);
189   }
190 
191   // Whether this message has an ExtensionSet.
HasExtensionSetReflectionSchema192   bool HasExtensionSet() const { return extensions_offset_ != -1; }
193 
194   // The offset of the ExtensionSet in this message.
GetExtensionSetOffsetReflectionSchema195   uint32 GetExtensionSetOffset() const {
196     GOOGLE_DCHECK(HasExtensionSet());
197     return static_cast<uint32>(extensions_offset_);
198   }
199 
200   // The off set of WeakFieldMap when the message contains weak fields.
201   // The default is 0 for now.
GetWeakFieldMapOffsetReflectionSchema202   int GetWeakFieldMapOffset() const { return weak_field_map_offset_; }
203 
IsDefaultInstanceReflectionSchema204   bool IsDefaultInstance(const Message& message) const {
205     return &message == default_instance_;
206   }
207 
208   // Returns a pointer to the default value for this field.  The size and type
209   // of the underlying data depends on the field's type.
GetFieldDefaultReflectionSchema210   const void* GetFieldDefault(const FieldDescriptor* field) const {
211     return reinterpret_cast<const uint8*>(default_instance_) +
212            OffsetValue(offsets_[field->index()], field->type());
213   }
214 
IsFieldStrippedReflectionSchema215   bool IsFieldStripped(const FieldDescriptor* field) const {
216     return false;
217   }
218 
IsMessageStrippedReflectionSchema219   bool IsMessageStripped(const Descriptor* descriptor) const {
220     return false;
221   }
222 
223 
HasWeakFieldsReflectionSchema224   bool HasWeakFields() const { return weak_field_map_offset_ > 0; }
225 
226   // These members are intended to be private, but we cannot actually make them
227   // private because this prevents us from using aggregate initialization of
228   // them, ie.
229   //
230   //   ReflectionSchema schema = {a, b, c, d, e, ...};
231   // private:
232   const Message* default_instance_;
233   const uint32* offsets_;
234   const uint32* has_bit_indices_;
235   int has_bits_offset_;
236   int metadata_offset_;
237   int extensions_offset_;
238   int oneof_case_offset_;
239   int object_size_;
240   int weak_field_map_offset_;
241 
242   // We tag offset values to provide additional data about fields (such as
243   // inlined).
OffsetValueReflectionSchema244   static uint32 OffsetValue(uint32 v, FieldDescriptor::Type type) {
245     if (type == FieldDescriptor::TYPE_STRING ||
246         type == FieldDescriptor::TYPE_BYTES) {
247       return v & ~1u;
248     } else {
249       return v;
250     }
251   }
252 
InlinedReflectionSchema253   static bool Inlined(uint32 v, FieldDescriptor::Type type) {
254     if (type == FieldDescriptor::TYPE_STRING ||
255         type == FieldDescriptor::TYPE_BYTES) {
256       return v & 1u;
257     } else {
258       // Non string/byte fields are not inlined.
259       return false;
260     }
261   }
262 };
263 
264 // Structs that the code generator emits directly to describe a message.
265 // These should never used directly except to build a ReflectionSchema
266 // object.
267 //
268 // EXPERIMENTAL: these are changing rapidly, and may completely disappear
269 // or merge with ReflectionSchema.
270 struct MigrationSchema {
271   int32 offsets_index;
272   int32 has_bit_indices_index;
273   int object_size;
274 };
275 
276 struct SCCInfoBase;
277 
278 struct PROTOBUF_EXPORT DescriptorTable {
279   mutable bool is_initialized;
280   bool is_eager;
281   const char* descriptor;
282   const char* filename;
283   int size;  // of serialized descriptor
284   once_flag* once;
285   SCCInfoBase* const* init_default_instances;
286   const DescriptorTable* const* deps;
287   int num_sccs;
288   int num_deps;
289   const MigrationSchema* schemas;
290   const Message* const* default_instances;
291   const uint32* offsets;
292   // update the following descriptor arrays.
293   Metadata* file_level_metadata;
294   int num_messages;
295   const EnumDescriptor** file_level_enum_descriptors;
296   const ServiceDescriptor** file_level_service_descriptors;
297 };
298 
299 // AssignDescriptors() pulls the compiled FileDescriptor from the DescriptorPool
300 // and uses it to populate all of the global variables which store pointers to
301 // the descriptor objects.  It also constructs the reflection objects.  It is
302 // called the first time anyone calls descriptor() or GetReflection() on one of
303 // the types defined in the file.  AssignDescriptors() is thread-safe.
304 void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table,
305                                        bool eager = false);
306 
307 // AddDescriptors() is a file-level procedure which adds the encoded
308 // FileDescriptorProto for this .proto file to the global DescriptorPool for
309 // generated files (DescriptorPool::generated_pool()). It ordinarily runs at
310 // static initialization time, but is not used at all in LITE_RUNTIME mode.
311 // AddDescriptors() is *not* thread-safe.
312 void PROTOBUF_EXPORT AddDescriptors(const DescriptorTable* table);
313 
314 // These cannot be in lite so we put them in the reflection.
315 PROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset,
316                                                uint32 tag, uint32 has_offset,
317                                                io::CodedOutputStream* output);
318 
319 }  // namespace internal
320 }  // namespace protobuf
321 }  // namespace google
322 
323 #include <google/protobuf/port_undef.inc>
324 
325 #endif  // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
326