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_MESSAGE_H__ |
| 36 | #define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_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 |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 42 | #include <set> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 43 | #include <string> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | #include <google/protobuf/compiler/cpp/cpp_field.h> |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 45 | #include <google/protobuf/compiler/cpp/cpp_options.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | |
| 47 | namespace google { |
| 48 | namespace protobuf { |
| 49 | namespace io { |
| 50 | class Printer; // printer.h |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | namespace protobuf { |
| 55 | namespace compiler { |
| 56 | namespace cpp { |
| 57 | |
| 58 | class EnumGenerator; // enum.h |
| 59 | class ExtensionGenerator; // extension.h |
| 60 | |
| 61 | class MessageGenerator { |
| 62 | public: |
| 63 | // See generator.cc for the meaning of dllexport_decl. |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 64 | MessageGenerator(const Descriptor* descriptor, const Options& options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 65 | ~MessageGenerator(); |
| 66 | |
| 67 | // Header stuff. |
| 68 | |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 69 | // 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); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 73 | |
| 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.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 78 | // Generate specializations of GetEnumDescriptor<MyEnum>(). |
| 79 | // Precondition: in ::google::protobuf namespace. |
| 80 | void GenerateGetEnumDescriptorSpecializations(io::Printer* printer); |
| 81 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 82 | // 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 Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame] | 87 | void GenerateInlineMethods(io::Printer* printer, bool is_inline); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 88 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 89 | // Dependent methods are always inline. |
| 90 | void GenerateDependentInlineMethods(io::Printer* printer); |
| 91 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 92 | // 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.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 102 | // 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.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 112 | void GenerateDefaultInstanceInitializer(io::Printer* printer); |
| 113 | |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 114 | // Generates code that should be run when ShutdownProtobufLibrary() is called, |
| 115 | // to delete all dynamically-allocated objects. |
| 116 | void GenerateShutdownCode(io::Printer* printer); |
| 117 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 118 | // 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 Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 123 | void GenerateDependentBaseClassDefinition(io::Printer* printer); |
| 124 | void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 125 | void GenerateFieldAccessorDeclarations(io::Printer* printer); |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 126 | void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame] | 127 | void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 128 | |
| 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.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 135 | // 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 Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 143 | // Generate the arena-specific destructor code. |
| 144 | void GenerateArenaDestructorCode(io::Printer* printer); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 145 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 146 | // Generate standard Message methods. |
| 147 | void GenerateClear(io::Printer* printer); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 148 | void GenerateOneofClear(io::Printer* printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 149 | void GenerateMergeFromCodedStream(io::Printer* printer); |
| 150 | void GenerateSerializeWithCachedSizes(io::Printer* printer); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 151 | void GenerateSerializeWithCachedSizesToArray(io::Printer* printer); |
| 152 | void GenerateSerializeWithCachedSizesBody(io::Printer* printer, |
| 153 | bool to_array); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 154 | void GenerateByteSize(io::Printer* printer); |
| 155 | void GenerateMergeFrom(io::Printer* printer); |
| 156 | void GenerateCopyFrom(io::Printer* printer); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 157 | void GenerateSwap(io::Printer* printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 158 | void GenerateIsInitialized(io::Printer* printer); |
| 159 | |
| 160 | // Helpers for GenerateSerializeWithCachedSizes(). |
| 161 | void GenerateSerializeOneField(io::Printer* printer, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 162 | const FieldDescriptor* field, |
| 163 | bool unbounded); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 164 | void GenerateSerializeOneExtensionRange( |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 165 | io::Printer* printer, const Descriptor::ExtensionRange* range, |
| 166 | bool unbounded); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 167 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 168 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 169 | // 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 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 184 | const Descriptor* descriptor_; |
| 185 | string classname_; |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 186 | Options options_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 187 | FieldGeneratorMap field_generators_; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 188 | vector< vector<string> > runs_of_fields_; // that might be trivially cleared |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 189 | 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 Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 192 | int num_required_fields_; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 193 | bool uses_string_; |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 194 | bool use_dependent_base_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 195 | |
| 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__ |