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's accessor is called by any external code (aka, 202 // non proto library code). IsFieldUsedReflectionSchema203 bool IsFieldUsed(const FieldDescriptor* field) const { 204 (void)field; 205 return true; 206 } 207 IsFieldStrippedReflectionSchema208 bool IsFieldStripped(const FieldDescriptor* field) const { 209 (void)field; 210 return false; 211 } 212 IsMessageStrippedReflectionSchema213 bool IsMessageStripped(const Descriptor* descriptor) const { 214 (void)descriptor; 215 return false; 216 } 217 218 HasWeakFieldsReflectionSchema219 bool HasWeakFields() const { return weak_field_map_offset_ > 0; } 220 221 // These members are intended to be private, but we cannot actually make them 222 // private because this prevents us from using aggregate initialization of 223 // them, ie. 224 // 225 // ReflectionSchema schema = {a, b, c, d, e, ...}; 226 // private: 227 const Message* default_instance_; 228 const uint32* offsets_; 229 const uint32* has_bit_indices_; 230 int has_bits_offset_; 231 int metadata_offset_; 232 int extensions_offset_; 233 int oneof_case_offset_; 234 int object_size_; 235 int weak_field_map_offset_; 236 237 // We tag offset values to provide additional data about fields (such as 238 // "unused"). OffsetValueReflectionSchema239 static uint32 OffsetValue(uint32 v, FieldDescriptor::Type type) { 240 return v & 0x7FFFFFFFu; 241 } 242 }; 243 244 // Structs that the code generator emits directly to describe a message. 245 // These should never used directly except to build a ReflectionSchema 246 // object. 247 // 248 // EXPERIMENTAL: these are changing rapidly, and may completely disappear 249 // or merge with ReflectionSchema. 250 struct MigrationSchema { 251 int32 offsets_index; 252 int32 has_bit_indices_index; 253 int object_size; 254 }; 255 256 struct SCCInfoBase; 257 258 struct PROTOBUF_EXPORT DescriptorTable { 259 mutable bool is_initialized; 260 bool is_eager; 261 const char* descriptor; 262 const char* filename; 263 int size; // of serialized descriptor 264 once_flag* once; 265 SCCInfoBase* const* init_default_instances; 266 const DescriptorTable* const* deps; 267 int num_sccs; 268 int num_deps; 269 const MigrationSchema* schemas; 270 const Message* const* default_instances; 271 const uint32* offsets; 272 // update the following descriptor arrays. 273 Metadata* file_level_metadata; 274 int num_messages; 275 const EnumDescriptor** file_level_enum_descriptors; 276 const ServiceDescriptor** file_level_service_descriptors; 277 }; 278 279 enum { 280 // Tag used on offsets for fields that don't have a real offset. 281 // For example, weak message fields go into the WeakFieldMap and not in an 282 // actual field. 283 kInvalidFieldOffsetTag = 0x40000000u, 284 }; 285 286 // AssignDescriptors() pulls the compiled FileDescriptor from the DescriptorPool 287 // and uses it to populate all of the global variables which store pointers to 288 // the descriptor objects. It also constructs the reflection objects. It is 289 // called the first time anyone calls descriptor() or GetReflection() on one of 290 // the types defined in the file. AssignDescriptors() is thread-safe. 291 void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table, 292 bool eager = false); 293 294 // AddDescriptors() is a file-level procedure which adds the encoded 295 // FileDescriptorProto for this .proto file to the global DescriptorPool for 296 // generated files (DescriptorPool::generated_pool()). It ordinarily runs at 297 // static initialization time, but is not used at all in LITE_RUNTIME mode. 298 // AddDescriptors() is *not* thread-safe. 299 void PROTOBUF_EXPORT AddDescriptors(const DescriptorTable* table); 300 301 // These cannot be in lite so we put them in the reflection. 302 PROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, 303 uint32 tag, uint32 has_offset, 304 io::CodedOutputStream* output); 305 306 } // namespace internal 307 } // namespace protobuf 308 } // namespace google 309 310 #include <google/protobuf/port_undef.inc> 311 312 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ 313