blob: 1cba19462839948a48b65057cc6a4075a7cfebfa [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 Lesinski060b53d2017-07-28 17:10:35 -0700256 *out << "(styled string) \"" << value->value << "\"";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 for (const StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700258 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260}
261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264bool FileReference::Equals(const Value* value) const {
265 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 if (!other) {
267 return false;
268 }
Adam Lesinski75421622017-01-06 15:20:04 -0800269 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700270}
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272bool FileReference::Flatten(android::Res_value* out_value) const {
273 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 return false;
275 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 out_value->dataType = android::Res_value::TYPE_STRING;
278 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280}
281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282FileReference* FileReference::Clone(StringPool* new_pool) const {
283 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 fr->comment_ = comment_;
286 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288}
289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292}
293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 value.dataType = dataType;
298 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299}
300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301bool BinaryPrimitive::Equals(const Value* value) const {
302 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 if (!other) {
304 return false;
305 }
306 return this->value.dataType == other->value.dataType &&
307 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700308}
309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
311 out_value->dataType = value.dataType;
312 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318}
319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 switch (value.dataType) {
322 case android::Res_value::TYPE_NULL:
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700323 if (value.data == android::Res_value::DATA_NULL_EMPTY) {
324 *out << "(empty)";
325 } else {
326 *out << "(null)";
327 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 break;
329 case android::Res_value::TYPE_INT_DEC:
330 *out << "(integer) " << static_cast<int32_t>(value.data);
331 break;
332 case android::Res_value::TYPE_INT_HEX:
333 *out << "(integer) 0x" << std::hex << value.data << std::dec;
334 break;
335 case android::Res_value::TYPE_INT_BOOLEAN:
336 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
337 break;
338 case android::Res_value::TYPE_INT_COLOR_ARGB8:
339 case android::Res_value::TYPE_INT_COLOR_RGB8:
340 case android::Res_value::TYPE_INT_COLOR_ARGB4:
341 case android::Res_value::TYPE_INT_COLOR_RGB4:
342 *out << "(color) #" << std::hex << value.data << std::dec;
343 break;
344 default:
345 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
346 << std::hex << value.data << std::dec;
347 break;
348 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349}
350
Adam Lesinskic744ae82017-05-17 19:28:38 -0700351Attribute::Attribute()
352 : type_mask(0u),
353 min_int(std::numeric_limits<int32_t>::min()),
354 max_int(std::numeric_limits<int32_t>::max()) {
355}
356
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 : type_mask(t),
359 min_int(std::numeric_limits<int32_t>::min()),
360 max_int(std::numeric_limits<int32_t>::max()) {
361 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362}
363
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700364std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
365 if (s.symbol.name) {
366 out << s.symbol.name.value().entry;
367 } else {
368 out << "???";
369 }
370 return out << "=" << s.value;
371}
372
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700373template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800374constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700376}
377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378bool Attribute::Equals(const Value* value) const {
379 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 if (!other) {
381 return false;
382 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700383
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 if (symbols.size() != other->symbols.size()) {
385 return false;
386 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700387
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700388 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 return false;
390 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 std::vector<const Symbol*> sorted_a;
393 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800394 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700395 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
396 return a->symbol.name < b->symbol.name;
397 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700400 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
401 add_pointer<const Symbol>);
402 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
403 return a->symbol.name < b->symbol.name;
404 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700408 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700410}
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800414}
415
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416void Attribute::PrintMask(std::ostream* out) const {
417 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 *out << "any";
419 return;
420 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 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 << "reference";
430 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 if ((type_mask & android::ResTable_map::TYPE_STRING) != 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 << "string";
439 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 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 << "integer";
448 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 if (!set) {
452 set = true;
453 } else {
454 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 *out << "boolean";
457 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 if (!set) {
461 set = true;
462 } else {
463 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 *out << "color";
466 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 if (!set) {
470 set = true;
471 } else {
472 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 *out << "float";
475 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800476
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 if (!set) {
479 set = true;
480 } else {
481 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 *out << "dimension";
484 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 if (!set) {
488 set = true;
489 } else {
490 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 *out << "fraction";
493 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 if (!set) {
497 set = true;
498 } else {
499 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 *out << "enum";
502 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 if (!set) {
506 set = true;
507 } else {
508 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 *out << "flags";
511 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700512}
513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 if (min_int != std::numeric_limits<int32_t>::min()) {
523 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700525
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 if (max_int != std::numeric_limits<int32_t>::max()) {
527 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 *out << " [weak]";
532 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533}
534
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700535static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
536 DiagMessage* out_msg) {
537 *out_msg << "expected";
538 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
539 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800541
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700542 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
543 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800545
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700546 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
547 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800549
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700550 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
551 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800553
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700554 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
555 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800557
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700558 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
559 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800561
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700562 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
563 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800565
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700566 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
567 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800569
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700570 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
571 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800573
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700574 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
575 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800577
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700578 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800579}
580
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700581bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
582 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
583 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
584 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
585 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
586
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700588 item.Flatten(&val);
589
590 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800591
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700593 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
594
595 // Only one type must match between the actual and expected.
596 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700598 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800599 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700601 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700603 // Enums and flags are encoded as integers, so check them first before doing any range checks.
604 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
605 for (const Symbol& s : symbols) {
606 if (flattened_data == s.value) {
607 return true;
608 }
609 }
610
611 // If the attribute accepts integers, we can't fail here.
612 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700614 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 }
616 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700617 }
618 }
619
620 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
621 uint32_t mask = 0u;
622 for (const Symbol& s : symbols) {
623 mask |= s.value;
624 }
625
626 // Check if the flattened data is covered by the flag bit mask.
627 // If the attribute accepts integers, we can't fail here.
628 if ((mask & flattened_data) == flattened_data) {
629 return true;
630 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700632 *out_msg << item << " is not a valid flag";
633 }
634 return false;
635 }
636 }
637
638 // Finally check the integer range of the value.
639 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
640 if (static_cast<int32_t>(flattened_data) < min_int) {
641 if (out_msg) {
642 *out_msg << item << " is less than minimum integer " << min_int;
643 }
644 return false;
645 } else if (static_cast<int32_t>(flattened_data) > max_int) {
646 if (out_msg) {
647 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
649 return false;
650 }
651 }
652 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800653}
654
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700655std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
656 if (entry.key.name) {
657 out << entry.key.name.value();
658 } else if (entry.key.id) {
659 out << entry.key.id.value();
660 } else {
661 out << "???";
662 }
663 out << " = " << entry.value;
664 return out;
665}
666
667template <typename T>
668std::vector<T*> ToPointerVec(std::vector<T>& src) {
669 std::vector<T*> dst;
670 dst.reserve(src.size());
671 for (T& in : src) {
672 dst.push_back(&in);
673 }
674 return dst;
675}
676
677template <typename T>
678std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
679 std::vector<const T*> dst;
680 dst.reserve(src.size());
681 for (const T& in : src) {
682 dst.push_back(&in);
683 }
684 return dst;
685}
686
687static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
688 return a->key.name < b->key.name;
689}
690
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691bool Style::Equals(const Value* value) const {
692 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 if (!other) {
694 return false;
695 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700698 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 return false;
700 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700701
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 if (entries.size() != other->entries.size()) {
703 return false;
704 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700705
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700706 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
707 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700708
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700709 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
710 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700711
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700714 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700716}
717
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 Style* style = new Style();
720 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 style->parent_inferred = parent_inferred;
722 style->comment_ = comment_;
723 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700725 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 }
727 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800728}
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 *out << "(style) ";
732 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700733 const Reference& parent_ref = parent.value();
734 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800736 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700737 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740}
741
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700742Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
743 Style::Entry cloned_entry{entry.key};
744 if (entry.value != nullptr) {
745 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700747 return cloned_entry;
748}
749
750void Style::MergeWith(Style* other, StringPool* pool) {
751 if (other->parent) {
752 parent = other->parent;
753 }
754
755 // We can't assume that the entries are sorted alphabetically since they're supposed to be
756 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
757 // them keying off that.
758 //
759 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
760 // those.
761
762 std::vector<Entry*> this_sorted = ToPointerVec(entries);
763 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
764
765 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
766 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
767
768 auto this_iter = this_sorted.begin();
769 const auto this_end = this_sorted.end();
770
771 auto other_iter = other_sorted.begin();
772 const auto other_end = other_sorted.end();
773
774 std::vector<Entry> merged_entries;
775 while (this_iter != this_end) {
776 if (other_iter != other_end) {
777 if ((*this_iter)->key.name < (*other_iter)->key.name) {
778 merged_entries.push_back(std::move(**this_iter));
779 ++this_iter;
780 } else {
781 // The other overrides.
782 merged_entries.push_back(CloneEntry(**other_iter, pool));
783 if ((*this_iter)->key.name == (*other_iter)->key.name) {
784 ++this_iter;
785 }
786 ++other_iter;
787 }
788 } else {
789 merged_entries.push_back(std::move(**this_iter));
790 ++this_iter;
791 }
792 }
793
794 while (other_iter != other_end) {
795 merged_entries.push_back(CloneEntry(**other_iter, pool));
796 ++other_iter;
797 }
798
799 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800800}
801
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802bool Array::Equals(const Value* value) const {
803 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 if (!other) {
805 return false;
806 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700807
Adam Lesinski4ffea042017-08-10 15:37:28 -0700808 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 return false;
810 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700811
Adam Lesinski4ffea042017-08-10 15:37:28 -0700812 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
813 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700816}
817
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 array->comment_ = comment_;
821 array->source_ = source_;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700822 for (auto& item : elements) {
823 array->elements.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
825 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800826}
827
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700829 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800830}
831
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832bool Plural::Equals(const Value* value) const {
833 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 if (!other) {
835 return false;
836 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700837
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800838 auto one_iter = values.begin();
839 auto one_end_iter = values.end();
840 auto two_iter = other->values.begin();
841 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
842 const std::unique_ptr<Item>& a = *one_iter;
843 const std::unique_ptr<Item>& b = *two_iter;
844 if (a != nullptr && b != nullptr) {
845 if (!a->Equals(b.get())) {
846 return false;
847 }
848 } else if (a != b) {
849 return false;
850 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800852 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700853}
854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 p->comment_ = comment_;
858 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 const size_t count = values.size();
860 for (size_t i = 0; i < count; i++) {
861 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700862 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
865 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800866}
867
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 *out << "(plural)";
870 if (values[Zero]) {
871 *out << " zero=" << *values[Zero];
872 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700873
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 if (values[One]) {
875 *out << " one=" << *values[One];
876 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700877
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 if (values[Two]) {
879 *out << " two=" << *values[Two];
880 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700881
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 if (values[Few]) {
883 *out << " few=" << *values[Few];
884 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700885
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 if (values[Many]) {
887 *out << " many=" << *values[Many];
888 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800889
890 if (values[Other]) {
891 *out << " other=" << *values[Other];
892 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893}
894
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895bool Styleable::Equals(const Value* value) const {
896 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 if (!other) {
898 return false;
899 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700900
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 if (entries.size() != other->entries.size()) {
902 return false;
903 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700904
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
906 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700909}
910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800913}
914
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700915void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918}
919
Adam Lesinski8197cc462016-08-19 12:16:49 -0700920bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 if (cmp != 0) return cmp < 0;
923 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700924}
925
926bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700927 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700928}
929
930bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700932}
933
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700934struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 bool operator()(const Reference& a, const Reference& b) const {
936 return a.name < b.name;
937 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700938};
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700942 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 std::set<Reference, NameOnlyComparator> references;
944 references.insert(entries.begin(), entries.end());
945 references.insert(other->entries.begin(), other->entries.end());
946 entries.clear();
947 entries.reserve(references.size());
948 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700949}
950
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951} // namespace aapt