Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: third_party/grpc/src/compiler/csharp_generator.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include <cctype>
35 #include <map>
36 #include <sstream>
37 #include <vector>
38
39 #include "src/compiler/csharp_generator.h"
40 #include "src/compiler/config.h"
41 #include "src/compiler/csharp_generator_helpers.h"
42 #include "src/compiler/csharp_generator.h"
43
44
45 using google::protobuf::compiler::csharp::GetFileNamespace;
46 using google::protobuf::compiler::csharp::GetClassName;
47 using google::protobuf::compiler::csharp::GetReflectionClassName;
48 using grpc::protobuf::FileDescriptor;
49 using grpc::protobuf::Descriptor;
50 using grpc::protobuf::ServiceDescriptor;
51 using grpc::protobuf::MethodDescriptor;
52 using grpc::protobuf::io::Printer;
53 using grpc::protobuf::io::StringOutputStream;
54 using grpc_generator::MethodType;
55 using grpc_generator::GetMethodType;
56 using grpc_generator::METHODTYPE_NO_STREAMING;
57 using grpc_generator::METHODTYPE_CLIENT_STREAMING;
58 using grpc_generator::METHODTYPE_SERVER_STREAMING;
59 using grpc_generator::METHODTYPE_BIDI_STREAMING;
60 using grpc_generator::StringReplace;
61 using std::map;
62 using std::vector;
63
64
65 namespace grpc_csharp_generator {
66 namespace {
67
68 std::string GetServiceClassName(const ServiceDescriptor* service) {
69 return service->name();
70 }
71
72 std::string GetClientInterfaceName(const ServiceDescriptor* service) {
73 return "I" + service->name() + "Client";
74 }
75
76 std::string GetClientClassName(const ServiceDescriptor* service) {
77 return service->name() + "Client";
78 }
79
80 std::string GetServerInterfaceName(const ServiceDescriptor* service) {
81 return "I" + service->name();
82 }
83
84 std::string GetCSharpMethodType(MethodType method_type) {
85 switch (method_type) {
86 case METHODTYPE_NO_STREAMING:
87 return "MethodType.Unary";
88 case METHODTYPE_CLIENT_STREAMING:
89 return "MethodType.ClientStreaming";
90 case METHODTYPE_SERVER_STREAMING:
91 return "MethodType.ServerStreaming";
92 case METHODTYPE_BIDI_STREAMING:
93 return "MethodType.DuplexStreaming";
94 }
95 GOOGLE_LOG(FATAL)<< "Can't get here.";
96 return "";
97 }
98
99 std::string GetServiceNameFieldName() {
100 return "__ServiceName";
101 }
102
103 std::string GetMarshallerFieldName(const Descriptor *message) {
104 return "__Marshaller_" + message->name();
105 }
106
107 std::string GetMethodFieldName(const MethodDescriptor *method) {
108 return "__Method_" + method->name();
109 }
110
111 std::string GetMethodRequestParamMaybe(const MethodDescriptor *method) {
112 if (method->client_streaming()) {
113 return "";
114 }
115 return GetClassName(method->input_type()) + " request, ";
116 }
117
118 std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
119 switch (GetMethodType(method)) {
120 case METHODTYPE_NO_STREAMING:
121 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
122 case METHODTYPE_CLIENT_STREAMING:
123 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
124 + ", " + GetClassName(method->output_type()) + ">";
125 case METHODTYPE_SERVER_STREAMING:
126 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
127 + ">";
128 case METHODTYPE_BIDI_STREAMING:
129 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
130 + ", " + GetClassName(method->output_type()) + ">";
131 }
132 GOOGLE_LOG(FATAL)<< "Can't get here.";
133 return "";
134 }
135
136 std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
137 switch (GetMethodType(method)) {
138 case METHODTYPE_NO_STREAMING:
139 case METHODTYPE_SERVER_STREAMING:
140 return GetClassName(method->input_type()) + " request";
141 case METHODTYPE_CLIENT_STREAMING:
142 case METHODTYPE_BIDI_STREAMING:
143 return "IAsyncStreamReader<" + GetClassName(method->input_type())
144 + "> requestStream";
145 }
146 GOOGLE_LOG(FATAL)<< "Can't get here.";
147 return "";
148 }
149
150 std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
151 switch (GetMethodType(method)) {
152 case METHODTYPE_NO_STREAMING:
153 case METHODTYPE_CLIENT_STREAMING:
154 return "Task<" + GetClassName(method->output_type()) + ">";
155 case METHODTYPE_SERVER_STREAMING:
156 case METHODTYPE_BIDI_STREAMING:
157 return "Task";
158 }
159 GOOGLE_LOG(FATAL)<< "Can't get here.";
160 return "";
161 }
162
163 std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
164 switch (GetMethodType(method)) {
165 case METHODTYPE_NO_STREAMING:
166 case METHODTYPE_CLIENT_STREAMING:
167 return "";
168 case METHODTYPE_SERVER_STREAMING:
169 case METHODTYPE_BIDI_STREAMING:
170 return ", IServerStreamWriter<" + GetClassName(method->output_type())
171 + "> responseStream";
172 }
173 GOOGLE_LOG(FATAL)<< "Can't get here.";
174 return "";
175 }
176
177 // Gets vector of all messages used as input or output types.
178 std::vector<const Descriptor*> GetUsedMessages(
179 const ServiceDescriptor *service) {
180 std::set<const Descriptor*> descriptor_set;
181 std::vector<const Descriptor*> result; // vector is to maintain stable orderi ng
182 for (int i = 0; i < service->method_count(); i++) {
183 const MethodDescriptor *method = service->method(i);
184 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
185 descriptor_set.insert(method->input_type());
186 result.push_back(method->input_type());
187 }
188 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
189 descriptor_set.insert(method->output_type());
190 result.push_back(method->output_type());
191 }
192 }
193 return result;
194 }
195
196 void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
197 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
198 for (size_t i = 0; i < used_messages.size(); i++) {
199 const Descriptor *message = used_messages[i];
200 out->Print(
201 "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((ar g) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), $type$.Parser. ParseFrom);\n",
202 "fieldname", GetMarshallerFieldName(message), "type",
203 GetClassName(message));
204 }
205 out->Print("\n");
206 }
207
208 void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
209 out->Print(
210 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$r equest$, $response$>(\n",
211 "fieldname", GetMethodFieldName(method), "request",
212 GetClassName(method->input_type()), "response",
213 GetClassName(method->output_type()));
214 out->Indent();
215 out->Indent();
216 out->Print("$methodtype$,\n", "methodtype",
217 GetCSharpMethodType(GetMethodType(method)));
218 out->Print("$servicenamefield$,\n", "servicenamefield",
219 GetServiceNameFieldName());
220 out->Print("\"$methodname$\",\n", "methodname", method->name());
221 out->Print("$requestmarshaller$,\n", "requestmarshaller",
222 GetMarshallerFieldName(method->input_type()));
223 out->Print("$responsemarshaller$);\n", "responsemarshaller",
224 GetMarshallerFieldName(method->output_type()));
225 out->Print("\n");
226 out->Outdent();
227 out->Outdent();
228 }
229
230 void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *se rvice) {
231 std::ostringstream index;
232 index << service->index();
233 out->Print("// service descriptor\n");
234 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
235 out->Print("{\n");
236 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
237 "umbrella", GetReflectionClassName(service->file()), "index",
238 index.str());
239 out->Print("}\n");
240 out->Print("\n");
241 }
242
243 void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) {
244 out->Print("// client interface\n");
245 out->Print("public interface $name$\n", "name",
246 GetClientInterfaceName(service));
247 out->Print("{\n");
248 out->Indent();
249 for (int i = 0; i < service->method_count(); i++) {
250 const MethodDescriptor *method = service->method(i);
251 MethodType method_type = GetMethodType(method);
252
253 if (method_type == METHODTYPE_NO_STREAMING) {
254 // unary calls have an extra synchronous stub method
255 out->Print(
256 "$response$ $methodname$($request$ request, Metadata headers = null, D ateTime? deadline = null, CancellationToken cancellationToken = default(Cancella tionToken));\n",
257 "methodname", method->name(), "request",
258 GetClassName(method->input_type()), "response",
259 GetClassName(method->output_type()));
260
261 // overload taking CallOptions as a param
262 out->Print(
263 "$response$ $methodname$($request$ request, CallOptions options);\n",
264 "methodname", method->name(), "request",
265 GetClassName(method->input_type()), "response",
266 GetClassName(method->output_type()));
267 }
268
269 std::string method_name = method->name();
270 if (method_type == METHODTYPE_NO_STREAMING) {
271 method_name += "Async"; // prevent name clash with synchronous method.
272 }
273 out->Print(
274 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateT ime? deadline = null, CancellationToken cancellationToken = default(Cancellation Token));\n",
275 "methodname", method_name, "request_maybe",
276 GetMethodRequestParamMaybe(method), "returntype",
277 GetMethodReturnTypeClient(method));
278
279 // overload taking CallOptions as a param
280 out->Print(
281 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
282 "methodname", method_name, "request_maybe",
283 GetMethodRequestParamMaybe(method), "returntype",
284 GetMethodReturnTypeClient(method));
285 }
286 out->Outdent();
287 out->Print("}\n");
288 out->Print("\n");
289 }
290
291 void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
292 out->Print("// server-side interface\n");
293 out->Print("public interface $name$\n", "name",
294 GetServerInterfaceName(service));
295 out->Print("{\n");
296 out->Indent();
297 for (int i = 0; i < service->method_count(); i++) {
298 const MethodDescriptor *method = service->method(i);
299 out->Print(
300 "$returntype$ $methodname$($request$$response_stream_maybe$, "
301 "ServerCallContext context);\n",
302 "methodname", method->name(), "returntype",
303 GetMethodReturnTypeServer(method), "request",
304 GetMethodRequestParamServer(method), "response_stream_maybe",
305 GetMethodResponseStreamMaybe(method));
306 }
307 out->Outdent();
308 out->Print("}\n");
309 out->Print("\n");
310 }
311
312 void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
313 out->Print("// client stub\n");
314 out->Print(
315 "public class $name$ : ClientBase, $interface$\n",
316 "name", GetClientClassName(service), "interface",
317 GetClientInterfaceName(service));
318 out->Print("{\n");
319 out->Indent();
320
321 // constructors
322 out->Print(
323 "public $name$(Channel channel) : base(channel)\n",
324 "name", GetClientClassName(service));
325 out->Print("{\n");
326 out->Print("}\n");
327
328 for (int i = 0; i < service->method_count(); i++) {
329 const MethodDescriptor *method = service->method(i);
330 MethodType method_type = GetMethodType(method);
331
332 if (method_type == METHODTYPE_NO_STREAMING) {
333 // unary calls have an extra synchronous stub method
334 out->Print(
335 "public $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(C ancellationToken))\n",
336 "methodname", method->name(), "request",
337 GetClassName(method->input_type()), "response",
338 GetClassName(method->output_type()));
339 out->Print("{\n");
340 out->Indent();
341 out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, deadline, cancellationToken));\n",
342 "methodfield", GetMethodFieldName(method));
343 out->Print("return Calls.BlockingUnaryCall(call, request);\n");
344 out->Outdent();
345 out->Print("}\n");
346
347 // overload taking CallOptions as a param
348 out->Print(
349 "public $response$ $methodname$($request$ request, CallOptions o ptions)\n",
350 "methodname", method->name(), "request",
351 GetClassName(method->input_type()), "response",
352 GetClassName(method->output_type()));
353 out->Print("{\n");
354 out->Indent();
355 out->Print("var call = CreateCall($methodfield$, options);\n",
356 "methodfield", GetMethodFieldName(method));
357 out->Print("return Calls.BlockingUnaryCall(call, request);\n");
358 out->Outdent();
359 out->Print("}\n");
360 }
361
362 std::string method_name = method->name();
363 if (method_type == METHODTYPE_NO_STREAMING) {
364 method_name += "Async"; // prevent name clash with synchronous method.
365 }
366 out->Print(
367 "public $returntype$ $methodname$($request_maybe$Metadata headers = null , DateTime? deadline = null, CancellationToken cancellationToken = default(Cance llationToken))\n",
368 "methodname", method_name, "request_maybe",
369 GetMethodRequestParamMaybe(method), "returntype",
370 GetMethodReturnTypeClient(method));
371 out->Print("{\n");
372 out->Indent();
373 out->Print("var call = CreateCall($methodfield$, new CallOptions(headers, de adline, cancellationToken));\n",
374 "methodfield", GetMethodFieldName(method));
375 switch (GetMethodType(method)) {
376 case METHODTYPE_NO_STREAMING:
377 out->Print("return Calls.AsyncUnaryCall(call, request);\n");
378 break;
379 case METHODTYPE_CLIENT_STREAMING:
380 out->Print("return Calls.AsyncClientStreamingCall(call);\n");
381 break;
382 case METHODTYPE_SERVER_STREAMING:
383 out->Print(
384 "return Calls.AsyncServerStreamingCall(call, request);\n");
385 break;
386 case METHODTYPE_BIDI_STREAMING:
387 out->Print("return Calls.AsyncDuplexStreamingCall(call);\n");
388 break;
389 default:
390 GOOGLE_LOG(FATAL)<< "Can't get here.";
391 }
392 out->Outdent();
393 out->Print("}\n");
394
395 // overload taking CallOptions as a param
396 out->Print(
397 "public $returntype$ $methodname$($request_maybe$CallOptions options)\n" ,
398 "methodname", method_name, "request_maybe",
399 GetMethodRequestParamMaybe(method), "returntype",
400 GetMethodReturnTypeClient(method));
401 out->Print("{\n");
402 out->Indent();
403 out->Print("var call = CreateCall($methodfield$, options);\n",
404 "methodfield", GetMethodFieldName(method));
405 switch (GetMethodType(method)) {
406 case METHODTYPE_NO_STREAMING:
407 out->Print("return Calls.AsyncUnaryCall(call, request);\n");
408 break;
409 case METHODTYPE_CLIENT_STREAMING:
410 out->Print("return Calls.AsyncClientStreamingCall(call);\n");
411 break;
412 case METHODTYPE_SERVER_STREAMING:
413 out->Print(
414 "return Calls.AsyncServerStreamingCall(call, request);\n");
415 break;
416 case METHODTYPE_BIDI_STREAMING:
417 out->Print("return Calls.AsyncDuplexStreamingCall(call);\n");
418 break;
419 default:
420 GOOGLE_LOG(FATAL)<< "Can't get here.";
421 }
422 out->Outdent();
423 out->Print("}\n");
424 }
425 out->Outdent();
426 out->Print("}\n");
427 out->Print("\n");
428 }
429
430 void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) {
431 out->Print(
432 "// creates service definition that can be registered with a server\n");
433 out->Print(
434 "public static ServerServiceDefinition BindService($interface$ serviceImpl )\n",
435 "interface", GetServerInterfaceName(service));
436 out->Print("{\n");
437 out->Indent();
438
439 out->Print(
440 "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n",
441 "servicenamefield", GetServiceNameFieldName());
442 out->Indent();
443 out->Indent();
444 for (int i = 0; i < service->method_count(); i++) {
445 const MethodDescriptor *method = service->method(i);
446 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
447 "methodfield", GetMethodFieldName(method), "methodname",
448 method->name());
449 if (i == service->method_count() - 1) {
450 out->Print(".Build();");
451 }
452 out->Print("\n");
453 }
454 out->Outdent();
455 out->Outdent();
456
457 out->Outdent();
458 out->Print("}\n");
459 out->Print("\n");
460 }
461
462 void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
463 out->Print("// creates a new client\n");
464 out->Print("public static $classname$ NewClient(Channel channel)\n",
465 "classname", GetClientClassName(service));
466 out->Print("{\n");
467 out->Indent();
468 out->Print("return new $classname$(channel);\n", "classname",
469 GetClientClassName(service));
470 out->Outdent();
471 out->Print("}\n");
472 out->Print("\n");
473 }
474
475 void GenerateService(Printer* out, const ServiceDescriptor *service) {
476 out->Print("public static class $classname$\n", "classname",
477 GetServiceClassName(service));
478 out->Print("{\n");
479 out->Indent();
480 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
481 "servicenamefield", GetServiceNameFieldName(), "servicename",
482 service->full_name());
483 out->Print("\n");
484
485 GenerateMarshallerFields(out, service);
486 for (int i = 0; i < service->method_count(); i++) {
487 GenerateStaticMethodField(out, service->method(i));
488 }
489 GenerateServiceDescriptorProperty(out, service);
490 GenerateClientInterface(out, service);
491 GenerateServerInterface(out, service);
492 GenerateClientStub(out, service);
493 GenerateBindServiceMethod(out, service);
494 GenerateNewStubMethods(out, service);
495
496 out->Outdent();
497 out->Print("}\n");
498 }
499
500 } // anonymous namespace
501
502 grpc::string GetServices(const FileDescriptor *file) {
503 grpc::string output;
504 {
505 // Scope the output stream so it closes and finalizes output to the string.
506
507 StringOutputStream output_stream(&output);
508 Printer out(&output_stream, '$');
509
510 // Don't write out any output if there no services, to avoid empty service
511 // files being generated for proto files that don't declare any.
512 if (file->service_count() == 0) {
513 return output;
514 }
515
516 // Write out a file header.
517 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
518 out.Print("// source: $filename$\n", "filename", file->name());
519 out.Print("#region Designer generated code\n");
520 out.Print("\n");
521 out.Print("using System;\n");
522 out.Print("using System.Threading;\n");
523 out.Print("using System.Threading.Tasks;\n");
524 out.Print("using Grpc.Core;\n");
525 out.Print("\n");
526
527 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
528 out.Indent();
529 for (int i = 0; i < file->service_count(); i++) {
530 GenerateService(&out, file->service(i));
531 }
532 out.Outdent();
533 out.Print("}\n");
534 out.Print("#endregion\n");
535 }
536 return output;
537 }
538
539 } // namespace grpc_csharp_generator
OLDNEW
« no previous file with comments | « third_party/grpc/src/compiler/csharp_generator.h ('k') | third_party/grpc/src/compiler/csharp_generator_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698