cpp_field.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/cpp/cpp_field.h>
  34. #include <memory>
  35. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  36. #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
  37. #include <google/protobuf/compiler/cpp/cpp_string_field.h>
  38. #include <google/protobuf/stubs/logging.h>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/compiler/cpp/cpp_enum_field.h>
  41. #include <google/protobuf/compiler/cpp/cpp_map_field.h>
  42. #include <google/protobuf/compiler/cpp/cpp_message_field.h>
  43. #include <google/protobuf/descriptor.pb.h>
  44. #include <google/protobuf/io/printer.h>
  45. #include <google/protobuf/wire_format.h>
  46. #include <google/protobuf/stubs/strutil.h>
  47. namespace google {
  48. namespace protobuf {
  49. namespace compiler {
  50. namespace cpp {
  51. using internal::WireFormat;
  52. void SetCommonFieldVariables(const FieldDescriptor* descriptor,
  53. std::map<string, string>* variables,
  54. const Options& options) {
  55. (*variables)["ns"] = Namespace(descriptor);
  56. (*variables)["name"] = FieldName(descriptor);
  57. (*variables)["index"] = SimpleItoa(descriptor->index());
  58. (*variables)["number"] = SimpleItoa(descriptor->number());
  59. (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
  60. (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
  61. (*variables)["field_member"] = FieldName(descriptor) + "_";
  62. (*variables)["tag_size"] = SimpleItoa(
  63. WireFormat::TagSize(descriptor->number(), descriptor->type()));
  64. (*variables)["deprecation"] = descriptor->options().deprecated()
  65. ? " PROTOBUF_DEPRECATED" : "";
  66. (*variables)["deprecated_attr"] = descriptor->options().deprecated()
  67. ? "GOOGLE_PROTOBUF_DEPRECATED_ATTR " : "";
  68. if (HasFieldPresence(descriptor->file())) {
  69. (*variables)["set_hasbit"] =
  70. "set_has_" + FieldName(descriptor) + "();";
  71. (*variables)["clear_hasbit"] =
  72. "clear_has_" + FieldName(descriptor) + "();";
  73. } else {
  74. (*variables)["set_hasbit"] = "";
  75. (*variables)["clear_hasbit"] = "";
  76. }
  77. // These variables are placeholders to pick out the beginning and ends of
  78. // identifiers for annotations (when doing so with existing variables would
  79. // be ambiguous or impossible). They should never be set to anything but the
  80. // empty string.
  81. (*variables)["{"] = "";
  82. (*variables)["}"] = "";
  83. }
  84. void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
  85. std::map<string, string>* variables) {
  86. const string prefix = descriptor->containing_oneof()->name() + "_.";
  87. (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
  88. (*variables)["field_member"] = StrCat(prefix, (*variables)["name"], "_");
  89. }
  90. FieldGenerator::~FieldGenerator() {}
  91. void FieldGenerator::
  92. GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
  93. // Reaching here indicates a bug. Cases are:
  94. // - This FieldGenerator should support packing, but this method should be
  95. // overridden.
  96. // - This FieldGenerator doesn't support packing, and this method should
  97. // never have been called.
  98. GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() "
  99. << "called on field generator that does not support packing.";
  100. }
  101. FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
  102. const Options& options,
  103. SCCAnalyzer* scc_analyzer)
  104. : descriptor_(descriptor),
  105. options_(options),
  106. field_generators_(descriptor->field_count()) {
  107. // Construct all the FieldGenerators.
  108. for (int i = 0; i < descriptor->field_count(); i++) {
  109. field_generators_[i].reset(
  110. MakeGenerator(descriptor->field(i), options, scc_analyzer));
  111. }
  112. }
  113. FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field,
  114. const Options& options,
  115. SCCAnalyzer* scc_analyzer) {
  116. if (field->is_repeated()) {
  117. switch (field->cpp_type()) {
  118. case FieldDescriptor::CPPTYPE_MESSAGE:
  119. if (field->is_map()) {
  120. return new MapFieldGenerator(field, options);
  121. } else {
  122. return new RepeatedMessageFieldGenerator(field, options,
  123. scc_analyzer);
  124. }
  125. case FieldDescriptor::CPPTYPE_STRING:
  126. switch (field->options().ctype()) {
  127. default: // RepeatedStringFieldGenerator handles unknown ctypes.
  128. case FieldOptions::STRING:
  129. return new RepeatedStringFieldGenerator(field, options);
  130. }
  131. case FieldDescriptor::CPPTYPE_ENUM:
  132. return new RepeatedEnumFieldGenerator(field, options);
  133. default:
  134. return new RepeatedPrimitiveFieldGenerator(field, options);
  135. }
  136. } else if (field->containing_oneof()) {
  137. switch (field->cpp_type()) {
  138. case FieldDescriptor::CPPTYPE_MESSAGE:
  139. return new MessageOneofFieldGenerator(field, options, scc_analyzer);
  140. case FieldDescriptor::CPPTYPE_STRING:
  141. switch (field->options().ctype()) {
  142. default: // StringOneofFieldGenerator handles unknown ctypes.
  143. case FieldOptions::STRING:
  144. return new StringOneofFieldGenerator(field, options);
  145. }
  146. case FieldDescriptor::CPPTYPE_ENUM:
  147. return new EnumOneofFieldGenerator(field, options);
  148. default:
  149. return new PrimitiveOneofFieldGenerator(field, options);
  150. }
  151. } else {
  152. switch (field->cpp_type()) {
  153. case FieldDescriptor::CPPTYPE_MESSAGE:
  154. return new MessageFieldGenerator(field, options, scc_analyzer);
  155. case FieldDescriptor::CPPTYPE_STRING:
  156. switch (field->options().ctype()) {
  157. default: // StringFieldGenerator handles unknown ctypes.
  158. case FieldOptions::STRING:
  159. return new StringFieldGenerator(field, options);
  160. }
  161. case FieldDescriptor::CPPTYPE_ENUM:
  162. return new EnumFieldGenerator(field, options);
  163. default:
  164. return new PrimitiveFieldGenerator(field, options);
  165. }
  166. }
  167. }
  168. FieldGeneratorMap::~FieldGeneratorMap() {}
  169. const FieldGenerator& FieldGeneratorMap::get(
  170. const FieldDescriptor* field) const {
  171. GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
  172. return *field_generators_[field->index()];
  173. }
  174. } // namespace cpp
  175. } // namespace compiler
  176. } // namespace protobuf
  177. } // namespace google