blob: 34bd2b4361ca4a9c7f174287d67cd103f4704255 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceValues.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
20#include <limits>
21#include <set>
22
23#include "androidfw/ResourceTypes.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080026#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070028#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070029
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033void BaseValue<Derived>::Accept(RawValueVisitor* visitor) {
34 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035}
36
37template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038void BaseItem<Derived>::Accept(RawValueVisitor* visitor) {
39 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040}
41
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044bool RawString::Equals(const Value* value) const {
45 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 if (!other) {
47 return false;
48 }
49 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070050}
51
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052RawString* RawString::Clone(StringPool* new_pool) const {
53 RawString* rs = new RawString(new_pool->MakeRef(*value));
54 rs->comment_ = comment_;
55 rs->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059bool RawString::Flatten(android::Res_value* out_value) const {
60 out_value->dataType = android::Res_value::TYPE_STRING;
61 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067}
68
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080bool Reference::Equals(const Value* value) const {
81 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (!other) {
83 return false;
84 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 return reference_type == other->reference_type &&
86 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -070088}
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090bool Reference::Flatten(android::Res_value* out_value) const {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080091 const ResourceId resid = id.value_or_default(ResourceId(0));
92 const bool dynamic =
93 (resid.package_id() != kFrameworkPackageId && resid.package_id() != kAppPackageId);
94
95 if (reference_type == Reference::Type::kResource) {
96 if (dynamic) {
97 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
98 } else {
99 out_value->dataType = android::Res_value::TYPE_REFERENCE;
100 }
101 } else {
102 if (dynamic) {
103 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
104 } else {
105 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
106 }
107 }
108 out_value->data = util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110}
111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114}
115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116void Reference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 *out << "(reference) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 if (reference_type == Reference::Type::kResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 *out << "@";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 if (private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 } else {
124 *out << "?";
125 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 if (name) {
128 *out << name.value();
129 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 if (id && !Res_INTERNALID(id.value().id)) {
132 *out << " " << id.value();
133 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134}
135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136bool Id::Equals(const Value* value) const {
137 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700138}
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144}
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148void Id::Print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152bool String::Equals(const Value* value) const {
153 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 if (!other) {
155 return false;
156 }
Adam Lesinski75421622017-01-06 15:20:04 -0800157
158 if (this->value != other->value) {
159 return false;
160 }
161
162 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
163 return false;
164 }
165
166 auto other_iter = other->untranslatable_sections.begin();
167 for (const UntranslatableSection& this_section : untranslatable_sections) {
168 if (this_section != *other_iter) {
169 return false;
170 }
171 ++other_iter;
172 }
173 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174}
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return false;
180 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 out_value->dataType = android::Res_value::TYPE_STRING;
183 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185}
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187String* String::Clone(StringPool* new_pool) const {
188 String* str = new String(new_pool->MakeRef(*value));
189 str->comment_ = comment_;
190 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800191 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193}
194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197}
198
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201bool StyledString::Equals(const Value* value) const {
202 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700204 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 }
206
Adam Lesinski75421622017-01-06 15:20:04 -0800207 if (this->value != other->value) {
208 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
Adam Lesinski75421622017-01-06 15:20:04 -0800210
211 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
212 return false;
213 }
214
215 auto other_iter = other->untranslatable_sections.begin();
216 for (const UntranslatableSection& this_section : untranslatable_sections) {
217 if (this_section != *other_iter) {
218 return false;
219 }
220 ++other_iter;
221 }
222 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223}
224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225bool StyledString::Flatten(android::Res_value* out_value) const {
226 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 return false;
228 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 out_value->dataType = android::Res_value::TYPE_STRING;
231 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233}
234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235StyledString* StyledString::Clone(StringPool* new_pool) const {
236 StyledString* str = new StyledString(new_pool->MakeRef(value));
237 str->comment_ = comment_;
238 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800239 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241}
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243void StyledString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 *out << "(styled string) \"" << *value->str << "\"";
245 for (const StringPool::Span& span : value->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 *out << " " << *span.name << ":" << span.first_char << ","
247 << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249}
250
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253bool FileReference::Equals(const Value* value) const {
254 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 if (!other) {
256 return false;
257 }
Adam Lesinski75421622017-01-06 15:20:04 -0800258 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700259}
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261bool FileReference::Flatten(android::Res_value* out_value) const {
262 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 return false;
264 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 out_value->dataType = android::Res_value::TYPE_STRING;
267 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269}
270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271FileReference* FileReference::Clone(StringPool* new_pool) const {
272 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 fr->comment_ = comment_;
275 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277}
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 value.dataType = dataType;
287 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700288}
289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290bool BinaryPrimitive::Equals(const Value* value) const {
291 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 if (!other) {
293 return false;
294 }
295 return this->value.dataType == other->value.dataType &&
296 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700297}
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
300 out_value->dataType = value.dataType;
301 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303}
304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307}
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 switch (value.dataType) {
311 case android::Res_value::TYPE_NULL:
312 *out << "(null)";
313 break;
314 case android::Res_value::TYPE_INT_DEC:
315 *out << "(integer) " << static_cast<int32_t>(value.data);
316 break;
317 case android::Res_value::TYPE_INT_HEX:
318 *out << "(integer) 0x" << std::hex << value.data << std::dec;
319 break;
320 case android::Res_value::TYPE_INT_BOOLEAN:
321 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
322 break;
323 case android::Res_value::TYPE_INT_COLOR_ARGB8:
324 case android::Res_value::TYPE_INT_COLOR_RGB8:
325 case android::Res_value::TYPE_INT_COLOR_ARGB4:
326 case android::Res_value::TYPE_INT_COLOR_RGB4:
327 *out << "(color) #" << std::hex << value.data << std::dec;
328 break;
329 default:
330 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
331 << std::hex << value.data << std::dec;
332 break;
333 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334}
335
Adam Lesinskic744ae82017-05-17 19:28:38 -0700336Attribute::Attribute()
337 : type_mask(0u),
338 min_int(std::numeric_limits<int32_t>::min()),
339 max_int(std::numeric_limits<int32_t>::max()) {
340}
341
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 : type_mask(t),
344 min_int(std::numeric_limits<int32_t>::min()),
345 max_int(std::numeric_limits<int32_t>::max()) {
346 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347}
348
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700349template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800350constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700352}
353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354bool Attribute::Equals(const Value* value) const {
355 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 if (!other) {
357 return false;
358 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700359
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 if (symbols.size() != other->symbols.size()) {
361 return false;
362 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 if (type_mask != other->type_mask || min_int != other->min_int ||
365 max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return false;
367 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700368
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 std::vector<const Symbol*> sorted_a;
370 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800371 add_pointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 [](const Symbol* a, const Symbol* b) -> bool {
374 return a->symbol.name < b->symbol.name;
375 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 std::vector<const Symbol*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 std::transform(other->symbols.begin(), other->symbols.end(),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800379 std::back_inserter(sorted_b), add_pointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 [](const Symbol* a, const Symbol* b) -> bool {
382 return a->symbol.name < b->symbol.name;
383 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 return a->symbol.Equals(&b->symbol) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 a->value == b->value;
389 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700390}
391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394}
395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396void Attribute::PrintMask(std::ostream* out) const {
397 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 *out << "any";
399 return;
400 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 if (!set) {
405 set = true;
406 } else {
407 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 *out << "reference";
410 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 if (!set) {
414 set = true;
415 } else {
416 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800417 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 *out << "string";
419 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 if (!set) {
423 set = true;
424 } else {
425 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800426 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 *out << "integer";
428 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 if (!set) {
432 set = true;
433 } else {
434 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 *out << "boolean";
437 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 if (!set) {
441 set = true;
442 } else {
443 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 *out << "color";
446 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 if (!set) {
450 set = true;
451 } else {
452 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 *out << "float";
455 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 if (!set) {
459 set = true;
460 } else {
461 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800462 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 *out << "dimension";
464 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 if (!set) {
468 set = true;
469 } else {
470 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800471 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 *out << "fraction";
473 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 if (!set) {
477 set = true;
478 } else {
479 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 *out << "enum";
482 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 if (!set) {
486 set = true;
487 } else {
488 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800489 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 *out << "flags";
491 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700492}
493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 if (min_int != std::numeric_limits<int32_t>::min()) {
503 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 if (max_int != std::numeric_limits<int32_t>::max()) {
507 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700509
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 *out << " [weak]";
512 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513}
514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800517 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 *msg << " boolean";
521 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 *msg << " color";
525 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 *msg << " dimension";
529 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 *msg << " enum";
533 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800534
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 *msg << " flags";
537 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800538
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 *msg << " float";
541 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 *msg << " fraction";
545 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 *msg << " integer";
549 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 *msg << " reference";
553 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (attr->type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 *msg << " string";
557 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800558
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800560}
561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800565
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
568 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
569 if (out_msg) {
570 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800571 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return false;
573
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
577 if (out_msg) {
578 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 }
580 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
582 if (out_msg) {
583 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 }
585 return false;
586 }
587 }
588 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800589}
590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591bool Style::Equals(const Value* value) const {
592 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 if (!other) {
594 return false;
595 }
596 if (bool(parent) != bool(other->parent) ||
597 (parent && other->parent &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return false;
600 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700601
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 if (entries.size() != other->entries.size()) {
603 return false;
604 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 std::vector<const Entry*> sorted_a;
607 std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800608 add_pointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 [](const Entry* a, const Entry* b) -> bool {
611 return a->key.name < b->key.name;
612 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700613
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 std::vector<const Entry*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 std::transform(other->entries.begin(), other->entries.end(),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800616 std::back_inserter(sorted_b), add_pointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 [](const Entry* a, const Entry* b) -> bool {
619 return a->key.name < b->key.name;
620 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 return a->key.Equals(&b->key) &&
625 a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700627}
628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 Style* style = new Style();
631 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 style->parent_inferred = parent_inferred;
633 style->comment_ = comment_;
634 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 for (auto& entry : entries) {
636 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 }
639 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800640}
641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 *out << "(style) ";
644 if (parent && parent.value().name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 if (parent.value().private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800647 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 *out << parent.value().name.value();
649 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800651}
652
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653static ::std::ostream& operator<<(::std::ostream& out,
654 const Style::Entry& value) {
655 if (value.key.name) {
656 out << value.key.name.value();
657 } else if (value.key.id) {
658 out << value.key.id.value();
659 } else {
660 out << "???";
661 }
662 out << " = ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 value.value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800665}
666
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667bool Array::Equals(const Value* value) const {
668 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 if (!other) {
670 return false;
671 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700672
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 if (items.size() != other->items.size()) {
674 return false;
675 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700676
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 return std::equal(items.begin(), items.end(), other->items.begin(),
678 [](const std::unique_ptr<Item>& a,
679 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700682}
683
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 array->comment_ = comment_;
687 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
691 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800692}
693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694void Array::Print(std::ostream* out) const {
695 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800696}
697
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698bool Plural::Equals(const Value* value) const {
699 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 if (!other) {
701 return false;
702 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700703
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800704 auto one_iter = values.begin();
705 auto one_end_iter = values.end();
706 auto two_iter = other->values.begin();
707 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
708 const std::unique_ptr<Item>& a = *one_iter;
709 const std::unique_ptr<Item>& b = *two_iter;
710 if (a != nullptr && b != nullptr) {
711 if (!a->Equals(b.get())) {
712 return false;
713 }
714 } else if (a != b) {
715 return false;
716 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800718 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700719}
720
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 p->comment_ = comment_;
724 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 const size_t count = values.size();
726 for (size_t i = 0; i < count; i++) {
727 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800729 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
731 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732}
733
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 *out << "(plural)";
736 if (values[Zero]) {
737 *out << " zero=" << *values[Zero];
738 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700739
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 if (values[One]) {
741 *out << " one=" << *values[One];
742 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700743
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 if (values[Two]) {
745 *out << " two=" << *values[Two];
746 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700747
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 if (values[Few]) {
749 *out << " few=" << *values[Few];
750 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700751
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 if (values[Many]) {
753 *out << " many=" << *values[Many];
754 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800755
756 if (values[Other]) {
757 *out << " other=" << *values[Other];
758 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800759}
760
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761static ::std::ostream& operator<<(::std::ostream& out,
762 const std::unique_ptr<Item>& item) {
763 return out << *item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800764}
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766bool Styleable::Equals(const Value* value) const {
767 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 if (!other) {
769 return false;
770 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700771
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 if (entries.size() != other->entries.size()) {
773 return false;
774 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700775
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
777 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700780}
781
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784}
785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800789}
790
Adam Lesinski8197cc462016-08-19 12:16:49 -0700791bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 if (cmp != 0) return cmp < 0;
794 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700795}
796
797bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700799}
800
801bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700803}
804
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700805struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 bool operator()(const Reference& a, const Reference& b) const {
807 return a.name < b.name;
808 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700809};
810
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 // Compare only names, because some References may already have their IDs
813 // assigned
814 // (framework IDs that don't change).
815 std::set<Reference, NameOnlyComparator> references;
816 references.insert(entries.begin(), entries.end());
817 references.insert(other->entries.begin(), other->entries.end());
818 entries.clear();
819 entries.reserve(references.size());
820 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700821}
822
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823} // namespace aapt