blob: 60590b6026b5f013b5867b508d1bb6af598b7bb8 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080019#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070021#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070022
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <androidfw/ResourceTypes.h>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <limits>
Adam Lesinski8197cc462016-08-19 12:16:49 -070026#include <set>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
28namespace aapt {
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030template <typename Derived>
31void BaseValue<Derived>::accept(RawValueVisitor* visitor) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 visitor->visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033}
34
35template <typename Derived>
36void BaseItem<Derived>::accept(RawValueVisitor* visitor) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 visitor->visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038}
39
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinski458b8772016-04-25 14:20:21 -070042bool RawString::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 const RawString* other = valueCast<RawString>(value);
44 if (!other) {
45 return false;
46 }
47 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070048}
49
Adam Lesinski769de982015-04-10 19:43:55 -070050RawString* RawString::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 RawString* rs = new RawString(newPool->makeRef(*value));
52 rs->mComment = mComment;
53 rs->mSource = mSource;
54 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057bool RawString::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 outValue->dataType = android::Res_value::TYPE_STRING;
59 outValue->data =
60 util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
61 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062}
63
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064void RawString::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066}
67
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068Reference::Reference() : referenceType(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070Reference::Reference(const ResourceNameRef& n, Type t)
71 : name(n.toResourceName()), referenceType(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073Reference::Reference(const ResourceId& i, Type type)
74 : id(i), referenceType(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
77 : name(n.toResourceName()), id(i), referenceType(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -070078
Adam Lesinski458b8772016-04-25 14:20:21 -070079bool Reference::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 const Reference* other = valueCast<Reference>(value);
81 if (!other) {
82 return false;
83 }
84 return referenceType == other->referenceType &&
85 privateReference == other->privateReference && id == other->id &&
86 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -070087}
88
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089bool Reference::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 outValue->dataType = (referenceType == Reference::Type::kResource)
91 ? android::Res_value::TYPE_REFERENCE
92 : android::Res_value::TYPE_ATTRIBUTE;
93 outValue->data = util::hostToDevice32(id ? id.value().id : 0);
94 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095}
96
Adam Lesinski769de982015-04-10 19:43:55 -070097Reference* Reference::clone(StringPool* /*newPool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099}
100
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101void Reference::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 *out << "(reference) ";
103 if (referenceType == Reference::Type::kResource) {
104 *out << "@";
105 if (privateReference) {
106 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 } else {
109 *out << "?";
110 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 if (name) {
113 *out << name.value();
114 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 if (id && !Res_INTERNALID(id.value().id)) {
117 *out << " " << id.value();
118 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
Adam Lesinski458b8772016-04-25 14:20:21 -0700121bool Id::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 return valueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700123}
124
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125bool Id::flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
127 out->data = util::hostToDevice32(0);
128 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129}
130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131Id* Id::clone(StringPool* /*newPool*/) const { return new Id(*this); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133void Id::print(std::ostream* out) const { *out << "(id)"; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135String::String(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800136
Adam Lesinski458b8772016-04-25 14:20:21 -0700137bool String::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 const String* other = valueCast<String>(value);
139 if (!other) {
140 return false;
141 }
142 return *this->value == *other->value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143}
144
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145bool String::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 // Verify that our StringPool index is within encode-able limits.
147 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
148 return false;
149 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 outValue->dataType = android::Res_value::TYPE_STRING;
152 outValue->data =
153 util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
154 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155}
156
Adam Lesinski769de982015-04-10 19:43:55 -0700157String* String::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 String* str = new String(newPool->makeRef(*value));
159 str->mComment = mComment;
160 str->mSource = mSource;
161 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162}
163
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164void String::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166}
167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800169
Adam Lesinski458b8772016-04-25 14:20:21 -0700170bool StyledString::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 const StyledString* other = valueCast<StyledString>(value);
172 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700173 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175
176 if (*this->value->str == *other->value->str) {
177 const std::vector<StringPool::Span>& spansA = this->value->spans;
178 const std::vector<StringPool::Span>& spansB = other->value->spans;
179 return std::equal(
180 spansA.begin(), spansA.end(), spansB.begin(),
181 [](const StringPool::Span& a, const StringPool::Span& b) -> bool {
182 return *a.name == *b.name && a.firstChar == b.firstChar &&
183 a.lastChar == b.lastChar;
184 });
185 }
186 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187}
188
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189bool StyledString::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
191 return false;
192 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 outValue->dataType = android::Res_value::TYPE_STRING;
195 outValue->data =
196 util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
197 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198}
199
Adam Lesinski769de982015-04-10 19:43:55 -0700200StyledString* StyledString::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 StyledString* str = new StyledString(newPool->makeRef(value));
202 str->mComment = mComment;
203 str->mSource = mSource;
204 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205}
206
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207void StyledString::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 *out << "(styled string) \"" << *value->str << "\"";
209 for (const StringPool::Span& span : value->spans) {
210 *out << " " << *span.name << ":" << span.firstChar << "," << span.lastChar;
211 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212}
213
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinski458b8772016-04-25 14:20:21 -0700216bool FileReference::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 const FileReference* other = valueCast<FileReference>(value);
218 if (!other) {
219 return false;
220 }
221 return *path == *other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700222}
223
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224bool FileReference::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 if (path.getIndex() > std::numeric_limits<uint32_t>::max()) {
226 return false;
227 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 outValue->dataType = android::Res_value::TYPE_STRING;
230 outValue->data = util::hostToDevice32(static_cast<uint32_t>(path.getIndex()));
231 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232}
233
Adam Lesinski769de982015-04-10 19:43:55 -0700234FileReference* FileReference::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 FileReference* fr = new FileReference(newPool->makeRef(*path));
236 fr->file = file;
237 fr->mComment = mComment;
238 fr->mSource = mSource;
239 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240}
241
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242void FileReference::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244}
245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 value.dataType = dataType;
250 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251}
252
Adam Lesinski458b8772016-04-25 14:20:21 -0700253bool BinaryPrimitive::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 const BinaryPrimitive* other = valueCast<BinaryPrimitive>(value);
255 if (!other) {
256 return false;
257 }
258 return this->value.dataType == other->value.dataType &&
259 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700260}
261
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262bool BinaryPrimitive::flatten(android::Res_value* outValue) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 outValue->dataType = value.dataType;
264 outValue->data = util::hostToDevice32(value.data);
265 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266}
267
Adam Lesinski769de982015-04-10 19:43:55 -0700268BinaryPrimitive* BinaryPrimitive::clone(StringPool* /*newPool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270}
271
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272void BinaryPrimitive::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 switch (value.dataType) {
274 case android::Res_value::TYPE_NULL:
275 *out << "(null)";
276 break;
277 case android::Res_value::TYPE_INT_DEC:
278 *out << "(integer) " << static_cast<int32_t>(value.data);
279 break;
280 case android::Res_value::TYPE_INT_HEX:
281 *out << "(integer) 0x" << std::hex << value.data << std::dec;
282 break;
283 case android::Res_value::TYPE_INT_BOOLEAN:
284 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
285 break;
286 case android::Res_value::TYPE_INT_COLOR_ARGB8:
287 case android::Res_value::TYPE_INT_COLOR_RGB8:
288 case android::Res_value::TYPE_INT_COLOR_ARGB4:
289 case android::Res_value::TYPE_INT_COLOR_RGB4:
290 *out << "(color) #" << std::hex << value.data << std::dec;
291 break;
292 default:
293 *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
294 << std::hex << value.data << std::dec;
295 break;
296 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299Attribute::Attribute(bool w, uint32_t t)
300 : typeMask(t),
301 minInt(std::numeric_limits<int32_t>::min()),
302 maxInt(std::numeric_limits<int32_t>::max()) {
303 mWeak = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304}
305
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700306template <typename T>
307T* addPointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700309}
310
Adam Lesinski458b8772016-04-25 14:20:21 -0700311bool Attribute::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 const Attribute* other = valueCast<Attribute>(value);
313 if (!other) {
314 return false;
315 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 if (symbols.size() != other->symbols.size()) {
318 return false;
319 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700320
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 if (typeMask != other->typeMask || minInt != other->minInt ||
322 maxInt != other->maxInt) {
323 return false;
324 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700325
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 std::vector<const Symbol*> sortedA;
327 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sortedA),
328 addPointer<const Symbol>);
329 std::sort(sortedA.begin(), sortedA.end(),
330 [](const Symbol* a, const Symbol* b) -> bool {
331 return a->symbol.name < b->symbol.name;
332 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700333
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 std::vector<const Symbol*> sortedB;
335 std::transform(other->symbols.begin(), other->symbols.end(),
336 std::back_inserter(sortedB), addPointer<const Symbol>);
337 std::sort(sortedB.begin(), sortedB.end(),
338 [](const Symbol* a, const Symbol* b) -> bool {
339 return a->symbol.name < b->symbol.name;
340 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700341
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 return std::equal(sortedA.begin(), sortedA.end(), sortedB.begin(),
343 [](const Symbol* a, const Symbol* b) -> bool {
344 return a->symbol.equals(&b->symbol) &&
345 a->value == b->value;
346 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700347}
348
Adam Lesinski769de982015-04-10 19:43:55 -0700349Attribute* Attribute::clone(StringPool* /*newPool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351}
352
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700353void Attribute::printMask(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 if (typeMask == android::ResTable_map::TYPE_ANY) {
355 *out << "any";
356 return;
357 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 bool set = false;
360 if ((typeMask & android::ResTable_map::TYPE_REFERENCE) != 0) {
361 if (!set) {
362 set = true;
363 } else {
364 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 *out << "reference";
367 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 if ((typeMask & android::ResTable_map::TYPE_STRING) != 0) {
370 if (!set) {
371 set = true;
372 } else {
373 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 *out << "string";
376 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800377
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 if ((typeMask & android::ResTable_map::TYPE_INTEGER) != 0) {
379 if (!set) {
380 set = true;
381 } else {
382 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 *out << "integer";
385 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 if ((typeMask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
388 if (!set) {
389 set = true;
390 } else {
391 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 *out << "boolean";
394 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 if ((typeMask & android::ResTable_map::TYPE_COLOR) != 0) {
397 if (!set) {
398 set = true;
399 } else {
400 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 *out << "color";
403 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 if ((typeMask & android::ResTable_map::TYPE_FLOAT) != 0) {
406 if (!set) {
407 set = true;
408 } else {
409 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 *out << "float";
412 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 if ((typeMask & android::ResTable_map::TYPE_DIMENSION) != 0) {
415 if (!set) {
416 set = true;
417 } else {
418 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 *out << "dimension";
421 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 if ((typeMask & android::ResTable_map::TYPE_FRACTION) != 0) {
424 if (!set) {
425 set = true;
426 } else {
427 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 *out << "fraction";
430 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 if ((typeMask & android::ResTable_map::TYPE_ENUM) != 0) {
433 if (!set) {
434 set = true;
435 } else {
436 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 *out << "enum";
439 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if ((typeMask & android::ResTable_map::TYPE_FLAGS) != 0) {
442 if (!set) {
443 set = true;
444 } else {
445 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 *out << "flags";
448 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700449}
450
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451void Attribute::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 *out << "(attr) ";
453 printMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 if (!symbols.empty()) {
456 *out << " [" << util::joiner(symbols, ", ") << "]";
457 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 if (minInt != std::numeric_limits<int32_t>::min()) {
460 *out << " min=" << minInt;
461 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700462
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 if (maxInt != std::numeric_limits<int32_t>::max()) {
464 *out << " max=" << maxInt;
465 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700466
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 if (isWeak()) {
468 *out << " [weak]";
469 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470}
471
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472static void buildAttributeMismatchMessage(DiagMessage* msg,
473 const Attribute* attr,
Adam Lesinskia5870652015-11-20 15:32:30 -0800474 const Item* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 *msg << "expected";
476 if (attr->typeMask & android::ResTable_map::TYPE_BOOLEAN) {
477 *msg << " boolean";
478 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800479
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 if (attr->typeMask & android::ResTable_map::TYPE_COLOR) {
481 *msg << " color";
482 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800483
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 if (attr->typeMask & android::ResTable_map::TYPE_DIMENSION) {
485 *msg << " dimension";
486 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800487
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 if (attr->typeMask & android::ResTable_map::TYPE_ENUM) {
489 *msg << " enum";
490 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800491
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 if (attr->typeMask & android::ResTable_map::TYPE_FLAGS) {
493 *msg << " flags";
494 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800495
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 if (attr->typeMask & android::ResTable_map::TYPE_FLOAT) {
497 *msg << " float";
498 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800499
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 if (attr->typeMask & android::ResTable_map::TYPE_FRACTION) {
501 *msg << " fraction";
502 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 if (attr->typeMask & android::ResTable_map::TYPE_INTEGER) {
505 *msg << " integer";
506 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800507
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 if (attr->typeMask & android::ResTable_map::TYPE_REFERENCE) {
509 *msg << " reference";
510 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800511
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 if (attr->typeMask & android::ResTable_map::TYPE_STRING) {
513 *msg << " string";
514 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 *msg << " but got " << *value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800517}
518
519bool Attribute::matches(const Item* item, DiagMessage* outMsg) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 android::Res_value val = {};
521 item->flatten(&val);
Adam Lesinskia5870652015-11-20 15:32:30 -0800522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 // Always allow references.
524 const uint32_t mask = typeMask | android::ResTable_map::TYPE_REFERENCE;
525 if (!(mask & ResourceUtils::androidTypeToAttributeTypeMask(val.dataType))) {
526 if (outMsg) {
527 buildAttributeMismatchMessage(outMsg, this, item);
Adam Lesinskia5870652015-11-20 15:32:30 -0800528 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 return false;
530
531 } else if (ResourceUtils::androidTypeToAttributeTypeMask(val.dataType) &
532 android::ResTable_map::TYPE_INTEGER) {
533 if (static_cast<int32_t>(util::deviceToHost32(val.data)) < minInt) {
534 if (outMsg) {
535 *outMsg << *item << " is less than minimum integer " << minInt;
536 }
537 return false;
538 } else if (static_cast<int32_t>(util::deviceToHost32(val.data)) > maxInt) {
539 if (outMsg) {
540 *outMsg << *item << " is greater than maximum integer " << maxInt;
541 }
542 return false;
543 }
544 }
545 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800546}
547
Adam Lesinski458b8772016-04-25 14:20:21 -0700548bool Style::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 const Style* other = valueCast<Style>(value);
550 if (!other) {
551 return false;
552 }
553 if (bool(parent) != bool(other->parent) ||
554 (parent && other->parent &&
555 !parent.value().equals(&other->parent.value()))) {
556 return false;
557 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700558
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 if (entries.size() != other->entries.size()) {
560 return false;
561 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700562
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 std::vector<const Entry*> sortedA;
564 std::transform(entries.begin(), entries.end(), std::back_inserter(sortedA),
565 addPointer<const Entry>);
566 std::sort(sortedA.begin(), sortedA.end(),
567 [](const Entry* a, const Entry* b) -> bool {
568 return a->key.name < b->key.name;
569 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700570
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 std::vector<const Entry*> sortedB;
572 std::transform(other->entries.begin(), other->entries.end(),
573 std::back_inserter(sortedB), addPointer<const Entry>);
574 std::sort(sortedB.begin(), sortedB.end(),
575 [](const Entry* a, const Entry* b) -> bool {
576 return a->key.name < b->key.name;
577 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700578
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return std::equal(sortedA.begin(), sortedA.end(), sortedB.begin(),
580 [](const Entry* a, const Entry* b) -> bool {
581 return a->key.equals(&b->key) &&
582 a->value->equals(b->value.get());
583 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700584}
585
Adam Lesinski769de982015-04-10 19:43:55 -0700586Style* Style::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 Style* style = new Style();
588 style->parent = parent;
589 style->parentInferred = parentInferred;
590 style->mComment = mComment;
591 style->mSource = mSource;
592 for (auto& entry : entries) {
593 style->entries.push_back(
594 Entry{entry.key, std::unique_ptr<Item>(entry.value->clone(newPool))});
595 }
596 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597}
598
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599void Style::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 *out << "(style) ";
601 if (parent && parent.value().name) {
602 if (parent.value().privateReference) {
603 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 *out << parent.value().name.value();
606 }
607 *out << " [" << util::joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608}
609
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610static ::std::ostream& operator<<(::std::ostream& out,
611 const Style::Entry& value) {
612 if (value.key.name) {
613 out << value.key.name.value();
614 } else if (value.key.id) {
615 out << value.key.id.value();
616 } else {
617 out << "???";
618 }
619 out << " = ";
620 value.value->print(&out);
621 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800622}
623
Adam Lesinski458b8772016-04-25 14:20:21 -0700624bool Array::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 const Array* other = valueCast<Array>(value);
626 if (!other) {
627 return false;
628 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700629
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 if (items.size() != other->items.size()) {
631 return false;
632 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700633
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 return std::equal(items.begin(), items.end(), other->items.begin(),
635 [](const std::unique_ptr<Item>& a,
636 const std::unique_ptr<Item>& b) -> bool {
637 return a->equals(b.get());
638 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700639}
640
Adam Lesinski769de982015-04-10 19:43:55 -0700641Array* Array::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 Array* array = new Array();
643 array->mComment = mComment;
644 array->mSource = mSource;
645 for (auto& item : items) {
646 array->items.emplace_back(std::unique_ptr<Item>(item->clone(newPool)));
647 }
648 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800649}
650
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651void Array::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 *out << "(array) [" << util::joiner(items, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800653}
654
Adam Lesinski458b8772016-04-25 14:20:21 -0700655bool Plural::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 const Plural* other = valueCast<Plural>(value);
657 if (!other) {
658 return false;
659 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700660
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 if (values.size() != other->values.size()) {
662 return false;
663 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700664
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 return std::equal(values.begin(), values.end(), other->values.begin(),
666 [](const std::unique_ptr<Item>& a,
667 const std::unique_ptr<Item>& b) -> bool {
668 if (bool(a) != bool(b)) {
669 return false;
670 }
671 return bool(a) == bool(b) || a->equals(b.get());
672 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700673}
674
Adam Lesinski769de982015-04-10 19:43:55 -0700675Plural* Plural::clone(StringPool* newPool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 Plural* p = new Plural();
677 p->mComment = mComment;
678 p->mSource = mSource;
679 const size_t count = values.size();
680 for (size_t i = 0; i < count; i++) {
681 if (values[i]) {
682 p->values[i] = std::unique_ptr<Item>(values[i]->clone(newPool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800683 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
685 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686}
687
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700688void Plural::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 *out << "(plural)";
690 if (values[Zero]) {
691 *out << " zero=" << *values[Zero];
692 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700693
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 if (values[One]) {
695 *out << " one=" << *values[One];
696 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700697
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 if (values[Two]) {
699 *out << " two=" << *values[Two];
700 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700701
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 if (values[Few]) {
703 *out << " few=" << *values[Few];
704 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700705
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 if (values[Many]) {
707 *out << " many=" << *values[Many];
708 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800709}
710
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711static ::std::ostream& operator<<(::std::ostream& out,
712 const std::unique_ptr<Item>& item) {
713 return out << *item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800714}
715
Adam Lesinski458b8772016-04-25 14:20:21 -0700716bool Styleable::equals(const Value* value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 const Styleable* other = valueCast<Styleable>(value);
718 if (!other) {
719 return false;
720 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700721
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 if (entries.size() != other->entries.size()) {
723 return false;
724 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
727 [](const Reference& a, const Reference& b) -> bool {
728 return a.equals(&b);
729 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700730}
731
Adam Lesinski769de982015-04-10 19:43:55 -0700732Styleable* Styleable::clone(StringPool* /*newPool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800734}
735
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700736void Styleable::print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 *out << "(styleable) "
738 << " [" << util::joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800739}
740
Adam Lesinski8197cc462016-08-19 12:16:49 -0700741bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 int cmp = a.name.valueOrDefault({}).compare(b.name.valueOrDefault({}));
743 if (cmp != 0) return cmp < 0;
744 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700745}
746
747bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700749}
750
751bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700753}
754
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700755struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 bool operator()(const Reference& a, const Reference& b) const {
757 return a.name < b.name;
758 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700759};
760
Adam Lesinski8197cc462016-08-19 12:16:49 -0700761void Styleable::mergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 // Compare only names, because some References may already have their IDs
763 // assigned
764 // (framework IDs that don't change).
765 std::set<Reference, NameOnlyComparator> references;
766 references.insert(entries.begin(), entries.end());
767 references.insert(other->entries.begin(), other->entries.end());
768 entries.clear();
769 entries.reserve(references.size());
770 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -0700771}
772
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773} // namespace aapt