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