cpp_file.h
Go to the documentation of this file.
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 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
37 
38 #include <algorithm>
39 #include <memory>
40 #include <set>
41 #include <string>
42 #include <vector>
48 
49 namespace google {
50 namespace protobuf {
51 class FileDescriptor; // descriptor.h
52 namespace io {
53 class Printer; // printer.h
54 }
55 } // namespace protobuf
56 } // namespace google
57 
58 namespace google {
59 namespace protobuf {
60 namespace compiler {
61 namespace cpp {
62 
63 class EnumGenerator; // enum.h
64 class MessageGenerator; // message.h
65 class ServiceGenerator; // service.h
66 class ExtensionGenerator; // extension.h
67 
69  public:
70  // See generator.cc for the meaning of dllexport_decl.
71  FileGenerator(const FileDescriptor* file, const Options& options);
73 
74  // Shared code between the two header generators below.
75  void GenerateHeader(io::Printer* printer);
76 
77  // info_path, if non-empty, should be the path (relative to printer's
78  // output) to the metadata file describing this proto header.
79  void GenerateProtoHeader(io::Printer* printer, const std::string& info_path);
80  // info_path, if non-empty, should be the path (relative to printer's
81  // output) to the metadata file describing this PB header.
82  void GeneratePBHeader(io::Printer* printer, const std::string& info_path);
83  void GenerateSource(io::Printer* printer);
84 
85  int NumMessages() const { return message_generators_.size(); }
86  // Similar to GenerateSource but generates only one message
87  void GenerateSourceForMessage(int idx, io::Printer* printer);
88  void GenerateGlobalSource(io::Printer* printer);
89 
90  private:
91  // Internal type used by GenerateForwardDeclarations (defined in file.cc).
92  class ForwardDeclarations;
93  struct CrossFileReferences;
94 
95  void IncludeFile(const std::string& google3_name, io::Printer* printer) {
96  DoIncludeFile(google3_name, false, printer);
97  }
98  void IncludeFileAndExport(const std::string& google3_name,
99  io::Printer* printer) {
100  DoIncludeFile(google3_name, true, printer);
101  }
102  void DoIncludeFile(const std::string& google3_name, bool do_export,
103  io::Printer* printer);
104 
106  const FileDescriptor* file);
108  CrossFileReferences* refs);
110  CrossFileReferences* refs);
111  void GenerateInternalForwardDeclarations(const CrossFileReferences& refs,
112  io::Printer* printer);
113  void GenerateSourceIncludes(io::Printer* printer);
114  void GenerateSourceDefaultInstance(int idx, io::Printer* printer);
115 
116  void GenerateInitForSCC(const SCC* scc, io::Printer* printer);
117  void GenerateTables(io::Printer* printer);
119 
120  // For other imports, generates their forward-declarations.
122 
123  // Generates top or bottom of a header file.
124  void GenerateTopHeaderGuard(io::Printer* printer, bool pb_h);
125  void GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h);
126 
127  // Generates #include directives.
128  void GenerateLibraryIncludes(io::Printer* printer);
130 
131  // Generate a pragma to pull in metadata using the given info_path (if
132  // non-empty). info_path should be relative to printer's output.
133  void GenerateMetadataPragma(io::Printer* printer,
134  const std::string& info_path);
135 
136  // Generates a couple of different pieces before definitions:
138 
139  // Generates types for classes.
141 
142  void GenerateEnumDefinitions(io::Printer* printer);
143 
144  // Generates generic service definitions.
146 
147  // Generates extension identifiers.
149 
150  // Generates inline function defintions.
152 
154 
155  // Sometimes the names we use in a .proto file happen to be defined as
156  // macros on some platforms (e.g., macro/minor used in plugin.proto are
157  // defined as macros in sys/types.h on FreeBSD and a few other platforms).
158  // To make the generated code compile on these platforms, we either have to
159  // undef the macro for these few platforms, or rename the field name for all
160  // platforms. Since these names are part of protobuf public API, renaming is
161  // generally a breaking change so we prefer the #undef approach.
162  void GenerateMacroUndefs(io::Printer* printer);
163 
165  return GetSCCRepresentative(d) == d;
166  }
168  return GetSCC(d)->GetRepresentative();
169  }
170  const SCC* GetSCC(const Descriptor* d) { return scc_analyzer_.GetSCC(d); }
171 
172  bool IsDepWeak(const FileDescriptor* dep) const {
173  if (weak_deps_.count(dep) != 0) {
175  return true;
176  }
177  return false;
178  }
179 
180  std::set<const FileDescriptor*> weak_deps_;
181  std::vector<const SCC*> sccs_;
182 
185 
187 
188  std::map<std::string, std::string> variables_;
189 
190  // Contains the post-order walk of all the messages (and child messages) in
191  // this file. If you need a pre-order walk just reverse iterate.
192  std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
193  std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
194  std::vector<std::unique_ptr<ServiceGenerator>> service_generators_;
195  std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
196 
198 };
199 
200 } // namespace cpp
201 } // namespace compiler
202 } // namespace protobuf
203 } // namespace google
204 
205 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
google::protobuf::compiler::cpp::FileGenerator::scc_analyzer_
MessageSCCAnalyzer scc_analyzer_
Definition: cpp_file.h:186
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::compiler::cpp::FileGenerator::message_generators_
std::vector< std::unique_ptr< MessageGenerator > > message_generators_
Definition: cpp_file.h:192
google::protobuf::compiler::SCC
Definition: scc.h:49
google::protobuf::compiler::cpp::FileGenerator::GenerateSource
void GenerateSource(io::Printer *printer)
Definition: cpp_file.cc:629
google::protobuf::compiler::cpp::FileGenerator::GenerateHeader
void GenerateHeader(io::Printer *printer)
Definition: cpp_file.cc:182
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::cpp::FileGenerator::GenerateServiceDefinitions
void GenerateServiceDefinitions(io::Printer *printer)
Definition: cpp_file.cc:1407
FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:125
google::protobuf::compiler::cpp::FileGenerator::weak_deps_
std::set< const FileDescriptor * > weak_deps_
Definition: cpp_file.h:180
google::protobuf::compiler::cpp::FileGenerator::DoIncludeFile
void DoIncludeFile(const std::string &google3_name, bool do_export, io::Printer *printer)
Definition: cpp_file.cc:328
google::protobuf::compiler::cpp::FileGenerator::GenerateDependencyIncludes
void GenerateDependencyIncludes(io::Printer *printer)
Definition: cpp_file.cc:1335
google::protobuf::compiler::cpp::FileGenerator::GenerateProto2NamespaceEnumSpecializations
void GenerateProto2NamespaceEnumSpecializations(io::Printer *printer)
Definition: cpp_file.cc:1465
google::protobuf::compiler::cpp::FileGenerator::IncludeFile
void IncludeFile(const std::string &google3_name, io::Printer *printer)
Definition: cpp_file.h:95
google::protobuf::compiler::cpp::FileGenerator::GenerateExtensionIdentifiers
void GenerateExtensionIdentifiers(io::Printer *printer)
Definition: cpp_file.cc:1426
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
scc.h
google::protobuf::compiler::cpp::FileGenerator::GenerateTables
void GenerateTables(io::Printer *printer)
Definition: cpp_file.cc:972
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceDefaultInstance
void GenerateSourceDefaultInstance(int idx, io::Printer *printer)
Definition: cpp_file.cc:442
idx
static uint32_t idx(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5925
cpp_helpers.h
google::protobuf::compiler::SCC::GetRepresentative
const Descriptor * GetRepresentative() const
Definition: scc.h:53
google::protobuf::compiler::cpp::FileGenerator::GeneratePBHeader
void GeneratePBHeader(io::Printer *printer, const std::string &info_path)
Definition: cpp_file.cc:283
google::protobuf::compiler::cpp::FileGenerator::GetSCC
const SCC * GetSCC(const Descriptor *d)
Definition: cpp_file.h:170
google::protobuf::compiler::cpp::FileGenerator::GenerateMessageDefinitions
void GenerateMessageDefinitions(io::Printer *printer)
Definition: cpp_file.cc:1387
google::protobuf::compiler::cpp::FileGenerator::GenerateMetadataPragma
void GenerateMetadataPragma(io::Printer *printer, const std::string &info_path)
Definition: cpp_file.cc:1320
google::protobuf::compiler::cpp::FileGenerator::GenerateGlobalStateFunctionDeclarations
void GenerateGlobalStateFunctionDeclarations(io::Printer *printer)
Definition: cpp_file.cc:1352
google::protobuf::compiler::cpp::FileGenerator::file_
const FileDescriptor * file_
Definition: cpp_file.h:183
google::protobuf::compiler::cpp::FileGenerator::FileGenerator
FileGenerator(const FileDescriptor *file, const Options &options)
Definition: cpp_file.cc:94
google::protobuf::compiler::cpp::FileGenerator::extension_generators_
std::vector< std::unique_ptr< ExtensionGenerator > > extension_generators_
Definition: cpp_file.h:195
google::protobuf::compiler::cpp::FileGenerator::options_
const Options options_
Definition: cpp_file.h:184
google::protobuf::compiler::cpp::FileGenerator::GenerateBottomHeaderGuard
void GenerateBottomHeaderGuard(io::Printer *printer, bool pb_h)
Definition: cpp_file.cc:1205
google::protobuf::compiler::cpp::FileGenerator::GetSCCRepresentative
const Descriptor * GetSCCRepresentative(const Descriptor *d)
Definition: cpp_file.h:167
google::protobuf::compiler::cpp::FileGenerator::CreateHeaderInclude
std::string CreateHeaderInclude(const std::string &basename, const FileDescriptor *file)
Definition: cpp_file.cc:357
google::protobuf::compiler::cpp::FileGenerator::GenerateInitForSCC
void GenerateInitForSCC(const SCC *scc, io::Printer *printer)
Definition: cpp_file.cc:909
google::protobuf::compiler::cpp::FileGenerator::GenerateInlineFunctionDefinitions
void GenerateInlineFunctionDefinitions(io::Printer *printer)
Definition: cpp_file.cc:1435
google::protobuf::compiler::cpp::FileGenerator::sccs_
std::vector< const SCC * > sccs_
Definition: cpp_file.h:181
google::protobuf::compiler::cpp::FileGenerator::IsDepWeak
bool IsDepWeak(const FileDescriptor *dep) const
Definition: cpp_file.h:172
google::protobuf::compiler::cpp::FileGenerator::variables_
std::map< std::string, std::string > variables_
Definition: cpp_file.h:188
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
d
d
google::protobuf::compiler::cpp::Options
Definition: cpp_options.h:52
google::protobuf::compiler::cpp::FileGenerator::GenerateTopHeaderGuard
void GenerateTopHeaderGuard(io::Printer *printer, bool pb_h)
Definition: cpp_file.cc:1185
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::cpp::Options::opensource_runtime
bool opensource_runtime
Definition: cpp_options.h:63
google::protobuf::compiler::cpp::FileGenerator
Definition: cpp_file.h:68
common.h
google::protobuf::compiler::cpp::MessageSCCAnalyzer::GetSCC
const SCC * GetSCC(const Descriptor *descriptor)
Definition: cpp_helpers.h:534
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: cpp_helpers.h:524
google::protobuf::compiler::cpp::FileGenerator::GenerateMacroUndefs
void GenerateMacroUndefs(io::Printer *printer)
Definition: cpp_file.cc:151
google::protobuf::compiler::cpp::FileGenerator::GetCrossFileReferencesForField
void GetCrossFileReferencesForField(const FieldDescriptor *field, CrossFileReferences *refs)
Definition: cpp_file.cc:471
google::protobuf::compiler::cpp::FileGenerator::GenerateGlobalSource
void GenerateGlobalSource(io::Printer *printer)
Definition: cpp_file.cc:592
google::protobuf::compiler::cpp::FileGenerator::GenerateProtoHeader
void GenerateProtoHeader(io::Printer *printer, const std::string &info_path)
Definition: cpp_file.cc:245
google::protobuf::compiler::cpp::FileGenerator::service_generators_
std::vector< std::unique_ptr< ServiceGenerator > > service_generators_
Definition: cpp_file.h:194
google::protobuf::compiler::cpp::FileGenerator::GenerateReflectionInitializationCode
void GenerateReflectionInitializationCode(io::Printer *printer)
Definition: cpp_file.cc:716
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceForMessage
void GenerateSourceForMessage(int idx, io::Printer *printer)
Definition: cpp_file.cc:542
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::cpp::FileGenerator::~FileGenerator
~FileGenerator()
google::protobuf::compiler::cpp::FileGenerator::GenerateForwardDeclarations
void GenerateForwardDeclarations(io::Printer *printer)
Definition: cpp_file.cc:1134
google::protobuf::compiler::cpp::FileGenerator::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator)
google::protobuf::compiler::cpp::FileGenerator::IsSCCRepresentative
bool IsSCCRepresentative(const Descriptor *d)
Definition: cpp_file.h:164
google::protobuf::compiler::cpp::FileGenerator::GenerateEnumDefinitions
void GenerateEnumDefinitions(io::Printer *printer)
Definition: cpp_file.cc:1400
cpp_options.h
cpp_field.h
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceIncludes
void GenerateSourceIncludes(io::Printer *printer)
Definition: cpp_file.cc:381
google::protobuf::compiler::cpp::FileGenerator::GenerateInternalForwardDeclarations
void GenerateInternalForwardDeclarations(const CrossFileReferences &refs, io::Printer *printer)
Definition: cpp_file.cc:507
google::protobuf::compiler::cpp::FileGenerator::GetCrossFileReferencesForFile
void GetCrossFileReferencesForFile(const FileDescriptor *file, CrossFileReferences *refs)
Definition: cpp_file.cc:488
google::protobuf::compiler::cpp::FileGenerator::GenerateLibraryIncludes
void GenerateLibraryIncludes(io::Printer *printer)
Definition: cpp_file.cc:1211
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::FileGenerator::enum_generators_
std::vector< std::unique_ptr< EnumGenerator > > enum_generators_
Definition: cpp_file.h:193
google::protobuf::compiler::cpp::FileGenerator::NumMessages
int NumMessages() const
Definition: cpp_file.h:85
google::protobuf::compiler::cpp::FileGenerator::IncludeFileAndExport
void IncludeFileAndExport(const std::string &google3_name, io::Printer *printer)
Definition: cpp_file.h:98


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49