GPBDescriptor_PackagePrivate.h 12 KB

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