1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://code.google.com/p/protobuf/
//
// 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.
// * 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.
//
// 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.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// Edited by Simon Newton for OLA
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <map>
#include <memory>
#include <string>
#include "protoc/CppFileGenerator.h"
#include "protoc/GeneratorHelpers.h"
#include "protoc/ServiceGenerator.h"
#include "protoc/StrUtil.h"
namespace ola {
using google::protobuf::FileDescriptor;
using google::protobuf::ServiceDescriptor;
using google::protobuf::io::Printer;
using std::auto_ptr;
using std::string;
FileGenerator::FileGenerator(const FileDescriptor *file,
const string &output_name)
: m_file(file),
m_output_name(output_name) {
SplitStringUsing(file->package(), ".", &package_parts_);
ServiceGenerator::Options options;
for (int i = 0; i < file->service_count(); i++) {
m_service_generators.push_back(
new ServiceGenerator(file->service(i), options));
}
}
FileGenerator::~FileGenerator() {
ServiceGenerators::iterator iter = m_service_generators.begin();
for (; iter != m_service_generators.end(); ++iter) {
delete *iter;
}
}
void FileGenerator::GenerateHeader(Printer *printer) {
const string filename_identifier = FilenameIdentifier(m_output_name);
std::map<string, string> var_map;
var_map["basename"] = StripProto(m_file->name());
var_map["filename"] = m_file->name();
var_map["filename_identifier"] = filename_identifier;
// Generate top of header.
printer->Print(
var_map,
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
"\n"
"#ifndef PROTOBUF_$filename_identifier$__INCLUDED "
"// NOLINT(build/header_guard)\n"
"#define PROTOBUF_$filename_identifier$__INCLUDED\n"
"\n"
"#include <google/protobuf/service.h>\n"
"\n"
"#include \"$basename$.pb.h\"\n"
"#include \"common/rpc/RpcService.h\"\n"
"\n"
"namespace ola {\n"
"namespace rpc {\n"
"class RpcController;\n"
"class RpcChannel;\n"
"} // rpc\n"
"} // ola\n"
"\n");
GenerateNamespaceOpeners(printer);
ServiceGenerators::iterator iter = m_service_generators.begin();
for (; iter != m_service_generators.end(); ++iter) {
(*iter)->GenerateDeclarations(printer);
}
GenerateNamespaceClosers(printer);
printer->Print(
"#endif // PROTOBUF_$filename_identifier$__INCLUDED\n",
"filename_identifier", filename_identifier);
}
void FileGenerator::GenerateImplementation(Printer *printer) {
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
"\n"
// TODO(Peter): This should be a full path to remove the lint error
"#include \"$file$.pb.h\"\n"
"\n"
"#include <google/protobuf/descriptor.h> // NOLINT(build/include)\n"
"#include <google/protobuf/stubs/once.h>\n"
"\n"
"#include \"common/rpc/RpcChannel.h\"\n"
"#include \"common/rpc/RpcController.h\"\n"
"\n",
"file", m_output_name,
"filename", m_file->name());
GenerateNamespaceOpeners(printer);
printer->Print(
"\n"
"namespace {\n"
"\n");
for (int i = 0; i < m_file->service_count(); i++) {
printer->Print(
"const ::google::protobuf::ServiceDescriptor* $name$_descriptor_ =\n"
" NULL;\n",
"name", m_file->service(i)->name());
}
printer->Print(
"\n"
"} // namespace\n"
"\n");
// Define our externally-visible BuildDescriptors() function. (For the lite
// library, all this does is initialize default instances.)
GenerateBuildDescriptors(printer);
printer->Print("\n");
printer->Print(kThickSeparator);
printer->Print("\n");
ServiceGenerators::iterator iter = m_service_generators.begin();
for (; iter != m_service_generators.end(); ++iter) {
(*iter)->GenerateImplementation(printer);
}
GenerateNamespaceClosers(printer);
}
void FileGenerator::GenerateBuildDescriptors(Printer* printer) {
// AddDescriptors() is a file-level procedure which adds the encoded
// FileDescriptorProto for this .proto file to the global DescriptorPool for
// generated files (DescriptorPool::generated_pool()). It either runs at
// static initialization time (by default) or when default_instance() is
// called for the first time (in LITE_RUNTIME mode with
// GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER flag enabled). This procedure also
// constructs default instances and registers extensions.
//
// Its sibling, AssignDescriptors(), actually pulls the compiled
// FileDescriptor from the DescriptorPool and uses it to populate all of
// the global variables which store pointers to the descriptor objects.
// It also constructs the reflection objects. It is called the first time
// anyone calls descriptor() or GetReflection() on one of the types defined
// in the file.
// In optimize_for = LITE_RUNTIME mode, we don't generate AssignDescriptors()
// and we only use AddDescriptors() to allocate default instances.
if (HasDescriptorMethods(m_file)) {
printer->Print(
"\n"
"void $assigndescriptorsname$() {\n",
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
printer->Indent();
// No longer needed since protobuf 3.2
#if GOOGLE_PROTOBUF_VERSION < 3002000
// Make sure the file has found its way into the pool. If a descriptor
// is requested *during* static init then AddDescriptors() may not have
// been called yet, so we call it manually. Note that it's fine if
// AddDescriptors() is called multiple times.
printer->Print(
"$adddescriptorsname$();\n",
"adddescriptorsname", GlobalAddDescriptorsName(m_file->name()));
#endif
// Get the file's descriptor from the pool.
printer->Print(
"const ::google::protobuf::FileDescriptor* file =\n"
" ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName("
"\n"
" \"$filename$\");\n"
// Note that this GOOGLE_CHECK is necessary to prevent a warning about
// "file" being unused when compiling an empty .proto file.
"GOOGLE_CHECK(file != NULL);\n",
"filename", m_file->name());
for (int i = 0; i < m_file->service_count(); i++) {
m_service_generators[i]->GenerateDescriptorInitializer(printer, i);
}
printer->Outdent();
printer->Print(
"}\n"
"\n");
// ---------------------------------------------------------------
// protobuf_AssignDescriptorsOnce(): The first time it is called, calls
// AssignDescriptors(). All later times, waits for the first call to
// complete and then returns.
// We need to generate different code, depending on the version
// of protobuf we compile against
#if GOOGLE_PROTOBUF_VERSION < 3007000
printer->Print(
"namespace {\n"
"\n"
"GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);\n"
"inline void protobuf_AssignDescriptorsOnce() {\n"
" ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,"
"\n"
" &$assigndescriptorsname$);\n"
"}\n"
"\n",
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
printer->Print("} // namespace\n");
#else
printer->Print(
"namespace {\n"
"\n"
"inline void protobuf_AssignDescriptorsOnce() {\n"
" static ::google::protobuf::internal::once_flag once;\n"
" ::google::protobuf::internal::call_once(once,\n"
" &$assigndescriptorsname$);\n"
"}\n"
"\n",
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
printer->Print("} // namespace\n");
#endif
}
}
void FileGenerator::GenerateNamespaceOpeners(Printer* printer) {
if (package_parts_.size() > 0) printer->Print("\n");
for (unsigned int i = 0; i < package_parts_.size(); i++) {
printer->Print("namespace $part$ {\n", "part", package_parts_[i]);
}
}
void FileGenerator::GenerateNamespaceClosers(Printer* printer) {
if (package_parts_.size() > 0) printer->Print("\n");
for (int i = package_parts_.size() - 1; i >= 0; i--) {
printer->Print("} // namespace $part$\n",
"part", package_parts_[i]);
}
}
} // namespace ola
|