csharp_reflection_class.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <sstream>
  31. #include <google/protobuf/compiler/code_generator.h>
  32. #include <google/protobuf/compiler/plugin.h>
  33. #include <google/protobuf/descriptor.h>
  34. #include <google/protobuf/descriptor.pb.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/io/zero_copy_stream.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. #include <google/protobuf/compiler/csharp/csharp_enum.h>
  39. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  40. #include <google/protobuf/compiler/csharp/csharp_message.h>
  41. #include <google/protobuf/compiler/csharp/csharp_names.h>
  42. #include <google/protobuf/compiler/csharp/csharp_options.h>
  43. #include <google/protobuf/compiler/csharp/csharp_reflection_class.h>
  44. namespace google {
  45. namespace protobuf {
  46. namespace compiler {
  47. namespace csharp {
  48. ReflectionClassGenerator::ReflectionClassGenerator(const FileDescriptor* file,
  49. const Options* options)
  50. : SourceGeneratorBase(file, options),
  51. file_(file) {
  52. namespace_ = GetFileNamespace(file);
  53. reflectionClassname_ = GetReflectionClassUnqualifiedName(file);
  54. }
  55. ReflectionClassGenerator::~ReflectionClassGenerator() {
  56. }
  57. void ReflectionClassGenerator::Generate(io::Printer* printer) {
  58. WriteIntroduction(printer);
  59. WriteDescriptor(printer);
  60. // Close the class declaration.
  61. printer->Outdent();
  62. printer->Print("}\n");
  63. // write children: Enums
  64. if (file_->enum_type_count() > 0) {
  65. printer->Print("#region Enums\n");
  66. for (int i = 0; i < file_->enum_type_count(); i++) {
  67. EnumGenerator enumGenerator(file_->enum_type(i), this->options());
  68. enumGenerator.Generate(printer);
  69. }
  70. printer->Print("#endregion\n");
  71. printer->Print("\n");
  72. }
  73. // write children: Messages
  74. if (file_->message_type_count() > 0) {
  75. printer->Print("#region Messages\n");
  76. for (int i = 0; i < file_->message_type_count(); i++) {
  77. MessageGenerator messageGenerator(file_->message_type(i), this->options());
  78. messageGenerator.Generate(printer);
  79. }
  80. printer->Print("#endregion\n");
  81. printer->Print("\n");
  82. }
  83. // TODO(jtattermusch): add insertion point for services.
  84. if (!namespace_.empty()) {
  85. printer->Outdent();
  86. printer->Print("}\n");
  87. }
  88. printer->Print("\n");
  89. printer->Print("#endregion Designer generated code\n");
  90. }
  91. void ReflectionClassGenerator::WriteIntroduction(io::Printer* printer) {
  92. printer->Print(
  93. "// <auto-generated>\n"
  94. "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
  95. "// source: $file_name$\n"
  96. "// </auto-generated>\n"
  97. "#pragma warning disable 1591, 0612, 3021\n"
  98. "#region Designer generated code\n"
  99. "\n"
  100. "using pb = global::Google.Protobuf;\n"
  101. "using pbc = global::Google.Protobuf.Collections;\n"
  102. "using pbr = global::Google.Protobuf.Reflection;\n"
  103. "using scg = global::System.Collections.Generic;\n",
  104. "file_name", file_->name());
  105. if (!namespace_.empty()) {
  106. printer->Print("namespace $namespace$ {\n", "namespace", namespace_);
  107. printer->Indent();
  108. printer->Print("\n");
  109. }
  110. printer->Print(
  111. "/// <summary>Holder for reflection information generated from $file_name$</summary>\n"
  112. "$access_level$ static partial class $reflection_class_name$ {\n"
  113. "\n",
  114. "file_name", file_->name(),
  115. "access_level", class_access_level(),
  116. "reflection_class_name", reflectionClassname_);
  117. printer->Indent();
  118. }
  119. void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) {
  120. printer->Print(
  121. "#region Descriptor\n"
  122. "/// <summary>File descriptor for $file_name$</summary>\n"
  123. "public static pbr::FileDescriptor Descriptor {\n"
  124. " get { return descriptor; }\n"
  125. "}\n"
  126. "private static pbr::FileDescriptor descriptor;\n"
  127. "\n"
  128. "static $reflection_class_name$() {\n",
  129. "file_name", file_->name(),
  130. "reflection_class_name", reflectionClassname_);
  131. printer->Indent();
  132. printer->Print(
  133. "byte[] descriptorData = global::System.Convert.FromBase64String(\n");
  134. printer->Indent();
  135. printer->Indent();
  136. printer->Print("string.Concat(\n");
  137. printer->Indent();
  138. // TODO(jonskeet): Consider a C#-escaping format here instead of just Base64.
  139. std::string base64 = FileDescriptorToBase64(file_);
  140. while (base64.size() > 60) {
  141. printer->Print("\"$base64$\",\n", "base64", base64.substr(0, 60));
  142. base64 = base64.substr(60);
  143. }
  144. printer->Print("\"$base64$\"));\n", "base64", base64);
  145. printer->Outdent();
  146. printer->Outdent();
  147. printer->Outdent();
  148. // -----------------------------------------------------------------
  149. // Invoke InternalBuildGeneratedFileFrom() to build the file.
  150. printer->Print(
  151. "descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,\n");
  152. printer->Print(" new pbr::FileDescriptor[] { ");
  153. for (int i = 0; i < file_->dependency_count(); i++) {
  154. // descriptor.proto is special: we don't allow access to the generated code, but there's
  155. // a separately-exposed property to get at the file descriptor, specifically to allow this
  156. // kind of dependency.
  157. if (IsDescriptorProto(file_->dependency(i))) {
  158. printer->Print("pbr::FileDescriptor.DescriptorProtoFileDescriptor, ");
  159. } else {
  160. printer->Print(
  161. "$full_reflection_class_name$.Descriptor, ",
  162. "full_reflection_class_name",
  163. GetReflectionClassName(file_->dependency(i)));
  164. }
  165. }
  166. printer->Print("},\n"
  167. " new pbr::GeneratedClrTypeInfo(");
  168. // Specify all the generated code information, recursively.
  169. if (file_->enum_type_count() > 0) {
  170. printer->Print("new[] {");
  171. for (int i = 0; i < file_->enum_type_count(); i++) {
  172. printer->Print("typeof($type_name$), ", "type_name", GetClassName(file_->enum_type(i)));
  173. }
  174. printer->Print("}, ");
  175. }
  176. else {
  177. printer->Print("null, ");
  178. }
  179. if (file_->message_type_count() > 0) {
  180. printer->Print("new pbr::GeneratedClrTypeInfo[] {\n");
  181. printer->Indent();
  182. printer->Indent();
  183. printer->Indent();
  184. for (int i = 0; i < file_->message_type_count(); i++) {
  185. WriteGeneratedCodeInfo(file_->message_type(i), printer, i == file_->message_type_count() - 1);
  186. }
  187. printer->Outdent();
  188. printer->Print("\n}));\n");
  189. printer->Outdent();
  190. printer->Outdent();
  191. }
  192. else {
  193. printer->Print("null));\n");
  194. }
  195. printer->Outdent();
  196. printer->Print("}\n");
  197. printer->Print("#endregion\n\n");
  198. }
  199. // Write out the generated code for a particular message. This consists of the CLR type, property names
  200. // corresponding to fields, names corresponding to oneofs, nested enums, and nested types. Each array part
  201. // can be specified as null if it would be empty, to make the generated code somewhat simpler to read.
  202. // We write a line break at the end of each generated code info, so that in the final file we'll see all
  203. // the types, pre-ordered depth first, one per line. The indentation will be slightly unusual,
  204. // in that it will look like a single array when it's actually constructing a tree, but it'll be easy to
  205. // read even with multiple levels of nesting.
  206. // The "last" parameter indicates whether this message descriptor is the last one being printed in this immediate
  207. // context. It governs whether or not a trailing comma and newline is written after the constructor, effectively
  208. // just controlling the formatting in the generated code.
  209. void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last) {
  210. if (IsMapEntryMessage(descriptor)) {
  211. printer->Print("null, ");
  212. return;
  213. }
  214. // Generated message type
  215. printer->Print("new pbr::GeneratedClrTypeInfo(typeof($type_name$), $type_name$.Parser, ", "type_name", GetClassName(descriptor));
  216. // Fields
  217. if (descriptor->field_count() > 0) {
  218. std::vector<std::string> fields;
  219. for (int i = 0; i < descriptor->field_count(); i++) {
  220. fields.push_back(GetPropertyName(descriptor->field(i)));
  221. }
  222. printer->Print("new[]{ \"$fields$\" }, ", "fields", JoinStrings(fields, "\", \""));
  223. }
  224. else {
  225. printer->Print("null, ");
  226. }
  227. // Oneofs
  228. if (descriptor->oneof_decl_count() > 0) {
  229. std::vector<std::string> oneofs;
  230. for (int i = 0; i < descriptor->oneof_decl_count(); i++) {
  231. oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true));
  232. }
  233. printer->Print("new[]{ \"$oneofs$\" }, ", "oneofs", JoinStrings(oneofs, "\", \""));
  234. }
  235. else {
  236. printer->Print("null, ");
  237. }
  238. // Nested enums
  239. if (descriptor->enum_type_count() > 0) {
  240. std::vector<std::string> enums;
  241. for (int i = 0; i < descriptor->enum_type_count(); i++) {
  242. enums.push_back(GetClassName(descriptor->enum_type(i)));
  243. }
  244. printer->Print("new[]{ typeof($enums$) }, ", "enums", JoinStrings(enums, "), typeof("));
  245. }
  246. else {
  247. printer->Print("null, ");
  248. }
  249. // Nested types
  250. if (descriptor->nested_type_count() > 0) {
  251. // Need to specify array type explicitly here, as all elements may be null.
  252. printer->Print("new pbr::GeneratedClrTypeInfo[] { ");
  253. for (int i = 0; i < descriptor->nested_type_count(); i++) {
  254. WriteGeneratedCodeInfo(descriptor->nested_type(i), printer, i == descriptor->nested_type_count() - 1);
  255. }
  256. printer->Print("}");
  257. }
  258. else {
  259. printer->Print("null");
  260. }
  261. printer->Print(last ? ")" : "),\n");
  262. }
  263. } // namespace csharp
  264. } // namespace compiler
  265. } // namespace protobuf
  266. } // namespace google