blob: 0cb8c67705f9daa85e7173581620836a155098bc [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 Lesinskicacb28f2016-10-19 12:18:14 -0700336Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 : type_mask(t),
338 min_int(std::numeric_limits<int32_t>::min()),
339 max_int(std::numeric_limits<int32_t>::max()) {
340 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341}
342
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700343template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800344constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700346}
347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348bool Attribute::Equals(const Value* value) const {
349 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 if (!other) {
351 return false;
352 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 if (symbols.size() != other->symbols.size()) {
355 return false;
356 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 if (type_mask != other->type_mask || min_int != other->min_int ||
359 max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 return false;
361 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 std::vector<const Symbol*> sorted_a;
364 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800365 add_pointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 [](const Symbol* a, const Symbol* b) -> bool {
368 return a->symbol.name < b->symbol.name;
369 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700370
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 std::vector<const Symbol*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 std::transform(other->symbols.begin(), other->symbols.end(),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800373 std::back_inserter(sorted_b), add_pointer<const Symbol>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 [](const Symbol* a, const Symbol* b) -> bool {
376 return a->symbol.name < b->symbol.name;
377 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 return a->symbol.Equals(&b->symbol) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 a->value == b->value;
383 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700384}
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388}
389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390void Attribute::PrintMask(std::ostream* out) const {
391 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 *out << "any";
393 return;
394 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 if (!set) {
399 set = true;
400 } else {
401 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 *out << "reference";
404 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 if (!set) {
408 set = true;
409 } else {
410 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 *out << "string";
413 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 if (!set) {
417 set = true;
418 } else {
419 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 *out << "integer";
422 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 if (!set) {
426 set = true;
427 } else {
428 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 *out << "boolean";
431 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (!set) {
435 set = true;
436 } else {
437 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 *out << "color";
440 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 if (!set) {
444 set = true;
445 } else {
446 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 *out << "float";
449 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 if (!set) {
453 set = true;
454 } else {
455 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 *out << "dimension";
458 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 if (!set) {
462 set = true;
463 } else {
464 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 *out << "fraction";
467 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 if (!set) {
471 set = true;
472 } else {
473 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 *out << "enum";
476 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 if (!set) {
480 set = true;
481 } else {
482 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 *out << "flags";
485 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700486}
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488void Attribute::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 *out << "(attr) ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 PrintMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (min_int != std::numeric_limits<int32_t>::min()) {
497 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 if (max_int != std::numeric_limits<int32_t>::max()) {
501 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 *out << " [weak]";
506 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507}
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509static void BuildAttributeMismatchMessage(DiagMessage* msg,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800511 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 *msg << "expected";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 *msg << " boolean";
515 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 *msg << " color";
519 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 *msg << " dimension";
523 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 *msg << " enum";
527 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 *msg << " flags";
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_FLOAT) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 *msg << " float";
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_FRACTION) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 *msg << " fraction";
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_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 *msg << " integer";
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_REFERENCE) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 *msg << " reference";
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_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 *msg << " string";
551 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800552
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800554}
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 android::Res_value val = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 item->Flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800559
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 // Always allow references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
562 if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
563 if (out_msg) {
564 BuildAttributeMismatchMessage(out_msg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800565 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 return false;
567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
571 if (out_msg) {
572 *out_msg << *item << " is less than minimum integer " << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 }
574 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
576 if (out_msg) {
577 *out_msg << *item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 }
579 return false;
580 }
581 }
582 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800583}
584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585bool Style::Equals(const Value* value) const {
586 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 if (!other) {
588 return false;
589 }
590 if (bool(parent) != bool(other->parent) ||
591 (parent && other->parent &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return false;
594 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700595
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 if (entries.size() != other->entries.size()) {
597 return false;
598 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 std::vector<const Entry*> sorted_a;
601 std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800602 add_pointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 std::sort(sorted_a.begin(), sorted_a.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 [](const Entry* a, const Entry* b) -> bool {
605 return a->key.name < b->key.name;
606 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 std::vector<const Entry*> sorted_b;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 std::transform(other->entries.begin(), other->entries.end(),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800610 std::back_inserter(sorted_b), add_pointer<const Entry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 std::sort(sorted_b.begin(), sorted_b.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 [](const Entry* a, const Entry* b) -> bool {
613 return a->key.name < b->key.name;
614 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700615
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 return a->key.Equals(&b->key) &&
619 a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700621}
622
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 Style* style = new Style();
625 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 style->parent_inferred = parent_inferred;
627 style->comment_ = comment_;
628 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 for (auto& entry : entries) {
630 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 }
633 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800634}
635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 *out << "(style) ";
638 if (parent && parent.value().name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 if (parent.value().private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800641 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 *out << parent.value().name.value();
643 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800645}
646
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647static ::std::ostream& operator<<(::std::ostream& out,
648 const Style::Entry& value) {
649 if (value.key.name) {
650 out << value.key.name.value();
651 } else if (value.key.id) {
652 out << value.key.id.value();
653 } else {
654 out << "???";
655 }
656 out << " = ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 value.value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659}
660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661bool Array::Equals(const Value* value) const {
662 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 if (!other) {
664 return false;
665 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700666
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 if (items.size() != other->items.size()) {
668 return false;
669 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700670
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 return std::equal(items.begin(), items.end(), other->items.begin(),
672 [](const std::unique_ptr<Item>& a,
673 const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700676}
677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 array->comment_ = comment_;
681 array->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 for (auto& item : items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
685 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686}
687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688void Array::Print(std::ostream* out) const {
689 *out << "(array) [" << util::Joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800690}
691
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692bool Plural::Equals(const Value* value) const {
693 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 if (!other) {
695 return false;
696 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700697
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800698 auto one_iter = values.begin();
699 auto one_end_iter = values.end();
700 auto two_iter = other->values.begin();
701 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
702 const std::unique_ptr<Item>& a = *one_iter;
703 const std::unique_ptr<Item>& b = *two_iter;
704 if (a != nullptr && b != nullptr) {
705 if (!a->Equals(b.get())) {
706 return false;
707 }
708 } else if (a != b) {
709 return false;
710 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800712 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700713}
714
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 p->comment_ = comment_;
718 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 const size_t count = values.size();
720 for (size_t i = 0; i < count; i++) {
721 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800723 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 }
725 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800726}
727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 *out << "(plural)";
730 if (values[Zero]) {
731 *out << " zero=" << *values[Zero];
732 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700733
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 if (values[One]) {
735 *out << " one=" << *values[One];
736 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700737
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 if (values[Two]) {
739 *out << " two=" << *values[Two];
740 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700741
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 if (values[Few]) {
743 *out << " few=" << *values[Few];
744 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700745
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 if (values[Many]) {
747 *out << " many=" << *values[Many];
748 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800749
750 if (values[Other]) {
751 *out << " other=" << *values[Other];
752 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800753}
754
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755static ::std::ostream& operator<<(::std::ostream& out,
756 const std::unique_ptr<Item>& item) {
757 return out << *item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800758}
759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760bool Styleable::Equals(const Value* value) const {
761 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 if (!other) {
763 return false;
764 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700765
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 if (entries.size() != other->entries.size()) {
767 return false;
768 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700769
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
771 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700774}
775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800778}
779
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800783}
784
Adam Lesinski8197cc462016-08-19 12:16:49 -0700785bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 if (cmp != 0) return cmp < 0;
788 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700789}
790
791bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700793}
794
795bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700797}
798
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700799struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 bool operator()(const Reference& a, const Reference& b) const {
801 return a.name < b.name;
802 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700803};
804
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 // Compare only names, because some References may already have their IDs
807 // assigned
808 // (framework IDs that don't change).
809 std::set<Reference, NameOnlyComparator> references;
810 references.insert(entries.begin(), entries.end());
811 references.insert(other->entries.begin(), other->entries.end());
812 entries.clear();
813 entries.reserve(references.size());
814 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700815}
816
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817} // namespace aapt