csharp_helpers.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <algorithm>
  34. #include <google/protobuf/stubs/hash.h>
  35. #include <limits>
  36. #include <vector>
  37. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  38. #include <google/protobuf/descriptor.pb.h>
  39. #include <google/protobuf/io/printer.h>
  40. #include <google/protobuf/wire_format.h>
  41. #include <google/protobuf/stubs/strutil.h>
  42. #include <google/protobuf/stubs/substitute.h>
  43. #include <google/protobuf/compiler/csharp/csharp_field_base.h>
  44. #include <google/protobuf/compiler/csharp/csharp_enum_field.h>
  45. #include <google/protobuf/compiler/csharp/csharp_map_field.h>
  46. #include <google/protobuf/compiler/csharp/csharp_message_field.h>
  47. #include <google/protobuf/compiler/csharp/csharp_primitive_field.h>
  48. #include <google/protobuf/compiler/csharp/csharp_repeated_enum_field.h>
  49. #include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h>
  50. #include <google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h>
  51. #include <google/protobuf/compiler/csharp/csharp_wrapper_field.h>
  52. namespace google {
  53. namespace protobuf {
  54. namespace compiler {
  55. namespace csharp {
  56. CSharpType GetCSharpType(FieldDescriptor::Type type) {
  57. switch (type) {
  58. case FieldDescriptor::TYPE_INT32:
  59. return CSHARPTYPE_INT32;
  60. case FieldDescriptor::TYPE_INT64:
  61. return CSHARPTYPE_INT64;
  62. case FieldDescriptor::TYPE_UINT32:
  63. return CSHARPTYPE_UINT32;
  64. case FieldDescriptor::TYPE_UINT64:
  65. return CSHARPTYPE_UINT32;
  66. case FieldDescriptor::TYPE_SINT32:
  67. return CSHARPTYPE_INT32;
  68. case FieldDescriptor::TYPE_SINT64:
  69. return CSHARPTYPE_INT64;
  70. case FieldDescriptor::TYPE_FIXED32:
  71. return CSHARPTYPE_UINT32;
  72. case FieldDescriptor::TYPE_FIXED64:
  73. return CSHARPTYPE_UINT64;
  74. case FieldDescriptor::TYPE_SFIXED32:
  75. return CSHARPTYPE_INT32;
  76. case FieldDescriptor::TYPE_SFIXED64:
  77. return CSHARPTYPE_INT64;
  78. case FieldDescriptor::TYPE_FLOAT:
  79. return CSHARPTYPE_FLOAT;
  80. case FieldDescriptor::TYPE_DOUBLE:
  81. return CSHARPTYPE_DOUBLE;
  82. case FieldDescriptor::TYPE_BOOL:
  83. return CSHARPTYPE_BOOL;
  84. case FieldDescriptor::TYPE_ENUM:
  85. return CSHARPTYPE_ENUM;
  86. case FieldDescriptor::TYPE_STRING:
  87. return CSHARPTYPE_STRING;
  88. case FieldDescriptor::TYPE_BYTES:
  89. return CSHARPTYPE_BYTESTRING;
  90. case FieldDescriptor::TYPE_GROUP:
  91. return CSHARPTYPE_MESSAGE;
  92. case FieldDescriptor::TYPE_MESSAGE:
  93. return CSHARPTYPE_MESSAGE;
  94. // No default because we want the compiler to complain if any new
  95. // types are added.
  96. }
  97. GOOGLE_LOG(FATAL)<< "Can't get here.";
  98. return (CSharpType) -1;
  99. }
  100. std::string StripDotProto(const std::string& proto_file) {
  101. int lastindex = proto_file.find_last_of(".");
  102. return proto_file.substr(0, lastindex);
  103. }
  104. std::string GetFileNamespace(const FileDescriptor* descriptor) {
  105. if (descriptor->options().has_csharp_namespace()) {
  106. return descriptor->options().csharp_namespace();
  107. }
  108. return UnderscoresToCamelCase(descriptor->package(), true, true);
  109. }
  110. // Returns the Pascal-cased last part of the proto file. For example,
  111. // input of "google/protobuf/foo_bar.proto" would result in "FooBar".
  112. std::string GetFileNameBase(const FileDescriptor* descriptor) {
  113. std::string proto_file = descriptor->name();
  114. int lastslash = proto_file.find_last_of("/");
  115. std::string base = proto_file.substr(lastslash + 1);
  116. return UnderscoresToPascalCase(StripDotProto(base));
  117. }
  118. std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
  119. // umbrella_classname can no longer be set using message option.
  120. // TODO: Detect collisions with existing messages, and append an underscore if necessary.
  121. return GetFileNameBase(descriptor) + "Reflection";
  122. }
  123. // TODO(jtattermusch): can we reuse a utility function?
  124. std::string UnderscoresToCamelCase(const std::string& input,
  125. bool cap_next_letter,
  126. bool preserve_period) {
  127. string result;
  128. // Note: I distrust ctype.h due to locales.
  129. for (int i = 0; i < input.size(); i++) {
  130. if ('a' <= input[i] && input[i] <= 'z') {
  131. if (cap_next_letter) {
  132. result += input[i] + ('A' - 'a');
  133. } else {
  134. result += input[i];
  135. }
  136. cap_next_letter = false;
  137. } else if ('A' <= input[i] && input[i] <= 'Z') {
  138. if (i == 0 && !cap_next_letter) {
  139. // Force first letter to lower-case unless explicitly told to
  140. // capitalize it.
  141. result += input[i] + ('a' - 'A');
  142. } else {
  143. // Capital letters after the first are left as-is.
  144. result += input[i];
  145. }
  146. cap_next_letter = false;
  147. } else if ('0' <= input[i] && input[i] <= '9') {
  148. result += input[i];
  149. cap_next_letter = true;
  150. } else {
  151. cap_next_letter = true;
  152. if (input[i] == '.' && preserve_period) {
  153. result += '.';
  154. }
  155. }
  156. }
  157. // Add a trailing "_" if the name should be altered.
  158. if (input[input.size() - 1] == '#') {
  159. result += '_';
  160. }
  161. return result;
  162. }
  163. std::string UnderscoresToPascalCase(const std::string& input) {
  164. return UnderscoresToCamelCase(input, true);
  165. }
  166. std::string ToCSharpName(const std::string& name, const FileDescriptor* file) {
  167. std::string result = GetFileNamespace(file);
  168. if (result != "") {
  169. result += '.';
  170. }
  171. string classname;
  172. if (file->package().empty()) {
  173. classname = name;
  174. } else {
  175. // Strip the proto package from full_name since we've replaced it with
  176. // the C# namespace.
  177. classname = name.substr(file->package().size() + 1);
  178. }
  179. result += StringReplace(classname, ".", ".Types.", true);
  180. return "global::" + result;
  181. }
  182. std::string GetUmbrellaClassName(const FileDescriptor* descriptor) {
  183. std::string result = GetFileNamespace(descriptor);
  184. if (!result.empty()) {
  185. result += '.';
  186. }
  187. result += GetUmbrellaClassUnqualifiedName(descriptor);
  188. return "global::" + result;
  189. }
  190. std::string GetClassName(const Descriptor* descriptor) {
  191. return ToCSharpName(descriptor->full_name(), descriptor->file());
  192. }
  193. std::string GetClassName(const EnumDescriptor* descriptor) {
  194. return ToCSharpName(descriptor->full_name(), descriptor->file());
  195. }
  196. // Groups are hacky: The name of the field is just the lower-cased name
  197. // of the group type. In C#, though, we would like to retain the original
  198. // capitalization of the type name.
  199. std::string GetFieldName(const FieldDescriptor* descriptor) {
  200. if (descriptor->type() == FieldDescriptor::TYPE_GROUP) {
  201. return descriptor->message_type()->name();
  202. } else {
  203. return descriptor->name();
  204. }
  205. }
  206. std::string GetFieldConstantName(const FieldDescriptor* field) {
  207. return GetPropertyName(field) + "FieldNumber";
  208. }
  209. std::string GetPropertyName(const FieldDescriptor* descriptor) {
  210. // TODO(jtattermusch): consider introducing csharp_property_name field option
  211. std::string property_name = UnderscoresToPascalCase(GetFieldName(descriptor));
  212. // Avoid either our own type name or reserved names. Note that not all names
  213. // are reserved - a field called to_string, write_to etc would still cause a problem.
  214. // There are various ways of ending up with naming collisions, but we try to avoid obvious
  215. // ones.
  216. if (property_name == descriptor->containing_type()->name()
  217. || property_name == "Types"
  218. || property_name == "Descriptor") {
  219. property_name += "_";
  220. }
  221. return property_name;
  222. }
  223. std::string GetOutputFile(
  224. const google::protobuf::FileDescriptor* descriptor,
  225. const std::string file_extension,
  226. const bool generate_directories,
  227. const std::string base_namespace,
  228. string* error) {
  229. string relative_filename = GetFileNameBase(descriptor) + file_extension;
  230. if (!generate_directories) {
  231. return relative_filename;
  232. }
  233. string ns = GetFileNamespace(descriptor);
  234. string namespace_suffix = ns;
  235. if (!base_namespace.empty()) {
  236. // Check that the base_namespace is either equal to or a leading part of
  237. // the file namespace. This isn't just a simple prefix; "Foo.B" shouldn't
  238. // be regarded as a prefix of "Foo.Bar". The simplest option is to add "."
  239. // to both.
  240. string extended_ns = ns + ".";
  241. if (extended_ns.find(base_namespace + ".") != 0) {
  242. *error = "Namespace " + ns + " is not a prefix namespace of base namespace " + base_namespace;
  243. return ""; // This will be ignored, because we've set an error.
  244. }
  245. namespace_suffix = ns.substr(base_namespace.length());
  246. if (namespace_suffix.find(".") == 0) {
  247. namespace_suffix = namespace_suffix.substr(1);
  248. }
  249. }
  250. string namespace_dir = StringReplace(namespace_suffix, ".", "/", true);
  251. if (!namespace_dir.empty()) {
  252. namespace_dir += "/";
  253. }
  254. return namespace_dir + relative_filename;
  255. }
  256. // TODO: c&p from Java protoc plugin
  257. // For encodings with fixed sizes, returns that size in bytes. Otherwise
  258. // returns -1.
  259. int GetFixedSize(FieldDescriptor::Type type) {
  260. switch (type) {
  261. case FieldDescriptor::TYPE_INT32 : return -1;
  262. case FieldDescriptor::TYPE_INT64 : return -1;
  263. case FieldDescriptor::TYPE_UINT32 : return -1;
  264. case FieldDescriptor::TYPE_UINT64 : return -1;
  265. case FieldDescriptor::TYPE_SINT32 : return -1;
  266. case FieldDescriptor::TYPE_SINT64 : return -1;
  267. case FieldDescriptor::TYPE_FIXED32 : return internal::WireFormatLite::kFixed32Size;
  268. case FieldDescriptor::TYPE_FIXED64 : return internal::WireFormatLite::kFixed64Size;
  269. case FieldDescriptor::TYPE_SFIXED32: return internal::WireFormatLite::kSFixed32Size;
  270. case FieldDescriptor::TYPE_SFIXED64: return internal::WireFormatLite::kSFixed64Size;
  271. case FieldDescriptor::TYPE_FLOAT : return internal::WireFormatLite::kFloatSize;
  272. case FieldDescriptor::TYPE_DOUBLE : return internal::WireFormatLite::kDoubleSize;
  273. case FieldDescriptor::TYPE_BOOL : return internal::WireFormatLite::kBoolSize;
  274. case FieldDescriptor::TYPE_ENUM : return -1;
  275. case FieldDescriptor::TYPE_STRING : return -1;
  276. case FieldDescriptor::TYPE_BYTES : return -1;
  277. case FieldDescriptor::TYPE_GROUP : return -1;
  278. case FieldDescriptor::TYPE_MESSAGE : return -1;
  279. // No default because we want the compiler to complain if any new
  280. // types are added.
  281. }
  282. GOOGLE_LOG(FATAL) << "Can't get here.";
  283. return -1;
  284. }
  285. static const char base64_chars[] =
  286. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  287. std::string StringToBase64(const std::string& input) {
  288. std::string result;
  289. size_t remaining = input.size();
  290. const unsigned char *src = (const unsigned char*) input.c_str();
  291. while (remaining > 2) {
  292. result += base64_chars[src[0] >> 2];
  293. result += base64_chars[((src[0] & 0x3) << 4) | (src[1] >> 4)];
  294. result += base64_chars[((src[1] & 0xf) << 2) | (src[2] >> 6)];
  295. result += base64_chars[src[2] & 0x3f];
  296. remaining -= 3;
  297. src += 3;
  298. }
  299. switch (remaining) {
  300. case 2:
  301. result += base64_chars[src[0] >> 2];
  302. result += base64_chars[((src[0] & 0x3) << 4) | (src[1] >> 4)];
  303. result += base64_chars[(src[1] & 0xf) << 2];
  304. result += '=';
  305. src += 2;
  306. break;
  307. case 1:
  308. result += base64_chars[src[0] >> 2];
  309. result += base64_chars[((src[0] & 0x3) << 4)];
  310. result += '=';
  311. result += '=';
  312. src += 1;
  313. break;
  314. }
  315. return result;
  316. }
  317. std::string FileDescriptorToBase64(const FileDescriptor* descriptor) {
  318. std::string fdp_bytes;
  319. FileDescriptorProto fdp;
  320. descriptor->CopyTo(&fdp);
  321. fdp.SerializeToString(&fdp_bytes);
  322. return StringToBase64(fdp_bytes);
  323. }
  324. FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
  325. int fieldOrdinal) {
  326. switch (descriptor->type()) {
  327. case FieldDescriptor::TYPE_GROUP:
  328. case FieldDescriptor::TYPE_MESSAGE:
  329. if (descriptor->is_repeated()) {
  330. if (descriptor->is_map()) {
  331. return new MapFieldGenerator(descriptor, fieldOrdinal);
  332. } else {
  333. return new RepeatedMessageFieldGenerator(descriptor, fieldOrdinal);
  334. }
  335. } else {
  336. if (IsWrapperType(descriptor)) {
  337. if (descriptor->containing_oneof()) {
  338. return new WrapperOneofFieldGenerator(descriptor, fieldOrdinal);
  339. } else {
  340. return new WrapperFieldGenerator(descriptor, fieldOrdinal);
  341. }
  342. } else {
  343. if (descriptor->containing_oneof()) {
  344. return new MessageOneofFieldGenerator(descriptor, fieldOrdinal);
  345. } else {
  346. return new MessageFieldGenerator(descriptor, fieldOrdinal);
  347. }
  348. }
  349. }
  350. case FieldDescriptor::TYPE_ENUM:
  351. if (descriptor->is_repeated()) {
  352. return new RepeatedEnumFieldGenerator(descriptor, fieldOrdinal);
  353. } else {
  354. if (descriptor->containing_oneof()) {
  355. return new EnumOneofFieldGenerator(descriptor, fieldOrdinal);
  356. } else {
  357. return new EnumFieldGenerator(descriptor, fieldOrdinal);
  358. }
  359. }
  360. default:
  361. if (descriptor->is_repeated()) {
  362. return new RepeatedPrimitiveFieldGenerator(descriptor, fieldOrdinal);
  363. } else {
  364. if (descriptor->containing_oneof()) {
  365. return new PrimitiveOneofFieldGenerator(descriptor, fieldOrdinal);
  366. } else {
  367. return new PrimitiveFieldGenerator(descriptor, fieldOrdinal);
  368. }
  369. }
  370. }
  371. }
  372. } // namespace csharp
  373. } // namespace compiler
  374. } // namespace protobuf
  375. } // namespace google