1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: kenton@google.com (Kenton Varda)
32
// Based on original Protocol Buffers design by
33
// Sanjay Ghemawat, Jeff Dean, and others.
35
#include <google/protobuf/compiler/cpp/cpp_file.h>
36
#include <google/protobuf/compiler/cpp/cpp_enum.h>
37
#include <google/protobuf/compiler/cpp/cpp_service.h>
38
#include <google/protobuf/compiler/cpp/cpp_extension.h>
39
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
40
#include <google/protobuf/compiler/cpp/cpp_message.h>
41
#include <google/protobuf/compiler/cpp/cpp_field.h>
42
#include <google/protobuf/io/printer.h>
43
#include <google/protobuf/descriptor.pb.h>
44
#include <google/protobuf/stubs/strutil.h>
51
// ===================================================================
53
FileGenerator::FileGenerator(const FileDescriptor* file,
54
const string& dllexport_decl)
57
new scoped_ptr<MessageGenerator>[file->message_type_count()]),
59
new scoped_ptr<EnumGenerator>[file->enum_type_count()]),
61
new scoped_ptr<ServiceGenerator>[file->service_count()]),
62
extension_generators_(
63
new scoped_ptr<ExtensionGenerator>[file->extension_count()]),
64
dllexport_decl_(dllexport_decl) {
66
for (int i = 0; i < file->message_type_count(); i++) {
67
message_generators_[i].reset(
68
new MessageGenerator(file->message_type(i), dllexport_decl));
71
for (int i = 0; i < file->enum_type_count(); i++) {
72
enum_generators_[i].reset(
73
new EnumGenerator(file->enum_type(i), dllexport_decl));
76
for (int i = 0; i < file->service_count(); i++) {
77
service_generators_[i].reset(
78
new ServiceGenerator(file->service(i), dllexport_decl));
81
for (int i = 0; i < file->extension_count(); i++) {
82
extension_generators_[i].reset(
83
new ExtensionGenerator(file->extension(i), dllexport_decl));
86
SplitStringUsing(file_->package(), ".", &package_parts_);
89
FileGenerator::~FileGenerator() {}
91
void FileGenerator::GenerateHeader(io::Printer* printer) {
92
string filename_identifier = FilenameIdentifier(file_->name());
94
// Generate top of header.
96
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
97
"// source: $filename$\n"
99
"#ifndef PROTOBUF_$filename_identifier$__INCLUDED\n"
100
"#define PROTOBUF_$filename_identifier$__INCLUDED\n"
102
"#include <string>\n"
104
"filename", file_->name(),
105
"filename_identifier", filename_identifier);
108
"#include <google/protobuf/stubs/common.h>\n"
111
// Verify the protobuf library header version is compatible with the protoc
112
// version before going any further.
114
"#if GOOGLE_PROTOBUF_VERSION < $min_header_version$\n"
115
"#error This file was generated by a newer version of protoc which is\n"
116
"#error incompatible with your Protocol Buffer headers. Please update\n"
117
"#error your headers.\n"
119
"#if $protoc_version$ < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION\n"
120
"#error This file was generated by an older version of protoc which is\n"
121
"#error incompatible with your Protocol Buffer headers. Please\n"
122
"#error regenerate this file with a newer version of protoc.\n"
125
"min_header_version",
126
SimpleItoa(protobuf::internal::kMinHeaderVersionForProtoc),
127
"protoc_version", SimpleItoa(GOOGLE_PROTOBUF_VERSION));
129
// OK, it's now safe to #include other files.
131
"#include <google/protobuf/generated_message_util.h>\n"
132
"#include <google/protobuf/repeated_field.h>\n"
133
"#include <google/protobuf/extension_set.h>\n");
135
if (HasDescriptorMethods(file_)) {
137
"#include <google/protobuf/generated_message_reflection.h>\n");
140
if (HasGenericServices(file_)) {
142
"#include <google/protobuf/service.h>\n");
146
for (int i = 0; i < file_->dependency_count(); i++) {
148
"#include \"$dependency$.pb.h\"\n",
149
"dependency", StripProto(file_->dependency(i)->name()));
153
"// @@protoc_insertion_point(includes)\n");
156
GenerateNamespaceOpeners(printer);
158
// Forward-declare the AddDescriptors, AssignDescriptors, and ShutdownFile
159
// functions, so that we can declare them to be friends of each class.
162
"// Internal implementation detail -- do not call these.\n"
163
"void $dllexport_decl$ $adddescriptorsname$();\n",
164
"adddescriptorsname", GlobalAddDescriptorsName(file_->name()),
165
"dllexport_decl", dllexport_decl_);
168
// Note that we don't put dllexport_decl on these because they are only
169
// called by the .pb.cc file in which they are defined.
170
"void $assigndescriptorsname$();\n"
171
"void $shutdownfilename$();\n"
173
"assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()),
174
"shutdownfilename", GlobalShutdownFileName(file_->name()));
176
// Generate forward declarations of classes.
177
for (int i = 0; i < file_->message_type_count(); i++) {
178
message_generators_[i]->GenerateForwardDeclaration(printer);
181
printer->Print("\n");
183
// Generate enum definitions.
184
for (int i = 0; i < file_->message_type_count(); i++) {
185
message_generators_[i]->GenerateEnumDefinitions(printer);
187
for (int i = 0; i < file_->enum_type_count(); i++) {
188
enum_generators_[i]->GenerateDefinition(printer);
191
printer->Print(kThickSeparator);
192
printer->Print("\n");
194
// Generate class definitions.
195
for (int i = 0; i < file_->message_type_count(); i++) {
197
printer->Print("\n");
198
printer->Print(kThinSeparator);
199
printer->Print("\n");
201
message_generators_[i]->GenerateClassDefinition(printer);
204
printer->Print("\n");
205
printer->Print(kThickSeparator);
206
printer->Print("\n");
208
if (HasGenericServices(file_)) {
209
// Generate service definitions.
210
for (int i = 0; i < file_->service_count(); i++) {
212
printer->Print("\n");
213
printer->Print(kThinSeparator);
214
printer->Print("\n");
216
service_generators_[i]->GenerateDeclarations(printer);
219
printer->Print("\n");
220
printer->Print(kThickSeparator);
221
printer->Print("\n");
224
// Declare extension identifiers.
225
for (int i = 0; i < file_->extension_count(); i++) {
226
extension_generators_[i]->GenerateDeclaration(printer);
229
printer->Print("\n");
230
printer->Print(kThickSeparator);
231
printer->Print("\n");
233
// Generate class inline methods.
234
for (int i = 0; i < file_->message_type_count(); i++) {
236
printer->Print(kThinSeparator);
237
printer->Print("\n");
239
message_generators_[i]->GenerateInlineMethods(printer);
244
"// @@protoc_insertion_point(namespace_scope)\n");
246
// Close up namespace.
247
GenerateNamespaceClosers(printer);
249
// Emit GetEnumDescriptor specializations into google::protobuf namespace:
250
if (HasDescriptorMethods(file_)) {
251
// The SWIG conditional is to avoid a null-pointer dereference
252
// (bug 1984964) in swig-1.3.21 resulting from the following syntax:
253
// namespace X { void Y<Z::W>(); }
254
// which appears in GetEnumDescriptor() specializations.
258
"namespace google {\nnamespace protobuf {\n"
260
for (int i = 0; i < file_->message_type_count(); i++) {
261
message_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
263
for (int i = 0; i < file_->enum_type_count(); i++) {
264
enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
268
"} // namespace google\n} // namespace protobuf\n"
274
"// @@protoc_insertion_point(global_scope)\n"
278
"#endif // PROTOBUF_$filename_identifier$__INCLUDED\n",
279
"filename_identifier", filename_identifier);
282
void FileGenerator::GenerateSource(io::Printer* printer) {
284
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
286
// The generated code calls accessors that might be deprecated. We don't
287
// want the compiler to warn in generated code.
288
"#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION\n"
289
"#include \"$basename$.pb.h\"\n"
291
"#include <algorithm>\n" // for swap()
293
"#include <google/protobuf/stubs/once.h>\n"
294
"#include <google/protobuf/io/coded_stream.h>\n"
295
"#include <google/protobuf/wire_format_lite_inl.h>\n",
296
"basename", StripProto(file_->name()));
298
if (HasDescriptorMethods(file_)) {
300
"#include <google/protobuf/descriptor.h>\n"
301
"#include <google/protobuf/reflection_ops.h>\n"
302
"#include <google/protobuf/wire_format.h>\n");
306
"// @@protoc_insertion_point(includes)\n");
308
GenerateNamespaceOpeners(printer);
310
if (HasDescriptorMethods(file_)) {
315
for (int i = 0; i < file_->message_type_count(); i++) {
316
message_generators_[i]->GenerateDescriptorDeclarations(printer);
318
for (int i = 0; i < file_->enum_type_count(); i++) {
320
"const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n",
321
"name", ClassName(file_->enum_type(i), false));
324
if (HasGenericServices(file_)) {
325
for (int i = 0; i < file_->service_count(); i++) {
327
"const ::google::protobuf::ServiceDescriptor* $name$_descriptor_ = NULL;\n",
328
"name", file_->service(i)->name());
338
// Define our externally-visible BuildDescriptors() function. (For the lite
339
// library, all this does is initialize default instances.)
340
GenerateBuildDescriptors(printer);
343
for (int i = 0; i < file_->enum_type_count(); i++) {
344
enum_generators_[i]->GenerateMethods(printer);
348
for (int i = 0; i < file_->message_type_count(); i++) {
349
printer->Print("\n");
350
printer->Print(kThickSeparator);
351
printer->Print("\n");
352
message_generators_[i]->GenerateClassMethods(printer);
355
if (HasGenericServices(file_)) {
356
// Generate services.
357
for (int i = 0; i < file_->service_count(); i++) {
358
if (i == 0) printer->Print("\n");
359
printer->Print(kThickSeparator);
360
printer->Print("\n");
361
service_generators_[i]->GenerateImplementation(printer);
365
// Define extensions.
366
for (int i = 0; i < file_->extension_count(); i++) {
367
extension_generators_[i]->GenerateDefinition(printer);
372
"// @@protoc_insertion_point(namespace_scope)\n");
374
GenerateNamespaceClosers(printer);
378
"// @@protoc_insertion_point(global_scope)\n");
381
void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
382
// AddDescriptors() is a file-level procedure which adds the encoded
383
// FileDescriptorProto for this .proto file to the global DescriptorPool
384
// for generated files (DescriptorPool::generated_pool()). It always runs
385
// at static initialization time, so all files will be registered before
386
// main() starts. This procedure also constructs default instances and
387
// registers extensions.
389
// Its sibling, AssignDescriptors(), actually pulls the compiled
390
// FileDescriptor from the DescriptorPool and uses it to populate all of
391
// the global variables which store pointers to the descriptor objects.
392
// It also constructs the reflection objects. It is called the first time
393
// anyone calls descriptor() or GetReflection() on one of the types defined
396
// In optimize_for = LITE_RUNTIME mode, we don't generate AssignDescriptors()
397
// and we only use AddDescriptors() to allocate default instances.
398
if (HasDescriptorMethods(file_)) {
401
"void $assigndescriptorsname$() {\n",
402
"assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()));
405
// Make sure the file has found its way into the pool. If a descriptor
406
// is requested *during* static init then AddDescriptors() may not have
407
// been called yet, so we call it manually. Note that it's fine if
408
// AddDescriptors() is called multiple times.
410
"$adddescriptorsname$();\n",
411
"adddescriptorsname", GlobalAddDescriptorsName(file_->name()));
413
// Get the file's descriptor from the pool.
415
"const ::google::protobuf::FileDescriptor* file =\n"
416
" ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName(\n"
417
" \"$filename$\");\n"
418
// Note that this GOOGLE_CHECK is necessary to prevent a warning about "file"
419
// being unused when compiling an empty .proto file.
420
"GOOGLE_CHECK(file != NULL);\n",
421
"filename", file_->name());
423
// Go through all the stuff defined in this file and generated code to
424
// assign the global descriptor pointers based on the file descriptor.
425
for (int i = 0; i < file_->message_type_count(); i++) {
426
message_generators_[i]->GenerateDescriptorInitializer(printer, i);
428
for (int i = 0; i < file_->enum_type_count(); i++) {
429
enum_generators_[i]->GenerateDescriptorInitializer(printer, i);
431
if (HasGenericServices(file_)) {
432
for (int i = 0; i < file_->service_count(); i++) {
433
service_generators_[i]->GenerateDescriptorInitializer(printer, i);
442
// ---------------------------------------------------------------
444
// protobuf_AssignDescriptorsOnce(): The first time it is called, calls
445
// AssignDescriptors(). All later times, waits for the first call to
446
// complete and then returns.
450
"GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);\n"
451
"inline void protobuf_AssignDescriptorsOnce() {\n"
452
" ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,\n"
453
" &$assigndescriptorsname$);\n"
456
"assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()));
458
// protobuf_RegisterTypes(): Calls
459
// MessageFactory::InternalRegisterGeneratedType() for each message type.
461
"void protobuf_RegisterTypes(const ::std::string&) {\n"
462
" protobuf_AssignDescriptorsOnce();\n");
465
for (int i = 0; i < file_->message_type_count(); i++) {
466
message_generators_[i]->GenerateTypeRegistrations(printer);
476
// -----------------------------------------------------------------
478
// ShutdownFile(): Deletes descriptors, default instances, etc. on shutdown.
481
"void $shutdownfilename$() {\n",
482
"shutdownfilename", GlobalShutdownFileName(file_->name()));
485
for (int i = 0; i < file_->message_type_count(); i++) {
486
message_generators_[i]->GenerateShutdownCode(printer);
493
// -----------------------------------------------------------------
495
// Now generate the AddDescriptors() function.
498
"void $adddescriptorsname$() {\n"
499
// We don't need any special synchronization here because this code is
500
// called at static init time before any threads exist.
501
" static bool already_here = false;\n"
502
" if (already_here) return;\n"
503
" already_here = true;\n"
504
" GOOGLE_PROTOBUF_VERIFY_VERSION;\n"
506
"adddescriptorsname", GlobalAddDescriptorsName(file_->name()));
509
// Call the AddDescriptors() methods for all of our dependencies, to make
510
// sure they get added first.
511
for (int i = 0; i < file_->dependency_count(); i++) {
512
const FileDescriptor* dependency = file_->dependency(i);
513
// Print the namespace prefix for the dependency.
514
vector<string> dependency_package_parts;
515
SplitStringUsing(dependency->package(), ".", &dependency_package_parts);
516
printer->Print("::");
517
for (int i = 0; i < dependency_package_parts.size(); i++) {
518
printer->Print("$name$::",
519
"name", dependency_package_parts[i]);
521
// Call its AddDescriptors function.
524
"name", GlobalAddDescriptorsName(dependency->name()));
527
if (HasDescriptorMethods(file_)) {
528
// Embed the descriptor. We simply serialize the entire FileDescriptorProto
529
// and embed it as a string literal, which is parsed and built into real
530
// descriptors at initialization time.
531
FileDescriptorProto file_proto;
532
file_->CopyTo(&file_proto);
534
file_proto.SerializeToString(&file_data);
537
"::google::protobuf::DescriptorPool::InternalAddGeneratedFile(");
539
// Only write 40 bytes per line.
540
static const int kBytesPerLine = 40;
541
for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
542
printer->Print("\n \"$data$\"",
543
"data", CEscape(file_data.substr(i, kBytesPerLine)));
547
"size", SimpleItoa(file_data.size()));
549
// Call MessageFactory::InternalRegisterGeneratedFile().
551
"::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(\n"
552
" \"$filename$\", &protobuf_RegisterTypes);\n",
553
"filename", file_->name());
556
// Allocate and initialize default instances. This can't be done lazily
557
// since default instances are returned by simple accessors and are used with
558
// extensions. Speaking of which, we also register extensions at this time.
559
for (int i = 0; i < file_->message_type_count(); i++) {
560
message_generators_[i]->GenerateDefaultInstanceAllocator(printer);
562
for (int i = 0; i < file_->extension_count(); i++) {
563
extension_generators_[i]->GenerateRegistration(printer);
565
for (int i = 0; i < file_->message_type_count(); i++) {
566
message_generators_[i]->GenerateDefaultInstanceInitializer(printer);
570
"::google::protobuf::internal::OnShutdown(&$shutdownfilename$);\n",
571
"shutdownfilename", GlobalShutdownFileName(file_->name()));
578
"// Force AddDescriptors() to be called at static initialization time.\n"
579
"struct StaticDescriptorInitializer_$filename$ {\n"
580
" StaticDescriptorInitializer_$filename$() {\n"
581
" $adddescriptorsname$();\n"
583
"} static_descriptor_initializer_$filename$_;\n"
585
"adddescriptorsname", GlobalAddDescriptorsName(file_->name()),
586
"filename", FilenameIdentifier(file_->name()));
589
void FileGenerator::GenerateNamespaceOpeners(io::Printer* printer) {
590
if (package_parts_.size() > 0) printer->Print("\n");
592
for (int i = 0; i < package_parts_.size(); i++) {
593
printer->Print("namespace $part$ {\n",
594
"part", package_parts_[i]);
598
void FileGenerator::GenerateNamespaceClosers(io::Printer* printer) {
599
if (package_parts_.size() > 0) printer->Print("\n");
601
for (int i = package_parts_.size() - 1; i >= 0; i--) {
602
printer->Print("} // namespace $part$\n",
603
"part", package_parts_[i]);
608
} // namespace compiler
609
} // namespace protobuf
610
} // namespace google