| 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 |
| 67 // Header stuff. | 73 // Header stuff. |
| 68 | 74 |
| 69 // Return names for foward declarations of this class and all its nested | 75 // Return names for forward 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 | 76 // 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 | 77 // 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. | 78 // used to associate the descriptor with the code generated for it. |
| 73 void FillMessageForwardDeclarations( | 79 void FillMessageForwardDeclarations( |
| 74 map<string, const Descriptor*>* class_names); | 80 std::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 | 81 |
| 86 // Generate definitions for this class and all its nested types. | 82 // Generate definitions for this class and all its nested types. |
| 87 void GenerateClassDefinition(io::Printer* printer); | 83 void GenerateClassDefinition(io::Printer* printer); |
| 88 | 84 |
| 89 // Generate definitions of inline methods (placed at the end of the header | 85 // Generate definitions of inline methods (placed at the end of the header |
| 90 // file). | 86 // file). |
| 91 void GenerateInlineMethods(io::Printer* printer, bool is_inline); | 87 void GenerateInlineMethods(io::Printer* printer, bool is_inline); |
| 92 | 88 |
| 93 // Dependent methods are always inline. | 89 // Dependent methods are always inline. |
| 94 void GenerateDependentInlineMethods(io::Printer* printer); | 90 void GenerateDependentInlineMethods(io::Printer* printer); |
| 95 | 91 |
| 96 // Source file stuff. | 92 // Source file stuff. |
| 97 | 93 |
| 98 // Generate code which declares all the global descriptor pointers which | 94 // Generate code which declares all the global descriptor pointers which |
| 99 // will be initialized by the methods below. | 95 // will be initialized by the methods below. |
| 100 void GenerateDescriptorDeclarations(io::Printer* printer); | 96 void GenerateDescriptorDeclarations(io::Printer* printer); |
| 101 | 97 |
| 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() | 98 // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage() |
| 107 // for all types. | 99 // for all types. |
| 108 void GenerateTypeRegistrations(io::Printer* printer); | 100 void GenerateTypeRegistrations(io::Printer* printer); |
| 109 | 101 |
| 110 // Generates code that allocates the message's default instance. | 102 // Generates code that allocates the message's default instance. |
| 111 void GenerateDefaultInstanceAllocator(io::Printer* printer); | 103 void GenerateDefaultInstanceAllocator(io::Printer* printer); |
| 112 | 104 |
| 113 // Generates code that initializes the message's default instance. This | 105 // Generates code that initializes the message's default instance. This |
| 114 // is separate from allocating because all default instances must be | 106 // is separate from allocating because all default instances must be |
| 115 // allocated before any can be initialized. | 107 // allocated before any can be initialized. |
| 116 void GenerateDefaultInstanceInitializer(io::Printer* printer); | 108 void GenerateDefaultInstanceInitializer(io::Printer* printer); |
| 117 | 109 |
| 118 // Generates code that should be run when ShutdownProtobufLibrary() is called, | 110 // Generates code that should be run when ShutdownProtobufLibrary() is called, |
| 119 // to delete all dynamically-allocated objects. | 111 // to delete all dynamically-allocated objects. |
| 120 void GenerateShutdownCode(io::Printer* printer); | 112 void GenerateShutdownCode(io::Printer* printer); |
| 121 | 113 |
| 122 // Generate all non-inline methods for this class. | 114 // Generate all non-inline methods for this class. |
| 123 void GenerateClassMethods(io::Printer* printer); | 115 void GenerateClassMethods(io::Printer* printer); |
| 124 | 116 |
| 125 private: | 117 private: |
| 126 // Generate declarations and definitions of accessors for fields. | 118 // Generate declarations and definitions of accessors for fields. |
| 127 void GenerateDependentBaseClassDefinition(io::Printer* printer); | 119 void GenerateDependentBaseClassDefinition(io::Printer* printer); |
| 128 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); | 120 void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); |
| 129 void GenerateFieldAccessorDeclarations(io::Printer* printer); | 121 void GenerateFieldAccessorDeclarations(io::Printer* printer); |
| 130 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); | 122 void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); |
| 131 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); | 123 void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); |
| 132 | 124 |
| 133 // Generate the field offsets array. | 125 // Generate the field offsets array. Returns the a pair of the total numer |
| 134 void GenerateOffsets(io::Printer* printer); | 126 // of entries generated and the index of the first has_bit entry. |
| 127 std::pair<size_t, size_t> GenerateOffsets(io::Printer* printer); |
| 128 void GenerateSchema(io::Printer* printer, int offset, int has_offset); |
| 135 | 129 |
| 136 // Generate constructors and destructor. | 130 // Generate constructors and destructor. |
| 137 void GenerateStructors(io::Printer* printer); | 131 void GenerateStructors(io::Printer* printer); |
| 138 | 132 |
| 139 // The compiler typically generates multiple copies of each constructor and | 133 // The compiler typically generates multiple copies of each constructor and |
| 140 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx | 134 // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx |
| 141 // Placing common code in a separate method reduces the generated code size. | 135 // Placing common code in a separate method reduces the generated code size. |
| 142 // | 136 // |
| 143 // Generate the shared constructor code. | 137 // Generate the shared constructor code. |
| 144 void GenerateSharedConstructorCode(io::Printer* printer); | 138 void GenerateSharedConstructorCode(io::Printer* printer); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 void GenerateByteSize(io::Printer* printer); | 152 void GenerateByteSize(io::Printer* printer); |
| 159 void GenerateMergeFrom(io::Printer* printer); | 153 void GenerateMergeFrom(io::Printer* printer); |
| 160 void GenerateCopyFrom(io::Printer* printer); | 154 void GenerateCopyFrom(io::Printer* printer); |
| 161 void GenerateSwap(io::Printer* printer); | 155 void GenerateSwap(io::Printer* printer); |
| 162 void GenerateIsInitialized(io::Printer* printer); | 156 void GenerateIsInitialized(io::Printer* printer); |
| 163 | 157 |
| 164 // Helpers for GenerateSerializeWithCachedSizes(). | 158 // Helpers for GenerateSerializeWithCachedSizes(). |
| 165 void GenerateSerializeOneField(io::Printer* printer, | 159 void GenerateSerializeOneField(io::Printer* printer, |
| 166 const FieldDescriptor* field, | 160 const FieldDescriptor* field, |
| 167 bool unbounded); | 161 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); |
| 168 void GenerateSerializeOneExtensionRange( | 167 void GenerateSerializeOneExtensionRange( |
| 169 io::Printer* printer, const Descriptor::ExtensionRange* range, | 168 io::Printer* printer, const Descriptor::ExtensionRange* range, |
| 170 bool unbounded); | 169 bool unbounded); |
| 171 | 170 |
| 172 | 171 |
| 173 // Generates has_foo() functions and variables for singular field has-bits. | 172 // Generates has_foo() functions and variables for singular field has-bits. |
| 174 void GenerateSingularFieldHasBits(const FieldDescriptor* field, | 173 void GenerateSingularFieldHasBits(const FieldDescriptor* field, |
| 175 map<string, string> vars, | 174 std::map<string, string> vars, |
| 176 io::Printer* printer); | 175 io::Printer* printer); |
| 177 // Generates has_foo() functions and variables for oneof field has-bits. | 176 // Generates has_foo() functions and variables for oneof field has-bits. |
| 178 void GenerateOneofHasBits(io::Printer* printer, bool is_inline); | 177 void GenerateOneofHasBits(io::Printer* printer, bool is_inline); |
| 179 // Generates has_foo_bar() functions for oneof members. | 178 // Generates has_foo_bar() functions for oneof members. |
| 180 void GenerateOneofMemberHasBits(const FieldDescriptor* field, | 179 void GenerateOneofMemberHasBits(const FieldDescriptor* field, |
| 181 const map<string, string>& vars, | 180 const std::map<string, string>& vars, |
| 182 io::Printer* printer); | 181 io::Printer* printer); |
| 183 // Generates the clear_foo() method for a field. | 182 // Generates the clear_foo() method for a field. |
| 184 void GenerateFieldClear(const FieldDescriptor* field, | 183 void GenerateFieldClear(const FieldDescriptor* field, |
| 185 const map<string, string>& vars, | 184 const std::map<string, string>& vars, |
| 186 io::Printer* printer); | 185 io::Printer* printer); |
| 187 | 186 |
| 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 |
| 188 const Descriptor* descriptor_; | 194 const Descriptor* descriptor_; |
| 189 string classname_; | 195 string classname_; |
| 190 Options options_; | 196 Options options_; |
| 191 FieldGeneratorMap field_generators_; | 197 FieldGeneratorMap field_generators_; |
| 192 vector< vector<string> > runs_of_fields_; // that might be trivially cleared | 198 // optimized_order_ is the order we layout the message's fields in the class. |
| 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_; |
| 193 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator>
> nested_generators_; | 204 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator>
> nested_generators_; |
| 194 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > e
num_generators_; | 205 google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > e
num_generators_; |
| 195 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator
> > extension_generators_; | 206 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator
> > extension_generators_; |
| 196 int num_required_fields_; | 207 int num_required_fields_; |
| 197 bool uses_string_; | |
| 198 bool use_dependent_base_; | 208 bool use_dependent_base_; |
| 199 | 209 |
| 210 int index_in_metadata_; |
| 211 |
| 212 friend class FileGenerator; |
| 200 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); | 213 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); |
| 201 }; | 214 }; |
| 202 | 215 |
| 203 } // namespace cpp | 216 } // namespace cpp |
| 204 } // namespace compiler | 217 } // namespace compiler |
| 205 } // namespace protobuf | 218 } // namespace protobuf |
| 206 | 219 |
| 207 } // namespace google | 220 } // namespace google |
| 208 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__ | 221 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__ |
| OLD | NEW |