aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/cpp/cpp_message.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_message.cc')
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_message.cc4506
1 files changed, 2563 insertions, 1943 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc
index 8304ebbd..778fc406 100644
--- a/src/google/protobuf/compiler/cpp/cpp_message.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_message.cc
@@ -32,25 +32,30 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
+#include <google/protobuf/compiler/cpp/cpp_message.h>
+
#include <algorithm>
#include <google/protobuf/stubs/hash.h>
#include <map>
#include <memory>
-#ifndef _SHARED_PTR_H
-#include <google/protobuf/stubs/shared_ptr.h>
-#endif
#include <utility>
#include <vector>
-#include <google/protobuf/compiler/cpp/cpp_message.h>
-#include <google/protobuf/compiler/cpp/cpp_field.h>
+
#include <google/protobuf/compiler/cpp/cpp_enum.h>
#include <google/protobuf/compiler/cpp/cpp_extension.h>
+#include <google/protobuf/compiler/cpp/cpp_field.h>
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
-#include <google/protobuf/stubs/strutil.h>
-#include <google/protobuf/io/printer.h>
+#include <google/protobuf/compiler/cpp/cpp_padding_optimizer.h>
+#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/generated_message_table_driven.h>
+#include <google/protobuf/generated_message_util.h>
+#include <google/protobuf/map_entry_lite.h>
#include <google/protobuf/wire_format.h>
-#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/stubs/substitute.h>
+
namespace google {
@@ -85,14 +90,13 @@ struct FieldOrderingByNumber {
// Sort the fields of the given Descriptor by number into a new[]'d array
// and return it.
-const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
- const FieldDescriptor** fields =
- new const FieldDescriptor*[descriptor->field_count()];
+std::vector<const FieldDescriptor*> SortFieldsByNumber(
+ const Descriptor* descriptor) {
+ std::vector<const FieldDescriptor*> fields(descriptor->field_count());
for (int i = 0; i < descriptor->field_count(); i++) {
fields[i] = descriptor->field(i);
}
- std::sort(fields, fields + descriptor->field_count(),
- FieldOrderingByNumber());
+ std::sort(fields.begin(), fields.end(), FieldOrderingByNumber());
return fields;
}
@@ -104,188 +108,37 @@ struct ExtensionRangeSorter {
}
};
-// Returns true if the "required" restriction check should be ignored for the
-// given field.
-inline static bool ShouldIgnoreRequiredFieldCheck(
- const FieldDescriptor* field) {
- return false;
-}
-
-// Returns true if the message type has any required fields. If it doesn't,
-// we can optimize out calls to its IsInitialized() method.
-//
-// already_seen is used to avoid checking the same type multiple times
-// (and also to protect against recursion).
-static bool HasRequiredFields(
- const Descriptor* type,
- hash_set<const Descriptor*>* already_seen) {
- if (already_seen->count(type) > 0) {
- // Since the first occurrence of a required field causes the whole
- // function to return true, we can assume that if the type is already
- // in the cache it didn't have any required fields.
- return false;
- }
- already_seen->insert(type);
-
- // If the type has extensions, an extension with message type could contain
- // required fields, so we have to be conservative and assume such an
- // extension exists.
- if (type->extension_range_count() > 0) return true;
-
- for (int i = 0; i < type->field_count(); i++) {
- const FieldDescriptor* field = type->field(i);
- if (field->is_required()) {
- return true;
- }
- if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
- !ShouldIgnoreRequiredFieldCheck(field)) {
- if (HasRequiredFields(field->message_type(), already_seen)) {
- return true;
- }
- }
- }
-
- return false;
-}
-
-static bool HasRequiredFields(const Descriptor* type) {
- hash_set<const Descriptor*> already_seen;
- return HasRequiredFields(type, &already_seen);
-}
-
-// This returns an estimate of the compiler's alignment for the field. This
-// can't guarantee to be correct because the generated code could be compiled on
-// different systems with different alignment rules. The estimates below assume
-// 64-bit pointers.
-int EstimateAlignmentSize(const FieldDescriptor* field) {
- if (field == NULL) return 0;
- if (field->is_repeated()) return 8;
+bool IsPOD(const FieldDescriptor* field) {
+ if (field->is_repeated() || field->is_extension()) return false;
switch (field->cpp_type()) {
- case FieldDescriptor::CPPTYPE_BOOL:
- return 1;
-
- case FieldDescriptor::CPPTYPE_INT32:
- case FieldDescriptor::CPPTYPE_UINT32:
case FieldDescriptor::CPPTYPE_ENUM:
- case FieldDescriptor::CPPTYPE_FLOAT:
- return 4;
-
+ case FieldDescriptor::CPPTYPE_INT32:
case FieldDescriptor::CPPTYPE_INT64:
+ case FieldDescriptor::CPPTYPE_UINT32:
case FieldDescriptor::CPPTYPE_UINT64:
+ case FieldDescriptor::CPPTYPE_FLOAT:
case FieldDescriptor::CPPTYPE_DOUBLE:
+ case FieldDescriptor::CPPTYPE_BOOL:
+ return true;
case FieldDescriptor::CPPTYPE_STRING:
- case FieldDescriptor::CPPTYPE_MESSAGE:
- return 8;
- }
- GOOGLE_LOG(FATAL) << "Can't get here.";
- return -1; // Make compiler happy.
-}
-
-// FieldGroup is just a helper for OptimizePadding below. It holds a vector of
-// fields that are grouped together because they have compatible alignment, and
-// a preferred location in the final field ordering.
-class FieldGroup {
- public:
- FieldGroup()
- : preferred_location_(0) {}
-
- // A group with a single field.
- FieldGroup(float preferred_location, const FieldDescriptor* field)
- : preferred_location_(preferred_location),
- fields_(1, field) {}
-
- // Append the fields in 'other' to this group.
- void Append(const FieldGroup& other) {
- if (other.fields_.empty()) {
- return;
- }
- // Preferred location is the average among all the fields, so we weight by
- // the number of fields on each FieldGroup object.
- preferred_location_ =
- (preferred_location_ * fields_.size() +
- (other.preferred_location_ * other.fields_.size())) /
- (fields_.size() + other.fields_.size());
- fields_.insert(fields_.end(), other.fields_.begin(), other.fields_.end());
- }
-
- void SetPreferredLocation(float location) { preferred_location_ = location; }
- const vector<const FieldDescriptor*>& fields() const { return fields_; }
-
- // FieldGroup objects sort by their preferred location.
- bool operator<(const FieldGroup& other) const {
- return preferred_location_ < other.preferred_location_;
- }
-
- private:
- // "preferred_location_" is an estimate of where this group should go in the
- // final list of fields. We compute this by taking the average index of each
- // field in this group in the original ordering of fields. This is very
- // approximate, but should put this group close to where its member fields
- // originally went.
- float preferred_location_;
- vector<const FieldDescriptor*> fields_;
- // We rely on the default copy constructor and operator= so this type can be
- // used in a vector.
-};
-
-// Reorder 'fields' so that if the fields are output into a c++ class in the new
-// order, the alignment padding is minimized. We try to do this while keeping
-// each field as close as possible to its original position so that we don't
-// reduce cache locality much for function that access each field in order.
-void OptimizePadding(vector<const FieldDescriptor*>* fields) {
- // First divide fields into those that align to 1 byte, 4 bytes or 8 bytes.
- vector<FieldGroup> aligned_to_1, aligned_to_4, aligned_to_8;
- for (int i = 0; i < fields->size(); ++i) {
- switch (EstimateAlignmentSize((*fields)[i])) {
- case 1: aligned_to_1.push_back(FieldGroup(i, (*fields)[i])); break;
- case 4: aligned_to_4.push_back(FieldGroup(i, (*fields)[i])); break;
- case 8: aligned_to_8.push_back(FieldGroup(i, (*fields)[i])); break;
- default:
- GOOGLE_LOG(FATAL) << "Unknown alignment size.";
- }
- }
-
- // Now group fields aligned to 1 byte into sets of 4, and treat those like a
- // single field aligned to 4 bytes.
- for (int i = 0; i < aligned_to_1.size(); i += 4) {
- FieldGroup field_group;
- for (int j = i; j < aligned_to_1.size() && j < i + 4; ++j) {
- field_group.Append(aligned_to_1[j]);
- }
- aligned_to_4.push_back(field_group);
- }
- // Sort by preferred location to keep fields as close to their original
- // location as possible. Using stable_sort ensures that the output is
- // consistent across runs.
- std::stable_sort(aligned_to_4.begin(), aligned_to_4.end());
-
- // Now group fields aligned to 4 bytes (or the 4-field groups created above)
- // into pairs, and treat those like a single field aligned to 8 bytes.
- for (int i = 0; i < aligned_to_4.size(); i += 2) {
- FieldGroup field_group;
- for (int j = i; j < aligned_to_4.size() && j < i + 2; ++j) {
- field_group.Append(aligned_to_4[j]);
- }
- if (i == aligned_to_4.size() - 1) {
- // Move incomplete 4-byte block to the end.
- field_group.SetPreferredLocation(fields->size() + 1);
- }
- aligned_to_8.push_back(field_group);
- }
- // Sort by preferred location.
- std::stable_sort(aligned_to_8.begin(), aligned_to_8.end());
-
- // Now pull out all the FieldDescriptors in order.
- fields->clear();
- for (int i = 0; i < aligned_to_8.size(); ++i) {
- fields->insert(fields->end(),
- aligned_to_8[i].fields().begin(),
- aligned_to_8[i].fields().end());
+ return false;
+ default:
+ return false;
}
}
-string MessageTypeProtoName(const FieldDescriptor* field) {
- return field->message_type()->full_name();
+// Helper for the code that emits the SharedCtor() method.
+bool CanConstructByZeroing(const FieldDescriptor* field,
+ const Options& options) {
+ bool ret = CanInitializeByZeroing(field);
+
+ // Non-repeated, non-lazy message fields are simply raw pointers, so we can
+ // use memset to initialize these in SharedCtor. We cannot use this in
+ // Clear, as we need to potentially delete the existing value.
+ ret = ret ||
+ (!field->is_repeated() &&
+ field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE);
+ return ret;
}
// Emits an if-statement with a condition that evaluates to true if |field| is
@@ -340,27 +193,33 @@ bool HasHasMethod(const FieldDescriptor* field) {
// Collects map entry message type information.
void CollectMapInfo(const Descriptor* descriptor,
- map<string, string>* variables) {
+ std::map<string, string>* variables) {
GOOGLE_CHECK(IsMapEntryMessage(descriptor));
+ std::map<string, string>& vars = *variables;
const FieldDescriptor* key = descriptor->FindFieldByName("key");
const FieldDescriptor* val = descriptor->FindFieldByName("value");
- (*variables)["key"] = PrimitiveTypeName(key->cpp_type());
+ vars["key_cpp"] = PrimitiveTypeName(key->cpp_type());
switch (val->cpp_type()) {
case FieldDescriptor::CPPTYPE_MESSAGE:
- (*variables)["val"] = FieldMessageTypeName(val);
+ vars["val_cpp"] = FieldMessageTypeName(val);
break;
case FieldDescriptor::CPPTYPE_ENUM:
- (*variables)["val"] = ClassName(val->enum_type(), true);
+ vars["val_cpp"] = ClassName(val->enum_type(), true);
break;
default:
- (*variables)["val"] = PrimitiveTypeName(val->cpp_type());
+ vars["val_cpp"] = PrimitiveTypeName(val->cpp_type());
+ }
+ vars["key_wire_type"] = "::google::protobuf::internal::WireFormatLite::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(key->type()));
+ vars["val_wire_type"] = "::google::protobuf::internal::WireFormatLite::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(val->type()));
+ if (descriptor->file()->syntax() != FileDescriptor::SYNTAX_PROTO3 &&
+ val->type() == FieldDescriptor::TYPE_ENUM) {
+ const EnumValueDescriptor* default_value = val->default_value_enum();
+ vars["default_enum_value"] = Int32ToString(default_value->number());
+ } else {
+ vars["default_enum_value"] = "0";
}
- (*variables)["key_wire_type"] =
- "::google::protobuf::internal::WireFormatLite::TYPE_" +
- ToUpper(DeclaredTypeMethodName(key->type()));
- (*variables)["val_wire_type"] =
- "::google::protobuf::internal::WireFormatLite::TYPE_" +
- ToUpper(DeclaredTypeMethodName(val->type()));
}
// Does the given field have a private (internal helper only) has_$name$()
@@ -372,27 +231,247 @@ bool HasPrivateHasMethod(const FieldDescriptor* field) {
field->containing_oneof() != NULL);
}
+
+bool TableDrivenParsingEnabled(
+ const Descriptor* descriptor, const Options& options) {
+ if (!options.table_driven_parsing) {
+ return false;
+ }
+
+ // Consider table-driven parsing. We only do this if:
+ // - We have has_bits for fields. This avoids a check on every field we set
+ // when are present (the common case).
+ if (!HasFieldPresence(descriptor->file())) {
+ return false;
+ }
+
+ const double table_sparseness = 0.5;
+ int max_field_number = 0;
+ for (int i = 0; i < descriptor->field_count(); i++) {
+ const FieldDescriptor* field = descriptor->field(i);
+ if (max_field_number < field->number()) {
+ max_field_number = field->number();
+ }
+
+ // - There are no weak fields.
+ if (field->options().weak()) {
+ return false;
+ }
+ }
+
+ // - There range of field numbers is "small"
+ if (max_field_number >= (2 << 14)) {
+ return false;
+ }
+
+ // - Field numbers are relatively dense within the actual number of fields.
+ // We check for strictly greater than in the case where there are no fields
+ // (only extensions) so max_field_number == descriptor->field_count() == 0.
+ if (max_field_number * table_sparseness > descriptor->field_count()) {
+ return false;
+ }
+
+ // - This is not a MapEntryMessage.
+ if (IsMapEntryMessage(descriptor)) {
+ return false;
+ }
+
+ return true;
+}
+
+void SetUnknkownFieldsVariable(const Descriptor* descriptor,
+ const Options& options,
+ std::map<string, string>* variables) {
+ if (UseUnknownFieldSet(descriptor->file(), options)) {
+ (*variables)["unknown_fields_type"] = "::google::protobuf::UnknownFieldSet";
+ } else {
+ (*variables)["unknown_fields_type"] = "::std::string";
+ }
+ if (AlwaysPreserveUnknownFields(descriptor)) {
+ (*variables)["have_unknown_fields"] =
+ "_internal_metadata_.have_unknown_fields()";
+ (*variables)["unknown_fields"] = "_internal_metadata_.unknown_fields()";
+ } else {
+ (*variables)["have_unknown_fields"] =
+ "(_internal_metadata_.have_unknown_fields() && "
+ " ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())";
+ (*variables)["unknown_fields"] =
+ "(::google::protobuf::internal::GetProto3PreserveUnknownsDefault()"
+ " ? _internal_metadata_.unknown_fields()"
+ " : _internal_metadata_.default_instance())";
+ }
+ (*variables)["mutable_unknown_fields"] =
+ "_internal_metadata_.mutable_unknown_fields()";
+}
+
+bool IsCrossFileMapField(const FieldDescriptor* field) {
+ if (!field->is_map()) {
+ return false;
+ }
+
+ const Descriptor* d = field->message_type();
+ const FieldDescriptor* value = d->FindFieldByNumber(2);
+
+ return IsCrossFileMessage(value);
+}
+
+bool IsCrossFileMaybeMap(const FieldDescriptor* field) {
+ if (IsCrossFileMapField(field)) {
+ return true;
+ }
+
+ return IsCrossFileMessage(field);
+}
+
+bool IsRequired(const std::vector<const FieldDescriptor*>& v) {
+ return v.front()->is_required();
+}
+
+// Allows chunking repeated fields together and non-repeated fields if the
+// fields share the same has_byte index.
+// TODO(seongkim): use lambda with capture instead of functor.
+class MatchRepeatedAndHasByte {
+ public:
+ MatchRepeatedAndHasByte(const std::vector<int>* has_bit_indices,
+ bool has_field_presence)
+ : has_bit_indices_(*has_bit_indices),
+ has_field_presence_(has_field_presence) {}
+
+ // Returns true if the following conditions are met:
+ // --both fields are repeated fields
+ // --both fields are non-repeated fields with either has_field_presence is
+ // false or have the same has_byte index.
+ bool operator()(const FieldDescriptor* a, const FieldDescriptor* b) const {
+ return a->is_repeated() == b->is_repeated() &&
+ (!has_field_presence_ || a->is_repeated() ||
+ has_bit_indices_[a->index()] / 8 ==
+ has_bit_indices_[b->index()] / 8);
+ }
+
+ private:
+ const std::vector<int>& has_bit_indices_;
+ const bool has_field_presence_;
+};
+
+// Allows chunking required fields separately after chunking with
+// MatchRepeatedAndHasByte.
+class MatchRepeatedAndHasByteAndRequired : public MatchRepeatedAndHasByte {
+ public:
+ MatchRepeatedAndHasByteAndRequired(const std::vector<int>* has_bit_indices,
+ bool has_field_presence)
+ : MatchRepeatedAndHasByte(has_bit_indices, has_field_presence) {}
+
+ bool operator()(const FieldDescriptor* a, const FieldDescriptor* b) const {
+ return MatchRepeatedAndHasByte::operator()(a, b) &&
+ a->is_required() == b->is_required();
+ }
+};
+
+// Allows chunking zero-initializable fields separately after chunking with
+// MatchRepeatedAndHasByte.
+class MatchRepeatedAndHasByteAndZeroInits : public MatchRepeatedAndHasByte {
+ public:
+ MatchRepeatedAndHasByteAndZeroInits(const std::vector<int>* has_bit_indices,
+ bool has_field_presence)
+ : MatchRepeatedAndHasByte(has_bit_indices, has_field_presence) {}
+
+ bool operator()(const FieldDescriptor* a, const FieldDescriptor* b) const {
+ return MatchRepeatedAndHasByte::operator()(a, b) &&
+ CanInitializeByZeroing(a) == CanInitializeByZeroing(b);
+ }
+};
+
+// Collects neighboring fields based on a given criteria (equivalent predicate).
+template <typename Predicate>
+std::vector<std::vector<const FieldDescriptor*> > CollectFields(
+ const std::vector<const FieldDescriptor*>& fields,
+ const Predicate& equivalent) {
+ std::vector<std::vector<const FieldDescriptor*> > chunks;
+ if (fields.empty()) {
+ return chunks;
+ }
+
+ const FieldDescriptor* last_field = fields.front();
+ std::vector<const FieldDescriptor*> chunk;
+ for (int i = 0; i < fields.size(); i++) {
+ if (!equivalent(last_field, fields[i]) && !chunk.empty()) {
+ chunks.push_back(chunk);
+ chunk.clear();
+ }
+ chunk.push_back(fields[i]);
+ last_field = fields[i];
+ }
+ if (!chunk.empty()) {
+ chunks.push_back(chunk);
+ }
+ return chunks;
+}
+
+// Returns a bit mask based on has_bit index of "fields" that are typically on
+// the same chunk. It is used in a group presence check where _has_bits_ is
+// masked to tell if any thing in "fields" is present.
+uint32 GenChunkMask(const std::vector<const FieldDescriptor*>& fields,
+ const std::vector<int>& has_bit_indices) {
+ GOOGLE_CHECK(!fields.empty());
+ int first_index_offset = has_bit_indices[fields.front()->index()] / 32;
+ uint32 chunk_mask = 0;
+ for (int i = 0; i < fields.size(); i++) {
+ const FieldDescriptor* field = fields[i];
+ // "index" defines where in the _has_bits_ the field appears.
+ int index = has_bit_indices[field->index()];
+ GOOGLE_CHECK_EQ(first_index_offset, index / 32);
+ chunk_mask |= static_cast<uint32>(1) << (index % 32);
+ }
+ GOOGLE_CHECK_NE(0, chunk_mask);
+ return chunk_mask;
+}
+
} // anonymous namespace
// ===================================================================
MessageGenerator::MessageGenerator(const Descriptor* descriptor,
- const Options& options)
+ int index_in_file_messages,
+ const Options& options,
+ SCCAnalyzer* scc_analyzer)
: descriptor_(descriptor),
+ index_in_file_messages_(index_in_file_messages),
classname_(ClassName(descriptor, false)),
options_(options),
- field_generators_(descriptor, options),
- nested_generators_(new google::protobuf::scoped_ptr<
- MessageGenerator>[descriptor->nested_type_count()]),
+ field_generators_(descriptor, options, scc_analyzer),
+ max_has_bit_index_(0),
enum_generators_(
- new google::protobuf::scoped_ptr<EnumGenerator>[descriptor->enum_type_count()]),
- extension_generators_(new google::protobuf::scoped_ptr<
- ExtensionGenerator>[descriptor->extension_count()]),
- use_dependent_base_(false) {
+ new std::unique_ptr<EnumGenerator>[descriptor->enum_type_count()]),
+ extension_generators_(new std::unique_ptr<
+ ExtensionGenerator>[descriptor->extension_count()]),
+ num_weak_fields_(0),
+ message_layout_helper_(new PaddingOptimizer()),
+ scc_analyzer_(scc_analyzer) {
+ // Compute optimized field order to be used for layout and initialization
+ // purposes.
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (field->options().weak()) {
+ num_weak_fields_++;
+ } else if (!field->containing_oneof()) {
+ optimized_order_.push_back(field);
+ }
+ }
+
+ message_layout_helper_->OptimizeLayout(&optimized_order_, options_);
+
+ if (HasFieldPresence(descriptor_->file())) {
+ // We use -1 as a sentinel.
+ has_bit_indices_.resize(descriptor_->field_count(), -1);
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ // Skip fields that do not have has bits.
+ if (field->is_repeated()) {
+ continue;
+ }
- for (int i = 0; i < descriptor->nested_type_count(); i++) {
- nested_generators_[i].reset(
- new MessageGenerator(descriptor->nested_type(i), options));
+ has_bit_indices_[field->index()] = max_has_bit_index_++;
+ }
}
for (int i = 0; i < descriptor->enum_type_count(); i++) {
@@ -410,127 +489,96 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
if (descriptor->field(i)->is_required()) {
++num_required_fields_;
}
- if (options.proto_h && IsFieldDependent(descriptor->field(i))) {
- use_dependent_base_ = true;
- }
- }
- if (options.proto_h && descriptor->oneof_decl_count() > 0) {
- // Always make oneofs dependent.
- use_dependent_base_ = true;
}
+
+ table_driven_ = TableDrivenParsingEnabled(descriptor_, options_);
+
+ scc_name_ =
+ ClassName(scc_analyzer_->GetSCC(descriptor_)->GetRepresentative(), false);
}
MessageGenerator::~MessageGenerator() {}
-void MessageGenerator::
-FillMessageForwardDeclarations(set<string>* class_names) {
- class_names->insert(classname_);
-
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need forward declaration. Since map entry
- // message cannot be a top level class, we just need to avoid calling
- // GenerateForwardDeclaration here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->FillMessageForwardDeclarations(class_names);
+size_t MessageGenerator::HasBitsSize() const {
+ size_t sizeof_has_bits = (max_has_bit_index_ + 31) / 32 * 4;
+ if (sizeof_has_bits == 0) {
+ // Zero-size arrays aren't technically allowed, and MSVC in particular
+ // doesn't like them. We still need to declare these arrays to make
+ // other code compile. Since this is an uncommon case, we'll just declare
+ // them with size 1 and waste some space. Oh well.
+ sizeof_has_bits = 4;
}
-}
-void MessageGenerator::
-FillEnumForwardDeclarations(set<string>* enum_names) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->FillEnumForwardDeclarations(enum_names);
- }
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->FillForwardDeclaration(enum_names);
- }
+ return sizeof_has_bits;
}
-void MessageGenerator::
-GenerateEnumDefinitions(io::Printer* printer) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateEnumDefinitions(printer);
- }
-
+void MessageGenerator::AddGenerators(
+ std::vector<EnumGenerator*>* enum_generators,
+ std::vector<ExtensionGenerator*>* extension_generators) {
for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->GenerateDefinition(printer);
+ enum_generators->push_back(enum_generators_[i].get());
+ }
+ for (int i = 0; i < descriptor_->extension_count(); i++) {
+ extension_generators->push_back(extension_generators_[i].get());
}
}
-void MessageGenerator::
-GenerateGetEnumDescriptorSpecializations(io::Printer* printer) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
- }
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->GenerateGetEnumDescriptorSpecializations(printer);
- }
+void MessageGenerator::FillMessageForwardDeclarations(
+ std::map<string, const Descriptor*>* class_names) {
+ (*class_names)[classname_] = descriptor_;
}
void MessageGenerator::
-GenerateDependentFieldAccessorDeclarations(io::Printer* printer) {
+GenerateFieldAccessorDeclarations(io::Printer* printer) {
+ // optimized_fields_ does not contain fields where
+ // field->containing_oneof() != NULL
+ // so we need to iterate over those as well.
+ //
+ // We place the non-oneof fields in optimized_order_, as that controls the
+ // order of the _has_bits_ entries and we want GDB's pretty printers to be
+ // able to infer these indices from the k[FIELDNAME]FieldNumber order.
+ std::vector<const FieldDescriptor*> ordered_fields;
+ ordered_fields.reserve(descriptor_->field_count());
+
+ ordered_fields.insert(
+ ordered_fields.begin(), optimized_order_.begin(), optimized_order_.end());
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
-
- PrintFieldComment(printer, field);
-
- map<string, string> vars;
- SetCommonFieldVariables(field, &vars, options_);
-
- if (use_dependent_base_ && IsFieldDependent(field)) {
- // If the message is dependent, the inline clear_*() method will need
- // to delete the message type, so it must be in the dependent base
- // class. (See also GenerateFieldAccessorDeclarations.)
- printer->Print(vars, "void clear_$name$()$deprecation$;\n");
+ if (field->containing_oneof() == NULL && !field->options().weak()) {
+ continue;
}
- // Generate type-specific accessor declarations.
- field_generators_.get(field).GenerateDependentAccessorDeclarations(printer);
- printer->Print("\n");
+ ordered_fields.push_back(field);
}
-}
-void MessageGenerator::
-GenerateFieldAccessorDeclarations(io::Printer* printer) {
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ for (int i = 0; i < ordered_fields.size(); i++) {
+ const FieldDescriptor* field = ordered_fields[i];
PrintFieldComment(printer, field);
- map<string, string> vars;
+ std::map<string, string> vars;
SetCommonFieldVariables(field, &vars, options_);
vars["constant_name"] = FieldConstantName(field);
- bool dependent_field = use_dependent_base_ && IsFieldDependent(field);
- if (dependent_field &&
- field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
- !field->is_map()) {
- // If this field is dependent, the dependent base class determines
- // the message type from the derived class (which is a template
- // parameter). This typedef is for that:
- printer->Print(
- "private:\n"
- "typedef $field_type$ $dependent_type$;\n"
- "public:\n",
- "field_type", FieldMessageTypeName(field),
- "dependent_type", DependentTypeName(field));
- }
-
if (field->is_repeated()) {
- printer->Print(vars, "int $name$_size() const$deprecation$;\n");
+ printer->Print(vars, "$deprecated_attr$int ${$$name$_size$}$() const;\n");
+ printer->Annotate("{", "}", field);
} else if (HasHasMethod(field)) {
- printer->Print(vars, "bool has_$name$() const$deprecation$;\n");
+ printer->Print(vars, "$deprecated_attr$bool ${$has_$name$$}$() const;\n");
+ printer->Annotate("{", "}", field);
} else if (HasPrivateHasMethod(field)) {
printer->Print(vars,
- "private:\n"
- "bool has_$name$() const$deprecation$;\n"
- "public:\n");
+ "private:\n"
+ "bool ${$has_$name$$}$() const;\n"
+ "public:\n");
+ printer->Annotate("{", "}", field);
}
- if (!dependent_field) {
- // If this field is dependent, then its clear_() method is in the
- // depenent base class. (See also GenerateDependentAccessorDeclarations.)
- printer->Print(vars, "void clear_$name$()$deprecation$;\n");
- }
- printer->Print(vars, "static const int $constant_name$ = $number$;\n");
+ printer->Print(vars, "$deprecated_attr$void ${$clear_$name$$}$();\n");
+ printer->Annotate("{", "}", field);
+ printer->Print(vars,
+ "$deprecated_attr$static const int $constant_name$ = "
+ "$number$;\n");
+ printer->Annotate("constant_name", field);
// Generate type-specific accessor declarations.
field_generators_.get(field).GenerateAccessorDeclarations(printer);
@@ -548,6 +596,7 @@ GenerateFieldAccessorDeclarations(io::Printer* printer) {
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
+ "void clear_$oneof_name$();\n"
"$camel_oneof_name$Case $oneof_name$_case() const;\n",
"camel_oneof_name",
UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true),
@@ -556,80 +605,34 @@ GenerateFieldAccessorDeclarations(io::Printer* printer) {
}
void MessageGenerator::
-GenerateDependentFieldAccessorDefinitions(io::Printer* printer) {
- if (!use_dependent_base_) return;
-
- printer->Print("// $classname$\n\n", "classname",
- DependentBaseClassTemplateName(descriptor_));
-
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
-
- PrintFieldComment(printer, field);
-
- // These functions are not really dependent: they are part of the
- // (non-dependent) derived class. However, they need to live outside
- // any #ifdef guards, so we treat them as if they were dependent.
- //
- // See the comment in FileGenerator::GenerateInlineFunctionDefinitions
- // for a more complete explanation.
- if (use_dependent_base_ && IsFieldDependent(field)) {
- map<string, string> vars;
- SetCommonFieldVariables(field, &vars, options_);
- vars["inline"] = "inline ";
- if (field->containing_oneof()) {
- vars["field_name"] = UnderscoresToCamelCase(field->name(), true);
- vars["oneof_name"] = field->containing_oneof()->name();
- vars["oneof_index"] = SimpleItoa(field->containing_oneof()->index());
- GenerateOneofMemberHasBits(field, vars, printer);
- } else if (!field->is_repeated()) {
- // There will be no header guard, so this always has to be inline.
- GenerateSingularFieldHasBits(field, vars, printer);
- }
- // vars needed for clear_(), which is in the dependent base:
- // (See also GenerateDependentFieldAccessorDeclarations.)
- vars["tmpl"] = "template<class T>\n";
- vars["dependent_classname"] =
- DependentBaseClassTemplateName(descriptor_) + "<T>";
- vars["this_message"] = DependentBaseDownCast();
- vars["this_const_message"] = DependentBaseConstDownCast();
- GenerateFieldClear(field, vars, printer);
- }
-
- // Generate type-specific accessors.
- field_generators_.get(field)
- .GenerateDependentInlineAccessorDefinitions(printer);
-
- printer->Print("\n");
- }
-
- // Generate has_$name$() and clear_has_$name$() functions for oneofs
- // Similar to other has-bits, these must always be in the header if we
- // are using a dependent base class.
- GenerateOneofHasBits(printer, true /* is_inline */);
-}
-
-void MessageGenerator::
GenerateSingularFieldHasBits(const FieldDescriptor* field,
- map<string, string> vars,
+ std::map<string, string> vars,
io::Printer* printer) {
+ if (field->options().weak()) {
+ printer->Print(
+ vars,
+ "inline bool $classname$::has_$name$() const {\n"
+ " return _weak_field_map_.Has($number$);\n"
+ "}\n");
+ return;
+ }
if (HasFieldPresence(descriptor_->file())) {
// N.B.: without field presence, we do not use has-bits or generate
// has_$name$() methods.
- vars["has_array_index"] = SimpleItoa(field->index() / 32);
- vars["has_mask"] = StrCat(strings::Hex(1u << (field->index() % 32),
+ int has_bit_index = has_bit_indices_[field->index()];
+ GOOGLE_CHECK_GE(has_bit_index, 0);
+
+ vars["has_array_index"] = SimpleItoa(has_bit_index / 32);
+ vars["has_mask"] = StrCat(strings::Hex(1u << (has_bit_index % 32),
strings::ZERO_PAD_8));
printer->Print(vars,
- "$inline$"
- "bool $classname$::has_$name$() const {\n"
+ "inline bool $classname$::has_$name$() const {\n"
" return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n"
"}\n"
- "$inline$"
- "void $classname$::set_has_$name$() {\n"
+ "inline void $classname$::set_has_$name$() {\n"
" _has_bits_[$has_array_index$] |= 0x$has_mask$u;\n"
"}\n"
- "$inline$"
- "void $classname$::clear_has_$name$() {\n"
+ "inline void $classname$::clear_has_$name$() {\n"
" _has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n"
"}\n");
} else {
@@ -638,39 +641,35 @@ GenerateSingularFieldHasBits(const FieldDescriptor* field,
bool is_lazy = false;
if (is_lazy) {
printer->Print(vars,
- "$inline$"
- "bool $classname$::has_$name$() const {\n"
+ "inline bool $classname$::has_$name$() const {\n"
" return !$name$_.IsCleared();\n"
"}\n");
} else {
- printer->Print(vars,
- "$inline$"
- "bool $classname$::has_$name$() const {\n"
- " return !_is_default_instance_ && $name$_ != NULL;\n"
- "}\n");
+ printer->Print(
+ vars,
+ "inline bool $classname$::has_$name$() const {\n"
+ " return this != internal_default_instance() && $name$_ != NULL;\n"
+ "}\n");
}
}
}
}
void MessageGenerator::
-GenerateOneofHasBits(io::Printer* printer, bool is_inline) {
+GenerateOneofHasBits(io::Printer* printer) {
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
- map<string, string> vars;
+ std::map<string, string> vars;
vars["oneof_name"] = descriptor_->oneof_decl(i)->name();
vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
vars["cap_oneof_name"] =
ToUpper(descriptor_->oneof_decl(i)->name());
vars["classname"] = classname_;
- vars["inline"] = (is_inline ? "inline " : "");
printer->Print(
vars,
- "$inline$"
- "bool $classname$::has_$oneof_name$() const {\n"
+ "inline bool $classname$::has_$oneof_name$() const {\n"
" return $oneof_name$_case() != $cap_oneof_name$_NOT_SET;\n"
"}\n"
- "$inline$"
- "void $classname$::clear_has_$oneof_name$() {\n"
+ "inline void $classname$::clear_has_$oneof_name$() {\n"
" _oneof_case_[$oneof_index$] = $cap_oneof_name$_NOT_SET;\n"
"}\n");
}
@@ -678,7 +677,7 @@ GenerateOneofHasBits(io::Printer* printer, bool is_inline) {
void MessageGenerator::
GenerateOneofMemberHasBits(const FieldDescriptor* field,
- const map<string, string>& vars,
+ const std::map<string, string>& vars,
io::Printer* printer) {
// Singular field in a oneof
// N.B.: Without field presence, we do not use has-bits or generate
@@ -687,28 +686,26 @@ GenerateOneofMemberHasBits(const FieldDescriptor* field,
// method, so that generated code is slightly cleaner (vs. comparing
// _oneof_case_[index] against a constant everywhere).
printer->Print(vars,
- "$inline$"
- "bool $classname$::has_$name$() const {\n"
+ "inline bool $classname$::has_$name$() const {\n"
" return $oneof_name$_case() == k$field_name$;\n"
"}\n");
printer->Print(vars,
- "$inline$"
- "void $classname$::set_has_$name$() {\n"
+ "inline void $classname$::set_has_$name$() {\n"
" _oneof_case_[$oneof_index$] = k$field_name$;\n"
"}\n");
}
void MessageGenerator::
GenerateFieldClear(const FieldDescriptor* field,
- const map<string, string>& vars,
+ const std::map<string, string>& vars,
+ bool is_inline,
io::Printer* printer) {
- // Generate clear_$name$() (See GenerateFieldAccessorDeclarations and
- // GenerateDependentFieldAccessorDeclarations, $dependent_classname$ is
- // set by the Generate*Definitions functions.)
+ // Generate clear_$name$().
+ if (is_inline) {
+ printer->Print("inline ");
+ }
printer->Print(vars,
- "$tmpl$"
- "$inline$"
- "void $dependent_classname$::clear_$name$() {\n");
+ "void $classname$::clear_$name$() {\n");
printer->Indent();
@@ -716,21 +713,20 @@ GenerateFieldClear(const FieldDescriptor* field,
// Clear this field only if it is the active field in this oneof,
// otherwise ignore
printer->Print(vars,
- "if ($this_message$has_$name$()) {\n");
+ "if (has_$name$()) {\n");
printer->Indent();
field_generators_.get(field)
.GenerateClearingCode(printer);
printer->Print(vars,
- "$this_message$clear_has_$oneof_name$();\n");
+ "clear_has_$oneof_name$();\n");
printer->Outdent();
printer->Print("}\n");
} else {
field_generators_.get(field)
.GenerateClearingCode(printer);
if (HasFieldPresence(descriptor_->file())) {
- if (!field->is_repeated()) {
- printer->Print(vars,
- "$this_message$clear_has_$name$();\n");
+ if (!field->is_repeated() && !field->options().weak()) {
+ printer->Print(vars, "clear_has_$name$();\n");
}
}
}
@@ -740,7 +736,7 @@ GenerateFieldClear(const FieldDescriptor* field,
}
void MessageGenerator::
-GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) {
+GenerateFieldAccessorDefinitions(io::Printer* printer) {
printer->Print("// $classname$\n\n", "classname", classname_);
for (int i = 0; i < descriptor_->field_count(); i++) {
@@ -748,135 +744,81 @@ GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) {
PrintFieldComment(printer, field);
- map<string, string> vars;
+ std::map<string, string> vars;
SetCommonFieldVariables(field, &vars, options_);
- vars["inline"] = is_inline ? "inline " : "";
- if (use_dependent_base_ && IsFieldDependent(field)) {
- vars["tmpl"] = "template<class T>\n";
- vars["dependent_classname"] =
- DependentBaseClassTemplateName(descriptor_) + "<T>";
- vars["this_message"] = "reinterpret_cast<T*>(this)->";
- vars["this_const_message"] = "reinterpret_cast<const T*>(this)->";
- } else {
- vars["tmpl"] = "";
- vars["dependent_classname"] = vars["classname"];
- vars["this_message"] = "";
- vars["this_const_message"] = "";
- }
// Generate has_$name$() or $name$_size().
if (field->is_repeated()) {
printer->Print(vars,
- "$inline$"
- "int $classname$::$name$_size() const {\n"
+ "inline int $classname$::$name$_size() const {\n"
" return $name$_.size();\n"
"}\n");
} else if (field->containing_oneof()) {
vars["field_name"] = UnderscoresToCamelCase(field->name(), true);
vars["oneof_name"] = field->containing_oneof()->name();
vars["oneof_index"] = SimpleItoa(field->containing_oneof()->index());
- if (!use_dependent_base_ || !IsFieldDependent(field)) {
- GenerateOneofMemberHasBits(field, vars, printer);
- }
+ GenerateOneofMemberHasBits(field, vars, printer);
} else {
// Singular field.
- if (!use_dependent_base_ || !IsFieldDependent(field)) {
- GenerateSingularFieldHasBits(field, vars, printer);
- }
+ GenerateSingularFieldHasBits(field, vars, printer);
}
- if (!use_dependent_base_ || !IsFieldDependent(field)) {
- GenerateFieldClear(field, vars, printer);
+ if (!IsCrossFileMaybeMap(field)) {
+ GenerateFieldClear(field, vars, true, printer);
}
// Generate type-specific accessors.
- field_generators_.get(field).GenerateInlineAccessorDefinitions(printer,
- is_inline);
+ field_generators_.get(field).GenerateInlineAccessorDefinitions(printer);
printer->Print("\n");
}
- if (!use_dependent_base_) {
- // Generate has_$name$() and clear_has_$name$() functions for oneofs
- // If we aren't using a dependent base, they can be with the other functions
- // that are #ifdef-guarded.
- GenerateOneofHasBits(printer, is_inline);
- }
-}
-
-// Helper for the code that emits the Clear() method.
-static bool CanClearByZeroing(const FieldDescriptor* field) {
- if (field->is_repeated() || field->is_extension()) return false;
- switch (field->cpp_type()) {
- case internal::WireFormatLite::CPPTYPE_ENUM:
- return field->default_value_enum()->number() == 0;
- case internal::WireFormatLite::CPPTYPE_INT32:
- return field->default_value_int32() == 0;
- case internal::WireFormatLite::CPPTYPE_INT64:
- return field->default_value_int64() == 0;
- case internal::WireFormatLite::CPPTYPE_UINT32:
- return field->default_value_uint32() == 0;
- case internal::WireFormatLite::CPPTYPE_UINT64:
- return field->default_value_uint64() == 0;
- case internal::WireFormatLite::CPPTYPE_FLOAT:
- return field->default_value_float() == 0;
- case internal::WireFormatLite::CPPTYPE_DOUBLE:
- return field->default_value_double() == 0;
- case internal::WireFormatLite::CPPTYPE_BOOL:
- return field->default_value_bool() == false;
- default:
- return false;
- }
-}
-
-void MessageGenerator::
-GenerateDependentBaseClassDefinition(io::Printer* printer) {
- if (!use_dependent_base_) {
- return;
- }
-
- map<string, string> vars;
- vars["classname"] = DependentBaseClassTemplateName(descriptor_);
- vars["superclass"] = SuperClassName(descriptor_);
-
- printer->Print(vars,
- "template <class T>\n"
- "class $classname$ : public $superclass$ {\n"
- " public:\n");
- printer->Indent();
-
- printer->Print(vars,
- "$classname$() {}\n"
- "virtual ~$classname$() {}\n"
- "\n");
-
- // Generate dependent accessor methods for all fields.
- GenerateDependentFieldAccessorDeclarations(printer);
-
- printer->Outdent();
- printer->Print("};\n");
+ // Generate has_$name$() and clear_has_$name$() functions for oneofs.
+ GenerateOneofHasBits(printer);
}
void MessageGenerator::
GenerateClassDefinition(io::Printer* printer) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need class definition. Since map entry message
- // cannot be a top level class, we just need to avoid calling
- // GenerateClassDefinition here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateClassDefinition(printer);
- printer->Print("\n");
- printer->Print(kThinSeparator);
- printer->Print("\n");
- }
-
- if (use_dependent_base_) {
- GenerateDependentBaseClassDefinition(printer);
- printer->Print("\n");
+ if (IsMapEntryMessage(descriptor_)) {
+ std::map<string, string> vars;
+ vars["classname"] = classname_;
+ CollectMapInfo(descriptor_, &vars);
+ vars["lite"] =
+ HasDescriptorMethods(descriptor_->file(), options_) ? "" : "Lite";
+ printer->Print(
+ vars,
+ "class $classname$ : public "
+ "::google::protobuf::internal::MapEntry$lite$<$classname$, \n"
+ " $key_cpp$, $val_cpp$,\n"
+ " $key_wire_type$,\n"
+ " $val_wire_type$,\n"
+ " $default_enum_value$ > {\n"
+ "public:\n"
+ " typedef ::google::protobuf::internal::MapEntry$lite$<$classname$, \n"
+ " $key_cpp$, $val_cpp$,\n"
+ " $key_wire_type$,\n"
+ " $val_wire_type$,\n"
+ " $default_enum_value$ > SuperType;\n"
+ " $classname$();\n"
+ " $classname$(::google::protobuf::Arena* arena);\n"
+ " void MergeFrom(const $classname$& other);\n"
+ " static const $classname$* internal_default_instance() { return "
+ "reinterpret_cast<const "
+ "$classname$*>(&_$classname$_default_instance_); }\n");
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
+ printer->Print(
+ " void MergeFrom(const ::google::protobuf::Message& other) final;\n"
+ " ::google::protobuf::Metadata GetMetadata() const;\n"
+ "};\n");
+ } else {
+ printer->Print("};\n");
+ }
+ return;
}
- map<string, string> vars;
+ std::map<string, string> vars;
vars["classname"] = classname_;
+ vars["full_name"] = descriptor_->full_name();
vars["field_count"] = SimpleItoa(descriptor_->field_count());
vars["oneof_decl_count"] = SimpleItoa(descriptor_->oneof_decl_count());
if (options_.dllexport_decl.empty()) {
@@ -884,71 +826,63 @@ GenerateClassDefinition(io::Printer* printer) {
} else {
vars["dllexport"] = options_.dllexport_decl + " ";
}
- if (use_dependent_base_) {
- vars["superclass"] =
- DependentBaseClassTemplateName(descriptor_) + "<" + classname_ + ">";
- } else {
- vars["superclass"] = SuperClassName(descriptor_);
- }
+ vars["superclass"] = SuperClassName(descriptor_, options_);
printer->Print(vars,
- "class $dllexport$$classname$ : public $superclass$ {\n");
- if (use_dependent_base_) {
- printer->Print(vars, " friend class $superclass$;\n");
- }
+ "class $dllexport$$classname$ : public $superclass$ "
+ "/* @@protoc_insertion_point(class_definition:$full_name$) */ "
+ "{\n");
+ printer->Annotate("classname", descriptor_);
printer->Print(" public:\n");
printer->Indent();
+ printer->Print(
+ vars,
+ "$classname$();\n"
+ "virtual ~$classname$();\n"
+ "\n"
+ "$classname$(const $classname$& from);\n"
+ "\n"
+ "inline $classname$& operator=(const $classname$& from) {\n"
+ " CopyFrom(from);\n"
+ " return *this;\n"
+ "}\n");
+
+ if (options_.table_driven_serialization) {
+ printer->Print(
+ "private:\n"
+ "const void* InternalGetTable() const;\n"
+ "public:\n"
+ "\n");
+ }
+
+ // Generate move constructor and move assignment operator.
printer->Print(vars,
- "$classname$();\n"
- "virtual ~$classname$();\n"
- "\n"
- "$classname$(const $classname$& from);\n"
+ "#if LANG_CXX11\n"
+ "$classname$($classname$&& from) noexcept\n"
+ " : $classname$() {\n"
+ " *this = ::std::move(from);\n"
+ "}\n"
"\n"
- "inline $classname$& operator=(const $classname$& from) {\n"
- " CopyFrom(from);\n"
+ "inline $classname$& operator=($classname$&& from) noexcept {\n"
+ " if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {\n"
+ " if (this != &from) InternalSwap(&from);\n"
+ " } else {\n"
+ " CopyFrom(from);\n"
+ " }\n"
" return *this;\n"
"}\n"
- "\n");
+ "#endif\n");
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
- " return _internal_metadata_.unknown_fields();\n"
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ if (PublicUnknownFieldsAccessors(descriptor_)) {
+ printer->Print(vars,
+ "inline const $unknown_fields_type$& unknown_fields() const {\n"
+ " return $unknown_fields$;\n"
"}\n"
- "\n"
- "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
- " return _internal_metadata_.mutable_unknown_fields();\n"
+ "inline $unknown_fields_type$* mutable_unknown_fields() {\n"
+ " return $mutable_unknown_fields$;\n"
"}\n"
"\n");
- } else {
- if (SupportsArenas(descriptor_)) {
- printer->Print(
- "inline const ::std::string& unknown_fields() const {\n"
- " return _unknown_fields_.Get(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n"
- "}\n"
- "\n"
- "inline ::std::string* mutable_unknown_fields() {\n"
- " return _unknown_fields_.Mutable(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited(),\n"
- " GetArenaNoVirtual());\n"
- "}\n"
- "\n");
- } else {
- printer->Print(
- "inline const ::std::string& unknown_fields() const {\n"
- " return _unknown_fields_.GetNoArena(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n"
- "}\n"
- "\n"
- "inline ::std::string* mutable_unknown_fields() {\n"
- " return _unknown_fields_.MutableNoArena(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n"
- "}\n"
- "\n");
- }
- }
}
// N.B.: We exclude GetArena() when arena support is disabled, falling back on
@@ -958,14 +892,16 @@ GenerateClassDefinition(io::Printer* printer) {
// virtual method version of GetArenaNoVirtual(), required for generic dispatch given a
// MessageLite* (e.g., in RepeatedField::AddAllocated()).
printer->Print(
- "inline ::google::protobuf::Arena* GetArena() const { return GetArenaNoVirtual(); }\n"
- "inline void* GetMaybeArenaPointer() const {\n"
+ "inline ::google::protobuf::Arena* GetArena() const final {\n"
+ " return GetArenaNoVirtual();\n"
+ "}\n"
+ "inline void* GetMaybeArenaPointer() const final {\n"
" return MaybeArenaPtr();\n"
"}\n");
}
// Only generate this member if it's not disabled.
- if (HasDescriptorMethods(descriptor_->file()) &&
+ if (HasDescriptorMethods(descriptor_->file(), options_) &&
!descriptor_->options().no_standard_descriptor_accessor()) {
printer->Print(vars,
"static const ::google::protobuf::Descriptor* descriptor();\n");
@@ -1002,20 +938,18 @@ GenerateClassDefinition(io::Printer* printer) {
"\n");
}
- if (!StaticInitializersForced(descriptor_->file())) {
- printer->Print(vars,
- "#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n"
- "// Returns the internal default instance pointer. This function can\n"
- "// return NULL thus should not be used by the user. This is intended\n"
- "// for Protobuf internal code. Please use default_instance() declared\n"
- "// above instead.\n"
+ // TODO(gerbens) make this private, while still granting other protos access.
+ vars["message_index"] = SimpleItoa(index_in_file_messages_);
+ printer->Print(
+ vars,
+ "static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY\n"
"static inline const $classname$* internal_default_instance() {\n"
- " return default_instance_;\n"
+ " return reinterpret_cast<const $classname$*>(\n"
+ " &_$classname$_default_instance_);\n"
"}\n"
- "#endif\n"
+ "static constexpr int kIndexInFileMessages =\n"
+ " $message_index$;\n"
"\n");
- }
-
if (SupportsArenas(descriptor_)) {
printer->Print(vars,
@@ -1027,94 +961,99 @@ GenerateClassDefinition(io::Printer* printer) {
"// implements Any -----------------------------------------------\n"
"\n"
"void PackFrom(const ::google::protobuf::Message& message);\n"
+ "void PackFrom(const ::google::protobuf::Message& message,\n"
+ " const ::std::string& type_url_prefix);\n"
"bool UnpackTo(::google::protobuf::Message* message) const;\n"
"template<typename T> bool Is() const {\n"
" return _any_metadata_.Is<T>();\n"
"}\n"
+ "static bool ParseAnyTypeUrl(const string& type_url,\n"
+ " string* full_type_name);\n"
"\n");
}
+ vars["new_final"] = " final";
+
printer->Print(vars,
"void Swap($classname$* other);\n"
+ "friend void swap($classname$& a, $classname$& b) {\n"
+ " a.Swap(&b);\n"
+ "}\n"
"\n"
"// implements Message ----------------------------------------------\n"
"\n"
- "inline $classname$* New() const { return New(NULL); }\n"
+ "inline $classname$* New() const$new_final$ {\n"
+ " return CreateMaybeMessage<$classname$>(NULL);\n"
+ "}\n"
"\n"
- "$classname$* New(::google::protobuf::Arena* arena) const;\n");
+ "$classname$* New(::google::protobuf::Arena* arena) const$new_final$ {\n"
+ " return CreateMaybeMessage<$classname$>(arena);\n"
+ "}\n");
- if (HasGeneratedMethods(descriptor_->file())) {
- if (HasDescriptorMethods(descriptor_->file())) {
+ // For instances that derive from Message (rather than MessageLite), some
+ // methods are virtual and should be marked as final.
+ string use_final = HasDescriptorMethods(descriptor_->file(), options_) ?
+ " final" : "";
+
+ if (HasGeneratedMethods(descriptor_->file(), options_)) {
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
printer->Print(vars,
- "void CopyFrom(const ::google::protobuf::Message& from);\n"
- "void MergeFrom(const ::google::protobuf::Message& from);\n");
+ "void CopyFrom(const ::google::protobuf::Message& from) final;\n"
+ "void MergeFrom(const ::google::protobuf::Message& from) final;\n");
} else {
printer->Print(vars,
- "void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from);\n");
+ "void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from)\n"
+ " final;\n");
}
- printer->Print(vars,
- "void CopyFrom(const $classname$& from);\n"
- "void MergeFrom(const $classname$& from);\n"
- "void Clear();\n"
- "bool IsInitialized() const;\n"
- "\n"
- "int ByteSize() const;\n"
- "bool MergePartialFromCodedStream(\n"
- " ::google::protobuf::io::CodedInputStream* input);\n"
- "void SerializeWithCachedSizes(\n"
- " ::google::protobuf::io::CodedOutputStream* output) const;\n");
+ vars["clear_final"] = " final";
+ vars["is_initialized_final"] = " final";
+ vars["merge_partial_final"] = " final";
+
+ printer->Print(
+ vars,
+ "void CopyFrom(const $classname$& from);\n"
+ "void MergeFrom(const $classname$& from);\n"
+ "void Clear()$clear_final$;\n"
+ "bool IsInitialized() const$is_initialized_final$;\n"
+ "\n"
+ "size_t ByteSizeLong() const final;\n"
+ "bool MergePartialFromCodedStream(\n"
+ " ::google::protobuf::io::CodedInputStream* input)$merge_partial_final$;\n");
+ if (!options_.table_driven_serialization ||
+ descriptor_->options().message_set_wire_format()) {
+ printer->Print(
+ "void SerializeWithCachedSizes(\n"
+ " ::google::protobuf::io::CodedOutputStream* output) const "
+ "final;\n");
+ }
// DiscardUnknownFields() is implemented in message.cc using reflections. We
// need to implement this function in generated code for messages.
- if (!UseUnknownFieldSet(descriptor_->file())) {
+ if (!UseUnknownFieldSet(descriptor_->file(), options_)) {
printer->Print(
- "void DiscardUnknownFields();\n");
+ "void DiscardUnknownFields()$final$;\n",
+ "final", use_final);
}
- if (HasFastArraySerialization(descriptor_->file())) {
+ if (HasFastArraySerialization(descriptor_->file(), options_)) {
printer->Print(
- "::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;\n");
- }
- }
-
- // Check all FieldDescriptors including those in oneofs to estimate
- // whether ::std::string is likely to be used, and depending on that
- // estimate, set uses_string_ to true or false. That contols
- // whether to force initialization of empty_string_ in SharedCtor().
- // It's often advantageous to do so to keep "is empty_string_
- // inited?" code from appearing all over the place.
- vector<const FieldDescriptor*> descriptors;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- descriptors.push_back(descriptor_->field(i));
- }
- for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
- for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
- descriptors.push_back(descriptor_->oneof_decl(i)->field(j));
- }
- }
- uses_string_ = false;
- if (PreserveUnknownFields(descriptor_) &&
- !UseUnknownFieldSet(descriptor_->file())) {
- uses_string_ = true;
- }
- for (int i = 0; i < descriptors.size(); i++) {
- const FieldDescriptor* field = descriptors[i];
- if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
- switch (field->options().ctype()) {
- default: uses_string_ = true; break;
- }
+ "::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(\n"
+ " bool deterministic, ::google::protobuf::uint8* target) const final;\n");
}
}
printer->Print(
- "int GetCachedSize() const { return _cached_size_; }\n"
- "private:\n"
- "void SharedCtor();\n"
- "void SharedDtor();\n"
- "void SetCachedSize(int size) const;\n"
- "void InternalSwap($classname$* other);\n",
- "classname", classname_);
+ "int GetCachedSize() const final { return _cached_size_.Get(); }"
+ "\n\nprivate:\n"
+ "void SharedCtor();\n"
+ "void SharedDtor();\n"
+ "void SetCachedSize(int size) const$final$;\n"
+ "void InternalSwap($classname$* other);\n",
+ "classname", classname_, "final", use_final);
if (SupportsArenas(descriptor_)) {
printer->Print(
+ // TODO(gerbens) Make this private! Currently people are deriving from
+ // protos to give access to this constructor, breaking the invariants
+ // we rely on.
"protected:\n"
"explicit $classname$(::google::protobuf::Arena* arena);\n"
"private:\n"
@@ -1123,7 +1062,7 @@ GenerateClassDefinition(io::Printer* printer) {
"classname", classname_);
}
- if (UseUnknownFieldSet(descriptor_->file())) {
+ if (SupportsArenas(descriptor_)) {
printer->Print(
"private:\n"
"inline ::google::protobuf::Arena* GetArenaNoVirtual() const {\n"
@@ -1131,29 +1070,29 @@ GenerateClassDefinition(io::Printer* printer) {
"}\n"
"inline void* MaybeArenaPtr() const {\n"
" return _internal_metadata_.raw_arena_ptr();\n"
- "}\n"
- "public:\n"
- "\n");
+ "}\n");
} else {
printer->Print(
"private:\n"
"inline ::google::protobuf::Arena* GetArenaNoVirtual() const {\n"
- " return _arena_ptr_;\n"
- "}\n"
- "inline ::google::protobuf::Arena* MaybeArenaPtr() const {\n"
- " return _arena_ptr_;\n"
+ " return NULL;\n"
"}\n"
+ "inline void* MaybeArenaPtr() const {\n"
+ " return NULL;\n"
+ "}\n");
+ }
+
+ printer->Print(
"public:\n"
"\n");
- }
- if (HasDescriptorMethods(descriptor_->file())) {
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
printer->Print(
- "::google::protobuf::Metadata GetMetadata() const;\n"
+ "::google::protobuf::Metadata GetMetadata() const final;\n"
"\n");
} else {
printer->Print(
- "::std::string GetTypeName() const;\n"
+ "::std::string GetTypeName() const final;\n"
"\n");
}
@@ -1168,6 +1107,8 @@ GenerateClassDefinition(io::Printer* printer) {
printer->Print("typedef $nested_full_name$ $nested_name$;\n",
"nested_name", nested_type->name(),
"nested_full_name", ClassName(nested_type, false));
+ printer->Annotate("nested_full_name", nested_type);
+ printer->Annotate("nested_name", nested_type);
}
}
@@ -1206,22 +1147,21 @@ GenerateClassDefinition(io::Printer* printer) {
for (int i = 0; i < descriptor_->field_count(); i++) {
- if (!descriptor_->field(i)->is_repeated()) {
+ if (!descriptor_->field(i)->is_repeated() &&
+ !descriptor_->field(i)->options().weak()) {
// set_has_***() generated in all proto1/2 code and in oneofs (only) for
// messages without true field presence.
if (HasFieldPresence(descriptor_->file()) ||
descriptor_->field(i)->containing_oneof()) {
- printer->Print(
- "inline void set_has_$name$();\n",
- "name", FieldName(descriptor_->field(i)));
+ printer->Print("void set_has_$name$();\n", "name",
+ FieldName(descriptor_->field(i)));
}
// clear_has_***() generated only for non-oneof fields
// in proto1/2.
if (!descriptor_->field(i)->containing_oneof() &&
HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "inline void clear_has_$name$();\n",
- "name", FieldName(descriptor_->field(i)));
+ printer->Print("void clear_has_$name$();\n", "name",
+ FieldName(descriptor_->field(i)));
}
}
}
@@ -1231,17 +1171,16 @@ GenerateClassDefinition(io::Printer* printer) {
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
"inline bool has_$oneof_name$() const;\n"
- "void clear_$oneof_name$();\n"
"inline void clear_has_$oneof_name$();\n\n",
"oneof_name", descriptor_->oneof_decl(i)->name());
}
- if (HasGeneratedMethods(descriptor_->file()) &&
+ if (HasGeneratedMethods(descriptor_->file(), options_) &&
!descriptor_->options().message_set_wire_format() &&
num_required_fields_ > 1) {
printer->Print(
- "// helper for ByteSize()\n"
- "int RequiredFieldsByteSizeFallback() const;\n\n");
+ "// helper for ByteSizeLong()\n"
+ "size_t RequiredFieldsByteSizeFallback() const;\n\n");
}
// Prepare decls for _cached_size_ and _has_bits_. Their position in the
@@ -1249,20 +1188,13 @@ GenerateClassDefinition(io::Printer* printer) {
bool need_to_emit_cached_size = true;
// TODO(kenton): Make _cached_size_ an atomic<int> when C++ supports it.
- const string cached_size_decl = "mutable int _cached_size_;\n";
+ const string cached_size_decl =
+ "mutable ::google::protobuf::internal::CachedSize _cached_size_;\n";
- // TODO(jieluo) - Optimize _has_bits_ for repeated and oneof fields.
- size_t sizeof_has_bits = (descriptor_->field_count() + 31) / 32 * 4;
- if (descriptor_->field_count() == 0) {
- // Zero-size arrays aren't technically allowed, and MSVC in particular
- // doesn't like them. We still need to declare these arrays to make
- // other code compile. Since this is an uncommon case, we'll just declare
- // them with size 1 and waste some space. Oh well.
- sizeof_has_bits = 4;
- }
+ const size_t sizeof_has_bits = HasBitsSize();
const string has_bits_decl = sizeof_has_bits == 0 ? "" :
- "::google::protobuf::uint32 _has_bits_[" + SimpleItoa(sizeof_has_bits / 4) + "];\n";
-
+ "::google::protobuf::internal::HasBits<" + SimpleItoa(sizeof_has_bits / 4) +
+ "> _has_bits_;\n";
// To minimize padding, data members are divided into three sections:
// (1) members assumed to align to 8 bytes
@@ -1278,19 +1210,18 @@ GenerateClassDefinition(io::Printer* printer) {
"\n");
}
- if (UseUnknownFieldSet(descriptor_->file())) {
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
printer->Print(
"::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;\n");
} else {
printer->Print(
- "::google::protobuf::internal::ArenaStringPtr _unknown_fields_;\n"
- "::google::protobuf::Arena* _arena_ptr_;\n"
- "\n");
+ "::google::protobuf::internal::InternalMetadataWithArenaLite "
+ "_internal_metadata_;\n");
}
if (SupportsArenas(descriptor_)) {
printer->Print(
- "friend class ::google::protobuf::Arena;\n"
+ "template <typename T> friend class ::google::protobuf::Arena::InternalHelper;\n"
"typedef void InternalArenaConstructable_;\n"
"typedef void DestructorSkippable_;\n");
}
@@ -1306,55 +1237,16 @@ GenerateClassDefinition(io::Printer* printer) {
printer->Print(cached_size_decl.c_str());
need_to_emit_cached_size = false;
}
- } else {
- // Without field presence, we need another way to disambiguate the default
- // instance, because the default instance's submessage fields (if any) store
- // pointers to the default instances of the submessages even when they
- // aren't present. Alternatives to this approach might be to (i) use a
- // tagged pointer on all message fields, setting a tag bit for "not really
- // present, just default instance"; or (ii) comparing |this| against the
- // return value from GeneratedMessageFactory::GetPrototype() in all
- // has_$field$() calls. However, both of these options are much more
- // expensive (in code size and CPU overhead) than just checking a field in
- // the message. Long-term, the best solution would be to rearchitect the
- // default instance design not to store pointers to submessage default
- // instances, and have reflection get those some other way; but that change
- // would have too much impact on proto2.
- printer->Print(
- "bool _is_default_instance_;\n");
}
// Field members:
- // List fields which doesn't belong to any oneof
- vector<const FieldDescriptor*> fields;
- hash_map<string, int> fieldname_to_chunk;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- if (!descriptor_->field(i)->containing_oneof()) {
- const FieldDescriptor* field = descriptor_->field(i);
- fields.push_back(field);
- fieldname_to_chunk[FieldName(field)] = i / 8;
- }
- }
- OptimizePadding(&fields);
// Emit some private and static members
- runs_of_fields_ = vector< vector<string> >(1);
- for (int i = 0; i < fields.size(); ++i) {
- const FieldDescriptor* field = fields[i];
+ for (int i = 0; i < optimized_order_.size(); ++i) {
+ const FieldDescriptor* field = optimized_order_[i];
const FieldGenerator& generator = field_generators_.get(field);
generator.GenerateStaticMembers(printer);
generator.GeneratePrivateMembers(printer);
- if (CanClearByZeroing(field)) {
- const string& fieldname = FieldName(field);
- if (!runs_of_fields_.back().empty() &&
- (fieldname_to_chunk[runs_of_fields_.back().back()] !=
- fieldname_to_chunk[fieldname])) {
- runs_of_fields_.push_back(vector<string>());
- }
- runs_of_fields_.back().push_back(fieldname);
- } else if (!runs_of_fields_.back().empty()) {
- runs_of_fields_.push_back(vector<string>());
- }
}
// For each oneof generate a union
@@ -1395,86 +1287,44 @@ GenerateClassDefinition(io::Printer* printer) {
"\n");
}
+ if (num_weak_fields_) {
+ printer->Print(
+ "::google::protobuf::internal::WeakFieldMap _weak_field_map_;\n");
+ }
// Generate _any_metadata_ for the Any type.
if (IsAnyMessage(descriptor_)) {
printer->Print(vars,
"::google::protobuf::internal::AnyMetadata _any_metadata_;\n");
}
- // Declare AddDescriptors(), BuildDescriptors(), and ShutdownFile() as
- // friends so that they can access private static variables like
- // default_instance_ and reflection_.
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- "friend void $dllexport_decl$ $adddescriptorsname$();\n",
- // Without.
- "friend void $dllexport_decl$ $adddescriptorsname$_impl();\n",
- // Vars.
- "dllexport_decl", options_.dllexport_decl,
- "adddescriptorsname",
- GlobalAddDescriptorsName(descriptor_->file()->name()));
-
- printer->Print(
- "friend void $assigndescriptorsname$();\n"
- "friend void $shutdownfilename$();\n"
- "\n",
- "assigndescriptorsname",
- GlobalAssignDescriptorsName(descriptor_->file()->name()),
- "shutdownfilename", GlobalShutdownFileName(descriptor_->file()->name()));
-
- printer->Print(
- "void InitAsDefaultInstance();\n"
- "static $classname$* default_instance_;\n",
- "classname", classname_);
+ // The TableStruct struct needs access to the private parts, in order to
+ // construct the offsets of all members.
+ printer->Print("friend struct ::$file_namespace$::TableStruct;\n",
+ // Vars.
+ "scc_name", scc_name_, "file_namespace",
+ FileLevelNamespace(descriptor_));
printer->Outdent();
- printer->Print(vars, "};");
+ printer->Print("};");
GOOGLE_DCHECK(!need_to_emit_cached_size);
}
void MessageGenerator::
-GenerateDependentInlineMethods(io::Printer* printer) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need inline methods. Since map entry message
- // cannot be a top level class, we just need to avoid calling
- // GenerateInlineMethods here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateDependentInlineMethods(printer);
- printer->Print(kThinSeparator);
- printer->Print("\n");
- }
-
- GenerateDependentFieldAccessorDefinitions(printer);
-}
-
-void MessageGenerator::
-GenerateInlineMethods(io::Printer* printer, bool is_inline) {
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need inline methods. Since map entry message
- // cannot be a top level class, we just need to avoid calling
- // GenerateInlineMethods here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateInlineMethods(printer, is_inline);
- printer->Print(kThinSeparator);
- printer->Print("\n");
- }
-
- GenerateFieldAccessorDefinitions(printer, is_inline);
+GenerateInlineMethods(io::Printer* printer) {
+ if (IsMapEntryMessage(descriptor_)) return;
+ GenerateFieldAccessorDefinitions(printer);
// Generate oneof_case() functions.
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
- map<string, string> vars;
+ std::map<string, string> vars;
vars["class_name"] = classname_;
vars["camel_oneof_name"] = UnderscoresToCamelCase(
descriptor_->oneof_decl(i)->name(), true);
vars["oneof_name"] = descriptor_->oneof_decl(i)->name();
vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
- vars["inline"] = is_inline ? "inline " : "";
printer->Print(
vars,
- "$inline$"
- "$class_name$::$camel_oneof_name$Case $class_name$::"
+ "inline $class_name$::$camel_oneof_name$Case $class_name$::"
"$oneof_name$_case() const {\n"
" return $class_name$::$camel_oneof_name$Case("
"_oneof_case_[$oneof_index$]);\n"
@@ -1483,27 +1333,13 @@ GenerateInlineMethods(io::Printer* printer, bool is_inline) {
}
void MessageGenerator::
-GenerateDescriptorDeclarations(io::Printer* printer) {
- if (!IsMapEntryMessage(descriptor_)) {
- printer->Print(
- "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n"
- "const ::google::protobuf::internal::GeneratedMessageReflection*\n"
- " $name$_reflection_ = NULL;\n",
- "name", classname_);
- } else {
- printer->Print(
- "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n",
- "name", classname_);
- }
-
- // Generate oneof default instance for reflection usage.
- if (descriptor_->oneof_decl_count() > 0) {
- printer->Print("struct $name$OneofInstance {\n",
- "name", classname_);
+GenerateExtraDefaultFields(io::Printer* printer) {
+ // Generate oneof default instance and weak field instances for reflection
+ // usage.
+ if (descriptor_->oneof_decl_count() > 0 || num_weak_fields_ > 0) {
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
- printer->Print(" ");
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
(field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
EffectiveStringCType(field) != FieldOptions::STRING)) {
@@ -1512,309 +1348,465 @@ GenerateDescriptorDeclarations(io::Printer* printer) {
field_generators_.get(field).GeneratePrivateMembers(printer);
}
}
-
- printer->Print("}* $name$_default_oneof_instance_ = NULL;\n",
- "name", classname_);
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (field->options().weak()) {
+ printer->Print(
+ " const ::google::protobuf::Message* $name$_;\n", "name", FieldName(field));
+ }
+ }
}
+}
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateDescriptorDeclarations(printer);
+bool MessageGenerator::GenerateParseTable(io::Printer* printer, size_t offset,
+ size_t aux_offset) {
+ if (!table_driven_) {
+ printer->Print("{ NULL, NULL, 0, -1, -1, -1, -1, NULL, false },\n");
+ return false;
}
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- printer->Print(
- "const ::google::protobuf::EnumDescriptor* $name$_descriptor_ = NULL;\n",
- "name", ClassName(descriptor_->enum_type(i), false));
- }
-}
+ std::map<string, string> vars;
-void MessageGenerator::
-GenerateDescriptorInitializer(io::Printer* printer, int index) {
- // TODO(kenton): Passing the index to this method is redundant; just use
- // descriptor_->index() instead.
- map<string, string> vars;
- vars["classname"] = classname_;
- vars["index"] = SimpleItoa(index);
+ vars["classname"] = ClassName(descriptor_);
+ vars["classtype"] = QualifiedClassName(descriptor_);
+ vars["offset"] = SimpleItoa(offset);
+ vars["aux_offset"] = SimpleItoa(aux_offset);
- // Obtain the descriptor from the parent's descriptor.
- if (descriptor_->containing_type() == NULL) {
- printer->Print(vars,
- "$classname$_descriptor_ = file->message_type($index$);\n");
- } else {
- vars["parent"] = ClassName(descriptor_->containing_type(), false);
- printer->Print(vars,
- "$classname$_descriptor_ = "
- "$parent$_descriptor_->nested_type($index$);\n");
+ int max_field_number = 0;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (max_field_number < field->number()) {
+ max_field_number = field->number();
+ }
}
- if (IsMapEntryMessage(descriptor_)) return;
+ vars["max_field_number"] = SimpleItoa(max_field_number);
- // Generate the offsets.
- GenerateOffsets(printer);
+ printer->Print("{\n");
+ printer->Indent();
- const bool pass_pool_and_factory = false;
- vars["fn"] = pass_pool_and_factory ?
- "new ::google::protobuf::internal::GeneratedMessageReflection" :
- "::google::protobuf::internal::GeneratedMessageReflection"
- "::NewGeneratedMessageReflection";
- // Construct the reflection object.
printer->Print(vars,
- "$classname$_reflection_ =\n"
- " $fn$(\n"
- " $classname$_descriptor_,\n"
- " $classname$::default_instance_,\n"
- " $classname$_offsets_,\n");
+ "TableStruct::entries + $offset$,\n"
+ "TableStruct::aux + $aux_offset$,\n"
+ "$max_field_number$,\n");
+
if (!HasFieldPresence(descriptor_->file())) {
// If we don't have field presence, then _has_bits_ does not exist.
- printer->Print(vars,
- " -1,\n");
+ printer->Print(vars, "-1,\n");
} else {
printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, _has_bits_[0]),\n");
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n"
+ " $classtype$, _has_bits_),\n");
}
- // Unknown field offset: either points to the unknown field set if embedded
- // directly, or indicates that the unknown field set is stored as part of the
- // internal metadata if not.
- if (UseUnknownFieldSet(descriptor_->file())) {
+ if (descriptor_->oneof_decl_count() > 0) {
printer->Print(vars,
- " -1,\n");
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n"
+ " $classtype$, _oneof_case_),\n");
} else {
- printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _unknown_fields_),\n");
+ printer->Print("-1, // no _oneof_case_\n");
}
if (descriptor_->extension_range_count() > 0) {
printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _extensions_),\n");
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, "
+ "_extensions_),\n");
} else {
- // No extensions.
- printer->Print(vars,
- " -1,\n");
- }
-
- if (descriptor_->oneof_decl_count() > 0) {
- printer->Print(vars,
- " $classname$_default_oneof_instance_,\n"
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _oneof_case_[0]),\n");
+ printer->Print("-1, // no _extensions_\n");
}
- if (pass_pool_and_factory) {
- printer->Print(
- " ::google::protobuf::DescriptorPool::generated_pool(),\n");
- printer->Print(vars,
- " ::google::protobuf::MessageFactory::generated_factory(),\n");
- }
+ // TODO(ckennelly): Consolidate this with the calculation for
+ // AuxillaryParseTableField.
+ vars["ns"] = Namespace(descriptor_);
printer->Print(vars,
- " sizeof($classname$),\n");
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n"
+ " $classtype$, _internal_metadata_),\n"
+ "&$ns$::_$classname$_default_instance_,\n");
- // Arena offset: either an offset to the metadata struct that contains the
- // arena pointer and unknown field set (in a space-efficient way) if we use
- // that implementation strategy, or an offset directly to the arena pointer if
- // not (because e.g. we don't have an unknown field set).
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _internal_metadata_),\n");
- } else {
- printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _arena_),\n");
- }
-
- // is_default_instance_ offset.
- if (HasFieldPresence(descriptor_->file())) {
- printer->Print(vars,
- " -1);\n");
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(vars, "true,\n");
} else {
- printer->Print(vars,
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
- "$classname$, _is_default_instance_));\n");
- }
-
- // Handle nested types.
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateDescriptorInitializer(printer, i);
+ printer->Print(vars, "false,\n");
}
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->GenerateDescriptorInitializer(printer, i);
- }
+ printer->Outdent();
+ printer->Print("},\n");
+ return true;
}
-void MessageGenerator::
-GenerateTypeRegistrations(io::Printer* printer) {
- // Register this message type with the message factory.
- if (!IsMapEntryMessage(descriptor_)) {
- printer->Print(
- "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
- " $classname$_descriptor_, &$classname$::default_instance());\n",
- "classname", classname_);
- }
- else {
- map<string, string> vars;
- CollectMapInfo(descriptor_, &vars);
- vars["classname"] = classname_;
+void MessageGenerator::GenerateSchema(io::Printer* printer, int offset,
+ int has_offset) {
+ std::map<string, string> vars;
- const FieldDescriptor* val = descriptor_->FindFieldByName("value");
- if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO2 &&
- val->type() == FieldDescriptor::TYPE_ENUM) {
- const EnumValueDescriptor* default_value = val->default_value_enum();
- vars["default_enum_value"] = Int32ToString(default_value->number());
- } else {
- vars["default_enum_value"] = "0";
- }
+ vars["classname"] = QualifiedClassName(descriptor_);
+ vars["offset"] = SimpleItoa(offset);
+ vars["has_bits_offsets"] =
+ HasFieldPresence(descriptor_->file()) || IsMapEntryMessage(descriptor_)
+ ? SimpleItoa(offset + has_offset)
+ : "-1";
- printer->Print(vars,
- "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
- " $classname$_descriptor_,\n"
- " ::google::protobuf::internal::MapEntry<\n"
- " $key$,\n"
- " $val$,\n"
- " $key_wire_type$,\n"
- " $val_wire_type$,\n"
- " $default_enum_value$>::CreateDefaultInstance(\n"
- " $classname$_descriptor_));\n");
- }
-
- // Handle nested types.
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateTypeRegistrations(printer);
- }
+ printer->Print(vars,
+ "{ $offset$, $has_bits_offsets$, sizeof($classname$)},\n");
}
-void MessageGenerator::
-GenerateDefaultInstanceAllocator(io::Printer* printer) {
- // Construct the default instances of all fields, as they will be used
- // when creating the default instance of the entire message.
- for (int i = 0; i < descriptor_->field_count(); i++) {
- field_generators_.get(descriptor_->field(i))
- .GenerateDefaultInstanceAllocator(printer);
- }
+namespace {
- if (IsMapEntryMessage(descriptor_)) return;
+// TODO(gerbens) remove this after the next sync with GitHub code base.
+// Then the opensource testing has gained the functionality to compile
+// the CalcFieldNum given the symbols defined in generated-message-util.
+#ifdef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP
+// We need a clean version of CalcFieldNum that doesn't use new functionality
+// in the runtime, because this functionality is not yet in the opensource
+// runtime
- // Construct the default instance. We can't call InitAsDefaultInstance() yet
- // because we need to make sure all default instances that this one might
- // depend on are constructed first.
- printer->Print(
- "$classname$::default_instance_ = new $classname$();\n",
- "classname", classname_);
+uint32 CalculateType(uint32 type, uint32 type_class) {
+ return (type - 1) + type_class * 20;
+}
- if ((descriptor_->oneof_decl_count() > 0) &&
- HasDescriptorMethods(descriptor_->file())) {
- printer->Print(
- "$classname$_default_oneof_instance_ = new $classname$OneofInstance();\n",
- "classname", classname_);
+uint32 CalcFieldNum(const FieldGenerator&, const FieldDescriptor* field,
+ const Options& options) {
+ bool is_a_map = IsMapEntryMessage(field->containing_type());
+ int type = field->type();
+ if (field->containing_oneof()) {
+ return CalculateType(type, 4);
+ }
+ if (field->is_packed()) {
+ return CalculateType(type, 3);
+ } else if (field->is_repeated()) {
+ return CalculateType(type, 2);
+ } else if (!HasFieldPresence(field->file()) &&
+ field->containing_oneof() == NULL && !is_a_map) {
+ return CalculateType(type, 1);
+ } else {
+ return CalculateType(type, 0);
}
+}
- // Handle nested types.
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateDefaultInstanceAllocator(printer);
+#else
+// We need to calculate for each field what function the table driven code
+// should use to serialize it. This returns the index in a lookup table.
+uint32 CalcFieldNum(const FieldGenerator& generator,
+ const FieldDescriptor* field, const Options& options) {
+ bool is_a_map = IsMapEntryMessage(field->containing_type());
+ int type = field->type();
+ if (type == FieldDescriptor::TYPE_STRING ||
+ type == FieldDescriptor::TYPE_BYTES) {
+ if (generator.IsInlined()) {
+ type = internal::FieldMetadata::kInlinedType;
+ }
+ }
+ if (field->containing_oneof()) {
+ return internal::FieldMetadata::CalculateType(
+ type, internal::FieldMetadata::kOneOf);
+ }
+ if (field->is_packed()) {
+ return internal::FieldMetadata::CalculateType(
+ type, internal::FieldMetadata::kPacked);
+ } else if (field->is_repeated()) {
+ return internal::FieldMetadata::CalculateType(
+ type, internal::FieldMetadata::kRepeated);
+ } else if (!HasFieldPresence(field->file()) &&
+ field->containing_oneof() == NULL && !is_a_map) {
+ return internal::FieldMetadata::CalculateType(
+ type, internal::FieldMetadata::kNoPresence);
+ } else {
+ return internal::FieldMetadata::CalculateType(
+ type, internal::FieldMetadata::kPresence);
}
+}
+#endif
+int FindMessageIndexInFile(const Descriptor* descriptor) {
+ std::vector<const Descriptor*> flatten =
+ FlattenMessagesInFile(descriptor->file());
+ return std::find(flatten.begin(), flatten.end(), descriptor) -
+ flatten.begin();
}
-void MessageGenerator::
-GenerateDefaultInstanceInitializer(io::Printer* printer) {
+} // namespace
+
+int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) {
+ if (!options_.table_driven_serialization) {
+ return 0;
+ }
+
+ string full_classname = QualifiedClassName(descriptor_);
+
+ std::vector<const FieldDescriptor*> sorted = SortFieldsByNumber(descriptor_);
+ if (IsMapEntryMessage(descriptor_)) {
+ for (int i = 0; i < 2; i++) {
+ const FieldDescriptor* field = sorted[i];
+ const FieldGenerator& generator = field_generators_.get(field);
+
+ uint32 tag = internal::WireFormatLite::MakeTag(
+ field->number(), WireFormat::WireTypeForFieldType(field->type()));
+
+ std::map<string, string> vars;
+ vars["classname"] = QualifiedClassName(descriptor_);
+ vars["field_name"] = FieldName(field);
+ vars["tag"] = SimpleItoa(tag);
+ vars["hasbit"] = SimpleItoa(i);
+ vars["type"] = SimpleItoa(CalcFieldNum(generator, field, options_));
+ vars["ptr"] = "NULL";
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ GOOGLE_CHECK(!IsMapEntryMessage(field->message_type()));
+ {
+ vars["ptr"] =
+ "::" + FileLevelNamespace(field->message_type()) +
+ "::TableStruct::serialization_table + " +
+ SimpleItoa(FindMessageIndexInFile(field->message_type()));
+ }
+ }
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "::google::protobuf::internal::MapEntryHelper<$classname$::"
+ "SuperType>, $field_name$_), $tag$,"
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "::google::protobuf::internal::MapEntryHelper<$classname$::"
+ "SuperType>, _has_bits_) * 8 + $hasbit$, $type$, "
+ "$ptr$},\n");
+ }
+ return 2;
+ }
printer->Print(
- "$classname$::default_instance_->InitAsDefaultInstance();\n",
- "classname", classname_);
-
- // Register extensions.
- for (int i = 0; i < descriptor_->extension_count(); i++) {
- extension_generators_[i]->GenerateRegistration(printer);
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_cached_size_), 0, 0, 0, NULL},\n",
+ "classname", full_classname);
+ std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
+ for (int i = 0; i < descriptor_->extension_range_count(); ++i) {
+ sorted_extensions.push_back(descriptor_->extension_range(i));
}
+ std::sort(sorted_extensions.begin(), sorted_extensions.end(),
+ ExtensionRangeSorter());
+ for (int i = 0, extension_idx = 0; /* no range */; i++) {
+ for (; extension_idx < sorted_extensions.size() &&
+ (i == sorted.size() ||
+ sorted_extensions[extension_idx]->start < sorted[i]->number());
+ extension_idx++) {
+ const Descriptor::ExtensionRange* range =
+ sorted_extensions[extension_idx];
+ printer->Print(
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_extensions_), $start$, $end$, "
+ "::google::protobuf::internal::FieldMetadata::kSpecial, "
+ "reinterpret_cast<const "
+ "void*>(::google::protobuf::internal::ExtensionSerializer)},\n",
+ "classname", full_classname, "start", SimpleItoa(range->start), "end",
+ SimpleItoa(range->end));
+ }
+ if (i == sorted.size()) break;
+ const FieldDescriptor* field = sorted[i];
+
+ uint32 tag = internal::WireFormatLite::MakeTag(
+ field->number(), WireFormat::WireTypeForFieldType(field->type()));
+ if (field->is_packed()) {
+ tag = internal::WireFormatLite::MakeTag(
+ field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
+ }
- // Handle nested types.
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need to initialize default instance manually.
- // Since map entry message cannot be a top level class, we just need to
- // avoid calling DefaultInstanceInitializer here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateDefaultInstanceInitializer(printer);
- }
-}
+ string classfieldname = FieldName(field);
+ if (field->containing_oneof()) {
+ classfieldname = field->containing_oneof()->name();
+ }
+ std::map<string, string> vars;
+ vars["classname"] = full_classname;
+ vars["field_name"] = classfieldname;
+ vars["tag"] = SimpleItoa(tag);
+ vars["ptr"] = "NULL";
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ if (IsMapEntryMessage(field->message_type())) {
+ vars["idx"] = SimpleItoa(FindMessageIndexInFile(field->message_type()));
+ vars["fieldclassname"] = QualifiedClassName(field->message_type());
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, $field_name$_), $tag$, $idx$, "
+ "::google::protobuf::internal::FieldMetadata::kSpecial, "
+ "reinterpret_cast<const void*>(static_cast< "
+ "::google::protobuf::internal::SpecialSerializer>("
+ "::google::protobuf::internal::MapFieldSerializer< "
+ "::google::protobuf::internal::MapEntryToMapField<"
+ "$fieldclassname$>::MapFieldType, "
+ "TableStruct::serialization_table>))},\n");
+ continue;
+ } else {
+ vars["ptr"] =
+ "::" + FileLevelNamespace(field->message_type()) +
+ "::TableStruct::serialization_table + " +
+ SimpleItoa(FindMessageIndexInFile(field->message_type()));
+ }
+ }
-void MessageGenerator::
-GenerateShutdownCode(io::Printer* printer) {
- printer->Print(
- "delete $classname$::default_instance_;\n",
- "classname", classname_);
+ const FieldGenerator& generator = field_generators_.get(field);
+ vars["type"] = SimpleItoa(CalcFieldNum(generator, field, options_));
- if (HasDescriptorMethods(descriptor_->file())) {
- if (descriptor_->oneof_decl_count() > 0) {
- printer->Print(
- "delete $classname$_default_oneof_instance_;\n",
- "classname", classname_);
+
+ if (field->options().weak()) {
+ // TODO(gerbens) merge weak fields into ranges
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, _weak_field_map_), $tag$, $tag$, "
+ "::google::protobuf::internal::FieldMetadata::kSpecial, "
+ "reinterpret_cast<const "
+ "void*>(::google::protobuf::internal::WeakFieldSerializer)},\n");
+ } else if (field->containing_oneof()) {
+ vars["oneofoffset"] =
+ SimpleItoa(sizeof(uint32) * field->containing_oneof()->index());
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, $field_name$_), $tag$, "
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, _oneof_case_) + $oneofoffset$, "
+ "$type$, $ptr$},\n");
+ } else if (HasFieldPresence(descriptor_->file()) &&
+ has_bit_indices_[field->index()] != -1) {
+ vars["hasbitsoffset"] = SimpleItoa(has_bit_indices_[field->index()]);
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, $field_name$_), $tag$, "
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, _has_bits_) * 8 + $hasbitsoffset$, $type$, "
+ "$ptr$},\n");
+ } else {
+ printer->Print(vars,
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($"
+ "classname$, $field_name$_), $tag$, ~0u, $type$, "
+ "$ptr$},\n");
}
- printer->Print(
- "delete $classname$_reflection_;\n",
- "classname", classname_);
}
+ int num_field_metadata = 1 + sorted.size() + sorted_extensions.size();
+ num_field_metadata++;
+ string serializer = UseUnknownFieldSet(descriptor_->file(), options_)
+ ? "::google::protobuf::internal::UnknownFieldSetSerializer"
+ : "::google::protobuf::internal::UnknownFieldSerializerLite";
+ printer->Print(
+ "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_internal_metadata_), 0, ~0u, "
+ "::google::protobuf::internal::FieldMetadata::kSpecial, reinterpret_cast<const "
+ "void*>($serializer$)},\n",
+ "classname", full_classname, "serializer", serializer);
+ return num_field_metadata;
+}
- // Handle default instances of fields.
+void MessageGenerator::GenerateFieldDefaultInstances(io::Printer* printer) {
+ // Construct the default instances for all fields that need one.
for (int i = 0; i < descriptor_->field_count(); i++) {
field_generators_.get(descriptor_->field(i))
- .GenerateShutdownCode(printer);
+ .GenerateDefaultInstanceAllocator(printer);
}
+}
- // Handle nested types.
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateShutdownCode(printer);
+void MessageGenerator::
+GenerateDefaultInstanceInitializer(io::Printer* printer) {
+ // The default instance needs all of its embedded message pointers
+ // cross-linked to other default instances. We can't do this initialization
+ // in the constructor because some other default instances may not have been
+ // constructed yet at that time.
+ // TODO(kenton): Maybe all message fields (even for non-default messages)
+ // should be initialized to point at default instances rather than NULL?
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+
+ if (!field->is_repeated() &&
+ field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
+ (field->containing_oneof() == NULL ||
+ HasDescriptorMethods(descriptor_->file(), options_))) {
+ string name;
+ if (field->containing_oneof() || field->options().weak()) {
+ name = "_" + classname_ + "_default_instance_.";
+ } else {
+ name =
+ "_" + classname_ + "_default_instance_._instance.get_mutable()->";
+ }
+ name += FieldName(field);
+ printer->Print(
+ "$ns$::$name$_ = const_cast< $type$*>(\n"
+ " $type$::internal_default_instance());\n",
+ // Vars.
+ "name", name, "type", FieldMessageTypeName(field), "ns",
+ Namespace(descriptor_));
+ } else if (field->containing_oneof() &&
+ HasDescriptorMethods(descriptor_->file(), options_)) {
+ field_generators_.get(descriptor_->field(i))
+ .GenerateConstructorCode(printer);
+ }
}
}
void MessageGenerator::
GenerateClassMethods(io::Printer* printer) {
- // mutable_unknown_fields wrapper function for LazyStringOutputStream
- // callback.
- if (!UseUnknownFieldSet(descriptor_->file())) {
+ if (IsMapEntryMessage(descriptor_)) {
printer->Print(
- "static ::std::string* MutableUnknownFieldsFor$classname$(\n"
- " $classname$* ptr) {\n"
- " return ptr->mutable_unknown_fields();\n"
- "}\n"
- "\n",
+ "$classname$::$classname$() {}\n"
+ "$classname$::$classname$(::google::protobuf::Arena* arena) : "
+ "SuperType(arena) {}\n"
+ "void $classname$::MergeFrom(const $classname$& other) {\n"
+ " MergeFromInternal(other);\n"
+ "}\n",
"classname", classname_);
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
+ printer->Print(
+ "::google::protobuf::Metadata $classname$::GetMetadata() const {\n"
+ " ::$file_namespace$::protobuf_AssignDescriptorsOnce();\n"
+ " return ::$file_namespace$::file_level_metadata[$index$];\n"
+ "}\n"
+ "void $classname$::MergeFrom(\n"
+ " const ::google::protobuf::Message& other) {\n"
+ " ::google::protobuf::Message::MergeFrom(other);\n"
+ "}\n"
+ "\n",
+ "file_namespace", FileLevelNamespace(descriptor_),
+ "classname", classname_, "index",
+ SimpleItoa(index_in_file_messages_));
+ }
+ return;
}
+
+ // TODO(gerbens) Remove this function. With a little bit of cleanup and
+ // refactoring this is superfluous.
+ printer->Print("void $classname$::InitAsDefaultInstance() {\n", "classname",
+ classname_);
+ printer->Indent();
+ GenerateDefaultInstanceInitializer(printer);
+ printer->Outdent();
+ printer->Print("}\n");
+
if (IsAnyMessage(descriptor_)) {
printer->Print(
"void $classname$::PackFrom(const ::google::protobuf::Message& message) {\n"
" _any_metadata_.PackFrom(message);\n"
"}\n"
"\n"
+ "void $classname$::PackFrom(const ::google::protobuf::Message& message,\n"
+ " const ::std::string& type_url_prefix) {\n"
+ " _any_metadata_.PackFrom(message, type_url_prefix);\n"
+ "}\n"
+ "\n"
"bool $classname$::UnpackTo(::google::protobuf::Message* message) const {\n"
" return _any_metadata_.UnpackTo(message);\n"
"}\n"
+ "bool $classname$::ParseAnyTypeUrl(const string& type_url,\n"
+ " string* full_type_name) {\n"
+ " return ::google::protobuf::internal::ParseAnyTypeUrl(type_url,\n"
+ " full_type_name);\n"
+ "}\n"
"\n",
"classname", classname_);
}
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->GenerateMethods(printer);
- }
-
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- // map entry message doesn't need class methods. Since map entry message
- // cannot be a top level class, we just need to avoid calling
- // GenerateClassMethods here.
- if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
- nested_generators_[i]->GenerateClassMethods(printer);
- printer->Print("\n");
- printer->Print(kThinSeparator);
- printer->Print("\n");
- }
-
// Generate non-inline field definitions.
for (int i = 0; i < descriptor_->field_count(); i++) {
- field_generators_.get(descriptor_->field(i))
+ const FieldDescriptor* field = descriptor_->field(i);
+ field_generators_.get(field)
.GenerateNonInlineAccessorDefinitions(printer);
+ if (IsCrossFileMaybeMap(field)) {
+ std::map<string, string> vars;
+ SetCommonFieldVariables(field, &vars, options_);
+ if (field->containing_oneof()) {
+ SetCommonOneofFieldVariables(field, &vars);
+ }
+ GenerateFieldClear(field, vars, false, printer);
+ }
}
// Generate field number constants.
@@ -1830,11 +1822,6 @@ GenerateClassMethods(io::Printer* printer) {
"#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"
"\n");
- // Define extension identifiers.
- for (int i = 0; i < descriptor_->extension_count(); i++) {
- extension_generators_[i]->GenerateDefinition(printer);
- }
-
GenerateStructors(printer);
printer->Print("\n");
@@ -1843,7 +1830,7 @@ GenerateClassMethods(io::Printer* printer) {
printer->Print("\n");
}
- if (HasGeneratedMethods(descriptor_->file())) {
+ if (HasGeneratedMethods(descriptor_->file(), options_)) {
GenerateClear(printer);
printer->Print("\n");
@@ -1853,7 +1840,7 @@ GenerateClassMethods(io::Printer* printer) {
GenerateSerializeWithCachedSizes(printer);
printer->Print("\n");
- if (HasFastArraySerialization(descriptor_->file())) {
+ if (HasFastArraySerialization(descriptor_->file(), options_)) {
GenerateSerializeWithCachedSizesToArray(printer);
printer->Print("\n");
}
@@ -1874,17 +1861,26 @@ GenerateClassMethods(io::Printer* printer) {
GenerateSwap(printer);
printer->Print("\n");
- if (HasDescriptorMethods(descriptor_->file())) {
+ if (options_.table_driven_serialization) {
printer->Print(
- "::google::protobuf::Metadata $classname$::GetMetadata() const {\n"
- " protobuf_AssignDescriptorsOnce();\n"
- " ::google::protobuf::Metadata metadata;\n"
- " metadata.descriptor = $classname$_descriptor_;\n"
- " metadata.reflection = $classname$_reflection_;\n"
- " return metadata;\n"
- "}\n"
- "\n",
- "classname", classname_);
+ "const void* $classname$::InternalGetTable() const {\n"
+ " return ::$file_namespace$::TableStruct::serialization_table + "
+ "$index$;\n"
+ "}\n"
+ "\n",
+ "classname", classname_, "index", SimpleItoa(index_in_file_messages_),
+ "file_namespace", FileLevelNamespace(descriptor_));
+ }
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
+ printer->Print(
+ "::google::protobuf::Metadata $classname$::GetMetadata() const {\n"
+ " $file_namespace$::protobuf_AssignDescriptorsOnce();\n"
+ " return ::"
+ "$file_namespace$::file_level_metadata[kIndexInFileMessages];\n"
+ "}\n"
+ "\n",
+ "classname", classname_, "file_namespace",
+ FileLevelNamespace(descriptor_));
} else {
printer->Print(
"::std::string $classname$::GetTypeName() const {\n"
@@ -1897,79 +1893,283 @@ GenerateClassMethods(io::Printer* printer) {
}
-void MessageGenerator::
-GenerateOffsets(io::Printer* printer) {
- printer->Print(
- "static const int $classname$_offsets_[$field_count$] = {\n",
- "classname", classname_,
- "field_count", SimpleItoa(max(
- 1, descriptor_->field_count() + descriptor_->oneof_decl_count())));
- printer->Indent();
+size_t MessageGenerator::GenerateParseOffsets(io::Printer* printer) {
+ if (!table_driven_) {
+ return 0;
+ }
+
+ // Field "0" is special: We use it in our switch statement of processing
+ // types to handle the successful end tag case.
+ printer->Print("{0, 0, 0, ::google::protobuf::internal::kInvalidMask, 0, 0},\n");
+ int last_field_number = 1;
+
+ std::vector<const FieldDescriptor*> ordered_fields =
+ SortFieldsByNumber(descriptor_);
for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
- if (field->containing_oneof()) {
+ const FieldDescriptor* field = ordered_fields[i];
+ GOOGLE_CHECK_GE(field->number(), last_field_number);
+
+ for (; last_field_number < field->number(); last_field_number++) {
printer->Print(
- "PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET("
- "$classname$_default_oneof_instance_, $name$_),\n",
- "classname", classname_,
- "name", FieldName(field));
+ "{ 0, 0, ::google::protobuf::internal::kInvalidMask,\n"
+ " ::google::protobuf::internal::kInvalidMask, 0, 0 },\n");
+ }
+ last_field_number++;
+
+ unsigned char normal_wiretype, packed_wiretype, processing_type;
+ normal_wiretype = WireFormat::WireTypeForFieldType(field->type());
+
+ if (field->is_packable()) {
+ packed_wiretype = WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
} else {
- printer->Print(
- "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
- "$name$_),\n",
- "classname", classname_,
- "name", FieldName(field));
+ packed_wiretype = internal::kNotPackedMask;
}
- }
- for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
- const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
- printer->Print(
- "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_),\n",
- "classname", classname_,
- "name", oneof->name());
+ processing_type = static_cast<unsigned>(field->type());
+ const FieldGenerator& generator = field_generators_.get(field);
+ if (field->type() == FieldDescriptor::TYPE_STRING) {
+ switch (EffectiveStringCType(field)) {
+ case FieldOptions::STRING:
+ default: {
+ if (generator.IsInlined()) {
+ processing_type = internal::TYPE_STRING_INLINED;
+ break;
+ }
+ break;
+ }
+ }
+ } else if (field->type() == FieldDescriptor::TYPE_BYTES) {
+ switch (EffectiveStringCType(field)) {
+ case FieldOptions::STRING:
+ default:
+ if (generator.IsInlined()) {
+ processing_type = internal::TYPE_BYTES_INLINED;
+ break;
+ }
+ break;
+ }
+ }
+
+ processing_type |= static_cast<unsigned>(
+ field->is_repeated() ? internal::kRepeatedMask : 0);
+ processing_type |= static_cast<unsigned>(
+ field->containing_oneof() ? internal::kOneofMask : 0);
+
+ if (field->is_map()) {
+ processing_type = internal::TYPE_MAP;
+ }
+
+ const unsigned char tag_size =
+ WireFormat::TagSize(field->number(), field->type());
+
+ std::map<string, string> vars;
+ vars["classname"] = QualifiedClassName(descriptor_);
+ if (field->containing_oneof() != NULL) {
+ vars["name"] = field->containing_oneof()->name();
+ vars["presence"] = SimpleItoa(field->containing_oneof()->index());
+ } else {
+ vars["name"] = FieldName(field);
+ vars["presence"] = SimpleItoa(has_bit_indices_[field->index()]);
+ }
+ vars["nwtype"] = SimpleItoa(normal_wiretype);
+ vars["pwtype"] = SimpleItoa(packed_wiretype);
+ vars["ptype"] = SimpleItoa(processing_type);
+ vars["tag_size"] = SimpleItoa(tag_size);
+
+ printer->Print(vars,
+ "{\n"
+ " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n"
+ " $classname$, $name$_),\n"
+ " static_cast<::google::protobuf::uint32>($presence$),\n"
+ " $nwtype$, $pwtype$, $ptype$, $tag_size$\n"
+ "},\n");
}
- printer->Outdent();
- printer->Print("};\n");
+ return last_field_number;
}
-void MessageGenerator::
-GenerateSharedConstructorCode(io::Printer* printer) {
- printer->Print(
- "void $classname$::SharedCtor() {\n",
- "classname", classname_);
- printer->Indent();
+size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) {
+ if (!table_driven_) {
+ return 0;
+ }
- if (!HasFieldPresence(descriptor_->file())) {
- printer->Print(
- " _is_default_instance_ = false;\n");
+ std::vector<const FieldDescriptor*> ordered_fields =
+ SortFieldsByNumber(descriptor_);
+
+ printer->Print("::google::protobuf::internal::AuxillaryParseTableField(),\n");
+ int last_field_number = 1;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = ordered_fields[i];
+
+ GOOGLE_CHECK_GE(field->number(), last_field_number);
+ for (; last_field_number < field->number(); last_field_number++) {
+ printer->Print("::google::protobuf::internal::AuxillaryParseTableField(),\n");
+ }
+
+ std::map<string, string> vars;
+ SetCommonFieldVariables(field, &vars, options_);
+
+ switch (field->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_ENUM:
+ vars["type"] = ClassName(field->enum_type(), true);
+ printer->Print(
+ vars,
+ "{::google::protobuf::internal::AuxillaryParseTableField::enum_aux{"
+ "$type$_IsValid}},\n");
+ last_field_number++;
+ break;
+ case FieldDescriptor::CPPTYPE_MESSAGE: {
+ if (field->is_map()) {
+ vars["classname"] = QualifiedClassName(field->message_type());
+ printer->Print(vars,
+ "{::google::protobuf::internal::AuxillaryParseTableField::map_"
+ "aux{&::google::protobuf::internal::ParseMap<$classname$>}},\n");
+ last_field_number++;
+ break;
+ } else {
+ vars["classname"] = ClassName(field->message_type(), false);
+ }
+ vars["ns"] = Namespace(field->message_type());
+ vars["type"] = FieldMessageTypeName(field);
+ vars["file_namespace"] =
+ FileLevelNamespace(field->message_type());
+
+ printer->Print(
+ vars,
+ "{::google::protobuf::internal::AuxillaryParseTableField::message_aux{\n"
+ " &$ns$::_$classname$_default_instance_}},\n");
+ last_field_number++;
+ break;
+ }
+ case FieldDescriptor::CPPTYPE_STRING:
+ switch (EffectiveStringCType(field)) {
+ case FieldOptions::STRING:
+ vars["default"] =
+ field->default_value_string().empty()
+ ? "&::google::protobuf::internal::fixed_address_empty_string"
+ : "&" + Namespace(field) + " ::" + classname_ +
+ "::" + MakeDefaultName(field);
+ break;
+ case FieldOptions::CORD:
+ case FieldOptions::STRING_PIECE:
+ vars["default"] =
+ "\"" + CEscape(field->default_value_string()) + "\"";
+ break;
+ }
+ vars["full_name"] = field->full_name();
+ printer->Print(vars,
+ "{::google::protobuf::internal::AuxillaryParseTableField::string_aux{\n"
+ " $default$,\n"
+ " \"$full_name$\"\n"
+ "}},\n");
+ last_field_number++;
+ break;
+ default:
+ break;
+ }
}
- printer->Print(StrCat(
- uses_string_ ? "::google::protobuf::internal::GetEmptyString();\n" : "",
- "_cached_size_ = 0;\n").c_str());
+ return last_field_number;
+}
- if (PreserveUnknownFields(descriptor_) &&
- !UseUnknownFieldSet(descriptor_->file())) {
+std::pair<size_t, size_t> MessageGenerator::GenerateOffsets(
+ io::Printer* printer) {
+ std::map<string, string> variables;
+ string full_classname = QualifiedClassName(descriptor_);
+ variables["classname"] = full_classname;
+
+ if (HasFieldPresence(descriptor_->file()) || IsMapEntryMessage(descriptor_)) {
printer->Print(
- "_unknown_fields_.UnsafeSetDefault(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n");
+ variables,
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_has_bits_),\n");
+ } else {
+ printer->Print("~0u, // no _has_bits_\n");
}
-
+ printer->Print(variables,
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_internal_metadata_),\n");
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print(
+ variables,
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, "
+ "_extensions_),\n");
+ } else {
+ printer->Print("~0u, // no _extensions_\n");
+ }
+ if (descriptor_->oneof_decl_count() > 0) {
+ printer->Print(variables,
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "$classname$, _oneof_case_[0]),\n");
+ } else {
+ printer->Print("~0u, // no _oneof_case_\n");
+ }
+ if (num_weak_fields_ > 0) {
+ printer->Print(variables,
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$,"
+ " _weak_field_map_),\n");
+ } else {
+ printer->Print("~0u, // no _weak_field_map_\n");
+ }
+ const int kNumGenericOffsets = 5; // the number of fixed offsets above
+ const size_t offsets = kNumGenericOffsets +
+ descriptor_->field_count() +
+ descriptor_->oneof_decl_count();
+ size_t entries = offsets;
for (int i = 0; i < descriptor_->field_count(); i++) {
- if (!descriptor_->field(i)->containing_oneof()) {
- field_generators_.get(descriptor_->field(i))
- .GenerateConstructorCode(printer);
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (field->containing_oneof() || field->options().weak()) {
+ printer->Print("offsetof($classname$DefaultTypeInternal, $name$_)",
+ "classname", full_classname, "name", FieldName(field));
+ } else {
+ printer->Print(
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_)",
+ "classname", full_classname, "name", FieldName(field));
}
+
+ uint32 tag = field_generators_.get(field).CalculateFieldTag();
+ if (tag != 0) {
+ printer->Print(" | $tag$", "tag", SimpleItoa(tag));
+ }
+
+ printer->Print(",\n");
}
- if (HasFieldPresence(descriptor_->file())) {
+ for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
+ const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
+ printer->Print(
+ "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, $name$_),\n",
+ "classname", full_classname, "name", oneof->name());
+ }
+
+ if (IsMapEntryMessage(descriptor_)) {
+ entries += 2;
printer->Print(
- "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
+ "0,\n"
+ "1,\n");
+ } else if (HasFieldPresence(descriptor_->file())) {
+ entries += has_bit_indices_.size();
+ for (int i = 0; i < has_bit_indices_.size(); i++) {
+ const string index = has_bit_indices_[i] >= 0 ?
+ SimpleItoa(has_bit_indices_[i]) : "~0u";
+ printer->Print("$index$,\n", "index", index);
+ }
}
+ return std::make_pair(entries, offsets);
+}
+
+void MessageGenerator::
+GenerateSharedConstructorCode(io::Printer* printer) {
+ printer->Print(
+ "void $classname$::SharedCtor() {\n",
+ "classname", classname_);
+ printer->Indent();
+
+ std::vector<bool> processed(optimized_order_.size(), false);
+ GenerateConstructorBody(printer, processed, false);
+
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
"clear_has_$oneof_name$();\n",
@@ -1987,35 +2187,14 @@ GenerateSharedDestructorCode(io::Printer* printer) {
"classname", classname_);
printer->Indent();
if (SupportsArenas(descriptor_)) {
- // Do nothing when the message is allocated in an arena.
printer->Print(
- "if (GetArenaNoVirtual() != NULL) {\n"
- " return;\n"
- "}\n"
- "\n");
+ "GOOGLE_DCHECK(GetArenaNoVirtual() == NULL);\n");
}
-
- // Write the desctructor for _unknown_fields_ in lite runtime.
- if (PreserveUnknownFields(descriptor_) &&
- !UseUnknownFieldSet(descriptor_->file())) {
- if (SupportsArenas(descriptor_)) {
- printer->Print(
- "_unknown_fields_.Destroy(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited(),\n"
- " GetArenaNoVirtual());\n");
- } else {
- printer->Print(
- "_unknown_fields_.DestroyNoArena(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n");
- }
- }
-
// Write the destructors for each field except oneof members.
- for (int i = 0; i < descriptor_->field_count(); i++) {
- if (!descriptor_->field(i)->containing_oneof()) {
- field_generators_.get(descriptor_->field(i))
- .GenerateDestructorCode(printer);
- }
+ // optimized_order_ does not contain oneof fields.
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ field_generators_.get(field).GenerateDestructorCode(printer);
}
// Generate code to destruct oneofs. Clearing should do the work.
@@ -2027,34 +2206,11 @@ GenerateSharedDestructorCode(io::Printer* printer) {
"oneof_name", descriptor_->oneof_decl(i)->name());
}
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- "if (this != default_instance_) {\n",
- // Without.
- "if (this != &default_instance()) {\n");
-
- // We need to delete all embedded messages.
- // TODO(kenton): If we make unset messages point at default instances
- // instead of NULL, then it would make sense to move this code into
- // MessageFieldGenerator::GenerateDestructorCode().
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
-
- if (!field->is_repeated() &&
- field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
- // Skip oneof members
- if (!field->containing_oneof()) {
- printer->Print(
- " delete $name$_;\n",
- "name", FieldName(field));
- }
- }
+ if (num_weak_fields_) {
+ printer->Print("_weak_field_map_.ClearAll();\n");
}
-
printer->Outdent();
printer->Print(
- " }\n"
"}\n"
"\n");
}
@@ -2079,12 +2235,37 @@ GenerateArenaDestructorCode(io::Printer* printer) {
"classname", classname_);
bool need_registration = false;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- if (field_generators_.get(descriptor_->field(i))
+ // Process non-oneof fields first.
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ if (field_generators_.get(field)
.GenerateArenaDestructorCode(printer)) {
need_registration = true;
}
}
+
+ // Process oneof fields.
+ //
+ // Note: As of 10/5/2016, GenerateArenaDestructorCode does not emit anything
+ // and returns false for oneof fields.
+ for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
+ const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
+
+ for (int j = 0; j < oneof->field_count(); j++) {
+ const FieldDescriptor* field = oneof->field(j);
+ if (field_generators_.get(field)
+ .GenerateArenaDestructorCode(printer)) {
+ need_registration = true;
+ }
+ }
+ }
+ if (num_weak_fields_) {
+ // _this is the object being destructed (we are inside a static method
+ // here).
+ printer->Print("_this->_weak_field_map_.ClearAll();\n");
+ need_registration = true;
+ }
+
printer->Outdent();
printer->Print(
"}\n");
@@ -2099,153 +2280,253 @@ GenerateArenaDestructorCode(io::Printer* printer) {
"classname", classname_);
} else {
printer->Print(
- "void $classname$::RegisterArenaDtor(::google::protobuf::Arena* arena) {\n"
+ "void $classname$::RegisterArenaDtor(::google::protobuf::Arena*) {\n"
"}\n",
"classname", classname_);
}
}
+void MessageGenerator::GenerateConstructorBody(io::Printer* printer,
+ std::vector<bool> processed,
+ bool copy_constructor) const {
+ const FieldDescriptor* last_start = NULL;
+ // RunMap maps from fields that start each run to the number of fields in that
+ // run. This is optimized for the common case that there are very few runs in
+ // a message and that most of the eligible fields appear together.
+ typedef hash_map<const FieldDescriptor*, size_t> RunMap;
+ RunMap runs;
+
+ for (int i = 0; i < optimized_order_.size(); ++i) {
+ const FieldDescriptor* field = optimized_order_[i];
+ if ((copy_constructor && IsPOD(field)) ||
+ (!copy_constructor && CanConstructByZeroing(field, options_))) {
+ if (last_start == NULL) {
+ last_start = field;
+ }
+
+ runs[last_start]++;
+ } else {
+ last_start = NULL;
+ }
+ }
+
+ string pod_template;
+ if (copy_constructor) {
+ pod_template =
+ "::memcpy(&$first$_, &from.$first$_,\n"
+ " static_cast<size_t>(reinterpret_cast<char*>(&$last$_) -\n"
+ " reinterpret_cast<char*>(&$first$_)) + sizeof($last$_));\n";
+ } else {
+ pod_template =
+ "::memset(&$first$_, 0, static_cast<size_t>(\n"
+ " reinterpret_cast<char*>(&$last$_) -\n"
+ " reinterpret_cast<char*>(&$first$_)) + sizeof($last$_));\n";
+ }
+
+ for (int i = 0; i < optimized_order_.size(); ++i) {
+ if (processed[i]) {
+ continue;
+ }
+
+ const FieldDescriptor* field = optimized_order_[i];
+ RunMap::const_iterator it = runs.find(field);
+
+ // We only apply the memset technique to runs of more than one field, as
+ // assignment is better than memset for generated code clarity.
+ if (it != runs.end() && it->second > 1) {
+ // Use a memset, then skip run_length fields.
+ const size_t run_length = it->second;
+ const string first_field_name = FieldName(field);
+ const string last_field_name =
+ FieldName(optimized_order_[i + run_length - 1]);
+
+ printer->Print(pod_template.c_str(),
+ "first", first_field_name,
+ "last", last_field_name);
+
+ i += run_length - 1;
+ // ++i at the top of the loop.
+ } else {
+ if (copy_constructor) {
+ field_generators_.get(field).GenerateCopyConstructorCode(printer);
+ } else {
+ field_generators_.get(field).GenerateConstructorCode(printer);
+ }
+ }
+ }
+}
+
void MessageGenerator::
GenerateStructors(io::Printer* printer) {
string superclass;
- if (use_dependent_base_) {
- superclass =
- DependentBaseClassTemplateName(descriptor_) + "<" + classname_ + ">";
- } else {
- superclass = SuperClassName(descriptor_);
- }
+ superclass = SuperClassName(descriptor_, options_);
string initializer_with_arena = superclass + "()";
if (descriptor_->extension_range_count() > 0) {
initializer_with_arena += ",\n _extensions_(arena)";
}
- if (UseUnknownFieldSet(descriptor_->file())) {
- initializer_with_arena += ",\n _internal_metadata_(arena)";
- } else {
- initializer_with_arena += ",\n _arena_ptr_(arena)";
- }
+ initializer_with_arena += ",\n _internal_metadata_(arena)";
// Initialize member variables with arena constructor.
- for (int i = 0; i < descriptor_->field_count(); i++) {
- bool has_arena_constructor = descriptor_->field(i)->is_repeated();
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+
+ bool has_arena_constructor = field->is_repeated();
if (has_arena_constructor) {
initializer_with_arena += string(",\n ") +
- FieldName(descriptor_->field(i)) + string("_(arena)");
+ FieldName(field) + string("_(arena)");
}
}
if (IsAnyMessage(descriptor_)) {
- initializer_with_arena += ",\n _any_metadata_(&type_url, &value_)";
+ initializer_with_arena += ",\n _any_metadata_(&type_url_, &value_)";
+ }
+ if (num_weak_fields_ > 0) {
+ initializer_with_arena += ", _weak_field_map_(arena)";
}
- string initializer_null;
- initializer_null = (UseUnknownFieldSet(descriptor_->file()) ?
- ", _internal_metadata_(NULL)" : ", _arena_ptr_(NULL)");
+ string initializer_null = superclass + "(), _internal_metadata_(NULL)";
if (IsAnyMessage(descriptor_)) {
initializer_null += ", _any_metadata_(&type_url_, &value_)";
}
+ if (num_weak_fields_ > 0) {
+ initializer_null += ", _weak_field_map_(nullptr)";
+ }
printer->Print(
"$classname$::$classname$()\n"
- " : $superclass$()$initializer$ {\n"
+ " : $initializer$ {\n"
+ " ::google::protobuf::internal::InitSCC(\n"
+ " &$file_namespace$::scc_info_$scc_name$.base);\n"
" SharedCtor();\n"
" // @@protoc_insertion_point(constructor:$full_name$)\n"
"}\n",
- "classname", classname_,
- "superclass", superclass,
- "full_name", descriptor_->full_name(),
- "initializer", initializer_null);
+ "classname", classname_, "full_name", descriptor_->full_name(),
+ "scc_name", scc_name_, "initializer", initializer_null, "file_namespace",
+ FileLevelNamespace(descriptor_));
if (SupportsArenas(descriptor_)) {
printer->Print(
- "\n"
"$classname$::$classname$(::google::protobuf::Arena* arena)\n"
" : $initializer$ {\n"
+ " "
+ "::google::protobuf::internal::InitSCC(&$file_namespace$::scc_info_$scc_name$."
+ "base);\n"
" SharedCtor();\n"
" RegisterArenaDtor(arena);\n"
" // @@protoc_insertion_point(arena_constructor:$full_name$)\n"
"}\n",
- "initializer", initializer_with_arena,
- "classname", classname_,
- "superclass", superclass,
- "full_name", descriptor_->full_name());
+ "initializer", initializer_with_arena, "classname", classname_,
+ "superclass", superclass, "full_name", descriptor_->full_name(),
+ "scc_name", scc_name_, "file_namespace",
+ FileLevelNamespace(descriptor_));
}
- printer->Print(
- "\n"
- "void $classname$::InitAsDefaultInstance() {\n",
- "classname", classname_);
+ // Generate the copy constructor.
+ if (UsingImplicitWeakFields(descriptor_->file(), options_)) {
+ // If we are in lite mode and using implicit weak fields, we generate a
+ // one-liner copy constructor that delegates to MergeFrom. This saves some
+ // code size and also cuts down on the complexity of implicit weak fields.
+ // We might eventually want to do this for all lite protos.
+ printer->Print(
+ "$classname$::$classname$(const $classname$& from)\n"
+ " : $classname$() {\n"
+ " MergeFrom(from);\n"
+ "}\n",
+ "classname", classname_);
+ } else {
+ printer->Print(
+ "$classname$::$classname$(const $classname$& from)\n"
+ " : $superclass$()",
+ "classname", classname_,
+ "superclass", superclass,
+ "full_name", descriptor_->full_name());
+ printer->Indent();
+ printer->Indent();
+ printer->Indent();
- if (!HasFieldPresence(descriptor_->file())) {
printer->Print(
- " _is_default_instance_ = true;\n");
- }
+ ",\n_internal_metadata_(NULL)");
- // The default instance needs all of its embedded message pointers
- // cross-linked to other default instances. We can't do this initialization
- // in the constructor because some other default instances may not have been
- // constructed yet at that time.
- // TODO(kenton): Maybe all message fields (even for non-default messages)
- // should be initialized to point at default instances rather than NULL?
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(",\n_has_bits_(from._has_bits_)");
+ }
- if (!field->is_repeated() &&
- field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
- (field->containing_oneof() == NULL ||
- HasDescriptorMethods(descriptor_->file()))) {
- string name;
- if (field->containing_oneof()) {
- name = classname_ + "_default_oneof_instance_->";
+ std::vector<bool> processed(optimized_order_.size(), false);
+ for (int i = 0; i < optimized_order_.size(); ++i) {
+ const FieldDescriptor* field = optimized_order_[i];
+
+ if (!(field->is_repeated() && !(field->is_map()))
+ ) {
+ continue;
}
- name += FieldName(field);
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " $name$_ = const_cast< $type$*>(&$type$::default_instance());\n",
- // Without.
- " $name$_ = const_cast< $type$*>(\n"
- " $type$::internal_default_instance());\n",
- // Vars.
- "name", name,
- "type", FieldMessageTypeName(field));
- } else if (field->containing_oneof() &&
- HasDescriptorMethods(descriptor_->file())) {
- field_generators_.get(descriptor_->field(i))
- .GenerateConstructorCode(printer);
+
+ processed[i] = true;
+ printer->Print(",\n$name$_(from.$name$_)",
+ "name", FieldName(field));
}
- }
- printer->Print(
- "}\n"
- "\n");
- // Generate the copy constructor.
- printer->Print(
- "$classname$::$classname$(const $classname$& from)\n"
- " : $superclass$()",
- "classname", classname_,
- "superclass", superclass,
- "full_name", descriptor_->full_name());
- if (UseUnknownFieldSet(descriptor_->file())) {
+ if (IsAnyMessage(descriptor_)) {
+ printer->Print(",\n_any_metadata_(&type_url_, &value_)");
+ }
+ if (num_weak_fields_ > 0) {
+ printer->Print(",\n_weak_field_map_(from._weak_field_map_)");
+ }
+
+ printer->Outdent();
+ printer->Outdent();
+ printer->Print(" {\n");
+
printer->Print(
- ",\n _internal_metadata_(NULL)");
- } else if (!UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(",\n _arena_ptr_(NULL)");
- }
- if (IsAnyMessage(descriptor_)) {
- printer->Print(",\n _any_metadata_(&type_url_, &value_)");
+ "_internal_metadata_.MergeFrom(from._internal_metadata_);\n");
+
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print("_extensions_.MergeFrom(from._extensions_);\n");
+ }
+
+ GenerateConstructorBody(printer, processed, true);
+
+ // Copy oneof fields. Oneof field requires oneof case check.
+ for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) {
+ printer->Print(
+ "clear_has_$oneofname$();\n"
+ "switch (from.$oneofname$_case()) {\n",
+ "oneofname", descriptor_->oneof_decl(i)->name());
+ printer->Indent();
+ for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
+ const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
+ printer->Print(
+ "case k$field_name$: {\n",
+ "field_name", UnderscoresToCamelCase(field->name(), true));
+ printer->Indent();
+ field_generators_.get(field).GenerateMergingCode(printer);
+ printer->Print(
+ "break;\n");
+ printer->Outdent();
+ printer->Print(
+ "}\n");
+ }
+ printer->Print(
+ "case $cap_oneof_name$_NOT_SET: {\n"
+ " break;\n"
+ "}\n",
+ "oneof_index",
+ SimpleItoa(descriptor_->oneof_decl(i)->index()),
+ "cap_oneof_name",
+ ToUpper(descriptor_->oneof_decl(i)->name()));
+ printer->Outdent();
+ printer->Print(
+ "}\n");
+ }
+
+ printer->Outdent();
+ printer->Print(
+ " // @@protoc_insertion_point(copy_constructor:$full_name$)\n"
+ "}\n"
+ "\n",
+ "full_name", descriptor_->full_name());
}
- printer->Print(" {\n");
- printer->Print(
- " SharedCtor();\n"
- " MergeFrom(from);\n"
- " // @@protoc_insertion_point(copy_constructor:$full_name$)\n"
- "}\n"
- "\n",
- "classname", classname_,
- "superclass", superclass,
- "full_name", descriptor_->full_name());
// Generate the shared constructor code.
GenerateSharedConstructorCode(printer);
@@ -2270,67 +2551,46 @@ GenerateStructors(io::Printer* printer) {
// Generate SetCachedSize.
printer->Print(
- "void $classname$::SetCachedSize(int size) const {\n"
- " GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
- " _cached_size_ = size;\n"
- " GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
- "}\n",
- "classname", classname_);
+ "void $classname$::SetCachedSize(int size) const {\n"
+ " _cached_size_.Set(size);\n"
+ "}\n",
+ "classname", classname_);
// Only generate this member if it's not disabled.
- if (HasDescriptorMethods(descriptor_->file()) &&
+ if (HasDescriptorMethods(descriptor_->file(), options_) &&
!descriptor_->options().no_standard_descriptor_accessor()) {
printer->Print(
- "const ::google::protobuf::Descriptor* $classname$::descriptor() {\n"
- " protobuf_AssignDescriptorsOnce();\n"
- " return $classname$_descriptor_;\n"
- "}\n"
- "\n",
- "classname", classname_,
- "adddescriptorsname",
- GlobalAddDescriptorsName(descriptor_->file()->name()));
+ "const ::google::protobuf::Descriptor* $classname$::descriptor() {\n"
+ " ::$file_namespace$::protobuf_AssignDescriptorsOnce();\n"
+ " return ::"
+ "$file_namespace$::file_level_metadata[kIndexInFileMessages]."
+ "descriptor;\n"
+ "}\n"
+ "\n",
+ "classname", classname_, "file_namespace",
+ FileLevelNamespace(descriptor_));
}
printer->Print(
- "const $classname$& $classname$::default_instance() {\n",
- "classname", classname_);
-
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " if (default_instance_ == NULL) $adddescriptorsname$();\n",
- // Without.
- " $adddescriptorsname$();\n",
- // Vars.
- "adddescriptorsname",
- GlobalAddDescriptorsName(descriptor_->file()->name()));
+ "const $classname$& $classname$::default_instance() {\n"
+ " "
+ "::google::protobuf::internal::InitSCC(&$file_namespace$::scc_info_$scc_name$.base)"
+ ";\n"
+ " return *internal_default_instance();\n"
+ "}\n\n",
+ "classname", classname_, "scc_name", scc_name_, "file_namespace",
+ FileLevelNamespace(descriptor_));
+}
+void MessageGenerator::GenerateSourceInProto2Namespace(io::Printer* printer) {
printer->Print(
- " return *default_instance_;\n"
- "}\n"
- "\n"
- "$classname$* $classname$::default_instance_ = NULL;\n"
- "\n",
- "classname", classname_);
-
- if (SupportsArenas(descriptor_)) {
- printer->Print(
- "$classname$* $classname$::New(::google::protobuf::Arena* arena) const {\n"
- " return ::google::protobuf::Arena::CreateMessage<$classname$>(arena);\n"
- "}\n",
- "classname", classname_);
- } else {
- printer->Print(
- "$classname$* $classname$::New(::google::protobuf::Arena* arena) const {\n"
- " $classname$* n = new $classname$;\n"
- " if (arena != NULL) {\n"
- " arena->Own(n);\n"
- " }\n"
- " return n;\n"
+ "template<> "
+ "GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE "
+ "$classname$* Arena::CreateMaybeMessage< $classname$ >(Arena* arena) {\n"
+ " return Arena::$create_func$Internal< $classname$ >(arena);\n"
"}\n",
- "classname", classname_);
- }
-
+ "classname", QualifiedClassName(descriptor_),
+ "create_func", MessageCreateFunction(descriptor_));
}
// Return the number of bits set in n, a non-negative integer.
@@ -2343,159 +2603,214 @@ static int popcnt(uint32 n) {
return result;
}
+bool MessageGenerator::MaybeGenerateOptionalFieldCondition(
+ io::Printer* printer, const FieldDescriptor* field,
+ int expected_has_bits_index) {
+ int has_bit_index = has_bit_indices_[field->index()];
+ if (!field->options().weak() &&
+ expected_has_bits_index == has_bit_index / 32) {
+ const string mask =
+ StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8));
+ printer->Print(
+ "if (cached_has_bits & 0x$mask$u) {\n",
+ "mask", mask);
+ return true;
+ }
+ return false;
+}
+
void MessageGenerator::
GenerateClear(io::Printer* printer) {
- printer->Print("void $classname$::Clear() {\n",
- "classname", classname_);
+ // Performance tuning parameters
+ const int kMaxUnconditionalPrimitiveBytesClear = 4;
+
+ printer->Print(
+ "void $classname$::Clear() {\n"
+ "// @@protoc_insertion_point(message_clear_start:$full_name$)\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
+ printer->Print(
+ // TODO(jwb): It would be better to avoid emitting this if it is not used,
+ // rather than emitting a workaround for the resulting warning.
+ "::google::protobuf::uint32 cached_has_bits = 0;\n"
+ "// Prevent compiler warnings about cached_has_bits being unused\n"
+ "(void) cached_has_bits;\n\n");
+
+ int cached_has_bit_index = -1;
+
// Step 1: Extensions
if (descriptor_->extension_range_count() > 0) {
printer->Print("_extensions_.Clear();\n");
}
- // Step 2: Everything but extensions, repeateds, unions.
- // These are handled in chunks of 8. The first chunk is
- // the non-extensions-non-repeateds-non-unions in
- // descriptor_->field(0), descriptor_->field(1), ... descriptor_->field(7),
- // and the second chunk is the same for
- // descriptor_->field(8), descriptor_->field(9), ... descriptor_->field(15),
- // etc.
- set<int> step2_indices;
- hash_map<string, int> fieldname_to_chunk;
- hash_map<int, string> memsets_for_chunk;
- hash_map<int, int> memset_field_count_for_chunk;
- hash_set<string> handled; // fields that appear anywhere in memsets_for_chunk
- hash_map<int, uint32> fields_mask_for_chunk;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
- if (!field->is_repeated() && !field->containing_oneof()) {
- step2_indices.insert(i);
- int chunk = i / 8;
- fieldname_to_chunk[FieldName(field)] = chunk;
- fields_mask_for_chunk[chunk] |= static_cast<uint32>(1) << (i % 32);
- }
- }
-
- // Step 2a: Greedily seek runs of fields that can be cleared by memset-to-0.
- // The generated code uses two macros to help it clear runs of fields:
- // ZR_HELPER_(f1) - ZR_HELPER_(f0) computes the difference, in bytes, of the
- // positions of two fields in the Message.
- // ZR_ zeroes a non-empty range of fields via memset.
- const char* macros =
- "#define ZR_HELPER_(f) reinterpret_cast<char*>(\\\n"
- " &reinterpret_cast<$classname$*>(16)->f)\n\n"
- "#define ZR_(first, last) do {\\\n"
- " ::memset(&first, 0,\\\n"
- " ZR_HELPER_(last) - ZR_HELPER_(first) + sizeof(last));\\\n"
- "} while (0)\n\n";
- for (int i = 0; i < runs_of_fields_.size(); i++) {
- const vector<string>& run = runs_of_fields_[i];
- if (run.size() < 2) continue;
- const string& first_field_name = run[0];
- const string& last_field_name = run.back();
- int chunk = fieldname_to_chunk[run[0]];
- memsets_for_chunk[chunk].append(
- "ZR_(" + first_field_name + "_, " + last_field_name + "_);\n");
- for (int j = 0; j < run.size(); j++) {
- GOOGLE_DCHECK_EQ(chunk, fieldname_to_chunk[run[j]]);
- handled.insert(run[j]);
- }
- memset_field_count_for_chunk[chunk] += run.size();
- }
- const bool macros_are_needed = handled.size() > 0;
- if (macros_are_needed) {
- printer->Outdent();
- printer->Print(macros,
- "classname", classname_);
- printer->Indent();
+ int unconditional_budget = kMaxUnconditionalPrimitiveBytesClear;
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+
+ if (!CanInitializeByZeroing(field)) {
+ continue;
+ }
+
+ unconditional_budget -= EstimateAlignmentSize(field);
}
- // Step 2b: Finish step 2, ignoring fields handled in step 2a.
- int last_index = -1;
- bool chunk_block_in_progress = false;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- if (step2_indices.count(i) == 0) continue;
- const FieldDescriptor* field = descriptor_->field(i);
- const string fieldname = FieldName(field);
- if (i / 8 != last_index / 8 || last_index < 0) {
- // End previous chunk, if there was one.
- if (chunk_block_in_progress) {
- printer->Outdent();
- printer->Print("}\n");
- chunk_block_in_progress = false;
+
+ std::vector<std::vector<const FieldDescriptor*> > chunks_frag = CollectFields(
+ optimized_order_,
+ MatchRepeatedAndHasByteAndZeroInits(
+ &has_bit_indices_, HasFieldPresence(descriptor_->file())));
+
+ // Merge next non-zero initializable chunk if it has the same has_byte index
+ // and not meeting unconditional clear condition.
+ std::vector<std::vector<const FieldDescriptor*> > chunks;
+ if (!HasFieldPresence(descriptor_->file())) {
+ // Don't bother with merging without has_bit field.
+ chunks = chunks_frag;
+ } else {
+ // Note that only the next chunk is considered for merging.
+ for (int i = 0; i < chunks_frag.size(); i++) {
+ chunks.push_back(chunks_frag[i]);
+ const FieldDescriptor* field = chunks_frag[i].front();
+ const FieldDescriptor* next_field =
+ (i + 1) < chunks_frag.size() ? chunks_frag[i + 1].front() : nullptr;
+ if (CanInitializeByZeroing(field) &&
+ (chunks_frag[i].size() == 1 || unconditional_budget < 0) &&
+ next_field != nullptr &&
+ has_bit_indices_[field->index()] / 8 ==
+ has_bit_indices_[next_field->index()] / 8) {
+ GOOGLE_CHECK(!CanInitializeByZeroing(next_field));
+ // Insert next chunk to the current one and skip next chunk.
+ chunks.back().insert(chunks.back().end(), chunks_frag[i + 1].begin(),
+ chunks_frag[i + 1].end());
+ i++;
}
- // Start chunk.
- const string& memsets = memsets_for_chunk[i / 8];
- uint32 mask = fields_mask_for_chunk[i / 8];
- int count = popcnt(mask);
- GOOGLE_DCHECK_GE(count, 1);
- if (count == 1 ||
- (count <= 4 && count == memset_field_count_for_chunk[i / 8])) {
- // No "if" here because the chunk is trivial.
- } else {
- if (HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "if (_has_bits_[$index$ / 32] & $mask$u) {\n",
- "index", SimpleItoa(i / 8 * 8),
- "mask", SimpleItoa(mask));
- printer->Indent();
- chunk_block_in_progress = true;
+ }
+ }
+
+ for (int chunk_index = 0; chunk_index < chunks.size(); chunk_index++) {
+ std::vector<const FieldDescriptor*>& chunk = chunks[chunk_index];
+ GOOGLE_CHECK(!chunk.empty());
+
+ // Step 2: Repeated fields don't use _has_bits_; emit code to clear them
+ // here.
+ if (chunk.front()->is_repeated()) {
+ for (int i = 0; i < chunk.size(); i++) {
+ const FieldDescriptor* field = chunk[i];
+ const FieldGenerator& generator = field_generators_.get(field);
+
+ generator.GenerateMessageClearingCode(printer);
+ }
+ continue;
+ }
+
+ // Step 3: Non-repeated fields that can be cleared by memset-to-0, then
+ // non-repeated, non-zero initializable fields.
+ int last_chunk = HasFieldPresence(descriptor_->file())
+ ? has_bit_indices_[chunk.front()->index()] / 8
+ : 0;
+ int last_chunk_start = 0;
+ int memset_run_start = -1;
+ int memset_run_end = -1;
+
+ for (int i = 0; i < chunk.size(); i++) {
+ const FieldDescriptor* field = chunk[i];
+ if (CanInitializeByZeroing(field)) {
+ if (memset_run_start == -1) {
+ memset_run_start = i;
}
+ memset_run_end = i;
}
- printer->Print(memsets.c_str());
- }
- last_index = i;
- if (handled.count(fieldname) > 0) continue;
-
- // It's faster to just overwrite primitive types, but we should
- // only clear strings and messages if they were set.
- // TODO(kenton): Let the CppFieldGenerator decide this somehow.
- bool should_check_bit =
- field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
- field->cpp_type() == FieldDescriptor::CPPTYPE_STRING;
-
- bool have_enclosing_if = false;
- if (should_check_bit &&
- // If no field presence, then always clear strings/messages as well.
- HasFieldPresence(descriptor_->file())) {
- printer->Print("if (has_$name$()) {\n", "name", fieldname);
- printer->Indent();
- have_enclosing_if = true;
}
- if (use_dependent_base_ && IsFieldDependent(field)) {
- printer->Print("clear_$name$();\n", "name", fieldname);
- } else {
- field_generators_.get(field).GenerateClearingCode(printer);
+ const bool have_outer_if =
+ HasFieldPresence(descriptor_->file()) && chunk.size() > 1 &&
+ (memset_run_end != chunk.size() - 1 || unconditional_budget < 0);
+
+ if (have_outer_if) {
+ uint32 last_chunk_mask = GenChunkMask(chunk, has_bit_indices_);
+ const int count = popcnt(last_chunk_mask);
+
+ // Check (up to) 8 has_bits at a time if we have more than one field in
+ // this chunk. Due to field layout ordering, we may check
+ // _has_bits_[last_chunk * 8 / 32] multiple times.
+ GOOGLE_DCHECK_LE(2, count);
+ GOOGLE_DCHECK_GE(8, count);
+
+ if (cached_has_bit_index != last_chunk / 4) {
+ cached_has_bit_index = last_chunk / 4;
+ printer->Print("cached_has_bits = _has_bits_[$idx$];\n", "idx",
+ SimpleItoa(cached_has_bit_index));
+ }
+ printer->Print("if (cached_has_bits & $mask$u) {\n", "mask",
+ SimpleItoa(last_chunk_mask));
+ printer->Indent();
}
- if (have_enclosing_if) {
- printer->Outdent();
- printer->Print("}\n");
+ if (memset_run_start != -1) {
+ if (memset_run_start == memset_run_end) {
+ // For clarity, do not memset a single field.
+ const FieldGenerator& generator =
+ field_generators_.get(chunk[memset_run_start]);
+ generator.GenerateMessageClearingCode(printer);
+ } else {
+ const string first_field_name = FieldName(chunk[memset_run_start]);
+ const string last_field_name = FieldName(chunk[memset_run_end]);
+
+ printer->Print(
+ "::memset(&$first$_, 0, static_cast<size_t>(\n"
+ " reinterpret_cast<char*>(&$last$_) -\n"
+ " reinterpret_cast<char*>(&$first$_)) + sizeof($last$_));\n",
+ "first", first_field_name, "last", last_field_name);
+ }
+
+ // Advance last_chunk_start to skip over the fields we zeroed/memset.
+ last_chunk_start = memset_run_end + 1;
}
- }
- if (chunk_block_in_progress) {
- printer->Outdent();
- printer->Print("}\n");
- }
- if (macros_are_needed) {
- printer->Outdent();
- printer->Print("\n#undef ZR_HELPER_\n#undef ZR_\n\n");
- printer->Indent();
- }
+ // Go back and emit clears for each of the fields we processed.
+ for (int j = last_chunk_start; j < chunk.size(); j++) {
+ const FieldDescriptor* field = chunk[j];
+ const string fieldname = FieldName(field);
+ const FieldGenerator& generator = field_generators_.get(field);
- // Step 3: Repeated fields don't use _has_bits_; emit code to clear them here.
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ // It's faster to just overwrite primitive types, but we should only
+ // clear strings and messages if they were set.
+ //
+ // TODO(kenton): Let the CppFieldGenerator decide this somehow.
+ bool should_check_bit =
+ field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
+ field->cpp_type() == FieldDescriptor::CPPTYPE_STRING;
- if (field->is_repeated()) {
- if (use_dependent_base_ && IsFieldDependent(field)) {
- printer->Print("clear_$name$();\n", "name", FieldName(field));
- } else {
- field_generators_.get(field).GenerateClearingCode(printer);
+ bool have_enclosing_if = false;
+ if (should_check_bit &&
+ // If no field presence, then always clear strings/messages as well.
+ HasFieldPresence(descriptor_->file())) {
+ if (!field->options().weak() &&
+ cached_has_bit_index != (has_bit_indices_[field->index()] / 32)) {
+ cached_has_bit_index = (has_bit_indices_[field->index()] / 32);
+ printer->Print("cached_has_bits = _has_bits_[$new_index$];\n",
+ "new_index", SimpleItoa(cached_has_bit_index));
+ }
+ if (!MaybeGenerateOptionalFieldCondition(printer, field,
+ cached_has_bit_index)) {
+ printer->Print("if (has_$name$()) {\n", "name", fieldname);
+ }
+ printer->Indent();
+ have_enclosing_if = true;
+ }
+
+ generator.GenerateMessageClearingCode(printer);
+
+ if (have_enclosing_if) {
+ printer->Outdent();
+ printer->Print("}\n");
}
}
+
+ if (have_outer_if) {
+ printer->Outdent();
+ printer->Print("}\n");
+ }
}
// Step 4: Unions.
@@ -2505,31 +2820,16 @@ GenerateClear(io::Printer* printer) {
"oneof_name", descriptor_->oneof_decl(i)->name());
}
+ if (num_weak_fields_) {
+ printer->Print("_weak_field_map_.ClearAll();\n");
+ }
+
if (HasFieldPresence(descriptor_->file())) {
// Step 5: Everything else.
- printer->Print(
- "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
+ printer->Print("_has_bits_.Clear();\n");
}
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "if (_internal_metadata_.have_unknown_fields()) {\n"
- " mutable_unknown_fields()->Clear();\n"
- "}\n");
- } else {
- if (SupportsArenas(descriptor_)) {
- printer->Print(
- "_unknown_fields_.ClearToEmpty(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited(),\n"
- " GetArenaNoVirtual());\n");
- } else {
- printer->Print(
- "_unknown_fields_.ClearToEmptyNoArena(\n"
- " &::google::protobuf::internal::GetEmptyStringAlreadyInited());\n");
- }
- }
- }
+ printer->Print("_internal_metadata_.Clear();\n");
printer->Outdent();
printer->Print("}\n");
@@ -2539,16 +2839,19 @@ void MessageGenerator::
GenerateOneofClear(io::Printer* printer) {
// Generated function clears the active field and union case (e.g. foo_case_).
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
- map<string, string> oneof_vars;
+ std::map<string, string> oneof_vars;
oneof_vars["classname"] = classname_;
oneof_vars["oneofname"] = descriptor_->oneof_decl(i)->name();
+ oneof_vars["full_name"] = descriptor_->full_name();
string message_class;
printer->Print(oneof_vars,
- "void $classname$::clear_$oneofname$() {\n");
+ "void $classname$::clear_$oneofname$() {\n"
+ "// @@protoc_insertion_point(one_of_clear_start:"
+ "$full_name$)\n");
printer->Indent();
printer->Print(oneof_vars,
- "switch($oneofname$_case()) {\n");
+ "switch ($oneofname$_case()) {\n");
printer->Indent();
for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
@@ -2602,10 +2905,13 @@ GenerateSwap(io::Printer* printer) {
" if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {\n"
" InternalSwap(other);\n"
" } else {\n"
- " $classname$ temp;\n"
- " temp.MergeFrom(*this);\n"
- " CopyFrom(*other);\n"
- " other->CopyFrom(temp);\n"
+ " $classname$* temp = New(GetArenaNoVirtual());\n"
+ " temp->MergeFrom(*other);\n"
+ " other->CopyFrom(*this);\n"
+ " InternalSwap(temp);\n"
+ " if (GetArenaNoVirtual() == NULL) {\n"
+ " delete temp;\n"
+ " }\n"
" }\n"
"}\n"
"void $classname$::UnsafeArenaSwap($classname$* other) {\n"
@@ -2627,45 +2933,40 @@ GenerateSwap(io::Printer* printer) {
printer->Print("void $classname$::InternalSwap($classname$* other) {\n",
"classname", classname_);
printer->Indent();
+ printer->Print("using std::swap;\n");
- if (HasGeneratedMethods(descriptor_->file())) {
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ if (HasGeneratedMethods(descriptor_->file(), options_)) {
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ // optimized_order_ does not contain oneof fields, but the field
+ // generators for these fields do not emit swapping code on their own.
+ const FieldDescriptor* field = optimized_order_[i];
field_generators_.get(field).GenerateSwappingCode(printer);
}
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
- "std::swap($oneof_name$_, other->$oneof_name$_);\n"
- "std::swap(_oneof_case_[$i$], other->_oneof_case_[$i$]);\n",
+ "swap($oneof_name$_, other->$oneof_name$_);\n"
+ "swap(_oneof_case_[$i$], other->_oneof_case_[$i$]);\n",
"oneof_name", descriptor_->oneof_decl(i)->name(),
"i", SimpleItoa(i));
}
if (HasFieldPresence(descriptor_->file())) {
- for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) {
- printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
+ for (int i = 0; i < HasBitsSize() / 4; ++i) {
+ printer->Print("swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
"i", SimpleItoa(i));
}
}
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "_internal_metadata_.Swap(&other->_internal_metadata_);\n");
- } else {
- printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n");
- }
- } else {
- // Still swap internal_metadata as it may contain more than just
- // unknown fields.
- printer->Print(
- "_internal_metadata_.Swap(&other->_internal_metadata_);\n");
- }
- printer->Print("std::swap(_cached_size_, other->_cached_size_);\n");
+ printer->Print("_internal_metadata_.Swap(&other->_internal_metadata_);\n");
+
if (descriptor_->extension_range_count() > 0) {
printer->Print("_extensions_.Swap(&other->_extensions_);\n");
}
+ if (num_weak_fields_) {
+ printer->Print(
+ "_weak_field_map_.UnsafeArenaSwap(&other->_weak_field_map_);\n");
+ }
} else {
printer->Print("GetReflection()->Swap(this, other);");
}
@@ -2676,13 +2977,15 @@ GenerateSwap(io::Printer* printer) {
void MessageGenerator::
GenerateMergeFrom(io::Printer* printer) {
- if (HasDescriptorMethods(descriptor_->file())) {
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate the generalized MergeFrom (aka that which takes in the Message
// base class as a parameter).
printer->Print(
- "void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
- " if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);\n",
- "classname", classname_);
+ "void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
+ "// @@protoc_insertion_point(generalized_merge_from_start:"
+ "$full_name$)\n"
+ " GOOGLE_DCHECK_NE(&from, this);\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
// Cast the message to the proper type. If we find that the message is
@@ -2690,15 +2993,19 @@ GenerateMergeFrom(io::Printer* printer) {
// system, as the GOOGLE_CHECK above ensured that we have the same descriptor
// for each message.
printer->Print(
- "const $classname$* source = \n"
+ "const $classname$* source =\n"
" ::google::protobuf::internal::DynamicCastToGenerated<const $classname$>(\n"
" &from);\n"
"if (source == NULL) {\n"
+ "// @@protoc_insertion_point(generalized_merge_from_cast_fail:"
+ "$full_name$)\n"
" ::google::protobuf::internal::ReflectionOps::Merge(from, this);\n"
"} else {\n"
+ "// @@protoc_insertion_point(generalized_merge_from_cast_success:"
+ "$full_name$)\n"
" MergeFrom(*source);\n"
"}\n",
- "classname", classname_);
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Outdent();
printer->Print("}\n\n");
@@ -2715,18 +3022,166 @@ GenerateMergeFrom(io::Printer* printer) {
// Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast.
printer->Print(
- "void $classname$::MergeFrom(const $classname$& from) {\n"
- " if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);\n",
- "classname", classname_);
+ "void $classname$::MergeFrom(const $classname$& from) {\n"
+ "// @@protoc_insertion_point(class_specific_merge_from_start:"
+ "$full_name$)\n"
+ " GOOGLE_DCHECK_NE(&from, this);\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
- // Merge Repeated fields. These fields do not require a
- // check as we can simply iterate over them.
- for (int i = 0; i < descriptor_->field_count(); ++i) {
- const FieldDescriptor* field = descriptor_->field(i);
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print("_extensions_.MergeFrom(from._extensions_);\n");
+ }
- if (field->is_repeated()) {
- field_generators_.get(field).GenerateMergingCode(printer);
+ printer->Print(
+ "_internal_metadata_.MergeFrom(from._internal_metadata_);\n"
+ "::google::protobuf::uint32 cached_has_bits = 0;\n"
+ "(void) cached_has_bits;\n\n");
+
+ // cached_has_bit_index maintains that:
+ // cached_has_bits = from._has_bits_[cached_has_bit_index]
+ // for cached_has_bit_index >= 0
+ int cached_has_bit_index = -1;
+
+ int last_i = -1;
+ for (int i = 0; i < optimized_order_.size(); ) {
+ // Detect infinite loops.
+ GOOGLE_CHECK_NE(i, last_i);
+ last_i = i;
+
+ // Merge Repeated fields. These fields do not require a
+ // check as we can simply iterate over them.
+ for (; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ if (!field->is_repeated()) {
+ break;
+ }
+
+ const FieldGenerator& generator = field_generators_.get(field);
+ generator.GenerateMergingCode(printer);
+ }
+
+ // Merge Optional and Required fields (after a _has_bit_ check).
+ int last_chunk = -1;
+ int last_chunk_start = -1;
+ int last_chunk_end = -1;
+ uint32 last_chunk_mask = 0;
+ for (; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ if (field->is_repeated()) {
+ break;
+ }
+
+ // "index" defines where in the _has_bits_ the field appears.
+ // "i" is our loop counter within optimized_order_.
+ int index = HasFieldPresence(descriptor_->file()) ?
+ has_bit_indices_[field->index()] : 0;
+ int chunk = index / 8;
+
+ if (last_chunk == -1) {
+ last_chunk = chunk;
+ last_chunk_start = i;
+ } else if (chunk != last_chunk) {
+ // Emit the fields for this chunk so far.
+ break;
+ }
+
+ last_chunk_end = i;
+ last_chunk_mask |= static_cast<uint32>(1) << (index % 32);
+ }
+
+ if (last_chunk != -1) {
+ GOOGLE_DCHECK_NE(-1, last_chunk_start);
+ GOOGLE_DCHECK_NE(-1, last_chunk_end);
+ GOOGLE_DCHECK_NE(0, last_chunk_mask);
+
+ const int count = popcnt(last_chunk_mask);
+ const bool have_outer_if = HasFieldPresence(descriptor_->file()) &&
+ (last_chunk_start != last_chunk_end);
+
+ if (have_outer_if) {
+ // Check (up to) 8 has_bits at a time if we have more than one field in
+ // this chunk. Due to field layout ordering, we may check
+ // _has_bits_[last_chunk * 8 / 32] multiple times.
+ GOOGLE_DCHECK_LE(2, count);
+ GOOGLE_DCHECK_GE(8, count);
+
+ if (cached_has_bit_index != last_chunk / 4) {
+ int new_index = last_chunk / 4;
+ printer->Print("cached_has_bits = from._has_bits_[$new_index$];\n",
+ "new_index", SimpleItoa(new_index));
+ cached_has_bit_index = new_index;
+ }
+
+ printer->Print(
+ "if (cached_has_bits & $mask$u) {\n",
+ "mask", SimpleItoa(last_chunk_mask));
+ printer->Indent();
+ }
+
+ // Go back and emit clears for each of the fields we processed.
+ bool deferred_has_bit_changes = false;
+ for (int j = last_chunk_start; j <= last_chunk_end; j++) {
+ const FieldDescriptor* field = optimized_order_[j];
+ const FieldGenerator& generator = field_generators_.get(field);
+
+ bool have_enclosing_if = false;
+ if (HasFieldPresence(descriptor_->file())) {
+ // Attempt to use the state of cached_has_bits, if possible.
+ int has_bit_index = has_bit_indices_[field->index()];
+ if (!field->options().weak() &&
+ cached_has_bit_index == has_bit_index / 32) {
+ const string mask = StrCat(
+ strings::Hex(1u << (has_bit_index % 32),
+ strings::ZERO_PAD_8));
+
+ printer->Print(
+ "if (cached_has_bits & 0x$mask$u) {\n", "mask", mask);
+ } else {
+ printer->Print(
+ "if (from.has_$name$()) {\n",
+ "name", FieldName(field));
+ }
+
+ printer->Indent();
+ have_enclosing_if = true;
+ } else {
+ // Merge semantics without true field presence: primitive fields are
+ // merged only if non-zero (numeric) or non-empty (string).
+ have_enclosing_if = EmitFieldNonDefaultCondition(
+ printer, "from.", field);
+ }
+
+ if (have_outer_if && IsPOD(field)) {
+ // GenerateCopyConstructorCode for enum and primitive scalar fields
+ // does not do _has_bits_ modifications. We defer _has_bits_
+ // manipulation until the end of the outer if.
+ //
+ // This can reduce the number of loads/stores by up to 7 per 8 fields.
+ deferred_has_bit_changes = true;
+ generator.GenerateCopyConstructorCode(printer);
+ } else {
+ generator.GenerateMergingCode(printer);
+ }
+
+ if (have_enclosing_if) {
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+ }
+
+ if (have_outer_if) {
+ if (deferred_has_bit_changes) {
+ // Flush the has bits for the primitives we deferred.
+ GOOGLE_CHECK_LE(0, cached_has_bit_index);
+ printer->Print(
+ "_has_bits_[$index$] |= cached_has_bits;\n",
+ "index", SimpleItoa(cached_has_bit_index));
+ }
+
+ printer->Outdent();
+ printer->Print("}\n");
+ }
}
}
@@ -2759,76 +3214,8 @@ GenerateMergeFrom(io::Printer* printer) {
printer->Print(
"}\n");
}
-
- // Merge Optional and Required fields (after a _has_bit check).
- int last_index = -1;
-
- for (int i = 0; i < descriptor_->field_count(); ++i) {
- const FieldDescriptor* field = descriptor_->field(i);
-
- if (!field->is_repeated() && !field->containing_oneof()) {
- if (HasFieldPresence(descriptor_->file())) {
- // See above in GenerateClear for an explanation of this.
- if (i / 8 != last_index / 8 || last_index < 0) {
- if (last_index >= 0) {
- printer->Outdent();
- printer->Print("}\n");
- }
- printer->Print(
- "if (from._has_bits_[$index$ / 32] & "
- "(0xffu << ($index$ % 32))) {\n",
- "index", SimpleItoa(field->index()));
- printer->Indent();
- }
- }
-
- last_index = i;
-
- bool have_enclosing_if = false;
- if (HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "if (from.has_$name$()) {\n",
- "name", FieldName(field));
- printer->Indent();
- have_enclosing_if = true;
- } else {
- // Merge semantics without true field presence: primitive fields are
- // merged only if non-zero (numeric) or non-empty (string).
- have_enclosing_if = EmitFieldNonDefaultCondition(
- printer, "from.", field);
- }
-
- field_generators_.get(field).GenerateMergingCode(printer);
-
- if (have_enclosing_if) {
- printer->Outdent();
- printer->Print("}\n");
- }
- }
- }
-
- if (HasFieldPresence(descriptor_->file()) &&
- last_index >= 0) {
- printer->Outdent();
- printer->Print("}\n");
- }
-
- if (descriptor_->extension_range_count() > 0) {
- printer->Print("_extensions_.MergeFrom(from._extensions_);\n");
- }
-
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "if (from._internal_metadata_.have_unknown_fields()) {\n"
- " mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n"
- "}\n");
- } else {
- printer->Print(
- "if (!from.unknown_fields().empty()) {\n"
- " mutable_unknown_fields()->append(from.unknown_fields());\n"
- "}\n");
- }
+ if (num_weak_fields_) {
+ printer->Print("_weak_field_map_.MergeFrom(from._weak_field_map_);\n");
}
printer->Outdent();
@@ -2837,12 +3224,14 @@ GenerateMergeFrom(io::Printer* printer) {
void MessageGenerator::
GenerateCopyFrom(io::Printer* printer) {
- if (HasDescriptorMethods(descriptor_->file())) {
+ if (HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate the generalized CopyFrom (aka that which takes in the Message
// base class as a parameter).
printer->Print(
- "void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n",
- "classname", classname_);
+ "void $classname$::CopyFrom(const ::google::protobuf::Message& from) {\n"
+ "// @@protoc_insertion_point(generalized_copy_from_start:"
+ "$full_name$)\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
printer->Print(
@@ -2856,8 +3245,10 @@ GenerateCopyFrom(io::Printer* printer) {
// Generate the class-specific CopyFrom.
printer->Print(
- "void $classname$::CopyFrom(const $classname$& from) {\n",
- "classname", classname_);
+ "void $classname$::CopyFrom(const $classname$& from) {\n"
+ "// @@protoc_insertion_point(class_specific_copy_from_start:"
+ "$full_name$)\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
printer->Print(
@@ -2871,47 +3262,74 @@ GenerateCopyFrom(io::Printer* printer) {
void MessageGenerator::
GenerateMergeFromCodedStream(io::Printer* printer) {
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
if (descriptor_->options().message_set_wire_format()) {
// Special-case MessageSet.
- printer->Print(
+ vars["classname"] = classname_;
+ printer->Print(vars,
"bool $classname$::MergePartialFromCodedStream(\n"
- " ::google::protobuf::io::CodedInputStream* input) {\n",
- "classname", classname_);
-
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " return _extensions_.ParseMessageSet(input, default_instance_,\n"
- " mutable_unknown_fields());\n",
- // Without.
- " return _extensions_.ParseMessageSet(input, &default_instance(),\n"
- " mutable_unknown_fields());\n",
- // Vars.
- "classname", classname_);
-
- printer->Print(
+ " ::google::protobuf::io::CodedInputStream* input) {\n"
+ " return _extensions_.ParseMessageSet(input,\n"
+ " internal_default_instance(), $mutable_unknown_fields$);\n"
"}\n");
return;
}
+ std::vector<const FieldDescriptor*> ordered_fields =
+ SortFieldsByNumber(descriptor_);
printer->Print(
"bool $classname$::MergePartialFromCodedStream(\n"
- " ::google::protobuf::io::CodedInputStream* input) {\n"
- "#define DO_(EXPRESSION) if (!(EXPRESSION)) goto failure\n"
- " ::google::protobuf::uint32 tag;\n",
+ " ::google::protobuf::io::CodedInputStream* input) {\n",
"classname", classname_);
- if (!UseUnknownFieldSet(descriptor_->file())) {
- // Use LazyStringOutputString to avoid initializing unknown fields string
- // unless it is actually needed. For the same reason, disable eager refresh
- // on the CodedOutputStream.
+ if (table_driven_) {
+ printer->Indent();
+
+ const string lite = UseUnknownFieldSet(descriptor_->file(), options_) ?
+ "" : "Lite";
+
printer->Print(
- " ::google::protobuf::io::LazyStringOutputStream unknown_fields_string(\n"
- " ::google::protobuf::internal::NewPermanentCallback(\n"
- " &MutableUnknownFieldsFor$classname$, this));\n"
- " ::google::protobuf::io::CodedOutputStream unknown_fields_stream(\n"
- " &unknown_fields_string, false);\n",
- "classname", classname_);
+ "return ::google::protobuf::internal::MergePartialFromCodedStream$lite$(\n"
+ " this,\n"
+ " ::$file_namespace$::TableStruct::schema[\n"
+ " $classname$::kIndexInFileMessages],\n"
+ " input);\n",
+ "classname", classname_, "file_namespace",
+ FileLevelNamespace(descriptor_), "lite", lite);
+
+ printer->Outdent();
+
+ printer->Print("}\n");
+ return;
+ }
+
+ if (SupportsArenas(descriptor_)) {
+ for (int i = 0; i < ordered_fields.size(); i++) {
+ const FieldDescriptor* field = ordered_fields[i];
+ const FieldGenerator& field_generator = field_generators_.get(field);
+ if (field_generator.MergeFromCodedStreamNeedsArena()) {
+ printer->Print(
+ " ::google::protobuf::Arena* arena = GetArenaNoVirtual();\n");
+ break;
+ }
+ }
+ }
+
+ printer->Print(
+ "#define DO_(EXPRESSION) if (!GOOGLE_PREDICT_TRUE(EXPRESSION)) goto "
+ "failure\n"
+ " ::google::protobuf::uint32 tag;\n");
+
+ if (!UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(
+ " ::google::protobuf::internal::LiteUnknownFieldSetter unknown_fields_setter(\n"
+ " &_internal_metadata_);\n"
+ " ::google::protobuf::io::StringOutputStream unknown_fields_output(\n"
+ " unknown_fields_setter.buffer());\n"
+ " ::google::protobuf::io::CodedOutputStream unknown_fields_stream(\n"
+ " &unknown_fields_output, false);\n",
+ "classname", classname_);
}
printer->Print(
@@ -2922,19 +3340,57 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
printer->Print("for (;;) {\n");
printer->Indent();
- google::protobuf::scoped_array<const FieldDescriptor * > ordered_fields(
- SortFieldsByNumber(descriptor_));
- uint32 maxtag = descriptor_->field_count() == 0 ? 0 :
- WireFormat::MakeTag(ordered_fields[descriptor_->field_count() - 1]);
+ // To calculate the maximum tag to expect, we look at the highest-numbered
+ // field. We need to be prepared to handle more than one wire type if that
+ // field is a packable repeated field, so to simplify things we assume the
+ // highest possible wire type of 5.
+ uint32 maxtag =
+ ordered_fields.empty() ? 0 : ordered_fields.back()->number() * 8 + 5;
const int kCutoff0 = 127; // fits in 1-byte varint
const int kCutoff1 = (127 << 7) + 127; // fits in 2-byte varint
- printer->Print("::std::pair< ::google::protobuf::uint32, bool> p = "
- "input->ReadTagWithCutoff($max$);\n"
+
+ // We need to capture the last tag when parsing if this is a Group type, as
+ // our caller will verify (via CodedInputStream::LastTagWas) that the correct
+ // closing tag was received.
+ bool capture_last_tag = false;
+ const Descriptor* parent = descriptor_->containing_type();
+ if (parent) {
+ for (int i = 0; i < parent->field_count(); i++) {
+ const FieldDescriptor* field = parent->field(i);
+ if (field->type() == FieldDescriptor::TYPE_GROUP &&
+ field->message_type() == descriptor_) {
+ capture_last_tag = true;
+ break;
+ }
+ }
+
+ for (int i = 0; i < parent->extension_count(); i++) {
+ const FieldDescriptor* field = parent->extension(i);
+ if (field->type() == FieldDescriptor::TYPE_GROUP &&
+ field->message_type() == descriptor_) {
+ capture_last_tag = true;
+ break;
+ }
+ }
+ }
+
+ for (int i = 0; i < descriptor_->file()->extension_count(); i++) {
+ const FieldDescriptor* field = descriptor_->file()->extension(i);
+ if (field->type() == FieldDescriptor::TYPE_GROUP &&
+ field->message_type() == descriptor_) {
+ capture_last_tag = true;
+ break;
+ }
+ }
+
+ printer->Print("::std::pair<::google::protobuf::uint32, bool> p = "
+ "input->ReadTagWithCutoffNoLastTag($max$u);\n"
"tag = p.first;\n"
"if (!p.second) goto handle_unusual;\n",
"max", SimpleItoa(maxtag <= kCutoff0 ? kCutoff0 :
(maxtag <= kCutoff1 ? kCutoff1 :
maxtag)));
+
if (descriptor_->field_count() > 0) {
// We don't even want to print the switch() if we have no fields because
// MSVC dislikes switch() statements that contain only a default value.
@@ -2944,29 +3400,20 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
// of each case. However, this is actually a bit slower in practice as it
// creates a jump table that is 8x larger and sparser, and meanwhile the
// if()s are highly predictable.
+ //
+ // Historically, we inserted checks to peek at the next tag on the wire and
+ // jump directly to the next case statement. While this avoids the jump
+ // table that the switch uses, it greatly increases code size (20-60%) and
+ // inserts branches that may fail (especially for real world protos that
+ // interleave--in field number order--hot and cold fields). Loadtests
+ // confirmed that removing this optimization is performance neutral.
printer->Print("switch (::google::protobuf::internal::WireFormatLite::"
"GetTagFieldNumber(tag)) {\n");
printer->Indent();
- // Find repeated messages and groups now, to simplify what follows.
- hash_set<int> fields_with_parse_loop;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = ordered_fields[i];
- if (field->is_repeated() &&
- (field->type() == FieldDescriptor::TYPE_MESSAGE ||
- field->type() == FieldDescriptor::TYPE_GROUP)) {
- fields_with_parse_loop.insert(i);
- }
- }
-
- // need_label is true if we generated "goto parse_$name$" while handling the
- // previous field.
- bool need_label = false;
- for (int i = 0; i < descriptor_->field_count(); i++) {
+ for (int i = 0; i < ordered_fields.size(); i++) {
const FieldDescriptor* field = ordered_fields[i];
- const bool loops = fields_with_parse_loop.count(i) > 0;
- const bool next_field_loops = fields_with_parse_loop.count(i + 1) > 0;
PrintFieldComment(printer, field);
@@ -2977,21 +3424,11 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
const FieldGenerator& field_generator = field_generators_.get(field);
// Emit code to parse the common, expected case.
- printer->Print("if (tag == $commontag$) {\n",
- "commontag", SimpleItoa(WireFormat::MakeTag(field)));
-
- if (need_label ||
- (field->is_repeated() && !field->is_packed() && !loops)) {
- printer->Print(
- " parse_$name$:\n",
- "name", field->name());
- }
- if (loops) {
- printer->Print(
- " DO_(input->IncrementRecursionDepth());\n"
- " parse_loop_$name$:\n",
- "name", field->name());
- }
+ printer->Print(
+ "if (static_cast< ::google::protobuf::uint8>(tag) ==\n"
+ " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n",
+ "truncated", SimpleItoa(WireFormat::MakeTag(field) & 0xFF),
+ "full", SimpleItoa(WireFormat::MakeTag(field)));
printer->Indent();
if (field->is_packed()) {
@@ -3005,20 +3442,30 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
if (field->is_packed()) {
internal::WireFormatLite::WireType wiretype =
WireFormat::WireTypeForFieldType(field->type());
- printer->Print("} else if (tag == $uncommontag$) {\n",
- "uncommontag", SimpleItoa(
- internal::WireFormatLite::MakeTag(
- field->number(), wiretype)));
+ const uint32 tag = internal::WireFormatLite::MakeTag(
+ field->number(), wiretype);
+ printer->Print(
+ "} else if (\n"
+ " static_cast< ::google::protobuf::uint8>(tag) ==\n"
+ " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n",
+ "truncated", SimpleItoa(tag & 0xFF),
+ "full", SimpleItoa(tag));
+
printer->Indent();
field_generator.GenerateMergeFromCodedStream(printer);
printer->Outdent();
} else if (field->is_packable() && !field->is_packed()) {
internal::WireFormatLite::WireType wiretype =
internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
- printer->Print("} else if (tag == $uncommontag$) {\n",
- "uncommontag", SimpleItoa(
- internal::WireFormatLite::MakeTag(
- field->number(), wiretype)));
+ const uint32 tag = internal::WireFormatLite::MakeTag(
+ field->number(), wiretype);
+
+ printer->Print(
+ "} else if (\n"
+ " static_cast< ::google::protobuf::uint8>(tag) ==\n"
+ " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n",
+ "truncated", SimpleItoa(tag & 0xFF),
+ "full", SimpleItoa(tag));
printer->Indent();
field_generator.GenerateMergeFromCodedStreamWithPacking(printer);
printer->Outdent();
@@ -3029,64 +3476,12 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
" goto handle_unusual;\n"
"}\n");
- // switch() is slow since it can't be predicted well. Insert some if()s
- // here that attempt to predict the next tag.
- // For non-packed repeated fields, expect the same tag again.
- if (loops) {
- printer->Print(
- "if (input->ExpectTag($tag$)) goto parse_loop_$name$;\n",
- "tag", SimpleItoa(WireFormat::MakeTag(field)),
- "name", field->name());
- } else if (field->is_repeated() && !field->is_packed()) {
- printer->Print(
- "if (input->ExpectTag($tag$)) goto parse_$name$;\n",
- "tag", SimpleItoa(WireFormat::MakeTag(field)),
- "name", field->name());
- }
-
- // Have we emitted "if (input->ExpectTag($next_tag$)) ..." yet?
- bool emitted_goto_next_tag = false;
-
- // For repeated messages/groups, we need to decrement recursion depth,
- // unless the next tag is also for a repeated message/group.
- if (loops) {
- if (next_field_loops) {
- const FieldDescriptor* next_field = ordered_fields[i + 1];
- printer->Print(
- "if (input->ExpectTag($next_tag$)) goto parse_loop_$next_name$;\n",
- "next_tag", SimpleItoa(WireFormat::MakeTag(next_field)),
- "next_name", next_field->name());
- emitted_goto_next_tag = true;
- }
- printer->Print(
- "input->UnsafeDecrementRecursionDepth();\n");
- }
-
- // If there are more fields, expect the next one.
- need_label = false;
- if (!emitted_goto_next_tag) {
- if (i + 1 == descriptor_->field_count()) {
- // Expect EOF.
- // TODO(kenton): Expect group end-tag?
- printer->Print(
- "if (input->ExpectAtEnd()) goto success;\n");
- } else {
- const FieldDescriptor* next_field = ordered_fields[i + 1];
- printer->Print(
- "if (input->ExpectTag($next_tag$)) goto parse_$next_name$;\n",
- "next_tag", SimpleItoa(WireFormat::MakeTag(next_field)),
- "next_name", next_field->name());
- need_label = true;
- }
- }
-
printer->Print(
"break;\n");
printer->Outdent();
printer->Print("}\n\n");
}
-
printer->Print("default: {\n");
printer->Indent();
}
@@ -3095,12 +3490,20 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
printer->Print("handle_unusual:\n");
printer->Indent();
// If tag is 0 or an end-group tag then this must be the end of the message.
- printer->Print(
- "if (tag == 0 ||\n"
- " ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n"
- " ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n"
- " goto success;\n"
- "}\n");
+ if (capture_last_tag) {
+ printer->Print(
+ "if (tag == 0 ||\n"
+ " ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n"
+ " ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n"
+ " input->SetLastTag(tag);\n"
+ " goto success;\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "if (tag == 0) {\n"
+ " goto success;\n"
+ "}\n");
+ }
// Handle extension ranges.
if (descriptor_->extension_range_count() > 0) {
@@ -3128,33 +3531,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
}
}
printer->Print(") {\n");
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
- " mutable_unknown_fields()));\n",
- // Without.
- " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
- " mutable_unknown_fields()));\n");
- } else {
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
- " &unknown_fields_stream));\n",
- // Without.
- " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
- " &unknown_fields_stream));\n");
- }
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(vars,
+ " DO_(_extensions_.ParseField(tag, input,\n"
+ " internal_default_instance(),\n"
+ " $mutable_unknown_fields$));\n");
} else {
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " DO_(_extensions_.ParseField(tag, input, default_instance_);\n",
- // Without.
- " DO_(_extensions_.ParseField(tag, input, &default_instance());\n");
+ printer->Print(
+ " DO_(_extensions_.ParseField(tag, input,\n"
+ " internal_default_instance(),\n"
+ " &unknown_fields_stream));\n");
}
printer->Print(
" continue;\n"
@@ -3162,19 +3548,14 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
}
// We really don't recognize this tag. Skip it.
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(vars,
"DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
- " input, tag, mutable_unknown_fields()));\n");
- } else {
- printer->Print(
- "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n"
- " input, tag, &unknown_fields_stream));\n");
- }
+ " input, tag, $mutable_unknown_fields$));\n");
} else {
printer->Print(
- "DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag));\n");
+ "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n"
+ " input, tag, &unknown_fields_stream));\n");
}
if (descriptor_->field_count() > 0) {
@@ -3199,15 +3580,69 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
"}\n", "full_name", descriptor_->full_name());
}
+void MessageGenerator::GenerateSerializeOneofFields(
+ io::Printer* printer, const std::vector<const FieldDescriptor*>& fields,
+ bool to_array) {
+ GOOGLE_CHECK(!fields.empty());
+ if (fields.size() == 1) {
+ GenerateSerializeOneField(printer, fields[0], to_array, -1);
+ return;
+ }
+ // We have multiple mutually exclusive choices. Emit a switch statement.
+ const OneofDescriptor* oneof = fields[0]->containing_oneof();
+ printer->Print(
+ "switch ($oneofname$_case()) {\n",
+ "oneofname", oneof->name());
+ printer->Indent();
+ for (int i = 0; i < fields.size(); i++) {
+ const FieldDescriptor* field = fields[i];
+ printer->Print(
+ "case k$field_name$:\n",
+ "field_name", UnderscoresToCamelCase(field->name(), true));
+ printer->Indent();
+ if (to_array) {
+ field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(
+ printer);
+ } else {
+ field_generators_.get(field).GenerateSerializeWithCachedSizes(printer);
+ }
+ printer->Print(
+ "break;\n");
+ printer->Outdent();
+ }
+ printer->Outdent();
+ // Doing nothing is an option.
+ printer->Print(
+ " default: ;\n"
+ "}\n");
+}
+
void MessageGenerator::GenerateSerializeOneField(
- io::Printer* printer, const FieldDescriptor* field, bool to_array) {
- PrintFieldComment(printer, field);
+ io::Printer* printer, const FieldDescriptor* field, bool to_array,
+ int cached_has_bits_index) {
+ if (!field->options().weak()) {
+ // For weakfields, PrintFieldComment is called during iteration.
+ PrintFieldComment(printer, field);
+ }
bool have_enclosing_if = false;
- if (!field->is_repeated() && HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "if (has_$name$()) {\n",
- "name", FieldName(field));
+ if (field->options().weak()) {
+ } else if (!field->is_repeated() && HasFieldPresence(descriptor_->file())) {
+ // Attempt to use the state of cached_has_bits, if possible.
+ int has_bit_index = has_bit_indices_[field->index()];
+ if (cached_has_bits_index == has_bit_index / 32) {
+ const string mask = StrCat(
+ strings::Hex(1u << (has_bit_index % 32),
+ strings::ZERO_PAD_8));
+
+ printer->Print(
+ "if (cached_has_bits & 0x$mask$u) {\n", "mask", mask);
+ } else {
+ printer->Print(
+ "if (has_$name$()) {\n",
+ "name", FieldName(field));
+ }
+
printer->Indent();
have_enclosing_if = true;
} else if (!HasFieldPresence(descriptor_->file())) {
@@ -3231,15 +3666,15 @@ void MessageGenerator::GenerateSerializeOneField(
void MessageGenerator::GenerateSerializeOneExtensionRange(
io::Printer* printer, const Descriptor::ExtensionRange* range,
bool to_array) {
- map<string, string> vars;
+ std::map<string, string> vars;
vars["start"] = SimpleItoa(range->start);
vars["end"] = SimpleItoa(range->end);
printer->Print(vars,
"// Extension range [$start$, $end$)\n");
if (to_array) {
printer->Print(vars,
- "target = _extensions_.SerializeWithCachedSizesToArray(\n"
- " $start$, $end$, target);\n\n");
+ "target = _extensions_.InternalSerializeWithCachedSizesToArray(\n"
+ " $start$, $end$, deterministic, target);\n\n");
} else {
printer->Print(vars,
"_extensions_.SerializeWithCachedSizes(\n"
@@ -3256,14 +3691,17 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) {
" ::google::protobuf::io::CodedOutputStream* output) const {\n"
" _extensions_.SerializeMessageSetWithCachedSizes(output);\n",
"classname", classname_);
- GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file()));
- printer->Print(
+ GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_));
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ printer->Print(vars,
" ::google::protobuf::internal::WireFormat::SerializeUnknownMessageSetItems(\n"
- " unknown_fields(), output);\n");
+ " $unknown_fields$, output);\n");
printer->Print(
"}\n");
return;
}
+ if (options_.table_driven_serialization) return;
printer->Print(
"void $classname$::SerializeWithCachedSizes(\n"
@@ -3291,16 +3729,19 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) {
if (descriptor_->options().message_set_wire_format()) {
// Special-case MessageSet.
printer->Print(
- "::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n"
- " ::google::protobuf::uint8* target) const {\n"
- " target =\n"
- " _extensions_.SerializeMessageSetWithCachedSizesToArray(target);\n",
+ "::google::protobuf::uint8* $classname$::InternalSerializeWithCachedSizesToArray(\n"
+ " bool deterministic, ::google::protobuf::uint8* target) const {\n"
+ " target = _extensions_."
+ "InternalSerializeMessageSetWithCachedSizesToArray(\n"
+ " deterministic, target);\n",
"classname", classname_);
- GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file()));
- printer->Print(
+ GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_));
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ printer->Print(vars,
" target = ::google::protobuf::internal::WireFormat::\n"
" SerializeUnknownMessageSetItemsToArray(\n"
- " unknown_fields(), target);\n");
+ " $unknown_fields$, target);\n");
printer->Print(
" return target;\n"
"}\n");
@@ -3308,11 +3749,12 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) {
}
printer->Print(
- "::google::protobuf::uint8* $classname$::SerializeWithCachedSizesToArray(\n"
- " ::google::protobuf::uint8* target) const {\n",
+ "::google::protobuf::uint8* $classname$::InternalSerializeWithCachedSizesToArray(\n"
+ " bool deterministic, ::google::protobuf::uint8* target) const {\n",
"classname", classname_);
printer->Indent();
+ printer->Print("(void)deterministic; // Unused\n");
printer->Print(
"// @@protoc_insertion_point(serialize_to_array_start:$full_name$)\n",
"full_name", descriptor_->full_name());
@@ -3331,86 +3773,188 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) {
void MessageGenerator::
GenerateSerializeWithCachedSizesBody(io::Printer* printer, bool to_array) {
- google::protobuf::scoped_array<const FieldDescriptor * > ordered_fields(
- SortFieldsByNumber(descriptor_));
+ // If there are multiple fields in a row from the same oneof then we
+ // coalesce them and emit a switch statement. This is more efficient
+ // because it lets the C++ compiler know this is a "at most one can happen"
+ // situation. If we emitted "if (has_x()) ...; if (has_y()) ..." the C++
+ // compiler's emitted code might check has_y() even when has_x() is true.
+ class LazySerializerEmitter {
+ public:
+ LazySerializerEmitter(MessageGenerator* mg, io::Printer* printer,
+ bool to_array)
+ : mg_(mg),
+ printer_(printer),
+ to_array_(to_array),
+ eager_(!HasFieldPresence(mg->descriptor_->file())),
+ cached_has_bit_index_(-1) {}
+
+ ~LazySerializerEmitter() { Flush(); }
+
+ // If conditions allow, try to accumulate a run of fields from the same
+ // oneof, and handle them at the next Flush().
+ void Emit(const FieldDescriptor* field) {
+ if (eager_ || MustFlush(field)) {
+ Flush();
+ }
+ if (field->containing_oneof() == NULL) {
+ // TODO(ckennelly): Defer non-oneof fields similarly to oneof fields.
+
+ if (!field->options().weak() && !field->is_repeated() && !eager_) {
+ // We speculatively load the entire _has_bits_[index] contents, even
+ // if it is for only one field. Deferring non-oneof emitting would
+ // allow us to determine whether this is going to be useful.
+ int has_bit_index = mg_->has_bit_indices_[field->index()];
+ if (cached_has_bit_index_ != has_bit_index / 32) {
+ // Reload.
+ int new_index = has_bit_index / 32;
+
+ printer_->Print(
+ "cached_has_bits = _has_bits_[$new_index$];\n",
+ "new_index", SimpleItoa(new_index));
+
+ cached_has_bit_index_ = new_index;
+ }
+ }
+
+ mg_->GenerateSerializeOneField(
+ printer_, field, to_array_, cached_has_bit_index_);
+ } else {
+ v_.push_back(field);
+ }
+ }
+
+ void Flush() {
+ if (!v_.empty()) {
+ mg_->GenerateSerializeOneofFields(printer_, v_, to_array_);
+ v_.clear();
+ }
+ }
+
+ private:
+ // If we have multiple fields in v_ then they all must be from the same
+ // oneof. Would adding field to v_ break that invariant?
+ bool MustFlush(const FieldDescriptor* field) {
+ return !v_.empty() &&
+ v_[0]->containing_oneof() != field->containing_oneof();
+ }
+
+ MessageGenerator* mg_;
+ io::Printer* printer_;
+ const bool to_array_;
+ const bool eager_;
+ std::vector<const FieldDescriptor*> v_;
- vector<const Descriptor::ExtensionRange*> sorted_extensions;
+ // cached_has_bit_index_ maintains that:
+ // cached_has_bits = from._has_bits_[cached_has_bit_index_]
+ // for cached_has_bit_index_ >= 0
+ int cached_has_bit_index_;
+ };
+
+ std::vector<const FieldDescriptor*> ordered_fields =
+ SortFieldsByNumber(descriptor_);
+
+ std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
for (int i = 0; i < descriptor_->extension_range_count(); ++i) {
sorted_extensions.push_back(descriptor_->extension_range(i));
}
std::sort(sorted_extensions.begin(), sorted_extensions.end(),
ExtensionRangeSorter());
+ if (num_weak_fields_) {
+ printer->Print(
+ "::google::protobuf::internal::WeakFieldMap::FieldWriter field_writer("
+ "_weak_field_map_);\n");
+ }
+
+ printer->Print(
+ "::google::protobuf::uint32 cached_has_bits = 0;\n"
+ "(void) cached_has_bits;\n\n");
// Merge the fields and the extension ranges, both sorted by field number.
- int i, j;
- for (i = 0, j = 0;
- i < descriptor_->field_count() || j < sorted_extensions.size();
- ) {
- if (i == descriptor_->field_count()) {
- GenerateSerializeOneExtensionRange(printer,
- sorted_extensions[j++],
- to_array);
- } else if (j == sorted_extensions.size()) {
- GenerateSerializeOneField(printer, ordered_fields[i++], to_array);
- } else if (ordered_fields[i]->number() < sorted_extensions[j]->start) {
- GenerateSerializeOneField(printer, ordered_fields[i++], to_array);
- } else {
- GenerateSerializeOneExtensionRange(printer,
- sorted_extensions[j++],
- to_array);
+ {
+ LazySerializerEmitter e(this, printer, to_array);
+ const FieldDescriptor* last_weak_field = nullptr;
+ int i, j;
+ for (i = 0, j = 0;
+ i < ordered_fields.size() || j < sorted_extensions.size();) {
+ if ((j == sorted_extensions.size()) ||
+ (i < descriptor_->field_count() &&
+ ordered_fields[i]->number() < sorted_extensions[j]->start)) {
+ const FieldDescriptor* field = ordered_fields[i++];
+ if (field->options().weak()) {
+ last_weak_field = field;
+ PrintFieldComment(printer, field);
+ } else {
+ if (last_weak_field != nullptr) {
+ e.Emit(last_weak_field);
+ last_weak_field = nullptr;
+ }
+ e.Emit(field);
+ }
+ } else {
+ if (last_weak_field != nullptr) {
+ e.Emit(last_weak_field);
+ last_weak_field = nullptr;
+ }
+ e.Flush();
+ GenerateSerializeOneExtensionRange(printer,
+ sorted_extensions[j++],
+ to_array);
+ }
+ }
+ if (last_weak_field != nullptr) {
+ e.Emit(last_weak_field);
}
}
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print("if (_internal_metadata_.have_unknown_fields()) {\n");
- printer->Indent();
- if (to_array) {
- printer->Print(
- "target = "
- "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
- " unknown_fields(), target);\n");
- } else {
- printer->Print(
- "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
- " unknown_fields(), output);\n");
- }
- printer->Outdent();
-
- printer->Print(
- "}\n");
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(vars,
+ "if ($have_unknown_fields$) {\n");
+ printer->Indent();
+ if (to_array) {
+ printer->Print(vars,
+ "target = "
+ "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
+ " $unknown_fields$, target);\n");
} else {
- printer->Print(
- "output->WriteRaw(unknown_fields().data(),\n"
- " static_cast<int>(unknown_fields().size()));\n");
+ printer->Print(vars,
+ "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
+ " $unknown_fields$, output);\n");
}
+ printer->Outdent();
+
+ printer->Print("}\n");
+ } else {
+ printer->Print(vars,
+ "output->WriteRaw($unknown_fields$.data(),\n"
+ " static_cast<int>($unknown_fields$.size()));\n");
}
}
-static vector<uint32> RequiredFieldsBitMask(const Descriptor* desc) {
- vector<uint32> result;
- uint32 mask = 0;
- for (int i = 0; i < desc->field_count(); i++) {
- if (i > 0 && i % 32 == 0) {
- result.push_back(mask);
- mask = 0;
- }
- if (desc->field(i)->is_required()) {
- mask |= (1 << (i & 31));
+std::vector<uint32> MessageGenerator::RequiredFieldsBitMask() const {
+ const int array_size = HasBitsSize();
+ std::vector<uint32> masks(array_size, 0);
+
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (!field->is_required()) {
+ continue;
}
+
+ const int has_bit_index = has_bit_indices_[field->index()];
+ masks[has_bit_index / 32] |=
+ static_cast<uint32>(1) << (has_bit_index % 32);
}
- if (mask != 0) {
- result.push_back(mask);
- }
- return result;
+ return masks;
}
// Create an expression that evaluates to
// "for all i, (_has_bits_[i] & masks[i]) == masks[i]"
// masks is allowed to be shorter than _has_bits_, but at least one element of
// masks must be non-zero.
-static string ConditionalToCheckBitmasks(const vector<uint32>& masks) {
- vector<string> parts;
+static string ConditionalToCheckBitmasks(const std::vector<uint32>& masks) {
+ std::vector<string> parts;
for (int i = 0; i < masks.size(); i++) {
if (masks[i] == 0) continue;
string m = StrCat("0x", strings::Hex(masks[i], strings::ZERO_PAD_8));
@@ -3419,8 +3963,9 @@ static string ConditionalToCheckBitmasks(const vector<uint32>& masks) {
}
GOOGLE_CHECK(!parts.empty());
// If we have multiple parts, each expected to be 0, then bitwise-or them.
- string result = parts.size() == 1 ? parts[0] :
- StrCat("(", Join(parts, "\n | "), ")");
+ string result = parts.size() == 1
+ ? parts[0]
+ : StrCat("(", Join(parts, "\n | "), ")");
return result + " == 0";
}
@@ -3428,22 +3973,24 @@ void MessageGenerator::
GenerateByteSize(io::Printer* printer) {
if (descriptor_->options().message_set_wire_format()) {
// Special-case MessageSet.
+ GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_));
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ vars["classname"] = classname_;
+ vars["full_name"] = descriptor_->full_name();
printer->Print(
- "int $classname$::ByteSize() const {\n"
- " int total_size = _extensions_.MessageSetByteSize();\n",
- "classname", classname_);
- GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file()));
- printer->Print(
- "if (_internal_metadata_.have_unknown_fields()) {\n"
- " total_size += ::google::protobuf::internal::WireFormat::\n"
- " ComputeUnknownMessageSetItemsSize(unknown_fields());\n"
- "}\n");
- printer->Print(
- " GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
- " _cached_size_ = total_size;\n"
- " GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
- " return total_size;\n"
- "}\n");
+ vars,
+ "size_t $classname$::ByteSizeLong() const {\n"
+ "// @@protoc_insertion_point(message_set_byte_size_start:$full_name$)\n"
+ " size_t total_size = _extensions_.MessageSetByteSize();\n"
+ " if ($have_unknown_fields$) {\n"
+ " total_size += ::google::protobuf::internal::WireFormat::\n"
+ " ComputeUnknownMessageSetItemsSize($unknown_fields$);\n"
+ " }\n"
+ " int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);\n"
+ " SetCachedSize(cached_size);\n"
+ " return total_size;\n"
+ "}\n");
return;
}
@@ -3451,12 +3998,14 @@ GenerateByteSize(io::Printer* printer) {
// Emit a function (rarely used, we hope) that handles the required fields
// by checking for each one individually.
printer->Print(
- "int $classname$::RequiredFieldsByteSizeFallback() const {\n",
- "classname", classname_);
+ "size_t $classname$::RequiredFieldsByteSizeFallback() const {\n"
+ "// @@protoc_insertion_point(required_fields_byte_size_fallback_start:"
+ "$full_name$)\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
- printer->Print("int total_size = 0;\n");
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ printer->Print("size_t total_size = 0;\n");
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
if (field->is_required()) {
printer->Print("\n"
"if (has_$name$()) {\n",
@@ -3475,25 +4024,49 @@ GenerateByteSize(io::Printer* printer) {
}
printer->Print(
- "int $classname$::ByteSize() const {\n",
- "classname", classname_);
+ "size_t $classname$::ByteSizeLong() const {\n"
+ "// @@protoc_insertion_point(message_byte_size_start:$full_name$)\n",
+ "classname", classname_, "full_name", descriptor_->full_name());
printer->Indent();
printer->Print(
- "int total_size = 0;\n"
+ "size_t total_size = 0;\n"
"\n");
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print(
+ "total_size += _extensions_.ByteSize();\n"
+ "\n");
+ }
+
+ std::map<string, string> vars;
+ SetUnknkownFieldsVariable(descriptor_, options_, &vars);
+ if (UseUnknownFieldSet(descriptor_->file(), options_)) {
+ printer->Print(vars,
+ "if ($have_unknown_fields$) {\n"
+ " total_size +=\n"
+ " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
+ " $unknown_fields$);\n"
+ "}\n");
+ } else {
+ printer->Print(vars,
+ "total_size += $unknown_fields$.size();\n"
+ "\n");
+ }
+
// Handle required fields (if any). We expect all of them to be
// present, so emit one conditional that checks for that. If they are all
// present then the fast path executes; otherwise the slow path executes.
if (num_required_fields_ > 1 && HasFieldPresence(descriptor_->file())) {
// The fast path works if all required fields are present.
- vector<uint32> masks_for_has_bits = RequiredFieldsBitMask(descriptor_);
+ const std::vector<uint32> masks_for_has_bits = RequiredFieldsBitMask();
printer->Print((string("if (") +
ConditionalToCheckBitmasks(masks_for_has_bits) +
") { // All required fields are present.\n").c_str());
printer->Indent();
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ // Oneof fields cannot be required, so optimized_order_ contains all of the
+ // fields that we need to potentially emit.
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
if (!field->is_required()) continue;
PrintFieldComment(printer, field);
field_generators_.get(field).GenerateByteSize(printer);
@@ -3505,8 +4078,8 @@ GenerateByteSize(io::Printer* printer) {
"}\n");
} else {
// num_required_fields_ <= 1: no need to be tricky
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
if (!field->is_required()) continue;
PrintFieldComment(printer, field);
printer->Print("if (has_$name$()) {\n",
@@ -3518,98 +4091,97 @@ GenerateByteSize(io::Printer* printer) {
}
}
- // Handle optional fields (worry below about repeateds, oneofs, etc.).
- // These are handled in chunks of 8. The first chunk is
- // the non-requireds-non-repeateds-non-unions-non-extensions in
- // descriptor_->field(0), descriptor_->field(1), ... descriptor_->field(7),
- // and the second chunk is the same for
- // descriptor_->field(8), descriptor_->field(9), ... descriptor_->field(15),
- // etc.
- hash_map<int, uint32> fields_mask_for_chunk;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
- if (!field->is_required() && !field->is_repeated() &&
- !field->containing_oneof()) {
- fields_mask_for_chunk[i / 8] |= static_cast<uint32>(1) << (i % 32);
- }
- }
+ std::vector<std::vector<const FieldDescriptor*> > chunks = CollectFields(
+ optimized_order_,
+ MatchRepeatedAndHasByteAndRequired(
+ &has_bit_indices_, HasFieldPresence(descriptor_->file())));
- int last_index = -1;
- bool chunk_block_in_progress = false;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
- if (!field->is_required() && !field->is_repeated() &&
- !field->containing_oneof()) {
- // See above in GenerateClear for an explanation of this.
- // TODO(kenton): Share code? Unclear how to do so without
- // over-engineering.
- if (i / 8 != last_index / 8 || last_index < 0) {
- // End previous chunk, if there was one.
- if (chunk_block_in_progress) {
- printer->Outdent();
- printer->Print("}\n");
- chunk_block_in_progress = false;
- }
- // Start chunk.
- uint32 mask = fields_mask_for_chunk[i / 8];
- int count = popcnt(mask);
- GOOGLE_DCHECK_GE(count, 1);
- if (count == 1) {
- // No "if" here because the chunk is trivial.
- } else {
- if (HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "if (_has_bits_[$index$ / 32] & $mask$u) {\n",
- "index", SimpleItoa(i),
- "mask", SimpleItoa(mask));
- printer->Indent();
- chunk_block_in_progress = true;
- }
- }
+ // Remove chunks with required fields.
+ chunks.erase(std::remove_if(chunks.begin(), chunks.end(), IsRequired),
+ chunks.end());
+
+ for (int chunk_index = 0; chunk_index < chunks.size(); chunk_index++) {
+ const std::vector<const FieldDescriptor*>& chunk = chunks[chunk_index];
+ GOOGLE_CHECK(!chunk.empty());
+
+ // Handle repeated fields.
+ if (chunk.front()->is_repeated()) {
+ for (int i = 0; i < chunk.size(); i++) {
+ const FieldDescriptor* field = chunk[i];
+
+ PrintFieldComment(printer, field);
+ const FieldGenerator& generator = field_generators_.get(field);
+ generator.GenerateByteSize(printer);
+ printer->Print("\n");
}
- last_index = i;
+ continue;
+ }
+
+ // Handle optional (non-repeated/oneof) fields.
+ //
+ // These are handled in chunks of 8. The first chunk is
+ // the non-requireds-non-repeateds-non-unions-non-extensions in
+ // descriptor_->field(0), descriptor_->field(1), ... descriptor_->field(7),
+ // and the second chunk is the same for
+ // descriptor_->field(8), descriptor_->field(9), ...
+ // descriptor_->field(15),
+ // etc.
+ int last_chunk = HasFieldPresence(descriptor_->file())
+ ? has_bit_indices_[chunk.front()->index()] / 8
+ : 0;
+ GOOGLE_DCHECK_NE(-1, last_chunk);
+
+ const bool have_outer_if =
+ HasFieldPresence(descriptor_->file()) && chunk.size() > 1;
+
+ if (have_outer_if) {
+ uint32 last_chunk_mask = GenChunkMask(chunk, has_bit_indices_);
+ const int count = popcnt(last_chunk_mask);
+
+ // Check (up to) 8 has_bits at a time if we have more than one field in
+ // this chunk. Due to field layout ordering, we may check
+ // _has_bits_[last_chunk * 8 / 32] multiple times.
+ GOOGLE_DCHECK_LE(2, count);
+ GOOGLE_DCHECK_GE(8, count);
+
+ printer->Print("if (_has_bits_[$index$ / 32] & $mask$u) {\n", "index",
+ SimpleItoa(last_chunk * 8), "mask",
+ SimpleItoa(last_chunk_mask));
+ printer->Indent();
+ }
+
+ // Go back and emit checks for each of the fields we processed.
+ for (int j = 0; j < chunk.size(); j++) {
+ const FieldDescriptor* field = chunk[j];
+ const FieldGenerator& generator = field_generators_.get(field);
PrintFieldComment(printer, field);
bool have_enclosing_if = false;
if (HasFieldPresence(descriptor_->file())) {
- printer->Print(
- "if (has_$name$()) {\n",
- "name", FieldName(field));
+ printer->Print("if (has_$name$()) {\n", "name", FieldName(field));
printer->Indent();
have_enclosing_if = true;
} else {
// Without field presence: field is serialized only if it has a
// non-default value.
- have_enclosing_if = EmitFieldNonDefaultCondition(
- printer, "this->", field);
+ have_enclosing_if =
+ EmitFieldNonDefaultCondition(printer, "this->", field);
}
- field_generators_.get(field).GenerateByteSize(printer);
+ generator.GenerateByteSize(printer);
if (have_enclosing_if) {
printer->Outdent();
printer->Print(
- "}\n"
- "\n");
+ "}\n"
+ "\n");
}
}
- }
- if (chunk_block_in_progress) {
- printer->Outdent();
- printer->Print("}\n");
- }
-
- // Repeated fields don't use _has_bits_ so we count them in a separate
- // pass.
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
-
- if (field->is_repeated()) {
- PrintFieldComment(printer, field);
- field_generators_.get(field).GenerateByteSize(printer);
- printer->Print("\n");
+ if (have_outer_if) {
+ printer->Outdent();
+ printer->Print("}\n");
}
}
@@ -3645,37 +4217,23 @@ GenerateByteSize(io::Printer* printer) {
"}\n");
}
- if (descriptor_->extension_range_count() > 0) {
- printer->Print(
- "total_size += _extensions_.ByteSize();\n"
- "\n");
- }
-
- if (PreserveUnknownFields(descriptor_)) {
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "if (_internal_metadata_.have_unknown_fields()) {\n"
- " total_size +=\n"
- " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
- " unknown_fields());\n"
- "}\n");
- } else {
- printer->Print(
- "total_size += unknown_fields().size();\n"
- "\n");
- }
+ if (num_weak_fields_) {
+ // TagSize + MessageSize
+ printer->Print("total_size += _weak_field_map_.ByteSizeLong();\n");
}
- // We update _cached_size_ even though this is a const method. In theory,
- // this is not thread-compatible, because concurrent writes have undefined
- // results. In practice, since any concurrent writes will be writing the
- // exact same value, it works on all common processors. In a future version
- // of C++, _cached_size_ should be made into an atomic<int>.
+ // We update _cached_size_ even though this is a const method. Because
+ // const methods might be called concurrently this needs to be atomic
+ // operations or the program is undefined. In practice, since any concurrent
+ // writes will be writing the exact same value, normal writes will work on
+ // all common processors. We use a dedicated wrapper class to abstract away
+ // the underlying atomic. This makes it easier on platforms where even relaxed
+ // memory order might have perf impact to replace it with ordinary loads and
+ // stores.
printer->Print(
- "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
- "_cached_size_ = total_size;\n"
- "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
- "return total_size;\n");
+ "int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);\n"
+ "SetCachedSize(cached_size);\n"
+ "return total_size;\n");
printer->Outdent();
printer->Print("}\n");
@@ -3688,68 +4246,131 @@ GenerateIsInitialized(io::Printer* printer) {
"classname", classname_);
printer->Indent();
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print(
+ "if (!_extensions_.IsInitialized()) {\n"
+ " return false;\n"
+ "}\n\n");
+ }
+
if (HasFieldPresence(descriptor_->file())) {
// Check that all required fields in this message are set. We can do this
// most efficiently by checking 32 "has bits" at a time.
- int has_bits_array_size = (descriptor_->field_count() + 31) / 32;
- for (int i = 0; i < has_bits_array_size; i++) {
- uint32 mask = 0;
- for (int bit = 0; bit < 32; bit++) {
- int index = i * 32 + bit;
- if (index >= descriptor_->field_count()) break;
- const FieldDescriptor* field = descriptor_->field(index);
-
- if (field->is_required()) {
- mask |= 1 << bit;
- }
- }
+ const std::vector<uint32> masks = RequiredFieldsBitMask();
- if (mask != 0) {
- printer->Print(
- "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n",
- "i", SimpleItoa(i),
- "mask", StrCat(strings::Hex(mask, strings::ZERO_PAD_8)));
+ for (int i = 0; i < masks.size(); i++) {
+ uint32 mask = masks[i];
+ if (mask == 0) {
+ continue;
}
+
+ // TODO(ckennelly): Consider doing something similar to ByteSizeLong(),
+ // where we check all of the required fields in a single branch (assuming
+ // that we aren't going to benefit from early termination).
+ printer->Print(
+ "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n",
+ "i", SimpleItoa(i),
+ "mask", StrCat(strings::Hex(mask, strings::ZERO_PAD_8)));
}
}
- // Now check that all embedded messages are initialized.
- printer->Print("\n");
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const FieldDescriptor* field = descriptor_->field(i);
+ // Now check that all non-oneof embedded messages are initialized.
+ for (int i = 0; i < optimized_order_.size(); i++) {
+ const FieldDescriptor* field = optimized_order_[i];
+ // TODO(ckennelly): Push this down into a generator?
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
- !ShouldIgnoreRequiredFieldCheck(field) &&
- HasRequiredFields(field->message_type())) {
+ !ShouldIgnoreRequiredFieldCheck(field, options_) &&
+ scc_analyzer_->HasRequiredFields(field->message_type())) {
if (field->is_repeated()) {
- printer->Print(
- "if (!::google::protobuf::internal::AllAreInitialized(this->$name$()))"
- " return false;\n",
- "name", FieldName(field));
- } else {
- if (field->options().weak() || !field->containing_oneof()) {
- // For weak fields, use the data member (::google::protobuf::Message*) instead
- // of the getter to avoid a link dependency on the weak message type
- // which is only forward declared.
+ if (IsImplicitWeakField(field, options_, scc_analyzer_)) {
printer->Print(
- "if (has_$name$()) {\n"
- " if (!this->$name$_->IsInitialized()) return false;\n"
- "}\n",
+ "if (!::google::protobuf::internal::AllAreInitializedWeak(this->$name$_))"
+ " return false;\n",
"name", FieldName(field));
} else {
printer->Print(
+ "if (!::google::protobuf::internal::AllAreInitialized(this->$name$()))"
+ " return false;\n",
+ "name", FieldName(field));
+ }
+ } else if (field->options().weak()) {
+ continue;
+ } else {
+ GOOGLE_CHECK(!field->containing_oneof());
+ printer->Print(
"if (has_$name$()) {\n"
- " if (!this->$name$().IsInitialized()) return false;\n"
+ " if (!this->$name$_->IsInitialized()) return false;\n"
"}\n",
"name", FieldName(field));
- }
}
}
}
+ if (num_weak_fields_) {
+ // For Weak fields.
+ printer->Print("if (!_weak_field_map_.IsInitialized()) return false;\n");
+ }
+ // Go through the oneof fields, emitting a switch if any might have required
+ // fields.
+ for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
+ const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
+
+ bool has_required_fields = false;
+ for (int j = 0; j < oneof->field_count(); j++) {
+ const FieldDescriptor* field = oneof->field(j);
+
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
+ !ShouldIgnoreRequiredFieldCheck(field, options_) &&
+ scc_analyzer_->HasRequiredFields(field->message_type())) {
+ has_required_fields = true;
+ break;
+ }
+ }
+
+ if (!has_required_fields) {
+ continue;
+ }
- if (descriptor_->extension_range_count() > 0) {
printer->Print(
- "\n"
- "if (!_extensions_.IsInitialized()) return false;");
+ "switch ($oneofname$_case()) {\n",
+ "oneofname", oneof->name());
+ printer->Indent();
+ for (int j = 0; j < oneof->field_count(); j++) {
+ const FieldDescriptor* field = oneof->field(j);
+ printer->Print(
+ "case k$field_name$: {\n",
+ "field_name", UnderscoresToCamelCase(field->name(), true));
+ printer->Indent();
+
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
+ !ShouldIgnoreRequiredFieldCheck(field, options_) &&
+ scc_analyzer_->HasRequiredFields(field->message_type())) {
+ GOOGLE_CHECK(!(field->options().weak() || !field->containing_oneof()));
+ if (field->options().weak()) {
+ // Just skip.
+ } else {
+ printer->Print(
+ "if (has_$name$()) {\n"
+ " if (!this->$name$().IsInitialized()) return false;\n"
+ "}\n",
+ "name", FieldName(field));
+ }
+ }
+
+ printer->Print(
+ "break;\n");
+ printer->Outdent();
+ printer->Print(
+ "}\n");
+ }
+ printer->Print(
+ "case $cap_oneof_name$_NOT_SET: {\n"
+ " break;\n"
+ "}\n",
+ "cap_oneof_name",
+ ToUpper(oneof->name()));
+ printer->Outdent();
+ printer->Print(
+ "}\n");
}
printer->Outdent();
@@ -3758,7 +4379,6 @@ GenerateIsInitialized(io::Printer* printer) {
"}\n");
}
-
} // namespace cpp
} // namespace compiler
} // namespace protobuf