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