1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 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. 18 // 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. 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 38 #include <memory> 39 #ifndef _SHARED_PTR_H 40 #include <google/protobuf/stubs/shared_ptr.h> 41 #endif 42 #include <set> 43 #include <string> 44 #include <google/protobuf/compiler/cpp/cpp_field.h> 45 #include <google/protobuf/compiler/cpp/cpp_options.h> 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. 64 MessageGenerator(const Descriptor* descriptor, const Options& options); 65 ~MessageGenerator(); 66 67 // Header stuff. 68 69 // Return names for foward declarations of this class and all its nested 70 // types. A given key in {class,enum}_names will map from a class name to the 71 // descriptor that was responsible for its inclusion in the map. This can be 72 // used to associate the descriptor with the code generated for it. 73 void FillMessageForwardDeclarations( 74 map<string, const Descriptor*>* class_names); 75 void FillEnumForwardDeclarations( 76 map<string, const EnumDescriptor*>* enum_names); 77 78 // Generate definitions of all nested enums (must come before class 79 // definitions because those classes use the enums definitions). 80 void GenerateEnumDefinitions(io::Printer* printer); 81 82 // Generate specializations of GetEnumDescriptor<MyEnum>(). 83 // Precondition: in ::google::protobuf namespace. 84 void GenerateGetEnumDescriptorSpecializations(io::Printer* printer); 85 86 // Generate definitions for this class and all its nested types. 87 void GenerateClassDefinition(io::Printer* printer); 88 89 // Generate definitions of inline methods (placed at the end of the header 90 // file). 91 void GenerateInlineMethods(io::Printer* printer, bool is_inline); 92 93 // Dependent methods are always inline. 94 void GenerateDependentInlineMethods(io::Printer* printer); 95 96 // Source file stuff. 97 98 // Generate code which declares all the global descriptor pointers which 99 // will be initialized by the methods below. 100 void GenerateDescriptorDeclarations(io::Printer* printer); 101 102 // Generate code that initializes the global variable storing the message's 103 // descriptor. 104 void GenerateDescriptorInitializer(io::Printer* printer, int index); 105 106 // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage() 107 // for all types. 108 void GenerateTypeRegistrations(io::Printer* printer); 109 110 // Generates code that allocates the message's default instance. 111 void GenerateDefaultInstanceAllocator(io::Printer* printer); 112 113 // Generates code that initializes the message's default instance. This 114 // is separate from allocating because all default instances must be 115 // allocated before any can be initialized. 116 void GenerateDefaultInstanceInitializer(io::Printer* printer); 117 118 // Generates code that should be run when ShutdownProtobufLibrary() is called, 119 // to delete all dynamically-allocated objects. 120 void GenerateShutdownCode(io::Printer* printer); 121 122 // Generate all non-inline methods for this class. 123 void GenerateClassMethods(io::Printer* printer); 124 125 private: 126 // Generate declarations and definitions of accessors for fields. 127 void GenerateDependentBaseClassDefinition(io::Printer* printer); 128 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); 129 void GenerateFieldAccessorDeclarations(io::Printer* printer); 130 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); 131 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); 132 133 // Generate the field offsets array. 134 void GenerateOffsets(io::Printer* printer); 135 136 // Generate constructors and destructor. 137 void GenerateStructors(io::Printer* printer); 138 139 // The compiler typically generates multiple copies of each constructor and 140 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx 141 // Placing common code in a separate method reduces the generated code size. 142 // 143 // Generate the shared constructor code. 144 void GenerateSharedConstructorCode(io::Printer* printer); 145 // Generate the shared destructor code. 146 void GenerateSharedDestructorCode(io::Printer* printer); 147 // Generate the arena-specific destructor code. 148 void GenerateArenaDestructorCode(io::Printer* printer); 149 150 // Generate standard Message methods. 151 void GenerateClear(io::Printer* printer); 152 void GenerateOneofClear(io::Printer* printer); 153 void GenerateMergeFromCodedStream(io::Printer* printer); 154 void GenerateSerializeWithCachedSizes(io::Printer* printer); 155 void GenerateSerializeWithCachedSizesToArray(io::Printer* printer); 156 void GenerateSerializeWithCachedSizesBody(io::Printer* printer, 157 bool to_array); 158 void GenerateByteSize(io::Printer* printer); 159 void GenerateMergeFrom(io::Printer* printer); 160 void GenerateCopyFrom(io::Printer* printer); 161 void GenerateSwap(io::Printer* printer); 162 void GenerateIsInitialized(io::Printer* printer); 163 164 // Helpers for GenerateSerializeWithCachedSizes(). 165 void GenerateSerializeOneField(io::Printer* printer, 166 const FieldDescriptor* field, 167 bool unbounded); 168 void GenerateSerializeOneExtensionRange( 169 io::Printer* printer, const Descriptor::ExtensionRange* range, 170 bool unbounded); 171 172 173 // Generates has_foo() functions and variables for singular field has-bits. 174 void GenerateSingularFieldHasBits(const FieldDescriptor* field, 175 map<string, string> vars, 176 io::Printer* printer); 177 // Generates has_foo() functions and variables for oneof field has-bits. 178 void GenerateOneofHasBits(io::Printer* printer, bool is_inline); 179 // Generates has_foo_bar() functions for oneof members. 180 void GenerateOneofMemberHasBits(const FieldDescriptor* field, 181 const map<string, string>& vars, 182 io::Printer* printer); 183 // Generates the clear_foo() method for a field. 184 void GenerateFieldClear(const FieldDescriptor* field, 185 const map<string, string>& vars, 186 io::Printer* printer); 187 188 const Descriptor* descriptor_; 189 string classname_; 190 Options options_; 191 FieldGeneratorMap field_generators_; 192 vector< vector<string> > runs_of_fields_; // that might be trivially cleared 193 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > nested_generators_; 194 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_; 195 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_; 196 int num_required_fields_; 197 bool uses_string_; 198 bool use_dependent_base_; 199 200 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); 201 }; 202 203 } // namespace cpp 204 } // namespace compiler 205 } // namespace protobuf 206 207 } // namespace google 208 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__ 209