blob: 6f213e19e5f6f1f5ee7ecdad8c340efd1d021ebf [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 Lesinskia65bbdf2018-02-15 12:39:44 -0800372 switch (type) {
373 case ResourceFile::Type::kBinaryXml:
374 *out << " type=XML";
375 break;
376 case ResourceFile::Type::kProtoXml:
377 *out << " type=protoXML";
378 break;
379 case ResourceFile::Type::kPng:
380 *out << " type=PNG";
381 break;
382 default:
383 break;
384 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385}
386
Adam Lesinski93190b72017-11-03 15:20:17 -0700387BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
388}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800389
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700390BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 value.dataType = dataType;
392 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393}
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395bool BinaryPrimitive::Equals(const Value* value) const {
396 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 if (!other) {
398 return false;
399 }
400 return this->value.dataType == other->value.dataType &&
401 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700402}
403
Adam Lesinski93190b72017-11-03 15:20:17 -0700404bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 out_value->dataType = value.dataType;
406 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408}
409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412}
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700415 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
416}
417
418static std::string ComplexToString(uint32_t complex_value, bool fraction) {
419 using ::android::Res_value;
420
421 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
422
423 // Determine the radix that was used.
424 const uint32_t radix =
425 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
426 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
427 Res_value::COMPLEX_MANTISSA_MASK}
428 << kRadixShifts[radix];
429 const float value = mantissa * (1.0f / (1 << 23));
430
431 std::string str = StringPrintf("%f", value);
432
433 const int unit_type =
434 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
435 if (fraction) {
436 switch (unit_type) {
437 case Res_value::COMPLEX_UNIT_FRACTION:
438 str += "%";
439 break;
440 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
441 str += "%p";
442 break;
443 default:
444 str += "???";
445 break;
446 }
447 } else {
448 switch (unit_type) {
449 case Res_value::COMPLEX_UNIT_PX:
450 str += "px";
451 break;
452 case Res_value::COMPLEX_UNIT_DIP:
453 str += "dp";
454 break;
455 case Res_value::COMPLEX_UNIT_SP:
456 str += "sp";
457 break;
458 case Res_value::COMPLEX_UNIT_PT:
459 str += "pt";
460 break;
461 case Res_value::COMPLEX_UNIT_IN:
462 str += "in";
463 break;
464 case Res_value::COMPLEX_UNIT_MM:
465 str += "mm";
466 break;
467 default:
468 str += "???";
469 break;
470 }
471 }
472 return str;
473}
474
475void BinaryPrimitive::PrettyPrint(Printer* printer) const {
476 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700478 case Res_value::TYPE_NULL:
479 if (value.data == Res_value::DATA_NULL_EMPTY) {
480 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700481 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700482 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700483 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700485
486 case Res_value::TYPE_INT_DEC:
487 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700489
490 case Res_value::TYPE_INT_HEX:
491 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700493
494 case Res_value::TYPE_INT_BOOLEAN:
495 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700497
498 case Res_value::TYPE_INT_COLOR_ARGB8:
499 case Res_value::TYPE_INT_COLOR_RGB8:
500 case Res_value::TYPE_INT_COLOR_ARGB4:
501 case Res_value::TYPE_INT_COLOR_RGB4:
502 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700504
505 case Res_value::TYPE_FLOAT:
506 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
507 break;
508
509 case Res_value::TYPE_DIMENSION:
510 printer->Print(ComplexToString(value.data, false /*fraction*/));
511 break;
512
513 case Res_value::TYPE_FRACTION:
514 printer->Print(ComplexToString(value.data, true /*fraction*/));
515 break;
516
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700518 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 break;
520 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521}
522
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800523Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 : type_mask(t),
525 min_int(std::numeric_limits<int32_t>::min()),
526 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527}
528
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700529std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
530 if (s.symbol.name) {
531 out << s.symbol.name.value().entry;
532 } else {
533 out << "???";
534 }
535 return out << "=" << s.value;
536}
537
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700538template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800539constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700541}
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543bool Attribute::Equals(const Value* value) const {
544 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 if (!other) {
546 return false;
547 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700548
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 if (symbols.size() != other->symbols.size()) {
550 return false;
551 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700552
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700553 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 return false;
555 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700556
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 std::vector<const Symbol*> sorted_a;
558 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800559 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700560 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
561 return a->symbol.name < b->symbol.name;
562 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700565 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
566 add_pointer<const Symbol>);
567 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
568 return a->symbol.name < b->symbol.name;
569 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700573 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700575}
576
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800577bool Attribute::IsCompatibleWith(const Attribute& attr) const {
578 // If the high bits are set on any of these attribute type masks, then they are incompatible.
579 // We don't check that flags and enums are identical.
580 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
581 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
582 return false;
583 }
584
585 // Every attribute accepts a reference.
586 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
587 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
588 return this_type_mask == that_type_mask;
589}
590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800593}
594
Adam Lesinski93190b72017-11-03 15:20:17 -0700595std::string Attribute::MaskString() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700597 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800599
Adam Lesinski93190b72017-11-03 15:20:17 -0700600 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 if (!set) {
604 set = true;
605 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700606 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800607 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700608 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 if (!set) {
613 set = true;
614 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700615 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800616 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700617 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800619
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 if (!set) {
622 set = true;
623 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700624 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700626 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 if (!set) {
631 set = true;
632 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700633 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800634 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700635 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800637
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 if (!set) {
640 set = true;
641 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700642 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800643 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700644 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 if (!set) {
649 set = true;
650 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700651 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800652 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700653 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 if (!set) {
658 set = true;
659 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700660 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800661 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700662 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 if (!set) {
667 set = true;
668 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700669 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800670 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700671 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 if (!set) {
676 set = true;
677 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700678 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800679 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700680 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 if (!set) {
685 set = true;
686 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700687 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800688 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700689 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700691 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700692}
693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700695 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800700
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 if (min_int != std::numeric_limits<int32_t>::min()) {
702 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 if (max_int != std::numeric_limits<int32_t>::max()) {
706 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700708
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 *out << " [weak]";
711 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800712}
713
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700714static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
715 DiagMessage* out_msg) {
716 *out_msg << "expected";
717 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
718 *out_msg << " boolean";
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_COLOR) {
722 *out_msg << " color";
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_DIMENSION) {
726 *out_msg << " dimension";
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_ENUM) {
730 *out_msg << " enum";
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_FLAGS) {
734 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800736
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700737 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
738 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800740
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700741 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
742 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800744
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700745 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
746 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800748
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700749 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
750 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800752
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700753 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
754 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800756
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700757 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800758}
759
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700760bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
761 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
762 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
763 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
764 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
765
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700767 item.Flatten(&val);
768
769 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800770
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700772 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
773
774 // Only one type must match between the actual and expected.
775 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700777 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800778 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700780 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700782 // Enums and flags are encoded as integers, so check them first before doing any range checks.
783 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
784 for (const Symbol& s : symbols) {
785 if (flattened_data == s.value) {
786 return true;
787 }
788 }
789
790 // If the attribute accepts integers, we can't fail here.
791 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700793 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 }
795 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700796 }
797 }
798
799 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
800 uint32_t mask = 0u;
801 for (const Symbol& s : symbols) {
802 mask |= s.value;
803 }
804
805 // Check if the flattened data is covered by the flag bit mask.
806 // If the attribute accepts integers, we can't fail here.
807 if ((mask & flattened_data) == flattened_data) {
808 return true;
809 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700811 *out_msg << item << " is not a valid flag";
812 }
813 return false;
814 }
815 }
816
817 // Finally check the integer range of the value.
818 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
819 if (static_cast<int32_t>(flattened_data) < min_int) {
820 if (out_msg) {
821 *out_msg << item << " is less than minimum integer " << min_int;
822 }
823 return false;
824 } else if (static_cast<int32_t>(flattened_data) > max_int) {
825 if (out_msg) {
826 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 }
828 return false;
829 }
830 }
831 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800832}
833
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700834std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
835 if (entry.key.name) {
836 out << entry.key.name.value();
837 } else if (entry.key.id) {
838 out << entry.key.id.value();
839 } else {
840 out << "???";
841 }
842 out << " = " << entry.value;
843 return out;
844}
845
846template <typename T>
847std::vector<T*> ToPointerVec(std::vector<T>& src) {
848 std::vector<T*> dst;
849 dst.reserve(src.size());
850 for (T& in : src) {
851 dst.push_back(&in);
852 }
853 return dst;
854}
855
856template <typename T>
857std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
858 std::vector<const T*> dst;
859 dst.reserve(src.size());
860 for (const T& in : src) {
861 dst.push_back(&in);
862 }
863 return dst;
864}
865
866static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
867 return a->key.name < b->key.name;
868}
869
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870bool Style::Equals(const Value* value) const {
871 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 if (!other) {
873 return false;
874 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700875
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700877 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return false;
879 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700880
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 if (entries.size() != other->entries.size()) {
882 return false;
883 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700884
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700885 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
886 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700887
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700888 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
889 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700893 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700895}
896
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 Style* style = new Style();
899 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 style->parent_inferred = parent_inferred;
901 style->comment_ = comment_;
902 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700904 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 }
906 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800907}
908
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 *out << "(style) ";
911 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700912 const Reference& parent_ref = parent.value();
913 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800915 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700916 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800919}
920
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700921Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
922 Style::Entry cloned_entry{entry.key};
923 if (entry.value != nullptr) {
924 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700926 return cloned_entry;
927}
928
929void Style::MergeWith(Style* other, StringPool* pool) {
930 if (other->parent) {
931 parent = other->parent;
932 }
933
934 // We can't assume that the entries are sorted alphabetically since they're supposed to be
935 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
936 // them keying off that.
937 //
938 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
939 // those.
940
941 std::vector<Entry*> this_sorted = ToPointerVec(entries);
942 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
943
944 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
945 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
946
947 auto this_iter = this_sorted.begin();
948 const auto this_end = this_sorted.end();
949
950 auto other_iter = other_sorted.begin();
951 const auto other_end = other_sorted.end();
952
953 std::vector<Entry> merged_entries;
954 while (this_iter != this_end) {
955 if (other_iter != other_end) {
956 if ((*this_iter)->key.name < (*other_iter)->key.name) {
957 merged_entries.push_back(std::move(**this_iter));
958 ++this_iter;
959 } else {
960 // The other overrides.
961 merged_entries.push_back(CloneEntry(**other_iter, pool));
962 if ((*this_iter)->key.name == (*other_iter)->key.name) {
963 ++this_iter;
964 }
965 ++other_iter;
966 }
967 } else {
968 merged_entries.push_back(std::move(**this_iter));
969 ++this_iter;
970 }
971 }
972
973 while (other_iter != other_end) {
974 merged_entries.push_back(CloneEntry(**other_iter, pool));
975 ++other_iter;
976 }
977
978 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800979}
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981bool Array::Equals(const Value* value) const {
982 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 if (!other) {
984 return false;
985 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700986
Adam Lesinski4ffea042017-08-10 15:37:28 -0700987 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 return false;
989 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700990
Adam Lesinski4ffea042017-08-10 15:37:28 -0700991 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
992 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700993 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700995}
996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 array->comment_ = comment_;
1000 array->source_ = source_;
Adam Lesinski4ffea042017-08-10 15:37:28 -07001001 for (auto& item : elements) {
1002 array->elements.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 }
1004 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001005}
1006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -07001008 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001009}
1010
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011bool Plural::Equals(const Value* value) const {
1012 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 if (!other) {
1014 return false;
1015 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001016
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001017 auto one_iter = values.begin();
1018 auto one_end_iter = values.end();
1019 auto two_iter = other->values.begin();
1020 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
1021 const std::unique_ptr<Item>& a = *one_iter;
1022 const std::unique_ptr<Item>& b = *two_iter;
1023 if (a != nullptr && b != nullptr) {
1024 if (!a->Equals(b.get())) {
1025 return false;
1026 }
1027 } else if (a != b) {
1028 return false;
1029 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001031 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -07001032}
1033
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 p->comment_ = comment_;
1037 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001038 const size_t count = values.size();
1039 for (size_t i = 0; i < count; i++) {
1040 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001041 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001042 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 }
1044 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001045}
1046
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 *out << "(plural)";
1049 if (values[Zero]) {
1050 *out << " zero=" << *values[Zero];
1051 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001052
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 if (values[One]) {
1054 *out << " one=" << *values[One];
1055 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001056
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 if (values[Two]) {
1058 *out << " two=" << *values[Two];
1059 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001060
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001061 if (values[Few]) {
1062 *out << " few=" << *values[Few];
1063 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001064
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 if (values[Many]) {
1066 *out << " many=" << *values[Many];
1067 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001068
1069 if (values[Other]) {
1070 *out << " other=" << *values[Other];
1071 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001072}
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074bool Styleable::Equals(const Value* value) const {
1075 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001076 if (!other) {
1077 return false;
1078 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001079
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 if (entries.size() != other->entries.size()) {
1081 return false;
1082 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001083
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1085 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001088}
1089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001092}
1093
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001097}
1098
Adam Lesinski8197cc462016-08-19 12:16:49 -07001099bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 if (cmp != 0) return cmp < 0;
1102 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001103}
1104
1105bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001107}
1108
1109bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001111}
1112
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001113struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 bool operator()(const Reference& a, const Reference& b) const {
1115 return a.name < b.name;
1116 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001117};
1118
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001121 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 std::set<Reference, NameOnlyComparator> references;
1123 references.insert(entries.begin(), entries.end());
1124 references.insert(other->entries.begin(), other->entries.end());
1125 entries.clear();
1126 entries.reserve(references.size());
1127 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001128}
1129
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130} // namespace aapt