objectivec_enum_field.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_field.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/wire_format.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace objectivec {
  42. namespace {
  43. void SetEnumVariables(const FieldDescriptor* descriptor,
  44. map<string, string>* variables) {
  45. string type = EnumName(descriptor->enum_type());
  46. (*variables)["storage_type"] = type;
  47. // For non repeated fields, if it was defined in a different file, the
  48. // property decls need to use "enum NAME" rather than just "NAME" to support
  49. // the forward declaration of the enums.
  50. if (!descriptor->is_repeated() &&
  51. (descriptor->file() != descriptor->enum_type()->file())) {
  52. (*variables)["property_type"] = "enum " + type;
  53. }
  54. (*variables)["enum_verifier"] = type + "_IsValidValue";
  55. (*variables)["enum_desc_func"] = type + "_EnumDescriptor";
  56. (*variables)["dataTypeSpecific_name"] = "enumDescFunc";
  57. (*variables)["dataTypeSpecific_value"] = (*variables)["enum_desc_func"];
  58. const Descriptor* msg_descriptor = descriptor->containing_type();
  59. (*variables)["owning_message_class"] = ClassName(msg_descriptor);
  60. }
  61. } // namespace
  62. EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
  63. const Options& options)
  64. : SingleFieldGenerator(descriptor, options) {
  65. SetEnumVariables(descriptor, &variables_);
  66. }
  67. EnumFieldGenerator::~EnumFieldGenerator() {}
  68. void EnumFieldGenerator::GenerateCFunctionDeclarations(
  69. io::Printer* printer) const {
  70. if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
  71. return;
  72. }
  73. printer->Print(
  74. variables_,
  75. "/// Fetches the raw value of a @c $owning_message_class$'s @c $name$ property, even\n"
  76. "/// if the value was not defined by the enum at the time the code was generated.\n"
  77. "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message);\n"
  78. "/// Sets the raw value of an @c $owning_message_class$'s @c $name$ property, allowing\n"
  79. "/// it to be set to a value that was not defined by the enum at the time the code\n"
  80. "/// was generated.\n"
  81. "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value);\n"
  82. "\n");
  83. }
  84. void EnumFieldGenerator::GenerateCFunctionImplementations(
  85. io::Printer* printer) const {
  86. if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return;
  87. printer->Print(
  88. variables_,
  89. "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message) {\n"
  90. " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
  91. " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
  92. " return GPBGetMessageInt32Field(message, field);\n"
  93. "}\n"
  94. "\n"
  95. "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value) {\n"
  96. " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
  97. " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
  98. " GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax);\n"
  99. "}\n"
  100. "\n");
  101. }
  102. void EnumFieldGenerator::DetermineForwardDeclarations(
  103. set<string>* fwd_decls) const {
  104. SingleFieldGenerator::DetermineForwardDeclarations(fwd_decls);
  105. // If it is an enum defined in a different file, then we'll need a forward
  106. // declaration for it. When it is in our file, all the enums are output
  107. // before the message, so it will be declared before it is needed.
  108. if (descriptor_->file() != descriptor_->enum_type()->file()) {
  109. // Enum name is already in "storage_type".
  110. const string& name = variable("storage_type");
  111. fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")");
  112. }
  113. }
  114. RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
  115. const FieldDescriptor* descriptor, const Options& options)
  116. : RepeatedFieldGenerator(descriptor, options) {
  117. SetEnumVariables(descriptor, &variables_);
  118. variables_["array_storage_type"] = "GPBEnumArray";
  119. }
  120. RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
  121. void RepeatedEnumFieldGenerator::FinishInitialization(void) {
  122. RepeatedFieldGenerator::FinishInitialization();
  123. variables_["array_comment"] =
  124. "// |" + variables_["name"] + "| contains |" + variables_["storage_type"] + "|\n";
  125. }
  126. } // namespace objectivec
  127. } // namespace compiler
  128. } // namespace protobuf
  129. } // namespace google