blob: 29cdaea53d25bb4ca5140c730d9afc01d86fbc13 [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
Feng Xiaoeee38b02015-08-22 18:25:48 -070072 void GenerateProtoHeader(io::Printer* printer);
73 void GeneratePBHeader(io::Printer* printer);
temporal40ee5512008-07-10 02:12:20 +000074 void GenerateSource(io::Printer* printer);
75
76 private:
Feng Xiaoeee38b02015-08-22 18:25:48 -070077 // Internal type used by GenerateForwardDeclarations (defined in file.cc).
78 class ForwardDeclarations;
79
temporal40ee5512008-07-10 02:12:20 +000080 // Generate the BuildDescriptors() procedure, which builds all descriptors
81 // for types defined in the file.
82 void GenerateBuildDescriptors(io::Printer* printer);
83
84 void GenerateNamespaceOpeners(io::Printer* printer);
85 void GenerateNamespaceClosers(io::Printer* printer);
86
Feng Xiaoeee38b02015-08-22 18:25:48 -070087 // For other imports, generates their forward-declarations.
88 void GenerateForwardDeclarations(io::Printer* printer);
89
90 // Internal helper used by GenerateForwardDeclarations: fills 'decls'
91 // with all necessary forward-declarations for this file and its
92 // transient depednencies.
93 void FillForwardDeclarations(ForwardDeclarations* decls);
94
Bo Yang5db21732015-05-21 14:28:59 -070095 // Generates top or bottom of a header file.
Feng Xiaoeee38b02015-08-22 18:25:48 -070096 void GenerateTopHeaderGuard(io::Printer* printer,
97 const string& filename_identifier);
98 void GenerateBottomHeaderGuard(io::Printer* printer,
99 const string& filename_identifier);
Bo Yang5db21732015-05-21 14:28:59 -0700100
101 // Generates #include directives.
102 void GenerateLibraryIncludes(io::Printer* printer);
103 void GenerateDependencyIncludes(io::Printer* printer);
104
105 // Generates a couple of different pieces before definitions:
106 void GenerateGlobalStateFunctionDeclarations(io::Printer* printer);
107
108 // Generates types for classes.
Bo Yang5db21732015-05-21 14:28:59 -0700109 void GenerateMessageDefinitions(io::Printer* printer);
110
Feng Xiaoeee38b02015-08-22 18:25:48 -0700111 // Generates forward-declarations for just this file's classes. This is
112 // used for .pb.h headers, but not in proto_h mode.
113 void GenerateMessageForwardDeclarations(io::Printer* printer);
114
115 // Fills in types for forward declarations. This is used internally, and
116 // also by other FileGenerators to determine imports' declarations.
117 void FillMessageForwardDeclarations(ForwardDeclarations* decls);
118 void FillMessageDefinitions(ForwardDeclarations* decls);
119
Bo Yang5db21732015-05-21 14:28:59 -0700120 // Generates enum definitions.
Feng Xiaoeee38b02015-08-22 18:25:48 -0700121 void GenerateEnumForwardDeclarations(io::Printer* printer);
122 void FillEnumForwardDeclarations(ForwardDeclarations* decls);
Bo Yang5db21732015-05-21 14:28:59 -0700123 void GenerateEnumDefinitions(io::Printer* printer);
124
125 // Generates generic service definitions.
126 void GenerateServiceDefinitions(io::Printer* printer);
127
128 // Generates extension identifiers.
129 void GenerateExtensionIdentifiers(io::Printer* printer);
130
131 // Generates inline function defintions.
132 void GenerateInlineFunctionDefinitions(io::Printer* printer);
133
134 void GenerateProto2NamespaceEnumSpecializations(io::Printer* printer);
135
temporal40ee5512008-07-10 02:12:20 +0000136 const FileDescriptor* file_;
137
Feng Xiaof157a562014-11-14 11:50:31 -0800138 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_;
139 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_;
140 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ServiceGenerator> > service_generators_;
141 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
temporal40ee5512008-07-10 02:12:20 +0000142
143 // E.g. if the package is foo.bar, package_parts_ is {"foo", "bar"}.
144 vector<string> package_parts_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000145 const Options options_;
kenton@google.com9b10f582008-09-30 00:09:40 +0000146
temporal40ee5512008-07-10 02:12:20 +0000147 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
148};
149
150} // namespace cpp
151} // namespace compiler
152} // namespace protobuf
153
154} // namespace google
155#endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__