blob: abfdec48df67b4c523cf685b71c8f653467e4c96 [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));
97 const bool dynamic =
98 (resid.package_id() != kFrameworkPackageId && resid.package_id() != kAppPackageId);
99
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 Lesinskicacb28f2016-10-19 12:18:14 -0700122 *out << "(reference) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 if (reference_type == Reference::Type::kResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 *out << "@";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 if (private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 } else {
129 *out << "?";
130 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 if (name) {
133 *out << name.value();
134 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 if (id && !Res_INTERNALID(id.value().id)) {
137 *out << " " << id.value();
138 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141bool Id::Equals(const Value* value) const {
142 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700143}
144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149}
150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153void Id::Print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157bool String::Equals(const Value* value) const {
158 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (!other) {
160 return false;
161 }
Adam Lesinski75421622017-01-06 15:20:04 -0800162
163 if (this->value != other->value) {
164 return false;
165 }
166
167 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
168 return false;
169 }
170
171 auto other_iter = other->untranslatable_sections.begin();
172 for (const UntranslatableSection& this_section : untranslatable_sections) {
173 if (this_section != *other_iter) {
174 return false;
175 }
176 ++other_iter;
177 }
178 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179}
180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 return false;
185 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 out_value->dataType = android::Res_value::TYPE_STRING;
188 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190}
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192String* String::Clone(StringPool* new_pool) const {
193 String* str = new String(new_pool->MakeRef(*value));
194 str->comment_ = comment_;
195 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800196 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198}
199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202}
203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206bool StyledString::Equals(const Value* value) const {
207 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700209 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
211
Adam Lesinski75421622017-01-06 15:20:04 -0800212 if (this->value != other->value) {
213 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
Adam Lesinski75421622017-01-06 15:20:04 -0800215
216 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
217 return false;
218 }
219
220 auto other_iter = other->untranslatable_sections.begin();
221 for (const UntranslatableSection& this_section : untranslatable_sections) {
222 if (this_section != *other_iter) {
223 return false;
224 }
225 ++other_iter;
226 }
227 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228}
229
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230bool StyledString::Flatten(android::Res_value* out_value) const {
231 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 return false;
233 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 out_value->dataType = android::Res_value::TYPE_STRING;
236 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238}
239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240StyledString* StyledString::Clone(StringPool* new_pool) const {
241 StyledString* str = new StyledString(new_pool->MakeRef(value));
242 str->comment_ = comment_;
243 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800244 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246}
247
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248void StyledString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 *out << "(styled string) \"" << *value->str << "\"";
250 for (const StringPool::Span& span : value->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 *out << " " << *span.name << ":" << span.first_char << ","
252 << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254}
255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258bool FileReference::Equals(const Value* value) const {
259 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 if (!other) {
261 return false;
262 }
Adam Lesinski75421622017-01-06 15:20:04 -0800263 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700264}
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266bool FileReference::Flatten(android::Res_value* out_value) const {
267 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 return false;
269 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 out_value->dataType = android::Res_value::TYPE_STRING;
272 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274}
275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276FileReference* FileReference::Clone(StringPool* new_pool) const {
277 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 fr->comment_ = comment_;
280 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286}
287
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 value.dataType = dataType;
292 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293}
294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295bool BinaryPrimitive::Equals(const Value* value) const {
296 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 if (!other) {
298 return false;
299 }
300 return this->value.dataType == other->value.dataType &&
301 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700302}
303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
305 out_value->dataType = value.dataType;
306 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308}
309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 switch (value.dataType) {
316 case android::Res_value::TYPE_NULL:
317 *out << "(null)";
318 break;
319 case android::Res_value::TYPE_INT_DEC:
320 *out << "(integer) " << static_cast<int32_t>(value.data);
321 break;
322 case android::Res_value::TYPE_INT_HEX:
323 *out << "(integer) 0x" << std::hex << value.data << std::dec;
324 break;
325 case android::Res_value::TYPE_INT_BOOLEAN:
326 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
327 break;
328 case android::Res_value::TYPE_INT_COLOR_ARGB8:
329 case android::Res_value::TYPE_INT_COLOR_RGB8:
330 case android::Res_value::TYPE_INT_COLOR_ARGB4:
331 case android::Res_value::TYPE_INT_COLOR_RGB4:
332 *out << "(color) #" << std::hex << value.data << std::dec;
333 break;
334 default:
335 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
336 << std::hex << value.data << std::dec;
337 break;
338 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800339}
340
Adam Lesinskic744ae82017-05-17 19:28:38 -0700341Attribute::Attribute()
342 : type_mask(0u),
343 min_int(std::numeric_limits<int32_t>::min()),
344 max_int(std::numeric_limits<int32_t>::max()) {
345}
346
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 : type_mask(t),
349 min_int(std::numeric_limits<int32_t>::min()),
350 max_int(std::numeric_limits<int32_t>::max()) {
351 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352}
353
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700354std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
355 if (s.symbol.name) {
356 out << s.symbol.name.value().entry;
357 } else {
358 out << "???";
359 }
360 return out << "=" << s.value;
361}
362
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700363template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800364constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700366}
367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368bool Attribute::Equals(const Value* value) const {
369 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 if (!other) {
371 return false;
372 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700373
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 if (symbols.size() != other->symbols.size()) {
375 return false;
376 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700377
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700378 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 return false;
380 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 std::vector<const Symbol*> sorted_a;
383 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800384 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700385 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
386 return a->symbol.name < b->symbol.name;
387 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700388
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700390 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
391 add_pointer<const Symbol>);
392 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
393 return a->symbol.name < b->symbol.name;
394 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700398 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700400}
401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404}
405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406void Attribute::PrintMask(std::ostream* out) const {
407 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 *out << "any";
409 return;
410 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 if (!set) {
415 set = true;
416 } else {
417 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 *out << "reference";
420 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 if (!set) {
424 set = true;
425 } else {
426 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 *out << "string";
429 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 if (!set) {
433 set = true;
434 } else {
435 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 *out << "integer";
438 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if (!set) {
442 set = true;
443 } else {
444 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 *out << "boolean";
447 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 if (!set) {
451 set = true;
452 } else {
453 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 *out << "color";
456 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 if (!set) {
460 set = true;
461 } else {
462 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 *out << "float";
465 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 if (!set) {
469 set = true;
470 } else {
471 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800472 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 *out << "dimension";
474 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 if (!set) {
478 set = true;
479 } else {
480 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 *out << "fraction";
483 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 if (!set) {
487 set = true;
488 } else {
489 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 *out << "enum";
492 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 if (!set) {
496 set = true;
497 } else {
498 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 *out << "flags";
501 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700502}
503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (min_int != std::numeric_limits<int32_t>::min()) {
513 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 if (max_int != std::numeric_limits<int32_t>::max()) {
517 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 *out << " [weak]";
522 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800523}
524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800527 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 *msg << " boolean";
531 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 *msg << " color";
535 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800536
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 *msg << " dimension";
539 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 *msg << " enum";
543 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 *msg << " flags";
547 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 *msg << " float";
551 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 *msg << " fraction";
555 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800556
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 *msg << " integer";
559 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800560
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 *msg << " reference";
563 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 if (attr->type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 *msg << " string";
567 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800568
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800570}
571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800575
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
578 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
579 if (out_msg) {
580 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800581 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 return false;
583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
587 if (out_msg) {
588 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 }
590 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
592 if (out_msg) {
593 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 }
595 return false;
596 }
597 }
598 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800599}
600
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700601std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
602 if (entry.key.name) {
603 out << entry.key.name.value();
604 } else if (entry.key.id) {
605 out << entry.key.id.value();
606 } else {
607 out << "???";
608 }
609 out << " = " << entry.value;
610 return out;
611}
612
613template <typename T>
614std::vector<T*> ToPointerVec(std::vector<T>& src) {
615 std::vector<T*> dst;
616 dst.reserve(src.size());
617 for (T& in : src) {
618 dst.push_back(&in);
619 }
620 return dst;
621}
622
623template <typename T>
624std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
625 std::vector<const T*> dst;
626 dst.reserve(src.size());
627 for (const T& in : src) {
628 dst.push_back(&in);
629 }
630 return dst;
631}
632
633static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
634 return a->key.name < b->key.name;
635}
636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637bool Style::Equals(const Value* value) const {
638 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 if (!other) {
640 return false;
641 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700642
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700644 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 return false;
646 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700647
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 if (entries.size() != other->entries.size()) {
649 return false;
650 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700651
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700652 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
653 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700654
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700655 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
656 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700657
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700660 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700662}
663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 Style* style = new Style();
666 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 style->parent_inferred = parent_inferred;
668 style->comment_ = comment_;
669 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700671 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
673 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800674}
675
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 *out << "(style) ";
678 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700679 const Reference& parent_ref = parent.value();
680 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800682 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700683 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686}
687
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700688Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
689 Style::Entry cloned_entry{entry.key};
690 if (entry.value != nullptr) {
691 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700693 return cloned_entry;
694}
695
696void Style::MergeWith(Style* other, StringPool* pool) {
697 if (other->parent) {
698 parent = other->parent;
699 }
700
701 // We can't assume that the entries are sorted alphabetically since they're supposed to be
702 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
703 // them keying off that.
704 //
705 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
706 // those.
707
708 std::vector<Entry*> this_sorted = ToPointerVec(entries);
709 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
710
711 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
712 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
713
714 auto this_iter = this_sorted.begin();
715 const auto this_end = this_sorted.end();
716
717 auto other_iter = other_sorted.begin();
718 const auto other_end = other_sorted.end();
719
720 std::vector<Entry> merged_entries;
721 while (this_iter != this_end) {
722 if (other_iter != other_end) {
723 if ((*this_iter)->key.name < (*other_iter)->key.name) {
724 merged_entries.push_back(std::move(**this_iter));
725 ++this_iter;
726 } else {
727 // The other overrides.
728 merged_entries.push_back(CloneEntry(**other_iter, pool));
729 if ((*this_iter)->key.name == (*other_iter)->key.name) {
730 ++this_iter;
731 }
732 ++other_iter;
733 }
734 } else {
735 merged_entries.push_back(std::move(**this_iter));
736 ++this_iter;
737 }
738 }
739
740 while (other_iter != other_end) {
741 merged_entries.push_back(CloneEntry(**other_iter, pool));
742 ++other_iter;
743 }
744
745 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800746}
747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748bool Array::Equals(const Value* value) const {
749 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 if (!other) {
751 return false;
752 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700753
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 if (items.size() != other->items.size()) {
755 return false;
756 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700757
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 return std::equal(items.begin(), items.end(), other->items.begin(),
759 [](const std::unique_ptr<Item>& a,
760 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700763}
764
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 array->comment_ = comment_;
768 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 }
772 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800773}
774
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775void Array::Print(std::ostream* out) const {
776 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800777}
778
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779bool Plural::Equals(const Value* value) const {
780 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 if (!other) {
782 return false;
783 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700784
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800785 auto one_iter = values.begin();
786 auto one_end_iter = values.end();
787 auto two_iter = other->values.begin();
788 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
789 const std::unique_ptr<Item>& a = *one_iter;
790 const std::unique_ptr<Item>& b = *two_iter;
791 if (a != nullptr && b != nullptr) {
792 if (!a->Equals(b.get())) {
793 return false;
794 }
795 } else if (a != b) {
796 return false;
797 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800799 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700800}
801
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 p->comment_ = comment_;
805 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 const size_t count = values.size();
807 for (size_t i = 0; i < count; i++) {
808 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800810 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 }
812 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800813}
814
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 *out << "(plural)";
817 if (values[Zero]) {
818 *out << " zero=" << *values[Zero];
819 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700820
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 if (values[One]) {
822 *out << " one=" << *values[One];
823 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700824
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 if (values[Two]) {
826 *out << " two=" << *values[Two];
827 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700828
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 if (values[Few]) {
830 *out << " few=" << *values[Few];
831 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700832
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 if (values[Many]) {
834 *out << " many=" << *values[Many];
835 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800836
837 if (values[Other]) {
838 *out << " other=" << *values[Other];
839 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800840}
841
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842bool Styleable::Equals(const Value* value) const {
843 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 if (!other) {
845 return false;
846 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700847
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 if (entries.size() != other->entries.size()) {
849 return false;
850 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700851
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
853 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700856}
857
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700858Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860}
861
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700862void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800865}
866
Adam Lesinski8197cc462016-08-19 12:16:49 -0700867bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 if (cmp != 0) return cmp < 0;
870 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700871}
872
873bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700875}
876
877bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700879}
880
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700881struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 bool operator()(const Reference& a, const Reference& b) const {
883 return a.name < b.name;
884 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700885};
886
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700887void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700889 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 std::set<Reference, NameOnlyComparator> references;
891 references.insert(entries.begin(), entries.end());
892 references.insert(other->entries.begin(), other->entries.end());
893 entries.clear();
894 entries.reserve(references.size());
895 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700896}
897
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898} // namespace aapt