blob: 8e19a3f0557379a75b16ec7dcde5ece6b04b8793 [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_MESSAGE_H__
36#define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_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
Feng Xiaoeee38b02015-08-22 18:25:48 -070042#include <set>
temporal40ee5512008-07-10 02:12:20 +000043#include <string>
temporal40ee5512008-07-10 02:12:20 +000044#include <google/protobuf/compiler/cpp/cpp_field.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000045#include <google/protobuf/compiler/cpp/cpp_options.h>
temporal40ee5512008-07-10 02:12:20 +000046
47namespace google {
48namespace protobuf {
49 namespace io {
50 class Printer; // printer.h
51 }
52}
53
54namespace protobuf {
55namespace compiler {
56namespace cpp {
57
58class EnumGenerator; // enum.h
59class ExtensionGenerator; // extension.h
60
61class MessageGenerator {
62 public:
63 // See generator.cc for the meaning of dllexport_decl.
Feng Xiao6ef984a2014-11-10 17:34:54 -080064 MessageGenerator(const Descriptor* descriptor, const Options& options);
temporal40ee5512008-07-10 02:12:20 +000065 ~MessageGenerator();
66
67 // Header stuff.
68
Feng Xiaoeee38b02015-08-22 18:25:48 -070069 // Return names for foward declarations of this class and all its nested
70 // types.
71 void FillMessageForwardDeclarations(set<string>* class_names);
72 void FillEnumForwardDeclarations(set<string>* enum_names);
temporal40ee5512008-07-10 02:12:20 +000073
74 // Generate definitions of all nested enums (must come before class
75 // definitions because those classes use the enums definitions).
76 void GenerateEnumDefinitions(io::Printer* printer);
77
kenton@google.com80b1d622009-07-29 01:13:20 +000078 // Generate specializations of GetEnumDescriptor<MyEnum>().
79 // Precondition: in ::google::protobuf namespace.
80 void GenerateGetEnumDescriptorSpecializations(io::Printer* printer);
81
temporal40ee5512008-07-10 02:12:20 +000082 // Generate definitions for this class and all its nested types.
83 void GenerateClassDefinition(io::Printer* printer);
84
85 // Generate definitions of inline methods (placed at the end of the header
86 // file).
Jisi Liu885b6122015-02-28 14:51:22 -080087 void GenerateInlineMethods(io::Printer* printer, bool is_inline);
temporal40ee5512008-07-10 02:12:20 +000088
Bo Yang5db21732015-05-21 14:28:59 -070089 // Dependent methods are always inline.
90 void GenerateDependentInlineMethods(io::Printer* printer);
91
temporal40ee5512008-07-10 02:12:20 +000092 // Source file stuff.
93
94 // Generate code which declares all the global descriptor pointers which
95 // will be initialized by the methods below.
96 void GenerateDescriptorDeclarations(io::Printer* printer);
97
98 // Generate code that initializes the global variable storing the message's
99 // descriptor.
100 void GenerateDescriptorInitializer(io::Printer* printer, int index);
101
kenton@google.comd37d46d2009-04-25 02:53:47 +0000102 // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage()
103 // for all types.
104 void GenerateTypeRegistrations(io::Printer* printer);
105
106 // Generates code that allocates the message's default instance.
107 void GenerateDefaultInstanceAllocator(io::Printer* printer);
108
109 // Generates code that initializes the message's default instance. This
110 // is separate from allocating because all default instances must be
111 // allocated before any can be initialized.
kenton@google.com24bf56f2008-09-24 20:31:01 +0000112 void GenerateDefaultInstanceInitializer(io::Printer* printer);
113
kenton@google.com63e646b2009-05-06 19:27:03 +0000114 // Generates code that should be run when ShutdownProtobufLibrary() is called,
115 // to delete all dynamically-allocated objects.
116 void GenerateShutdownCode(io::Printer* printer);
117
temporal40ee5512008-07-10 02:12:20 +0000118 // Generate all non-inline methods for this class.
119 void GenerateClassMethods(io::Printer* printer);
120
121 private:
122 // Generate declarations and definitions of accessors for fields.
Bo Yang5db21732015-05-21 14:28:59 -0700123 void GenerateDependentBaseClassDefinition(io::Printer* printer);
124 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer);
temporal40ee5512008-07-10 02:12:20 +0000125 void GenerateFieldAccessorDeclarations(io::Printer* printer);
Bo Yang5db21732015-05-21 14:28:59 -0700126 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer);
Jisi Liu885b6122015-02-28 14:51:22 -0800127 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline);
temporal40ee5512008-07-10 02:12:20 +0000128
129 // Generate the field offsets array.
130 void GenerateOffsets(io::Printer* printer);
131
132 // Generate constructors and destructor.
133 void GenerateStructors(io::Printer* printer);
134
kenton@google.comd37d46d2009-04-25 02:53:47 +0000135 // The compiler typically generates multiple copies of each constructor and
136 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx
137 // Placing common code in a separate method reduces the generated code size.
138 //
139 // Generate the shared constructor code.
140 void GenerateSharedConstructorCode(io::Printer* printer);
141 // Generate the shared destructor code.
142 void GenerateSharedDestructorCode(io::Printer* printer);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800143 // Generate the arena-specific destructor code.
144 void GenerateArenaDestructorCode(io::Printer* printer);
kenton@google.comd37d46d2009-04-25 02:53:47 +0000145
temporal40ee5512008-07-10 02:12:20 +0000146 // Generate standard Message methods.
147 void GenerateClear(io::Printer* printer);
jieluo@google.com4de8f552014-07-18 00:47:59 +0000148 void GenerateOneofClear(io::Printer* printer);
temporal40ee5512008-07-10 02:12:20 +0000149 void GenerateMergeFromCodedStream(io::Printer* printer);
150 void GenerateSerializeWithCachedSizes(io::Printer* printer);
kenton@google.comd37d46d2009-04-25 02:53:47 +0000151 void GenerateSerializeWithCachedSizesToArray(io::Printer* printer);
152 void GenerateSerializeWithCachedSizesBody(io::Printer* printer,
153 bool to_array);
temporal40ee5512008-07-10 02:12:20 +0000154 void GenerateByteSize(io::Printer* printer);
155 void GenerateMergeFrom(io::Printer* printer);
156 void GenerateCopyFrom(io::Printer* printer);
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000157 void GenerateSwap(io::Printer* printer);
temporal40ee5512008-07-10 02:12:20 +0000158 void GenerateIsInitialized(io::Printer* printer);
159
160 // Helpers for GenerateSerializeWithCachedSizes().
161 void GenerateSerializeOneField(io::Printer* printer,
kenton@google.comd37d46d2009-04-25 02:53:47 +0000162 const FieldDescriptor* field,
163 bool unbounded);
temporal40ee5512008-07-10 02:12:20 +0000164 void GenerateSerializeOneExtensionRange(
kenton@google.comd37d46d2009-04-25 02:53:47 +0000165 io::Printer* printer, const Descriptor::ExtensionRange* range,
166 bool unbounded);
temporal40ee5512008-07-10 02:12:20 +0000167
kenton@google.comfccb1462009-12-18 02:11:36 +0000168
Bo Yang5db21732015-05-21 14:28:59 -0700169 // Generates has_foo() functions and variables for singular field has-bits.
170 void GenerateSingularFieldHasBits(const FieldDescriptor* field,
171 map<string, string> vars,
172 io::Printer* printer);
173 // Generates has_foo() functions and variables for oneof field has-bits.
174 void GenerateOneofHasBits(io::Printer* printer, bool is_inline);
175 // Generates has_foo_bar() functions for oneof members.
176 void GenerateOneofMemberHasBits(const FieldDescriptor* field,
177 const map<string, string>& vars,
178 io::Printer* printer);
179 // Generates the clear_foo() method for a field.
180 void GenerateFieldClear(const FieldDescriptor* field,
181 const map<string, string>& vars,
182 io::Printer* printer);
183
temporal40ee5512008-07-10 02:12:20 +0000184 const Descriptor* descriptor_;
185 string classname_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000186 Options options_;
temporal40ee5512008-07-10 02:12:20 +0000187 FieldGeneratorMap field_generators_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000188 vector< vector<string> > runs_of_fields_; // that might be trivially cleared
Feng Xiaof157a562014-11-14 11:50:31 -0800189 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > nested_generators_;
190 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_;
191 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800192 int num_required_fields_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000193 bool uses_string_;
Bo Yang5db21732015-05-21 14:28:59 -0700194 bool use_dependent_base_;
temporal40ee5512008-07-10 02:12:20 +0000195
196 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
197};
198
199} // namespace cpp
200} // namespace compiler
201} // namespace protobuf
202
203} // namespace google
204#endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__