2008-08-22 20:38:05 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright 2008 Google Inc. All rights reserved.
|
2008-08-22 20:38:05 +00:00
|
|
|
// http://code.google.com/p/protobuf/
|
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Author: kenton@google.com (Kenton Varda)
|
|
|
|
// Based on original Protocol Buffers design by
|
|
|
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
|
|
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright (c) 2008-2013, Dave Benson. All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
// Modified to implement C code by Dave Benson.
|
|
|
|
|
2013-11-16 17:11:48 -05:00
|
|
|
#include <protoc-c/c_service.h>
|
|
|
|
#include <protoc-c/c_helpers.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/io/printer.h>
|
|
|
|
|
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
namespace c {
|
|
|
|
|
|
|
|
ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor,
|
2020-10-16 16:35:43 -07:00
|
|
|
const std::string& dllexport_decl)
|
2008-08-22 20:38:05 +00:00
|
|
|
: descriptor_(descriptor) {
|
|
|
|
vars_["name"] = descriptor_->name();
|
|
|
|
vars_["fullname"] = descriptor_->full_name();
|
2021-03-21 19:46:21 -07:00
|
|
|
vars_["cname"] = FullNameToC(descriptor_->full_name(), descriptor_->file());
|
|
|
|
vars_["lcfullname"] = FullNameToLower(descriptor_->full_name(), descriptor_->file());
|
|
|
|
vars_["ucfullname"] = FullNameToUpper(descriptor_->full_name(), descriptor_->file());
|
2008-08-24 22:11:04 +00:00
|
|
|
vars_["lcfullpadd"] = ConvertToSpaces(vars_["lcfullname"]);
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["package"] = descriptor_->file()->package();
|
|
|
|
if (dllexport_decl.empty()) {
|
|
|
|
vars_["dllexport"] = "";
|
|
|
|
} else {
|
|
|
|
vars_["dllexport"] = dllexport_decl + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceGenerator::~ServiceGenerator() {}
|
|
|
|
|
|
|
|
// Header stuff.
|
|
|
|
void ServiceGenerator::GenerateMainHFile(io::Printer* printer)
|
|
|
|
{
|
|
|
|
GenerateVfuncs(printer);
|
2009-01-28 04:57:49 +00:00
|
|
|
GenerateInitMacros(printer);
|
2008-08-22 20:38:05 +00:00
|
|
|
GenerateCallersDeclarations(printer);
|
|
|
|
}
|
|
|
|
void ServiceGenerator::GenerateVfuncs(io::Printer* printer)
|
|
|
|
{
|
|
|
|
printer->Print(vars_,
|
2020-05-13 15:10:33 -07:00
|
|
|
"typedef struct $cname$_Service $cname$_Service;\n"
|
|
|
|
"struct $cname$_Service\n"
|
2008-08-24 22:11:04 +00:00
|
|
|
"{\n"
|
|
|
|
" ProtobufCService base;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
for (int i = 0; i < descriptor_->method_count(); i++) {
|
|
|
|
const MethodDescriptor *method = descriptor_->method(i);
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string lcname = CamelToLower(method->name());
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["method"] = lcname;
|
|
|
|
vars_["metpad"] = ConvertToSpaces(lcname);
|
2021-03-21 19:46:21 -07:00
|
|
|
vars_["input_typename"] = FullNameToC(method->input_type()->full_name(), method->input_type()->file());
|
|
|
|
vars_["output_typename"] = FullNameToC(method->output_type()->full_name(), method->output_type()->file());
|
2008-08-22 20:38:05 +00:00
|
|
|
printer->Print(vars_,
|
|
|
|
" void (*$method$)($cname$_Service *service,\n"
|
|
|
|
" $metpad$ const $input_typename$ *input,\n"
|
2008-08-26 14:45:34 +00:00
|
|
|
" $metpad$ $output_typename$_Closure closure,\n"
|
|
|
|
" $metpad$ void *closure_data);\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
printer->Print(vars_,
|
|
|
|
"};\n");
|
2008-08-24 22:11:04 +00:00
|
|
|
printer->Print(vars_,
|
2009-01-08 01:39:52 +00:00
|
|
|
"typedef void (*$cname$_ServiceDestroy)($cname$_Service *);\n"
|
2008-08-24 22:11:04 +00:00
|
|
|
"void $lcfullname$__init ($cname$_Service *service,\n"
|
|
|
|
" $lcfullpadd$ $cname$_ServiceDestroy destroy);\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
2009-01-28 04:57:49 +00:00
|
|
|
void ServiceGenerator::GenerateInitMacros(io::Printer* printer)
|
|
|
|
{
|
|
|
|
printer->Print(vars_,
|
|
|
|
"#define $ucfullname$__BASE_INIT \\\n"
|
|
|
|
" { &$lcfullname$__descriptor, protobuf_c_service_invoke_internal, NULL }\n"
|
|
|
|
"#define $ucfullname$__INIT(function_prefix__) \\\n"
|
|
|
|
" { $ucfullname$__BASE_INIT");
|
|
|
|
for (int i = 0; i < descriptor_->method_count(); i++) {
|
|
|
|
const MethodDescriptor *method = descriptor_->method(i);
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string lcname = CamelToLower(method->name());
|
2009-01-28 04:57:49 +00:00
|
|
|
vars_["method"] = lcname;
|
|
|
|
vars_["metpad"] = ConvertToSpaces(lcname);
|
|
|
|
printer->Print(vars_,
|
|
|
|
",\\\n function_prefix__ ## $method$");
|
|
|
|
}
|
|
|
|
printer->Print(vars_,
|
|
|
|
" }\n");
|
|
|
|
}
|
2008-08-22 20:38:05 +00:00
|
|
|
void ServiceGenerator::GenerateCallersDeclarations(io::Printer* printer)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < descriptor_->method_count(); i++) {
|
|
|
|
const MethodDescriptor *method = descriptor_->method(i);
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string lcname = CamelToLower(method->name());
|
2021-03-21 19:46:21 -07:00
|
|
|
std::string lcfullname = FullNameToLower(descriptor_->full_name(), descriptor_->file());
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["method"] = lcname;
|
|
|
|
vars_["metpad"] = ConvertToSpaces(lcname);
|
2021-03-21 19:46:21 -07:00
|
|
|
vars_["input_typename"] = FullNameToC(method->input_type()->full_name(), method->input_type()->file());
|
|
|
|
vars_["output_typename"] = FullNameToC(method->output_type()->full_name(), method->output_type()->file());
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["padddddddddddddddddd"] = ConvertToSpaces(lcfullname + "__" + lcname);
|
|
|
|
printer->Print(vars_,
|
|
|
|
"void $lcfullname$__$method$(ProtobufCService *service,\n"
|
|
|
|
" $padddddddddddddddddd$ const $input_typename$ *input,\n"
|
2008-08-26 14:45:34 +00:00
|
|
|
" $padddddddddddddddddd$ $output_typename$_Closure closure,\n"
|
|
|
|
" $padddddddddddddddddd$ void *closure_data);\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceGenerator::GenerateDescriptorDeclarations(io::Printer* printer)
|
|
|
|
{
|
|
|
|
printer->Print(vars_, "extern const ProtobufCServiceDescriptor $lcfullname$__descriptor;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Source file stuff.
|
|
|
|
void ServiceGenerator::GenerateCFile(io::Printer* printer)
|
|
|
|
{
|
|
|
|
GenerateServiceDescriptor(printer);
|
|
|
|
GenerateCallersImplementations(printer);
|
2008-08-24 22:11:04 +00:00
|
|
|
GenerateInit(printer);
|
|
|
|
}
|
|
|
|
void ServiceGenerator::GenerateInit(io::Printer* printer)
|
|
|
|
{
|
|
|
|
printer->Print(vars_,
|
|
|
|
"void $lcfullname$__init ($cname$_Service *service,\n"
|
|
|
|
" $lcfullpadd$ $cname$_ServiceDestroy destroy)\n"
|
|
|
|
"{\n"
|
|
|
|
" protobuf_c_service_generated_init (&service->base,\n"
|
|
|
|
" &$lcfullname$__descriptor,\n"
|
|
|
|
" (ProtobufCServiceDestroy) destroy);\n"
|
|
|
|
"}\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
2008-08-24 22:11:04 +00:00
|
|
|
|
2009-02-09 23:52:18 +00:00
|
|
|
struct MethodIndexAndName { unsigned i; const char *name; };
|
|
|
|
static int
|
|
|
|
compare_method_index_and_name_by_name (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const MethodIndexAndName *ma = (const MethodIndexAndName *) a;
|
|
|
|
const MethodIndexAndName *mb = (const MethodIndexAndName *) b;
|
|
|
|
return strcmp (ma->name, mb->name);
|
|
|
|
}
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
|
|
|
|
{
|
|
|
|
int n_methods = descriptor_->method_count();
|
2009-02-09 23:52:18 +00:00
|
|
|
MethodIndexAndName *mi_array = new MethodIndexAndName[n_methods];
|
2015-03-11 01:51:48 +02:00
|
|
|
|
2015-10-30 15:54:00 -07:00
|
|
|
bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
|
2015-02-14 17:17:10 -08:00
|
|
|
descriptor_->file()->options().optimize_for() ==
|
2015-10-30 15:54:00 -07:00
|
|
|
FileOptions_OptimizeMode_CODE_SIZE;
|
2015-02-14 17:17:10 -08:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["n_methods"] = SimpleItoa(n_methods);
|
|
|
|
printer->Print(vars_, "static const ProtobufCMethodDescriptor $lcfullname$__method_descriptors[$n_methods$] =\n"
|
|
|
|
"{\n");
|
|
|
|
for (int i = 0; i < n_methods; i++) {
|
|
|
|
const MethodDescriptor *method = descriptor_->method(i);
|
|
|
|
vars_["method"] = method->name();
|
2021-03-21 19:46:21 -07:00
|
|
|
vars_["input_descriptor"] = "&" + FullNameToLower(method->input_type()->full_name(), method->input_type()->file()) + "__descriptor";
|
|
|
|
vars_["output_descriptor"] = "&" + FullNameToLower(method->output_type()->full_name(), method->output_type()->file()) + "__descriptor";
|
2015-10-30 15:54:00 -07:00
|
|
|
if (optimize_code_size) {
|
2015-02-14 17:17:10 -08:00
|
|
|
printer->Print(vars_,
|
2015-10-30 15:54:00 -07:00
|
|
|
" { NULL, $input_descriptor$, $output_descriptor$ }, /* CODE_SIZE */\n");
|
2015-02-14 17:17:10 -08:00
|
|
|
} else {
|
|
|
|
printer->Print(vars_,
|
|
|
|
" { \"$method$\", $input_descriptor$, $output_descriptor$ },\n");
|
|
|
|
}
|
2009-02-09 23:52:18 +00:00
|
|
|
mi_array[i].i = i;
|
|
|
|
mi_array[i].name = method->name().c_str();
|
|
|
|
}
|
|
|
|
printer->Print(vars_, "};\n");
|
|
|
|
|
2015-10-30 15:54:00 -07:00
|
|
|
if (!optimize_code_size) {
|
2015-02-14 17:17:10 -08:00
|
|
|
qsort ((void*)mi_array, n_methods, sizeof (MethodIndexAndName),
|
|
|
|
compare_method_index_and_name_by_name);
|
|
|
|
printer->Print(vars_, "const unsigned $lcfullname$__method_indices_by_name[] = {\n");
|
|
|
|
for (int i = 0; i < n_methods; i++) {
|
|
|
|
vars_["i"] = SimpleItoa(mi_array[i].i);
|
|
|
|
vars_["name"] = mi_array[i].name;
|
|
|
|
vars_["comma"] = (i + 1 < n_methods) ? "," : " ";
|
|
|
|
printer->Print(vars_, " $i$$comma$ /* $name$ */\n");
|
|
|
|
}
|
|
|
|
printer->Print(vars_, "};\n");
|
|
|
|
vars_["name"] = descriptor_->name();
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
2009-02-09 23:52:18 +00:00
|
|
|
|
2015-10-30 15:54:00 -07:00
|
|
|
if (optimize_code_size) {
|
2015-02-14 17:17:10 -08:00
|
|
|
printer->Print(vars_, "const ProtobufCServiceDescriptor $lcfullname$__descriptor =\n"
|
|
|
|
"{\n"
|
|
|
|
" PROTOBUF_C__SERVICE_DESCRIPTOR_MAGIC,\n"
|
2015-10-30 15:54:00 -07:00
|
|
|
" NULL,NULL,NULL,NULL, /* CODE_SIZE */\n"
|
2015-02-14 17:17:10 -08:00
|
|
|
" $n_methods$,\n"
|
|
|
|
" $lcfullname$__method_descriptors,\n"
|
2015-10-30 15:54:00 -07:00
|
|
|
" NULL /* CODE_SIZE */\n"
|
2015-02-14 17:17:10 -08:00
|
|
|
"};\n");
|
|
|
|
} else {
|
|
|
|
printer->Print(vars_, "const ProtobufCServiceDescriptor $lcfullname$__descriptor =\n"
|
|
|
|
"{\n"
|
|
|
|
" PROTOBUF_C__SERVICE_DESCRIPTOR_MAGIC,\n"
|
|
|
|
" \"$fullname$\",\n"
|
|
|
|
" \"$name$\",\n"
|
|
|
|
" \"$cname$\",\n"
|
|
|
|
" \"$package$\",\n"
|
|
|
|
" $n_methods$,\n"
|
|
|
|
" $lcfullname$__method_descriptors,\n"
|
|
|
|
" $lcfullname$__method_indices_by_name\n"
|
|
|
|
"};\n");
|
|
|
|
}
|
2014-01-11 15:38:29 -05:00
|
|
|
|
|
|
|
delete[] mi_array;
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceGenerator::GenerateCallersImplementations(io::Printer* printer)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < descriptor_->method_count(); i++) {
|
|
|
|
const MethodDescriptor *method = descriptor_->method(i);
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string lcname = CamelToLower(method->name());
|
2021-03-21 19:46:21 -07:00
|
|
|
std::string lcfullname = FullNameToLower(descriptor_->full_name(), descriptor_->file());
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["method"] = lcname;
|
|
|
|
vars_["metpad"] = ConvertToSpaces(lcname);
|
2021-03-21 19:46:21 -07:00
|
|
|
vars_["input_typename"] = FullNameToC(method->input_type()->full_name(), method->input_type()->file());
|
|
|
|
vars_["output_typename"] = FullNameToC(method->output_type()->full_name(), method->output_type()->file());
|
2008-08-22 20:38:05 +00:00
|
|
|
vars_["padddddddddddddddddd"] = ConvertToSpaces(lcfullname + "__" + lcname);
|
|
|
|
vars_["index"] = SimpleItoa(i);
|
2015-03-11 01:51:48 +02:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
printer->Print(vars_,
|
|
|
|
"void $lcfullname$__$method$(ProtobufCService *service,\n"
|
|
|
|
" $padddddddddddddddddd$ const $input_typename$ *input,\n"
|
2008-08-26 14:45:34 +00:00
|
|
|
" $padddddddddddddddddd$ $output_typename$_Closure closure,\n"
|
|
|
|
" $padddddddddddddddddd$ void *closure_data)\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"{\n"
|
2014-06-04 17:14:44 -04:00
|
|
|
" assert(service->descriptor == &$lcfullname$__descriptor);\n"
|
2008-08-26 14:45:34 +00:00
|
|
|
" service->invoke(service, $index$, (const ProtobufCMessage *) input, (ProtobufCClosure) closure, closure_data);\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace c
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
} // namespace google
|