diff options
author | Adam Cozzette <acozzette@google.com> | 2017-12-01 10:05:10 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2017-12-01 10:05:10 -0800 |
commit | 92a7e778e7394386f413cec28d67a07630f784b1 (patch) | |
tree | cb5673c7c09b0d3905b48a24765d07e423bc5b30 /src/google/protobuf/compiler/cpp/cpp_file.cc | |
parent | ce0a53273a400369932ba788d17500336a6ecaad (diff) |
Integrated internal changes from Google
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_file.cc')
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_file.cc | 95 |
1 files changed, 81 insertions, 14 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index 52a16835..1f90ae43 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -397,7 +397,7 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) { // Define default instances GenerateSourceDefaultInstance(idx, printer); - if (UsingImplicitWeakFields(file_, options_)) { + if (options_.lite_implicit_weak_fields) { printer->Print("void $classname$_ReferenceStrong() {}\n", "classname", message_generators_[idx]->classname_); } @@ -416,6 +416,13 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) { GenerateInitForSCC(GetSCC(message_generators_[idx]->descriptor_), printer); } + + printer->Print( + "namespace google {\nnamespace protobuf {\n"); + message_generators_[idx]->GenerateSourceInProto2Namespace(printer); + printer->Print( + "} // namespace protobuf\n} // namespace google\n"); + printer->Print( "\n" "// @@protoc_insertion_point(global_scope)\n"); @@ -467,7 +474,7 @@ void FileGenerator::GenerateSource(io::Printer* printer) { // Define default instances for (int i = 0; i < message_generators_.size(); i++) { GenerateSourceDefaultInstance(i, printer); - if (UsingImplicitWeakFields(file_, options_)) { + if (options_.lite_implicit_weak_fields) { printer->Print("void $classname$_ReferenceStrong() {}\n", "classname", message_generators_[i]->classname_); } @@ -525,6 +532,15 @@ void FileGenerator::GenerateSource(io::Printer* printer) { "\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"); @@ -553,7 +569,42 @@ class FileGenerator::ForwardDeclarations { std::map<string, const Descriptor*>& classes() { return classes_; } std::map<string, const EnumDescriptor*>& enums() { return enums_; } - void Print(io::Printer* printer, const Options& options) const { + 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); + } + } + + void PrintDeclarationsInsideNamespace(io::Printer* printer, + const Options& options) const { for (std::map<string, const EnumDescriptor *>::const_iterator it = enums_.begin(), end = enums_.end(); @@ -584,20 +635,36 @@ class FileGenerator::ForwardDeclarations { "classname", it->first); } } - for (std::map<string, ForwardDeclarations *>::const_iterator - it = namespaces_.begin(), - end = namespaces_.end(); + } + + 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) { - printer->Print("namespace $nsname$ {\n", - "nsname", it->first); - it->second->Print(printer, options); - printer->Print("} // namespace $nsname$\n", - "nsname", it->first); + const Descriptor* d = it->second; + string extra_class_qualifier; + // "class" is to disambiguate in case there is also a function with this + // name. There is code out there that does this! + printer->Print( + "template<> " + "$dllexport_decl$" + "$class$$classname$* Arena::$func$< $class$$classname$>(Arena*);\n", + "classname", QualifiedClassName(d), + "func", MessageCreateFunction(d), + "class", extra_class_qualifier, + "dllexport_decl", + options.dllexport_decl.empty() ? "" : options.dllexport_decl + " "); } + printer->Print( + "} // namespace protobuf\n} // namespace google\n"); } - - private: std::map<string, ForwardDeclarations*> namespaces_; std::map<string, const Descriptor*> classes_; std::map<string, const EnumDescriptor*> enums_; @@ -1053,7 +1120,7 @@ void FileGenerator::GenerateInitializationCode(io::Printer* printer) { void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) { ForwardDeclarations decls; FillForwardDeclarations(&decls); - decls.Print(printer, options_); + decls.PrintForwardDeclarations(printer, options_); } void FileGenerator::FillForwardDeclarations(ForwardDeclarations* decls) { |