objectivec_helpers.h 12 KB

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