GPBDescriptor_PackagePrivate.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // Indicate that the field should "clear" when set to zero value. This is the
  45. // proto3 non optional behavior for singular data (ints, data, string, enum)
  46. // fields.
  47. GPBFieldClearHasIvarOnZero = 1 << 5,
  48. // Indicates the field needs custom handling for the TextFormat name, if not
  49. // set, the name can be derived from the ObjC name.
  50. GPBFieldTextFormatNameCustom = 1 << 6,
  51. // Indicates the field has an enum descriptor.
  52. GPBFieldHasEnumDescriptor = 1 << 7,
  53. // These are not standard protobuf concepts, they are specific to the
  54. // Objective C runtime.
  55. // These bits are used to mark the field as a map and what the key
  56. // type is.
  57. GPBFieldMapKeyMask = 0xF << 8,
  58. GPBFieldMapKeyInt32 = 1 << 8,
  59. GPBFieldMapKeyInt64 = 2 << 8,
  60. GPBFieldMapKeyUInt32 = 3 << 8,
  61. GPBFieldMapKeyUInt64 = 4 << 8,
  62. GPBFieldMapKeySInt32 = 5 << 8,
  63. GPBFieldMapKeySInt64 = 6 << 8,
  64. GPBFieldMapKeyFixed32 = 7 << 8,
  65. GPBFieldMapKeyFixed64 = 8 << 8,
  66. GPBFieldMapKeySFixed32 = 9 << 8,
  67. GPBFieldMapKeySFixed64 = 10 << 8,
  68. GPBFieldMapKeyBool = 11 << 8,
  69. GPBFieldMapKeyString = 12 << 8,
  70. };
  71. // NOTE: The structures defined here have their members ordered to minimize
  72. // their size. This directly impacts the size of apps since these exist per
  73. // field/extension.
  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. union {
  79. // className is deprecated and will be removed in favor of clazz.
  80. // kept around right now for backwards compatibility.
  81. // clazz is used iff GPBDescriptorInitializationFlag_UsesClassRefs is set.
  82. char *className; // Name of the class of the message.
  83. Class clazz; // Class of the message.
  84. // For enums only: If EnumDescriptors are compiled in, it will be that,
  85. // otherwise it will be the verifier.
  86. GPBEnumDescriptorFunc enumDescFunc;
  87. GPBEnumValidationFunc enumVerifier;
  88. } dataTypeSpecific;
  89. // The field number for the ivar.
  90. uint32_t number;
  91. // The index (in bits) into _has_storage_.
  92. // >= 0: the bit to use for a value being set.
  93. // = GPBNoHasBit(INT32_MAX): no storage used.
  94. // < 0: in a oneOf, use a full int32 to record the field active.
  95. int32_t hasIndex;
  96. // Offset of the variable into it's structure struct.
  97. uint32_t offset;
  98. // Field flags. Use accessor functions below.
  99. GPBFieldFlags flags;
  100. // Data type of the ivar.
  101. GPBDataType dataType;
  102. } GPBMessageFieldDescription;
  103. // Fields in messages defined in a 'proto2' syntax file can provide a default
  104. // value. This struct provides the default along with the field info.
  105. typedef struct GPBMessageFieldDescriptionWithDefault {
  106. // Default value for the ivar.
  107. GPBGenericValue defaultValue;
  108. GPBMessageFieldDescription core;
  109. } GPBMessageFieldDescriptionWithDefault;
  110. // Describes attributes of the extension.
  111. typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
  112. GPBExtensionNone = 0,
  113. // These map to standard protobuf concepts.
  114. GPBExtensionRepeated = 1 << 0,
  115. GPBExtensionPacked = 1 << 1,
  116. GPBExtensionSetWireFormat = 1 << 2,
  117. };
  118. // An extension
  119. typedef struct GPBExtensionDescription {
  120. GPBGenericValue defaultValue;
  121. const char *singletonName;
  122. // Before 3.12, `extendedClass` was just a `const char *`. Thanks to nested
  123. // initialization (https://en.cppreference.com/w/c/language/struct_initialization#Nested_initialization)
  124. // old generated code with `.extendedClass = GPBStringifySymbol(Something)`
  125. // still works; and the current generator can use `extendedClass.clazz`, to
  126. // pass a Class reference.
  127. union {
  128. const char *name;
  129. Class clazz;
  130. } extendedClass;
  131. // Before 3.12, this was `const char *messageOrGroupClassName`. In the
  132. // initial 3.12 release, we moved the `union messageOrGroupClass`, and failed
  133. // to realize that would break existing source code for extensions. So to
  134. // keep existing source code working, we added an unnamed union (C11) to
  135. // provide both the old field name and the new union. This keeps both older
  136. // and newer code working.
  137. // Background: https://github.com/protocolbuffers/protobuf/issues/7555
  138. union {
  139. const char *messageOrGroupClassName;
  140. union {
  141. const char *name;
  142. Class clazz;
  143. } messageOrGroupClass;
  144. };
  145. GPBEnumDescriptorFunc enumDescriptorFunc;
  146. int32_t fieldNumber;
  147. GPBDataType dataType;
  148. GPBExtensionOptions options;
  149. } GPBExtensionDescription;
  150. typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
  151. GPBDescriptorInitializationFlag_None = 0,
  152. GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
  153. GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
  154. // This is used as a stopgap as we move from using class names to class
  155. // references. The runtime needs to support both until we allow a
  156. // breaking change in the runtime.
  157. GPBDescriptorInitializationFlag_UsesClassRefs = 1 << 2,
  158. // This flag is used to indicate that the generated sources already contain
  159. // the `GPBFieldClearHasIvarOnZero` flag and it doesn't have to be computed
  160. // at startup. This allows older generated code to still work with the
  161. // current runtime library.
  162. GPBDescriptorInitializationFlag_Proto3OptionalKnown = 1 << 3,
  163. };
  164. @interface GPBDescriptor () {
  165. @package
  166. NSArray *fields_;
  167. NSArray *oneofs_;
  168. uint32_t storageSize_;
  169. }
  170. // fieldDescriptions have to be long lived, they are held as raw pointers.
  171. + (instancetype)
  172. allocDescriptorForClass:(Class)messageClass
  173. rootClass:(Class)rootClass
  174. file:(GPBFileDescriptor *)file
  175. fields:(void *)fieldDescriptions
  176. fieldCount:(uint32_t)fieldCount
  177. storageSize:(uint32_t)storageSize
  178. flags:(GPBDescriptorInitializationFlags)flags;
  179. - (instancetype)initWithClass:(Class)messageClass
  180. file:(GPBFileDescriptor *)file
  181. fields:(NSArray *)fields
  182. storageSize:(uint32_t)storage
  183. wireFormat:(BOOL)wireFormat;
  184. // Called right after init to provide extra information to avoid init having
  185. // an explosion of args. These pointers are recorded, so they are expected
  186. // to live for the lifetime of the app.
  187. - (void)setupOneofs:(const char **)oneofNames
  188. count:(uint32_t)count
  189. firstHasIndex:(int32_t)firstHasIndex;
  190. - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
  191. - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
  192. - (void)setupContainingMessageClass:(Class)msgClass;
  193. - (void)setupMessageClassNameSuffix:(NSString *)suffix;
  194. // Deprecated. Use setupContainingMessageClass instead.
  195. - (void)setupContainingMessageClassName:(const char *)msgClassName;
  196. @end
  197. @interface GPBFileDescriptor ()
  198. - (instancetype)initWithPackage:(NSString *)package
  199. objcPrefix:(NSString *)objcPrefix
  200. syntax:(GPBFileSyntax)syntax;
  201. - (instancetype)initWithPackage:(NSString *)package
  202. syntax:(GPBFileSyntax)syntax;
  203. @end
  204. @interface GPBOneofDescriptor () {
  205. @package
  206. const char *name_;
  207. NSArray *fields_;
  208. SEL caseSel_;
  209. }
  210. // name must be long lived.
  211. - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
  212. @end
  213. @interface GPBFieldDescriptor () {
  214. @package
  215. GPBMessageFieldDescription *description_;
  216. GPB_UNSAFE_UNRETAINED GPBOneofDescriptor *containingOneof_;
  217. SEL getSel_;
  218. SEL setSel_;
  219. SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
  220. SEL setHasSel_;
  221. }
  222. // Single initializer
  223. // description has to be long lived, it is held as a raw pointer.
  224. - (instancetype)initWithFieldDescription:(void *)description
  225. includesDefault:(BOOL)includesDefault
  226. usesClassRefs:(BOOL)usesClassRefs
  227. proto3OptionalKnown:(BOOL)proto3OptionalKnown
  228. syntax:(GPBFileSyntax)syntax;
  229. @end
  230. @interface GPBEnumDescriptor ()
  231. // valueNames, values and extraTextFormatInfo have to be long lived, they are
  232. // held as raw pointers.
  233. + (instancetype)
  234. allocDescriptorForName:(NSString *)name
  235. valueNames:(const char *)valueNames
  236. values:(const int32_t *)values
  237. count:(uint32_t)valueCount
  238. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  239. + (instancetype)
  240. allocDescriptorForName:(NSString *)name
  241. valueNames:(const char *)valueNames
  242. values:(const int32_t *)values
  243. count:(uint32_t)valueCount
  244. enumVerifier:(GPBEnumValidationFunc)enumVerifier
  245. extraTextFormatInfo:(const char *)extraTextFormatInfo;
  246. - (instancetype)initWithName:(NSString *)name
  247. valueNames:(const char *)valueNames
  248. values:(const int32_t *)values
  249. count:(uint32_t)valueCount
  250. enumVerifier:(GPBEnumValidationFunc)enumVerifier;
  251. @end
  252. @interface GPBExtensionDescriptor () {
  253. @package
  254. GPBExtensionDescription *description_;
  255. }
  256. @property(nonatomic, readonly) GPBWireFormat wireType;
  257. // For repeated extensions, alternateWireType is the wireType with the opposite
  258. // value for the packable property. i.e. - if the extension was marked packed
  259. // it would be the wire type for unpacked; if the extension was marked unpacked,
  260. // it would be the wire type for packed.
  261. @property(nonatomic, readonly) GPBWireFormat alternateWireType;
  262. // description has to be long lived, it is held as a raw pointer.
  263. - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
  264. usesClassRefs:(BOOL)usesClassRefs;
  265. // Deprecated. Calls above with `usesClassRefs = NO`
  266. - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc;
  267. - (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
  268. @end
  269. CF_EXTERN_C_BEGIN
  270. // Direct access is use for speed, to avoid even internally declaring things
  271. // read/write, etc. The warning is enabled in the project to ensure code calling
  272. // protos can turn on -Wdirect-ivar-access without issues.
  273. #pragma clang diagnostic push
  274. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  275. GPB_INLINE BOOL GPBFieldIsMapOrArray(GPBFieldDescriptor *field) {
  276. return (field->description_->flags &
  277. (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0;
  278. }
  279. GPB_INLINE GPBDataType GPBGetFieldDataType(GPBFieldDescriptor *field) {
  280. return field->description_->dataType;
  281. }
  282. GPB_INLINE int32_t GPBFieldHasIndex(GPBFieldDescriptor *field) {
  283. return field->description_->hasIndex;
  284. }
  285. GPB_INLINE uint32_t GPBFieldNumber(GPBFieldDescriptor *field) {
  286. return field->description_->number;
  287. }
  288. #pragma clang diagnostic pop
  289. uint32_t GPBFieldTag(GPBFieldDescriptor *self);
  290. // For repeated fields, alternateWireType is the wireType with the opposite
  291. // value for the packable property. i.e. - if the field was marked packed it
  292. // would be the wire type for unpacked; if the field was marked unpacked, it
  293. // would be the wire type for packed.
  294. uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self);
  295. GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) {
  296. return syntax == GPBFileSyntaxProto3;
  297. }
  298. GPB_INLINE BOOL GPBExtensionIsRepeated(GPBExtensionDescription *description) {
  299. return (description->options & GPBExtensionRepeated) != 0;
  300. }
  301. GPB_INLINE BOOL GPBExtensionIsPacked(GPBExtensionDescription *description) {
  302. return (description->options & GPBExtensionPacked) != 0;
  303. }
  304. GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
  305. return (description->options & GPBExtensionSetWireFormat) != 0;
  306. }
  307. // Helper for compile time assets.
  308. #ifndef GPBInternalCompileAssert
  309. #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
  310. #define GPBInternalCompileAssert(test, msg) _Static_assert((test), #msg)
  311. #else
  312. // Pre-Xcode 7 support.
  313. #define GPBInternalCompileAssertSymbolInner(line, msg) GPBInternalCompileAssert ## line ## __ ## msg
  314. #define GPBInternalCompileAssertSymbol(line, msg) GPBInternalCompileAssertSymbolInner(line, msg)
  315. #define GPBInternalCompileAssert(test, msg) \
  316. typedef char GPBInternalCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
  317. #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
  318. #endif // GPBInternalCompileAssert
  319. // Sanity check that there isn't padding between the field description
  320. // structures with and without a default.
  321. GPBInternalCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
  322. (sizeof(GPBGenericValue) +
  323. sizeof(GPBMessageFieldDescription)),
  324. DescriptionsWithDefault_different_size_than_expected);
  325. CF_EXTERN_C_END