You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
12 KiB

6 years ago
  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. // This header is private to the ProtobolBuffers library and must NOT be
  31. // included by any sources outside this library. The contents of this file are
  32. // subject to change at any time without notice.
  33. #import "GPBDescriptor.h"
  34. #import "GPBWireFormat.h"
  35. // Describes attributes of the field.
  36. typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
  37. GPBFieldNone = 0,
  38. // These map to standard protobuf concepts.
  39. GPBFieldRequired = 1 << 0,
  40. GPBFieldRepeated = 1 << 1,
  41. GPBFieldPacked = 1 << 2,
  42. GPBFieldOptional = 1 << 3,
  43. GPBFieldHasDefaultValue = 1 << 4,
  44. // Indicates the field needs custom handling for the TextFormat name, if not
  45. // set, the name can be derived from the ObjC name.
  46. GPBFieldTextFormatNameCustom = 1 << 6,
  47. // Indicates the field has an enum descriptor.
  48. GPBFieldHasEnumDescriptor = 1 << 7,
  49. // These are not standard protobuf concepts, they are specific to the
  50. // Objective C runtime.
  51. // These bits are used to mark the field as a map and what the key
  52. // type is.
  53. GPBFieldMapKeyMask = 0xF << 8,
  54. GPBFieldMapKeyInt32 = 1 << 8,
  55. GPBFieldMapKeyInt64 = 2 << 8,
  56. GPBFieldMapKeyUInt32 = 3 << 8,
  57. GPBFieldMapKeyUInt64 = 4 << 8,
  58. GPBFieldMapKeySInt32 = 5 << 8,
  59. GPBFieldMapKeySInt64 = 6 << 8,
  60. GPBFieldMapKeyFixed32 = 7 << 8,
  61. GPBFieldMapKeyFixed64 = 8 << 8,
  62. GPBFieldMapKeySFixed32 = 9 << 8,
  63. GPBFieldMapKeySFixed64 = 10 << 8,
  64. GPBFieldMapKeyBool = 11 << 8,
  65. GPBFieldMapKeyString = 12 << 8,
  66. };
  67. // NOTE: The structures defined here have their members ordered to minimize
  68. // their size. This directly impacts the size of apps since these exist per
  69. // field/extension.
  70. // Describes a single field in a protobuf as it is represented as an ivar.
  71. typedef struct GPBMessageFieldDescription {
  72. // Name of ivar.
  73. const char *name;
  74. union {
  75. const char *className; // Name for message class.
  76. // For enums only: If EnumDescriptors are compiled in, it will be that,
  77. // otherwise it will be the verifier.
  78. GPBEnumDescriptorFunc enumDescFunc;
  79. GPBEnumValidationFunc enumVerifier;
  80. } dataTypeSpecific;
  81. // The field number for the ivar.
  82. uint32_t number;
  83. // The index (in bits) into _has_storage_.
  84. // >= 0: the bit to use for a value being set.
  85. // = GPBNoHasBit(INT32_MAX): no storage used.
  86. // < 0: in a oneOf, use a full int32 to record the field active.
  87. int32_t hasIndex;
  88. // Offset of the variable into it's structure struct.
  89. uint32_t offset;
  90. // Field flags. Use accessor functions below.
  91. GPBFieldFlags flags;
  92. // Data type of the ivar.
  93. GPBDataType dataType;
  94. } GPBMessageFieldDescription;
  95. // Fields in messages defined in a 'proto2' syntax file can provide a default
  96. // value. This struct provides the default along with the field info.
  97. typedef struct GPBMessageFieldDescriptionWithDefault {
  98. // Default value for the ivar.
  99. GPBGenericValue defaultValue;
  100. GPBMessageFieldDescription core;
  101. } GPBMessageFieldDescriptionWithDefault;
  102. // Describes attributes of the extension.
  103. typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
  104. GPBExtensionNone = 0,
  105. // These map to standard protobuf concepts.
  106. GPBExtensionRepeated = 1 << 0,
  107. GPBExtensionPacked = 1 << 1,
  108. GPBExtensionSetWireFormat = 1 << 2,
  109. };
  110. // An extension
  111. typedef struct GPBExtensionDescription {
  112. GPBGenericValue defaultValue;
  113. const char *singletonName;
  114. const char *extendedClass;
  115. const char *messageOrGroupClassName;
  116. GPBEnumDescriptorFunc enumDescriptorFunc;
  117. int32_t fieldNumber;
  118. GPBDataType dataType;
  119. GPBExtensionOptions options;
  120. } GPBExtensionDescription;
  121. typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
  122. GPBDescriptorInitializationFlag_None = 0,
  123. GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
  124. GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
  125. };
  126. @interface GPBDescriptor () {
  127. @package
  128. NSArray *fields_;
  129. NSArray *oneofs_;
  130. uint32_t storageSize_;
  131. }
  132. // fieldDescriptions have to be long lived, they are held as raw pointers.
  133. + (instancetype)
  134. allocDescriptorForClass:(Class)messageClass
  135. rootClass:(Class)rootClass
  136. file:(GPBFileDescriptor *)file
  137. fields:(void *)fieldDescriptions
  138. fieldCount:(uint32_t)fieldCount
  139. storageSize:(uint32_t)storageSize
  140. flags:(GPBDescriptorInitializationFlags)flags;
  141. - (instancetype)initWithClass:(Class)messageClass
  142. file:(GPBFileDescriptor *)file
  143. fields:(NSArray *)fields
  144. storageSize:(uint32_t)storage
  145. wireFormat:(BOOL)wireFormat;
  146. // Called right after init to provide extra information to avoid init having
  147. // an explosion of args. These pointers are recorded, so they are expected
  148. // to live for the lifetime of the app.
  149. - (void)setupOneofs:(const char **)oneofNames
  150. count:(uint32_t)count
  151. firstHasIndex:(int32_t)firstHasIndex;
  152. - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
  153. - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
  154. - (void)setupContainingMessageClassName:(const char *)msgClassName;
  155. - (void)setupMessageClassNameSuffix:(NSString *)suffix;
  156. @end
  157. @interface GPBFileDescriptor ()
  158. - (instancetype)initWithPackage:(NSString *)package
  159. objcPrefix:(NSString *)objcPrefix
  160. syntax:(GPBFileSyntax)syntax;
  161. - (instancetype)initWithPackage:(NSString *)package
  162. syntax:(GPBFileSyntax)syntax;
  163. @end
  164. @interface GPBOneofDescriptor () {
  165. @package
  166. const char *name_;
  167. NSArray *fields_;
  168. SEL caseSel_;
  169. }
  170. // name must be long lived.
  171. - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
  172. @end
  173. @interface GPBFieldDescriptor () {
  174. @package
  175. GPBMessageFieldDescription *description_;
  176. GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
  177. SEL getSel_;
  178. SEL setSel_;
  179. SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
  180. SEL setHasSel_;
  181. }
  182. // Single initializer
  183. // description has to be long lived, it is held as a raw pointer.
  184. - (instancetype)initWithFieldDescription:(void *)description
  185. includesDefault:(BOOL)includesDefault
  186. syntax:(GPBFileSyntax)syntax;
  187. @end
  188. @interface GPBEnumDescriptor ()
  189. // valueNames, values and extraTextFormatInfo have to be long lived, they are
  190. // held as raw pointers.
  191. + (instancetype)
  192. allocDescriptorForName:(NSString *)name
  193. valueNames:(const char *)valueNames
  194. values:(const int32_t *)values
  195. count:(uint32_t)valueCount
  196. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  197. + (instancetype)
  198. allocDescriptorForName:(NSString *)name
  199. valueNames:(const char *)valueNames
  200. values:(const int32_t *)values
  201. count:(uint32_t)valueCount
  202. enumVerifier:(GPBEnumValidationFunc)enumVerifier
  203. extraTextFormatInfo:(const char *)extraTextFormatInfo;
  204. - (instancetype)initWithName:(NSString *)name
  205. valueNames:(const char *)valueNames
  206. values:(const int32_t *)values
  207. count:(uint32_t)valueCount
  208. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  209. @end
  210. @interface GPBExtensionDescriptor () {
  211. @package
  212. GPBExtensionDescription *description_;
  213. }
  214. @property(nonatomic, readonly) GPBWireFormat wireType;
  215. // For repeated extensions, alternateWireType is the wireType with the opposite
  216. // value for the packable property. i.e. - if the extension was marked packed
  217. // it would be the wire type for unpacked; if the extension was marked unpacked,
  218. // it would be the wire type for packed.
  219. @property(nonatomic, readonly) GPBWireFormat alternateWireType;
  220. // description has to be long lived, it is held as a raw pointer.
  221. - (instancetype)initWithExtensionDescription:
  222. (GPBExtensionDescription *)description;
  223. - (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
  224. @end
  225. CF_EXTERN_C_BEGIN
  226. // Direct access is use for speed, to avoid even internally declaring things
  227. // read/write, etc. The warning is enabled in the project to ensure code calling
  228. // protos can turn on -Wdirect-ivar-access without issues.
  229. #pragma clang diagnostic push
  230. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  231. GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
  232. return (field->description_->flags &
  233. (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
  234. }
  235. GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
  236. return field->description_->dataType;
  237. }
  238. GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
  239. return field->description_->hasIndex;
  240. }
  241. GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
  242. return field->description_->number;
  243. }
  244. #pragma clang diagnostic pop
  245. uint32_t GPBFieldTag(GPBFieldDescriptor *self);
  246. // For repeated fields, alternateWireType is the wireType with the opposite
  247. // value for the packable property. i.e. - if the field was marked packed it
  248. // would be the wire type for unpacked; if the field was marked unpacked, it
  249. // would be the wire type for packed.
  250. uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
  251. GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
  252. return syntax == GPBFileSyntaxProto3;
  253. }
  254. GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
  255. return (description->options & GPBExtensionRepeated) != 0;
  256. }
  257. GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
  258. return (description->options & GPBExtensionPacked) != 0;
  259. }
  260. GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
  261. return (description->options & GPBExtensionSetWireFormat) != 0;
  262. }
  263. // Helper for compile time assets.
  264. #ifndef GPBInternalCompileAssert
  265. #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
  266. #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
  267. #else
  268. // Pre-Xcode 7 support.
  269. #define GPBInternalCompileAssertSymbolInner(line, msg) GPBInternalCompileAssert ## line ## __ ## msg
  270. #define GPBInternalCompileAssertSymbol(line, msg) GPBInternalCompileAssertSymbolInner(line, msg)
  271. #define GPBInternalCompileAssert(test, msg) \
  272. typedef char GPBInternalCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  273. #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
  274. #endif // GPBInternalCompileAssert
  275. // Sanity check that there isn't padding between the field description
  276. // structures with and without a default.
  277. GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
  278. (sizeof(GPBGenericValue) +
  279. sizeof(GPBMessageFieldDescription)),
  280. DescriptionsWithDefault_different_size_than_expected);
  281. CF_EXTERN_C_END