cpp_file.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
  35. #include <algorithm>
  36. #include <memory>
  37. #include <set>
  38. #include <string>
  39. #include <vector>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/compiler/cpp/cpp_field.h>
  42. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  43. #include <google/protobuf/compiler/cpp/cpp_options.h>
  44. #include <google/protobuf/compiler/scc.h>
  45. namespace google {
  46. namespace protobuf {
  47. class FileDescriptor; // descriptor.h
  48. namespace io {
  49. class Printer; // printer.h
  50. }
  51. } // namespace protobuf
  52. } // namespace google
  53. namespace google {
  54. namespace protobuf {
  55. namespace compiler {
  56. namespace cpp {
  57. class EnumGenerator; // enum.h
  58. class MessageGenerator; // message.h
  59. class ServiceGenerator; // service.h
  60. class ExtensionGenerator; // extension.h
  61. class FileGenerator {
  62. public:
  63. // See generator.cc for the meaning of dllexport_decl.
  64. FileGenerator(const FileDescriptor* file, const Options& options);
  65. ~FileGenerator();
  66. // Shared code between the two header generators below.
  67. void GenerateHeader(io::Printer* printer);
  68. // info_path, if non-empty, should be the path (relative to printer's
  69. // output) to the metadata file describing this proto header.
  70. void GenerateProtoHeader(io::Printer* printer, const std::string& info_path);
  71. // info_path, if non-empty, should be the path (relative to printer's
  72. // output) to the metadata file describing this PB header.
  73. void GeneratePBHeader(io::Printer* printer, const std::string& info_path);
  74. void GenerateSource(io::Printer* printer);
  75. // The following member functions are used when the lite_implicit_weak_fields
  76. // option is set. In this mode the code is organized a bit differently to
  77. // promote better linker stripping of unused code. In particular, we generate
  78. // one .cc file per message, one .cc file per extension, and a main pb.cc file
  79. // containing everything else.
  80. int NumMessages() const { return message_generators_.size(); }
  81. int NumExtensions() const { return extension_generators_.size(); }
  82. // Generates the source file for one message.
  83. void GenerateSourceForMessage(int idx, io::Printer* printer);
  84. // Generates the source file for one extension.
  85. void GenerateSourceForExtension(int idx, io::Printer* printer);
  86. // Generates a source file containing everything except messages and
  87. // extensions.
  88. void GenerateGlobalSource(io::Printer* printer);
  89. private:
  90. // Internal type used by GenerateForwardDeclarations (defined in file.cc).
  91. class ForwardDeclarations;
  92. struct CrossFileReferences;
  93. void IncludeFile(const std::string& google3_name, io::Printer* printer) {
  94. DoIncludeFile(google3_name, false, printer);
  95. }
  96. void IncludeFileAndExport(const std::string& google3_name,
  97. io::Printer* printer) {
  98. DoIncludeFile(google3_name, true, printer);
  99. }
  100. void DoIncludeFile(const std::string& google3_name, bool do_export,
  101. io::Printer* printer);
  102. std::string CreateHeaderInclude(const std::string& basename,
  103. const FileDescriptor* file);
  104. void GetCrossFileReferencesForField(const FieldDescriptor* field,
  105. CrossFileReferences* refs);
  106. void GetCrossFileReferencesForFile(const FileDescriptor* file,
  107. CrossFileReferences* refs);
  108. void GenerateInternalForwardDeclarations(const CrossFileReferences& refs,
  109. io::Printer* printer);
  110. void GenerateSourceIncludes(io::Printer* printer);
  111. void GenerateSourceDefaultInstance(int idx, io::Printer* printer);
  112. void GenerateInitForSCC(const SCC* scc, const CrossFileReferences& refs,
  113. io::Printer* printer);
  114. void GenerateTables(io::Printer* printer);
  115. void GenerateReflectionInitializationCode(io::Printer* printer);
  116. // For other imports, generates their forward-declarations.
  117. void GenerateForwardDeclarations(io::Printer* printer);
  118. // Generates top or bottom of a header file.
  119. void GenerateTopHeaderGuard(io::Printer* printer, bool pb_h);
  120. void GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h);
  121. // Generates #include directives.
  122. void GenerateLibraryIncludes(io::Printer* printer);
  123. void GenerateDependencyIncludes(io::Printer* printer);
  124. // Generate a pragma to pull in metadata using the given info_path (if
  125. // non-empty). info_path should be relative to printer's output.
  126. void GenerateMetadataPragma(io::Printer* printer,
  127. const std::string& info_path);
  128. // Generates a couple of different pieces before definitions:
  129. void GenerateGlobalStateFunctionDeclarations(io::Printer* printer);
  130. // Generates types for classes.
  131. void GenerateMessageDefinitions(io::Printer* printer);
  132. void GenerateEnumDefinitions(io::Printer* printer);
  133. // Generates generic service definitions.
  134. void GenerateServiceDefinitions(io::Printer* printer);
  135. // Generates extension identifiers.
  136. void GenerateExtensionIdentifiers(io::Printer* printer);
  137. // Generates inline function definitions.
  138. void GenerateInlineFunctionDefinitions(io::Printer* printer);
  139. void GenerateProto2NamespaceEnumSpecializations(io::Printer* printer);
  140. // Sometimes the names we use in a .proto file happen to be defined as
  141. // macros on some platforms (e.g., macro/minor used in plugin.proto are
  142. // defined as macros in sys/types.h on FreeBSD and a few other platforms).
  143. // To make the generated code compile on these platforms, we either have to
  144. // undef the macro for these few platforms, or rename the field name for all
  145. // platforms. Since these names are part of protobuf public API, renaming is
  146. // generally a breaking change so we prefer the #undef approach.
  147. void GenerateMacroUndefs(io::Printer* printer);
  148. bool IsDepWeak(const FileDescriptor* dep) const {
  149. if (weak_deps_.count(dep) != 0) {
  150. GOOGLE_CHECK(!options_.opensource_runtime);
  151. return true;
  152. }
  153. return false;
  154. }
  155. std::set<const FileDescriptor*> weak_deps_;
  156. const FileDescriptor* file_;
  157. const Options options_;
  158. MessageSCCAnalyzer scc_analyzer_;
  159. std::map<std::string, std::string> variables_;
  160. // Contains the post-order walk of all the messages (and child messages) in
  161. // this file. If you need a pre-order walk just reverse iterate.
  162. std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
  163. std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
  164. std::vector<std::unique_ptr<ServiceGenerator>> service_generators_;
  165. std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
  166. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
  167. };
  168. } // namespace cpp
  169. } // namespace compiler
  170. } // namespace protobuf
  171. } // namespace google
  172. #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__