diff options
author | Xin Li <delphij@google.com> | 2017-11-14 12:31:11 -0800 |
---|---|---|
committer | Xin Li <delphij@google.com> | 2017-11-14 12:31:11 -0800 |
commit | 220871a697290529278ed16db508eda8e12f3fc7 (patch) | |
tree | bc13101b63c6fe39a9d92706ecb7ded7f98f5a9c /tools/aapt2/proto/TableProtoSerializer.cpp | |
parent | 802f191b2b84a1b1b82c7f6f3268846084b35dfb (diff) | |
parent | 98e12851336b7db16e583f9afac63ecc97465980 (diff) |
Merge commit '98e12851336b7db16e583f9afac63ecc97465980' from
oc-mr1-dev-plus-aosp-without-vendor into stage-aosp-master.
Change-Id: Ia7b8da4a00d215160e4a4fa40f6044208d1297b7
Merged-In: I19846d2a3ee27aecbae2367a74ee49082eea154d
Diffstat (limited to 'tools/aapt2/proto/TableProtoSerializer.cpp')
-rw-r--r-- | tools/aapt2/proto/TableProtoSerializer.cpp | 150 |
1 files changed, 70 insertions, 80 deletions
diff --git a/tools/aapt2/proto/TableProtoSerializer.cpp b/tools/aapt2/proto/TableProtoSerializer.cpp index d87d64e1cb46..a08df71eae1e 100644 --- a/tools/aapt2/proto/TableProtoSerializer.cpp +++ b/tools/aapt2/proto/TableProtoSerializer.cpp @@ -24,9 +24,9 @@ #include "android-base/logging.h" -using google::protobuf::io::CodedOutputStream; -using google::protobuf::io::CodedInputStream; -using google::protobuf::io::ZeroCopyOutputStream; +using ::google::protobuf::io::CodedInputStream; +using ::google::protobuf::io::CodedOutputStream; +using ::google::protobuf::io::ZeroCopyOutputStream; namespace aapt { @@ -36,46 +36,46 @@ class PbSerializerVisitor : public RawValueVisitor { public: using RawValueVisitor::Visit; - /** - * Constructor to use when expecting to serialize any value. - */ - PbSerializerVisitor(StringPool* source_pool, StringPool* symbol_pool, - pb::Value* out_pb_value) - : source_pool_(source_pool), - symbol_pool_(symbol_pool), - out_pb_value_(out_pb_value), - out_pb_item_(nullptr) {} - - /** - * Constructor to use when expecting to serialize an Item. - */ - PbSerializerVisitor(StringPool* sourcePool, StringPool* symbolPool, - pb::Item* outPbItem) - : source_pool_(sourcePool), - symbol_pool_(symbolPool), - out_pb_value_(nullptr), - out_pb_item_(outPbItem) {} + // Constructor to use when expecting to serialize any value. + PbSerializerVisitor(StringPool* source_pool, pb::Value* out_pb_value) + : source_pool_(source_pool), out_pb_value_(out_pb_value), out_pb_item_(nullptr) { + } + + // Constructor to use when expecting to serialize an Item. + PbSerializerVisitor(StringPool* sourcePool, pb::Item* outPbItem) + : source_pool_(sourcePool), out_pb_value_(nullptr), out_pb_item_(outPbItem) { + } void Visit(Reference* ref) override { SerializeReferenceToPb(*ref, pb_item()->mutable_ref()); } void Visit(String* str) override { - pb_item()->mutable_str()->set_idx(str->value.index()); + pb_item()->mutable_str()->set_value(*str->value); + } + + void Visit(RawString* str) override { + pb_item()->mutable_raw_str()->set_value(*str->value); } void Visit(StyledString* str) override { - pb_item()->mutable_str()->set_idx(str->value.index()); + pb::StyledString* pb_str = pb_item()->mutable_styled_str(); + pb_str->set_value(str->value->value); + + for (const StringPool::Span& span : str->value->spans) { + pb::StyledString::Span* pb_span = pb_str->add_span(); + pb_span->set_tag(*span.name); + pb_span->set_first_char(span.first_char); + pb_span->set_last_char(span.last_char); + } } void Visit(FileReference* file) override { - pb_item()->mutable_file()->set_path_idx(file->path.index()); + pb_item()->mutable_file()->set_path(*file->path); } - void Visit(Id* id) override { pb_item()->mutable_id(); } - - void Visit(RawString* raw_str) override { - pb_item()->mutable_raw_str()->set_idx(raw_str->value.index()); + void Visit(Id* /*id*/) override { + pb_item()->mutable_id(); } void Visit(BinaryPrimitive* prim) override { @@ -87,7 +87,9 @@ class PbSerializerVisitor : public RawValueVisitor { pb_prim->set_data(val.data); } - void VisitItem(Item* item) override { LOG(FATAL) << "unimplemented item"; } + void VisitItem(Item* item) override { + LOG(FATAL) << "unimplemented item"; + } void Visit(Attribute* attr) override { pb::Attribute* pb_attr = pb_compound_value()->mutable_attr(); @@ -96,7 +98,7 @@ class PbSerializerVisitor : public RawValueVisitor { pb_attr->set_max_int(attr->max_int); for (auto& symbol : attr->symbols) { - pb::Attribute_Symbol* pb_symbol = pb_attr->add_symbols(); + pb::Attribute_Symbol* pb_symbol = pb_attr->add_symbol(); SerializeItemCommonToPb(symbol.symbol, pb_symbol); SerializeReferenceToPb(symbol.symbol, pb_symbol->mutable_name()); pb_symbol->set_value(symbol.value); @@ -112,12 +114,12 @@ class PbSerializerVisitor : public RawValueVisitor { } for (Style::Entry& entry : style->entries) { - pb::Style_Entry* pb_entry = pb_style->add_entries(); + pb::Style_Entry* pb_entry = pb_style->add_entry(); SerializeReferenceToPb(entry.key, pb_entry->mutable_key()); pb::Item* pb_item = pb_entry->mutable_item(); SerializeItemCommonToPb(entry.key, pb_entry); - PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, pb_item); + PbSerializerVisitor sub_visitor(source_pool_, pb_item); entry.value->Accept(&sub_visitor); } } @@ -125,7 +127,7 @@ class PbSerializerVisitor : public RawValueVisitor { void Visit(Styleable* styleable) override { pb::Styleable* pb_styleable = pb_compound_value()->mutable_styleable(); for (Reference& entry : styleable->entries) { - pb::Styleable_Entry* pb_entry = pb_styleable->add_entries(); + pb::Styleable_Entry* pb_entry = pb_styleable->add_entry(); SerializeItemCommonToPb(entry, pb_entry); SerializeReferenceToPb(entry, pb_entry->mutable_attr()); } @@ -133,11 +135,10 @@ class PbSerializerVisitor : public RawValueVisitor { void Visit(Array* array) override { pb::Array* pb_array = pb_compound_value()->mutable_array(); - for (auto& value : array->items) { - pb::Array_Entry* pb_entry = pb_array->add_entries(); - SerializeItemCommonToPb(*value, pb_entry); - PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, - pb_entry->mutable_item()); + for (auto& value : array->elements) { + pb::Array_Element* pb_element = pb_array->add_element(); + SerializeItemCommonToPb(*value, pb_element); + PbSerializerVisitor sub_visitor(source_pool_, pb_element->mutable_item()); value->Accept(&sub_visitor); } } @@ -151,11 +152,11 @@ class PbSerializerVisitor : public RawValueVisitor { continue; } - pb::Plural_Entry* pb_entry = pb_plural->add_entries(); + pb::Plural_Entry* pb_entry = pb_plural->add_entry(); pb_entry->set_arity(SerializePluralEnumToPb(i)); pb::Item* pb_element = pb_entry->mutable_item(); SerializeItemCommonToPb(*plural->values[i], pb_entry); - PbSerializerVisitor sub_visitor(source_pool_, symbol_pool_, pb_element); + PbSerializerVisitor sub_visitor(source_pool_, pb_element); plural->values[i]->Accept(&sub_visitor); } } @@ -177,8 +178,7 @@ class PbSerializerVisitor : public RawValueVisitor { template <typename T> void SerializeItemCommonToPb(const Item& item, T* pb_item) { - SerializeSourceToPb(item.GetSource(), source_pool_, - pb_item->mutable_source()); + SerializeSourceToPb(item.GetSource(), source_pool_, pb_item->mutable_source()); if (!item.GetComment().empty()) { pb_item->set_comment(item.GetComment()); } @@ -190,8 +190,7 @@ class PbSerializerVisitor : public RawValueVisitor { } if (ref.name) { - StringPool::Ref symbol_ref = symbol_pool_->MakeRef(ref.name.value().ToString()); - pb_ref->set_symbol_idx(static_cast<uint32_t>(symbol_ref.index())); + pb_ref->set_name(ref.name.value().ToString()); } pb_ref->set_private_(ref.private_reference); @@ -199,7 +198,6 @@ class PbSerializerVisitor : public RawValueVisitor { } StringPool* source_pool_; - StringPool* symbol_pool_; pb::Value* out_pb_value_; pb::Item* out_pb_item_; }; @@ -207,41 +205,35 @@ class PbSerializerVisitor : public RawValueVisitor { } // namespace std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { - // We must do this before writing the resources, since the string pool IDs may - // change. - table->string_pool.Sort( - [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { - int diff = a.context.priority - b.context.priority; - if (diff < 0) return true; - if (diff > 0) return false; - diff = a.context.config.compare(b.context.config); - if (diff < 0) return true; - if (diff > 0) return false; - return a.value < b.value; - }); + // We must do this before writing the resources, since the string pool IDs may change. table->string_pool.Prune(); + table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { + int diff = util::compare(a.priority, b.priority); + if (diff == 0) { + diff = a.config.compare(b.config); + } + return diff; + }); auto pb_table = util::make_unique<pb::ResourceTable>(); - SerializeStringPoolToPb(table->string_pool, pb_table->mutable_string_pool()); - - StringPool source_pool, symbol_pool; + StringPool source_pool; for (auto& package : table->packages) { - pb::Package* pb_package = pb_table->add_packages(); + pb::Package* pb_package = pb_table->add_package(); if (package->id) { pb_package->set_package_id(package->id.value()); } pb_package->set_package_name(package->name); for (auto& type : package->types) { - pb::Type* pb_type = pb_package->add_types(); + pb::Type* pb_type = pb_package->add_type(); if (type->id) { pb_type->set_id(type->id.value()); } pb_type->set_name(ToString(type->type).to_string()); for (auto& entry : type->entries) { - pb::Entry* pb_entry = pb_type->add_entries(); + pb::Entry* pb_entry = pb_type->add_entry(); if (entry->id) { pb_entry->set_id(entry->id.value()); } @@ -255,7 +247,7 @@ std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { pb_status->set_allow_new(entry->symbol_status.allow_new); for (auto& config_value : entry->values) { - pb::ConfigValue* pb_config_value = pb_entry->add_config_values(); + pb::ConfigValue* pb_config_value = pb_entry->add_config_value(); SerializeConfig(config_value->config, pb_config_value->mutable_config()); if (!config_value->product.empty()) { pb_config_value->mutable_config()->set_product(config_value->product); @@ -272,7 +264,7 @@ std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { pb_value->set_weak(true); } - PbSerializerVisitor visitor(&source_pool, &symbol_pool, pb_value); + PbSerializerVisitor visitor(&source_pool, pb_value); config_value->value->Accept(&visitor); } } @@ -280,27 +272,25 @@ std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) { } SerializeStringPoolToPb(source_pool, pb_table->mutable_source_pool()); - SerializeStringPoolToPb(symbol_pool, pb_table->mutable_symbol_pool()); return pb_table; } -std::unique_ptr<pb::CompiledFile> SerializeCompiledFileToPb( - const ResourceFile& file) { - auto pb_file = util::make_unique<pb::CompiledFile>(); +std::unique_ptr<pb::internal::CompiledFile> SerializeCompiledFileToPb(const ResourceFile& file) { + auto pb_file = util::make_unique<pb::internal::CompiledFile>(); pb_file->set_resource_name(file.name.ToString()); pb_file->set_source_path(file.source.path); SerializeConfig(file.config, pb_file->mutable_config()); for (const SourcedResourceName& exported : file.exported_symbols) { - pb::CompiledFile_Symbol* pb_symbol = pb_file->add_exported_symbols(); + pb::internal::CompiledFile_Symbol* pb_symbol = pb_file->add_exported_symbol(); pb_symbol->set_resource_name(exported.name.ToString()); - pb_symbol->set_line_no(exported.line); + pb_symbol->mutable_source()->set_line_number(exported.line); } return pb_file; } -CompiledFileOutputStream::CompiledFileOutputStream(ZeroCopyOutputStream* out) - : out_(out) {} +CompiledFileOutputStream::CompiledFileOutputStream(ZeroCopyOutputStream* out) : out_(out) { +} void CompiledFileOutputStream::EnsureAlignedWrite() { const int padding = out_.ByteCount() % 4; @@ -315,8 +305,7 @@ void CompiledFileOutputStream::WriteLittleEndian32(uint32_t val) { out_.WriteLittleEndian32(val); } -void CompiledFileOutputStream::WriteCompiledFile( - const pb::CompiledFile* compiled_file) { +void CompiledFileOutputStream::WriteCompiledFile(const pb::internal::CompiledFile* compiled_file) { EnsureAlignedWrite(); out_.WriteLittleEndian64(static_cast<uint64_t>(compiled_file->ByteSize())); compiled_file->SerializeWithCachedSizes(&out_); @@ -336,7 +325,9 @@ void CompiledFileOutputStream::WriteData(const void* data, size_t len) { out_.WriteRaw(data, len); } -bool CompiledFileOutputStream::HadError() { return out_.HadError(); } +bool CompiledFileOutputStream::HadError() { + return out_.HadError(); +} CompiledFileInputStream::CompiledFileInputStream(const void* data, size_t size) : in_(static_cast<const uint8_t*>(data), size) {} @@ -354,7 +345,7 @@ bool CompiledFileInputStream::ReadLittleEndian32(uint32_t* out_val) { return in_.ReadLittleEndian32(out_val); } -bool CompiledFileInputStream::ReadCompiledFile(pb::CompiledFile* out_val) { +bool CompiledFileInputStream::ReadCompiledFile(pb::internal::CompiledFile* out_val) { EnsureAlignedRead(); google::protobuf::uint64 pb_size = 0u; @@ -381,8 +372,7 @@ bool CompiledFileInputStream::ReadCompiledFile(pb::CompiledFile* out_val) { return true; } -bool CompiledFileInputStream::ReadDataMetaData(uint64_t* out_offset, - uint64_t* out_len) { +bool CompiledFileInputStream::ReadDataMetaData(uint64_t* out_offset, uint64_t* out_len) { EnsureAlignedRead(); google::protobuf::uint64 pb_size = 0u; |