objectivec_helpers.h 13 KB

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