blob: ebe990c2778e53b1bd642cfe398b6694559acfaa [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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.com4de8f552014-07-18 00:47:59 +000038#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080039#ifndef _SHARED_PTR_H
40#include <google/protobuf/stubs/shared_ptr.h>
41#endif
temporal40ee5512008-07-10 02:12:20 +000042#include <string>
43#include <vector>
44#include <google/protobuf/stubs/common.h>
45#include <google/protobuf/compiler/cpp/cpp_field.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000046#include <google/protobuf/compiler/cpp/cpp_options.h>
temporal40ee5512008-07-10 02:12:20 +000047
48namespace google {
49namespace protobuf {
50 class FileDescriptor; // descriptor.h
51 namespace io {
52 class Printer; // printer.h
53 }
54}
55
56namespace protobuf {
57namespace compiler {
58namespace cpp {
59
60class EnumGenerator; // enum.h
61class MessageGenerator; // message.h
62class ServiceGenerator; // service.h
63class ExtensionGenerator; // extension.h
64
65class FileGenerator {
66 public:
67 // See generator.cc for the meaning of dllexport_decl.
68 explicit FileGenerator(const FileDescriptor* file,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000069 const Options& options);
temporal40ee5512008-07-10 02:12:20 +000070 ~FileGenerator();
71
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070072 // 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);
temporal40ee5512008-07-10 02:12:20 +000080 void GenerateSource(io::Printer* printer);
81
82 private:
Feng Xiaoeee38b02015-08-22 18:25:48 -070083 // Internal type used by GenerateForwardDeclarations (defined in file.cc).
84 class ForwardDeclarations;
85
temporal40ee5512008-07-10 02:12:20 +000086 // 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 Xiaoeee38b02015-08-22 18:25:48 -070093 // 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 Yang5db21732015-05-21 14:28:59 -0700101 // Generates top or bottom of a header file.
Feng Xiaoeee38b02015-08-22 18:25:48 -0700102 void GenerateTopHeaderGuard(io::Printer* printer,
103 const string& filename_identifier);
104 void GenerateBottomHeaderGuard(io::Printer* printer,
105 const string& filename_identifier);
Bo Yang5db21732015-05-21 14:28:59 -0700106
107 // Generates #include directives.
108 void GenerateLibraryIncludes(io::Printer* printer);
109 void GenerateDependencyIncludes(io::Printer* printer);
110
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700111 // 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 Yang5db21732015-05-21 14:28:59 -0700115 // Generates a couple of different pieces before definitions:
116 void GenerateGlobalStateFunctionDeclarations(io::Printer* printer);
117
118 // Generates types for classes.
Bo Yang5db21732015-05-21 14:28:59 -0700119 void GenerateMessageDefinitions(io::Printer* printer);
120
Feng Xiaoeee38b02015-08-22 18:25:48 -0700121 // 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 Yang5db21732015-05-21 14:28:59 -0700130 // Generates enum definitions.
Feng Xiaoeee38b02015-08-22 18:25:48 -0700131 void GenerateEnumForwardDeclarations(io::Printer* printer);
132 void FillEnumForwardDeclarations(ForwardDeclarations* decls);
Bo Yang5db21732015-05-21 14:28:59 -0700133 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
temporal40ee5512008-07-10 02:12:20 +0000146 const FileDescriptor* file_;
147
Feng Xiaof157a562014-11-14 11:50:31 -0800148 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_;
temporal40ee5512008-07-10 02:12:20 +0000152
153 // E.g. if the package is foo.bar, package_parts_ is {"foo", "bar"}.
154 vector<string> package_parts_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000155 const Options options_;
kenton@google.com9b10f582008-09-30 00:09:40 +0000156
temporal40ee5512008-07-10 02:12:20 +0000157 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__