objectivec_extension.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <iostream>
  31. #include <google/protobuf/compiler/objectivec/objectivec_extension.h>
  32. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  33. #include <google/protobuf/descriptor.pb.h>
  34. #include <google/protobuf/stubs/strutil.h>
  35. #include <google/protobuf/io/printer.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace compiler {
  39. namespace objectivec {
  40. ExtensionGenerator::ExtensionGenerator(const string& root_class_name,
  41. const FieldDescriptor* descriptor)
  42. : method_name_(ExtensionMethodName(descriptor)),
  43. root_class_and_method_name_(root_class_name + "_" + method_name_),
  44. descriptor_(descriptor) {
  45. if (descriptor->is_map()) {
  46. // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some
  47. // error cases, so it seems to be ok to use as a back door for errors.
  48. std::cerr << "error: Extension is a map<>!"
  49. << " That used to be blocked by the compiler." << std::endl;
  50. std::cerr.flush();
  51. abort();
  52. }
  53. }
  54. ExtensionGenerator::~ExtensionGenerator() {}
  55. void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) {
  56. std::map<string, string> vars;
  57. vars["method_name"] = method_name_;
  58. SourceLocation location;
  59. if (descriptor_->GetSourceLocation(&location)) {
  60. vars["comments"] = BuildCommentsString(location, true);
  61. } else {
  62. vars["comments"] = "";
  63. }
  64. // Unlike normal message fields, check if the file for the extension was
  65. // deprecated.
  66. vars["deprecated_attribute"] = GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file());
  67. printer->Print(vars,
  68. "$comments$"
  69. "+ (GPBExtensionDescriptor *)$method_name$$deprecated_attribute$;\n");
  70. }
  71. void ExtensionGenerator::GenerateStaticVariablesInitialization(
  72. io::Printer* printer) {
  73. std::map<string, string> vars;
  74. vars["root_class_and_method_name"] = root_class_and_method_name_;
  75. vars["extended_type"] = ClassName(descriptor_->containing_type());
  76. vars["number"] = SimpleItoa(descriptor_->number());
  77. std::vector<string> options;
  78. if (descriptor_->is_repeated()) options.push_back("GPBExtensionRepeated");
  79. if (descriptor_->is_packed()) options.push_back("GPBExtensionPacked");
  80. if (descriptor_->containing_type()->options().message_set_wire_format())
  81. options.push_back("GPBExtensionSetWireFormat");
  82. vars["options"] = BuildFlagsString(FLAGTYPE_EXTENSION, options);
  83. ObjectiveCType objc_type = GetObjectiveCType(descriptor_);
  84. string singular_type;
  85. if (objc_type == OBJECTIVECTYPE_MESSAGE) {
  86. vars["type"] = string("GPBStringifySymbol(") +
  87. ClassName(descriptor_->message_type()) + ")";
  88. } else {
  89. vars["type"] = "NULL";
  90. }
  91. vars["default_name"] = GPBGenericValueFieldName(descriptor_);
  92. if (descriptor_->is_repeated()) {
  93. vars["default"] = "nil";
  94. } else {
  95. vars["default"] = DefaultValue(descriptor_);
  96. }
  97. string type = GetCapitalizedType(descriptor_);
  98. vars["extension_type"] = string("GPBDataType") + type;
  99. if (objc_type == OBJECTIVECTYPE_ENUM) {
  100. vars["enum_desc_func_name"] =
  101. EnumName(descriptor_->enum_type()) + "_EnumDescriptor";
  102. } else {
  103. vars["enum_desc_func_name"] = "NULL";
  104. }
  105. printer->Print(vars,
  106. "{\n"
  107. " .defaultValue.$default_name$ = $default$,\n"
  108. " .singletonName = GPBStringifySymbol($root_class_and_method_name$),\n"
  109. " .extendedClass = GPBStringifySymbol($extended_type$),\n"
  110. " .messageOrGroupClassName = $type$,\n"
  111. " .enumDescriptorFunc = $enum_desc_func_name$,\n"
  112. " .fieldNumber = $number$,\n"
  113. " .dataType = $extension_type$,\n"
  114. " .options = $options$,\n"
  115. "},\n");
  116. }
  117. void ExtensionGenerator::GenerateRegistrationSource(io::Printer* printer) {
  118. printer->Print(
  119. "[registry addExtension:$root_class_and_method_name$];\n",
  120. "root_class_and_method_name", root_class_and_method_name_);
  121. }
  122. } // namespace objectivec
  123. } // namespace compiler
  124. } // namespace protobuf
  125. } // namespace google