csharp_field_base.cc 13 KB

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