csharp_field_base.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/coded_stream.h>
  37. #include <google/protobuf/io/printer.h>
  38. #include <google/protobuf/io/zero_copy_stream.h>
  39. #include <google/protobuf/stubs/mathlimits.h>
  40. #include <google/protobuf/stubs/strutil.h>
  41. #include <google/protobuf/wire_format.h>
  42. #include <google/protobuf/compiler/csharp/csharp_field_base.h>
  43. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  44. #include <google/protobuf/compiler/csharp/csharp_names.h>
  45. using google::protobuf::internal::scoped_ptr;
  46. namespace google {
  47. namespace protobuf {
  48. namespace compiler {
  49. namespace csharp {
  50. void FieldGeneratorBase::SetCommonFieldVariables(
  51. map<string, string>* variables) {
  52. // Note: this will be valid even though the tag emitted for packed and unpacked versions of
  53. // repeated fields varies by wire format. The wire format is encoded in the bottom 3 bits, which
  54. // never effects the tag size.
  55. int tag_size = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type());
  56. uint tag = internal::WireFormat::MakeTag(descriptor_);
  57. uint8 tag_array[5];
  58. io::CodedOutputStream::WriteTagToArray(tag, tag_array);
  59. string tag_bytes = SimpleItoa(tag_array[0]);
  60. for (int i = 1; i < tag_size; i++) {
  61. tag_bytes += ", " + SimpleItoa(tag_array[i]);
  62. }
  63. (*variables)["access_level"] = "public";
  64. (*variables)["tag"] = SimpleItoa(tag);
  65. (*variables)["tag_size"] = SimpleItoa(tag_size);
  66. (*variables)["tag_bytes"] = tag_bytes;
  67. (*variables)["property_name"] = property_name();
  68. (*variables)["type_name"] = type_name();
  69. (*variables)["name"] = name();
  70. (*variables)["descriptor_name"] = descriptor_->name();
  71. (*variables)["default_value"] = default_value();
  72. if (has_default_value()) {
  73. (*variables)["name_def_message"] =
  74. (*variables)["name"] + "_ = " + (*variables)["default_value"];
  75. } else {
  76. (*variables)["name_def_message"] = (*variables)["name"] + "_";
  77. }
  78. (*variables)["capitalized_type_name"] = capitalized_type_name();
  79. (*variables)["number"] = number();
  80. (*variables)["has_property_check"] =
  81. (*variables)["property_name"] + " != " + (*variables)["default_value"];
  82. (*variables)["other_has_property_check"] = "other." +
  83. (*variables)["property_name"] + " != " + (*variables)["default_value"];
  84. }
  85. void FieldGeneratorBase::SetCommonOneofFieldVariables(
  86. map<string, string>* variables) {
  87. (*variables)["oneof_name"] = oneof_name();
  88. (*variables)["has_property_check"] =
  89. oneof_name() + "Case_ == " + oneof_property_name() +
  90. "OneofCase." + property_name();
  91. (*variables)["oneof_property_name"] = oneof_property_name();
  92. }
  93. FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor,
  94. int fieldOrdinal, const Options* options)
  95. : SourceGeneratorBase(descriptor->file(), options),
  96. descriptor_(descriptor),
  97. fieldOrdinal_(fieldOrdinal) {
  98. SetCommonFieldVariables(&variables_);
  99. }
  100. FieldGeneratorBase::~FieldGeneratorBase() {
  101. }
  102. void FieldGeneratorBase::GenerateFreezingCode(io::Printer* printer) {
  103. // No-op: only message fields and repeated fields need
  104. // special handling for freezing, so default to not generating any code.
  105. }
  106. void FieldGeneratorBase::GenerateCodecCode(io::Printer* printer) {
  107. // No-op: expect this to be overridden by appropriate types.
  108. // Could fail if we get called here though...
  109. }
  110. void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) {
  111. if (descriptor_->options().deprecated()) {
  112. printer->Print("[global::System.ObsoleteAttribute]\n");
  113. } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE &&
  114. descriptor_->message_type()->options().deprecated()) {
  115. printer->Print("[global::System.ObsoleteAttribute]\n");
  116. }
  117. }
  118. void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) {
  119. AddDeprecatedFlag(printer);
  120. WriteGeneratedCodeAttributes(printer);
  121. }
  122. std::string FieldGeneratorBase::oneof_property_name() {
  123. return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), true);
  124. }
  125. std::string FieldGeneratorBase::oneof_name() {
  126. return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), false);
  127. }
  128. std::string FieldGeneratorBase::property_name() {
  129. return GetPropertyName(descriptor_);
  130. }
  131. std::string FieldGeneratorBase::name() {
  132. return UnderscoresToCamelCase(GetFieldName(descriptor_), false);
  133. }
  134. std::string FieldGeneratorBase::type_name() {
  135. return type_name(descriptor_);
  136. }
  137. std::string FieldGeneratorBase::type_name(const FieldDescriptor* descriptor) {
  138. switch (descriptor->type()) {
  139. case FieldDescriptor::TYPE_ENUM:
  140. return GetClassName(descriptor->enum_type());
  141. case FieldDescriptor::TYPE_MESSAGE:
  142. case FieldDescriptor::TYPE_GROUP:
  143. if (IsWrapperType(descriptor)) {
  144. const FieldDescriptor* wrapped_field =
  145. descriptor->message_type()->field(0);
  146. string wrapped_field_type_name = type_name(wrapped_field);
  147. // String and ByteString go to the same type; other wrapped types
  148. // go to the nullable equivalent.
  149. if (wrapped_field->type() == FieldDescriptor::TYPE_STRING ||
  150. wrapped_field->type() == FieldDescriptor::TYPE_BYTES) {
  151. return wrapped_field_type_name;
  152. } else {
  153. return wrapped_field_type_name + "?";
  154. }
  155. }
  156. return GetClassName(descriptor->message_type());
  157. case FieldDescriptor::TYPE_DOUBLE:
  158. return "double";
  159. case FieldDescriptor::TYPE_FLOAT:
  160. return "float";
  161. case FieldDescriptor::TYPE_INT64:
  162. return "long";
  163. case FieldDescriptor::TYPE_UINT64:
  164. return "ulong";
  165. case FieldDescriptor::TYPE_INT32:
  166. return "int";
  167. case FieldDescriptor::TYPE_FIXED64:
  168. return "ulong";
  169. case FieldDescriptor::TYPE_FIXED32:
  170. return "uint";
  171. case FieldDescriptor::TYPE_BOOL:
  172. return "bool";
  173. case FieldDescriptor::TYPE_STRING:
  174. return "string";
  175. case FieldDescriptor::TYPE_BYTES:
  176. return "pb::ByteString";
  177. case FieldDescriptor::TYPE_UINT32:
  178. return "uint";
  179. case FieldDescriptor::TYPE_SFIXED32:
  180. return "int";
  181. case FieldDescriptor::TYPE_SFIXED64:
  182. return "long";
  183. case FieldDescriptor::TYPE_SINT32:
  184. return "int";
  185. case FieldDescriptor::TYPE_SINT64:
  186. return "long";
  187. default:
  188. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  189. return "";
  190. }
  191. }
  192. bool FieldGeneratorBase::has_default_value() {
  193. switch (descriptor_->type()) {
  194. case FieldDescriptor::TYPE_ENUM:
  195. case FieldDescriptor::TYPE_MESSAGE:
  196. case FieldDescriptor::TYPE_GROUP:
  197. return true;
  198. case FieldDescriptor::TYPE_DOUBLE:
  199. return descriptor_->default_value_double() != 0.0;
  200. case FieldDescriptor::TYPE_FLOAT:
  201. return descriptor_->default_value_float() != 0.0;
  202. case FieldDescriptor::TYPE_INT64:
  203. return descriptor_->default_value_int64() != 0L;
  204. case FieldDescriptor::TYPE_UINT64:
  205. return descriptor_->default_value_uint64() != 0L;
  206. case FieldDescriptor::TYPE_INT32:
  207. return descriptor_->default_value_int32() != 0;
  208. case FieldDescriptor::TYPE_FIXED64:
  209. return descriptor_->default_value_uint64() != 0L;
  210. case FieldDescriptor::TYPE_FIXED32:
  211. return descriptor_->default_value_uint32() != 0;
  212. case FieldDescriptor::TYPE_BOOL:
  213. return descriptor_->default_value_bool();
  214. case FieldDescriptor::TYPE_STRING:
  215. return true;
  216. case FieldDescriptor::TYPE_BYTES:
  217. return true;
  218. case FieldDescriptor::TYPE_UINT32:
  219. return descriptor_->default_value_uint32() != 0;
  220. case FieldDescriptor::TYPE_SFIXED32:
  221. return descriptor_->default_value_int32() != 0;
  222. case FieldDescriptor::TYPE_SFIXED64:
  223. return descriptor_->default_value_int64() != 0L;
  224. case FieldDescriptor::TYPE_SINT32:
  225. return descriptor_->default_value_int32() != 0;
  226. case FieldDescriptor::TYPE_SINT64:
  227. return descriptor_->default_value_int64() != 0L;
  228. default:
  229. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  230. return true;
  231. }
  232. }
  233. bool FieldGeneratorBase::is_nullable_type() {
  234. switch (descriptor_->type()) {
  235. case FieldDescriptor::TYPE_ENUM:
  236. case FieldDescriptor::TYPE_DOUBLE:
  237. case FieldDescriptor::TYPE_FLOAT:
  238. case FieldDescriptor::TYPE_INT64:
  239. case FieldDescriptor::TYPE_UINT64:
  240. case FieldDescriptor::TYPE_INT32:
  241. case FieldDescriptor::TYPE_FIXED64:
  242. case FieldDescriptor::TYPE_FIXED32:
  243. case FieldDescriptor::TYPE_BOOL:
  244. case FieldDescriptor::TYPE_UINT32:
  245. case FieldDescriptor::TYPE_SFIXED32:
  246. case FieldDescriptor::TYPE_SFIXED64:
  247. case FieldDescriptor::TYPE_SINT32:
  248. case FieldDescriptor::TYPE_SINT64:
  249. return false;
  250. case FieldDescriptor::TYPE_MESSAGE:
  251. case FieldDescriptor::TYPE_GROUP:
  252. case FieldDescriptor::TYPE_STRING:
  253. case FieldDescriptor::TYPE_BYTES:
  254. return true;
  255. default:
  256. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  257. return true;
  258. }
  259. }
  260. bool AllPrintableAscii(const std::string& text) {
  261. for(int i = 0; i < text.size(); i++) {
  262. if (text[i] < 0x20 || text[i] > 0x7e) {
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. std::string FieldGeneratorBase::GetStringDefaultValueInternal() {
  269. // No other default values needed for proto3...
  270. return "\"\"";
  271. }
  272. std::string FieldGeneratorBase::GetBytesDefaultValueInternal() {
  273. // No other default values needed for proto3...
  274. return "pb::ByteString.Empty";
  275. }
  276. std::string FieldGeneratorBase::default_value() {
  277. return default_value(descriptor_);
  278. }
  279. std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) {
  280. switch (descriptor->type()) {
  281. case FieldDescriptor::TYPE_ENUM:
  282. // All proto3 enums have a default value of 0, and there's an implicit conversion from the constant 0 to
  283. // any C# enum. This means we don't need to work out what we actually mapped the enum value name to.
  284. return "0";
  285. case FieldDescriptor::TYPE_MESSAGE:
  286. case FieldDescriptor::TYPE_GROUP:
  287. if (IsWrapperType(descriptor)) {
  288. const FieldDescriptor* wrapped_field = descriptor->message_type()->field(0);
  289. return default_value(wrapped_field);
  290. } else {
  291. return "null";
  292. }
  293. case FieldDescriptor::TYPE_DOUBLE: {
  294. double value = descriptor->default_value_double();
  295. if (value == std::numeric_limits<double>::infinity()) {
  296. return "double.PositiveInfinity";
  297. } else if (value == -std::numeric_limits<double>::infinity()) {
  298. return "double.NegativeInfinity";
  299. } else if (MathLimits<double>::IsNaN(value)) {
  300. return "double.NaN";
  301. }
  302. return SimpleDtoa(value) + "D";
  303. }
  304. case FieldDescriptor::TYPE_FLOAT: {
  305. float value = descriptor->default_value_float();
  306. if (value == std::numeric_limits<float>::infinity()) {
  307. return "float.PositiveInfinity";
  308. } else if (value == -std::numeric_limits<float>::infinity()) {
  309. return "float.NegativeInfinity";
  310. } else if (MathLimits<float>::IsNaN(value)) {
  311. return "float.NaN";
  312. }
  313. return SimpleFtoa(value) + "F";
  314. }
  315. case FieldDescriptor::TYPE_INT64:
  316. return SimpleItoa(descriptor->default_value_int64()) + "L";
  317. case FieldDescriptor::TYPE_UINT64:
  318. return SimpleItoa(descriptor->default_value_uint64()) + "UL";
  319. case FieldDescriptor::TYPE_INT32:
  320. return SimpleItoa(descriptor->default_value_int32());
  321. case FieldDescriptor::TYPE_FIXED64:
  322. return SimpleItoa(descriptor->default_value_uint64()) + "UL";
  323. case FieldDescriptor::TYPE_FIXED32:
  324. return SimpleItoa(descriptor->default_value_uint32());
  325. case FieldDescriptor::TYPE_BOOL:
  326. if (descriptor->default_value_bool()) {
  327. return "true";
  328. } else {
  329. return "false";
  330. }
  331. case FieldDescriptor::TYPE_STRING:
  332. return GetStringDefaultValueInternal();
  333. case FieldDescriptor::TYPE_BYTES:
  334. return GetBytesDefaultValueInternal();
  335. case FieldDescriptor::TYPE_UINT32:
  336. return SimpleItoa(descriptor->default_value_uint32());
  337. case FieldDescriptor::TYPE_SFIXED32:
  338. return SimpleItoa(descriptor->default_value_int32());
  339. case FieldDescriptor::TYPE_SFIXED64:
  340. return SimpleItoa(descriptor->default_value_int64()) + "L";
  341. case FieldDescriptor::TYPE_SINT32:
  342. return SimpleItoa(descriptor->default_value_int32());
  343. case FieldDescriptor::TYPE_SINT64:
  344. return SimpleItoa(descriptor->default_value_int64()) + "L";
  345. default:
  346. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  347. return "";
  348. }
  349. }
  350. std::string FieldGeneratorBase::number() {
  351. return SimpleItoa(descriptor_->number());
  352. }
  353. std::string FieldGeneratorBase::capitalized_type_name() {
  354. switch (descriptor_->type()) {
  355. case FieldDescriptor::TYPE_ENUM:
  356. return "Enum";
  357. case FieldDescriptor::TYPE_MESSAGE:
  358. return "Message";
  359. case FieldDescriptor::TYPE_GROUP:
  360. return "Group";
  361. case FieldDescriptor::TYPE_DOUBLE:
  362. return "Double";
  363. case FieldDescriptor::TYPE_FLOAT:
  364. return "Float";
  365. case FieldDescriptor::TYPE_INT64:
  366. return "Int64";
  367. case FieldDescriptor::TYPE_UINT64:
  368. return "UInt64";
  369. case FieldDescriptor::TYPE_INT32:
  370. return "Int32";
  371. case FieldDescriptor::TYPE_FIXED64:
  372. return "Fixed64";
  373. case FieldDescriptor::TYPE_FIXED32:
  374. return "Fixed32";
  375. case FieldDescriptor::TYPE_BOOL:
  376. return "Bool";
  377. case FieldDescriptor::TYPE_STRING:
  378. return "String";
  379. case FieldDescriptor::TYPE_BYTES:
  380. return "Bytes";
  381. case FieldDescriptor::TYPE_UINT32:
  382. return "UInt32";
  383. case FieldDescriptor::TYPE_SFIXED32:
  384. return "SFixed32";
  385. case FieldDescriptor::TYPE_SFIXED64:
  386. return "SFixed64";
  387. case FieldDescriptor::TYPE_SINT32:
  388. return "SInt32";
  389. case FieldDescriptor::TYPE_SINT64:
  390. return "SInt64";
  391. default:
  392. GOOGLE_LOG(FATAL)<< "Unknown field type.";
  393. return "";
  394. }
  395. }
  396. } // namespace csharp
  397. } // namespace compiler
  398. } // namespace protobuf
  399. } // namespace google