blob: f75ed7ad978a620904335d98145ecfc4d43c2f5b [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 {
91 out_value->dataType = (reference_type == Reference::Type::kResource)
92 ? android::Res_value::TYPE_REFERENCE
93 : android::Res_value::TYPE_ATTRIBUTE;
94 out_value->data = util::HostToDevice32(id ? id.value().id : 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102void Reference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 *out << "(reference) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 if (reference_type == Reference::Type::kResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 *out << "@";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 if (private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 } else {
110 *out << "?";
111 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 if (name) {
114 *out << name.value();
115 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 if (id && !Res_INTERNALID(id.value().id)) {
118 *out << " " << id.value();
119 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120}
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122bool Id::Equals(const Value* value) const {
123 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700124}
125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134void Id::Print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138bool String::Equals(const Value* value) const {
139 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 if (!other) {
141 return false;
142 }
Adam Lesinski75421622017-01-06 15:20:04 -0800143
144 if (this->value != other->value) {
145 return false;
146 }
147
148 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
149 return false;
150 }
151
152 auto other_iter = other->untranslatable_sections.begin();
153 for (const UntranslatableSection& this_section : untranslatable_sections) {
154 if (this_section != *other_iter) {
155 return false;
156 }
157 ++other_iter;
158 }
159 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 return false;
166 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 out_value->dataType = android::Res_value::TYPE_STRING;
169 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171}
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173String* String::Clone(StringPool* new_pool) const {
174 String* str = new String(new_pool->MakeRef(*value));
175 str->comment_ = comment_;
176 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800177 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179}
180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183}
184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187bool StyledString::Equals(const Value* value) const {
188 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700190 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 }
192
Adam Lesinski75421622017-01-06 15:20:04 -0800193 if (this->value != other->value) {
194 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 }
Adam Lesinski75421622017-01-06 15:20:04 -0800196
197 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
198 return false;
199 }
200
201 auto other_iter = other->untranslatable_sections.begin();
202 for (const UntranslatableSection& this_section : untranslatable_sections) {
203 if (this_section != *other_iter) {
204 return false;
205 }
206 ++other_iter;
207 }
208 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209}
210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211bool StyledString::Flatten(android::Res_value* out_value) const {
212 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 return false;
214 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 out_value->dataType = android::Res_value::TYPE_STRING;
217 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219}
220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221StyledString* StyledString::Clone(StringPool* new_pool) const {
222 StyledString* str = new StyledString(new_pool->MakeRef(value));
223 str->comment_ = comment_;
224 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800225 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227}
228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229void StyledString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 *out << "(styled string) \"" << *value->str << "\"";
231 for (const StringPool::Span& span : value->spans) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 *out << " " << *span.name << ":" << span.first_char << ","
233 << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235}
236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239bool FileReference::Equals(const Value* value) const {
240 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 if (!other) {
242 return false;
243 }
Adam Lesinski75421622017-01-06 15:20:04 -0800244 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700245}
246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247bool FileReference::Flatten(android::Res_value* out_value) const {
248 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 return false;
250 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 out_value->dataType = android::Res_value::TYPE_STRING;
253 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257FileReference* FileReference::Clone(StringPool* new_pool) const {
258 FileReference* fr = new FileReference(new_pool->MakeRef(*path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 fr->file = file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 fr->comment_ = comment_;
261 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263}
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267}
268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 value.dataType = dataType;
273 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274}
275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276bool BinaryPrimitive::Equals(const Value* value) const {
277 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 if (!other) {
279 return false;
280 }
281 return this->value.dataType == other->value.dataType &&
282 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
286 out_value->dataType = value.dataType;
287 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289}
290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 switch (value.dataType) {
297 case android::Res_value::TYPE_NULL:
298 *out << "(null)";
299 break;
300 case android::Res_value::TYPE_INT_DEC:
301 *out << "(integer) " << static_cast<int32_t>(value.data);
302 break;
303 case android::Res_value::TYPE_INT_HEX:
304 *out << "(integer) 0x" << std::hex << value.data << std::dec;
305 break;
306 case android::Res_value::TYPE_INT_BOOLEAN:
307 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
308 break;
309 case android::Res_value::TYPE_INT_COLOR_ARGB8:
310 case android::Res_value::TYPE_INT_COLOR_RGB8:
311 case android::Res_value::TYPE_INT_COLOR_ARGB4:
312 case android::Res_value::TYPE_INT_COLOR_RGB4:
313 *out << "(color) #" << std::hex << value.data << std::dec;
314 break;
315 default:
316 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
317 << std::hex << value.data << std::dec;
318 break;
319 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800320}
321
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 : type_mask(t),
324 min_int(std::numeric_limits<int32_t>::min()),
325 max_int(std::numeric_limits<int32_t>::max()) {
326 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700329template <typename T>
330T* addPointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700332}
333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334bool Attribute::Equals(const Value* value) const {
335 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 if (!other) {
337 return false;
338 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700339
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 if (symbols.size() != other->symbols.size()) {
341 return false;
342 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 if (type_mask != other->type_mask || min_int != other->min_int ||
345 max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 return false;
347 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 std::vector<const Symbol*> sorted_a;
350 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 addPointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 [](const Symbol* a, const Symbol* b) -> bool {
354 return a->symbol.name < b->symbol.name;
355 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 std::vector<const Symbol*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 std::transform(other->symbols.begin(), other->symbols.end(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 std::back_inserter(sorted_b), addPointer<const Symbol>);
360 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 [](const Symbol* a, const Symbol* b) -> bool {
362 return a->symbol.name < b->symbol.name;
363 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 return a->symbol.Equals(&b->symbol) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 a->value == b->value;
369 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700370}
371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374}
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376void Attribute::PrintMask(std::ostream* out) const {
377 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 *out << "any";
379 return;
380 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 if (!set) {
385 set = true;
386 } else {
387 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 *out << "reference";
390 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 if (!set) {
394 set = true;
395 } else {
396 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 *out << "string";
399 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 if (!set) {
403 set = true;
404 } else {
405 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 *out << "integer";
408 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 if (!set) {
412 set = true;
413 } else {
414 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800415 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 *out << "boolean";
417 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 if (!set) {
421 set = true;
422 } else {
423 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 *out << "color";
426 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 if (!set) {
430 set = true;
431 } else {
432 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 *out << "float";
435 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 if (!set) {
439 set = true;
440 } else {
441 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800442 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 *out << "dimension";
444 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 if (!set) {
448 set = true;
449 } else {
450 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800451 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 *out << "fraction";
453 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 if (!set) {
457 set = true;
458 } else {
459 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 *out << "enum";
462 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 if (!set) {
466 set = true;
467 } else {
468 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 *out << "flags";
471 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700472}
473
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 if (min_int != std::numeric_limits<int32_t>::min()) {
483 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 if (max_int != std::numeric_limits<int32_t>::max()) {
487 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 *out << " [weak]";
492 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493}
494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800497 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 *msg << " boolean";
501 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800502
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 *msg << " color";
505 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 *msg << " dimension";
509 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800510
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 *msg << " enum";
513 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 *msg << " flags";
517 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 *msg << " float";
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_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 *msg << " fraction";
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_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 *msg << " integer";
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_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 *msg << " reference";
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_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 *msg << " string";
537 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800538
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800540}
541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800545
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
548 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
549 if (out_msg) {
550 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800551 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 return false;
553
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
557 if (out_msg) {
558 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 }
560 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
562 if (out_msg) {
563 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 }
565 return false;
566 }
567 }
568 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571bool Style::Equals(const Value* value) const {
572 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 if (!other) {
574 return false;
575 }
576 if (bool(parent) != bool(other->parent) ||
577 (parent && other->parent &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return false;
580 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700581
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 if (entries.size() != other->entries.size()) {
583 return false;
584 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 std::vector<const Entry*> sorted_a;
587 std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 addPointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 [](const Entry* a, const Entry* b) -> bool {
591 return a->key.name < b->key.name;
592 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 std::vector<const Entry*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 std::transform(other->entries.begin(), other->entries.end(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 std::back_inserter(sorted_b), addPointer<const Entry>);
597 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 [](const Entry* a, const Entry* b) -> bool {
599 return a->key.name < b->key.name;
600 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700601
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 return a->key.Equals(&b->key) &&
605 a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700607}
608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 Style* style = new Style();
611 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 style->parent_inferred = parent_inferred;
613 style->comment_ = comment_;
614 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 for (auto& entry : entries) {
616 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 }
619 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800620}
621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 *out << "(style) ";
624 if (parent && parent.value().name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 if (parent.value().private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800627 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 *out << parent.value().name.value();
629 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631}
632
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633static ::std::ostream& operator<<(::std::ostream& out,
634 const Style::Entry& value) {
635 if (value.key.name) {
636 out << value.key.name.value();
637 } else if (value.key.id) {
638 out << value.key.id.value();
639 } else {
640 out << "???";
641 }
642 out << " = ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 value.value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800645}
646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647bool Array::Equals(const Value* value) const {
648 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 if (!other) {
650 return false;
651 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700652
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 if (items.size() != other->items.size()) {
654 return false;
655 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700656
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 return std::equal(items.begin(), items.end(), other->items.begin(),
658 [](const std::unique_ptr<Item>& a,
659 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 return a->Equals(b.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 -0700664Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 array->comment_ = comment_;
667 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 }
671 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800672}
673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674void Array::Print(std::ostream* out) const {
675 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800676}
677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678bool Plural::Equals(const Value* value) const {
679 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 if (!other) {
681 return false;
682 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700683
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 if (values.size() != other->values.size()) {
685 return false;
686 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700687
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 return std::equal(values.begin(), values.end(), other->values.begin(),
689 [](const std::unique_ptr<Item>& a,
690 const std::unique_ptr<Item>& b) -> bool {
691 if (bool(a) != bool(b)) {
692 return false;
693 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 return bool(a) == bool(b) || a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700696}
697
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 p->comment_ = comment_;
701 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 const size_t count = values.size();
703 for (size_t i = 0; i < count; i++) {
704 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800706 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
708 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800709}
710
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 *out << "(plural)";
713 if (values[Zero]) {
714 *out << " zero=" << *values[Zero];
715 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700716
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 if (values[One]) {
718 *out << " one=" << *values[One];
719 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700720
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 if (values[Two]) {
722 *out << " two=" << *values[Two];
723 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700724
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 if (values[Few]) {
726 *out << " few=" << *values[Few];
727 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700728
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 if (values[Many]) {
730 *out << " many=" << *values[Many];
731 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732}
733
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734static ::std::ostream& operator<<(::std::ostream& out,
735 const std::unique_ptr<Item>& item) {
736 return out << *item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737}
738
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739bool Styleable::Equals(const Value* value) const {
740 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 if (!other) {
742 return false;
743 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700744
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 if (entries.size() != other->entries.size()) {
746 return false;
747 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700748
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
750 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700753}
754
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757}
758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800762}
763
Adam Lesinski8197cc462016-08-19 12:16:49 -0700764bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 if (cmp != 0) return cmp < 0;
767 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700768}
769
770bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700772}
773
774bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700776}
777
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700778struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 bool operator()(const Reference& a, const Reference& b) const {
780 return a.name < b.name;
781 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700782};
783
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 // Compare only names, because some References may already have their IDs
786 // assigned
787 // (framework IDs that don't change).
788 std::set<Reference, NameOnlyComparator> references;
789 references.insert(entries.begin(), entries.end());
790 references.insert(other->entries.begin(), other->entries.end());
791 entries.clear();
792 entries.reserve(references.size());
793 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700794}
795
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796} // namespace aapt