objectivec_enum.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <map>
  31. #include <string>
  32. #include <google/protobuf/compiler/objectivec/objectivec_enum.h>
  33. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  34. #include <google/protobuf/io/printer.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace compiler {
  39. namespace objectivec {
  40. EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
  41. : descriptor_(descriptor),
  42. name_(EnumName(descriptor_)) {
  43. for (int i = 0; i < descriptor_->value_count(); i++) {
  44. const EnumValueDescriptor* value = descriptor_->value(i);
  45. const EnumValueDescriptor* canonical_value =
  46. descriptor_->FindValueByNumber(value->number());
  47. if (value == canonical_value) {
  48. base_values_.push_back(value);
  49. }
  50. all_values_.push_back(value);
  51. }
  52. }
  53. EnumGenerator::~EnumGenerator() {}
  54. void EnumGenerator::GenerateHeader(io::Printer* printer) {
  55. string enum_comments;
  56. SourceLocation location;
  57. if (descriptor_->GetSourceLocation(&location)) {
  58. enum_comments = BuildCommentsString(location, true);
  59. } else {
  60. enum_comments = "";
  61. }
  62. printer->Print(
  63. "#pragma mark - Enum $name$\n"
  64. "\n",
  65. "name", name_);
  66. printer->Print("$comments$typedef$deprecated_attribute$ GPB_ENUM($name$) {\n",
  67. "comments", enum_comments,
  68. "deprecated_attribute", GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file()),
  69. "name", name_);
  70. printer->Indent();
  71. if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
  72. // Include the unknown value.
  73. printer->Print(
  74. "/**\n"
  75. " * Value used if any message's field encounters a value that is not defined\n"
  76. " * by this enum. The message will also have C functions to get/set the rawValue\n"
  77. " * of the field.\n"
  78. " **/\n"
  79. "$name$_GPBUnrecognizedEnumeratorValue = kGPBUnrecognizedEnumeratorValue,\n",
  80. "name", name_);
  81. }
  82. for (int i = 0; i < all_values_.size(); i++) {
  83. SourceLocation location;
  84. if (all_values_[i]->GetSourceLocation(&location)) {
  85. string comments = BuildCommentsString(location, true).c_str();
  86. if (comments.length() > 0) {
  87. if (i > 0) {
  88. printer->Print("\n");
  89. }
  90. printer->Print(comments.c_str());
  91. }
  92. }
  93. printer->Print(
  94. "$name$$deprecated_attribute$ = $value$,\n",
  95. "name", EnumValueName(all_values_[i]),
  96. "deprecated_attribute", GetOptionalDeprecatedAttribute(all_values_[i]),
  97. "value", SimpleItoa(all_values_[i]->number()));
  98. }
  99. printer->Outdent();
  100. printer->Print(
  101. "};\n"
  102. "\n"
  103. "GPBEnumDescriptor *$name$_EnumDescriptor(void);\n"
  104. "\n"
  105. "/**\n"
  106. " * Checks to see if the given value is defined by the enum or was not known at\n"
  107. " * the time this source was generated.\n"
  108. " **/\n"
  109. "BOOL $name$_IsValidValue(int32_t value);\n"
  110. "\n",
  111. "name", name_);
  112. }
  113. void EnumGenerator::GenerateSource(io::Printer* printer) {
  114. printer->Print(
  115. "#pragma mark - Enum $name$\n"
  116. "\n",
  117. "name", name_);
  118. // Note: For the TextFormat decode info, we can't use the enum value as
  119. // the key because protocol buffer enums have 'allow_alias', which lets
  120. // a value be used more than once. Instead, the index into the list of
  121. // enum value descriptions is used. Note: start with -1 so the first one
  122. // will be zero.
  123. TextFormatDecodeData text_format_decode_data;
  124. int enum_value_description_key = -1;
  125. string text_blob;
  126. for (int i = 0; i < all_values_.size(); i++) {
  127. ++enum_value_description_key;
  128. string short_name(EnumValueShortName(all_values_[i]));
  129. text_blob += short_name + '\0';
  130. if (UnCamelCaseEnumShortName(short_name) != all_values_[i]->name()) {
  131. text_format_decode_data.AddString(enum_value_description_key, short_name,
  132. all_values_[i]->name());
  133. }
  134. }
  135. printer->Print(
  136. "GPBEnumDescriptor *$name$_EnumDescriptor(void) {\n"
  137. " static _Atomic(GPBEnumDescriptor*) descriptor = nil;\n"
  138. " if (!descriptor) {\n",
  139. "name", name_);
  140. static const int kBytesPerLine = 40; // allow for escaping
  141. printer->Print(
  142. " static const char *valueNames =");
  143. for (int i = 0; i < text_blob.size(); i += kBytesPerLine) {
  144. printer->Print(
  145. "\n \"$data$\"",
  146. "data", EscapeTrigraphs(CEscape(text_blob.substr(i, kBytesPerLine))));
  147. }
  148. printer->Print(
  149. ";\n"
  150. " static const int32_t values[] = {\n");
  151. for (int i = 0; i < all_values_.size(); i++) {
  152. printer->Print(" $name$,\n", "name", EnumValueName(all_values_[i]));
  153. }
  154. printer->Print(" };\n");
  155. if (text_format_decode_data.num_entries() == 0) {
  156. printer->Print(
  157. " GPBEnumDescriptor *worker =\n"
  158. " [GPBEnumDescriptor allocDescriptorForName:GPBNSStringifySymbol($name$)\n"
  159. " valueNames:valueNames\n"
  160. " values:values\n"
  161. " count:(uint32_t)(sizeof(values) / sizeof(int32_t))\n"
  162. " enumVerifier:$name$_IsValidValue];\n",
  163. "name", name_);
  164. } else {
  165. printer->Print(
  166. " static const char *extraTextFormatInfo = \"$extraTextFormatInfo$\";\n"
  167. " GPBEnumDescriptor *worker =\n"
  168. " [GPBEnumDescriptor allocDescriptorForName:GPBNSStringifySymbol($name$)\n"
  169. " valueNames:valueNames\n"
  170. " values:values\n"
  171. " count:(uint32_t)(sizeof(values) / sizeof(int32_t))\n"
  172. " enumVerifier:$name$_IsValidValue\n"
  173. " extraTextFormatInfo:extraTextFormatInfo];\n",
  174. "name", name_,
  175. "extraTextFormatInfo", CEscape(text_format_decode_data.Data()));
  176. }
  177. printer->Print(
  178. " GPBEnumDescriptor *expected = nil;\n"
  179. " if (!atomic_compare_exchange_strong(&descriptor, &expected, worker)) {\n"
  180. " [worker release];\n"
  181. " }\n"
  182. " }\n"
  183. " return descriptor;\n"
  184. "}\n\n");
  185. printer->Print(
  186. "BOOL $name$_IsValidValue(int32_t value__) {\n"
  187. " switch (value__) {\n",
  188. "name", name_);
  189. for (int i = 0; i < base_values_.size(); i++) {
  190. printer->Print(
  191. " case $name$:\n",
  192. "name", EnumValueName(base_values_[i]));
  193. }
  194. printer->Print(
  195. " return YES;\n"
  196. " default:\n"
  197. " return NO;\n"
  198. " }\n"
  199. "}\n\n");
  200. }
  201. } // namespace objectivec
  202. } // namespace compiler
  203. } // namespace protobuf
  204. } // namespace google