objectivec_primitive_field.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_primitive_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/wire_format_lite_inl.h>
  38. #include <google/protobuf/stubs/strutil.h>
  39. #include <google/protobuf/stubs/substitute.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace objectivec {
  44. using internal::WireFormat;
  45. using internal::WireFormatLite;
  46. namespace {
  47. const char* PrimitiveTypeName(const FieldDescriptor* descriptor) {
  48. ObjectiveCType type = GetObjectiveCType(descriptor);
  49. switch (type) {
  50. case OBJECTIVECTYPE_INT32:
  51. return "int32_t";
  52. case OBJECTIVECTYPE_UINT32:
  53. return "uint32_t";
  54. case OBJECTIVECTYPE_INT64:
  55. return "int64_t";
  56. case OBJECTIVECTYPE_UINT64:
  57. return "uint64_t";
  58. case OBJECTIVECTYPE_FLOAT:
  59. return "float";
  60. case OBJECTIVECTYPE_DOUBLE:
  61. return "double";
  62. case OBJECTIVECTYPE_BOOLEAN:
  63. return "BOOL";
  64. case OBJECTIVECTYPE_STRING:
  65. return "NSString";
  66. case OBJECTIVECTYPE_DATA:
  67. return "NSData";
  68. case OBJECTIVECTYPE_ENUM:
  69. return "int32_t";
  70. case OBJECTIVECTYPE_MESSAGE:
  71. return NULL; // Messages go through objectivec_message_field.cc|h.
  72. }
  73. // Some compilers report reaching end of function even though all cases of
  74. // the enum are handed in the switch.
  75. GOOGLE_LOG(FATAL) << "Can't get here.";
  76. return NULL;
  77. }
  78. const char* PrimitiveArrayTypeName(const FieldDescriptor* descriptor) {
  79. ObjectiveCType type = GetObjectiveCType(descriptor);
  80. switch (type) {
  81. case OBJECTIVECTYPE_INT32:
  82. return "Int32";
  83. case OBJECTIVECTYPE_UINT32:
  84. return "UInt32";
  85. case OBJECTIVECTYPE_INT64:
  86. return "Int64";
  87. case OBJECTIVECTYPE_UINT64:
  88. return "UInt64";
  89. case OBJECTIVECTYPE_FLOAT:
  90. return "Float";
  91. case OBJECTIVECTYPE_DOUBLE:
  92. return "Double";
  93. case OBJECTIVECTYPE_BOOLEAN:
  94. return "Bool";
  95. case OBJECTIVECTYPE_STRING:
  96. return ""; // Want NSArray
  97. case OBJECTIVECTYPE_DATA:
  98. return ""; // Want NSArray
  99. case OBJECTIVECTYPE_ENUM:
  100. return "Enum";
  101. case OBJECTIVECTYPE_MESSAGE:
  102. // Want NSArray (but goes through objectivec_message_field.cc|h).
  103. return "";
  104. }
  105. // Some compilers report reaching end of function even though all cases of
  106. // the enum are handed in the switch.
  107. GOOGLE_LOG(FATAL) << "Can't get here.";
  108. return NULL;
  109. }
  110. void SetPrimitiveVariables(const FieldDescriptor* descriptor,
  111. std::map<string, string>* variables) {
  112. std::string primitive_name = PrimitiveTypeName(descriptor);
  113. (*variables)["type"] = primitive_name;
  114. (*variables)["storage_type"] = primitive_name;
  115. }
  116. } // namespace
  117. PrimitiveFieldGenerator::PrimitiveFieldGenerator(
  118. const FieldDescriptor* descriptor, const Options& options)
  119. : SingleFieldGenerator(descriptor, options) {
  120. SetPrimitiveVariables(descriptor, &variables_);
  121. }
  122. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  123. void PrimitiveFieldGenerator::GenerateFieldStorageDeclaration(
  124. io::Printer* printer) const {
  125. if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
  126. // Nothing, BOOLs are stored in the has bits.
  127. } else {
  128. SingleFieldGenerator::GenerateFieldStorageDeclaration(printer);
  129. }
  130. }
  131. int PrimitiveFieldGenerator::ExtraRuntimeHasBitsNeeded(void) const {
  132. if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
  133. // Reserve a bit for the storage of the boolean.
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int has_base) {
  139. if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
  140. // Set into the offset the has bit to use for the actual value.
  141. variables_["storage_offset_value"] = SimpleItoa(has_base);
  142. variables_["storage_offset_comment"] =
  143. " // Stored in _has_storage_ to save space.";
  144. }
  145. }
  146. PrimitiveObjFieldGenerator::PrimitiveObjFieldGenerator(
  147. const FieldDescriptor* descriptor, const Options& options)
  148. : ObjCObjFieldGenerator(descriptor, options) {
  149. SetPrimitiveVariables(descriptor, &variables_);
  150. variables_["property_storage_attribute"] = "copy";
  151. }
  152. PrimitiveObjFieldGenerator::~PrimitiveObjFieldGenerator() {}
  153. RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
  154. const FieldDescriptor* descriptor, const Options& options)
  155. : RepeatedFieldGenerator(descriptor, options) {
  156. SetPrimitiveVariables(descriptor, &variables_);
  157. string base_name = PrimitiveArrayTypeName(descriptor);
  158. if (base_name.length()) {
  159. variables_["array_storage_type"] = "GPB" + base_name + "Array";
  160. } else {
  161. variables_["array_storage_type"] = "NSMutableArray";
  162. variables_["array_property_type"] =
  163. "NSMutableArray<" + variables_["storage_type"] + "*>";
  164. }
  165. }
  166. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  167. } // namespace objectivec
  168. } // namespace compiler
  169. } // namespace protobuf
  170. } // namespace google