objectivec_helpers.h 13 KB

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