aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/cpp/cpp_file.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_file.cc')
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_file.cc1504
1 files changed, 773 insertions, 731 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc
index 42525687..1e80715d 100644
--- a/src/google/protobuf/compiler/cpp/cpp_file.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_file.cc
@@ -44,72 +44,79 @@
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
#include <google/protobuf/compiler/cpp/cpp_message.h>
#include <google/protobuf/compiler/cpp/cpp_service.h>
+#include <google/protobuf/compiler/scc.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/strutil.h>
+
+#include <google/protobuf/port_def.inc>
+
namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {
FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options)
- : file_(file),
- options_(options),
- scc_analyzer_(options),
- enum_generators_owner_(
- new std::unique_ptr<EnumGenerator>[file->enum_type_count()]),
- service_generators_owner_(
- new std::unique_ptr<ServiceGenerator>[file->service_count()]),
- extension_generators_owner_(
- new std::unique_ptr<ExtensionGenerator>[file->extension_count()]) {
+ : file_(file), options_(options), scc_analyzer_(options) {
+ // These variables are the same on a file level
+ SetCommonVars(options, &variables_);
+ variables_["dllexport_decl"] = options.dllexport_decl;
+ variables_["tablename"] = UniqueName("TableStruct", file_, options_);
+ variables_["assign_desc_table"] =
+ UniqueName("assign_descriptors_table", file_, options_);
+ variables_["file_level_metadata"] =
+ UniqueName("file_level_metadata", file_, options_);
+ variables_["file_level_enum_descriptors"] =
+ UniqueName("file_level_enum_descriptors", file_, options_);
+ variables_["file_level_service_descriptors"] =
+ UniqueName("file_level_service_descriptors", file_, options_);
+ variables_["add_descriptors"] = UniqueName("AddDescriptors", file_, options_);
+ variables_["filename"] = file_->name();
+ variables_["package_ns"] = Namespace(file_);
+ variables_["init_defaults"] = UniqueName("InitDefaults", file_, options_);
+
std::vector<const Descriptor*> msgs = FlattenMessagesInFile(file);
for (int i = 0; i < msgs.size(); i++) {
// Deleted in destructor
MessageGenerator* msg_gen =
- new MessageGenerator(msgs[i], i, options, &scc_analyzer_);
- message_generators_.push_back(msg_gen);
+ new MessageGenerator(msgs[i], variables_, i, options, &scc_analyzer_);
+ message_generators_.emplace_back(msg_gen);
msg_gen->AddGenerators(&enum_generators_, &extension_generators_);
}
for (int i = 0; i < file->enum_type_count(); i++) {
- enum_generators_owner_[i].reset(
- new EnumGenerator(file->enum_type(i), options));
- enum_generators_.push_back(enum_generators_owner_[i].get());
+ enum_generators_.emplace_back(
+ new EnumGenerator(file->enum_type(i), variables_, options));
}
for (int i = 0; i < file->service_count(); i++) {
- service_generators_owner_[i].reset(
- new ServiceGenerator(file->service(i), options));
- service_generators_.push_back(service_generators_owner_[i].get());
+ service_generators_.emplace_back(
+ new ServiceGenerator(file->service(i), variables_, options));
}
if (HasGenericServices(file_, options_)) {
for (int i = 0; i < service_generators_.size(); i++) {
service_generators_[i]->index_in_metadata_ = i;
}
}
-
for (int i = 0; i < file->extension_count(); i++) {
- extension_generators_owner_[i].reset(
+ extension_generators_.emplace_back(
new ExtensionGenerator(file->extension(i), options));
- extension_generators_.push_back(extension_generators_owner_[i].get());
}
-
-
- package_parts_ = Split(file_->package(), ".", true);
-}
-
-FileGenerator::~FileGenerator() {
- for (int i = 0; i < message_generators_.size(); i++) {
- delete message_generators_[i];
+ for (int i = 0; i < file->weak_dependency_count(); ++i) {
+ weak_deps_.insert(file->weak_dependency(i));
}
}
+FileGenerator::~FileGenerator() = default;
+
void FileGenerator::GenerateMacroUndefs(io::Printer* printer) {
+ Formatter format(printer, variables_);
// Only do this for protobuf's own types. There are some google3 protos using
// macros as field names and the generated code compiles after the macro
// expansion. Undefing these macros actually breaks such code.
- if (file_->name() != "google/protobuf/compiler/plugin.proto") {
+ if (file_->name() != "net/proto2/compiler/proto/plugin.proto" &&
+ file_->name() != "google/protobuf/compiler/plugin.proto") {
return;
}
std::vector<string> names_to_undef;
@@ -126,70 +133,89 @@ void FileGenerator::GenerateMacroUndefs(io::Printer* printer) {
}
}
for (int i = 0; i < names_to_undef.size(); ++i) {
- printer->Print(
- "#ifdef $name$\n"
- "#undef $name$\n"
+ format(
+ "#ifdef $1$\n"
+ "#undef $1$\n"
"#endif\n",
- "name", names_to_undef[i]);
+ names_to_undef[i]);
}
}
void FileGenerator::GenerateHeader(io::Printer* printer) {
- printer->Print(
- "// @@protoc_insertion_point(includes)\n");
+ Formatter format(printer, variables_);
- printer->Print("#define PROTOBUF_INTERNAL_EXPORT_$filename$ $export$\n",
- "filename", FileLevelNamespace(file_),
- "export", options_.dllexport_decl);
+ // port_def.inc must be included after all other includes.
+ IncludeFile("net/proto2/public/port_def.inc", printer);
+ format("#define $1$$ dllexport_decl$\n",
+ UniqueName("PROTOBUF_INTERNAL_EXPORT", file_, options_));
GenerateMacroUndefs(printer);
+ if (IsProto2MessageSetFile(file_, options_)) {
+ // Proto2 MessageSet overrides GetMapper() so we forward declare TagMapper
+ // to avoid inclusion of "tagmapper.h".
+ format("class TagMapper;\n");
+ }
+
+ if (!options_.opensource_runtime) {
+ // EmbeddedMessageHolder is a proxy class to provide access into arena
+ // constructors for proto1 message objects.
+ // See net/proto/proto_arena_internal.h
+ format(
+ "namespace proto {\n"
+ "namespace internal {\n"
+ "template <typename T> struct EmbeddedMessageHolder;\n"
+ "} // namespace internal\n"
+ "} // namespace proto\n");
+ }
GenerateGlobalStateFunctionDeclarations(printer);
GenerateForwardDeclarations(printer);
{
- NamespaceOpener ns(Namespace(file_), printer);
+ NamespaceOpener ns(Namespace(file_), format);
- printer->Print("\n");
+ format("\n");
GenerateEnumDefinitions(printer);
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ format(kThickSeparator);
+ format("\n");
GenerateMessageDefinitions(printer);
- printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThickSeparator);
+ format("\n");
GenerateServiceDefinitions(printer);
GenerateExtensionIdentifiers(printer);
- printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThickSeparator);
+ format("\n");
GenerateInlineFunctionDefinitions(printer);
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(namespace_scope)\n"
- "\n");
+ format(
+ "\n"
+ "// @@protoc_insertion_point(namespace_scope)\n"
+ "\n");
}
// We need to specialize some templates in the ::google::protobuf namespace:
GenerateProto2NamespaceEnumSpecializations(printer);
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(global_scope)\n"
- "\n");
+ format(
+ "\n"
+ "// @@protoc_insertion_point(global_scope)\n"
+ "\n");
+ IncludeFile("net/proto2/public/port_undef.inc", printer);
}
void FileGenerator::GenerateProtoHeader(io::Printer* printer,
const string& info_path) {
+ Formatter format(printer, variables_);
if (!options_.proto_h) {
return;
}
@@ -197,99 +223,187 @@ void FileGenerator::GenerateProtoHeader(io::Printer* printer,
string filename_identifier = FilenameIdentifier(file_->name());
GenerateTopHeaderGuard(printer, filename_identifier);
+ if (!options_.opensource_runtime) {
+ format(
+ "#ifdef SWIG\n"
+ "#error \"Do not SWIG-wrap protobufs.\"\n"
+ "#endif // SWIG\n"
+ "\n");
+ }
+
+ if (IsBootstrapProto(options_, file_)) {
+ format("// IWYU pragma: private, include \"$1$.proto.h\"\n\n",
+ StripProto(file_->name()));
+ }
GenerateLibraryIncludes(printer);
for (int i = 0; i < file_->public_dependency_count(); i++) {
const FileDescriptor* dep = file_->public_dependency(i);
const char* extension = ".proto.h";
+ // The proto1 compiler only generates .pb.h files, so even if we are
+ // running in proto-h mode, we can only use the .pb.h.
+ if (IsProto1(dep, options_)) {
+ extension = ".pb.h";
+ }
string dependency = StripProto(dep->name()) + extension;
- printer->Print(
- "#include \"$dependency$\" // IWYU pragma: export\n",
- "dependency", dependency);
+ format("#include \"$1$\"\n", dependency);
}
+ format("// @@protoc_insertion_point(includes)\n");
+
GenerateMetadataPragma(printer, info_path);
GenerateHeader(printer);
+ IncludeFile("net/proto2/public/port_undef.inc", printer);
+
GenerateBottomHeaderGuard(printer, filename_identifier);
}
void FileGenerator::GeneratePBHeader(io::Printer* printer,
const string& info_path) {
+ Formatter format(printer, variables_);
string filename_identifier =
FilenameIdentifier(file_->name() + (options_.proto_h ? ".pb.h" : ""));
GenerateTopHeaderGuard(printer, filename_identifier);
if (options_.proto_h) {
string target_basename = StripProto(file_->name());
- printer->Print("#include \"$basename$.proto.h\" // IWYU pragma: export\n",
- "basename", target_basename);
+ if (!options_.opensource_runtime) {
+ GetBootstrapBasename(options_, target_basename, &target_basename);
+ }
+ format("#include \"$1$.proto.h\" // IWYU pragma: export\n",
+ target_basename);
} else {
GenerateLibraryIncludes(printer);
}
- GenerateDependencyIncludes(printer);
+ if (options_.transitive_pb_h) {
+ GenerateDependencyIncludes(printer);
+ }
+
+ // This is unfortunately necessary for some plugins. I don't see why we
+ // need two of the same insertion points.
+ // TODO(gerbens) remove this.
+ format("// @@protoc_insertion_point(includes)\n");
+
GenerateMetadataPragma(printer, info_path);
if (!options_.proto_h) {
GenerateHeader(printer);
} else {
- // This is unfortunately necessary for some plugins. I don't see why we
- // need two of the same insertion points.
- // TODO(gerbens) remove this.
- printer->Print(
- "// @@protoc_insertion_point(includes)\n");
{
- NamespaceOpener ns(Namespace(file_), printer);
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(namespace_scope)\n");
+ NamespaceOpener ns(Namespace(file_), format);
+ format(
+ "\n"
+ "// @@protoc_insertion_point(namespace_scope)\n");
}
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(global_scope)\n"
- "\n");
+ format(
+ "\n"
+ "// @@protoc_insertion_point(global_scope)\n"
+ "\n");
}
GenerateBottomHeaderGuard(printer, filename_identifier);
}
+void FileGenerator::DoIncludeFile(const string& google3_name, bool do_export,
+ io::Printer* printer) {
+ Formatter format(printer, variables_);
+ const string prefix = "net/proto2/";
+ GOOGLE_CHECK(google3_name.find(prefix) == 0) << google3_name;
+
+ if (options_.opensource_runtime) {
+ string path = google3_name.substr(prefix.size());
+
+ path = StringReplace(path, "internal/", "", false);
+ path = StringReplace(path, "proto/", "", false);
+ path = StringReplace(path, "public/", "", false);
+ if (options_.opensource_include_paths) {
+ format("#include <google/protobuf/$1$>", path);
+ } else {
+ format(
+ "#include "
+ "\"third_party/protobuf/testing/extracted/src/google/protobuf/$1$\"",
+ path);
+ }
+ } else {
+ format("#include \"$1$\"", google3_name);
+ }
+
+ if (do_export) {
+ format(" // IWYU pragma: export");
+ }
+
+ format("\n");
+}
+
+string FileGenerator::CreateHeaderInclude(const string& basename,
+ const FileDescriptor* file) {
+ bool use_system_include = false;
+ string name = basename;
+
+ if (options_.opensource_runtime) {
+ if (IsWellKnownMessage(file)) {
+ if (options_.opensource_include_paths) {
+ use_system_include = true;
+ } else {
+ name = "third_party/protobuf/testing/extracted/src/" + basename;
+ }
+ }
+ }
+
+ string left = "\"";
+ string right = "\"";
+ if (use_system_include) {
+ left = "<";
+ right = ">";
+ }
+ return left + name + right;
+}
+
void FileGenerator::GenerateSourceIncludes(io::Printer* printer) {
+ Formatter format(printer, variables_);
string target_basename = StripProto(file_->name());
- const bool use_system_include = IsWellKnownMessage(file_);
-
- string header = target_basename + (options_.proto_h ? ".proto.h" : ".pb.h");
- printer->Print(
- "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
- "// source: $filename$\n"
- "\n"
- "#include $left$$header$$right$\n"
- "\n"
- "#include <algorithm>\n" // for swap()
- "\n"
- "#include <google/protobuf/stubs/common.h>\n"
- "#include <google/protobuf/stubs/port.h>\n"
- "#include <google/protobuf/io/coded_stream.h>\n"
- "#include <google/protobuf/wire_format_lite_inl.h>\n",
- "filename", file_->name(),
- "header", header,
- "left", use_system_include ? "<" : "\"",
- "right", use_system_include ? ">" : "\"");
+ if (!options_.opensource_runtime) {
+ GetBootstrapBasename(options_, target_basename, &target_basename);
+ }
+ target_basename += options_.proto_h ? ".proto.h" : ".pb.h";
+ format(
+ "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
+ "// source: $filename$\n"
+ "\n"
+ "#include $1$\n"
+ "\n"
+ "#include <algorithm>\n" // for swap()
+ "\n",
+ CreateHeaderInclude(target_basename, file_));
+
+ if (options_.opensource_runtime) {
+ DoIncludeFile("net/proto2/public/stubs/common.h", false, printer);
+ }
+
+ IncludeFile("net/proto2/io/public/coded_stream.h", printer);
+ // TODO(gerbens) This is to include parse_context.h, we need a better way
+ IncludeFile("net/proto2/public/extension_set.h", printer);
+ IncludeFile("net/proto2/public/wire_format_lite_inl.h", printer);
// Unknown fields implementation in lite mode uses StringOutputStream
if (!UseUnknownFieldSet(file_, options_) && !message_generators_.empty()) {
- printer->Print(
- "#include <google/protobuf/io/zero_copy_stream_impl_lite.h>\n");
+ IncludeFile("net/proto2/io/public/zero_copy_stream_impl_lite.h", printer);
}
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/descriptor.h>\n"
- "#include <google/protobuf/generated_message_reflection.h>\n"
- "#include <google/protobuf/reflection_ops.h>\n"
- "#include <google/protobuf/wire_format.h>\n");
+ IncludeFile("net/proto2/public/descriptor.h", printer);
+ IncludeFile("net/proto2/public/generated_message_reflection.h", printer);
+ IncludeFile("net/proto2/public/reflection_ops.h", printer);
+ IncludeFile("net/proto2/public/wire_format.h", printer);
+ }
+ if (IsProto2MessageSetFile(file_, options_)) {
+ format(
+ // Implementation of proto1 MessageSet API methods.
+ "#include \"net/proto2/bridge/internal/message_set_util.h\"\n");
}
if (options_.proto_h) {
@@ -298,104 +412,100 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* printer) {
const FileDescriptor* dep = file_->dependency(i);
const char* extension = ".proto.h";
string basename = StripProto(dep->name());
+ // Do not import weak deps.
+ if (!options_.opensource_runtime && IsDepWeak(dep)) continue;
+ // The proto1 compiler only generates .pb.h files, so even if we are
+ // running in proto-h mode, we can only use the .pb.h.
+ if (IsProto1(dep, options_)) {
+ extension = ".pb.h";
+ }
+ if (IsBootstrapProto(options_, file_)) {
+ GetBootstrapBasename(options_, basename, &basename);
+ }
string dependency = basename + extension;
- printer->Print(
- "#include \"$dependency$\"\n",
- "dependency", dependency);
+ format("#include \"$1$\"\n", dependency);
}
}
- // TODO(gerbens) Remove this when all code in google is using the same
- // proto library. This is a temporary hack to force build errors if
- // the proto library is compiled with GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS
- // and is also linking internal proto2. This is to prevent regressions while
- // we work cleaning up the code base. After this is completed and we have
- // one proto lib all code uses this should be removed.
- printer->Print(
- "// This is a temporary google only hack\n"
- "#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS\n"
- "#include \"third_party/protobuf/version.h\"\n"
- "#endif\n");
-
- printer->Print(
- "// @@protoc_insertion_point(includes)\n");
+ format("// @@protoc_insertion_point(includes)\n");
+ IncludeFile("net/proto2/public/port_def.inc", printer);
}
void FileGenerator::GenerateSourceDefaultInstance(int idx,
io::Printer* printer) {
- printer->Print(
- "class $classname$DefaultTypeInternal {\n"
+ Formatter format(printer, variables_);
+ format(
+ "class $1$DefaultTypeInternal {\n"
" public:\n"
- " ::google::protobuf::internal::ExplicitlyConstructed<$classname$>\n"
- " _instance;\n",
- "classname", message_generators_[idx]->classname_);
- printer->Indent();
+ " ::$proto_ns$::internal::ExplicitlyConstructed<$1$> _instance;\n",
+ message_generators_[idx]->classname_);
+ format.Indent();
message_generators_[idx]->GenerateExtraDefaultFields(printer);
- printer->Outdent();
- printer->Print("} _$classname$_default_instance_;\n", "classname",
- message_generators_[idx]->classname_);
+ format.Outdent();
+ format("} _$1$_default_instance_;\n", message_generators_[idx]->classname_);
}
-namespace {
-
// Generates weak symbol declarations for types that are to be considered weakly
// referenced.
-void GenerateInternalForwardDeclarations(
+void FileGenerator::GenerateInternalForwardDeclarations(
const std::vector<const FieldDescriptor*>& fields, const Options& options,
- SCCAnalyzer* scc_analyzer, io::Printer* printer) {
+ MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) {
+ Formatter format(printer);
// To ensure determinism and minimize the number of namespace statements,
// we output the forward declarations sorted on namespace and type / function
// name.
+ std::set<string> global_namespace_decls;
+ // weak defaults
std::set<std::pair<string, string> > messages;
- std::set<std::pair<string, string> > sccs;
- std::set<std::pair<string, string> > inits;
for (int i = 0; i < fields.size(); ++i) {
const FieldDescriptor* field = fields[i];
const Descriptor* msg = field->message_type();
if (msg == nullptr) continue;
bool is_weak = IsImplicitWeakField(field, options, scc_analyzer);
- string flns = FileLevelNamespace(msg);
- auto scc = scc_analyzer->GetSCC(msg);
- string repr = ClassName(scc->GetRepresentative());
+ if (field->options().weak()) {
+ GOOGLE_CHECK(!options_.opensource_runtime);
+ is_weak = true;
+ }
string weak_attr;
if (is_weak) {
- inits.insert(std::make_pair(flns, "AddDescriptors"));
+ global_namespace_decls.insert(
+ "void " + UniqueName("AddDescriptors", msg, options_) +
+ "() __attribute__((weak))");
messages.insert(std::make_pair(Namespace(msg), ClassName(msg)));
weak_attr = " __attribute__((weak))";
}
- string dllexport = "PROTOBUF_INTERNAL_EXPORT_" + FileLevelNamespace(msg);
- sccs.insert(std::make_pair(flns, "extern " + dllexport + weak_attr +
- " ::google::protobuf::internal::SCCInfo<" +
- SimpleItoa(scc->children.size()) +
- "> scc_info_" + repr + ";\n"));
- }
-
- printer->Print("\n");
- NamespaceOpener ns(printer);
- for (std::set<std::pair<string, string> >::const_iterator it =
- messages.begin();
- it != messages.end(); ++it) {
- ns.ChangeTo(it->first);
- printer->Print(
- "extern __attribute__((weak)) $classname$DefaultTypeInternal "
- "_$classname$_default_instance_;\n",
- "classname", it->second);
- }
- for (std::set<std::pair<string, string> >::const_iterator it = inits.begin();
- it != inits.end(); ++it) {
- ns.ChangeTo(it->first);
- printer->Print("void $name$() __attribute__((weak));\n",
- "name", it->second);
- }
- for (const auto& p : sccs) {
- ns.ChangeTo(p.first);
- printer->Print(p.second.c_str());
+ string dllexport = UniqueName("PROTOBUF_INTERNAL_EXPORT", msg, options_);
+ if (IsProto1(msg->file(), options_) || IsWeak(field, options_)) {
+ dllexport = "";
+ }
+ auto scc = scc_analyzer->GetSCC(msg);
+ string repr =
+ UniqueName(ClassName(scc->GetRepresentative()), msg, options_);
+ global_namespace_decls.insert(
+ "extern " + dllexport + weak_attr + " ::" + ProtobufNamespace(options) +
+ "::internal::SCCInfo<" + SimpleItoa(scc->children.size()) +
+ "> scc_info_" + repr);
+ }
+
+ format("\n");
+
+ for (const string& decl : global_namespace_decls) {
+ format("$1$;\n", decl);
}
-}
-} // namespace
+ // Weak external fields
+ NamespaceOpener ns(format);
+ for (const auto& pair : messages) {
+ ns.ChangeTo(pair.first);
+ format(
+ "extern __attribute__((weak)) $1$DefaultTypeInternal "
+ "_$1$_default_instance_;\n",
+ pair.second);
+ }
+}
void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) {
+ Formatter format(printer, variables_);
GenerateSourceIncludes(printer);
// Generate weak declarations. We do this for the whole strongly-connected
@@ -411,45 +521,43 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) {
printer);
if (IsSCCRepresentative(message_generators_[idx]->descriptor_)) {
- NamespaceOpener ns(FileLevelNamespace(file_), printer);
GenerateInitForSCC(GetSCC(message_generators_[idx]->descriptor_), printer);
}
{ // package namespace
- NamespaceOpener ns(Namespace(file_), printer);
+ NamespaceOpener ns(Namespace(file_), format);
// Define default instances
GenerateSourceDefaultInstance(idx, printer);
if (options_.lite_implicit_weak_fields) {
- printer->Print("void $classname$_ReferenceStrong() {}\n", "classname",
- message_generators_[idx]->classname_);
+ format("void $1$_ReferenceStrong() {}\n",
+ message_generators_[idx]->classname_);
}
// Generate classes.
- printer->Print("\n");
+ format("\n");
message_generators_[idx]->GenerateClassMethods(printer);
- printer->Print(
+ format(
"\n"
"// @@protoc_insertion_point(namespace_scope)\n");
} // end package namespace
- printer->Print(
- "namespace google {\nnamespace protobuf {\n");
- message_generators_[idx]->GenerateSourceInProto2Namespace(printer);
- printer->Print(
- "} // namespace protobuf\n} // namespace google\n");
+ {
+ NamespaceOpener proto_ns(ProtobufNamespace(options_), format);
+ message_generators_[idx]->GenerateSourceInProto2Namespace(printer);
+ }
- printer->Print(
+ format(
"\n"
"// @@protoc_insertion_point(global_scope)\n");
}
void FileGenerator::GenerateGlobalSource(io::Printer* printer) {
+ Formatter format(printer, variables_);
GenerateSourceIncludes(printer);
{
- NamespaceOpener ns(FileLevelNamespace(file_), printer);
GenerateTables(printer);
// Define the code to initialize reflection. This code uses a global
@@ -459,7 +567,7 @@ void FileGenerator::GenerateGlobalSource(io::Printer* printer) {
}
}
- NamespaceOpener ns(Namespace(file_), printer);
+ NamespaceOpener ns(Namespace(file_), format);
// Generate enums.
for (int i = 0; i < enum_generators_.size(); i++) {
@@ -474,15 +582,16 @@ void FileGenerator::GenerateGlobalSource(io::Printer* printer) {
if (HasGenericServices(file_, options_)) {
// Generate services.
for (int i = 0; i < service_generators_.size(); i++) {
- if (i == 0) printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ if (i == 0) format("\n");
+ format(kThickSeparator);
+ format("\n");
service_generators_[i]->GenerateImplementation(printer);
}
}
}
void FileGenerator::GenerateSource(io::Printer* printer) {
+ Formatter format(printer, variables_);
GenerateSourceIncludes(printer);
std::vector<const FieldDescriptor*> fields;
ListAllFields(file_, &fields);
@@ -490,20 +599,19 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
printer);
{
- NamespaceOpener ns(Namespace(file_), printer);
+ NamespaceOpener ns(Namespace(file_), format);
// Define default instances
for (int i = 0; i < message_generators_.size(); i++) {
GenerateSourceDefaultInstance(i, printer);
if (options_.lite_implicit_weak_fields) {
- printer->Print("void $classname$_ReferenceStrong() {}\n", "classname",
- message_generators_[i]->classname_);
+ format("void $1$_ReferenceStrong() {}\n",
+ message_generators_[i]->classname_);
}
}
}
{
- NamespaceOpener ns(FileLevelNamespace(file_), printer);
GenerateTables(printer);
// Now generate the InitDefaults for each SCC.
@@ -514,26 +622,28 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
}
}
- printer->Print("void InitDefaults() {\n");
- for (int i = 0; i < message_generators_.size(); i++) {
- if (!IsSCCRepresentative(message_generators_[i]->descriptor_)) continue;
- string scc_name = ClassName(message_generators_[i]->descriptor_);
- printer->Print(
- " ::google::protobuf::internal::InitSCC(&scc_info_$scc_name$.base);\n",
- "scc_name", scc_name);
- }
- printer->Print("}\n\n");
-
- // Define the code to initialize reflection. This code uses a global
- // constructor to register reflection data with the runtime pre-main.
if (HasDescriptorMethods(file_, options_)) {
+ // TODO(gerbens) This is for proto1 interoperability. Remove when proto1
+ // is gone.
+ format("void $init_defaults$() {\n");
+ for (int i = 0; i < message_generators_.size(); i++) {
+ if (!IsSCCRepresentative(message_generators_[i]->descriptor_)) continue;
+ string scc_name =
+ UniqueName(ClassName(message_generators_[i]->descriptor_),
+ message_generators_[i]->descriptor_, options_);
+ format(" ::$proto_ns$::internal::InitSCC(&scc_info_$1$.base);\n",
+ scc_name);
+ }
+ format("}\n\n");
+
+ // Define the code to initialize reflection. This code uses a global
+ // constructor to register reflection data with the runtime pre-main.
GenerateReflectionInitializationCode(printer);
}
}
-
{
- NamespaceOpener ns(Namespace(file_), printer);
+ NamespaceOpener ns(Namespace(file_), format);
// Actually implement the protos
@@ -544,18 +654,18 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
// Generate classes.
for (int i = 0; i < message_generators_.size(); i++) {
- printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThickSeparator);
+ format("\n");
message_generators_[i]->GenerateClassMethods(printer);
}
if (HasGenericServices(file_, options_)) {
// Generate services.
for (int i = 0; i < service_generators_.size(); i++) {
- if (i == 0) printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ if (i == 0) format("\n");
+ format(kThickSeparator);
+ format("\n");
service_generators_[i]->GenerateImplementation(printer);
}
}
@@ -565,144 +675,28 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
extension_generators_[i]->GenerateDefinition(printer);
}
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(namespace_scope)\n");
- }
-
- printer->Print(
- "namespace google {\nnamespace protobuf {\n");
- for (int i = 0; i < message_generators_.size(); i++) {
- message_generators_[i]->GenerateSourceInProto2Namespace(printer);
- }
- printer->Print(
- "} // namespace protobuf\n} // namespace google\n");
-
- printer->Print(
- "\n"
- "// @@protoc_insertion_point(global_scope)\n");
-}
-
-class FileGenerator::ForwardDeclarations {
- public:
- ~ForwardDeclarations() {
- for (std::map<string, ForwardDeclarations*>::iterator
- it = namespaces_.begin(),
- end = namespaces_.end();
- it != end; ++it) {
- delete it->second;
- }
- namespaces_.clear();
- }
-
- ForwardDeclarations* AddOrGetNamespace(const string& ns_name) {
- ForwardDeclarations*& ns = namespaces_[ns_name];
- if (ns == nullptr) {
- ns = new ForwardDeclarations;
- }
- return ns;
- }
-
- std::map<string, const Descriptor*>& classes() { return classes_; }
- std::map<string, const EnumDescriptor*>& enums() { return enums_; }
-
- void PrintForwardDeclarations(io::Printer* printer,
- const Options& options) const {
- PrintNestedDeclarations(printer, options);
- PrintTopLevelDeclarations(printer, options);
- }
-
-
- private:
- void PrintNestedDeclarations(io::Printer* printer,
- const Options& options) const {
- PrintDeclarationsInsideNamespace(printer, options);
- for (std::map<string, ForwardDeclarations *>::const_iterator
- it = namespaces_.begin(),
- end = namespaces_.end();
- it != end; ++it) {
- printer->Print("namespace $nsname$ {\n",
- "nsname", it->first);
- it->second->PrintNestedDeclarations(printer, options);
- printer->Print("} // namespace $nsname$\n",
- "nsname", it->first);
- }
- }
-
- void PrintTopLevelDeclarations(io::Printer* printer,
- const Options& options) const {
- PrintDeclarationsOutsideNamespace(printer, options);
- for (std::map<string, ForwardDeclarations *>::const_iterator
- it = namespaces_.begin(),
- end = namespaces_.end();
- it != end; ++it) {
- it->second->PrintTopLevelDeclarations(printer, options);
- }
+ format(
+ "\n"
+ "// @@protoc_insertion_point(namespace_scope)\n");
}
- void PrintDeclarationsInsideNamespace(io::Printer* printer,
- const Options& options) const {
- for (std::map<string, const EnumDescriptor *>::const_iterator
- it = enums_.begin(),
- end = enums_.end();
- it != end; ++it) {
- printer->Print("enum $enumname$ : int;\n", "enumname", it->first);
- printer->Annotate("enumname", it->second);
- printer->Print("bool $enumname$_IsValid(int value);\n", "enumname",
- it->first);
- }
- for (std::map<string, const Descriptor*>::const_iterator
- it = classes_.begin(),
- end = classes_.end();
- it != end; ++it) {
- printer->Print("class $classname$;\n", "classname", it->first);
- printer->Annotate("classname", it->second);
-
- printer->Print(
- "class $classname$DefaultTypeInternal;\n"
- "$dllexport_decl$"
- "extern $classname$DefaultTypeInternal "
- "_$classname$_default_instance_;\n", // NOLINT
- "dllexport_decl",
- options.dllexport_decl.empty() ? "" : options.dllexport_decl + " ",
- "classname",
- it->first);
- if (options.lite_implicit_weak_fields) {
- printer->Print("void $classname$_ReferenceStrong();\n",
- "classname", it->first);
- }
+ {
+ NamespaceOpener proto_ns(ProtobufNamespace(options_), format);
+ for (int i = 0; i < message_generators_.size(); i++) {
+ message_generators_[i]->GenerateSourceInProto2Namespace(printer);
}
}
- void PrintDeclarationsOutsideNamespace(io::Printer* printer,
- const Options& options) const {
- if (classes_.size() == 0) return;
-
- printer->Print(
- "namespace google {\nnamespace protobuf {\n");
- for (std::map<string, const Descriptor*>::const_iterator
- it = classes_.begin(),
- end = classes_.end();
- it != end; ++it) {
- const Descriptor* d = it->second;
- printer->Print(
- "template<> "
- "$dllexport_decl$"
- "$classname$* Arena::CreateMaybeMessage<$classname$>"
- "(Arena*);\n",
- "classname", QualifiedClassName(d), "dllexport_decl",
- options.dllexport_decl.empty() ? "" : options.dllexport_decl + " ");
- }
- printer->Print(
- "} // namespace protobuf\n} // namespace google\n");
- }
+ format(
+ "\n"
+ "// @@protoc_insertion_point(global_scope)\n");
- std::map<string, ForwardDeclarations*> namespaces_;
- std::map<string, const Descriptor*> classes_;
- std::map<string, const EnumDescriptor*> enums_;
-};
+ IncludeFile("net/proto2/public/port_undef.inc", printer);
+}
void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) {
+ Formatter format(printer, variables_);
+
// 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 ordinarily runs at
@@ -716,39 +710,50 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) {
// in the file.
if (!message_generators_.empty()) {
- printer->Print("::google::protobuf::Metadata file_level_metadata[$size$];\n", "size",
- SimpleItoa(message_generators_.size()));
+ format("::$proto_ns$::Metadata $file_level_metadata$[$1$];\n",
+ message_generators_.size());
+ } else {
+ format(
+ "constexpr ::$proto_ns$::Metadata* $file_level_metadata$ = nullptr;\n");
}
if (!enum_generators_.empty()) {
- printer->Print(
- "const ::google::protobuf::EnumDescriptor* "
- "file_level_enum_descriptors[$size$];\n",
- "size", SimpleItoa(enum_generators_.size()));
+ format(
+ "const ::$proto_ns$::EnumDescriptor* "
+ "$file_level_enum_descriptors$[$1$];\n",
+ enum_generators_.size());
+ } else {
+ format(
+ "constexpr ::$proto_ns$::EnumDescriptor const** "
+ "$file_level_enum_descriptors$ = nullptr;\n");
}
if (HasGenericServices(file_, options_) && file_->service_count() > 0) {
- printer->Print(
- "const ::google::protobuf::ServiceDescriptor* "
- "file_level_service_descriptors[$size$];\n",
- "size", SimpleItoa(file_->service_count()));
+ format(
+ "const ::$proto_ns$::ServiceDescriptor* "
+ "$file_level_service_descriptors$[$1$];\n",
+ file_->service_count());
+ } else {
+ format(
+ "constexpr ::$proto_ns$::ServiceDescriptor const** "
+ "$file_level_service_descriptors$ = nullptr;\n");
}
if (!message_generators_.empty()) {
- printer->Print(
+ format(
"\n"
- "const ::google::protobuf::uint32 TableStruct::offsets[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
- printer->Indent();
+ "const $uint32$ $tablename$::offsets[] "
+ "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n");
+ format.Indent();
std::vector<std::pair<size_t, size_t> > pairs;
pairs.reserve(message_generators_.size());
for (int i = 0; i < message_generators_.size(); i++) {
pairs.push_back(message_generators_[i]->GenerateOffsets(printer));
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
- "static const ::google::protobuf::internal::MigrationSchema schemas[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
- printer->Indent();
+ "static const ::$proto_ns$::internal::MigrationSchema schemas[] "
+ "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n");
+ format.Indent();
{
int offset = 0;
for (int i = 0; i < message_generators_.size(); i++) {
@@ -757,30 +762,33 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) {
offset += pairs[i].first;
}
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
"\nstatic "
- "::google::protobuf::Message const * const file_default_instances[] = {\n");
- printer->Indent();
+ "::$proto_ns$::Message const * const file_default_instances[] = {\n");
+ format.Indent();
for (int i = 0; i < message_generators_.size(); i++) {
const Descriptor* descriptor = message_generators_[i]->descriptor_;
- printer->Print(
+ format(
"reinterpret_cast<const "
- "::google::protobuf::Message*>(&$ns$::_$classname$_default_instance_),\n",
- "classname", ClassName(descriptor), "ns", Namespace(descriptor));
+ "::$proto_ns$::Message*>(&$1$::_$2$_default_instance_),\n",
+ Namespace(descriptor), // 1
+ ClassName(descriptor)); // 2
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
"\n");
} else {
// we still need these symbols to exist
- printer->Print(
+ format(
// MSVC doesn't like empty arrays, so we add a dummy.
- "const ::google::protobuf::uint32 TableStruct::offsets[1] = {};\n"
- "static const ::google::protobuf::internal::MigrationSchema* schemas = NULL;\n"
- "static const ::google::protobuf::Message* const* "
+ "const $uint32$ $tablename$::offsets[1] = {};\n"
+ "static constexpr ::$proto_ns$::internal::MigrationSchema* schemas = "
+ "NULL;"
+ "\n"
+ "static constexpr ::$proto_ns$::Message* const* "
"file_default_instances = NULL;\n"
"\n");
}
@@ -790,161 +798,90 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) {
// protobuf_AssignDescriptorsOnce(): The first time it is called, calls
// AssignDescriptors(). All later times, waits for the first call to
// complete and then returns.
- printer->Print(
- "void protobuf_AssignDescriptors() {\n"
- // 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.
- " AddDescriptors();\n"
- " AssignDescriptors(\n"
- " \"$filename$\", schemas, file_default_instances, "
- "TableStruct::offsets,\n"
- " $metadata$, $enum_descriptors$, $service_descriptors$);\n",
- "filename", file_->name(), "metadata",
- !message_generators_.empty() ? "file_level_metadata" : "NULL",
- "enum_descriptors",
- !enum_generators_.empty() ? "file_level_enum_descriptors" : "NULL",
- "service_descriptors",
- HasGenericServices(file_, options_) && file_->service_count() > 0
- ? "file_level_service_descriptors"
- : "NULL");
- printer->Print(
- "}\n"
- "\n"
- "void protobuf_AssignDescriptorsOnce() {\n"
- " static ::google::protobuf::internal::once_flag once;\n"
- " ::google::protobuf::internal::call_once(once, protobuf_AssignDescriptors);\n"
- "}\n"
- "\n",
- "filename", file_->name(), "metadata",
- !message_generators_.empty() ? "file_level_metadata" : "NULL",
- "enum_descriptors",
- !enum_generators_.empty() ? "file_level_enum_descriptors" : "NULL",
- "service_descriptors",
- HasGenericServices(file_, options_) && file_->service_count() > 0
- ? "file_level_service_descriptors"
- : "NULL");
-
- // Only here because of useless string reference that we don't want in
- // protobuf_AssignDescriptorsOnce, because that is called from all the
- // GetMetadata member methods.
- printer->Print(
- "void protobuf_RegisterTypes(const ::std::string&) "
- "GOOGLE_PROTOBUF_ATTRIBUTE_COLD;\n"
- "void protobuf_RegisterTypes(const ::std::string&) {\n"
- " protobuf_AssignDescriptorsOnce();\n");
- printer->Indent();
-
- // All normal messages can be done generically
- if (!message_generators_.empty()) {
- printer->Print(
- "::google::protobuf::internal::RegisterAllTypes(file_level_metadata, $size$);\n",
- "size", SimpleItoa(message_generators_.size()));
- }
+ format(
+ "::$proto_ns$::internal::AssignDescriptorsTable $assign_desc_table$ = "
+ "{\n"
+ " {}, $add_descriptors$, \"$filename$\", schemas,\n"
+ " file_default_instances, $tablename$::offsets,\n"
+ " $file_level_metadata$, $1$, $file_level_enum_descriptors$, "
+ "$file_level_service_descriptors$,\n"
+ "};\n"
+ "\n",
+ message_generators_.size());
+
+ // Now generate the AddDescriptors() function.
+ format(
+ "::$proto_ns$::internal::DescriptorTable $1$ = {\n"
+ " false, $init_defaults$, \n",
+ UniqueName("descriptor_table", file_, options_));
+ format.Indent();
+
+ // Embed the descriptor. We simply serialize the entire
+ // FileDescriptorProto
+ // and embed it as a string literal, which is parsed and built into real
+ // descriptors at initialization time.
+ FileDescriptorProto file_proto;
+ file_->CopyTo(&file_proto);
+ string file_data;
+ file_proto.SerializeToString(&file_data);
- printer->Outdent();
- printer->Print(
- "}\n"
- "\n");
-
- // Now generate the AddDescriptors() function.
- printer->Print(
- "void AddDescriptorsImpl() {\n"
- " InitDefaults();\n");
- printer->Indent();
-
- // Embed the descriptor. We simply serialize the entire
- // FileDescriptorProto
- // and embed it as a string literal, which is parsed and built into real
- // descriptors at initialization time.
- FileDescriptorProto file_proto;
- file_->CopyTo(&file_proto);
- string file_data;
- file_proto.SerializeToString(&file_data);
-
- printer->Print("static const char descriptor[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) "
- "= {\n");
- printer->Indent();
-
- if (file_data.size() > 65535) {
- // Workaround for MSVC: "Error C1091: compiler limit: string exceeds 65535
- // bytes in length". Declare a static array of characters rather than use
- // a string literal. Only write 25 bytes per line.
- static const int kBytesPerLine = 25;
- for (int i = 0; i < file_data.size();) {
- for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) {
- printer->Print("'$char$', ", "char",
- CEscape(file_data.substr(i, 1)));
- }
- printer->Print("\n");
- }
- } else {
- // Only write 40 bytes per line.
- static const int kBytesPerLine = 40;
- for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
- printer->Print(" \"$data$\"\n", "data",
- EscapeTrigraphs(CEscape(
- file_data.substr(i, kBytesPerLine))));
- }
+ {
+ // Only write 40 bytes per line.
+ static const int kBytesPerLine = 40;
+ for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
+ format(
+ "\"$1$\"\n",
+ EscapeTrigraphs(CEscape(file_data.substr(i, kBytesPerLine))));
}
-
- printer->Outdent();
- printer->Print("};\n");
- printer->Print(
- "::google::protobuf::DescriptorPool::InternalAddGeneratedFile(\n"
- " descriptor, $size$);\n",
- "size", SimpleItoa(file_data.size()));
-
- // Call MessageFactory::InternalRegisterGeneratedFile().
- printer->Print(
- "::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(\n"
- " \"$filename$\", &protobuf_RegisterTypes);\n",
- "filename", file_->name());
-
- // Call the AddDescriptors() methods for all of our dependencies, to make
- // sure they get added first.
- for (int i = 0; i < file_->dependency_count(); i++) {
- const FileDescriptor* dependency = file_->dependency(i);
- // Print the namespace prefix for the dependency.
- string file_namespace = FileLevelNamespace(dependency);
- // Call its AddDescriptors function.
- printer->Print("::$file_namespace$::AddDescriptors();\n", "file_namespace",
- file_namespace);
}
- printer->Outdent();
- printer->Print(
- "}\n"
- "\n"
- "void AddDescriptors() {\n"
- " static ::google::protobuf::internal::once_flag once;\n"
- " ::google::protobuf::internal::call_once(once, AddDescriptorsImpl);\n"
- "}\n");
-
- printer->Print(
- "// Force AddDescriptors() to be called at dynamic initialization "
- "time.\n"
- "struct StaticDescriptorInitializer {\n"
- " StaticDescriptorInitializer() {\n"
- " AddDescriptors();\n"
- " }\n"
- "} static_descriptor_initializer;\n");
+ format.Outdent();
+ const int num_deps = file_->dependency_count();
+ format(
+ ",\n \"$filename$\", &$assign_desc_table$, $1$,\n"
+ "};\n\n"
+ "void $add_descriptors$() {\n"
+ " static constexpr ::$proto_ns$::internal::InitFunc deps[$2$] =\n"
+ " {\n",
+ file_data.size(), std::max(num_deps, 1));
+ for (int i = 0; i < num_deps; i++) {
+ const FileDescriptor* dependency = file_->dependency(i);
+ format(" ::$1$,\n", UniqueName("AddDescriptors", dependency, options_));
+ }
+ format(
+ " };\n"
+ " ::$proto_ns$::internal::AddDescriptors(&$1$, deps, $2$);\n"
+ "}\n\n",
+ UniqueName("descriptor_table", file_, options_), // 1
+ num_deps); // 2
+ format(
+ "// Force running AddDescriptors() at dynamic initialization time.\n"
+ "static bool $1$ = []() { $add_descriptors$(); return true; }();\n",
+ UniqueName("dynamic_init_dummy", file_, options_));
}
void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) {
+ Formatter format(printer, variables_);
const string scc_name = ClassName(scc->GetRepresentative());
// We use static and not anonymous namespace because symbol names are
// substantially shorter.
- printer->Print(
- "static void InitDefaults$scc_name$() {\n"
- " GOOGLE_PROTOBUF_VERIFY_VERSION;\n\n"
- , // awkward comma due to macro
- "scc_name", scc_name);
+ format("static void InitDefaults$1$() {\n",
+ UniqueName(scc_name, scc->GetRepresentative(), options_));
- printer->Indent();
+ if (options_.opensource_runtime) {
+ format(" GOOGLE_PROTOBUF_VERIFY_VERSION;\n\n");
+ }
+ format.Indent();
+
+ if (!options_.opensource_runtime) {
+ for (int i = 0; i < scc->children.size(); i++) {
+ const SCC* child_scc = scc->children[i];
+ const FileDescriptor* dependency = child_scc->GetRepresentative()->file();
+ if (!IsProto1(dependency, options_)) continue;
+ format("$1$();\n", UniqueName("InitDefaults", dependency, options_));
+ }
+ }
// First construct all the necessary default instances.
for (int i = 0; i < message_generators_.size(); i++) {
@@ -954,17 +891,18 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) {
// TODO(gerbens) This requires this function to be friend. Remove
// the need for this.
message_generators_[i]->GenerateFieldDefaultInstances(printer);
- printer->Print(
+ format(
"{\n"
- " void* ptr = &$ns$::_$classname$_default_instance_;\n"
- " new (ptr) $ns$::$classname$();\n",
- "ns", Namespace(message_generators_[i]->descriptor_),
- "classname", ClassName(message_generators_[i]->descriptor_));
- if (!IsMapEntryMessage(message_generators_[i]->descriptor_)) {
- printer->Print(
+ " void* ptr = &$1$::_$2$_default_instance_;\n"
+ " new (ptr) $1$::$2$();\n",
+ Namespace(message_generators_[i]->descriptor_), // 1
+ ClassName(message_generators_[i]->descriptor_)); // 2
+ if (options_.opensource_runtime &&
+ !IsMapEntryMessage(message_generators_[i]->descriptor_)) {
+ format(
" ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);\n");
}
- printer->Print("}\n");
+ format("}\n");
}
// TODO(gerbens) make default instances be the same as normal instances.
@@ -974,37 +912,44 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) {
if (scc_analyzer_.GetSCC(message_generators_[i]->descriptor_) != scc) {
continue;
}
- printer->Print("$classname$::InitAsDefaultInstance();\n", "classname",
- QualifiedClassName(message_generators_[i]->descriptor_));
- }
- printer->Outdent();
- printer->Print("}\n\n");
-
- printer->Print(
- "$dllexport_decl$::google::protobuf::internal::SCCInfo<$size$> "
- "scc_info_$scc_name$ =\n"
- " {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), "
- "$size$, InitDefaults$scc_name$}, {",
- "size", SimpleItoa(scc->children.size()), "scc_name",
- ClassName(scc->GetRepresentative()), "dllexport_decl",
- options_.dllexport_decl.empty() ? "" : options_.dllexport_decl + " ");
+ format("$1$::InitAsDefaultInstance();\n",
+ QualifiedClassName(message_generators_[i]->descriptor_));
+ }
+ format.Outdent();
+ format("}\n\n");
+
+ format(
+ "$dllexport_decl $::$proto_ns$::internal::SCCInfo<$1$> "
+ "scc_info_$2$ =\n"
+ " "
+ "{{ATOMIC_VAR_INIT(::$proto_ns$::internal::SCCInfoBase::kUninitialized), "
+ "$1$, InitDefaults$2$}, {",
+ scc->children.size(), // 1
+ UniqueName(ClassName(scc->GetRepresentative()), scc->GetRepresentative(),
+ options_));
for (const SCC* child : scc->children) {
auto repr = child->GetRepresentative();
- printer->Print("\n &$ns$::scc_info_$child$.base,", "ns",
- FileLevelNamespace(repr), "child", ClassName(repr));
+ if (IsProto1(repr->file(), options_)) {
+ GOOGLE_CHECK(!options_.opensource_runtime);
+ format("\n nullptr,");
+ continue;
+ }
+ format("\n &scc_info_$1$.base,",
+ UniqueName(ClassName(repr), repr, options_));
}
- printer->Print("}};\n\n");
+ format("}};\n\n");
}
void FileGenerator::GenerateTables(io::Printer* printer) {
+ Formatter format(printer, variables_);
if (options_.table_driven_parsing) {
// TODO(ckennelly): Gate this with the same options flag to enable
// table-driven parsing.
- printer->Print(
- "PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTableField\n"
- " const TableStruct::entries[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
- printer->Indent();
+ format(
+ "PROTOBUF_CONSTEXPR_VAR ::$proto_ns$::internal::ParseTableField\n"
+ " const $tablename$::entries[] "
+ "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n");
+ format.Indent();
std::vector<size_t> entries;
size_t count = 0;
@@ -1016,17 +961,18 @@ void FileGenerator::GenerateTables(io::Printer* printer) {
// We need these arrays to exist, and MSVC does not like empty arrays.
if (count == 0) {
- printer->Print("{0, 0, 0, ::google::protobuf::internal::kInvalidMask, 0, 0},\n");
+ format("{0, 0, 0, ::$proto_ns$::internal::kInvalidMask, 0, 0},\n");
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
"\n"
- "PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::AuxillaryParseTableField\n"
- " const TableStruct::aux[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
- printer->Indent();
+ "PROTOBUF_CONSTEXPR_VAR "
+ "::$proto_ns$::internal::AuxillaryParseTableField\n"
+ " const $tablename$::aux[] "
+ "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n");
+ format.Indent();
std::vector<size_t> aux_entries;
count = 0;
@@ -1037,16 +983,16 @@ void FileGenerator::GenerateTables(io::Printer* printer) {
}
if (count == 0) {
- printer->Print("::google::protobuf::internal::AuxillaryParseTableField(),\n");
+ format("::$proto_ns$::internal::AuxillaryParseTableField(),\n");
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
- "PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTable const\n"
- " TableStruct::schema[] "
- "GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
- printer->Indent();
+ "PROTOBUF_CONSTEXPR_VAR ::$proto_ns$::internal::ParseTable const\n"
+ " $tablename$::schema[] "
+ "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n");
+ format.Indent();
size_t offset = 0;
size_t aux_offset = 0;
@@ -1057,20 +1003,21 @@ void FileGenerator::GenerateTables(io::Printer* printer) {
}
if (message_generators_.empty()) {
- printer->Print("{ NULL, NULL, 0, -1, -1, false },\n");
+ format("{ NULL, NULL, 0, -1, -1, false },\n");
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
"\n");
}
if (!message_generators_.empty() && options_.table_driven_serialization) {
- printer->Print(
- "const ::google::protobuf::internal::FieldMetadata TableStruct::field_metadata[] "
+ format(
+ "const ::$proto_ns$::internal::FieldMetadata "
+ "$tablename$::field_metadata[] "
"= {\n");
- printer->Indent();
+ format.Indent();
std::vector<int> field_metadata_offsets;
int idx = 0;
for (int i = 0; i < message_generators_.size(); i++) {
@@ -1078,12 +1025,12 @@ void FileGenerator::GenerateTables(io::Printer* printer) {
idx += message_generators_[i]->GenerateFieldMetadata(printer);
}
field_metadata_offsets.push_back(idx);
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
- "const ::google::protobuf::internal::SerializationTable "
- "TableStruct::serialization_table[] = {\n");
- printer->Indent();
+ "const ::$proto_ns$::internal::SerializationTable "
+ "$tablename$::serialization_table[] = {\n");
+ format.Indent();
// We rely on the order we layout the tables to match the order we
// calculate them with FlattenMessagesInFile, so we check here that
// these match exactly.
@@ -1092,235 +1039,327 @@ void FileGenerator::GenerateTables(io::Printer* printer) {
GOOGLE_CHECK_EQ(calculated_order.size(), message_generators_.size());
for (int i = 0; i < message_generators_.size(); i++) {
GOOGLE_CHECK_EQ(calculated_order[i], message_generators_[i]->descriptor_);
- printer->Print(
- "{$num_fields$, TableStruct::field_metadata + $index$},\n",
- "classname", message_generators_[i]->classname_, "num_fields",
- SimpleItoa(field_metadata_offsets[i + 1] - field_metadata_offsets[i]),
- "index", SimpleItoa(field_metadata_offsets[i]));
+ format("{$1$, $tablename$::field_metadata + $2$},\n",
+ field_metadata_offsets[i + 1] - field_metadata_offsets[i], // 1
+ field_metadata_offsets[i]); // 2
}
- printer->Outdent();
- printer->Print(
+ format.Outdent();
+ format(
"};\n"
"\n");
}
}
-void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) {
- ForwardDeclarations decls;
- FillForwardDeclarations(&decls);
- decls.PrintForwardDeclarations(printer, options_);
+class FileGenerator::ForwardDeclarations {
+ public:
+ void AddMessage(const Descriptor* d) { classes_[ClassName(d)] = d; }
+ void AddEnum(const EnumDescriptor* d) { enums_[ClassName(d)] = d; }
+
+ void Print(const Formatter& format, const Options& options) const {
+ for (const auto& p : enums_) {
+ const string& enumname = p.first;
+ const EnumDescriptor* enum_desc = p.second;
+ format(
+ "enum ${1$$2$$}$ : int;\n"
+ "bool $2$_IsValid(int value);\n",
+ enum_desc, enumname);
+ }
+ for (const auto& p : classes_) {
+ const string& classname = p.first;
+ const Descriptor* class_desc = p.second;
+ format(
+ "class ${1$$2$$}$;\n"
+ "class $2$DefaultTypeInternal;\n"
+ "$dllexport_decl $extern "
+ "$2$DefaultTypeInternal _$2$_default_instance_;\n",
+ class_desc, classname);
+ if (options.lite_implicit_weak_fields) {
+ format("void $1$_ReferenceStrong();\n", classname);
+ }
+ }
+ }
+
+ void PrintTopLevelDecl(const Formatter& format) const {
+ for (const auto& pair : classes_) {
+ format(
+ "template<> $dllexport_decl $"
+ "$1$* Arena::CreateMaybeMessage<$1$>(Arena*);\n",
+ QualifiedClassName(pair.second));
+ }
+ }
+
+ private:
+ std::map<string, const Descriptor*> classes_;
+ std::map<string, const EnumDescriptor*> enums_;
+};
+
+static void PublicImportDFS(const FileDescriptor* fd,
+ std::unordered_set<const FileDescriptor*>* fd_set) {
+ for (int i = 0; i < fd->public_dependency_count(); i++) {
+ const FileDescriptor* dep = fd->public_dependency(i);
+ if (fd_set->insert(dep).second) PublicImportDFS(dep, fd_set);
+ }
}
-void FileGenerator::FillForwardDeclarations(ForwardDeclarations* decls) {
- for (int i = 0; i < package_parts_.size(); i++) {
- decls = decls->AddOrGetNamespace(package_parts_[i]);
+void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) {
+ Formatter format(printer, variables_);
+ std::vector<const Descriptor*> classes;
+ std::vector<const EnumDescriptor*> enums;
+
+ FlattenMessagesInFile(file_, &classes); // All messages need forward decls.
+
+ if (options_.proto_h) { // proto.h needs extra forward declarations.
+ // All classes / enums refered to as field members
+ std::vector<const FieldDescriptor*> fields;
+ ListAllFields(file_, &fields);
+ for (int i = 0; i < fields.size(); i++) {
+ classes.push_back(fields[i]->containing_type());
+ classes.push_back(fields[i]->message_type());
+ enums.push_back(fields[i]->enum_type());
+ }
+ ListAllTypesForServices(file_, &classes);
}
- // Generate enum definitions.
- for (int i = 0; i < enum_generators_.size(); i++) {
- enum_generators_[i]->FillForwardDeclaration(&decls->enums());
+
+ // Calculate the set of files whose definitions we get through include.
+ // No need to forward declare types that are defined in these.
+ std::unordered_set<const FileDescriptor*> public_set;
+ PublicImportDFS(file_, &public_set);
+
+ std::map<string, ForwardDeclarations> decls;
+ for (int i = 0; i < classes.size(); i++) {
+ const Descriptor* d = classes[i];
+ if (d && !public_set.count(d->file())) decls[Namespace(d)].AddMessage(d);
}
- // Generate forward declarations of classes.
- for (int i = 0; i < message_generators_.size(); i++) {
- message_generators_[i]->FillMessageForwardDeclarations(
- &decls->classes());
+ for (int i = 0; i < enums.size(); i++) {
+ const EnumDescriptor* d = enums[i];
+ if (d && !public_set.count(d->file())) decls[Namespace(d)].AddEnum(d);
+ }
+
+
+ NamespaceOpener ns(format);
+ for (const auto& pair : decls) {
+ ns.ChangeTo(pair.first);
+ pair.second.Print(format, options_);
+ }
+ ns.ChangeTo(variables_["proto_ns"]);
+ for (const auto& pair : decls) {
+ pair.second.PrintTopLevelDecl(format);
}
}
void FileGenerator::GenerateTopHeaderGuard(io::Printer* printer,
const string& filename_identifier) {
+ Formatter format(printer, variables_);
// Generate top of header.
- printer->Print(
+ format(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
"\n"
- "#ifndef PROTOBUF_INCLUDED_$filename_identifier$\n"
- "#define PROTOBUF_INCLUDED_$filename_identifier$\n"
+ "#ifndef PROTOBUF_INCLUDED_$1$\n"
+ "#define PROTOBUF_INCLUDED_$1$\n"
"\n"
+ "#include <limits>\n"
"#include <string>\n",
- "filename", file_->name(), "filename_identifier", filename_identifier);
- printer->Print("\n");
+ filename_identifier);
+ if (!options_.opensource_runtime && !enum_generators_.empty()) {
+ // Add header to provide std::is_integral for safe Enum_Name() function.
+ format("#include <type_traits>\n");
+ }
+ format("\n");
}
void FileGenerator::GenerateBottomHeaderGuard(
io::Printer* printer, const string& filename_identifier) {
- printer->Print(
- "#endif // PROTOBUF_INCLUDED_$filename_identifier$\n",
- "filename_identifier", filename_identifier);
+ Formatter format(printer, variables_);
+ format("#endif // PROTOBUF_INCLUDED_$1$\n", filename_identifier);
}
void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) {
+ Formatter format(printer, variables_);
if (UsingImplicitWeakFields(file_, options_)) {
- printer->Print("#include <google/protobuf/implicit_weak_message.h>\n");
- }
-
- printer->Print(
- "#include <google/protobuf/stubs/common.h>\n"
- "\n");
-
- // Verify the protobuf library header version is compatible with the protoc
- // version before going any further.
- printer->Print(
- "#if GOOGLE_PROTOBUF_VERSION < $min_header_version$\n"
- "#error This file was generated by a newer version of protoc which is\n"
- "#error incompatible with your Protocol Buffer headers. Please update\n"
- "#error your headers.\n"
- "#endif\n"
- "#if $protoc_version$ < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION\n"
- "#error This file was generated by an older version of protoc which is\n"
- "#error incompatible with your Protocol Buffer headers. Please\n"
- "#error regenerate this file with a newer version of protoc.\n"
- "#endif\n"
- "\n",
- "min_header_version",
- SimpleItoa(protobuf::internal::kMinHeaderVersionForProtoc),
- "protoc_version", SimpleItoa(GOOGLE_PROTOBUF_VERSION));
+ IncludeFile("net/proto2/public/implicit_weak_message.h", printer);
+ }
+ if (HasWeakFields(file_, options_)) {
+ GOOGLE_CHECK(!options_.opensource_runtime);
+ IncludeFile("net/proto2/public/weak_field_map.h", printer);
+ }
+ if (HasLazyFields(file_, options_)) {
+ GOOGLE_CHECK(!options_.opensource_runtime);
+ IncludeFile("net/proto2/public/lazy_field.h", printer);
+ }
+
+ if (options_.opensource_runtime) {
+ // Verify the protobuf library header version is compatible with the protoc
+ // version before going any further.
+ IncludeFile("net/proto2/public/port_def.inc", printer);
+ format(
+ "#if PROTOBUF_VERSION < $1$\n"
+ "#error This file was generated by a newer version of protoc which is\n"
+ "#error incompatible with your Protocol Buffer headers. Please update\n"
+ "#error your headers.\n"
+ "#endif\n"
+ "#if $2$ < PROTOBUF_MIN_PROTOC_VERSION\n"
+ "#error This file was generated by an older version of protoc which "
+ "is\n"
+ "#error incompatible with your Protocol Buffer headers. Please\n"
+ "#error regenerate this file with a newer version of protoc.\n"
+ "#endif\n"
+ "\n",
+ PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC, // 1
+ PROTOBUF_VERSION); // 2
+ IncludeFile("net/proto2/public/port_undef.inc", printer);
+ }
// OK, it's now safe to #include other files.
- printer->Print(
- "#include <google/protobuf/io/coded_stream.h>\n"
- "#include <google/protobuf/arena.h>\n"
- "#include <google/protobuf/arenastring.h>\n"
- "#include <google/protobuf/generated_message_table_driven.h>\n"
- "#include <google/protobuf/generated_message_util.h>\n"
- "#include <google/protobuf/inlined_string_field.h>\n");
-
+ IncludeFile("net/proto2/io/public/coded_stream.h", printer);
+ IncludeFile("net/proto2/public/arena.h", printer);
+ IncludeFile("net/proto2/public/arenastring.h", printer);
+ IncludeFile("net/proto2/public/generated_message_table_driven.h", printer);
+ IncludeFile("net/proto2/public/generated_message_util.h", printer);
+ IncludeFile("net/proto2/public/inlined_string_field.h", printer);
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/metadata.h>\n");
+ IncludeFile("net/proto2/public/metadata.h", printer);
} else {
- printer->Print(
- "#include <google/protobuf/metadata_lite.h>\n");
+ IncludeFile("net/proto2/public/metadata_lite.h", printer);
}
if (!message_generators_.empty()) {
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/message.h>\n");
+ IncludeFile("net/proto2/public/message.h", printer);
} else {
- printer->Print(
- "#include <google/protobuf/message_lite.h>\n");
+ IncludeFile("net/proto2/public/message_lite.h", printer);
+ }
+ }
+ if (options_.opensource_runtime) {
+ // Open-source relies on unconditional includes of these.
+ IncludeFileAndExport("net/proto2/public/repeated_field.h", printer);
+ IncludeFileAndExport("net/proto2/public/extension_set.h", printer);
+ } else {
+ // Google3 includes these files only when they are necessary.
+ if (HasExtensionsOrExtendableMessage(file_)) {
+ IncludeFileAndExport("net/proto2/public/extension_set.h", printer);
+ }
+ if (HasRepeatedFields(file_)) {
+ IncludeFileAndExport("net/proto2/public/repeated_field.h", printer);
+ }
+ if (HasStringPieceFields(file_, options_)) {
+ IncludeFile("net/proto2/public/string_piece_field_support.h", printer);
+ }
+ if (HasCordFields(file_, options_)) {
+ format("#include \"third_party/absl/strings/cord.h\"\n");
}
}
- printer->Print(
- "#include <google/protobuf/repeated_field.h>"
- " // IWYU pragma: export\n"
- "#include <google/protobuf/extension_set.h>"
- " // IWYU pragma: export\n");
if (HasMapFields(file_)) {
- printer->Print(
- "#include <google/protobuf/map.h>"
- " // IWYU pragma: export\n");
+ IncludeFileAndExport("net/proto2/public/map.h", printer);
if (HasDescriptorMethods(file_, options_)) {
- printer->Print("#include <google/protobuf/map_entry.h>\n");
- printer->Print("#include <google/protobuf/map_field_inl.h>\n");
+ IncludeFile("net/proto2/public/map_entry.h", printer);
+ IncludeFile("net/proto2/public/map_field_inl.h", printer);
} else {
- printer->Print("#include <google/protobuf/map_entry_lite.h>\n");
- printer->Print("#include <google/protobuf/map_field_lite.h>\n");
+ IncludeFile("net/proto2/public/map_entry_lite.h", printer);
+ IncludeFile("net/proto2/public/map_field_lite.h", printer);
}
}
if (HasEnumDefinitions(file_)) {
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/generated_enum_reflection.h>\n");
+ IncludeFile("net/proto2/public/generated_enum_reflection.h", printer);
} else {
- printer->Print(
- "#include <google/protobuf/generated_enum_util.h>\n");
+ IncludeFile("net/proto2/public/generated_enum_util.h", printer);
}
}
if (HasGenericServices(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/service.h>\n");
+ IncludeFile("net/proto2/public/service.h", printer);
}
if (UseUnknownFieldSet(file_, options_) && !message_generators_.empty()) {
- printer->Print(
- "#include <google/protobuf/unknown_field_set.h>\n");
+ IncludeFile("net/proto2/public/unknown_field_set.h", printer);
}
-
if (IsAnyMessage(file_)) {
- printer->Print(
- "#include <google/protobuf/any.h>\n");
+ IncludeFile("net/proto2/internal/any.h", printer);
}
}
void FileGenerator::GenerateMetadataPragma(io::Printer* printer,
const string& info_path) {
+ Formatter format(printer, variables_);
if (!info_path.empty() && !options_.annotation_pragma_name.empty() &&
!options_.annotation_guard_name.empty()) {
- printer->Print(
+ format.Set("guard", options_.annotation_guard_name);
+ format.Set("pragma", options_.annotation_pragma_name);
+ format.Set("info_path", info_path);
+ format(
"#ifdef $guard$\n"
"#pragma $pragma$ \"$info_path$\"\n"
- "#endif // $guard$\n",
- "guard", options_.annotation_guard_name, "pragma",
- options_.annotation_pragma_name, "info_path", info_path);
+ "#endif // $guard$\n");
}
}
void FileGenerator::GenerateDependencyIncludes(io::Printer* printer) {
- std::set<string> public_import_names;
- for (int i = 0; i < file_->public_dependency_count(); i++) {
- public_import_names.insert(file_->public_dependency(i)->name());
- }
-
+ Formatter format(printer, variables_);
for (int i = 0; i < file_->dependency_count(); i++) {
- const bool use_system_include = IsWellKnownMessage(file_->dependency(i));
- const string& name = file_->dependency(i)->name();
- bool public_import = (public_import_names.count(name) != 0);
- string basename = StripProto(name);
+ string basename = StripProto(file_->dependency(i)->name());
+
+ // Do not import weak deps.
+ if (IsDepWeak(file_->dependency(i))) continue;
+ if (IsBootstrapProto(options_, file_)) {
+ GetBootstrapBasename(options_, basename, &basename);
+ }
- printer->Print(
- "#include $left$$dependency$.pb.h$right$$iwyu$\n",
- "dependency", basename,
- "iwyu", (public_import) ? " // IWYU pragma: export" : "",
- "left", use_system_include ? "<" : "\"",
- "right", use_system_include ? ">" : "\"");
+ format("#include $1$\n",
+ CreateHeaderInclude(basename + ".pb.h", file_->dependency(i)));
}
}
void FileGenerator::GenerateGlobalStateFunctionDeclarations(
io::Printer* printer) {
-// Forward-declare the AddDescriptors, InitDefaults because these are called
-// by .pb.cc files depending on this file.
- printer->Print(
+ Formatter format(printer, variables_);
+ // Forward-declare the AddDescriptors, InitDefaults because these are called
+ // by .pb.cc files depending on this file.
+ //
+ // The TableStruct is also outputted in weak_message_field.cc, because the
+ // weak fields must refer to table struct but cannot include the header.
+ // Also it annotates extra weak attributes.
+ // TODO(gerbens) make sure this situation is handled better.
+ format(
"\n"
- "namespace $file_namespace$ {\n"
"// Internal implementation detail -- do not use these members.\n"
- "struct $dllexport_decl$TableStruct {\n"
+ "struct $dllexport_decl $$tablename$ {\n"
// These tables describe how to serialize and parse messages. Used
// for table driven code.
- " static const ::google::protobuf::internal::ParseTableField entries[];\n"
- " static const ::google::protobuf::internal::AuxillaryParseTableField aux[];\n"
- " static const ::google::protobuf::internal::ParseTable schema[$num$];\n"
- " static const ::google::protobuf::internal::FieldMetadata field_metadata[];\n"
- " static const ::google::protobuf::internal::SerializationTable "
+ " static const ::$proto_ns$::internal::ParseTableField entries[]\n"
+ " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n"
+ " static const ::$proto_ns$::internal::AuxillaryParseTableField aux[]\n"
+ " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n"
+ " static const ::$proto_ns$::internal::ParseTable schema[$1$]\n"
+ " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n"
+ " static const ::$proto_ns$::internal::FieldMetadata field_metadata[];\n"
+ " static const ::$proto_ns$::internal::SerializationTable "
"serialization_table[];\n"
- " static const ::google::protobuf::uint32 offsets[];\n"
+ " static const $uint32$ offsets[];\n"
"};\n",
- "file_namespace", FileLevelNamespace(file_), "dllexport_decl",
- options_.dllexport_decl.empty() ? "" : options_.dllexport_decl + " ",
- "num", SimpleItoa(std::max(size_t(1), message_generators_.size())));
+ std::max(size_t(1), message_generators_.size()));
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "void $dllexport_decl$AddDescriptors();\n", "dllexport_decl",
- options_.dllexport_decl.empty() ? "" : options_.dllexport_decl + " ");
+ format("void $dllexport_decl $$add_descriptors$();\n");
+ if (!options_.opensource_runtime) {
+ // TODO(gerbens) This is for proto1 interoperability. Remove when proto1
+ // is gone.
+ format("void $dllexport_decl $$init_defaults$();\n");
+ }
}
- printer->Print(
- "} // namespace $file_namespace$\n",
- "file_namespace", FileLevelNamespace(file_));
}
void FileGenerator::GenerateMessageDefinitions(io::Printer* printer) {
+ Formatter format(printer, variables_);
// Generate class definitions.
for (int i = 0; i < message_generators_.size(); i++) {
if (i > 0) {
- printer->Print("\n");
- printer->Print(kThinSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThinSeparator);
+ format("\n");
}
message_generators_[i]->GenerateClassDefinition(printer);
}
@@ -1334,74 +1373,77 @@ void FileGenerator::GenerateEnumDefinitions(io::Printer* printer) {
}
void FileGenerator::GenerateServiceDefinitions(io::Printer* printer) {
+ Formatter format(printer, variables_);
if (HasGenericServices(file_, options_)) {
// Generate service definitions.
for (int i = 0; i < service_generators_.size(); i++) {
if (i > 0) {
- printer->Print("\n");
- printer->Print(kThinSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThinSeparator);
+ format("\n");
}
service_generators_[i]->GenerateDeclarations(printer);
}
- printer->Print("\n");
- printer->Print(kThickSeparator);
- printer->Print("\n");
+ format("\n");
+ format(kThickSeparator);
+ format("\n");
}
}
void FileGenerator::GenerateExtensionIdentifiers(io::Printer* printer) {
// Declare extension identifiers. These are in global scope and so only
// the global scope extensions.
- for (int i = 0; i < file_->extension_count(); i++) {
- extension_generators_owner_[i]->GenerateDeclaration(printer);
+ for (auto& extension_generator : extension_generators_) {
+ if (extension_generator->IsScoped()) continue;
+ extension_generator->GenerateDeclaration(printer);
}
}
void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) {
+ Formatter format(printer, variables_);
// TODO(gerbens) remove pragmas when gcc is no longer used. Current version
// of gcc fires a bogus error when compiled with strict-aliasing.
- printer->Print(
- "#ifdef __GNUC__\n"
- " #pragma GCC diagnostic push\n"
- " #pragma GCC diagnostic ignored \"-Wstrict-aliasing\"\n"
- "#endif // __GNUC__\n");
+ format(
+ "#ifdef __GNUC__\n"
+ " #pragma GCC diagnostic push\n"
+ " #pragma GCC diagnostic ignored \"-Wstrict-aliasing\"\n"
+ "#endif // __GNUC__\n");
// Generate class inline methods.
for (int i = 0; i < message_generators_.size(); i++) {
if (i > 0) {
- printer->Print(kThinSeparator);
- printer->Print("\n");
+ format(kThinSeparator);
+ format("\n");
}
message_generators_[i]->GenerateInlineMethods(printer);
}
- printer->Print(
- "#ifdef __GNUC__\n"
- " #pragma GCC diagnostic pop\n"
- "#endif // __GNUC__\n");
+ format(
+ "#ifdef __GNUC__\n"
+ " #pragma GCC diagnostic pop\n"
+ "#endif // __GNUC__\n");
for (int i = 0; i < message_generators_.size(); i++) {
if (i > 0) {
- printer->Print(kThinSeparator);
- printer->Print("\n");
+ format(kThinSeparator);
+ format("\n");
}
}
}
void FileGenerator::GenerateProto2NamespaceEnumSpecializations(
io::Printer* printer) {
+ Formatter format(printer, variables_);
// Emit GetEnumDescriptor specializations into google::protobuf namespace:
if (HasEnumDefinitions(file_)) {
- printer->Print(
- "\n"
- "namespace google {\nnamespace protobuf {\n"
- "\n");
- for (int i = 0; i < enum_generators_.size(); i++) {
- enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
+ format("\n");
+ {
+ NamespaceOpener proto_ns(ProtobufNamespace(options_), format);
+ format("\n");
+ for (int i = 0; i < enum_generators_.size(); i++) {
+ enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
+ }
+ format("\n");
}
- printer->Print(
- "\n"
- "} // namespace protobuf\n} // namespace google\n");
}
}