objectivec_helpers.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. // Helper functions for generating ObjectiveC code.
  31. #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
  32. #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
  33. #include <string>
  34. #include <vector>
  35. #include <google/protobuf/descriptor.h>
  36. #include <google/protobuf/descriptor.pb.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace objectivec {
  41. // Generator options (see objectivec_generator.cc for a description of each):
  42. struct Options {
  43. Options();
  44. string expected_prefixes_path;
  45. string generate_for_named_framework;
  46. string named_framework_to_proto_path_mappings_path;
  47. };
  48. // Escape C++ trigraphs by escaping question marks to "\?".
  49. string LIBPROTOC_EXPORT EscapeTrigraphs(const string& to_escape);
  50. // Strips ".proto" or ".protodevel" from the end of a filename.
  51. string LIBPROTOC_EXPORT StripProto(const string& filename);
  52. // Remove white space from either end of a StringPiece.
  53. void LIBPROTOC_EXPORT StringPieceTrimWhitespace(StringPiece* input);
  54. // Returns true if the name requires a ns_returns_not_retained attribute applied
  55. // to it.
  56. bool LIBPROTOC_EXPORT IsRetainedName(const string& name);
  57. // Returns true if the name starts with "init" and will need to have special
  58. // handling under ARC.
  59. bool LIBPROTOC_EXPORT IsInitName(const string& name);
  60. // Gets the objc_class_prefix.
  61. string LIBPROTOC_EXPORT FileClassPrefix(const FileDescriptor* file);
  62. // Gets the path of the file we're going to generate (sans the .pb.h
  63. // extension). The path will be dependent on the objectivec package
  64. // declared in the proto package.
  65. string LIBPROTOC_EXPORT FilePath(const FileDescriptor* file);
  66. // Just like FilePath(), but without the directory part.
  67. string LIBPROTOC_EXPORT FilePathBasename(const FileDescriptor* file);
  68. // Gets the name of the root class we'll generate in the file. This class
  69. // is not meant for external consumption, but instead contains helpers that
  70. // the rest of the classes need
  71. string LIBPROTOC_EXPORT FileClassName(const FileDescriptor* file);
  72. // These return the fully-qualified class name corresponding to the given
  73. // descriptor.
  74. string LIBPROTOC_EXPORT ClassName(const Descriptor* descriptor);
  75. string LIBPROTOC_EXPORT ClassName(const Descriptor* descriptor, string* out_suffix_added);
  76. string LIBPROTOC_EXPORT EnumName(const EnumDescriptor* descriptor);
  77. // Returns the fully-qualified name of the enum value corresponding to the
  78. // the descriptor.
  79. string LIBPROTOC_EXPORT EnumValueName(const EnumValueDescriptor* descriptor);
  80. // Returns the name of the enum value corresponding to the descriptor.
  81. string LIBPROTOC_EXPORT EnumValueShortName(const EnumValueDescriptor* descriptor);
  82. // Reverse what an enum does.
  83. string LIBPROTOC_EXPORT UnCamelCaseEnumShortName(const string& name);
  84. // Returns the name to use for the extension (used as the method off the file's
  85. // Root class).
  86. string LIBPROTOC_EXPORT ExtensionMethodName(const FieldDescriptor* descriptor);
  87. // Returns the transformed field name.
  88. string LIBPROTOC_EXPORT FieldName(const FieldDescriptor* field);
  89. string LIBPROTOC_EXPORT FieldNameCapitalized(const FieldDescriptor* field);
  90. // Returns the transformed oneof name.
  91. string LIBPROTOC_EXPORT OneofEnumName(const OneofDescriptor* descriptor);
  92. string LIBPROTOC_EXPORT OneofName(const OneofDescriptor* descriptor);
  93. string LIBPROTOC_EXPORT OneofNameCapitalized(const OneofDescriptor* descriptor);
  94. inline bool HasFieldPresence(const FileDescriptor* file) {
  95. return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
  96. }
  97. inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
  98. return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
  99. }
  100. inline bool IsMapEntryMessage(const Descriptor* descriptor) {
  101. return descriptor->options().map_entry();
  102. }
  103. // Reverse of the above.
  104. string LIBPROTOC_EXPORT UnCamelCaseFieldName(const string& name, const FieldDescriptor* field);
  105. enum ObjectiveCType {
  106. OBJECTIVECTYPE_INT32,
  107. OBJECTIVECTYPE_UINT32,
  108. OBJECTIVECTYPE_INT64,
  109. OBJECTIVECTYPE_UINT64,
  110. OBJECTIVECTYPE_FLOAT,
  111. OBJECTIVECTYPE_DOUBLE,
  112. OBJECTIVECTYPE_BOOLEAN,
  113. OBJECTIVECTYPE_STRING,
  114. OBJECTIVECTYPE_DATA,
  115. OBJECTIVECTYPE_ENUM,
  116. OBJECTIVECTYPE_MESSAGE
  117. };
  118. enum FlagType {
  119. FLAGTYPE_DESCRIPTOR_INITIALIZATION,
  120. FLAGTYPE_EXTENSION,
  121. FLAGTYPE_FIELD
  122. };
  123. template<class TDescriptor>
  124. string GetOptionalDeprecatedAttribute(
  125. const TDescriptor* descriptor,
  126. const FileDescriptor* file = NULL,
  127. bool preSpace = true, bool postNewline = false) {
  128. bool isDeprecated = descriptor->options().deprecated();
  129. // The file is only passed when checking Messages & Enums, so those types
  130. // get tagged. At the moment, it doesn't seem to make sense to tag every
  131. // field or enum value with when the file is deprecated.
  132. if (!isDeprecated && file) {
  133. isDeprecated = file->options().deprecated();
  134. }
  135. if (isDeprecated) {
  136. string result = "DEPRECATED_ATTRIBUTE";
  137. if (preSpace) {
  138. result.insert(0, " ");
  139. }
  140. if (postNewline) {
  141. result.append("\n");
  142. }
  143. return result;
  144. } else {
  145. return "";
  146. }
  147. }
  148. string LIBPROTOC_EXPORT GetCapitalizedType(const FieldDescriptor* field);
  149. ObjectiveCType LIBPROTOC_EXPORT GetObjectiveCType(FieldDescriptor::Type field_type);
  150. inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
  151. return GetObjectiveCType(field->type());
  152. }
  153. bool LIBPROTOC_EXPORT IsPrimitiveType(const FieldDescriptor* field);
  154. bool LIBPROTOC_EXPORT IsReferenceType(const FieldDescriptor* field);
  155. string LIBPROTOC_EXPORT GPBGenericValueFieldName(const FieldDescriptor* field);
  156. string LIBPROTOC_EXPORT DefaultValue(const FieldDescriptor* field);
  157. bool LIBPROTOC_EXPORT HasNonZeroDefaultValue(const FieldDescriptor* field);
  158. string LIBPROTOC_EXPORT BuildFlagsString(const FlagType type, const std::vector<string>& strings);
  159. // Builds HeaderDoc/appledoc style comments out of the comments in the .proto
  160. // file.
  161. string LIBPROTOC_EXPORT BuildCommentsString(const SourceLocation& location,
  162. bool prefer_single_line);
  163. // The name the commonly used by the library when built as a framework.
  164. // This lines up to the name used in the CocoaPod.
  165. extern LIBPROTOC_EXPORT const char* const ProtobufLibraryFrameworkName;
  166. // Returns the CPP symbol name to use as the gate for framework style imports
  167. // for the given framework name to use.
  168. string LIBPROTOC_EXPORT ProtobufFrameworkImportSymbol(const string& framework_name);
  169. // Checks if the file is one of the proto's bundled with the library.
  170. bool LIBPROTOC_EXPORT IsProtobufLibraryBundledProtoFile(const FileDescriptor* file);
  171. // Checks the prefix for the given files and outputs any warnings as needed. If
  172. // there are flat out errors, then out_error is filled in with the first error
  173. // and the result is false.
  174. bool LIBPROTOC_EXPORT ValidateObjCClassPrefixes(const std::vector<const FileDescriptor*>& files,
  175. const Options& generation_options,
  176. string* out_error);
  177. // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform
  178. // the input into the expected output.
  179. class LIBPROTOC_EXPORT TextFormatDecodeData {
  180. public:
  181. TextFormatDecodeData();
  182. ~TextFormatDecodeData();
  183. void AddString(int32 key, const string& input_for_decode,
  184. const string& desired_output);
  185. size_t num_entries() const { return entries_.size(); }
  186. string Data() const;
  187. static string DecodeDataForString(const string& input_for_decode,
  188. const string& desired_output);
  189. private:
  190. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormatDecodeData);
  191. typedef std::pair<int32, string> DataEntry;
  192. std::vector<DataEntry> entries_;
  193. };
  194. // Helper for parsing simple files.
  195. class LIBPROTOC_EXPORT LineConsumer {
  196. public:
  197. LineConsumer();
  198. virtual ~LineConsumer();
  199. virtual bool ConsumeLine(const StringPiece& line, string* out_error) = 0;
  200. };
  201. bool LIBPROTOC_EXPORT ParseSimpleFile(
  202. const string& path, LineConsumer* line_consumer, string* out_error);
  203. // Helper class for parsing framework import mappings and generating
  204. // import statements.
  205. class LIBPROTOC_EXPORT ImportWriter {
  206. public:
  207. ImportWriter(const string& generate_for_named_framework,
  208. const string& named_framework_to_proto_path_mappings_path,
  209. bool include_wkt_imports);
  210. ~ImportWriter();
  211. void AddFile(const FileDescriptor* file, const string& header_extension);
  212. void Print(io::Printer *printer) const;
  213. private:
  214. class ProtoFrameworkCollector : public LineConsumer {
  215. public:
  216. ProtoFrameworkCollector(std::map<string, string>* inout_proto_file_to_framework_name)
  217. : map_(inout_proto_file_to_framework_name) {}
  218. virtual bool ConsumeLine(const StringPiece& line, string* out_error);
  219. private:
  220. std::map<string, string>* map_;
  221. };
  222. void ParseFrameworkMappings();
  223. const string generate_for_named_framework_;
  224. const string named_framework_to_proto_path_mappings_path_;
  225. const bool include_wkt_imports_;
  226. std::map<string, string> proto_file_to_framework_name_;
  227. bool need_to_parse_mapping_file_;
  228. std::vector<string> protobuf_framework_imports_;
  229. std::vector<string> protobuf_non_framework_imports_;
  230. std::vector<string> other_framework_imports_;
  231. std::vector<string> other_imports_;
  232. };
  233. } // namespace objectivec
  234. } // namespace compiler
  235. } // namespace protobuf
  236. } // namespace google
  237. #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__