blob: 947e091e2d483173f24fef43e6c97411806c8485 [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 Lesinski5924d8c2017-05-30 15:15:58 -070032std::ostream& operator<<(std::ostream& out, const Value& value) {
33 value.Print(&out);
34 return out;
35}
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038void BaseValue<Derived>::Accept(RawValueVisitor* visitor) {
39 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040}
41
42template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043void BaseItem<Derived>::Accept(RawValueVisitor* visitor) {
44 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045}
46
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049bool RawString::Equals(const Value* value) const {
50 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 if (!other) {
52 return false;
53 }
54 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070055}
56
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057RawString* RawString::Clone(StringPool* new_pool) const {
58 RawString* rs = new RawString(new_pool->MakeRef(*value));
59 rs->comment_ = comment_;
60 rs->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062}
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064bool RawString::Flatten(android::Res_value* out_value) const {
65 out_value->dataType = android::Res_value::TYPE_STRING;
66 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068}
69
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072}
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085bool Reference::Equals(const Value* value) const {
86 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 if (!other) {
88 return false;
89 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 return reference_type == other->reference_type &&
91 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -070093}
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095bool Reference::Flatten(android::Res_value* out_value) const {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080096 const ResourceId resid = id.value_or_default(ResourceId(0));
Adam Lesinskibab4ef52017-06-01 15:22:57 -070097 const bool dynamic = resid.is_valid_dynamic() && resid.package_id() != kFrameworkPackageId &&
98 resid.package_id() != kAppPackageId;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080099
100 if (reference_type == Reference::Type::kResource) {
101 if (dynamic) {
102 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
103 } else {
104 out_value->dataType = android::Res_value::TYPE_REFERENCE;
105 }
106 } else {
107 if (dynamic) {
108 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
109 } else {
110 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
111 }
112 }
113 out_value->data = util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115}
116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121void Reference::Print(std::ostream* out) const {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700122 if (reference_type == Type::kResource) {
123 *out << "(reference) @";
124 if (!name && !id) {
125 *out << "null";
126 return;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 } else {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700129 *out << "(attr-reference) ?";
130 }
131
132 if (private_reference) {
133 *out << "*";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 if (name) {
137 *out << name.value();
138 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700140 if (id && id.value().is_valid_dynamic()) {
141 if (name) {
142 *out << " ";
143 }
144 *out << id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146}
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148bool Id::Equals(const Value* value) const {
149 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700150}
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156}
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160void Id::Print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164bool String::Equals(const Value* value) const {
165 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 if (!other) {
167 return false;
168 }
Adam Lesinski75421622017-01-06 15:20:04 -0800169
170 if (this->value != other->value) {
171 return false;
172 }
173
174 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
175 return false;
176 }
177
178 auto other_iter = other->untranslatable_sections.begin();
179 for (const UntranslatableSection& this_section : untranslatable_sections) {
180 if (this_section != *other_iter) {
181 return false;
182 }
183 ++other_iter;
184 }
185 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186}
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 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 -0700199String* String::Clone(StringPool* new_pool) const {
200 String* str = new String(new_pool->MakeRef(*value));
201 str->comment_ = comment_;
202 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800203 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205}
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209}
210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213bool StyledString::Equals(const Value* value) const {
214 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700216 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
218
Adam Lesinski75421622017-01-06 15:20:04 -0800219 if (this->value != other->value) {
220 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 }
Adam Lesinski75421622017-01-06 15:20:04 -0800222
223 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
224 return false;
225 }
226
227 auto other_iter = other->untranslatable_sections.begin();
228 for (const UntranslatableSection& this_section : untranslatable_sections) {
229 if (this_section != *other_iter) {
230 return false;
231 }
232 ++other_iter;
233 }
234 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235}
236
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237bool StyledString::Flatten(android::Res_value* out_value) const {
238 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 return false;
240 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 out_value->dataType = android::Res_value::TYPE_STRING;
243 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245}
246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247StyledString* StyledString::Clone(StringPool* new_pool) const {
248 StyledString* str = new StyledString(new_pool->MakeRef(value));
249 str->comment_ = comment_;
250 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800251 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253}
254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255void StyledString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 *out << "(styled string) \"" << *value->str << "\"";
257 for (const StringPool::Span& span : value->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 *out << " " << *span.name << ":" << span.first_char << ","
259 << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261}
262
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265bool FileReference::Equals(const Value* value) const {
266 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 if (!other) {
268 return false;
269 }
Adam Lesinski75421622017-01-06 15:20:04 -0800270 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700271}
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273bool FileReference::Flatten(android::Res_value* out_value) const {
274 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return false;
276 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 out_value->dataType = android::Res_value::TYPE_STRING;
279 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283FileReference* FileReference::Clone(StringPool* new_pool) const {
284 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 fr->comment_ = comment_;
287 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289}
290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 value.dataType = dataType;
299 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300}
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302bool BinaryPrimitive::Equals(const Value* value) const {
303 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 if (!other) {
305 return false;
306 }
307 return this->value.dataType == other->value.dataType &&
308 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
312 out_value->dataType = value.dataType;
313 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315}
316
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319}
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 switch (value.dataType) {
323 case android::Res_value::TYPE_NULL:
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700324 if (value.data == android::Res_value::DATA_NULL_EMPTY) {
325 *out << "(empty)";
326 } else {
327 *out << "(null)";
328 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 break;
330 case android::Res_value::TYPE_INT_DEC:
331 *out << "(integer) " << static_cast<int32_t>(value.data);
332 break;
333 case android::Res_value::TYPE_INT_HEX:
334 *out << "(integer) 0x" << std::hex << value.data << std::dec;
335 break;
336 case android::Res_value::TYPE_INT_BOOLEAN:
337 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
338 break;
339 case android::Res_value::TYPE_INT_COLOR_ARGB8:
340 case android::Res_value::TYPE_INT_COLOR_RGB8:
341 case android::Res_value::TYPE_INT_COLOR_ARGB4:
342 case android::Res_value::TYPE_INT_COLOR_RGB4:
343 *out << "(color) #" << std::hex << value.data << std::dec;
344 break;
345 default:
346 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
347 << std::hex << value.data << std::dec;
348 break;
349 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350}
351
Adam Lesinskic744ae82017-05-17 19:28:38 -0700352Attribute::Attribute()
353 : type_mask(0u),
354 min_int(std::numeric_limits<int32_t>::min()),
355 max_int(std::numeric_limits<int32_t>::max()) {
356}
357
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 : type_mask(t),
360 min_int(std::numeric_limits<int32_t>::min()),
361 max_int(std::numeric_limits<int32_t>::max()) {
362 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363}
364
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700365std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
366 if (s.symbol.name) {
367 out << s.symbol.name.value().entry;
368 } else {
369 out << "???";
370 }
371 return out << "=" << s.value;
372}
373
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700374template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800375constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700377}
378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379bool Attribute::Equals(const Value* value) const {
380 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 if (!other) {
382 return false;
383 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700384
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 if (symbols.size() != other->symbols.size()) {
386 return false;
387 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700388
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700389 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 return false;
391 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 std::vector<const Symbol*> sorted_a;
394 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800395 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700396 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
397 return a->symbol.name < b->symbol.name;
398 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700401 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
402 add_pointer<const Symbol>);
403 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
404 return a->symbol.name < b->symbol.name;
405 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700406
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700409 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700411}
412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800415}
416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417void Attribute::PrintMask(std::ostream* out) const {
418 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 *out << "any";
420 return;
421 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 if (!set) {
426 set = true;
427 } else {
428 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 *out << "reference";
431 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (!set) {
435 set = true;
436 } else {
437 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 *out << "string";
440 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 if (!set) {
444 set = true;
445 } else {
446 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 *out << "integer";
449 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 if (!set) {
453 set = true;
454 } else {
455 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 *out << "boolean";
458 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 if (!set) {
462 set = true;
463 } else {
464 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 *out << "color";
467 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 if (!set) {
471 set = true;
472 } else {
473 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 *out << "float";
476 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 if (!set) {
480 set = true;
481 } else {
482 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 *out << "dimension";
485 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 if (!set) {
489 set = true;
490 } else {
491 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 *out << "fraction";
494 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 if (!set) {
498 set = true;
499 } else {
500 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 *out << "enum";
503 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 if (!set) {
507 set = true;
508 } else {
509 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 *out << "flags";
512 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700513}
514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 if (min_int != std::numeric_limits<int32_t>::min()) {
524 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 if (max_int != std::numeric_limits<int32_t>::max()) {
528 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 *out << " [weak]";
533 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534}
535
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700536static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
537 DiagMessage* out_msg) {
538 *out_msg << "expected";
539 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
540 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800542
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700543 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
544 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800546
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700547 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
548 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800550
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700551 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
552 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800554
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700555 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
556 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800558
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700559 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
560 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800562
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700563 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
564 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800566
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700567 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
568 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800570
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700571 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
572 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800574
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700575 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
576 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800578
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700579 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800580}
581
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700582bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
583 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
584 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
585 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
586 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
587
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700589 item.Flatten(&val);
590
591 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800592
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700594 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
595
596 // Only one type must match between the actual and expected.
597 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700599 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800600 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700602 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700604 // Enums and flags are encoded as integers, so check them first before doing any range checks.
605 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
606 for (const Symbol& s : symbols) {
607 if (flattened_data == s.value) {
608 return true;
609 }
610 }
611
612 // If the attribute accepts integers, we can't fail here.
613 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700615 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 }
617 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700618 }
619 }
620
621 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
622 uint32_t mask = 0u;
623 for (const Symbol& s : symbols) {
624 mask |= s.value;
625 }
626
627 // Check if the flattened data is covered by the flag bit mask.
628 // If the attribute accepts integers, we can't fail here.
629 if ((mask & flattened_data) == flattened_data) {
630 return true;
631 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700633 *out_msg << item << " is not a valid flag";
634 }
635 return false;
636 }
637 }
638
639 // Finally check the integer range of the value.
640 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
641 if (static_cast<int32_t>(flattened_data) < min_int) {
642 if (out_msg) {
643 *out_msg << item << " is less than minimum integer " << min_int;
644 }
645 return false;
646 } else if (static_cast<int32_t>(flattened_data) > max_int) {
647 if (out_msg) {
648 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 }
650 return false;
651 }
652 }
653 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800654}
655
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700656std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
657 if (entry.key.name) {
658 out << entry.key.name.value();
659 } else if (entry.key.id) {
660 out << entry.key.id.value();
661 } else {
662 out << "???";
663 }
664 out << " = " << entry.value;
665 return out;
666}
667
668template <typename T>
669std::vector<T*> ToPointerVec(std::vector<T>& src) {
670 std::vector<T*> dst;
671 dst.reserve(src.size());
672 for (T& in : src) {
673 dst.push_back(&in);
674 }
675 return dst;
676}
677
678template <typename T>
679std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
680 std::vector<const T*> dst;
681 dst.reserve(src.size());
682 for (const T& in : src) {
683 dst.push_back(&in);
684 }
685 return dst;
686}
687
688static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
689 return a->key.name < b->key.name;
690}
691
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692bool Style::Equals(const Value* value) const {
693 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 if (!other) {
695 return false;
696 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700697
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700699 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 return false;
701 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700702
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 if (entries.size() != other->entries.size()) {
704 return false;
705 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700706
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700707 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
708 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700709
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700710 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
711 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700712
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700715 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700717}
718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 Style* style = new Style();
721 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 style->parent_inferred = parent_inferred;
723 style->comment_ = comment_;
724 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700726 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
728 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800729}
730
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 *out << "(style) ";
733 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700734 const Reference& parent_ref = parent.value();
735 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700738 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800741}
742
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700743Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
744 Style::Entry cloned_entry{entry.key};
745 if (entry.value != nullptr) {
746 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700748 return cloned_entry;
749}
750
751void Style::MergeWith(Style* other, StringPool* pool) {
752 if (other->parent) {
753 parent = other->parent;
754 }
755
756 // We can't assume that the entries are sorted alphabetically since they're supposed to be
757 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
758 // them keying off that.
759 //
760 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
761 // those.
762
763 std::vector<Entry*> this_sorted = ToPointerVec(entries);
764 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
765
766 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
767 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
768
769 auto this_iter = this_sorted.begin();
770 const auto this_end = this_sorted.end();
771
772 auto other_iter = other_sorted.begin();
773 const auto other_end = other_sorted.end();
774
775 std::vector<Entry> merged_entries;
776 while (this_iter != this_end) {
777 if (other_iter != other_end) {
778 if ((*this_iter)->key.name < (*other_iter)->key.name) {
779 merged_entries.push_back(std::move(**this_iter));
780 ++this_iter;
781 } else {
782 // The other overrides.
783 merged_entries.push_back(CloneEntry(**other_iter, pool));
784 if ((*this_iter)->key.name == (*other_iter)->key.name) {
785 ++this_iter;
786 }
787 ++other_iter;
788 }
789 } else {
790 merged_entries.push_back(std::move(**this_iter));
791 ++this_iter;
792 }
793 }
794
795 while (other_iter != other_end) {
796 merged_entries.push_back(CloneEntry(**other_iter, pool));
797 ++other_iter;
798 }
799
800 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800801}
802
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700803bool Array::Equals(const Value* value) const {
804 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 if (!other) {
806 return false;
807 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700808
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 if (items.size() != other->items.size()) {
810 return false;
811 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700812
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 return std::equal(items.begin(), items.end(), other->items.begin(),
814 [](const std::unique_ptr<Item>& a,
815 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700818}
819
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 array->comment_ = comment_;
823 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 }
827 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828}
829
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830void Array::Print(std::ostream* out) const {
831 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800832}
833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834bool Plural::Equals(const Value* value) const {
835 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 if (!other) {
837 return false;
838 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700839
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800840 auto one_iter = values.begin();
841 auto one_end_iter = values.end();
842 auto two_iter = other->values.begin();
843 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
844 const std::unique_ptr<Item>& a = *one_iter;
845 const std::unique_ptr<Item>& b = *two_iter;
846 if (a != nullptr && b != nullptr) {
847 if (!a->Equals(b.get())) {
848 return false;
849 }
850 } else if (a != b) {
851 return false;
852 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800854 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700855}
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 p->comment_ = comment_;
860 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 const size_t count = values.size();
862 for (size_t i = 0; i < count; i++) {
863 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800865 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 }
867 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868}
869
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700871 *out << "(plural)";
872 if (values[Zero]) {
873 *out << " zero=" << *values[Zero];
874 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700875
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 if (values[One]) {
877 *out << " one=" << *values[One];
878 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700879
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 if (values[Two]) {
881 *out << " two=" << *values[Two];
882 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700883
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 if (values[Few]) {
885 *out << " few=" << *values[Few];
886 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700887
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 if (values[Many]) {
889 *out << " many=" << *values[Many];
890 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800891
892 if (values[Other]) {
893 *out << " other=" << *values[Other];
894 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800895}
896
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897bool Styleable::Equals(const Value* value) const {
898 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 if (!other) {
900 return false;
901 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700902
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 if (entries.size() != other->entries.size()) {
904 return false;
905 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700906
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
908 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700911}
912
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800915}
916
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920}
921
Adam Lesinski8197cc462016-08-19 12:16:49 -0700922bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700923 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 if (cmp != 0) return cmp < 0;
925 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700926}
927
928bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700930}
931
932bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700933 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700934}
935
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700936struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 bool operator()(const Reference& a, const Reference& b) const {
938 return a.name < b.name;
939 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700940};
941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700944 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 std::set<Reference, NameOnlyComparator> references;
946 references.insert(entries.begin(), entries.end());
947 references.insert(other->entries.begin(), other->entries.end());
948 entries.clear();
949 entries.reserve(references.size());
950 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700951}
952
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953} // namespace aapt