objectivec_helpers.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
  31. #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
  32. #include <string>
  33. #include <vector>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/descriptor.pb.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace compiler {
  39. namespace objectivec {
  40. // Generator options (see objectivec_generator.cc for a description of each):
  41. struct Options {
  42. Options();
  43. string expected_prefixes_path;
  44. };
  45. // Escape C++ trigraphs by escaping question marks to "\?".
  46. string EscapeTrigraphs(const string& to_escape);
  47. // Strips ".proto" or ".protodevel" from the end of a filename.
  48. string StripProto(const string& filename);
  49. // Returns true if the name requires a ns_returns_not_retained attribute applied
  50. // to it.
  51. bool IsRetainedName(const string& name);
  52. // Returns true if the name starts with "init" and will need to have special
  53. // handling under ARC.
  54. bool IsInitName(const string& name);
  55. // Gets the name of the file we're going to generate (sans the .pb.h
  56. // extension). This does not include the path to that file.
  57. string FileName(const FileDescriptor* file);
  58. // Gets the path of the file we're going to generate (sans the .pb.h
  59. // extension). The path will be dependent on the objectivec package
  60. // declared in the proto package.
  61. string FilePath(const FileDescriptor* file);
  62. // Gets the name of the root class we'll generate in the file. This class
  63. // is not meant for external consumption, but instead contains helpers that
  64. // the rest of the classes need
  65. string FileClassName(const FileDescriptor* file);
  66. // These return the fully-qualified class name corresponding to the given
  67. // descriptor.
  68. string ClassName(const Descriptor* descriptor);
  69. string EnumName(const EnumDescriptor* descriptor);
  70. // Returns the fully-qualified name of the enum value corresponding to the
  71. // the descriptor.
  72. string EnumValueName(const EnumValueDescriptor* descriptor);
  73. // Returns the name of the enum value corresponding to the descriptor.
  74. string EnumValueShortName(const EnumValueDescriptor* descriptor);
  75. // Reverse what an enum does.
  76. string UnCamelCaseEnumShortName(const string& name);
  77. // Returns the name to use for the extension (used as the method off the file's
  78. // Root class).
  79. string ExtensionMethodName(const FieldDescriptor* descriptor);
  80. // Returns the transformed field name.
  81. string FieldName(const FieldDescriptor* field);
  82. string FieldNameCapitalized(const FieldDescriptor* field);
  83. // Returns the transformed oneof name.
  84. string OneofEnumName(const OneofDescriptor* descriptor);
  85. string OneofName(const OneofDescriptor* descriptor);
  86. string OneofNameCapitalized(const OneofDescriptor* descriptor);
  87. inline bool HasFieldPresence(const FileDescriptor* file) {
  88. return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
  89. }
  90. inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
  91. return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
  92. }
  93. inline bool IsMapEntryMessage(const Descriptor* descriptor) {
  94. return descriptor->options().map_entry();
  95. }
  96. // Reverse of the above.
  97. string UnCamelCaseFieldName(const string& name, const FieldDescriptor* field);
  98. enum ObjectiveCType {
  99. OBJECTIVECTYPE_INT32,
  100. OBJECTIVECTYPE_UINT32,
  101. OBJECTIVECTYPE_INT64,
  102. OBJECTIVECTYPE_UINT64,
  103. OBJECTIVECTYPE_FLOAT,
  104. OBJECTIVECTYPE_DOUBLE,
  105. OBJECTIVECTYPE_BOOLEAN,
  106. OBJECTIVECTYPE_STRING,
  107. OBJECTIVECTYPE_DATA,
  108. OBJECTIVECTYPE_ENUM,
  109. OBJECTIVECTYPE_MESSAGE
  110. };
  111. string GetCapitalizedType(const FieldDescriptor* field);
  112. ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type);
  113. inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
  114. return GetObjectiveCType(field->type());
  115. }
  116. bool IsPrimitiveType(const FieldDescriptor* field);
  117. bool IsReferenceType(const FieldDescriptor* field);
  118. string GPBGenericValueFieldName(const FieldDescriptor* field);
  119. string DefaultValue(const FieldDescriptor* field);
  120. bool HasNonZeroDefaultValue(const FieldDescriptor* field);
  121. string BuildFlagsString(const vector<string>& strings);
  122. // Builds a HeaderDoc style comment out of the comments in the .proto file.
  123. string BuildCommentsString(const SourceLocation& location);
  124. // Checks the prefix for a given file and outputs any warnings needed, if
  125. // there are flat out errors, then out_error is filled in and the result is
  126. // false.
  127. bool ValidateObjCClassPrefix(const FileDescriptor* file,
  128. const Options& generation_options,
  129. string* out_error);
  130. // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform
  131. // the input into the expected output.
  132. class LIBPROTOC_EXPORT TextFormatDecodeData {
  133. public:
  134. TextFormatDecodeData() {}
  135. void AddString(int32 key, const string& input_for_decode,
  136. const string& desired_output);
  137. size_t num_entries() const { return entries_.size(); }
  138. string Data() const;
  139. static string DecodeDataForString(const string& input_for_decode,
  140. const string& desired_output);
  141. private:
  142. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormatDecodeData);
  143. typedef std::pair<int32, string> DataEntry;
  144. vector<DataEntry> entries_;
  145. };
  146. } // namespace objectivec
  147. } // namespace compiler
  148. } // namespace protobuf
  149. } // namespace google
  150. #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__