cpp_file.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. namespace google {
  45. namespace protobuf {
  46. class FileDescriptor; // descriptor.h
  47. namespace io {
  48. class Printer; // printer.h
  49. }
  50. }
  51. namespace protobuf {
  52. namespace compiler {
  53. namespace cpp {
  54. class EnumGenerator; // enum.h
  55. class MessageGenerator; // message.h
  56. class ServiceGenerator; // service.h
  57. class ExtensionGenerator; // extension.h
  58. class FileGenerator {
  59. public:
  60. // See generator.cc for the meaning of dllexport_decl.
  61. FileGenerator(const FileDescriptor* file, const Options& options);
  62. ~FileGenerator();
  63. // Shared code between the two header generators below.
  64. void GenerateHeader(io::Printer* printer);
  65. // info_path, if non-empty, should be the path (relative to printer's output)
  66. // to the metadata file describing this proto header.
  67. void GenerateProtoHeader(io::Printer* printer,
  68. const string& info_path);
  69. // info_path, if non-empty, should be the path (relative to printer's output)
  70. // to the metadata file describing this PB header.
  71. void GeneratePBHeader(io::Printer* printer,
  72. const string& info_path);
  73. void GenerateSource(io::Printer* printer);
  74. int NumMessages() const { return message_generators_.size(); }
  75. // Similar to GenerateSource but generates only one message
  76. void GenerateSourceForMessage(int idx, io::Printer* printer);
  77. void GenerateGlobalSource(io::Printer* printer);
  78. private:
  79. // Internal type used by GenerateForwardDeclarations (defined in file.cc).
  80. class ForwardDeclarations;
  81. void GenerateSourceIncludes(io::Printer* printer);
  82. void GenerateSourceDefaultInstance(int idx, io::Printer* printer);
  83. void GenerateInitForSCC(const SCC* scc, io::Printer* printer);
  84. void GenerateTables(io::Printer* printer);
  85. void GenerateReflectionInitializationCode(io::Printer* printer);
  86. // For other imports, generates their forward-declarations.
  87. void GenerateForwardDeclarations(io::Printer* printer);
  88. // Internal helper used by GenerateForwardDeclarations: fills 'decls'
  89. // with all necessary forward-declarations for this file and its
  90. // transient depednencies.
  91. void FillForwardDeclarations(ForwardDeclarations* decls);
  92. // Generates top or bottom of a header file.
  93. void GenerateTopHeaderGuard(io::Printer* printer,
  94. const string& filename_identifier);
  95. void GenerateBottomHeaderGuard(io::Printer* printer,
  96. const string& filename_identifier);
  97. // Generates #include directives.
  98. void GenerateLibraryIncludes(io::Printer* printer);
  99. void GenerateDependencyIncludes(io::Printer* printer);
  100. // Generate a pragma to pull in metadata using the given info_path (if
  101. // non-empty). info_path should be relative to printer's output.
  102. void GenerateMetadataPragma(io::Printer* printer, const string& info_path);
  103. // Generates a couple of different pieces before definitions:
  104. void GenerateGlobalStateFunctionDeclarations(io::Printer* printer);
  105. // Generates types for classes.
  106. void GenerateMessageDefinitions(io::Printer* printer);
  107. void GenerateEnumDefinitions(io::Printer* printer);
  108. // Generates generic service definitions.
  109. void GenerateServiceDefinitions(io::Printer* printer);
  110. // Generates extension identifiers.
  111. void GenerateExtensionIdentifiers(io::Printer* printer);
  112. // Generates inline function defintions.
  113. void GenerateInlineFunctionDefinitions(io::Printer* printer);
  114. void GenerateProto2NamespaceEnumSpecializations(io::Printer* printer);
  115. // Sometimes the names we use in a .proto file happen to be defined as macros
  116. // on some platforms (e.g., macro/minor used in plugin.proto are defined as
  117. // macros in sys/types.h on FreeBSD and a few other platforms). To make the
  118. // generated code compile on these platforms, we either have to undef the
  119. // macro for these few platforms, or rename the field name for all platforms.
  120. // Since these names are part of protobuf public API, renaming is generally
  121. // a breaking change so we prefer the #undef approach.
  122. void GenerateMacroUndefs(io::Printer* printer);
  123. bool IsSCCRepresentative(const Descriptor* d) {
  124. return GetSCCRepresentative(d) == d;
  125. }
  126. const Descriptor* GetSCCRepresentative(const Descriptor* d) {
  127. return GetSCC(d)->GetRepresentative();
  128. }
  129. const SCC* GetSCC(const Descriptor* d) {
  130. return scc_analyzer_.GetSCC(d);
  131. }
  132. const FileDescriptor* file_;
  133. const Options options_;
  134. SCCAnalyzer scc_analyzer_;
  135. // Contains the post-order walk of all the messages (and child messages) in
  136. // this file. If you need a pre-order walk just reverse iterate.
  137. std::vector<MessageGenerator*> message_generators_;
  138. std::vector<EnumGenerator*> enum_generators_;
  139. std::vector<ServiceGenerator*> service_generators_;
  140. std::vector<ExtensionGenerator*> extension_generators_;
  141. // These members are just for owning (and thus proper deleting).
  142. // Nested (enum/extension)_generators are owned by child messages.
  143. std::unique_ptr<std::unique_ptr<EnumGenerator> []> enum_generators_owner_;
  144. std::unique_ptr<std::unique_ptr<ServiceGenerator> []>
  145. service_generators_owner_;
  146. std::unique_ptr<std::unique_ptr<ExtensionGenerator> []>
  147. extension_generators_owner_;
  148. // E.g. if the package is foo.bar, package_parts_ is {"foo", "bar"}.
  149. std::vector<string> package_parts_;
  150. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
  151. };
  152. } // namespace cpp
  153. } // namespace compiler
  154. } // namespace protobuf
  155. } // namespace google
  156. #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__