GPBDescriptor_PackagePrivate.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // className is deprecated and will be removed in favor of clazz.
  76. // kept around right now for backwards compatibility.
  77. // clazz is used iff GPBDescriptorInitializationFlag_UsesClassRefs is set.
  78. char *className; // Name of the class of the message.
  79. Class clazz; // Class of the message.
  80. // For enums only: If EnumDescriptors are compiled in, it will be that,
  81. // otherwise it will be the verifier.
  82. GPBEnumDescriptorFunc enumDescFunc;
  83. GPBEnumValidationFunc enumVerifier;
  84. } dataTypeSpecific;
  85. // The field number for the ivar.
  86. uint32_t number;
  87. // The index (in bits) into _has_storage_.
  88. // >= 0: the bit to use for a value being set.
  89. // = GPBNoHasBit(INT32_MAX): no storage used.
  90. // < 0: in a oneOf, use a full int32 to record the field active.
  91. int32_t hasIndex;
  92. // Offset of the variable into it's structure struct.
  93. uint32_t offset;
  94. // Field flags. Use accessor functions below.
  95. GPBFieldFlags flags;
  96. // Data type of the ivar.
  97. GPBDataType dataType;
  98. } GPBMessageFieldDescription;
  99. // Fields in messages defined in a 'proto2' syntax file can provide a default
  100. // value. This struct provides the default along with the field info.
  101. typedef struct GPBMessageFieldDescriptionWithDefault {
  102. // Default value for the ivar.
  103. GPBGenericValue defaultValue;
  104. GPBMessageFieldDescription core;
  105. } GPBMessageFieldDescriptionWithDefault;
  106. // Describes attributes of the extension.
  107. typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
  108. GPBExtensionNone = 0,
  109. // These map to standard protobuf concepts.
  110. GPBExtensionRepeated = 1 << 0,
  111. GPBExtensionPacked = 1 << 1,
  112. GPBExtensionSetWireFormat = 1 << 2,
  113. };
  114. // An extension
  115. typedef struct GPBExtensionDescription {
  116. GPBGenericValue defaultValue;
  117. const char *singletonName;
  118. union {
  119. const char *name;
  120. Class clazz;
  121. } extendedClass;
  122. union {
  123. const char *name;
  124. Class clazz;
  125. } messageOrGroupClass;
  126. GPBEnumDescriptorFunc enumDescriptorFunc;
  127. int32_t fieldNumber;
  128. GPBDataType dataType;
  129. GPBExtensionOptions options;
  130. } GPBExtensionDescription;
  131. typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
  132. GPBDescriptorInitializationFlag_None = 0,
  133. GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
  134. GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
  135. // This is used as a stopgap as we move from using class names to class
  136. // references. The runtime needs to support both until we allow a
  137. // breaking change in the runtime.
  138. GPBDescriptorInitializationFlag_UsesClassRefs = 1 << 2,
  139. };
  140. @interface GPBDescriptor () {
  141. @package
  142. NSArray *fields_;
  143. NSArray *oneofs_;
  144. uint32_t storageSize_;
  145. }
  146. // fieldDescriptions have to be long lived, they are held as raw pointers.
  147. + (instancetype)
  148. allocDescriptorForClass:(Class)messageClass
  149. rootClass:(Class)rootClass
  150. file:(GPBFileDescriptor *)file
  151. fields:(void *)fieldDescriptions
  152. fieldCount:(uint32_t)fieldCount
  153. storageSize:(uint32_t)storageSize
  154. flags:(GPBDescriptorInitializationFlags)flags;
  155. - (instancetype)initWithClass:(Class)messageClass
  156. file:(GPBFileDescriptor *)file
  157. fields:(NSArray *)fields
  158. storageSize:(uint32_t)storage
  159. wireFormat:(BOOL)wireFormat;
  160. // Called right after init to provide extra information to avoid init having
  161. // an explosion of args. These pointers are recorded, so they are expected
  162. // to live for the lifetime of the app.
  163. - (void)setupOneofs:(const char **)oneofNames
  164. count:(uint32_t)count
  165. firstHasIndex:(int32_t)firstHasIndex;
  166. - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
  167. - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
  168. - (void)setupContainingMessageClass:(Class)msgClass;
  169. - (void)setupMessageClassNameSuffix:(NSString *)suffix;
  170. // Deprecated. Use setupContainingMessageClass instead.
  171. - (void)setupContainingMessageClassName:(const char *)msgClassName;
  172. @end
  173. @interface GPBFileDescriptor ()
  174. - (instancetype)initWithPackage:(NSString *)package
  175. objcPrefix:(NSString *)objcPrefix
  176. syntax:(GPBFileSyntax)syntax;
  177. - (instancetype)initWithPackage:(NSString *)package
  178. syntax:(GPBFileSyntax)syntax;
  179. @end
  180. @interface GPBOneofDescriptor () {
  181. @package
  182. const char *name_;
  183. NSArray *fields_;
  184. SEL caseSel_;
  185. }
  186. // name must be long lived.
  187. - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
  188. @end
  189. @interface GPBFieldDescriptor () {
  190. @package
  191. GPBMessageFieldDescription *description_;
  192. GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
  193. SEL getSel_;
  194. SEL setSel_;
  195. SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
  196. SEL setHasSel_;
  197. }
  198. // Single initializer
  199. // description has to be long lived, it is held as a raw pointer.
  200. - (instancetype)initWithFieldDescription:(void *)description
  201. includesDefault:(BOOL)includesDefault
  202. usesClassRefs:(BOOL)usesClassRefs
  203. syntax:(GPBFileSyntax)syntax;
  204. // Deprecated. Equivalent to calling above with `usesClassRefs = NO`.
  205. - (instancetype)initWithFieldDescription:(void *)description
  206. includesDefault:(BOOL)includesDefault
  207. syntax:(GPBFileSyntax)syntax;
  208. @end
  209. @interface GPBEnumDescriptor ()
  210. // valueNames, values and extraTextFormatInfo have to be long lived, they are
  211. // held as raw pointers.
  212. + (instancetype)
  213. allocDescriptorForName:(NSString *)name
  214. valueNames:(const char *)valueNames
  215. values:(const int32_t *)values
  216. count:(uint32_t)valueCount
  217. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  218. + (instancetype)
  219. allocDescriptorForName:(NSString *)name
  220. valueNames:(const char *)valueNames
  221. values:(const int32_t *)values
  222. count:(uint32_t)valueCount
  223. enumVerifier:(GPBEnumValidationFunc)enumVerifier
  224. extraTextFormatInfo:(const char *)extraTextFormatInfo;
  225. - (instancetype)initWithName:(NSString *)name
  226. valueNames:(const char *)valueNames
  227. values:(const int32_t *)values
  228. count:(uint32_t)valueCount
  229. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  230. @end
  231. @interface GPBExtensionDescriptor () {
  232. @package
  233. GPBExtensionDescription *description_;
  234. }
  235. @property(nonatomic, readonly) GPBWireFormat wireType;
  236. // For repeated extensions, alternateWireType is the wireType with the opposite
  237. // value for the packable property. i.e. - if the extension was marked packed
  238. // it would be the wire type for unpacked; if the extension was marked unpacked,
  239. // it would be the wire type for packed.
  240. @property(nonatomic, readonly) GPBWireFormat alternateWireType;
  241. // description has to be long lived, it is held as a raw pointer.
  242. - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
  243. usesClassRefs:(BOOL)usesClassRefs;
  244. // Deprecated. Calls above with `usesClassRefs = NO`
  245. - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc;
  246. - (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
  247. @end
  248. CF_EXTERN_C_BEGIN
  249. // Direct access is use for speed, to avoid even internally declaring things
  250. // read/write, etc. The warning is enabled in the project to ensure code calling
  251. // protos can turn on -Wdirect-ivar-access without issues.
  252. #pragma clang diagnostic push
  253. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  254. GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
  255. return (field->description_->flags &
  256. (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
  257. }
  258. GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
  259. return field->description_->dataType;
  260. }
  261. GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
  262. return field->description_->hasIndex;
  263. }
  264. GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
  265. return field->description_->number;
  266. }
  267. #pragma clang diagnostic pop
  268. uint32_t GPBFieldTag(GPBFieldDescriptor *self);
  269. // For repeated fields, alternateWireType is the wireType with the opposite
  270. // value for the packable property. i.e. - if the field was marked packed it
  271. // would be the wire type for unpacked; if the field was marked unpacked, it
  272. // would be the wire type for packed.
  273. uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
  274. GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
  275. return syntax == GPBFileSyntaxProto3;
  276. }
  277. GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
  278. return (description->options & GPBExtensionRepeated) != 0;
  279. }
  280. GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
  281. return (description->options & GPBExtensionPacked) != 0;
  282. }
  283. GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
  284. return (description->options & GPBExtensionSetWireFormat) != 0;
  285. }
  286. // Helper for compile time assets.
  287. #ifndef GPBInternalCompileAssert
  288. #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
  289. #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
  290. #else
  291. // Pre-Xcode 7 support.
  292. #define GPBInternalCompileAssertSymbolInner(line, msg) GPBInternalCompileAssert ## line ## __ ## msg
  293. #define GPBInternalCompileAssertSymbol(line, msg) GPBInternalCompileAssertSymbolInner(line, msg)
  294. #define GPBInternalCompileAssert(test, msg) \
  295. typedef char GPBInternalCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  296. #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
  297. #endif // GPBInternalCompileAssert
  298. // Sanity check that there isn't padding between the field description
  299. // structures with and without a default.
  300. GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
  301. (sizeof(GPBGenericValue) +
  302. sizeof(GPBMessageFieldDescription)),
  303. DescriptionsWithDefault_different_size_than_expected);
  304. CF_EXTERN_C_END