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