csharp_field_base.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <limits>
  31. #include <sstream>
  32. #include <google/protobuf/compiler/code_generator.h>
  33. #include <google/protobuf/compiler/plugin.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/descriptor.pb.h>
  36. #include <google/protobuf/io/printer.h>
  37. #include <google/protobuf/io/zero_copy_stream.h>
  38. #include <google/protobuf/stubs/strutil.h>
  39. #include <google/protobuf/compiler/csharp/csharp_field_base.h>
  40. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  41. #include <google/protobuf/compiler/csharp/csharp_writer.h>
  42. using google::protobuf::internal::scoped_ptr;
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace csharp {
  47. FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor,
  48. int fieldOrdinal)
  49. : SourceGeneratorBase(descriptor->file()),
  50. descriptor_(descriptor),
  51. fieldOrdinal_(fieldOrdinal) {
  52. }
  53. FieldGeneratorBase::~FieldGeneratorBase() {
  54. }
  55. void FieldGeneratorBase::AddDeprecatedFlag(Writer* writer) {
  56. if (descriptor_->options().deprecated())
  57. {
  58. writer->WriteLine("[global::System.ObsoleteAttribute()]");
  59. }
  60. }
  61. void FieldGeneratorBase::AddNullCheck(Writer* writer) {
  62. AddNullCheck(writer, "value");
  63. }
  64. void FieldGeneratorBase::AddNullCheck(Writer* writer, const std::string& name) {
  65. if (is_nullable_type()) {
  66. writer->WriteLine(" pb::ThrowHelper.ThrowIfNull($0$, \"$0$\");", name);
  67. }
  68. }
  69. void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) {
  70. AddDeprecatedFlag(writer);
  71. }
  72. std::string FieldGeneratorBase::property_name() {
  73. return GetPropertyName(descriptor_);
  74. }
  75. std::string FieldGeneratorBase::name() {
  76. return UnderscoresToCamelCase(GetFieldName(descriptor_), false);
  77. }
  78. std::string FieldGeneratorBase::type_name() {
  79. switch (descriptor_->type()) {
  80. case FieldDescriptor::TYPE_ENUM:
  81. return GetClassName(descriptor_->enum_type());
  82. case FieldDescriptor::TYPE_MESSAGE:
  83. case FieldDescriptor::TYPE_GROUP:
  84. return GetClassName(descriptor_->message_type());
  85. case FieldDescriptor::TYPE_DOUBLE:
  86. return "double";
  87. case FieldDescriptor::TYPE_FLOAT:
  88. return "float";
  89. case FieldDescriptor::TYPE_INT64:
  90. return "long";
  91. case FieldDescriptor::TYPE_UINT64:
  92. return "ulong";
  93. case FieldDescriptor::TYPE_INT32:
  94. return "int";
  95. case FieldDescriptor::TYPE_FIXED64:
  96. return "ulong";
  97. case FieldDescriptor::TYPE_FIXED32:
  98. return "uint";
  99. case FieldDescriptor::TYPE_BOOL:
  100. return "bool";
  101. case FieldDescriptor::TYPE_STRING:
  102. return "string";
  103. case FieldDescriptor::TYPE_BYTES:
  104. return "pb::ByteString";
  105. case FieldDescriptor::TYPE_UINT32:
  106. return "uint";
  107. case FieldDescriptor::TYPE_SFIXED32:
  108. return "int";
  109. case FieldDescriptor::TYPE_SFIXED64:
  110. return "long";
  111. case FieldDescriptor::TYPE_SINT32:
  112. return "int";
  113. case FieldDescriptor::TYPE_SINT64:
  114. return "long";
  115. default:
  116. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  117. return "";
  118. }
  119. }
  120. bool FieldGeneratorBase::has_default_value() {
  121. switch (descriptor_->type()) {
  122. case FieldDescriptor::TYPE_ENUM:
  123. case FieldDescriptor::TYPE_MESSAGE:
  124. case FieldDescriptor::TYPE_GROUP:
  125. return true;
  126. case FieldDescriptor::TYPE_DOUBLE:
  127. return descriptor_->default_value_double() != 0.0;
  128. case FieldDescriptor::TYPE_FLOAT:
  129. return descriptor_->default_value_float() != 0.0;
  130. case FieldDescriptor::TYPE_INT64:
  131. return descriptor_->default_value_int64() != 0L;
  132. case FieldDescriptor::TYPE_UINT64:
  133. return descriptor_->default_value_uint64() != 0L;
  134. case FieldDescriptor::TYPE_INT32:
  135. return descriptor_->default_value_int32() != 0;
  136. case FieldDescriptor::TYPE_FIXED64:
  137. return descriptor_->default_value_uint64() != 0L;
  138. case FieldDescriptor::TYPE_FIXED32:
  139. return descriptor_->default_value_uint32() != 0;
  140. case FieldDescriptor::TYPE_BOOL:
  141. return descriptor_->default_value_bool();
  142. case FieldDescriptor::TYPE_STRING:
  143. return true;
  144. case FieldDescriptor::TYPE_BYTES:
  145. return true;
  146. case FieldDescriptor::TYPE_UINT32:
  147. return descriptor_->default_value_uint32() != 0;
  148. case FieldDescriptor::TYPE_SFIXED32:
  149. return descriptor_->default_value_int32() != 0;
  150. case FieldDescriptor::TYPE_SFIXED64:
  151. return descriptor_->default_value_int64() != 0L;
  152. case FieldDescriptor::TYPE_SINT32:
  153. return descriptor_->default_value_int32() != 0;
  154. case FieldDescriptor::TYPE_SINT64:
  155. return descriptor_->default_value_int64() != 0L;
  156. default:
  157. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  158. return true;
  159. }
  160. }
  161. bool FieldGeneratorBase::is_nullable_type() {
  162. switch (descriptor_->type()) {
  163. case FieldDescriptor::TYPE_ENUM:
  164. case FieldDescriptor::TYPE_DOUBLE:
  165. case FieldDescriptor::TYPE_FLOAT:
  166. case FieldDescriptor::TYPE_INT64:
  167. case FieldDescriptor::TYPE_UINT64:
  168. case FieldDescriptor::TYPE_INT32:
  169. case FieldDescriptor::TYPE_FIXED64:
  170. case FieldDescriptor::TYPE_FIXED32:
  171. case FieldDescriptor::TYPE_BOOL:
  172. case FieldDescriptor::TYPE_UINT32:
  173. case FieldDescriptor::TYPE_SFIXED32:
  174. case FieldDescriptor::TYPE_SFIXED64:
  175. case FieldDescriptor::TYPE_SINT32:
  176. case FieldDescriptor::TYPE_SINT64:
  177. return false;
  178. case FieldDescriptor::TYPE_MESSAGE:
  179. case FieldDescriptor::TYPE_GROUP:
  180. case FieldDescriptor::TYPE_STRING:
  181. case FieldDescriptor::TYPE_BYTES:
  182. return true;
  183. default:
  184. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  185. return true;
  186. }
  187. }
  188. inline bool IsNaN(double value) {
  189. // NaN is never equal to anything, even itself.
  190. return value != value;
  191. }
  192. bool AllPrintableAscii(const std::string& text) {
  193. for(int i = 0; i < text.size(); i++) {
  194. if (text[i] < 0x20 || text[i] > 0x7e) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. std::string FieldGeneratorBase::GetStringDefaultValueInternal() {
  201. if (!descriptor_->has_default_value()) {
  202. return "\"\"";
  203. }
  204. if (AllPrintableAscii(descriptor_->default_value_string())) {
  205. // All chars are ASCII and printable. In this case we only
  206. // need to escape quotes and backslashes.
  207. std::string temp = descriptor_->default_value_string();
  208. temp = StringReplace(temp, "\\", "\\\\", true);
  209. temp = StringReplace(temp, "'", "\\'", true);
  210. temp = StringReplace(temp, "\"", "\\\"", true);
  211. return "\"" + temp + "\"";
  212. }
  213. if (use_lite_runtime()) {
  214. return "pb::ByteString.FromBase64(\""
  215. + StringToBase64(descriptor_->default_value_string())
  216. + "\").ToStringUtf8()";
  217. }
  218. return "(string) " + GetClassName(descriptor_->containing_type())
  219. + ".Descriptor.Fields[" + SimpleItoa(descriptor_->index())
  220. + "].DefaultValue";
  221. }
  222. std::string FieldGeneratorBase::GetBytesDefaultValueInternal() {
  223. if (!descriptor_->has_default_value()) {
  224. return "pb::ByteString.Empty";
  225. }
  226. if (use_lite_runtime()) {
  227. return "pb::ByteString.FromBase64(\"" + StringToBase64(descriptor_->default_value_string()) + "\")";
  228. }
  229. return "(pb::ByteString) "+ GetClassName(descriptor_->containing_type()) +
  230. ".Descriptor.Fields[" + SimpleItoa(descriptor_->index()) + "].DefaultValue";
  231. }
  232. std::string FieldGeneratorBase::default_value() {
  233. switch (descriptor_->type()) {
  234. case FieldDescriptor::TYPE_ENUM:
  235. return type_name() + "." + descriptor_->default_value_enum()->name();
  236. case FieldDescriptor::TYPE_MESSAGE:
  237. case FieldDescriptor::TYPE_GROUP:
  238. return type_name() + ".DefaultInstance";
  239. case FieldDescriptor::TYPE_DOUBLE: {
  240. double value = descriptor_->default_value_double();
  241. if (value == numeric_limits<double>::infinity()) {
  242. return "double.PositiveInfinity";
  243. } else if (value == -numeric_limits<double>::infinity()) {
  244. return "double.NegativeInfinity";
  245. } else if (IsNaN(value)) {
  246. return "double.NaN";
  247. }
  248. return SimpleDtoa(value) + "D";
  249. }
  250. case FieldDescriptor::TYPE_FLOAT: {
  251. float value = descriptor_->default_value_float();
  252. if (value == numeric_limits<float>::infinity()) {
  253. return "float.PositiveInfinity";
  254. } else if (value == -numeric_limits<float>::infinity()) {
  255. return "float.NegativeInfinity";
  256. } else if (IsNaN(value)) {
  257. return "float.NaN";
  258. }
  259. return SimpleFtoa(value) + "F";
  260. }
  261. case FieldDescriptor::TYPE_INT64:
  262. return SimpleItoa(descriptor_->default_value_int64()) + "L";
  263. case FieldDescriptor::TYPE_UINT64:
  264. return SimpleItoa(descriptor_->default_value_uint64()) + "UL";
  265. case FieldDescriptor::TYPE_INT32:
  266. return SimpleItoa(descriptor_->default_value_int32());
  267. case FieldDescriptor::TYPE_FIXED64:
  268. return SimpleItoa(descriptor_->default_value_uint64()) + "UL";
  269. case FieldDescriptor::TYPE_FIXED32:
  270. return SimpleItoa(descriptor_->default_value_uint32());
  271. case FieldDescriptor::TYPE_BOOL:
  272. if (descriptor_->default_value_bool()) {
  273. return "true";
  274. } else {
  275. return "false";
  276. }
  277. case FieldDescriptor::TYPE_STRING:
  278. return GetStringDefaultValueInternal();
  279. case FieldDescriptor::TYPE_BYTES:
  280. return GetBytesDefaultValueInternal();
  281. case FieldDescriptor::TYPE_UINT32:
  282. return SimpleItoa(descriptor_->default_value_uint32());
  283. case FieldDescriptor::TYPE_SFIXED32:
  284. return SimpleItoa(descriptor_->default_value_int32());
  285. case FieldDescriptor::TYPE_SFIXED64:
  286. return SimpleItoa(descriptor_->default_value_int64()) + "L";
  287. case FieldDescriptor::TYPE_SINT32:
  288. return SimpleItoa(descriptor_->default_value_int32());
  289. case FieldDescriptor::TYPE_SINT64:
  290. return SimpleItoa(descriptor_->default_value_int64()) + "L";
  291. default:
  292. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  293. return "";
  294. }
  295. }
  296. std::string FieldGeneratorBase::number() {
  297. return SimpleItoa(descriptor_->number());
  298. }
  299. std::string FieldGeneratorBase::message_or_group() {
  300. return
  301. (descriptor_->type() == FieldDescriptor::TYPE_GROUP) ? "Group" : "Message";
  302. }
  303. std::string FieldGeneratorBase::capitalized_type_name() {
  304. switch (descriptor_->type()) {
  305. case FieldDescriptor::TYPE_ENUM:
  306. return "Enum";
  307. case FieldDescriptor::TYPE_MESSAGE:
  308. return "Message";
  309. case FieldDescriptor::TYPE_GROUP:
  310. return "Group";
  311. case FieldDescriptor::TYPE_DOUBLE:
  312. return "Double";
  313. case FieldDescriptor::TYPE_FLOAT:
  314. return "Float";
  315. case FieldDescriptor::TYPE_INT64:
  316. return "Int64";
  317. case FieldDescriptor::TYPE_UINT64:
  318. return "UInt64";
  319. case FieldDescriptor::TYPE_INT32:
  320. return "Int32";
  321. case FieldDescriptor::TYPE_FIXED64:
  322. return "Fixed64";
  323. case FieldDescriptor::TYPE_FIXED32:
  324. return "Fixed32";
  325. case FieldDescriptor::TYPE_BOOL:
  326. return "Bool";
  327. case FieldDescriptor::TYPE_STRING:
  328. return "String";
  329. case FieldDescriptor::TYPE_BYTES:
  330. return "Bytes";
  331. case FieldDescriptor::TYPE_UINT32:
  332. return "UInt32";
  333. case FieldDescriptor::TYPE_SFIXED32:
  334. return "SFixed32";
  335. case FieldDescriptor::TYPE_SFIXED64:
  336. return "SFixed64";
  337. case FieldDescriptor::TYPE_SINT32:
  338. return "SInt32";
  339. case FieldDescriptor::TYPE_SINT64:
  340. return "SInt64";
  341. default:
  342. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  343. return "";
  344. }
  345. }
  346. std::string FieldGeneratorBase::field_ordinal() {
  347. return SimpleItoa(fieldOrdinal_);
  348. }
  349. } // namespace csharp
  350. } // namespace compiler
  351. } // namespace protobuf
  352. } // namespace google