blob: 77cee0683f3e84177f366864e0b66d620d526252 [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 Lesinski73bff1e2017-12-08 16:06:10 -0800510Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 : type_mask(t),
512 min_int(std::numeric_limits<int32_t>::min()),
513 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514}
515
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700516std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
517 if (s.symbol.name) {
518 out << s.symbol.name.value().entry;
519 } else {
520 out << "???";
521 }
522 return out << "=" << s.value;
523}
524
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700525template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800526constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700528}
529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530bool Attribute::Equals(const Value* value) const {
531 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 if (!other) {
533 return false;
534 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700535
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 if (symbols.size() != other->symbols.size()) {
537 return false;
538 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700539
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700540 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 return false;
542 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 std::vector<const Symbol*> sorted_a;
545 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800546 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700547 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
548 return a->symbol.name < b->symbol.name;
549 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700552 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
553 add_pointer<const Symbol>);
554 std::sort(sorted_b.begin(), sorted_b.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 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700560 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700562}
563
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800564bool Attribute::IsCompatibleWith(const Attribute& attr) const {
565 // If the high bits are set on any of these attribute type masks, then they are incompatible.
566 // We don't check that flags and enums are identical.
567 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
568 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
569 return false;
570 }
571
572 // Every attribute accepts a reference.
573 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
574 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
575 return this_type_mask == that_type_mask;
576}
577
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800580}
581
Adam Lesinski93190b72017-11-03 15:20:17 -0700582std::string Attribute::MaskString() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700584 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800586
Adam Lesinski93190b72017-11-03 15:20:17 -0700587 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 if (!set) {
591 set = true;
592 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700593 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700595 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 if (!set) {
600 set = true;
601 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700602 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800603 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700604 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 if (!set) {
609 set = true;
610 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700611 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800612 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700613 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 if (!set) {
618 set = true;
619 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700620 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800621 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700622 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 if (!set) {
627 set = true;
628 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700629 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800630 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700631 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 if (!set) {
636 set = true;
637 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700638 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800639 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700640 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800642
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 if (!set) {
645 set = true;
646 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700647 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800648 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700649 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 if (!set) {
654 set = true;
655 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700656 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800657 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700658 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 if (!set) {
663 set = true;
664 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700665 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800666 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700667 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800669
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 if (!set) {
672 set = true;
673 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700674 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800675 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700676 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700678 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700679}
680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700682 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800683
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 if (min_int != std::numeric_limits<int32_t>::min()) {
689 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700691
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 if (max_int != std::numeric_limits<int32_t>::max()) {
693 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700695
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 *out << " [weak]";
698 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800699}
700
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700701static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
702 DiagMessage* out_msg) {
703 *out_msg << "expected";
704 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
705 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800707
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700708 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
709 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800711
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700712 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
713 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800715
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700716 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
717 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800719
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700720 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
721 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800723
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700724 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
725 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800727
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700728 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
729 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800731
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700732 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
733 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800735
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700736 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
737 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800739
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700740 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
741 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800743
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700744 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800745}
746
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700747bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
748 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
749 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
750 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
751 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
752
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700754 item.Flatten(&val);
755
756 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800757
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700759 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
760
761 // Only one type must match between the actual and expected.
762 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700764 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800765 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700767 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700769 // Enums and flags are encoded as integers, so check them first before doing any range checks.
770 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
771 for (const Symbol& s : symbols) {
772 if (flattened_data == s.value) {
773 return true;
774 }
775 }
776
777 // If the attribute accepts integers, we can't fail here.
778 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700780 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
782 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700783 }
784 }
785
786 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
787 uint32_t mask = 0u;
788 for (const Symbol& s : symbols) {
789 mask |= s.value;
790 }
791
792 // Check if the flattened data is covered by the flag bit mask.
793 // If the attribute accepts integers, we can't fail here.
794 if ((mask & flattened_data) == flattened_data) {
795 return true;
796 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700798 *out_msg << item << " is not a valid flag";
799 }
800 return false;
801 }
802 }
803
804 // Finally check the integer range of the value.
805 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
806 if (static_cast<int32_t>(flattened_data) < min_int) {
807 if (out_msg) {
808 *out_msg << item << " is less than minimum integer " << min_int;
809 }
810 return false;
811 } else if (static_cast<int32_t>(flattened_data) > max_int) {
812 if (out_msg) {
813 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 }
815 return false;
816 }
817 }
818 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800819}
820
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700821std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
822 if (entry.key.name) {
823 out << entry.key.name.value();
824 } else if (entry.key.id) {
825 out << entry.key.id.value();
826 } else {
827 out << "???";
828 }
829 out << " = " << entry.value;
830 return out;
831}
832
833template <typename T>
834std::vector<T*> ToPointerVec(std::vector<T>& src) {
835 std::vector<T*> dst;
836 dst.reserve(src.size());
837 for (T& in : src) {
838 dst.push_back(&in);
839 }
840 return dst;
841}
842
843template <typename T>
844std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
845 std::vector<const T*> dst;
846 dst.reserve(src.size());
847 for (const T& in : src) {
848 dst.push_back(&in);
849 }
850 return dst;
851}
852
853static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
854 return a->key.name < b->key.name;
855}
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857bool Style::Equals(const Value* value) const {
858 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 if (!other) {
860 return false;
861 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700862
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700864 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865 return false;
866 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700867
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 if (entries.size() != other->entries.size()) {
869 return false;
870 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700871
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700872 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
873 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700874
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700875 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
876 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700880 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700882}
883
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 Style* style = new Style();
886 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700887 style->parent_inferred = parent_inferred;
888 style->comment_ = comment_;
889 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700891 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 }
893 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800894}
895
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 *out << "(style) ";
898 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700899 const Reference& parent_ref = parent.value();
900 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800902 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700903 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906}
907
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700908Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
909 Style::Entry cloned_entry{entry.key};
910 if (entry.value != nullptr) {
911 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700913 return cloned_entry;
914}
915
916void Style::MergeWith(Style* other, StringPool* pool) {
917 if (other->parent) {
918 parent = other->parent;
919 }
920
921 // We can't assume that the entries are sorted alphabetically since they're supposed to be
922 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
923 // them keying off that.
924 //
925 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
926 // those.
927
928 std::vector<Entry*> this_sorted = ToPointerVec(entries);
929 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
930
931 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
932 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
933
934 auto this_iter = this_sorted.begin();
935 const auto this_end = this_sorted.end();
936
937 auto other_iter = other_sorted.begin();
938 const auto other_end = other_sorted.end();
939
940 std::vector<Entry> merged_entries;
941 while (this_iter != this_end) {
942 if (other_iter != other_end) {
943 if ((*this_iter)->key.name < (*other_iter)->key.name) {
944 merged_entries.push_back(std::move(**this_iter));
945 ++this_iter;
946 } else {
947 // The other overrides.
948 merged_entries.push_back(CloneEntry(**other_iter, pool));
949 if ((*this_iter)->key.name == (*other_iter)->key.name) {
950 ++this_iter;
951 }
952 ++other_iter;
953 }
954 } else {
955 merged_entries.push_back(std::move(**this_iter));
956 ++this_iter;
957 }
958 }
959
960 while (other_iter != other_end) {
961 merged_entries.push_back(CloneEntry(**other_iter, pool));
962 ++other_iter;
963 }
964
965 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800966}
967
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968bool Array::Equals(const Value* value) const {
969 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 if (!other) {
971 return false;
972 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700973
Adam Lesinski4ffea042017-08-10 15:37:28 -0700974 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 return false;
976 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700977
Adam Lesinski4ffea042017-08-10 15:37:28 -0700978 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
979 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700982}
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 array->comment_ = comment_;
987 array->source_ = source_;
Adam Lesinski4ffea042017-08-10 15:37:28 -0700988 for (auto& item : elements) {
989 array->elements.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 }
991 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800992}
993
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700995 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800996}
997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998bool Plural::Equals(const Value* value) const {
999 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 if (!other) {
1001 return false;
1002 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001003
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001004 auto one_iter = values.begin();
1005 auto one_end_iter = values.end();
1006 auto two_iter = other->values.begin();
1007 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
1008 const std::unique_ptr<Item>& a = *one_iter;
1009 const std::unique_ptr<Item>& b = *two_iter;
1010 if (a != nullptr && b != nullptr) {
1011 if (!a->Equals(b.get())) {
1012 return false;
1013 }
1014 } else if (a != b) {
1015 return false;
1016 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001017 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001018 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -07001019}
1020
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 p->comment_ = comment_;
1024 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001025 const size_t count = values.size();
1026 for (size_t i = 0; i < count; i++) {
1027 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001029 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 }
1031 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001032}
1033
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 *out << "(plural)";
1036 if (values[Zero]) {
1037 *out << " zero=" << *values[Zero];
1038 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001039
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 if (values[One]) {
1041 *out << " one=" << *values[One];
1042 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001043
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 if (values[Two]) {
1045 *out << " two=" << *values[Two];
1046 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001047
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 if (values[Few]) {
1049 *out << " few=" << *values[Few];
1050 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001051
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052 if (values[Many]) {
1053 *out << " many=" << *values[Many];
1054 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001055
1056 if (values[Other]) {
1057 *out << " other=" << *values[Other];
1058 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001059}
1060
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061bool Styleable::Equals(const Value* value) const {
1062 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 if (!other) {
1064 return false;
1065 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001066
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001067 if (entries.size() != other->entries.size()) {
1068 return false;
1069 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001070
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1072 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001075}
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001079}
1080
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001084}
1085
Adam Lesinski8197cc462016-08-19 12:16:49 -07001086bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001087 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 if (cmp != 0) return cmp < 0;
1089 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001090}
1091
1092bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001094}
1095
1096bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001098}
1099
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001100struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 bool operator()(const Reference& a, const Reference& b) const {
1102 return a.name < b.name;
1103 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001104};
1105
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001108 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 std::set<Reference, NameOnlyComparator> references;
1110 references.insert(entries.begin(), entries.end());
1111 references.insert(other->entries.begin(), other->entries.end());
1112 entries.clear();
1113 entries.reserve(references.size());
1114 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001115}
1116
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001117} // namespace aapt