blob: e808984c75f531e0a8197444bf4330af5be5c797 [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 Lesinskice5e56e2016-10-21 17:56:45 -0700536static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800538 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 *msg << " boolean";
542 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 *msg << " color";
546 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 *msg << " dimension";
550 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 *msg << " enum";
554 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 *msg << " flags";
558 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 *msg << " float";
562 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 *msg << " fraction";
566 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 *msg << " integer";
570 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 *msg << " reference";
574 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (attr->type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 *msg << " string";
578 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800579
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800581}
582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800586
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
589 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
590 if (out_msg) {
591 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800592 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return false;
594
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
598 if (out_msg) {
599 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
601 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
603 if (out_msg) {
604 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 }
606 return false;
607 }
608 }
609 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800610}
611
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700612std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
613 if (entry.key.name) {
614 out << entry.key.name.value();
615 } else if (entry.key.id) {
616 out << entry.key.id.value();
617 } else {
618 out << "???";
619 }
620 out << " = " << entry.value;
621 return out;
622}
623
624template <typename T>
625std::vector<T*> ToPointerVec(std::vector<T>& src) {
626 std::vector<T*> dst;
627 dst.reserve(src.size());
628 for (T& in : src) {
629 dst.push_back(&in);
630 }
631 return dst;
632}
633
634template <typename T>
635std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
636 std::vector<const T*> dst;
637 dst.reserve(src.size());
638 for (const T& in : src) {
639 dst.push_back(&in);
640 }
641 return dst;
642}
643
644static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
645 return a->key.name < b->key.name;
646}
647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648bool Style::Equals(const Value* value) const {
649 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 if (!other) {
651 return false;
652 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700653
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700655 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 return false;
657 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700658
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 if (entries.size() != other->entries.size()) {
660 return false;
661 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700662
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700663 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
664 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700665
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700666 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
667 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700668
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700671 return a->key.Equals(&b->key) && a->value->Equals(b->value.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 -0700675Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 Style* style = new Style();
677 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 style->parent_inferred = parent_inferred;
679 style->comment_ = comment_;
680 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700682 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
684 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800685}
686
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 *out << "(style) ";
689 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700690 const Reference& parent_ref = parent.value();
691 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800693 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700694 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800697}
698
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700699Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
700 Style::Entry cloned_entry{entry.key};
701 if (entry.value != nullptr) {
702 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700704 return cloned_entry;
705}
706
707void Style::MergeWith(Style* other, StringPool* pool) {
708 if (other->parent) {
709 parent = other->parent;
710 }
711
712 // We can't assume that the entries are sorted alphabetically since they're supposed to be
713 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
714 // them keying off that.
715 //
716 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
717 // those.
718
719 std::vector<Entry*> this_sorted = ToPointerVec(entries);
720 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
721
722 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
723 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
724
725 auto this_iter = this_sorted.begin();
726 const auto this_end = this_sorted.end();
727
728 auto other_iter = other_sorted.begin();
729 const auto other_end = other_sorted.end();
730
731 std::vector<Entry> merged_entries;
732 while (this_iter != this_end) {
733 if (other_iter != other_end) {
734 if ((*this_iter)->key.name < (*other_iter)->key.name) {
735 merged_entries.push_back(std::move(**this_iter));
736 ++this_iter;
737 } else {
738 // The other overrides.
739 merged_entries.push_back(CloneEntry(**other_iter, pool));
740 if ((*this_iter)->key.name == (*other_iter)->key.name) {
741 ++this_iter;
742 }
743 ++other_iter;
744 }
745 } else {
746 merged_entries.push_back(std::move(**this_iter));
747 ++this_iter;
748 }
749 }
750
751 while (other_iter != other_end) {
752 merged_entries.push_back(CloneEntry(**other_iter, pool));
753 ++other_iter;
754 }
755
756 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757}
758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759bool Array::Equals(const Value* value) const {
760 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 if (!other) {
762 return false;
763 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700764
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 if (items.size() != other->items.size()) {
766 return false;
767 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700768
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 return std::equal(items.begin(), items.end(), other->items.begin(),
770 [](const std::unique_ptr<Item>& a,
771 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700774}
775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 array->comment_ = comment_;
779 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 }
783 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784}
785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786void Array::Print(std::ostream* out) const {
787 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800788}
789
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790bool Plural::Equals(const Value* value) const {
791 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 if (!other) {
793 return false;
794 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700795
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800796 auto one_iter = values.begin();
797 auto one_end_iter = values.end();
798 auto two_iter = other->values.begin();
799 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
800 const std::unique_ptr<Item>& a = *one_iter;
801 const std::unique_ptr<Item>& b = *two_iter;
802 if (a != nullptr && b != nullptr) {
803 if (!a->Equals(b.get())) {
804 return false;
805 }
806 } else if (a != b) {
807 return false;
808 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800810 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700811}
812
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 p->comment_ = comment_;
816 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 const size_t count = values.size();
818 for (size_t i = 0; i < count; i++) {
819 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800821 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
823 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800824}
825
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 *out << "(plural)";
828 if (values[Zero]) {
829 *out << " zero=" << *values[Zero];
830 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700831
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 if (values[One]) {
833 *out << " one=" << *values[One];
834 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700835
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 if (values[Two]) {
837 *out << " two=" << *values[Two];
838 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700839
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 if (values[Few]) {
841 *out << " few=" << *values[Few];
842 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700843
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 if (values[Many]) {
845 *out << " many=" << *values[Many];
846 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800847
848 if (values[Other]) {
849 *out << " other=" << *values[Other];
850 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800851}
852
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700853bool Styleable::Equals(const Value* value) const {
854 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 if (!other) {
856 return false;
857 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700858
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 if (entries.size() != other->entries.size()) {
860 return false;
861 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700862
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
864 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700867}
868
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871}
872
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800876}
877
Adam Lesinski8197cc462016-08-19 12:16:49 -0700878bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 if (cmp != 0) return cmp < 0;
881 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700882}
883
884bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700886}
887
888bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700890}
891
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700892struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 bool operator()(const Reference& a, const Reference& b) const {
894 return a.name < b.name;
895 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700896};
897
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700900 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 std::set<Reference, NameOnlyComparator> references;
902 references.insert(entries.begin(), entries.end());
903 references.insert(other->entries.begin(), other->entries.end());
904 entries.clear();
905 entries.reserve(references.size());
906 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700907}
908
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909} // namespace aapt