blob: 7956ad826acdde55e11603c11e504a95ea20dd09 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceValues.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
20#include <limits>
21#include <set>
22
23#include "androidfw/ResourceTypes.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080026#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070028#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070029
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033void BaseValue<Derived>::Accept(RawValueVisitor* visitor) {
34 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035}
36
37template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038void BaseItem<Derived>::Accept(RawValueVisitor* visitor) {
39 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040}
41
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044bool RawString::Equals(const Value* value) const {
45 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 if (!other) {
47 return false;
48 }
49 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070050}
51
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052RawString* RawString::Clone(StringPool* new_pool) const {
53 RawString* rs = new RawString(new_pool->MakeRef(*value));
54 rs->comment_ = comment_;
55 rs->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059bool RawString::Flatten(android::Res_value* out_value) const {
60 out_value->dataType = android::Res_value::TYPE_STRING;
61 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067}
68
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080bool Reference::Equals(const Value* value) const {
81 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (!other) {
83 return false;
84 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 return reference_type == other->reference_type &&
86 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -070088}
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090bool Reference::Flatten(android::Res_value* out_value) const {
91 out_value->dataType = (reference_type == Reference::Type::kResource)
92 ? android::Res_value::TYPE_REFERENCE
93 : android::Res_value::TYPE_ATTRIBUTE;
94 out_value->data = util::HostToDevice32(id ? id.value().id : 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102void Reference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 *out << "(reference) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 if (reference_type == Reference::Type::kResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 *out << "@";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 if (private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 } else {
110 *out << "?";
111 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 if (name) {
114 *out << name.value();
115 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 if (id && !Res_INTERNALID(id.value().id)) {
118 *out << " " << id.value();
119 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120}
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122bool Id::Equals(const Value* value) const {
123 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700124}
125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134void Id::Print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138bool String::Equals(const Value* value) const {
139 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 if (!other) {
141 return false;
142 }
143 return *this->value == *other->value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144}
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
150 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 out_value->dataType = android::Res_value::TYPE_STRING;
153 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155}
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157String* String::Clone(StringPool* new_pool) const {
158 String* str = new String(new_pool->MakeRef(*value));
159 str->comment_ = comment_;
160 str->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162}
163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166}
167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170bool StyledString::Equals(const Value* value) const {
171 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700173 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175
176 if (*this->value->str == *other->value->str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 const std::vector<StringPool::Span>& spans_a = this->value->spans;
178 const std::vector<StringPool::Span>& spans_b = other->value->spans;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return std::equal(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 spans_a.begin(), spans_a.end(), spans_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 [](const StringPool::Span& a, const StringPool::Span& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 return *a.name == *b.name && a.first_char == b.first_char &&
183 a.last_char == b.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 });
185 }
186 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187}
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189bool StyledString::Flatten(android::Res_value* out_value) const {
190 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 return false;
192 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 out_value->dataType = android::Res_value::TYPE_STRING;
195 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197}
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199StyledString* StyledString::Clone(StringPool* new_pool) const {
200 StyledString* str = new StyledString(new_pool->MakeRef(value));
201 str->comment_ = comment_;
202 str->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204}
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206void StyledString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 *out << "(styled string) \"" << *value->str << "\"";
208 for (const StringPool::Span& span : value->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 *out << " " << *span.name << ":" << span.first_char << ","
210 << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212}
213
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216bool FileReference::Equals(const Value* value) const {
217 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 if (!other) {
219 return false;
220 }
221 return *path == *other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700222}
223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224bool FileReference::Flatten(android::Res_value* out_value) const {
225 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return false;
227 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 out_value->dataType = android::Res_value::TYPE_STRING;
230 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232}
233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234FileReference* FileReference::Clone(StringPool* new_pool) const {
235 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 fr->comment_ = comment_;
238 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240}
241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244}
245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 value.dataType = dataType;
250 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251}
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253bool BinaryPrimitive::Equals(const Value* value) const {
254 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 if (!other) {
256 return false;
257 }
258 return this->value.dataType == other->value.dataType &&
259 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700260}
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
263 out_value->dataType = value.dataType;
264 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266}
267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270}
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 switch (value.dataType) {
274 case android::Res_value::TYPE_NULL:
275 *out << "(null)";
276 break;
277 case android::Res_value::TYPE_INT_DEC:
278 *out << "(integer) " << static_cast<int32_t>(value.data);
279 break;
280 case android::Res_value::TYPE_INT_HEX:
281 *out << "(integer) 0x" << std::hex << value.data << std::dec;
282 break;
283 case android::Res_value::TYPE_INT_BOOLEAN:
284 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
285 break;
286 case android::Res_value::TYPE_INT_COLOR_ARGB8:
287 case android::Res_value::TYPE_INT_COLOR_RGB8:
288 case android::Res_value::TYPE_INT_COLOR_ARGB4:
289 case android::Res_value::TYPE_INT_COLOR_RGB4:
290 *out << "(color) #" << std::hex << value.data << std::dec;
291 break;
292 default:
293 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
294 << std::hex << value.data << std::dec;
295 break;
296 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 : type_mask(t),
301 min_int(std::numeric_limits<int32_t>::min()),
302 max_int(std::numeric_limits<int32_t>::max()) {
303 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304}
305
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700306template <typename T>
307T* addPointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311bool Attribute::Equals(const Value* value) const {
312 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 if (!other) {
314 return false;
315 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 if (symbols.size() != other->symbols.size()) {
318 return false;
319 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 if (type_mask != other->type_mask || min_int != other->min_int ||
322 max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 return false;
324 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700325
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 std::vector<const Symbol*> sorted_a;
327 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 addPointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 [](const Symbol* a, const Symbol* b) -> bool {
331 return a->symbol.name < b->symbol.name;
332 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 std::vector<const Symbol*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 std::transform(other->symbols.begin(), other->symbols.end(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 std::back_inserter(sorted_b), addPointer<const Symbol>);
337 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 [](const Symbol* a, const Symbol* b) -> bool {
339 return a->symbol.name < b->symbol.name;
340 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 return a->symbol.Equals(&b->symbol) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 a->value == b->value;
346 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700347}
348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351}
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353void Attribute::PrintMask(std::ostream* out) const {
354 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 *out << "any";
356 return;
357 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 if (!set) {
362 set = true;
363 } else {
364 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 *out << "reference";
367 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 if (!set) {
371 set = true;
372 } else {
373 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 *out << "string";
376 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 if (!set) {
380 set = true;
381 } else {
382 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 *out << "integer";
385 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 if (!set) {
389 set = true;
390 } else {
391 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 *out << "boolean";
394 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 if (!set) {
398 set = true;
399 } else {
400 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 *out << "color";
403 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 if (!set) {
407 set = true;
408 } else {
409 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 *out << "float";
412 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 if (!set) {
416 set = true;
417 } else {
418 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 *out << "dimension";
421 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 if (!set) {
425 set = true;
426 } else {
427 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 *out << "fraction";
430 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (!set) {
434 set = true;
435 } else {
436 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 *out << "enum";
439 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 if (!set) {
443 set = true;
444 } else {
445 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 *out << "flags";
448 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700449}
450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if (min_int != std::numeric_limits<int32_t>::min()) {
460 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 if (max_int != std::numeric_limits<int32_t>::max()) {
464 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 *out << " [weak]";
469 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470}
471
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800474 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 *msg << " boolean";
478 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 *msg << " color";
482 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 *msg << " dimension";
486 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 *msg << " enum";
490 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 *msg << " flags";
494 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 *msg << " float";
498 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 *msg << " fraction";
502 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 *msg << " integer";
506 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800507
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 *msg << " reference";
510 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (attr->type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 *msg << " string";
514 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800517}
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
525 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
526 if (out_msg) {
527 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800528 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 return false;
530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
534 if (out_msg) {
535 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 }
537 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
539 if (out_msg) {
540 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
542 return false;
543 }
544 }
545 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800546}
547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548bool Style::Equals(const Value* value) const {
549 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 if (!other) {
551 return false;
552 }
553 if (bool(parent) != bool(other->parent) ||
554 (parent && other->parent &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 return false;
557 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700558
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 if (entries.size() != other->entries.size()) {
560 return false;
561 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700562
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563 std::vector<const Entry*> sorted_a;
564 std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 addPointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 [](const Entry* a, const Entry* b) -> bool {
568 return a->key.name < b->key.name;
569 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 std::vector<const Entry*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 std::transform(other->entries.begin(), other->entries.end(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 std::back_inserter(sorted_b), addPointer<const Entry>);
574 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 [](const Entry* a, const Entry* b) -> bool {
576 return a->key.name < b->key.name;
577 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700578
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 return a->key.Equals(&b->key) &&
582 a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700584}
585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 Style* style = new Style();
588 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 style->parent_inferred = parent_inferred;
590 style->comment_ = comment_;
591 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 for (auto& entry : entries) {
593 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 }
596 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597}
598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 *out << "(style) ";
601 if (parent && parent.value().name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 if (parent.value().private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 *out << parent.value().name.value();
606 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608}
609
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610static ::std::ostream& operator<<(::std::ostream& out,
611 const Style::Entry& value) {
612 if (value.key.name) {
613 out << value.key.name.value();
614 } else if (value.key.id) {
615 out << value.key.id.value();
616 } else {
617 out << "???";
618 }
619 out << " = ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 value.value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800622}
623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624bool Array::Equals(const Value* value) const {
625 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 if (!other) {
627 return false;
628 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700629
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 if (items.size() != other->items.size()) {
631 return false;
632 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700633
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 return std::equal(items.begin(), items.end(), other->items.begin(),
635 [](const std::unique_ptr<Item>& a,
636 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700639}
640
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 array->comment_ = comment_;
644 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 }
648 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800649}
650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651void Array::Print(std::ostream* out) const {
652 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800653}
654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655bool Plural::Equals(const Value* value) const {
656 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 if (!other) {
658 return false;
659 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700660
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 if (values.size() != other->values.size()) {
662 return false;
663 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700664
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 return std::equal(values.begin(), values.end(), other->values.begin(),
666 [](const std::unique_ptr<Item>& a,
667 const std::unique_ptr<Item>& b) -> bool {
668 if (bool(a) != bool(b)) {
669 return false;
670 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 return bool(a) == bool(b) || a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700673}
674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 p->comment_ = comment_;
678 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 const size_t count = values.size();
680 for (size_t i = 0; i < count; i++) {
681 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800683 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
685 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686}
687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 *out << "(plural)";
690 if (values[Zero]) {
691 *out << " zero=" << *values[Zero];
692 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700693
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 if (values[One]) {
695 *out << " one=" << *values[One];
696 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700697
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 if (values[Two]) {
699 *out << " two=" << *values[Two];
700 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700701
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 if (values[Few]) {
703 *out << " few=" << *values[Few];
704 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700705
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 if (values[Many]) {
707 *out << " many=" << *values[Many];
708 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800709}
710
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711static ::std::ostream& operator<<(::std::ostream& out,
712 const std::unique_ptr<Item>& item) {
713 return out << *item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800714}
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716bool Styleable::Equals(const Value* value) const {
717 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 if (!other) {
719 return false;
720 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700721
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 if (entries.size() != other->entries.size()) {
723 return false;
724 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
727 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700730}
731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800734}
735
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700738 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800739}
740
Adam Lesinski8197cc462016-08-19 12:16:49 -0700741bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 if (cmp != 0) return cmp < 0;
744 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700745}
746
747bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700749}
750
751bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700753}
754
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700755struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 bool operator()(const Reference& a, const Reference& b) const {
757 return a.name < b.name;
758 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700759};
760
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 // Compare only names, because some References may already have their IDs
763 // assigned
764 // (framework IDs that don't change).
765 std::set<Reference, NameOnlyComparator> references;
766 references.insert(entries.begin(), entries.end());
767 references.insert(other->entries.begin(), other->entries.end());
768 entries.clear();
769 entries.reserve(references.size());
770 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700771}
772
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773} // namespace aapt