temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 38 | #include <memory> |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 39 | #ifndef _SHARED_PTR_H |
| 40 | #include <google/protobuf/stubs/shared_ptr.h> |
| 41 | #endif |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 42 | #include <string> |
| 43 | #include <vector> |
| 44 | #include <google/protobuf/stubs/common.h> |
| 45 | #include <google/protobuf/compiler/cpp/cpp_field.h> |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 46 | #include <google/protobuf/compiler/cpp/cpp_options.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | |
| 48 | namespace google { |
| 49 | namespace protobuf { |
| 50 | class FileDescriptor; // descriptor.h |
| 51 | namespace io { |
| 52 | class Printer; // printer.h |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | namespace protobuf { |
| 57 | namespace compiler { |
| 58 | namespace cpp { |
| 59 | |
| 60 | class EnumGenerator; // enum.h |
| 61 | class MessageGenerator; // message.h |
| 62 | class ServiceGenerator; // service.h |
| 63 | class ExtensionGenerator; // extension.h |
| 64 | |
| 65 | class FileGenerator { |
| 66 | public: |
| 67 | // See generator.cc for the meaning of dllexport_decl. |
| 68 | explicit FileGenerator(const FileDescriptor* file, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 69 | const Options& options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 70 | ~FileGenerator(); |
| 71 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 72 | // info_path, if non-empty, should be the path (relative to printer's output) |
| 73 | // to the metadata file describing this proto header. |
| 74 | void GenerateProtoHeader(io::Printer* printer, |
| 75 | const string& info_path); |
| 76 | // info_path, if non-empty, should be the path (relative to printer's output) |
| 77 | // to the metadata file describing this PB header. |
| 78 | void GeneratePBHeader(io::Printer* printer, |
| 79 | const string& info_path); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 80 | void GenerateSource(io::Printer* printer); |
| 81 | |
| 82 | private: |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 83 | // Internal type used by GenerateForwardDeclarations (defined in file.cc). |
| 84 | class ForwardDeclarations; |
| 85 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 86 | // Generate the BuildDescriptors() procedure, which builds all descriptors |
| 87 | // for types defined in the file. |
| 88 | void GenerateBuildDescriptors(io::Printer* printer); |
| 89 | |
| 90 | void GenerateNamespaceOpeners(io::Printer* printer); |
| 91 | void GenerateNamespaceClosers(io::Printer* printer); |
| 92 | |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 93 | // For other imports, generates their forward-declarations. |
| 94 | void GenerateForwardDeclarations(io::Printer* printer); |
| 95 | |
| 96 | // Internal helper used by GenerateForwardDeclarations: fills 'decls' |
| 97 | // with all necessary forward-declarations for this file and its |
| 98 | // transient depednencies. |
| 99 | void FillForwardDeclarations(ForwardDeclarations* decls); |
| 100 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 101 | // Generates top or bottom of a header file. |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 102 | void GenerateTopHeaderGuard(io::Printer* printer, |
| 103 | const string& filename_identifier); |
| 104 | void GenerateBottomHeaderGuard(io::Printer* printer, |
| 105 | const string& filename_identifier); |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 106 | |
| 107 | // Generates #include directives. |
| 108 | void GenerateLibraryIncludes(io::Printer* printer); |
| 109 | void GenerateDependencyIncludes(io::Printer* printer); |
| 110 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 111 | // Generate a pragma to pull in metadata using the given info_path (if |
| 112 | // non-empty). info_path should be relative to printer's output. |
| 113 | void GenerateMetadataPragma(io::Printer* printer, const string& info_path); |
| 114 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 115 | // Generates a couple of different pieces before definitions: |
| 116 | void GenerateGlobalStateFunctionDeclarations(io::Printer* printer); |
| 117 | |
| 118 | // Generates types for classes. |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 119 | void GenerateMessageDefinitions(io::Printer* printer); |
| 120 | |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 121 | // Generates forward-declarations for just this file's classes. This is |
| 122 | // used for .pb.h headers, but not in proto_h mode. |
| 123 | void GenerateMessageForwardDeclarations(io::Printer* printer); |
| 124 | |
| 125 | // Fills in types for forward declarations. This is used internally, and |
| 126 | // also by other FileGenerators to determine imports' declarations. |
| 127 | void FillMessageForwardDeclarations(ForwardDeclarations* decls); |
| 128 | void FillMessageDefinitions(ForwardDeclarations* decls); |
| 129 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 130 | // Generates enum definitions. |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 131 | void GenerateEnumForwardDeclarations(io::Printer* printer); |
| 132 | void FillEnumForwardDeclarations(ForwardDeclarations* decls); |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 133 | void GenerateEnumDefinitions(io::Printer* printer); |
| 134 | |
| 135 | // Generates generic service definitions. |
| 136 | void GenerateServiceDefinitions(io::Printer* printer); |
| 137 | |
| 138 | // Generates extension identifiers. |
| 139 | void GenerateExtensionIdentifiers(io::Printer* printer); |
| 140 | |
| 141 | // Generates inline function defintions. |
| 142 | void GenerateInlineFunctionDefinitions(io::Printer* printer); |
| 143 | |
| 144 | void GenerateProto2NamespaceEnumSpecializations(io::Printer* printer); |
| 145 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 146 | const FileDescriptor* file_; |
| 147 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 148 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_; |
| 149 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_; |
| 150 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<ServiceGenerator> > service_generators_; |
| 151 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 152 | |
| 153 | // E.g. if the package is foo.bar, package_parts_ is {"foo", "bar"}. |
| 154 | vector<string> package_parts_; |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 155 | const Options options_; |
kenton@google.com | 9b10f58 | 2008-09-30 00:09:40 +0000 | [diff] [blame] | 156 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 157 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator); |
| 158 | }; |
| 159 | |
| 160 | } // namespace cpp |
| 161 | } // namespace compiler |
| 162 | } // namespace protobuf |
| 163 | |
| 164 | } // namespace google |
| 165 | #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__ |