GPBDescriptor_PackagePrivate.h 11 KB

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