javanano_field.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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/javanano/javanano_field.h>
  34. #include <google/protobuf/compiler/javanano/javanano_helpers.h>
  35. #include <google/protobuf/compiler/javanano/javanano_primitive_field.h>
  36. #include <google/protobuf/compiler/javanano/javanano_enum_field.h>
  37. #include <google/protobuf/compiler/javanano/javanano_map_field.h>
  38. #include <google/protobuf/compiler/javanano/javanano_message_field.h>
  39. #include <google/protobuf/stubs/common.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace javanano {
  44. FieldGenerator::~FieldGenerator() {}
  45. bool FieldGenerator::SavedDefaultNeeded() const {
  46. // No saved default for this field by default.
  47. // Subclasses whose instances may need saved defaults will override this
  48. // and return the appropriate value.
  49. return false;
  50. }
  51. void FieldGenerator::GenerateInitSavedDefaultCode(io::Printer* printer) const {
  52. // No saved default for this field by default.
  53. // Subclasses whose instances may need saved defaults will override this
  54. // and generate the appropriate init code to the printer.
  55. }
  56. void FieldGenerator::GenerateMergingCodeFromPacked(io::Printer* printer) const {
  57. // Reaching here indicates a bug. Cases are:
  58. // - This FieldGenerator should support packing, but this method should be
  59. // overridden.
  60. // - This FieldGenerator doesn't support packing, and this method should
  61. // never have been called.
  62. GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
  63. << "called on field generator that does not support packing.";
  64. }
  65. // =============================================
  66. FieldGeneratorMap::FieldGeneratorMap(
  67. const Descriptor* descriptor, const Params &params)
  68. : descriptor_(descriptor),
  69. field_generators_(
  70. new scoped_ptr<FieldGenerator>[descriptor->field_count()]) {
  71. int next_has_bit_index = 0;
  72. bool saved_defaults_needed = false;
  73. // Construct all the FieldGenerators.
  74. for (int i = 0; i < descriptor->field_count(); i++) {
  75. FieldGenerator* field_generator = MakeGenerator(
  76. descriptor->field(i), params, &next_has_bit_index);
  77. saved_defaults_needed = saved_defaults_needed
  78. || field_generator->SavedDefaultNeeded();
  79. field_generators_[i].reset(field_generator);
  80. }
  81. total_bits_ = next_has_bit_index;
  82. saved_defaults_needed_ = saved_defaults_needed;
  83. }
  84. FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field,
  85. const Params &params, int* next_has_bit_index) {
  86. JavaType java_type = GetJavaType(field);
  87. if (field->is_repeated()) {
  88. switch (java_type) {
  89. case JAVATYPE_MESSAGE:
  90. if (IsMapEntry(field->message_type())) {
  91. return new MapFieldGenerator(field, params);
  92. } else {
  93. return new RepeatedMessageFieldGenerator(field, params);
  94. }
  95. case JAVATYPE_ENUM:
  96. return new RepeatedEnumFieldGenerator(field, params);
  97. default:
  98. return new RepeatedPrimitiveFieldGenerator(field, params);
  99. }
  100. } else if (field->containing_oneof()) {
  101. switch (java_type) {
  102. case JAVATYPE_MESSAGE:
  103. return new MessageOneofFieldGenerator(field, params);
  104. case JAVATYPE_ENUM:
  105. default:
  106. return new PrimitiveOneofFieldGenerator(field, params);
  107. }
  108. } else if (params.optional_field_accessors() && field->is_optional()
  109. && java_type != JAVATYPE_MESSAGE) {
  110. // We need a has-bit for each primitive/enum field because their default
  111. // values could be same as explicitly set values. But we don't need it
  112. // for a message field because they have no defaults and Nano uses 'null'
  113. // for unset messages, which cannot be set explicitly.
  114. switch (java_type) {
  115. case JAVATYPE_ENUM:
  116. return new AccessorEnumFieldGenerator(
  117. field, params, (*next_has_bit_index)++);
  118. default:
  119. return new AccessorPrimitiveFieldGenerator(
  120. field, params, (*next_has_bit_index)++);
  121. }
  122. } else {
  123. switch (java_type) {
  124. case JAVATYPE_MESSAGE:
  125. return new MessageFieldGenerator(field, params);
  126. case JAVATYPE_ENUM:
  127. return new EnumFieldGenerator(field, params);
  128. default:
  129. return new PrimitiveFieldGenerator(field, params);
  130. }
  131. }
  132. }
  133. FieldGeneratorMap::~FieldGeneratorMap() {}
  134. const FieldGenerator& FieldGeneratorMap::get(
  135. const FieldDescriptor* field) const {
  136. GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
  137. return *field_generators_[field->index()];
  138. }
  139. void SetCommonOneofVariables(const FieldDescriptor* descriptor,
  140. map<string, string>* variables) {
  141. (*variables)["oneof_name"] =
  142. UnderscoresToCamelCase(descriptor->containing_oneof());
  143. (*variables)["oneof_capitalized_name"] =
  144. UnderscoresToCapitalizedCamelCase(descriptor->containing_oneof());
  145. (*variables)["oneof_index"] =
  146. SimpleItoa(descriptor->containing_oneof()->index());
  147. (*variables)["set_oneof_case"] =
  148. (*variables)["oneof_name"] + "Case_ = " + SimpleItoa(descriptor->number());
  149. (*variables)["clear_oneof_case"] =
  150. (*variables)["oneof_name"] + "Case_ = 0";
  151. (*variables)["has_oneof_case"] =
  152. (*variables)["oneof_name"] + "Case_ == " + SimpleItoa(descriptor->number());
  153. }
  154. } // namespace javanano
  155. } // namespace compiler
  156. } // namespace protobuf
  157. } // namespace google