blob: a782cd3672d1943555282e784fcc6d41abd6b96e [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>
Adam Lesinski93190b72017-11-03 15:20:17 -070020#include <cinttypes>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <limits>
22#include <set>
Adam Lesinski93190b72017-11-03 15:20:17 -070023#include <sstream>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski93190b72017-11-03 15:20:17 -070025#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080029#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070031#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070032
Adam Lesinski93190b72017-11-03 15:20:17 -070033using ::aapt::text::Printer;
34using ::android::StringPiece;
35using ::android::base::StringPrintf;
36
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037namespace aapt {
38
Adam Lesinski93190b72017-11-03 15:20:17 -070039void Value::PrettyPrint(Printer* printer) const {
40 std::ostringstream str_stream;
41 Print(&str_stream);
42 printer->Print(str_stream.str());
43}
44
Adam Lesinski5924d8c2017-05-30 15:15:58 -070045std::ostream& operator<<(std::ostream& out, const Value& value) {
46 value.Print(&out);
47 return out;
48}
49
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070051void BaseValue<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053}
54
55template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070056void BaseValue<Derived>::Accept(ConstValueVisitor* visitor) const {
57 visitor->Visit(static_cast<const Derived*>(this));
58}
59
60template <typename Derived>
61void BaseItem<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063}
64
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070065template <typename Derived>
66void BaseItem<Derived>::Accept(ConstValueVisitor* visitor) const {
67 visitor->Visit(static_cast<const Derived*>(this));
68}
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool RawString::Equals(const Value* value) const {
73 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 if (!other) {
75 return false;
76 }
77 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070078}
79
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080RawString* RawString::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -070081 RawString* rs = new RawString(new_pool->MakeRef(value));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 rs->comment_ = comment_;
83 rs->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087bool RawString::Flatten(android::Res_value* out_value) const {
88 out_value->dataType = android::Res_value::TYPE_STRING;
89 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091}
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108bool Reference::Equals(const Value* value) const {
109 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 if (!other) {
111 return false;
112 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 return reference_type == other->reference_type &&
114 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -0700116}
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118bool Reference::Flatten(android::Res_value* out_value) const {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800119 const ResourceId resid = id.value_or_default(ResourceId(0));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700120 const bool dynamic = resid.is_valid_dynamic() && resid.package_id() != kFrameworkPackageId &&
Adam Lesinski490595a2017-11-07 17:08:07 -0800121 resid.package_id() < kAppPackageId;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800122
123 if (reference_type == Reference::Type::kResource) {
124 if (dynamic) {
125 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
126 } else {
127 out_value->dataType = android::Res_value::TYPE_REFERENCE;
128 }
129 } else {
130 if (dynamic) {
131 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
132 } else {
133 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
134 }
135 }
136 out_value->data = util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138}
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144void Reference::Print(std::ostream* out) const {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700145 if (reference_type == Type::kResource) {
146 *out << "(reference) @";
147 if (!name && !id) {
148 *out << "null";
149 return;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 } else {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700152 *out << "(attr-reference) ?";
153 }
154
155 if (private_reference) {
156 *out << "*";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (name) {
160 *out << name.value();
161 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700163 if (id && id.value().is_valid_dynamic()) {
164 if (name) {
165 *out << " ";
166 }
167 *out << id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169}
170
Adam Lesinski93190b72017-11-03 15:20:17 -0700171static void PrettyPrintReferenceImpl(const Reference& ref, bool print_package, Printer* printer) {
172 switch (ref.reference_type) {
173 case Reference::Type::kResource:
174 printer->Print("@");
175 break;
176
177 case Reference::Type::kAttribute:
178 printer->Print("?");
179 break;
180 }
181
182 if (!ref.name && !ref.id) {
183 printer->Print("null");
184 return;
185 }
186
187 if (ref.private_reference) {
188 printer->Print("*");
189 }
190
191 if (ref.name) {
192 const ResourceName& name = ref.name.value();
193 if (print_package) {
194 printer->Print(name.to_string());
195 } else {
196 printer->Print(to_string(name.type));
197 printer->Print("/");
198 printer->Print(name.entry);
199 }
200 } else if (ref.id && ref.id.value().is_valid_dynamic()) {
201 printer->Print(ref.id.value().to_string());
202 }
203}
204
205void Reference::PrettyPrint(Printer* printer) const {
206 PrettyPrintReferenceImpl(*this, true /*print_package*/, printer);
207}
208
209void Reference::PrettyPrint(const StringPiece& package, Printer* printer) const {
210 const bool print_package = name ? package != name.value().package : true;
211 PrettyPrintReferenceImpl(*this, print_package, printer);
212}
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214bool Id::Equals(const Value* value) const {
215 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700216}
217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222}
223
Adam Lesinski93190b72017-11-03 15:20:17 -0700224Id* Id::Clone(StringPool* /*new_pool*/) const {
225 return new Id(*this);
226}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinski93190b72017-11-03 15:20:17 -0700228void Id::Print(std::ostream* out) const {
229 *out << "(id)";
230}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Adam Lesinski93190b72017-11-03 15:20:17 -0700232String::String(const StringPool::Ref& ref) : value(ref) {
233}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235bool String::Equals(const Value* value) const {
236 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 if (!other) {
238 return false;
239 }
Adam Lesinski75421622017-01-06 15:20:04 -0800240
241 if (this->value != other->value) {
242 return false;
243 }
244
245 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
246 return false;
247 }
248
249 auto other_iter = other->untranslatable_sections.begin();
250 for (const UntranslatableSection& this_section : untranslatable_sections) {
251 if (this_section != *other_iter) {
252 return false;
253 }
254 ++other_iter;
255 }
256 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257}
258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return false;
263 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 out_value->dataType = android::Res_value::TYPE_STRING;
266 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268}
269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270String* String::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700271 String* str = new String(new_pool->MakeRef(value));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 str->comment_ = comment_;
273 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800274 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276}
277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280}
281
Adam Lesinski93190b72017-11-03 15:20:17 -0700282void String::PrettyPrint(Printer* printer) const {
283 printer->Print("\"");
284 printer->Print(*value);
285 printer->Print("\"");
286}
287
288StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
289}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291bool StyledString::Equals(const Value* value) const {
292 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700294 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 }
296
Adam Lesinski75421622017-01-06 15:20:04 -0800297 if (this->value != other->value) {
298 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 }
Adam Lesinski75421622017-01-06 15:20:04 -0800300
301 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
302 return false;
303 }
304
305 auto other_iter = other->untranslatable_sections.begin();
306 for (const UntranslatableSection& this_section : untranslatable_sections) {
307 if (this_section != *other_iter) {
308 return false;
309 }
310 ++other_iter;
311 }
312 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313}
314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315bool StyledString::Flatten(android::Res_value* out_value) const {
316 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 return false;
318 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 out_value->dataType = android::Res_value::TYPE_STRING;
321 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323}
324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325StyledString* StyledString::Clone(StringPool* new_pool) const {
326 StyledString* str = new StyledString(new_pool->MakeRef(value));
327 str->comment_ = comment_;
328 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800329 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331}
332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333void StyledString::Print(std::ostream* out) const {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700334 *out << "(styled string) \"" << value->value << "\"";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 for (const StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700336 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338}
339
Adam Lesinski93190b72017-11-03 15:20:17 -0700340FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
341}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343bool FileReference::Equals(const Value* value) const {
344 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 if (!other) {
346 return false;
347 }
Adam Lesinski75421622017-01-06 15:20:04 -0800348 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700349}
350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351bool FileReference::Flatten(android::Res_value* out_value) const {
352 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return false;
354 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 out_value->dataType = android::Res_value::TYPE_STRING;
357 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359}
360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361FileReference* FileReference::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700362 FileReference* fr = new FileReference(new_pool->MakeRef(path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 fr->file = file;
Adam Lesinski00451162017-10-03 07:44:08 -0700364 fr->type = type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 fr->comment_ = comment_;
366 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368}
369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372}
373
Adam Lesinski93190b72017-11-03 15:20:17 -0700374BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
375}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700377BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 value.dataType = dataType;
379 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700380}
381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382bool BinaryPrimitive::Equals(const Value* value) const {
383 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 if (!other) {
385 return false;
386 }
387 return this->value.dataType == other->value.dataType &&
388 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700389}
390
Adam Lesinski93190b72017-11-03 15:20:17 -0700391bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 out_value->dataType = value.dataType;
393 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395}
396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800399}
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700402 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
403}
404
405static std::string ComplexToString(uint32_t complex_value, bool fraction) {
406 using ::android::Res_value;
407
408 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
409
410 // Determine the radix that was used.
411 const uint32_t radix =
412 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
413 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
414 Res_value::COMPLEX_MANTISSA_MASK}
415 << kRadixShifts[radix];
416 const float value = mantissa * (1.0f / (1 << 23));
417
418 std::string str = StringPrintf("%f", value);
419
420 const int unit_type =
421 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
422 if (fraction) {
423 switch (unit_type) {
424 case Res_value::COMPLEX_UNIT_FRACTION:
425 str += "%";
426 break;
427 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
428 str += "%p";
429 break;
430 default:
431 str += "???";
432 break;
433 }
434 } else {
435 switch (unit_type) {
436 case Res_value::COMPLEX_UNIT_PX:
437 str += "px";
438 break;
439 case Res_value::COMPLEX_UNIT_DIP:
440 str += "dp";
441 break;
442 case Res_value::COMPLEX_UNIT_SP:
443 str += "sp";
444 break;
445 case Res_value::COMPLEX_UNIT_PT:
446 str += "pt";
447 break;
448 case Res_value::COMPLEX_UNIT_IN:
449 str += "in";
450 break;
451 case Res_value::COMPLEX_UNIT_MM:
452 str += "mm";
453 break;
454 default:
455 str += "???";
456 break;
457 }
458 }
459 return str;
460}
461
462void BinaryPrimitive::PrettyPrint(Printer* printer) const {
463 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700465 case Res_value::TYPE_NULL:
466 if (value.data == Res_value::DATA_NULL_EMPTY) {
467 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700468 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700469 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700470 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700472
473 case Res_value::TYPE_INT_DEC:
474 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700476
477 case Res_value::TYPE_INT_HEX:
478 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700480
481 case Res_value::TYPE_INT_BOOLEAN:
482 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700484
485 case Res_value::TYPE_INT_COLOR_ARGB8:
486 case Res_value::TYPE_INT_COLOR_RGB8:
487 case Res_value::TYPE_INT_COLOR_ARGB4:
488 case Res_value::TYPE_INT_COLOR_RGB4:
489 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700491
492 case Res_value::TYPE_FLOAT:
493 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
494 break;
495
496 case Res_value::TYPE_DIMENSION:
497 printer->Print(ComplexToString(value.data, false /*fraction*/));
498 break;
499
500 case Res_value::TYPE_FRACTION:
501 printer->Print(ComplexToString(value.data, true /*fraction*/));
502 break;
503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700505 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 break;
507 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508}
509
Adam Lesinskic744ae82017-05-17 19:28:38 -0700510Attribute::Attribute()
511 : type_mask(0u),
512 min_int(std::numeric_limits<int32_t>::min()),
513 max_int(std::numeric_limits<int32_t>::max()) {
514}
515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516Attribute::Attribute(bool w, uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 : type_mask(t),
518 min_int(std::numeric_limits<int32_t>::min()),
519 max_int(std::numeric_limits<int32_t>::max()) {
520 weak_ = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521}
522
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700523std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
524 if (s.symbol.name) {
525 out << s.symbol.name.value().entry;
526 } else {
527 out << "???";
528 }
529 return out << "=" << s.value;
530}
531
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700532template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800533constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700535}
536
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537bool Attribute::Equals(const Value* value) const {
538 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 if (!other) {
540 return false;
541 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700542
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 if (symbols.size() != other->symbols.size()) {
544 return false;
545 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700546
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700547 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 return false;
549 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 std::vector<const Symbol*> sorted_a;
552 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800553 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700554 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
555 return a->symbol.name < b->symbol.name;
556 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700559 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
560 add_pointer<const Symbol>);
561 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
562 return a->symbol.name < b->symbol.name;
563 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700567 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573}
574
Adam Lesinski93190b72017-11-03 15:20:17 -0700575std::string Attribute::MaskString() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700577 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800579
Adam Lesinski93190b72017-11-03 15:20:17 -0700580 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 if (!set) {
584 set = true;
585 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700586 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700588 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 if (!set) {
593 set = true;
594 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700595 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800596 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700597 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 if (!set) {
602 set = true;
603 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700604 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800605 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700606 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 if (!set) {
611 set = true;
612 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700613 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800614 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700615 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 if (!set) {
620 set = true;
621 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700622 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800623 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700624 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 if (!set) {
629 set = true;
630 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700631 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800632 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700633 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 if (!set) {
638 set = true;
639 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700640 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800641 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700642 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 if (!set) {
647 set = true;
648 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700649 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800650 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700651 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800653
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 if (!set) {
656 set = true;
657 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700658 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700660 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 if (!set) {
665 set = true;
666 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700667 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800668 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700669 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700671 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700672}
673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700675 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800676
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 if (min_int != std::numeric_limits<int32_t>::min()) {
682 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 if (max_int != std::numeric_limits<int32_t>::max()) {
686 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 *out << " [weak]";
691 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800692}
693
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700694static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
695 DiagMessage* out_msg) {
696 *out_msg << "expected";
697 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
698 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800700
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700701 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
702 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800704
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700705 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
706 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800708
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700709 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
710 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800712
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700713 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
714 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800716
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700717 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
718 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800720
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700721 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
722 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800724
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700725 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
726 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800728
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700729 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
730 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800732
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700733 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
734 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800736
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700737 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800738}
739
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700740bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
741 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
742 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
743 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
744 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
745
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700747 item.Flatten(&val);
748
749 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800750
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700752 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
753
754 // Only one type must match between the actual and expected.
755 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700757 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800758 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700760 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700762 // Enums and flags are encoded as integers, so check them first before doing any range checks.
763 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
764 for (const Symbol& s : symbols) {
765 if (flattened_data == s.value) {
766 return true;
767 }
768 }
769
770 // If the attribute accepts integers, we can't fail here.
771 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700773 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 }
775 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700776 }
777 }
778
779 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
780 uint32_t mask = 0u;
781 for (const Symbol& s : symbols) {
782 mask |= s.value;
783 }
784
785 // Check if the flattened data is covered by the flag bit mask.
786 // If the attribute accepts integers, we can't fail here.
787 if ((mask & flattened_data) == flattened_data) {
788 return true;
789 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700791 *out_msg << item << " is not a valid flag";
792 }
793 return false;
794 }
795 }
796
797 // Finally check the integer range of the value.
798 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
799 if (static_cast<int32_t>(flattened_data) < min_int) {
800 if (out_msg) {
801 *out_msg << item << " is less than minimum integer " << min_int;
802 }
803 return false;
804 } else if (static_cast<int32_t>(flattened_data) > max_int) {
805 if (out_msg) {
806 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 }
808 return false;
809 }
810 }
811 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800812}
813
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700814std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
815 if (entry.key.name) {
816 out << entry.key.name.value();
817 } else if (entry.key.id) {
818 out << entry.key.id.value();
819 } else {
820 out << "???";
821 }
822 out << " = " << entry.value;
823 return out;
824}
825
826template <typename T>
827std::vector<T*> ToPointerVec(std::vector<T>& src) {
828 std::vector<T*> dst;
829 dst.reserve(src.size());
830 for (T& in : src) {
831 dst.push_back(&in);
832 }
833 return dst;
834}
835
836template <typename T>
837std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
838 std::vector<const T*> dst;
839 dst.reserve(src.size());
840 for (const T& in : src) {
841 dst.push_back(&in);
842 }
843 return dst;
844}
845
846static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
847 return a->key.name < b->key.name;
848}
849
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850bool Style::Equals(const Value* value) const {
851 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 if (!other) {
853 return false;
854 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700855
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700857 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 return false;
859 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700860
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 if (entries.size() != other->entries.size()) {
862 return false;
863 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700864
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700865 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
866 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700867
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700868 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
869 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700873 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700875}
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 Style* style = new Style();
879 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700880 style->parent_inferred = parent_inferred;
881 style->comment_ = comment_;
882 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700884 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 }
886 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800887}
888
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 *out << "(style) ";
891 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700892 const Reference& parent_ref = parent.value();
893 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800895 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700896 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800899}
900
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700901Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
902 Style::Entry cloned_entry{entry.key};
903 if (entry.value != nullptr) {
904 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700906 return cloned_entry;
907}
908
909void Style::MergeWith(Style* other, StringPool* pool) {
910 if (other->parent) {
911 parent = other->parent;
912 }
913
914 // We can't assume that the entries are sorted alphabetically since they're supposed to be
915 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
916 // them keying off that.
917 //
918 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
919 // those.
920
921 std::vector<Entry*> this_sorted = ToPointerVec(entries);
922 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
923
924 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
925 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
926
927 auto this_iter = this_sorted.begin();
928 const auto this_end = this_sorted.end();
929
930 auto other_iter = other_sorted.begin();
931 const auto other_end = other_sorted.end();
932
933 std::vector<Entry> merged_entries;
934 while (this_iter != this_end) {
935 if (other_iter != other_end) {
936 if ((*this_iter)->key.name < (*other_iter)->key.name) {
937 merged_entries.push_back(std::move(**this_iter));
938 ++this_iter;
939 } else {
940 // The other overrides.
941 merged_entries.push_back(CloneEntry(**other_iter, pool));
942 if ((*this_iter)->key.name == (*other_iter)->key.name) {
943 ++this_iter;
944 }
945 ++other_iter;
946 }
947 } else {
948 merged_entries.push_back(std::move(**this_iter));
949 ++this_iter;
950 }
951 }
952
953 while (other_iter != other_end) {
954 merged_entries.push_back(CloneEntry(**other_iter, pool));
955 ++other_iter;
956 }
957
958 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800959}
960
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961bool Array::Equals(const Value* value) const {
962 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963 if (!other) {
964 return false;
965 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700966
Adam Lesinski4ffea042017-08-10 15:37:28 -0700967 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 return false;
969 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700970
Adam Lesinski4ffea042017-08-10 15:37:28 -0700971 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
972 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700975}
976
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 array->comment_ = comment_;
980 array->source_ = source_;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700981 for (auto& item : elements) {
982 array->elements.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 }
984 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800985}
986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700988 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800989}
990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991bool Plural::Equals(const Value* value) const {
992 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 if (!other) {
994 return false;
995 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700996
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800997 auto one_iter = values.begin();
998 auto one_end_iter = values.end();
999 auto two_iter = other->values.begin();
1000 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
1001 const std::unique_ptr<Item>& a = *one_iter;
1002 const std::unique_ptr<Item>& b = *two_iter;
1003 if (a != nullptr && b != nullptr) {
1004 if (!a->Equals(b.get())) {
1005 return false;
1006 }
1007 } else if (a != b) {
1008 return false;
1009 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001011 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -07001012}
1013
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 p->comment_ = comment_;
1017 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 const size_t count = values.size();
1019 for (size_t i = 0; i < count; i++) {
1020 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001022 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 }
1024 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001025}
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001028 *out << "(plural)";
1029 if (values[Zero]) {
1030 *out << " zero=" << *values[Zero];
1031 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001032
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 if (values[One]) {
1034 *out << " one=" << *values[One];
1035 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001036
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 if (values[Two]) {
1038 *out << " two=" << *values[Two];
1039 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001040
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 if (values[Few]) {
1042 *out << " few=" << *values[Few];
1043 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001044
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001045 if (values[Many]) {
1046 *out << " many=" << *values[Many];
1047 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001048
1049 if (values[Other]) {
1050 *out << " other=" << *values[Other];
1051 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001052}
1053
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054bool Styleable::Equals(const Value* value) const {
1055 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 if (!other) {
1057 return false;
1058 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001059
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 if (entries.size() != other->entries.size()) {
1061 return false;
1062 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001063
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1065 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001066 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001067 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001068}
1069
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001072}
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001077}
1078
Adam Lesinski8197cc462016-08-19 12:16:49 -07001079bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081 if (cmp != 0) return cmp < 0;
1082 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001083}
1084
1085bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001086 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001087}
1088
1089bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001091}
1092
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001093struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094 bool operator()(const Reference& a, const Reference& b) const {
1095 return a.name < b.name;
1096 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001097};
1098
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001101 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 std::set<Reference, NameOnlyComparator> references;
1103 references.insert(entries.begin(), entries.end());
1104 references.insert(other->entries.begin(), other->entries.end());
1105 entries.clear();
1106 entries.reserve(references.size());
1107 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001108}
1109
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110} // namespace aapt