• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/compiler/cpp/cpp_field.h>
45 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
46 #include <google/protobuf/compiler/cpp/cpp_options.h>
47 #include <google/protobuf/compiler/scc.h>
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 
68 class FileGenerator {
69  public:
70   // See generator.cc for the meaning of dllexport_decl.
71   FileGenerator(const FileDescriptor* file, const Options& options);
72   ~FileGenerator();
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 
NumMessages()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 
IncludeFile(const std::string & google3_name,io::Printer * printer)95   void IncludeFile(const std::string& google3_name, io::Printer* printer) {
96     DoIncludeFile(google3_name, false, printer);
97   }
IncludeFileAndExport(const std::string & google3_name,io::Printer * printer)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 
105   std::string CreateHeaderInclude(const std::string& basename,
106                                   const FileDescriptor* file);
107   void GetCrossFileReferencesForField(const FieldDescriptor* field,
108                                       CrossFileReferences* refs);
109   void GetCrossFileReferencesForFile(const FileDescriptor* file,
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, const CrossFileReferences& refs,
117                           io::Printer* printer);
118   void GenerateTables(io::Printer* printer);
119   void GenerateReflectionInitializationCode(io::Printer* printer);
120 
121   // For other imports, generates their forward-declarations.
122   void GenerateForwardDeclarations(io::Printer* printer);
123 
124   // Generates top or bottom of a header file.
125   void GenerateTopHeaderGuard(io::Printer* printer, bool pb_h);
126   void GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h);
127 
128   // Generates #include directives.
129   void GenerateLibraryIncludes(io::Printer* printer);
130   void GenerateDependencyIncludes(io::Printer* printer);
131 
132   // Generate a pragma to pull in metadata using the given info_path (if
133   // non-empty). info_path should be relative to printer's output.
134   void GenerateMetadataPragma(io::Printer* printer,
135                               const std::string& info_path);
136 
137   // Generates a couple of different pieces before definitions:
138   void GenerateGlobalStateFunctionDeclarations(io::Printer* printer);
139 
140   // Generates types for classes.
141   void GenerateMessageDefinitions(io::Printer* printer);
142 
143   void GenerateEnumDefinitions(io::Printer* printer);
144 
145   // Generates generic service definitions.
146   void GenerateServiceDefinitions(io::Printer* printer);
147 
148   // Generates extension identifiers.
149   void GenerateExtensionIdentifiers(io::Printer* printer);
150 
151   // Generates inline function definitions.
152   void GenerateInlineFunctionDefinitions(io::Printer* printer);
153 
154   void GenerateProto2NamespaceEnumSpecializations(io::Printer* printer);
155 
156   // Sometimes the names we use in a .proto file happen to be defined as
157   // macros on some platforms (e.g., macro/minor used in plugin.proto are
158   // defined as macros in sys/types.h on FreeBSD and a few other platforms).
159   // To make the generated code compile on these platforms, we either have to
160   // undef the macro for these few platforms, or rename the field name for all
161   // platforms. Since these names are part of protobuf public API, renaming is
162   // generally a breaking change so we prefer the #undef approach.
163   void GenerateMacroUndefs(io::Printer* printer);
164 
IsSCCRepresentative(const Descriptor * d)165   bool IsSCCRepresentative(const Descriptor* d) {
166     return GetSCCRepresentative(d) == d;
167   }
GetSCCRepresentative(const Descriptor * d)168   const Descriptor* GetSCCRepresentative(const Descriptor* d) {
169     return GetSCC(d)->GetRepresentative();
170   }
GetSCC(const Descriptor * d)171   const SCC* GetSCC(const Descriptor* d) { return scc_analyzer_.GetSCC(d); }
172 
IsDepWeak(const FileDescriptor * dep)173   bool IsDepWeak(const FileDescriptor* dep) const {
174     if (weak_deps_.count(dep) != 0) {
175       GOOGLE_CHECK(!options_.opensource_runtime);
176       return true;
177     }
178     return false;
179   }
180 
181   std::set<const FileDescriptor*> weak_deps_;
182   std::vector<const SCC*> sccs_;
183 
184   const FileDescriptor* file_;
185   const Options options_;
186 
187   MessageSCCAnalyzer scc_analyzer_;
188 
189   std::map<std::string, std::string> variables_;
190 
191   // Contains the post-order walk of all the messages (and child messages) in
192   // this file. If you need a pre-order walk just reverse iterate.
193   std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
194   std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
195   std::vector<std::unique_ptr<ServiceGenerator>> service_generators_;
196   std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
197 
198   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
199 };
200 
201 }  // namespace cpp
202 }  // namespace compiler
203 }  // namespace protobuf
204 }  // namespace google
205 
206 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
207