Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 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 | // This header is private to the ProtobolBuffers library and must NOT be |
| 32 | // included by any sources outside this library. The contents of this file are |
| 33 | // subject to change at any time without notice. |
| 34 | |
| 35 | #import "GPBDescriptor.h" |
| 36 | |
| 37 | // Describes attributes of the field. |
| 38 | typedef NS_OPTIONS(uint32_t, GPBFieldFlags) { |
| 39 | // These map to standard protobuf concepts. |
| 40 | GPBFieldRequired = 1 << 0, |
| 41 | GPBFieldRepeated = 1 << 1, |
| 42 | GPBFieldPacked = 1 << 2, |
| 43 | GPBFieldOptional = 1 << 3, |
| 44 | GPBFieldHasDefaultValue = 1 << 4, |
| 45 | |
| 46 | // These are not standard protobuf concepts, they are specific to the |
| 47 | // Objective C runtime. |
| 48 | |
| 49 | // These bits are used to mark the field as a map and what the key |
| 50 | // type is. |
| 51 | GPBFieldMapKeyMask = 0xF << 8, |
| 52 | GPBFieldMapKeyInt32 = 1 << 8, |
| 53 | GPBFieldMapKeyInt64 = 2 << 8, |
| 54 | GPBFieldMapKeyUInt32 = 3 << 8, |
| 55 | GPBFieldMapKeyUInt64 = 4 << 8, |
| 56 | GPBFieldMapKeySInt32 = 5 << 8, |
| 57 | GPBFieldMapKeySInt64 = 6 << 8, |
| 58 | GPBFieldMapKeyFixed32 = 7 << 8, |
| 59 | GPBFieldMapKeyFixed64 = 8 << 8, |
| 60 | GPBFieldMapKeySFixed32 = 9 << 8, |
| 61 | GPBFieldMapKeySFixed64 = 10 << 8, |
| 62 | GPBFieldMapKeyBool = 11 << 8, |
| 63 | GPBFieldMapKeyString = 12 << 8, |
| 64 | |
| 65 | // Indicates the field needs custom handling for the TextFormat name, if not |
| 66 | // set, the name can be derived from the ObjC name. |
| 67 | GPBFieldTextFormatNameCustom = 1 << 16, |
| 68 | // Indicates the field has an enum descriptor. |
| 69 | // TODO(thomasvl): Output the CPP check to use descFunc or validator based |
| 70 | // on final compile. This will then get added based on that. |
| 71 | GPBFieldHasEnumDescriptor = 1 << 17, |
| 72 | }; |
| 73 | |
| 74 | // Describes a single field in a protobuf as it is represented as an ivar. |
| 75 | typedef struct GPBMessageFieldDescription { |
| 76 | // Name of ivar. |
| 77 | const char *name; |
| 78 | // The field number for the ivar. |
| 79 | uint32_t number; |
| 80 | // The index (in bits) into _has_storage_. |
| 81 | // > 0: the bit to use for a value being set. |
| 82 | // = 0: no storage used. |
| 83 | // < 0: in a oneOf, use a full int32 to record the field active. |
| 84 | int32_t hasIndex; |
| 85 | // Field flags. Use accessor functions below. |
| 86 | GPBFieldFlags flags; |
| 87 | // Type of the ivar. |
| 88 | GPBType type; |
| 89 | // Offset of the variable into it's structure struct. |
| 90 | size_t offset; |
| 91 | // FieldOptions protobuf, serialized as string. |
| 92 | const char *fieldOptions; |
| 93 | |
| 94 | GPBValue defaultValue; // Default value for the ivar. |
| 95 | union { |
| 96 | const char *className; // Name for message class. |
| 97 | // For enums only: If EnumDescriptors are compiled in, it will be that, |
| 98 | // otherwise it will be the verifier. |
| 99 | GPBEnumDescriptorFunc enumDescFunc; |
| 100 | GPBEnumValidationFunc enumVerifier; |
| 101 | } typeSpecific; |
| 102 | } GPBMessageFieldDescription; |
| 103 | |
| 104 | // Describes a oneof. |
| 105 | typedef struct GPBMessageOneofDescription { |
| 106 | // Name of this enum oneof. |
| 107 | const char *name; |
| 108 | // The index of this oneof in the has_storage. |
| 109 | int32_t index; |
| 110 | } GPBMessageOneofDescription; |
| 111 | |
| 112 | // Describes an enum type defined in a .proto file. |
| 113 | typedef struct GPBMessageEnumDescription { |
| 114 | GPBEnumDescriptorFunc enumDescriptorFunc; |
| 115 | } GPBMessageEnumDescription; |
| 116 | |
| 117 | // Describes an individual enum constant of a particular type. |
| 118 | typedef struct GPBMessageEnumValueDescription { |
| 119 | // Name of this enum constant. |
| 120 | const char *name; |
| 121 | // Numeric value of this enum constant. |
| 122 | int32_t number; |
| 123 | } GPBMessageEnumValueDescription; |
| 124 | |
| 125 | // Describes attributes of the extension. |
| 126 | typedef NS_OPTIONS(uint32_t, GPBExtensionOptions) { |
| 127 | // These map to standard protobuf concepts. |
| 128 | GPBExtensionRepeated = 1 << 0, |
| 129 | GPBExtensionPacked = 1 << 1, |
| 130 | GPBExtensionSetWireFormat = 1 << 2, |
| 131 | }; |
| 132 | |
| 133 | // An extension |
| 134 | typedef struct GPBExtensionDescription { |
| 135 | const char *singletonName; |
| 136 | GPBType type; |
| 137 | const char *extendedClass; |
| 138 | int32_t fieldNumber; |
| 139 | GPBValue defaultValue; |
| 140 | const char *messageOrGroupClassName; |
| 141 | GPBExtensionOptions options; |
| 142 | GPBEnumDescriptorFunc enumDescriptorFunc; |
| 143 | } GPBExtensionDescription; |
| 144 | |
| 145 | @interface GPBDescriptor () { |
| 146 | @package |
| 147 | NSArray *fields_; |
| 148 | NSArray *oneofs_; |
| 149 | size_t storageSize_; |
| 150 | } |
| 151 | |
| 152 | // fieldDescriptions, enumDescriptions, rangeDescriptions, and |
| 153 | // extraTextFormatInfo have to be long lived, they are held as raw pointers. |
| 154 | + (instancetype) |
| 155 | allocDescriptorForClass:(Class)messageClass |
| 156 | rootClass:(Class)rootClass |
| 157 | file:(GPBFileDescriptor *)file |
| 158 | fields:(GPBMessageFieldDescription *)fieldDescriptions |
| 159 | fieldCount:(NSUInteger)fieldCount |
| 160 | oneofs:(GPBMessageOneofDescription *)oneofDescriptions |
| 161 | oneofCount:(NSUInteger)oneofCount |
| 162 | enums:(GPBMessageEnumDescription *)enumDescriptions |
| 163 | enumCount:(NSUInteger)enumCount |
| 164 | ranges:(const GPBExtensionRange *)ranges |
| 165 | rangeCount:(NSUInteger)rangeCount |
| 166 | storageSize:(size_t)storageSize |
| 167 | wireFormat:(BOOL)wireFormat; |
| 168 | + (instancetype) |
| 169 | allocDescriptorForClass:(Class)messageClass |
| 170 | rootClass:(Class)rootClass |
| 171 | file:(GPBFileDescriptor *)file |
| 172 | fields:(GPBMessageFieldDescription *)fieldDescriptions |
| 173 | fieldCount:(NSUInteger)fieldCount |
| 174 | oneofs:(GPBMessageOneofDescription *)oneofDescriptions |
| 175 | oneofCount:(NSUInteger)oneofCount |
| 176 | enums:(GPBMessageEnumDescription *)enumDescriptions |
| 177 | enumCount:(NSUInteger)enumCount |
| 178 | ranges:(const GPBExtensionRange *)ranges |
| 179 | rangeCount:(NSUInteger)rangeCount |
| 180 | storageSize:(size_t)storageSize |
| 181 | wireFormat:(BOOL)wireFormat |
| 182 | extraTextFormatInfo:(const char *)extraTextFormatInfo; |
| 183 | |
| 184 | - (instancetype)initWithClass:(Class)messageClass |
| 185 | file:(GPBFileDescriptor *)file |
| 186 | fields:(NSArray *)fields |
| 187 | oneofs:(NSArray *)oneofs |
| 188 | enums:(NSArray *)enums |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 189 | extensionRanges:(const GPBExtensionRange *)ranges |
| 190 | extensionRangesCount:(NSUInteger)rangeCount |
| 191 | storageSize:(size_t)storage |
| 192 | wireFormat:(BOOL)wireFormat; |
| 193 | |
| 194 | @end |
| 195 | |
| 196 | @interface GPBFileDescriptor () |
| 197 | - (instancetype)initWithPackage:(NSString *)package |
| 198 | syntax:(GPBFileSyntax)syntax; |
| 199 | @end |
| 200 | |
| 201 | @interface GPBOneofDescriptor () { |
| 202 | @package |
| 203 | GPBMessageOneofDescription *oneofDescription_; |
| 204 | NSArray *fields_; |
| 205 | |
| 206 | SEL caseSel_; |
| 207 | } |
| 208 | - (instancetype)initWithOneofDescription: |
| 209 | (GPBMessageOneofDescription *)oneofDescription |
| 210 | fields:(NSArray *)fields; |
| 211 | @end |
| 212 | |
| 213 | @interface GPBFieldDescriptor () { |
| 214 | @package |
| 215 | GPBMessageFieldDescription *description_; |
| 216 | GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_; |
| 217 | |
| 218 | SEL getSel_; |
| 219 | SEL setSel_; |
| 220 | SEL hasSel_; |
| 221 | SEL setHasSel_; |
| 222 | } |
| 223 | |
| 224 | // Single initializer |
| 225 | // description has to be long lived, it is held as a raw pointer. |
| 226 | - (instancetype)initWithFieldDescription: |
| 227 | (GPBMessageFieldDescription *)description |
| 228 | rootClass:(Class)rootClass |
| 229 | syntax:(GPBFileSyntax)syntax; |
| 230 | @end |
| 231 | |
| 232 | @interface GPBEnumDescriptor () |
| 233 | // valueDescriptions and extraTextFormatInfo have to be long lived, they are |
| 234 | // held as raw pointers. |
| 235 | + (instancetype) |
| 236 | allocDescriptorForName:(NSString *)name |
| 237 | values:(GPBMessageEnumValueDescription *)valueDescriptions |
| 238 | valueCount:(NSUInteger)valueCount |
| 239 | enumVerifier:(GPBEnumValidationFunc)enumVerifier; |
| 240 | + (instancetype) |
| 241 | allocDescriptorForName:(NSString *)name |
| 242 | values:(GPBMessageEnumValueDescription *)valueDescriptions |
| 243 | valueCount:(NSUInteger)valueCount |
| 244 | enumVerifier:(GPBEnumValidationFunc)enumVerifier |
| 245 | extraTextFormatInfo:(const char *)extraTextFormatInfo; |
| 246 | |
| 247 | - (instancetype)initWithName:(NSString *)name |
| 248 | values:(GPBMessageEnumValueDescription *)valueDescriptions |
| 249 | valueCount:(NSUInteger)valueCount |
| 250 | enumVerifier:(GPBEnumValidationFunc)enumVerifier; |
| 251 | @end |
| 252 | |
| 253 | @interface GPBExtensionDescriptor () { |
| 254 | @package |
| 255 | GPBExtensionDescription *description_; |
| 256 | } |
| 257 | |
| 258 | // description has to be long lived, it is held as a raw pointer. |
| 259 | - (instancetype)initWithExtensionDescription: |
| 260 | (GPBExtensionDescription *)description; |
| 261 | @end |
| 262 | |
| 263 | CF_EXTERN_C_BEGIN |
| 264 | |
| 265 | GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) { |
| 266 | return (field->description_->flags & |
| 267 | (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0; |
| 268 | } |
| 269 | |
| 270 | GPB_INLINE GPBType GPBGetFieldType(GPBFieldDescriptor *field) { |
| 271 | return field->description_->type; |
| 272 | } |
| 273 | |
| 274 | GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) { |
| 275 | return field->description_->hasIndex; |
| 276 | } |
| 277 | |
| 278 | GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) { |
| 279 | return field->description_->number; |
| 280 | } |
| 281 | |
| 282 | uint32_t GPBFieldTag(GPBFieldDescriptor *self); |
| 283 | |
| 284 | GPB_INLINE BOOL GPBPreserveUnknownFields(GPBFileSyntax syntax) { |
| 285 | return syntax != GPBFileSyntaxProto3; |
| 286 | } |
| 287 | |
| 288 | GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) { |
| 289 | return syntax == GPBFileSyntaxProto3; |
| 290 | } |
| 291 | |
| 292 | CF_EXTERN_C_END |