~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to protobuf/files/src/google/protobuf/compiler/cpp/cpp_file.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Protocol Buffers - Google's data interchange format
2
 
// Copyright 2008 Google Inc.  All rights reserved.
3
 
// http://code.google.com/p/protobuf/
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
 
// Author: kenton@google.com (Kenton Varda)
32
 
//  Based on original Protocol Buffers design by
33
 
//  Sanjay Ghemawat, Jeff Dean, and others.
34
 
 
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>
45
 
 
46
 
namespace google {
47
 
namespace protobuf {
48
 
namespace compiler {
49
 
namespace cpp {
50
 
 
51
 
// ===================================================================
52
 
 
53
 
FileGenerator::FileGenerator(const FileDescriptor* file,
54
 
                             const string& dllexport_decl)
55
 
  : file_(file),
56
 
    message_generators_(
57
 
      new scoped_ptr<MessageGenerator>[file->message_type_count()]),
58
 
    enum_generators_(
59
 
      new scoped_ptr<EnumGenerator>[file->enum_type_count()]),
60
 
    service_generators_(
61
 
      new scoped_ptr<ServiceGenerator>[file->service_count()]),
62
 
    extension_generators_(
63
 
      new scoped_ptr<ExtensionGenerator>[file->extension_count()]),
64
 
    dllexport_decl_(dllexport_decl) {
65
 
 
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));
69
 
  }
70
 
 
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));
74
 
  }
75
 
 
76
 
  for (int i = 0; i < file->service_count(); i++) {
77
 
    service_generators_[i].reset(
78
 
      new ServiceGenerator(file->service(i), dllexport_decl));
79
 
  }
80
 
 
81
 
  for (int i = 0; i < file->extension_count(); i++) {
82
 
    extension_generators_[i].reset(
83
 
      new ExtensionGenerator(file->extension(i), dllexport_decl));
84
 
  }
85
 
 
86
 
  SplitStringUsing(file_->package(), ".", &package_parts_);
87
 
}
88
 
 
89
 
FileGenerator::~FileGenerator() {}
90
 
 
91
 
void FileGenerator::GenerateHeader(io::Printer* printer) {
92
 
  string filename_identifier = FilenameIdentifier(file_->name());
93
 
 
94
 
  // Generate top of header.
95
 
  printer->Print(
96
 
    "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
97
 
    "// source: $filename$\n"
98
 
    "\n"
99
 
    "#ifndef PROTOBUF_$filename_identifier$__INCLUDED\n"
100
 
    "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
101
 
    "\n"
102
 
    "#include <string>\n"
103
 
    "\n",
104
 
    "filename", file_->name(),
105
 
    "filename_identifier", filename_identifier);
106
 
 
107
 
  printer->Print(
108
 
    "#include <google/protobuf/stubs/common.h>\n"
109
 
    "\n");
110
 
 
111
 
  // Verify the protobuf library header version is compatible with the protoc
112
 
  // version before going any further.
113
 
  printer->Print(
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"
118
 
    "#endif\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"
123
 
    "#endif\n"
124
 
    "\n",
125
 
    "min_header_version",
126
 
      SimpleItoa(protobuf::internal::kMinHeaderVersionForProtoc),
127
 
    "protoc_version", SimpleItoa(GOOGLE_PROTOBUF_VERSION));
128
 
 
129
 
  // OK, it's now safe to #include other files.
130
 
  printer->Print(
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");
134
 
 
135
 
  if (HasDescriptorMethods(file_)) {
136
 
    printer->Print(
137
 
      "#include <google/protobuf/generated_message_reflection.h>\n");
138
 
  }
139
 
 
140
 
  if (HasGenericServices(file_)) {
141
 
    printer->Print(
142
 
      "#include <google/protobuf/service.h>\n");
143
 
  }
144
 
 
145
 
 
146
 
  for (int i = 0; i < file_->dependency_count(); i++) {
147
 
    printer->Print(
148
 
      "#include \"$dependency$.pb.h\"\n",
149
 
      "dependency", StripProto(file_->dependency(i)->name()));
150
 
  }
151
 
 
152
 
  printer->Print(
153
 
    "// @@protoc_insertion_point(includes)\n");
154
 
 
155
 
  // Open namespace.
156
 
  GenerateNamespaceOpeners(printer);
157
 
 
158
 
  // Forward-declare the AddDescriptors, AssignDescriptors, and ShutdownFile
159
 
  // functions, so that we can declare them to be friends of each class.
160
 
  printer->Print(
161
 
    "\n"
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_);
166
 
 
167
 
  printer->Print(
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"
172
 
    "\n",
173
 
    "assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()),
174
 
    "shutdownfilename", GlobalShutdownFileName(file_->name()));
175
 
 
176
 
  // Generate forward declarations of classes.
177
 
  for (int i = 0; i < file_->message_type_count(); i++) {
178
 
    message_generators_[i]->GenerateForwardDeclaration(printer);
179
 
  }
180
 
 
181
 
  printer->Print("\n");
182
 
 
183
 
  // Generate enum definitions.
184
 
  for (int i = 0; i < file_->message_type_count(); i++) {
185
 
    message_generators_[i]->GenerateEnumDefinitions(printer);
186
 
  }
187
 
  for (int i = 0; i < file_->enum_type_count(); i++) {
188
 
    enum_generators_[i]->GenerateDefinition(printer);
189
 
  }
190
 
 
191
 
  printer->Print(kThickSeparator);
192
 
  printer->Print("\n");
193
 
 
194
 
  // Generate class definitions.
195
 
  for (int i = 0; i < file_->message_type_count(); i++) {
196
 
    if (i > 0) {
197
 
      printer->Print("\n");
198
 
      printer->Print(kThinSeparator);
199
 
      printer->Print("\n");
200
 
    }
201
 
    message_generators_[i]->GenerateClassDefinition(printer);
202
 
  }
203
 
 
204
 
  printer->Print("\n");
205
 
  printer->Print(kThickSeparator);
206
 
  printer->Print("\n");
207
 
 
208
 
  if (HasGenericServices(file_)) {
209
 
    // Generate service definitions.
210
 
    for (int i = 0; i < file_->service_count(); i++) {
211
 
      if (i > 0) {
212
 
        printer->Print("\n");
213
 
        printer->Print(kThinSeparator);
214
 
        printer->Print("\n");
215
 
      }
216
 
      service_generators_[i]->GenerateDeclarations(printer);
217
 
    }
218
 
 
219
 
    printer->Print("\n");
220
 
    printer->Print(kThickSeparator);
221
 
    printer->Print("\n");
222
 
  }
223
 
 
224
 
  // Declare extension identifiers.
225
 
  for (int i = 0; i < file_->extension_count(); i++) {
226
 
    extension_generators_[i]->GenerateDeclaration(printer);
227
 
  }
228
 
 
229
 
  printer->Print("\n");
230
 
  printer->Print(kThickSeparator);
231
 
  printer->Print("\n");
232
 
 
233
 
  // Generate class inline methods.
234
 
  for (int i = 0; i < file_->message_type_count(); i++) {
235
 
    if (i > 0) {
236
 
      printer->Print(kThinSeparator);
237
 
      printer->Print("\n");
238
 
    }
239
 
    message_generators_[i]->GenerateInlineMethods(printer);
240
 
  }
241
 
 
242
 
  printer->Print(
243
 
    "\n"
244
 
    "// @@protoc_insertion_point(namespace_scope)\n");
245
 
 
246
 
  // Close up namespace.
247
 
  GenerateNamespaceClosers(printer);
248
 
 
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.
255
 
    printer->Print(
256
 
        "\n"
257
 
        "#ifndef SWIG\n"
258
 
        "namespace google {\nnamespace protobuf {\n"
259
 
        "\n");
260
 
    for (int i = 0; i < file_->message_type_count(); i++) {
261
 
      message_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
262
 
    }
263
 
    for (int i = 0; i < file_->enum_type_count(); i++) {
264
 
      enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
265
 
    }
266
 
    printer->Print(
267
 
        "\n"
268
 
        "}  // namespace google\n}  // namespace protobuf\n"
269
 
        "#endif  // SWIG\n");
270
 
  }
271
 
 
272
 
  printer->Print(
273
 
    "\n"
274
 
    "// @@protoc_insertion_point(global_scope)\n"
275
 
    "\n");
276
 
 
277
 
  printer->Print(
278
 
    "#endif  // PROTOBUF_$filename_identifier$__INCLUDED\n",
279
 
    "filename_identifier", filename_identifier);
280
 
}
281
 
 
282
 
void FileGenerator::GenerateSource(io::Printer* printer) {
283
 
  printer->Print(
284
 
    "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
285
 
    "\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"
290
 
    "\n"
291
 
    "#include <algorithm>\n"    // for swap()
292
 
    "\n"
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()));
297
 
 
298
 
  if (HasDescriptorMethods(file_)) {
299
 
    printer->Print(
300
 
      "#include <google/protobuf/descriptor.h>\n"
301
 
      "#include <google/protobuf/reflection_ops.h>\n"
302
 
      "#include <google/protobuf/wire_format.h>\n");
303
 
  }
304
 
 
305
 
  printer->Print(
306
 
    "// @@protoc_insertion_point(includes)\n");
307
 
 
308
 
  GenerateNamespaceOpeners(printer);
309
 
 
310
 
  if (HasDescriptorMethods(file_)) {
311
 
    printer->Print(
312
 
      "\n"
313
 
      "namespace {\n"
314
 
      "\n");
315
 
    for (int i = 0; i < file_->message_type_count(); i++) {
316
 
      message_generators_[i]->GenerateDescriptorDeclarations(printer);
317
 
    }
318
 
    for (int i = 0; i < file_->enum_type_count(); i++) {
319
 
      printer->Print(
320
 
        "const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n",
321
 
        "name", ClassName(file_->enum_type(i), false));
322
 
    }
323
 
 
324
 
    if (HasGenericServices(file_)) {
325
 
      for (int i = 0; i < file_->service_count(); i++) {
326
 
        printer->Print(
327
 
          "const ::google::protobuf::ServiceDescriptor* $name$_descriptor_ = NULL;\n",
328
 
          "name", file_->service(i)->name());
329
 
      }
330
 
    }
331
 
 
332
 
    printer->Print(
333
 
      "\n"
334
 
      "}  // namespace\n"
335
 
      "\n");
336
 
  }
337
 
 
338
 
  // Define our externally-visible BuildDescriptors() function.  (For the lite
339
 
  // library, all this does is initialize default instances.)
340
 
  GenerateBuildDescriptors(printer);
341
 
 
342
 
  // Generate enums.
343
 
  for (int i = 0; i < file_->enum_type_count(); i++) {
344
 
    enum_generators_[i]->GenerateMethods(printer);
345
 
  }
346
 
 
347
 
  // Generate classes.
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);
353
 
  }
354
 
 
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);
362
 
    }
363
 
  }
364
 
 
365
 
  // Define extensions.
366
 
  for (int i = 0; i < file_->extension_count(); i++) {
367
 
    extension_generators_[i]->GenerateDefinition(printer);
368
 
  }
369
 
 
370
 
  printer->Print(
371
 
    "\n"
372
 
    "// @@protoc_insertion_point(namespace_scope)\n");
373
 
 
374
 
  GenerateNamespaceClosers(printer);
375
 
 
376
 
  printer->Print(
377
 
    "\n"
378
 
    "// @@protoc_insertion_point(global_scope)\n");
379
 
}
380
 
 
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.
388
 
  //
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
394
 
  // in the file.
395
 
 
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_)) {
399
 
    printer->Print(
400
 
      "\n"
401
 
      "void $assigndescriptorsname$() {\n",
402
 
      "assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()));
403
 
    printer->Indent();
404
 
 
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.
409
 
    printer->Print(
410
 
      "$adddescriptorsname$();\n",
411
 
      "adddescriptorsname", GlobalAddDescriptorsName(file_->name()));
412
 
 
413
 
    // Get the file's descriptor from the pool.
414
 
    printer->Print(
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());
422
 
 
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);
427
 
    }
428
 
    for (int i = 0; i < file_->enum_type_count(); i++) {
429
 
      enum_generators_[i]->GenerateDescriptorInitializer(printer, i);
430
 
    }
431
 
    if (HasGenericServices(file_)) {
432
 
      for (int i = 0; i < file_->service_count(); i++) {
433
 
        service_generators_[i]->GenerateDescriptorInitializer(printer, i);
434
 
      }
435
 
    }
436
 
 
437
 
    printer->Outdent();
438
 
    printer->Print(
439
 
      "}\n"
440
 
      "\n");
441
 
 
442
 
    // ---------------------------------------------------------------
443
 
 
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.
447
 
    printer->Print(
448
 
      "namespace {\n"
449
 
      "\n"
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"
454
 
      "}\n"
455
 
      "\n",
456
 
      "assigndescriptorsname", GlobalAssignDescriptorsName(file_->name()));
457
 
 
458
 
    // protobuf_RegisterTypes():  Calls
459
 
    // MessageFactory::InternalRegisterGeneratedType() for each message type.
460
 
    printer->Print(
461
 
      "void protobuf_RegisterTypes(const ::std::string&) {\n"
462
 
      "  protobuf_AssignDescriptorsOnce();\n");
463
 
    printer->Indent();
464
 
 
465
 
    for (int i = 0; i < file_->message_type_count(); i++) {
466
 
      message_generators_[i]->GenerateTypeRegistrations(printer);
467
 
    }
468
 
 
469
 
    printer->Outdent();
470
 
    printer->Print(
471
 
      "}\n"
472
 
      "\n"
473
 
      "}  // namespace\n");
474
 
  }
475
 
 
476
 
  // -----------------------------------------------------------------
477
 
 
478
 
  // ShutdownFile():  Deletes descriptors, default instances, etc. on shutdown.
479
 
  printer->Print(
480
 
    "\n"
481
 
    "void $shutdownfilename$() {\n",
482
 
    "shutdownfilename", GlobalShutdownFileName(file_->name()));
483
 
  printer->Indent();
484
 
 
485
 
  for (int i = 0; i < file_->message_type_count(); i++) {
486
 
    message_generators_[i]->GenerateShutdownCode(printer);
487
 
  }
488
 
 
489
 
  printer->Outdent();
490
 
  printer->Print(
491
 
    "}\n");
492
 
 
493
 
  // -----------------------------------------------------------------
494
 
 
495
 
  // Now generate the AddDescriptors() function.
496
 
  printer->Print(
497
 
    "\n"
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"
505
 
    "\n",
506
 
    "adddescriptorsname", GlobalAddDescriptorsName(file_->name()));
507
 
  printer->Indent();
508
 
 
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]);
520
 
    }
521
 
    // Call its AddDescriptors function.
522
 
    printer->Print(
523
 
      "$name$();\n",
524
 
      "name", GlobalAddDescriptorsName(dependency->name()));
525
 
  }
526
 
 
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);
533
 
    string file_data;
534
 
    file_proto.SerializeToString(&file_data);
535
 
 
536
 
    printer->Print(
537
 
      "::google::protobuf::DescriptorPool::InternalAddGeneratedFile(");
538
 
 
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)));
544
 
    }
545
 
    printer->Print(
546
 
      ", $size$);\n",
547
 
      "size", SimpleItoa(file_data.size()));
548
 
 
549
 
    // Call MessageFactory::InternalRegisterGeneratedFile().
550
 
    printer->Print(
551
 
      "::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(\n"
552
 
      "  \"$filename$\", &protobuf_RegisterTypes);\n",
553
 
      "filename", file_->name());
554
 
  }
555
 
 
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);
561
 
  }
562
 
  for (int i = 0; i < file_->extension_count(); i++) {
563
 
    extension_generators_[i]->GenerateRegistration(printer);
564
 
  }
565
 
  for (int i = 0; i < file_->message_type_count(); i++) {
566
 
    message_generators_[i]->GenerateDefaultInstanceInitializer(printer);
567
 
  }
568
 
 
569
 
  printer->Print(
570
 
    "::google::protobuf::internal::OnShutdown(&$shutdownfilename$);\n",
571
 
    "shutdownfilename", GlobalShutdownFileName(file_->name()));
572
 
 
573
 
  printer->Outdent();
574
 
 
575
 
  printer->Print(
576
 
    "}\n"
577
 
    "\n"
578
 
    "// Force AddDescriptors() to be called at static initialization time.\n"
579
 
    "struct StaticDescriptorInitializer_$filename$ {\n"
580
 
    "  StaticDescriptorInitializer_$filename$() {\n"
581
 
    "    $adddescriptorsname$();\n"
582
 
    "  }\n"
583
 
    "} static_descriptor_initializer_$filename$_;\n"
584
 
    "\n",
585
 
    "adddescriptorsname", GlobalAddDescriptorsName(file_->name()),
586
 
    "filename", FilenameIdentifier(file_->name()));
587
 
}
588
 
 
589
 
void FileGenerator::GenerateNamespaceOpeners(io::Printer* printer) {
590
 
  if (package_parts_.size() > 0) printer->Print("\n");
591
 
 
592
 
  for (int i = 0; i < package_parts_.size(); i++) {
593
 
    printer->Print("namespace $part$ {\n",
594
 
                   "part", package_parts_[i]);
595
 
  }
596
 
}
597
 
 
598
 
void FileGenerator::GenerateNamespaceClosers(io::Printer* printer) {
599
 
  if (package_parts_.size() > 0) printer->Print("\n");
600
 
 
601
 
  for (int i = package_parts_.size() - 1; i >= 0; i--) {
602
 
    printer->Print("}  // namespace $part$\n",
603
 
                   "part", package_parts_[i]);
604
 
  }
605
 
}
606
 
 
607
 
}  // namespace cpp
608
 
}  // namespace compiler
609
 
}  // namespace protobuf
610
 
}  // namespace google