csharp_field_base.cc 15 KB

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