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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 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 | #include <algorithm> |
| 36 | #include <google/protobuf/stubs/hash.h> |
| 37 | #include <google/protobuf/compiler/cpp/cpp_message.h> |
| 38 | #include <google/protobuf/compiler/cpp/cpp_enum.h> |
| 39 | #include <google/protobuf/compiler/cpp/cpp_extension.h> |
| 40 | #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
| 41 | #include <google/protobuf/stubs/strutil.h> |
| 42 | #include <google/protobuf/io/printer.h> |
| 43 | #include <google/protobuf/io/coded_stream.h> |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 44 | #include <google/protobuf/wire_format_inl.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 45 | #include <google/protobuf/descriptor.pb.h> |
| 46 | |
| 47 | namespace google { |
| 48 | namespace protobuf { |
| 49 | namespace compiler { |
| 50 | namespace cpp { |
| 51 | |
| 52 | using internal::WireFormat; |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) { |
| 57 | // Print the field's proto-syntax definition as a comment. We don't want to |
| 58 | // print group bodies so we cut off after the first line. |
| 59 | string def = field->DebugString(); |
| 60 | printer->Print("// $def$\n", |
| 61 | "def", def.substr(0, def.find_first_of('\n'))); |
| 62 | } |
| 63 | |
| 64 | struct FieldOrderingByNumber { |
| 65 | inline bool operator()(const FieldDescriptor* a, |
| 66 | const FieldDescriptor* b) const { |
| 67 | return a->number() < b->number(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | const char* kWireTypeNames[] = { |
| 72 | "VARINT", |
| 73 | "FIXED64", |
| 74 | "LENGTH_DELIMITED", |
| 75 | "START_GROUP", |
| 76 | "END_GROUP", |
| 77 | "FIXED32", |
| 78 | }; |
| 79 | |
| 80 | // Sort the fields of the given Descriptor by number into a new[]'d array |
| 81 | // and return it. |
| 82 | const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) { |
| 83 | const FieldDescriptor** fields = |
| 84 | new const FieldDescriptor*[descriptor->field_count()]; |
| 85 | for (int i = 0; i < descriptor->field_count(); i++) { |
| 86 | fields[i] = descriptor->field(i); |
| 87 | } |
| 88 | sort(fields, fields + descriptor->field_count(), |
| 89 | FieldOrderingByNumber()); |
| 90 | return fields; |
| 91 | } |
| 92 | |
| 93 | // Functor for sorting extension ranges by their "start" field number. |
| 94 | struct ExtensionRangeSorter { |
| 95 | bool operator()(const Descriptor::ExtensionRange* left, |
| 96 | const Descriptor::ExtensionRange* right) const { |
| 97 | return left->start < right->start; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | // Returns true if the message type has any required fields. If it doesn't, |
| 102 | // we can optimize out calls to its IsInitialized() method. |
| 103 | // |
| 104 | // already_seen is used to avoid checking the same type multiple times |
| 105 | // (and also to protect against recursion). |
| 106 | static bool HasRequiredFields( |
| 107 | const Descriptor* type, |
| 108 | hash_set<const Descriptor*>* already_seen) { |
| 109 | if (already_seen->count(type) > 0) { |
| 110 | // Since the first occurrence of a required field causes the whole |
| 111 | // function to return true, we can assume that if the type is already |
| 112 | // in the cache it didn't have any required fields. |
| 113 | return false; |
| 114 | } |
| 115 | already_seen->insert(type); |
| 116 | |
| 117 | // If the type has extensions, an extension with message type could contain |
| 118 | // required fields, so we have to be conservative and assume such an |
| 119 | // extension exists. |
| 120 | if (type->extension_range_count() > 0) return true; |
| 121 | |
| 122 | for (int i = 0; i < type->field_count(); i++) { |
| 123 | const FieldDescriptor* field = type->field(i); |
| 124 | if (field->is_required()) { |
| 125 | return true; |
| 126 | } |
| 127 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 128 | if (HasRequiredFields(field->message_type(), already_seen)) { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | static bool HasRequiredFields(const Descriptor* type) { |
| 138 | hash_set<const Descriptor*> already_seen; |
| 139 | return HasRequiredFields(type, &already_seen); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | // =================================================================== |
| 145 | |
| 146 | MessageGenerator::MessageGenerator(const Descriptor* descriptor, |
| 147 | const string& dllexport_decl) |
| 148 | : descriptor_(descriptor), |
| 149 | classname_(ClassName(descriptor, false)), |
| 150 | dllexport_decl_(dllexport_decl), |
| 151 | field_generators_(descriptor), |
| 152 | nested_generators_(new scoped_ptr<MessageGenerator>[ |
| 153 | descriptor->nested_type_count()]), |
| 154 | enum_generators_(new scoped_ptr<EnumGenerator>[ |
| 155 | descriptor->enum_type_count()]), |
| 156 | extension_generators_(new scoped_ptr<ExtensionGenerator>[ |
| 157 | descriptor->extension_count()]) { |
| 158 | |
| 159 | for (int i = 0; i < descriptor->nested_type_count(); i++) { |
| 160 | nested_generators_[i].reset( |
| 161 | new MessageGenerator(descriptor->nested_type(i), dllexport_decl)); |
| 162 | } |
| 163 | |
| 164 | for (int i = 0; i < descriptor->enum_type_count(); i++) { |
| 165 | enum_generators_[i].reset( |
| 166 | new EnumGenerator(descriptor->enum_type(i), dllexport_decl)); |
| 167 | } |
| 168 | |
| 169 | for (int i = 0; i < descriptor->extension_count(); i++) { |
| 170 | extension_generators_[i].reset( |
| 171 | new ExtensionGenerator(descriptor->extension(i), dllexport_decl)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | MessageGenerator::~MessageGenerator() {} |
| 176 | |
| 177 | void MessageGenerator:: |
| 178 | GenerateForwardDeclaration(io::Printer* printer) { |
| 179 | printer->Print("class $classname$;\n", |
| 180 | "classname", classname_); |
| 181 | |
| 182 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 183 | nested_generators_[i]->GenerateForwardDeclaration(printer); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void MessageGenerator:: |
| 188 | GenerateEnumDefinitions(io::Printer* printer) { |
| 189 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 190 | nested_generators_[i]->GenerateEnumDefinitions(printer); |
| 191 | } |
| 192 | |
| 193 | for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
| 194 | enum_generators_[i]->GenerateDefinition(printer); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void MessageGenerator:: |
| 199 | GenerateFieldAccessorDeclarations(io::Printer* printer) { |
| 200 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 201 | const FieldDescriptor* field = descriptor_->field(i); |
| 202 | |
| 203 | PrintFieldComment(printer, field); |
| 204 | |
| 205 | map<string, string> vars; |
| 206 | vars["name"] = FieldName(field); |
| 207 | |
| 208 | if (field->is_repeated()) { |
| 209 | printer->Print(vars, "inline int $name$_size() const;\n"); |
| 210 | } else { |
| 211 | printer->Print(vars, "inline bool has_$name$() const;\n"); |
| 212 | } |
| 213 | |
| 214 | printer->Print(vars, "inline void clear_$name$();\n"); |
| 215 | |
| 216 | // Generate type-specific accessor declarations. |
| 217 | field_generators_.get(field).GenerateAccessorDeclarations(printer); |
| 218 | |
| 219 | printer->Print("\n"); |
| 220 | } |
| 221 | |
| 222 | if (descriptor_->extension_range_count() > 0) { |
| 223 | // Generate accessors for extensions. |
| 224 | |
| 225 | // Normally I'd generate prototypes here and generate the actual |
| 226 | // definitions of these methods in GenerateFieldAccessorDefinitions, but |
| 227 | // the prototypes for these silly methods are so absurdly complicated that |
| 228 | // it meant way too much repitition. |
| 229 | // |
| 230 | // We use "_proto_TypeTraits" as a type name below because "TypeTraits" |
| 231 | // causes problems if the class has a nested message or enum type with that |
| 232 | // name and "_TypeTraits" is technically reserved for the C++ library since |
| 233 | // it starts with an underscore followed by a capital letter. |
| 234 | printer->Print( |
| 235 | // Has, Size, Clear |
| 236 | "template <typename _proto_TypeTraits>\n" |
| 237 | "inline bool HasExtension(\n" |
| 238 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 239 | " $classname$, _proto_TypeTraits>& id) const {\n" |
| 240 | " return _extensions_.Has(id.number());\n" |
| 241 | "}\n" |
| 242 | "\n" |
| 243 | "template <typename _proto_TypeTraits>\n" |
| 244 | "inline void ClearExtension(\n" |
| 245 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 246 | " $classname$, _proto_TypeTraits>& id) {\n" |
| 247 | " _extensions_.ClearExtension(id.number());\n" |
| 248 | "}\n" |
| 249 | "\n" |
| 250 | "template <typename _proto_TypeTraits>\n" |
| 251 | "inline int ExtensionSize(\n" |
| 252 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 253 | " $classname$, _proto_TypeTraits>& id) const {\n" |
| 254 | " return _extensions_.ExtensionSize(id.number());\n" |
| 255 | "}\n" |
| 256 | "\n" |
| 257 | |
| 258 | // Singular accessors |
| 259 | "template <typename _proto_TypeTraits>\n" |
| 260 | "inline typename _proto_TypeTraits::ConstType GetExtension(\n" |
| 261 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 262 | " $classname$, _proto_TypeTraits>& id) const {\n" |
| 263 | " return _proto_TypeTraits::Get(id.number(), _extensions_);\n" |
| 264 | "}\n" |
| 265 | "\n" |
| 266 | "template <typename _proto_TypeTraits>\n" |
| 267 | "inline typename _proto_TypeTraits::MutableType MutableExtension(\n" |
| 268 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 269 | " $classname$, _proto_TypeTraits>& id) {\n" |
| 270 | " return _proto_TypeTraits::Mutable(id.number(), &_extensions_);\n" |
| 271 | "}\n" |
| 272 | "\n" |
| 273 | "template <typename _proto_TypeTraits>\n" |
| 274 | "inline void SetExtension(\n" |
| 275 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 276 | " $classname$, _proto_TypeTraits>& id,\n" |
| 277 | " typename _proto_TypeTraits::ConstType value) {\n" |
| 278 | " _proto_TypeTraits::Set(id.number(), value, &_extensions_);\n" |
| 279 | "}\n" |
| 280 | "\n" |
| 281 | |
| 282 | // Repeated accessors |
| 283 | "template <typename _proto_TypeTraits>\n" |
| 284 | "inline typename _proto_TypeTraits::ConstType GetExtension(\n" |
| 285 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 286 | " $classname$, _proto_TypeTraits>& id,\n" |
| 287 | " int index) const {\n" |
| 288 | " return _proto_TypeTraits::Get(id.number(), _extensions_, index);\n" |
| 289 | "}\n" |
| 290 | "\n" |
| 291 | "template <typename _proto_TypeTraits>\n" |
| 292 | "inline typename _proto_TypeTraits::MutableType MutableExtension(\n" |
| 293 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 294 | " $classname$, _proto_TypeTraits>& id,\n" |
| 295 | " int index) {\n" |
| 296 | " return _proto_TypeTraits::Mutable(id.number(),index,&_extensions_);\n" |
| 297 | "}\n" |
| 298 | "\n" |
| 299 | "template <typename _proto_TypeTraits>\n" |
| 300 | "inline void SetExtension(\n" |
| 301 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 302 | " $classname$, _proto_TypeTraits>& id,\n" |
| 303 | " int index, typename _proto_TypeTraits::ConstType value) {\n" |
| 304 | " _proto_TypeTraits::Set(id.number(), index, value, &_extensions_);\n" |
| 305 | "}\n" |
| 306 | "\n" |
| 307 | "template <typename _proto_TypeTraits>\n" |
| 308 | "inline typename _proto_TypeTraits::MutableType AddExtension(\n" |
| 309 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 310 | " $classname$, _proto_TypeTraits>& id) {\n" |
| 311 | " return _proto_TypeTraits::Add(id.number(), &_extensions_);\n" |
| 312 | "}\n" |
| 313 | "\n" |
| 314 | "template <typename _proto_TypeTraits>\n" |
| 315 | "inline void AddExtension(\n" |
| 316 | " const ::google::protobuf::internal::ExtensionIdentifier<\n" |
| 317 | " $classname$, _proto_TypeTraits>& id,\n" |
| 318 | " typename _proto_TypeTraits::ConstType value) {\n" |
| 319 | " _proto_TypeTraits::Add(id.number(), value, &_extensions_);\n" |
| 320 | "}\n", |
| 321 | "classname", classname_); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void MessageGenerator:: |
| 326 | GenerateFieldAccessorDefinitions(io::Printer* printer) { |
| 327 | printer->Print("// $classname$\n\n", "classname", classname_); |
| 328 | |
| 329 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 330 | const FieldDescriptor* field = descriptor_->field(i); |
| 331 | |
| 332 | PrintFieldComment(printer, field); |
| 333 | |
| 334 | map<string, string> vars; |
| 335 | vars["name"] = FieldName(field); |
| 336 | vars["index"] = SimpleItoa(field->index()); |
| 337 | vars["classname"] = classname_; |
| 338 | |
| 339 | // Generate has_$name$() or $name$_size(). |
| 340 | if (field->is_repeated()) { |
| 341 | printer->Print(vars, |
| 342 | "inline int $classname$::$name$_size() const {\n" |
| 343 | " return $name$_.size();\n" |
| 344 | "}\n"); |
| 345 | } else { |
| 346 | // Singular field. |
| 347 | printer->Print(vars, |
| 348 | "inline bool $classname$::has_$name$() const {\n" |
| 349 | " return _has_bit($index$);\n" |
| 350 | "}\n"); |
| 351 | } |
| 352 | |
| 353 | // Generate clear_$name$() |
| 354 | printer->Print(vars, |
| 355 | "inline void $classname$::clear_$name$() {\n"); |
| 356 | |
| 357 | printer->Indent(); |
| 358 | field_generators_.get(field).GenerateClearingCode(printer); |
| 359 | printer->Outdent(); |
| 360 | |
| 361 | if (!field->is_repeated()) { |
| 362 | printer->Print(vars, " _clear_bit($index$);\n"); |
| 363 | } |
| 364 | |
| 365 | printer->Print("}\n"); |
| 366 | |
| 367 | // Generate type-specific accessors. |
| 368 | field_generators_.get(field).GenerateInlineAccessorDefinitions(printer); |
| 369 | |
| 370 | printer->Print("\n"); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void MessageGenerator:: |
| 375 | GenerateClassDefinition(io::Printer* printer) { |
| 376 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 377 | nested_generators_[i]->GenerateClassDefinition(printer); |
| 378 | printer->Print("\n"); |
| 379 | printer->Print(kThinSeparator); |
| 380 | printer->Print("\n"); |
| 381 | } |
| 382 | |
| 383 | map<string, string> vars; |
| 384 | vars["classname"] = classname_; |
| 385 | vars["field_count"] = SimpleItoa(descriptor_->field_count()); |
| 386 | if (dllexport_decl_.empty()) { |
| 387 | vars["dllexport"] = ""; |
| 388 | } else { |
| 389 | vars["dllexport"] = dllexport_decl_ + " "; |
| 390 | } |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 391 | vars["builddescriptorsname"] = |
| 392 | GlobalBuildDescriptorsName(descriptor_->file()->name()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 393 | |
| 394 | printer->Print(vars, |
| 395 | "class $dllexport$$classname$ : public ::google::protobuf::Message {\n" |
| 396 | " public:\n"); |
| 397 | printer->Indent(); |
| 398 | |
| 399 | printer->Print(vars, |
| 400 | "$classname$();\n" |
| 401 | "virtual ~$classname$();\n" |
| 402 | "\n" |
| 403 | "$classname$(const $classname$& from);\n" |
| 404 | "\n" |
| 405 | "inline $classname$& operator=(const $classname$& from) {\n" |
| 406 | " CopyFrom(from);\n" |
| 407 | " return *this;\n" |
| 408 | "}\n" |
| 409 | "\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 410 | "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 411 | " return _unknown_fields_;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 412 | "}\n" |
| 413 | "\n" |
| 414 | "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 415 | " return &_unknown_fields_;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 416 | "}\n" |
| 417 | "\n" |
| 418 | "static const ::google::protobuf::Descriptor* descriptor();\n" |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 419 | "static const $classname$& default_instance();\n" |
| 420 | "void Swap($classname$* other);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 421 | "\n" |
| 422 | "// implements Message ----------------------------------------------\n" |
| 423 | "\n" |
| 424 | "$classname$* New() const;\n"); |
| 425 | |
| 426 | if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) { |
| 427 | printer->Print(vars, |
| 428 | "void CopyFrom(const ::google::protobuf::Message& from);\n" |
| 429 | "void MergeFrom(const ::google::protobuf::Message& from);\n" |
| 430 | "void CopyFrom(const $classname$& from);\n" |
| 431 | "void MergeFrom(const $classname$& from);\n" |
| 432 | "void Clear();\n" |
| 433 | "bool IsInitialized() const;\n" |
| 434 | "int ByteSize() const;\n" |
| 435 | "\n" |
| 436 | "bool MergePartialFromCodedStream(\n" |
| 437 | " ::google::protobuf::io::CodedInputStream* input);\n" |
| 438 | "bool SerializeWithCachedSizes(\n" |
| 439 | " ::google::protobuf::io::CodedOutputStream* output) const;\n"); |
| 440 | } |
| 441 | |
| 442 | printer->Print(vars, |
| 443 | "int GetCachedSize() const { return _cached_size_; }\n" |
| 444 | "private:\n" |
| 445 | "void SetCachedSize(int size) const { _cached_size_ = size; }\n" |
| 446 | "public:\n" |
| 447 | "\n" |
| 448 | "const ::google::protobuf::Descriptor* GetDescriptor() const;\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 449 | "const ::google::protobuf::Reflection* GetReflection() const;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 450 | "\n" |
| 451 | "// nested types ----------------------------------------------------\n" |
| 452 | "\n"); |
| 453 | |
| 454 | // Import all nested message classes into this class's scope with typedefs. |
| 455 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 456 | const Descriptor* nested_type = descriptor_->nested_type(i); |
| 457 | printer->Print("typedef $nested_full_name$ $nested_name$;\n", |
| 458 | "nested_name", nested_type->name(), |
| 459 | "nested_full_name", ClassName(nested_type, false)); |
| 460 | } |
| 461 | |
| 462 | if (descriptor_->nested_type_count() > 0) { |
| 463 | printer->Print("\n"); |
| 464 | } |
| 465 | |
| 466 | // Import all nested enums and their values into this class's scope with |
| 467 | // typedefs and constants. |
| 468 | for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
| 469 | enum_generators_[i]->GenerateSymbolImports(printer); |
| 470 | printer->Print("\n"); |
| 471 | } |
| 472 | |
| 473 | printer->Print( |
| 474 | "// accessors -------------------------------------------------------\n" |
| 475 | "\n"); |
| 476 | |
| 477 | // Generate accessor methods for all fields. |
| 478 | GenerateFieldAccessorDeclarations(printer); |
| 479 | |
| 480 | // Declare extension identifiers. |
| 481 | for (int i = 0; i < descriptor_->extension_count(); i++) { |
| 482 | extension_generators_[i]->GenerateDeclaration(printer); |
| 483 | } |
| 484 | |
| 485 | // Generate private members for fields. |
| 486 | printer->Outdent(); |
| 487 | printer->Print(" private:\n"); |
| 488 | printer->Indent(); |
| 489 | |
| 490 | if (descriptor_->extension_range_count() > 0) { |
| 491 | printer->Print( |
| 492 | "::google::protobuf::internal::ExtensionSet _extensions_;\n"); |
| 493 | } |
| 494 | |
| 495 | // TODO(kenton): Make _cached_size_ an atomic<int> when C++ supports it. |
| 496 | printer->Print( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 497 | "::google::protobuf::UnknownFieldSet _unknown_fields_;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 498 | "mutable int _cached_size_;\n" |
| 499 | "\n"); |
| 500 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 501 | field_generators_.get(descriptor_->field(i)) |
| 502 | .GeneratePrivateMembers(printer); |
| 503 | } |
| 504 | |
| 505 | // Generate offsets and _has_bits_ boilerplate. |
| 506 | printer->Print(vars, |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 507 | "friend void $builddescriptorsname$_AssignGlobalDescriptors(\n" |
| 508 | " const ::google::protobuf::FileDescriptor* file);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 509 | |
| 510 | if (descriptor_->field_count() > 0) { |
| 511 | printer->Print(vars, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 512 | "::google::protobuf::uint32 _has_bits_[($field_count$ + 31) / 32];\n"); |
| 513 | } else { |
| 514 | // Zero-size arrays aren't technically allowed, and MSVC in particular |
| 515 | // doesn't like them. We still need to declare these arrays to make |
| 516 | // other code compile. Since this is an uncommon case, we'll just declare |
| 517 | // them with size 1 and waste some space. Oh well. |
| 518 | printer->Print( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 519 | "::google::protobuf::uint32 _has_bits_[1];\n"); |
| 520 | } |
| 521 | |
| 522 | printer->Print( |
| 523 | "\n" |
| 524 | "// WHY DOES & HAVE LOWER PRECEDENCE THAN != !?\n" |
| 525 | "inline bool _has_bit(int index) const {\n" |
| 526 | " return (_has_bits_[index / 32] & (1u << (index % 32))) != 0;\n" |
| 527 | "}\n" |
| 528 | "inline void _set_bit(int index) {\n" |
| 529 | " _has_bits_[index / 32] |= (1u << (index % 32));\n" |
| 530 | "}\n" |
| 531 | "inline void _clear_bit(int index) {\n" |
| 532 | " _has_bits_[index / 32] &= ~(1u << (index % 32));\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 533 | "}\n" |
| 534 | "\n" |
| 535 | "void InitAsDefaultInstance();\n" |
| 536 | "static $classname$* default_instance_;\n", |
| 537 | "classname", classname_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 538 | |
| 539 | printer->Outdent(); |
| 540 | printer->Print(vars, "};"); |
| 541 | } |
| 542 | |
| 543 | void MessageGenerator:: |
| 544 | GenerateInlineMethods(io::Printer* printer) { |
| 545 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 546 | nested_generators_[i]->GenerateInlineMethods(printer); |
| 547 | printer->Print(kThinSeparator); |
| 548 | printer->Print("\n"); |
| 549 | } |
| 550 | |
| 551 | GenerateFieldAccessorDefinitions(printer); |
| 552 | } |
| 553 | |
| 554 | void MessageGenerator:: |
| 555 | GenerateDescriptorDeclarations(io::Printer* printer) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 556 | printer->Print( |
| 557 | "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n" |
| 558 | "const ::google::protobuf::internal::GeneratedMessageReflection*\n" |
| 559 | " $name$_reflection_ = NULL;\n", |
| 560 | "name", classname_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 561 | |
| 562 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 563 | nested_generators_[i]->GenerateDescriptorDeclarations(printer); |
| 564 | } |
| 565 | |
| 566 | for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
| 567 | printer->Print( |
| 568 | "const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n", |
| 569 | "name", ClassName(descriptor_->enum_type(i), false)); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void MessageGenerator:: |
| 574 | GenerateDescriptorInitializer(io::Printer* printer, int index) { |
| 575 | // TODO(kenton): Passing the index to this method is redundant; just use |
| 576 | // descriptor_->index() instead. |
| 577 | map<string, string> vars; |
| 578 | vars["classname"] = classname_; |
| 579 | vars["index"] = SimpleItoa(index); |
| 580 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 581 | // Obtain the descriptor from the parent's descriptor. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 582 | if (descriptor_->containing_type() == NULL) { |
| 583 | printer->Print(vars, |
| 584 | "$classname$_descriptor_ = file->message_type($index$);\n"); |
| 585 | } else { |
| 586 | vars["parent"] = ClassName(descriptor_->containing_type(), false); |
| 587 | printer->Print(vars, |
| 588 | "$classname$_descriptor_ = " |
| 589 | "$parent$_descriptor_->nested_type($index$);\n"); |
| 590 | } |
| 591 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 592 | // Construct the default instance. We can't call InitAsDefaultInstance() yet |
| 593 | // because we need to make sure all default instances that this one might |
| 594 | // depend on are constructed first. |
| 595 | printer->Print(vars, |
| 596 | "$classname$::default_instance_ = new $classname$();\n"); |
| 597 | |
kenton@google.com | 3e91fcd | 2008-11-06 04:37:30 +0000 | [diff] [blame] | 598 | // Generate the offsets. |
| 599 | GenerateOffsets(printer); |
| 600 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 601 | // Construct the reflection object. |
| 602 | printer->Print(vars, |
| 603 | "$classname$_reflection_ =\n" |
| 604 | " new ::google::protobuf::internal::GeneratedMessageReflection(\n" |
| 605 | " $classname$_descriptor_,\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 606 | " $classname$::default_instance_,\n" |
kenton@google.com | 3e91fcd | 2008-11-06 04:37:30 +0000 | [diff] [blame] | 607 | " $classname$_offsets_,\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 608 | " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, _has_bits_[0]),\n" |
| 609 | " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" |
| 610 | "$classname$, _unknown_fields_),\n"); |
| 611 | if (descriptor_->extension_range_count() > 0) { |
| 612 | printer->Print(vars, |
| 613 | " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" |
| 614 | "$classname$, _extensions_),\n"); |
| 615 | } else { |
| 616 | // No extensions. |
| 617 | printer->Print(vars, |
| 618 | " -1,\n"); |
| 619 | } |
| 620 | printer->Print(vars, |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 621 | " ::google::protobuf::DescriptorPool::generated_pool(),\n" |
| 622 | " sizeof($classname$));\n"); |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 623 | |
| 624 | // Handle nested types. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 625 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 626 | nested_generators_[i]->GenerateDescriptorInitializer(printer, i); |
| 627 | } |
| 628 | |
| 629 | for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
| 630 | enum_generators_[i]->GenerateDescriptorInitializer(printer, i); |
| 631 | } |
| 632 | |
| 633 | // Register this message type with the message factory. |
| 634 | printer->Print(vars, |
| 635 | "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 636 | " $classname$_descriptor_, $classname$::default_instance_);\n"); |
| 637 | } |
| 638 | |
| 639 | void MessageGenerator:: |
| 640 | GenerateDefaultInstanceInitializer(io::Printer* printer) { |
| 641 | printer->Print( |
| 642 | "$classname$::default_instance_->InitAsDefaultInstance();\n", |
| 643 | "classname", classname_); |
| 644 | |
| 645 | // Handle nested types. |
| 646 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 647 | nested_generators_[i]->GenerateDefaultInstanceInitializer(printer); |
| 648 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void MessageGenerator:: |
| 652 | GenerateClassMethods(io::Printer* printer) { |
| 653 | for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
| 654 | enum_generators_[i]->GenerateMethods(printer); |
| 655 | } |
| 656 | |
| 657 | for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
| 658 | nested_generators_[i]->GenerateClassMethods(printer); |
| 659 | printer->Print("\n"); |
| 660 | printer->Print(kThinSeparator); |
| 661 | printer->Print("\n"); |
| 662 | } |
| 663 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 664 | // Generate non-inline field definitions. |
| 665 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 666 | field_generators_.get(descriptor_->field(i)) |
| 667 | .GenerateNonInlineAccessorDefinitions(printer); |
| 668 | printer->Print("\n"); |
| 669 | } |
| 670 | |
| 671 | // Define extension identifiers. |
| 672 | for (int i = 0; i < descriptor_->extension_count(); i++) { |
| 673 | extension_generators_[i]->GenerateDefinition(printer); |
| 674 | } |
| 675 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 676 | GenerateStructors(printer); |
| 677 | printer->Print("\n"); |
| 678 | |
| 679 | if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) { |
| 680 | GenerateClear(printer); |
| 681 | printer->Print("\n"); |
| 682 | |
| 683 | GenerateMergeFromCodedStream(printer); |
| 684 | printer->Print("\n"); |
| 685 | |
| 686 | GenerateSerializeWithCachedSizes(printer); |
| 687 | printer->Print("\n"); |
| 688 | |
| 689 | GenerateByteSize(printer); |
| 690 | printer->Print("\n"); |
| 691 | |
| 692 | GenerateMergeFrom(printer); |
| 693 | printer->Print("\n"); |
| 694 | |
| 695 | GenerateCopyFrom(printer); |
| 696 | printer->Print("\n"); |
| 697 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 698 | GenerateSwap(printer); |
| 699 | printer->Print("\n"); |
| 700 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 701 | GenerateIsInitialized(printer); |
| 702 | printer->Print("\n"); |
| 703 | } |
| 704 | |
| 705 | printer->Print( |
| 706 | "const ::google::protobuf::Descriptor* $classname$::GetDescriptor() const {\n" |
| 707 | " return descriptor();\n" |
| 708 | "}\n" |
| 709 | "\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 710 | "const ::google::protobuf::Reflection* $classname$::GetReflection() const {\n" |
| 711 | " if ($classname$_reflection_ == NULL) $builddescriptorsname$();\n" |
| 712 | " return $classname$_reflection_;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 713 | "}\n", |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 714 | "classname", classname_, |
| 715 | "builddescriptorsname", |
| 716 | GlobalBuildDescriptorsName(descriptor_->file()->name())); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void MessageGenerator:: |
| 720 | GenerateOffsets(io::Printer* printer) { |
| 721 | printer->Print( |
kenton@google.com | 3e91fcd | 2008-11-06 04:37:30 +0000 | [diff] [blame] | 722 | "static const int $classname$_offsets_[$field_count$] = {\n", |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 723 | "classname", classname_, |
| 724 | "field_count", SimpleItoa(max(1, descriptor_->field_count()))); |
| 725 | printer->Indent(); |
| 726 | |
| 727 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 728 | const FieldDescriptor* field = descriptor_->field(i); |
| 729 | printer->Print( |
| 730 | "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_),\n", |
| 731 | "classname", classname_, |
| 732 | "name", FieldName(field)); |
| 733 | } |
| 734 | |
| 735 | printer->Outdent(); |
| 736 | printer->Print("};\n"); |
| 737 | } |
| 738 | |
| 739 | void MessageGenerator:: |
| 740 | GenerateInitializerList(io::Printer* printer) { |
| 741 | printer->Indent(); |
| 742 | printer->Indent(); |
| 743 | |
kenton@google.com | a69deb6 | 2008-09-23 20:03:24 +0000 | [diff] [blame] | 744 | printer->Print( |
| 745 | "::google::protobuf::Message(),\n"); |
| 746 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 747 | if (descriptor_->extension_range_count() > 0) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 748 | printer->Print( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 749 | "_extensions_(&$classname$_descriptor_,\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 750 | " ::google::protobuf::DescriptorPool::generated_pool(),\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 751 | " ::google::protobuf::MessageFactory::generated_factory()),\n", |
| 752 | "classname", classname_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | printer->Print( |
kenton@google.com | eb26a1e | 2009-04-16 22:43:40 +0000 | [diff] [blame^] | 756 | "_unknown_fields_(),\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 757 | "_cached_size_(0)"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 758 | |
| 759 | // Write the initializers for each field. |
| 760 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 761 | field_generators_.get(descriptor_->field(i)) |
| 762 | .GenerateInitializer(printer); |
| 763 | } |
| 764 | |
| 765 | printer->Outdent(); |
| 766 | printer->Outdent(); |
| 767 | } |
| 768 | |
| 769 | void MessageGenerator:: |
| 770 | GenerateStructors(io::Printer* printer) { |
| 771 | // Generate the default constructor. |
| 772 | printer->Print( |
| 773 | "$classname$::$classname$()\n" |
| 774 | " : ", |
| 775 | "classname", classname_); |
| 776 | GenerateInitializerList(printer); |
| 777 | printer->Print(" {\n" |
| 778 | " ::memset(_has_bits_, 0, sizeof(_has_bits_));\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 779 | "}\n"); |
| 780 | |
| 781 | printer->Print( |
| 782 | "\n" |
| 783 | "void $classname$::InitAsDefaultInstance() {", |
| 784 | "classname", classname_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 785 | |
| 786 | // The default instance needs all of its embedded message pointers |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 787 | // cross-linked to other default instances. We can't do this initialization |
| 788 | // in the constructor because some other default instances may not have been |
| 789 | // constructed yet at that time. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 790 | // TODO(kenton): Maybe all message fields (even for non-default messages) |
| 791 | // should be initialized to point at default instances rather than NULL? |
| 792 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 793 | const FieldDescriptor* field = descriptor_->field(i); |
| 794 | |
| 795 | if (!field->is_repeated() && |
| 796 | field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 797 | printer->Print( |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 798 | " $name$_ = const_cast< $type$*>(&$type$::default_instance());\n", |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 799 | "name", FieldName(field), |
| 800 | "type", ClassName(field->message_type(), true)); |
| 801 | } |
| 802 | } |
| 803 | printer->Print( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 804 | "}\n" |
| 805 | "\n"); |
| 806 | |
| 807 | // Generate the copy constructor. |
| 808 | printer->Print( |
| 809 | "$classname$::$classname$(const $classname$& from)\n" |
| 810 | " : ", |
| 811 | "classname", classname_); |
| 812 | GenerateInitializerList(printer); |
| 813 | printer->Print(" {\n" |
| 814 | " ::memset(_has_bits_, 0, sizeof(_has_bits_));\n" |
| 815 | " MergeFrom(from);\n" |
| 816 | "}\n" |
| 817 | "\n"); |
| 818 | |
| 819 | // Generate the destructor. |
| 820 | printer->Print( |
| 821 | "$classname$::~$classname$() {\n", |
| 822 | "classname", classname_); |
| 823 | |
| 824 | printer->Indent(); |
| 825 | |
| 826 | // Write the destructors for each field. |
| 827 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 828 | field_generators_.get(descriptor_->field(i)) |
| 829 | .GenerateDestructorCode(printer); |
| 830 | } |
| 831 | |
| 832 | printer->Print( |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 833 | "if (this != default_instance_) {\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 834 | |
| 835 | // We need to delete all embedded messages. |
| 836 | // TODO(kenton): If we make unset messages point at default instances |
| 837 | // instead of NULL, then it would make sense to move this code into |
| 838 | // MessageFieldGenerator::GenerateDestructorCode(). |
| 839 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 840 | const FieldDescriptor* field = descriptor_->field(i); |
| 841 | |
| 842 | if (!field->is_repeated() && |
| 843 | field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 844 | printer->Print(" delete $name$_;\n", |
| 845 | "name", FieldName(field)); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | printer->Outdent(); |
| 850 | |
| 851 | printer->Print( |
| 852 | " }\n" |
| 853 | "}\n" |
| 854 | "\n" |
| 855 | "const ::google::protobuf::Descriptor* $classname$::descriptor() {\n" |
| 856 | " if ($classname$_descriptor_ == NULL) $builddescriptorsname$();\n" |
| 857 | " return $classname$_descriptor_;\n" |
| 858 | "}\n" |
| 859 | "\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 860 | "const $classname$& $classname$::default_instance() {\n" |
| 861 | " if (default_instance_ == NULL) $builddescriptorsname$();\n" |
| 862 | " return *default_instance_;\n" |
| 863 | "}\n" |
| 864 | "\n" |
| 865 | "$classname$* $classname$::default_instance_ = NULL;\n" |
| 866 | "\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 867 | "$classname$* $classname$::New() const {\n" |
| 868 | " return new $classname$;\n" |
| 869 | "}\n", |
| 870 | "classname", classname_, |
| 871 | "builddescriptorsname", |
| 872 | GlobalBuildDescriptorsName(descriptor_->file()->name())); |
| 873 | } |
| 874 | |
| 875 | void MessageGenerator:: |
| 876 | GenerateClear(io::Printer* printer) { |
| 877 | printer->Print("void $classname$::Clear() {\n", |
| 878 | "classname", classname_); |
| 879 | printer->Indent(); |
| 880 | |
| 881 | int last_index = -1; |
| 882 | |
| 883 | if (descriptor_->extension_range_count() > 0) { |
| 884 | printer->Print("_extensions_.Clear();\n"); |
| 885 | } |
| 886 | |
| 887 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 888 | const FieldDescriptor* field = descriptor_->field(i); |
| 889 | |
| 890 | if (!field->is_repeated()) { |
| 891 | map<string, string> vars; |
| 892 | vars["index"] = SimpleItoa(field->index()); |
| 893 | |
| 894 | // We can use the fact that _has_bits_ is a giant bitfield to our |
| 895 | // advantage: We can check up to 32 bits at a time for equality to |
| 896 | // zero, and skip the whole range if so. This can improve the speed |
| 897 | // of Clear() for messages which contain a very large number of |
| 898 | // optional fields of which only a few are used at a time. Here, |
| 899 | // we've chosen to check 8 bits at a time rather than 32. |
| 900 | if (i / 8 != last_index / 8 || last_index < 0) { |
| 901 | if (last_index >= 0) { |
| 902 | printer->Outdent(); |
| 903 | printer->Print("}\n"); |
| 904 | } |
| 905 | printer->Print(vars, |
| 906 | "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n"); |
| 907 | printer->Indent(); |
| 908 | } |
| 909 | last_index = i; |
| 910 | |
| 911 | // It's faster to just overwrite primitive types, but we should |
| 912 | // only clear strings and messages if they were set. |
| 913 | // TODO(kenton): Let the CppFieldGenerator decide this somehow. |
| 914 | bool should_check_bit = |
| 915 | field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE || |
| 916 | field->cpp_type() == FieldDescriptor::CPPTYPE_STRING; |
| 917 | |
| 918 | if (should_check_bit) { |
| 919 | printer->Print(vars, "if (_has_bit($index$)) {\n"); |
| 920 | printer->Indent(); |
| 921 | } |
| 922 | |
| 923 | field_generators_.get(field).GenerateClearingCode(printer); |
| 924 | |
| 925 | if (should_check_bit) { |
| 926 | printer->Outdent(); |
| 927 | printer->Print("}\n"); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | if (last_index >= 0) { |
| 933 | printer->Outdent(); |
| 934 | printer->Print("}\n"); |
| 935 | } |
| 936 | |
| 937 | // Repeated fields don't use _has_bits_ so we clear them in a separate |
| 938 | // pass. |
| 939 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 940 | const FieldDescriptor* field = descriptor_->field(i); |
| 941 | |
| 942 | if (field->is_repeated()) { |
| 943 | field_generators_.get(field).GenerateClearingCode(printer); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | printer->Print( |
| 948 | "::memset(_has_bits_, 0, sizeof(_has_bits_));\n" |
| 949 | "mutable_unknown_fields()->Clear();\n"); |
| 950 | |
| 951 | printer->Outdent(); |
| 952 | printer->Print("}\n"); |
| 953 | } |
| 954 | |
| 955 | void MessageGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 956 | GenerateSwap(io::Printer* printer) { |
| 957 | // Generate the Swap member function. |
| 958 | printer->Print("void $classname$::Swap($classname$* other) {\n", |
| 959 | "classname", classname_); |
| 960 | printer->Indent(); |
| 961 | printer->Print("if (other != this) {\n"); |
| 962 | printer->Indent(); |
| 963 | |
| 964 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 965 | const FieldDescriptor* field = descriptor_->field(i); |
| 966 | field_generators_.get(field).GenerateSwappingCode(printer); |
| 967 | } |
| 968 | |
| 969 | for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) { |
| 970 | printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n", |
| 971 | "i", SimpleItoa(i)); |
| 972 | } |
| 973 | |
| 974 | printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n"); |
| 975 | printer->Print("std::swap(_cached_size_, other->_cached_size_);\n"); |
| 976 | if (descriptor_->extension_range_count() > 0) { |
| 977 | printer->Print("_extensions_.Swap(&other->_extensions_);\n"); |
| 978 | } |
| 979 | |
| 980 | printer->Outdent(); |
| 981 | printer->Print("}\n"); |
| 982 | printer->Outdent(); |
| 983 | printer->Print("}\n"); |
| 984 | } |
| 985 | |
| 986 | void MessageGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 987 | GenerateMergeFrom(io::Printer* printer) { |
| 988 | // Generate the generalized MergeFrom (aka that which takes in the Message |
| 989 | // base class as a parameter). |
| 990 | printer->Print( |
| 991 | "void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n" |
| 992 | " GOOGLE_CHECK_NE(&from, this);\n", |
| 993 | "classname", classname_); |
| 994 | printer->Indent(); |
| 995 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 996 | // Cast the message to the proper type. If we find that the message is |
| 997 | // *not* of the proper type, we can still call Merge via the reflection |
| 998 | // system, as the GOOGLE_CHECK above ensured that we have the same descriptor |
| 999 | // for each message. |
| 1000 | printer->Print( |
| 1001 | "const $classname$* source =\n" |
| 1002 | " ::google::protobuf::internal::dynamic_cast_if_available<const $classname$*>(\n" |
| 1003 | " &from);\n" |
| 1004 | "if (source == NULL) {\n" |
| 1005 | " ::google::protobuf::internal::ReflectionOps::Merge(from, this);\n" |
| 1006 | "} else {\n" |
| 1007 | " MergeFrom(*source);\n" |
| 1008 | "}\n", |
| 1009 | "classname", classname_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1010 | |
| 1011 | printer->Outdent(); |
| 1012 | printer->Print("}\n\n"); |
| 1013 | |
| 1014 | // Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast. |
| 1015 | printer->Print( |
| 1016 | "void $classname$::MergeFrom(const $classname$& from) {\n" |
| 1017 | " GOOGLE_CHECK_NE(&from, this);\n", |
| 1018 | "classname", classname_); |
| 1019 | printer->Indent(); |
| 1020 | |
| 1021 | // Merge Repeated fields. These fields do not require a |
| 1022 | // check as we can simply iterate over them. |
| 1023 | for (int i = 0; i < descriptor_->field_count(); ++i) { |
| 1024 | const FieldDescriptor* field = descriptor_->field(i); |
| 1025 | |
| 1026 | if (field->is_repeated()) { |
| 1027 | field_generators_.get(field).GenerateMergingCode(printer); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // Merge Optional and Required fields (after a _has_bit check). |
| 1032 | int last_index = -1; |
| 1033 | |
| 1034 | for (int i = 0; i < descriptor_->field_count(); ++i) { |
| 1035 | const FieldDescriptor* field = descriptor_->field(i); |
| 1036 | |
| 1037 | if (!field->is_repeated()) { |
| 1038 | map<string, string> vars; |
| 1039 | vars["index"] = SimpleItoa(field->index()); |
| 1040 | |
| 1041 | // See above in GenerateClear for an explanation of this. |
| 1042 | if (i / 8 != last_index / 8 || last_index < 0) { |
| 1043 | if (last_index >= 0) { |
| 1044 | printer->Outdent(); |
| 1045 | printer->Print("}\n"); |
| 1046 | } |
| 1047 | printer->Print(vars, |
| 1048 | "if (from._has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n"); |
| 1049 | printer->Indent(); |
| 1050 | } |
| 1051 | |
| 1052 | last_index = i; |
| 1053 | |
| 1054 | printer->Print(vars, |
| 1055 | "if (from._has_bit($index$)) {\n"); |
| 1056 | printer->Indent(); |
| 1057 | |
| 1058 | field_generators_.get(field).GenerateMergingCode(printer); |
| 1059 | |
| 1060 | printer->Outdent(); |
| 1061 | printer->Print("}\n"); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | if (last_index >= 0) { |
| 1066 | printer->Outdent(); |
| 1067 | printer->Print("}\n"); |
| 1068 | } |
| 1069 | |
| 1070 | if (descriptor_->extension_range_count() > 0) { |
| 1071 | printer->Print("_extensions_.MergeFrom(from._extensions_);\n"); |
| 1072 | } |
| 1073 | |
| 1074 | printer->Print( |
| 1075 | "mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n"); |
| 1076 | |
| 1077 | printer->Outdent(); |
| 1078 | printer->Print("}\n"); |
| 1079 | } |
| 1080 | |
| 1081 | void MessageGenerator:: |
| 1082 | GenerateCopyFrom(io::Printer* printer) { |
| 1083 | // Generate the generalized CopyFrom (aka that which takes in the Message |
| 1084 | // base class as a parameter). |
| 1085 | printer->Print( |
| 1086 | "void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n", |
| 1087 | "classname", classname_); |
| 1088 | printer->Indent(); |
| 1089 | |
| 1090 | printer->Print( |
| 1091 | "if (&from == this) return;\n" |
| 1092 | "Clear();\n" |
| 1093 | "MergeFrom(from);\n"); |
| 1094 | |
| 1095 | printer->Outdent(); |
| 1096 | printer->Print("}\n\n"); |
| 1097 | |
| 1098 | // Generate the class-specific CopyFrom. |
| 1099 | printer->Print( |
| 1100 | "void $classname$::CopyFrom(const $classname$& from) {\n", |
| 1101 | "classname", classname_); |
| 1102 | printer->Indent(); |
| 1103 | |
| 1104 | printer->Print( |
| 1105 | "if (&from == this) return;\n" |
| 1106 | "Clear();\n" |
| 1107 | "MergeFrom(from);\n"); |
| 1108 | |
| 1109 | printer->Outdent(); |
| 1110 | printer->Print("}\n"); |
| 1111 | } |
| 1112 | |
| 1113 | void MessageGenerator:: |
| 1114 | GenerateMergeFromCodedStream(io::Printer* printer) { |
| 1115 | if (descriptor_->options().message_set_wire_format()) { |
| 1116 | // For message_set_wire_format, we don't generate a parser, for two |
| 1117 | // reasons: |
| 1118 | // - WireFormat already needs to special-case this, and we'd like to |
| 1119 | // avoid having multiple implementations of MessageSet wire format |
| 1120 | // lying around the code base. |
| 1121 | // - All fields are extensions, and extension parsing falls back to |
| 1122 | // reflection anyway, so it wouldn't be any faster. |
| 1123 | printer->Print( |
| 1124 | "bool $classname$::MergePartialFromCodedStream(\n" |
| 1125 | " ::google::protobuf::io::CodedInputStream* input) {\n" |
| 1126 | " return ::google::protobuf::internal::WireFormat::ParseAndMergePartial(\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1127 | " input, this);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1128 | "}\n", |
| 1129 | "classname", classname_); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | printer->Print( |
| 1134 | "bool $classname$::MergePartialFromCodedStream(\n" |
| 1135 | " ::google::protobuf::io::CodedInputStream* input) {\n" |
| 1136 | "#define DO_(EXPRESSION) if (!(EXPRESSION)) return false\n" |
| 1137 | " ::google::protobuf::uint32 tag;\n" |
| 1138 | " while ((tag = input->ReadTag()) != 0) {\n", |
| 1139 | "classname", classname_); |
| 1140 | |
| 1141 | printer->Indent(); |
| 1142 | printer->Indent(); |
| 1143 | |
| 1144 | if (descriptor_->field_count() > 0) { |
| 1145 | // We don't even want to print the switch() if we have no fields because |
| 1146 | // MSVC dislikes switch() statements that contain only a default value. |
| 1147 | |
| 1148 | // Note: If we just switched on the tag rather than the field number, we |
| 1149 | // could avoid the need for the if() to check the wire type at the beginning |
| 1150 | // of each case. However, this is actually a bit slower in practice as it |
| 1151 | // creates a jump table that is 8x larger and sparser, and meanwhile the |
| 1152 | // if()s are highly predictable. |
| 1153 | printer->Print( |
| 1154 | "switch (::google::protobuf::internal::WireFormat::GetTagFieldNumber(tag)) {\n"); |
| 1155 | |
| 1156 | printer->Indent(); |
| 1157 | |
| 1158 | scoped_array<const FieldDescriptor*> ordered_fields( |
| 1159 | SortFieldsByNumber(descriptor_)); |
| 1160 | |
| 1161 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 1162 | const FieldDescriptor* field = ordered_fields[i]; |
| 1163 | |
| 1164 | PrintFieldComment(printer, field); |
| 1165 | |
| 1166 | printer->Print( |
| 1167 | "case $number$: {\n" |
| 1168 | " if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) !=\n" |
| 1169 | " ::google::protobuf::internal::WireFormat::WIRETYPE_$wiretype$) {\n" |
| 1170 | " goto handle_uninterpreted;\n" |
| 1171 | " }\n", |
| 1172 | "number", SimpleItoa(field->number()), |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1173 | "wiretype", kWireTypeNames[WireFormat::WireTypeForField(field)]); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1174 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1175 | if (i > 0 || (field->is_repeated() && !field->options().packed())) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1176 | printer->Print( |
| 1177 | " parse_$name$:\n", |
| 1178 | "name", field->name()); |
| 1179 | } |
| 1180 | |
| 1181 | printer->Indent(); |
| 1182 | |
| 1183 | field_generators_.get(field).GenerateMergeFromCodedStream(printer); |
| 1184 | |
| 1185 | // switch() is slow since it can't be predicted well. Insert some if()s |
| 1186 | // here that attempt to predict the next tag. |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1187 | if (field->is_repeated() && !field->options().packed()) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1188 | // Expect repeats of this field. |
| 1189 | printer->Print( |
| 1190 | "if (input->ExpectTag($tag$)) goto parse_$name$;\n", |
| 1191 | "tag", SimpleItoa(WireFormat::MakeTag(field)), |
| 1192 | "name", field->name()); |
| 1193 | } |
| 1194 | |
| 1195 | if (i + 1 < descriptor_->field_count()) { |
| 1196 | // Expect the next field in order. |
| 1197 | const FieldDescriptor* next_field = ordered_fields[i + 1]; |
| 1198 | printer->Print( |
| 1199 | "if (input->ExpectTag($next_tag$)) goto parse_$next_name$;\n", |
| 1200 | "next_tag", SimpleItoa(WireFormat::MakeTag(next_field)), |
| 1201 | "next_name", next_field->name()); |
| 1202 | } else { |
| 1203 | // Expect EOF. |
| 1204 | // TODO(kenton): Expect group end-tag? |
| 1205 | printer->Print( |
| 1206 | "if (input->ExpectAtEnd()) return true;\n"); |
| 1207 | } |
| 1208 | |
| 1209 | printer->Print( |
| 1210 | "break;\n"); |
| 1211 | |
| 1212 | printer->Outdent(); |
| 1213 | printer->Print("}\n\n"); |
| 1214 | } |
| 1215 | |
| 1216 | printer->Print( |
| 1217 | "default: {\n" |
| 1218 | "handle_uninterpreted:\n"); |
| 1219 | printer->Indent(); |
| 1220 | } |
| 1221 | |
| 1222 | // Is this an end-group tag? If so, this must be the end of the message. |
| 1223 | printer->Print( |
| 1224 | "if (::google::protobuf::internal::WireFormat::GetTagWireType(tag) ==\n" |
| 1225 | " ::google::protobuf::internal::WireFormat::WIRETYPE_END_GROUP) {\n" |
| 1226 | " return true;\n" |
| 1227 | "}\n"); |
| 1228 | |
| 1229 | // Handle extension ranges. |
| 1230 | if (descriptor_->extension_range_count() > 0) { |
| 1231 | printer->Print( |
| 1232 | "if ("); |
| 1233 | for (int i = 0; i < descriptor_->extension_range_count(); i++) { |
| 1234 | const Descriptor::ExtensionRange* range = |
| 1235 | descriptor_->extension_range(i); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 1236 | if (i > 0) printer->Print(" ||\n "); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1237 | |
| 1238 | uint32 start_tag = WireFormat::MakeTag( |
| 1239 | range->start, static_cast<WireFormat::WireType>(0)); |
| 1240 | uint32 end_tag = WireFormat::MakeTag( |
| 1241 | range->end, static_cast<WireFormat::WireType>(0)); |
| 1242 | |
| 1243 | if (range->end > FieldDescriptor::kMaxNumber) { |
| 1244 | printer->Print( |
| 1245 | "($start$u <= tag)", |
| 1246 | "start", SimpleItoa(start_tag)); |
| 1247 | } else { |
| 1248 | printer->Print( |
| 1249 | "($start$u <= tag && tag < $end$u)", |
| 1250 | "start", SimpleItoa(start_tag), |
| 1251 | "end", SimpleItoa(end_tag)); |
| 1252 | } |
| 1253 | } |
| 1254 | printer->Print(") {\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1255 | " DO_(_extensions_.ParseField(tag, input, this));\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1256 | " continue;\n" |
| 1257 | "}\n"); |
| 1258 | } |
| 1259 | |
| 1260 | // We really don't recognize this tag. Skip it. |
| 1261 | printer->Print( |
| 1262 | "DO_(::google::protobuf::internal::WireFormat::SkipField(\n" |
| 1263 | " input, tag, mutable_unknown_fields()));\n"); |
| 1264 | |
| 1265 | if (descriptor_->field_count() > 0) { |
| 1266 | printer->Print("break;\n"); |
| 1267 | printer->Outdent(); |
| 1268 | printer->Print("}\n"); // default: |
| 1269 | printer->Outdent(); |
| 1270 | printer->Print("}\n"); // switch |
| 1271 | } |
| 1272 | |
| 1273 | printer->Outdent(); |
| 1274 | printer->Outdent(); |
| 1275 | printer->Print( |
| 1276 | " }\n" // while |
| 1277 | " return true;\n" |
| 1278 | "#undef DO_\n" |
| 1279 | "}\n"); |
| 1280 | } |
| 1281 | |
| 1282 | void MessageGenerator::GenerateSerializeOneField( |
| 1283 | io::Printer* printer, const FieldDescriptor* field) { |
| 1284 | PrintFieldComment(printer, field); |
| 1285 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1286 | if (!field->is_repeated()) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1287 | printer->Print( |
| 1288 | "if (_has_bit($index$)) {\n", |
| 1289 | "index", SimpleItoa(field->index())); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1290 | printer->Indent(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1293 | field_generators_.get(field).GenerateSerializeWithCachedSizes(printer); |
| 1294 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1295 | if (!field->is_repeated()) { |
| 1296 | printer->Outdent(); |
| 1297 | printer->Print("}\n"); |
| 1298 | } |
| 1299 | printer->Print("\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | void MessageGenerator::GenerateSerializeOneExtensionRange( |
| 1303 | io::Printer* printer, const Descriptor::ExtensionRange* range) { |
| 1304 | map<string, string> vars; |
| 1305 | vars["start"] = SimpleItoa(range->start); |
| 1306 | vars["end"] = SimpleItoa(range->end); |
| 1307 | printer->Print(vars, |
| 1308 | "// Extension range [$start$, $end$)\n" |
| 1309 | "DO_(_extensions_.SerializeWithCachedSizes(\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1310 | " $start$, $end$, *this, output));\n\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void MessageGenerator:: |
| 1314 | GenerateSerializeWithCachedSizes(io::Printer* printer) { |
| 1315 | printer->Print( |
| 1316 | "bool $classname$::SerializeWithCachedSizes(\n" |
| 1317 | " ::google::protobuf::io::CodedOutputStream* output) const {\n" |
| 1318 | "#define DO_(EXPRESSION) if (!(EXPRESSION)) return false\n", |
| 1319 | "classname", classname_); |
| 1320 | printer->Indent(); |
| 1321 | |
| 1322 | scoped_array<const FieldDescriptor*> ordered_fields( |
| 1323 | SortFieldsByNumber(descriptor_)); |
| 1324 | |
| 1325 | vector<const Descriptor::ExtensionRange*> sorted_extensions; |
| 1326 | for (int i = 0; i < descriptor_->extension_range_count(); ++i) { |
| 1327 | sorted_extensions.push_back(descriptor_->extension_range(i)); |
| 1328 | } |
| 1329 | sort(sorted_extensions.begin(), sorted_extensions.end(), |
| 1330 | ExtensionRangeSorter()); |
| 1331 | |
| 1332 | // Merge the fields and the extension ranges, both sorted by field number. |
| 1333 | int i, j; |
| 1334 | for (i = 0, j = 0; |
| 1335 | i < descriptor_->field_count() || j < sorted_extensions.size(); |
| 1336 | ) { |
| 1337 | if (i == descriptor_->field_count()) { |
| 1338 | GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); |
| 1339 | } else if (j == sorted_extensions.size()) { |
| 1340 | GenerateSerializeOneField(printer, ordered_fields[i++]); |
| 1341 | } else if (ordered_fields[i]->number() < sorted_extensions[j]->start) { |
| 1342 | GenerateSerializeOneField(printer, ordered_fields[i++]); |
| 1343 | } else { |
| 1344 | GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | printer->Print("if (!unknown_fields().empty()) {\n"); |
| 1349 | printer->Indent(); |
| 1350 | if (descriptor_->options().message_set_wire_format()) { |
| 1351 | printer->Print( |
| 1352 | "DO_(::google::protobuf::internal::WireFormat::SerializeUnknownMessageSetItems(\n" |
| 1353 | " unknown_fields(), output));\n"); |
| 1354 | } else { |
| 1355 | printer->Print( |
| 1356 | "DO_(::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n" |
| 1357 | " unknown_fields(), output));\n"); |
| 1358 | } |
| 1359 | printer->Outdent(); |
| 1360 | printer->Print( |
| 1361 | "}\n" |
| 1362 | "return true;\n"); |
| 1363 | |
| 1364 | printer->Outdent(); |
| 1365 | printer->Print( |
| 1366 | "#undef DO_\n" |
| 1367 | "}\n"); |
| 1368 | } |
| 1369 | |
| 1370 | void MessageGenerator:: |
| 1371 | GenerateByteSize(io::Printer* printer) { |
| 1372 | printer->Print( |
| 1373 | "int $classname$::ByteSize() const {\n", |
| 1374 | "classname", classname_); |
| 1375 | printer->Indent(); |
| 1376 | printer->Print( |
| 1377 | "int total_size = 0;\n" |
| 1378 | "\n"); |
| 1379 | |
| 1380 | int last_index = -1; |
| 1381 | |
| 1382 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 1383 | const FieldDescriptor* field = descriptor_->field(i); |
| 1384 | |
| 1385 | if (!field->is_repeated()) { |
| 1386 | // See above in GenerateClear for an explanation of this. |
| 1387 | // TODO(kenton): Share code? Unclear how to do so without |
| 1388 | // over-engineering. |
| 1389 | if ((i / 8) != (last_index / 8) || |
| 1390 | last_index < 0) { |
| 1391 | if (last_index >= 0) { |
| 1392 | printer->Outdent(); |
| 1393 | printer->Print("}\n"); |
| 1394 | } |
| 1395 | printer->Print( |
| 1396 | "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n", |
| 1397 | "index", SimpleItoa(field->index())); |
| 1398 | printer->Indent(); |
| 1399 | } |
| 1400 | last_index = i; |
| 1401 | |
| 1402 | PrintFieldComment(printer, field); |
| 1403 | |
| 1404 | printer->Print( |
| 1405 | "if (has_$name$()) {\n", |
| 1406 | "name", FieldName(field)); |
| 1407 | printer->Indent(); |
| 1408 | |
| 1409 | field_generators_.get(field).GenerateByteSize(printer); |
| 1410 | |
| 1411 | printer->Outdent(); |
| 1412 | printer->Print( |
| 1413 | "}\n" |
| 1414 | "\n"); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | if (last_index >= 0) { |
| 1419 | printer->Outdent(); |
| 1420 | printer->Print("}\n"); |
| 1421 | } |
| 1422 | |
| 1423 | // Repeated fields don't use _has_bits_ so we count them in a separate |
| 1424 | // pass. |
| 1425 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 1426 | const FieldDescriptor* field = descriptor_->field(i); |
| 1427 | |
| 1428 | if (field->is_repeated()) { |
| 1429 | PrintFieldComment(printer, field); |
| 1430 | field_generators_.get(field).GenerateByteSize(printer); |
| 1431 | printer->Print("\n"); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | if (descriptor_->extension_range_count() > 0) { |
| 1436 | printer->Print( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1437 | "total_size += _extensions_.ByteSize(*this);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1438 | "\n"); |
| 1439 | } |
| 1440 | |
| 1441 | printer->Print("if (!unknown_fields().empty()) {\n"); |
| 1442 | printer->Indent(); |
| 1443 | if (descriptor_->options().message_set_wire_format()) { |
| 1444 | printer->Print( |
| 1445 | "total_size +=\n" |
| 1446 | " ::google::protobuf::internal::WireFormat::ComputeUnknownMessageSetItemsSize(\n" |
| 1447 | " unknown_fields());\n"); |
| 1448 | } else { |
| 1449 | printer->Print( |
| 1450 | "total_size +=\n" |
| 1451 | " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n" |
| 1452 | " unknown_fields());\n"); |
| 1453 | } |
| 1454 | printer->Outdent(); |
| 1455 | printer->Print("}\n"); |
| 1456 | |
| 1457 | // We update _cached_size_ even though this is a const method. In theory, |
| 1458 | // this is not thread-compatible, because concurrent writes have undefined |
| 1459 | // results. In practice, since any concurrent writes will be writing the |
| 1460 | // exact same value, it works on all common processors. In a future version |
| 1461 | // of C++, _cached_size_ should be made into an atomic<int>. |
| 1462 | printer->Print( |
| 1463 | "_cached_size_ = total_size;\n" |
| 1464 | "return total_size;\n"); |
| 1465 | |
| 1466 | printer->Outdent(); |
| 1467 | printer->Print("}\n"); |
| 1468 | } |
| 1469 | |
| 1470 | void MessageGenerator:: |
| 1471 | GenerateIsInitialized(io::Printer* printer) { |
| 1472 | printer->Print( |
| 1473 | "bool $classname$::IsInitialized() const {\n", |
| 1474 | "classname", classname_); |
| 1475 | printer->Indent(); |
| 1476 | |
| 1477 | // Check that all required fields in this message are set. We can do this |
| 1478 | // most efficiently by checking 32 "has bits" at a time. |
| 1479 | int has_bits_array_size = (descriptor_->field_count() + 31) / 32; |
| 1480 | for (int i = 0; i < has_bits_array_size; i++) { |
| 1481 | uint32 mask = 0; |
| 1482 | for (int bit = 0; bit < 32; bit++) { |
| 1483 | int index = i * 32 + bit; |
| 1484 | if (index >= descriptor_->field_count()) break; |
| 1485 | const FieldDescriptor* field = descriptor_->field(index); |
| 1486 | |
| 1487 | if (field->is_required()) { |
| 1488 | mask |= 1 << bit; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | if (mask != 0) { |
| 1493 | char buffer[kFastToBufferSize]; |
| 1494 | printer->Print( |
| 1495 | "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n", |
| 1496 | "i", SimpleItoa(i), |
| 1497 | "mask", FastHex32ToBuffer(mask, buffer)); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // Now check that all embedded messages are initialized. |
| 1502 | printer->Print("\n"); |
| 1503 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 1504 | const FieldDescriptor* field = descriptor_->field(i); |
| 1505 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && |
| 1506 | HasRequiredFields(field->message_type())) { |
| 1507 | if (field->is_repeated()) { |
| 1508 | printer->Print( |
| 1509 | "for (int i = 0; i < $name$_size(); i++) {\n" |
| 1510 | " if (!this->$name$(i).IsInitialized()) return false;\n" |
| 1511 | "}\n", |
| 1512 | "name", FieldName(field)); |
| 1513 | } else { |
| 1514 | printer->Print( |
| 1515 | "if (has_$name$()) {\n" |
| 1516 | " if (!this->$name$().IsInitialized()) return false;\n" |
| 1517 | "}\n", |
| 1518 | "name", FieldName(field)); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | if (descriptor_->extension_range_count() > 0) { |
| 1524 | printer->Print( |
| 1525 | "\n" |
| 1526 | "if (!_extensions_.IsInitialized()) return false;"); |
| 1527 | } |
| 1528 | |
| 1529 | printer->Outdent(); |
| 1530 | printer->Print( |
| 1531 | " return true;\n" |
| 1532 | "}\n"); |
| 1533 | } |
| 1534 | |
| 1535 | } // namespace cpp |
| 1536 | } // namespace compiler |
| 1537 | } // namespace protobuf |
| 1538 | } // namespace google |