| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 class EnumGenerator; // enum.h | 58 class EnumGenerator; // enum.h |
| 59 class ExtensionGenerator; // extension.h | 59 class ExtensionGenerator; // extension.h |
| 60 | 60 |
| 61 class MessageGenerator { | 61 class MessageGenerator { |
| 62 public: | 62 public: |
| 63 // See generator.cc for the meaning of dllexport_decl. | 63 // See generator.cc for the meaning of dllexport_decl. |
| 64 MessageGenerator(const Descriptor* descriptor, const Options& options); | 64 MessageGenerator(const Descriptor* descriptor, const Options& options); |
| 65 ~MessageGenerator(); | 65 ~MessageGenerator(); |
| 66 | 66 |
| 67 // Appends the pre-order walk of the nested generators to list. | |
| 68 void Flatten(std::vector<MessageGenerator*>* list); | |
| 69 // Append the two types of nested generators to the corresponding vector. | |
| 70 void AddGenerators(std::vector<EnumGenerator*>* enum_generators, | |
| 71 std::vector<ExtensionGenerator*>* extension_generators); | |
| 72 | |
| 73 // Header stuff. | 67 // Header stuff. |
| 74 | 68 |
| 75 // Return names for forward declarations of this class and all its nested | 69 // Return names for foward declarations of this class and all its nested |
| 76 // types. A given key in {class,enum}_names will map from a class name to the | 70 // types. A given key in {class,enum}_names will map from a class name to the |
| 77 // descriptor that was responsible for its inclusion in the map. This can be | 71 // descriptor that was responsible for its inclusion in the map. This can be |
| 78 // used to associate the descriptor with the code generated for it. | 72 // used to associate the descriptor with the code generated for it. |
| 79 void FillMessageForwardDeclarations( | 73 void FillMessageForwardDeclarations( |
| 80 std::map<string, const Descriptor*>* class_names); | 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); |
| 81 | 85 |
| 82 // Generate definitions for this class and all its nested types. | 86 // Generate definitions for this class and all its nested types. |
| 83 void GenerateClassDefinition(io::Printer* printer); | 87 void GenerateClassDefinition(io::Printer* printer); |
| 84 | 88 |
| 85 // Generate definitions of inline methods (placed at the end of the header | 89 // Generate definitions of inline methods (placed at the end of the header |
| 86 // file). | 90 // file). |
| 87 void GenerateInlineMethods(io::Printer* printer, bool is_inline); | 91 void GenerateInlineMethods(io::Printer* printer, bool is_inline); |
| 88 | 92 |
| 89 // Dependent methods are always inline. | 93 // Dependent methods are always inline. |
| 90 void GenerateDependentInlineMethods(io::Printer* printer); | 94 void GenerateDependentInlineMethods(io::Printer* printer); |
| 91 | 95 |
| 92 // Source file stuff. | 96 // Source file stuff. |
| 93 | 97 |
| 94 // Generate code which declares all the global descriptor pointers which | 98 // Generate code which declares all the global descriptor pointers which |
| 95 // will be initialized by the methods below. | 99 // will be initialized by the methods below. |
| 96 void GenerateDescriptorDeclarations(io::Printer* printer); | 100 void GenerateDescriptorDeclarations(io::Printer* printer); |
| 97 | 101 |
| 102 // Generate code that initializes the global variable storing the message's |
| 103 // descriptor. |
| 104 void GenerateDescriptorInitializer(io::Printer* printer, int index); |
| 105 |
| 98 // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage() | 106 // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage() |
| 99 // for all types. | 107 // for all types. |
| 100 void GenerateTypeRegistrations(io::Printer* printer); | 108 void GenerateTypeRegistrations(io::Printer* printer); |
| 101 | 109 |
| 102 // Generates code that allocates the message's default instance. | 110 // Generates code that allocates the message's default instance. |
| 103 void GenerateDefaultInstanceAllocator(io::Printer* printer); | 111 void GenerateDefaultInstanceAllocator(io::Printer* printer); |
| 104 | 112 |
| 105 // Generates code that initializes the message's default instance. This | 113 // Generates code that initializes the message's default instance. This |
| 106 // is separate from allocating because all default instances must be | 114 // is separate from allocating because all default instances must be |
| 107 // allocated before any can be initialized. | 115 // allocated before any can be initialized. |
| 108 void GenerateDefaultInstanceInitializer(io::Printer* printer); | 116 void GenerateDefaultInstanceInitializer(io::Printer* printer); |
| 109 | 117 |
| 110 // Generates code that should be run when ShutdownProtobufLibrary() is called, | 118 // Generates code that should be run when ShutdownProtobufLibrary() is called, |
| 111 // to delete all dynamically-allocated objects. | 119 // to delete all dynamically-allocated objects. |
| 112 void GenerateShutdownCode(io::Printer* printer); | 120 void GenerateShutdownCode(io::Printer* printer); |
| 113 | 121 |
| 114 // Generate all non-inline methods for this class. | 122 // Generate all non-inline methods for this class. |
| 115 void GenerateClassMethods(io::Printer* printer); | 123 void GenerateClassMethods(io::Printer* printer); |
| 116 | 124 |
| 117 private: | 125 private: |
| 118 // Generate declarations and definitions of accessors for fields. | 126 // Generate declarations and definitions of accessors for fields. |
| 119 void GenerateDependentBaseClassDefinition(io::Printer* printer); | 127 void GenerateDependentBaseClassDefinition(io::Printer* printer); |
| 120 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); | 128 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); |
| 121 void GenerateFieldAccessorDeclarations(io::Printer* printer); | 129 void GenerateFieldAccessorDeclarations(io::Printer* printer); |
| 122 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); | 130 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); |
| 123 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); | 131 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); |
| 124 | 132 |
| 125 // Generate the field offsets array. Returns the a pair of the total numer | 133 // Generate the field offsets array. |
| 126 // of entries generated and the index of the first has_bit entry. | 134 void GenerateOffsets(io::Printer* printer); |
| 127 std::pair<size_t, size_t> GenerateOffsets(io::Printer* printer); | |
| 128 void GenerateSchema(io::Printer* printer, int offset, int has_offset); | |
| 129 | 135 |
| 130 // Generate constructors and destructor. | 136 // Generate constructors and destructor. |
| 131 void GenerateStructors(io::Printer* printer); | 137 void GenerateStructors(io::Printer* printer); |
| 132 | 138 |
| 133 // The compiler typically generates multiple copies of each constructor and | 139 // The compiler typically generates multiple copies of each constructor and |
| 134 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx | 140 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx |
| 135 // Placing common code in a separate method reduces the generated code size. | 141 // Placing common code in a separate method reduces the generated code size. |
| 136 // | 142 // |
| 137 // Generate the shared constructor code. | 143 // Generate the shared constructor code. |
| 138 void GenerateSharedConstructorCode(io::Printer* printer); | 144 void GenerateSharedConstructorCode(io::Printer* printer); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 void GenerateByteSize(io::Printer* printer); | 158 void GenerateByteSize(io::Printer* printer); |
| 153 void GenerateMergeFrom(io::Printer* printer); | 159 void GenerateMergeFrom(io::Printer* printer); |
| 154 void GenerateCopyFrom(io::Printer* printer); | 160 void GenerateCopyFrom(io::Printer* printer); |
| 155 void GenerateSwap(io::Printer* printer); | 161 void GenerateSwap(io::Printer* printer); |
| 156 void GenerateIsInitialized(io::Printer* printer); | 162 void GenerateIsInitialized(io::Printer* printer); |
| 157 | 163 |
| 158 // Helpers for GenerateSerializeWithCachedSizes(). | 164 // Helpers for GenerateSerializeWithCachedSizes(). |
| 159 void GenerateSerializeOneField(io::Printer* printer, | 165 void GenerateSerializeOneField(io::Printer* printer, |
| 160 const FieldDescriptor* field, | 166 const FieldDescriptor* field, |
| 161 bool unbounded); | 167 bool unbounded); |
| 162 // Generate a switch statement to serialize 2+ fields from the same oneof. | |
| 163 // Or, if fields.size() == 1, just call GenerateSerializeOneField(). | |
| 164 void GenerateSerializeOneofFields( | |
| 165 io::Printer* printer, const std::vector<const FieldDescriptor*>& fields, | |
| 166 bool to_array); | |
| 167 void GenerateSerializeOneExtensionRange( | 168 void GenerateSerializeOneExtensionRange( |
| 168 io::Printer* printer, const Descriptor::ExtensionRange* range, | 169 io::Printer* printer, const Descriptor::ExtensionRange* range, |
| 169 bool unbounded); | 170 bool unbounded); |
| 170 | 171 |
| 171 | 172 |
| 172 // Generates has_foo() functions and variables for singular field has-bits. | 173 // Generates has_foo() functions and variables for singular field has-bits. |
| 173 void GenerateSingularFieldHasBits(const FieldDescriptor* field, | 174 void GenerateSingularFieldHasBits(const FieldDescriptor* field, |
| 174 std::map<string, string> vars, | 175 map<string, string> vars, |
| 175 io::Printer* printer); | 176 io::Printer* printer); |
| 176 // Generates has_foo() functions and variables for oneof field has-bits. | 177 // Generates has_foo() functions and variables for oneof field has-bits. |
| 177 void GenerateOneofHasBits(io::Printer* printer, bool is_inline); | 178 void GenerateOneofHasBits(io::Printer* printer, bool is_inline); |
| 178 // Generates has_foo_bar() functions for oneof members. | 179 // Generates has_foo_bar() functions for oneof members. |
| 179 void GenerateOneofMemberHasBits(const FieldDescriptor* field, | 180 void GenerateOneofMemberHasBits(const FieldDescriptor* field, |
| 180 const std::map<string, string>& vars, | 181 const map<string, string>& vars, |
| 181 io::Printer* printer); | 182 io::Printer* printer); |
| 182 // Generates the clear_foo() method for a field. | 183 // Generates the clear_foo() method for a field. |
| 183 void GenerateFieldClear(const FieldDescriptor* field, | 184 void GenerateFieldClear(const FieldDescriptor* field, |
| 184 const std::map<string, string>& vars, | 185 const map<string, string>& vars, |
| 185 io::Printer* printer); | 186 io::Printer* printer); |
| 186 | 187 |
| 187 void GenerateConstructorBody(io::Printer* printer, | |
| 188 std::vector<bool> already_processed, | |
| 189 bool copy_constructor) const; | |
| 190 | |
| 191 size_t HasBitsSize() const; | |
| 192 std::vector<uint32> RequiredFieldsBitMask() const; | |
| 193 | |
| 194 const Descriptor* descriptor_; | 188 const Descriptor* descriptor_; |
| 195 string classname_; | 189 string classname_; |
| 196 Options options_; | 190 Options options_; |
| 197 FieldGeneratorMap field_generators_; | 191 FieldGeneratorMap field_generators_; |
| 198 // optimized_order_ is the order we layout the message's fields in the class. | 192 vector< vector<string> > runs_of_fields_; // that might be trivially cleared |
| 199 // This is reused to initialize the fields in-order for cache efficiency. | |
| 200 // | |
| 201 // optimized_order_ excludes oneof fields. | |
| 202 std::vector<const FieldDescriptor *> optimized_order_; | |
| 203 std::vector<int> has_bit_indices_; | |
| 204 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator>
> nested_generators_; | 193 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator>
> nested_generators_; |
| 205 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > e
num_generators_; | 194 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > e
num_generators_; |
| 206 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator
> > extension_generators_; | 195 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator
> > extension_generators_; |
| 207 int num_required_fields_; | 196 int num_required_fields_; |
| 197 bool uses_string_; |
| 208 bool use_dependent_base_; | 198 bool use_dependent_base_; |
| 209 | 199 |
| 210 int index_in_metadata_; | |
| 211 | |
| 212 friend class FileGenerator; | |
| 213 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); | 200 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); |
| 214 }; | 201 }; |
| 215 | 202 |
| 216 } // namespace cpp | 203 } // namespace cpp |
| 217 } // namespace compiler | 204 } // namespace compiler |
| 218 } // namespace protobuf | 205 } // namespace protobuf |
| 219 | 206 |
| 220 } // namespace google | 207 } // namespace google |
| 221 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__ | 208 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__ |
| OLD | NEW |