blob: 574bd2e44a84e7c19be88d01ae7909c5118d992b [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
Ryan Mitchellefcdb952021-04-14 17:31:37 -070050std::unique_ptr<Value> Value::Transform(ValueTransformer& transformer) const {
51 return std::unique_ptr<Value>(this->TransformValueImpl(transformer));
52}
53
54std::unique_ptr<Item> Item::Transform(ValueTransformer& transformer) const {
55 return std::unique_ptr<Item>(this->TransformItemImpl(transformer));
56}
57
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070059void BaseValue<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061}
62
63template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070064void BaseValue<Derived>::Accept(ConstValueVisitor* visitor) const {
65 visitor->Visit(static_cast<const Derived*>(this));
66}
67
68template <typename Derived>
69void BaseItem<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071}
72
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070073template <typename Derived>
74void BaseItem<Derived>::Accept(ConstValueVisitor* visitor) const {
75 visitor->Visit(static_cast<const Derived*>(this));
76}
77
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080bool RawString::Equals(const Value* value) const {
81 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (!other) {
83 return false;
84 }
85 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070086}
87
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088bool RawString::Flatten(android::Res_value* out_value) const {
89 out_value->dataType = android::Res_value::TYPE_STRING;
90 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092}
93
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109bool Reference::Equals(const Value* value) const {
110 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 if (!other) {
112 return false;
113 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 return reference_type == other->reference_type &&
115 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -0700117}
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119bool Reference::Flatten(android::Res_value* out_value) const {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800120 const ResourceId resid = id.value_or_default(ResourceId(0));
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800121 const bool dynamic = resid.is_valid() && is_dynamic;
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 -0700140void Reference::Print(std::ostream* out) const {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700141 if (reference_type == Type::kResource) {
142 *out << "(reference) @";
143 if (!name && !id) {
144 *out << "null";
145 return;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 } else {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700148 *out << "(attr-reference) ?";
149 }
150
151 if (private_reference) {
152 *out << "*";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 if (name) {
156 *out << name.value();
157 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800159 if (id && id.value().is_valid()) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700160 if (name) {
161 *out << " ";
162 }
163 *out << id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165}
166
Adam Lesinski93190b72017-11-03 15:20:17 -0700167static void PrettyPrintReferenceImpl(const Reference& ref, bool print_package, Printer* printer) {
168 switch (ref.reference_type) {
169 case Reference::Type::kResource:
170 printer->Print("@");
171 break;
172
173 case Reference::Type::kAttribute:
174 printer->Print("?");
175 break;
176 }
177
178 if (!ref.name && !ref.id) {
179 printer->Print("null");
180 return;
181 }
182
183 if (ref.private_reference) {
184 printer->Print("*");
185 }
186
187 if (ref.name) {
188 const ResourceName& name = ref.name.value();
189 if (print_package) {
190 printer->Print(name.to_string());
191 } else {
192 printer->Print(to_string(name.type));
193 printer->Print("/");
194 printer->Print(name.entry);
195 }
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800196 } else if (ref.id && ref.id.value().is_valid()) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700197 printer->Print(ref.id.value().to_string());
198 }
199}
200
201void Reference::PrettyPrint(Printer* printer) const {
202 PrettyPrintReferenceImpl(*this, true /*print_package*/, printer);
203}
204
205void Reference::PrettyPrint(const StringPiece& package, Printer* printer) const {
206 const bool print_package = name ? package != name.value().package : true;
207 PrettyPrintReferenceImpl(*this, print_package, printer);
208}
209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210bool Id::Equals(const Value* value) const {
211 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700212}
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218}
219
Adam Lesinski93190b72017-11-03 15:20:17 -0700220void Id::Print(std::ostream* out) const {
221 *out << "(id)";
222}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
Adam Lesinski93190b72017-11-03 15:20:17 -0700224String::String(const StringPool::Ref& ref) : value(ref) {
225}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227bool String::Equals(const Value* value) const {
228 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 if (!other) {
230 return false;
231 }
Adam Lesinski75421622017-01-06 15:20:04 -0800232
233 if (this->value != other->value) {
234 return false;
235 }
236
237 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
238 return false;
239 }
240
241 auto other_iter = other->untranslatable_sections.begin();
242 for (const UntranslatableSection& this_section : untranslatable_sections) {
243 if (this_section != *other_iter) {
244 return false;
245 }
246 ++other_iter;
247 }
248 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249}
250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 return false;
255 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 out_value->dataType = android::Res_value::TYPE_STRING;
258 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260}
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264}
265
Adam Lesinski93190b72017-11-03 15:20:17 -0700266void String::PrettyPrint(Printer* printer) const {
267 printer->Print("\"");
268 printer->Print(*value);
269 printer->Print("\"");
270}
271
272StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
273}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275bool StyledString::Equals(const Value* value) const {
276 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700278 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 }
280
Adam Lesinski75421622017-01-06 15:20:04 -0800281 if (this->value != other->value) {
282 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 }
Adam Lesinski75421622017-01-06 15:20:04 -0800284
285 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
286 return false;
287 }
288
289 auto other_iter = other->untranslatable_sections.begin();
290 for (const UntranslatableSection& this_section : untranslatable_sections) {
291 if (this_section != *other_iter) {
292 return false;
293 }
294 ++other_iter;
295 }
296 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299bool StyledString::Flatten(android::Res_value* out_value) const {
300 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return false;
302 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 out_value->dataType = android::Res_value::TYPE_STRING;
305 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307}
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309void StyledString::Print(std::ostream* out) const {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700310 *out << "(styled string) \"" << value->value << "\"";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 for (const StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700312 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
Adam Lesinski93190b72017-11-03 15:20:17 -0700316FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
317}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319bool FileReference::Equals(const Value* value) const {
320 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 if (!other) {
322 return false;
323 }
Adam Lesinski75421622017-01-06 15:20:04 -0800324 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700325}
326
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327bool FileReference::Flatten(android::Res_value* out_value) const {
328 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 return false;
330 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 out_value->dataType = android::Res_value::TYPE_STRING;
333 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335}
336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 *out << "(file) " << *path;
Adam Lesinskia65bbdf2018-02-15 12:39:44 -0800339 switch (type) {
340 case ResourceFile::Type::kBinaryXml:
341 *out << " type=XML";
342 break;
343 case ResourceFile::Type::kProtoXml:
344 *out << " type=protoXML";
345 break;
346 case ResourceFile::Type::kPng:
347 *out << " type=PNG";
348 break;
349 default:
350 break;
351 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352}
353
Adam Lesinski93190b72017-11-03 15:20:17 -0700354BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
355}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700357BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 value.dataType = dataType;
359 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700360}
361
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362bool BinaryPrimitive::Equals(const Value* value) const {
363 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 if (!other) {
365 return false;
366 }
367 return this->value.dataType == other->value.dataType &&
368 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700369}
370
Adam Lesinski93190b72017-11-03 15:20:17 -0700371bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 out_value->dataType = value.dataType;
373 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800375}
376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700378 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
379}
380
381static std::string ComplexToString(uint32_t complex_value, bool fraction) {
382 using ::android::Res_value;
383
384 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
385
386 // Determine the radix that was used.
387 const uint32_t radix =
388 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
389 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
390 Res_value::COMPLEX_MANTISSA_MASK}
391 << kRadixShifts[radix];
392 const float value = mantissa * (1.0f / (1 << 23));
393
394 std::string str = StringPrintf("%f", value);
395
396 const int unit_type =
397 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
398 if (fraction) {
399 switch (unit_type) {
400 case Res_value::COMPLEX_UNIT_FRACTION:
401 str += "%";
402 break;
403 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
404 str += "%p";
405 break;
406 default:
407 str += "???";
408 break;
409 }
410 } else {
411 switch (unit_type) {
412 case Res_value::COMPLEX_UNIT_PX:
413 str += "px";
414 break;
415 case Res_value::COMPLEX_UNIT_DIP:
416 str += "dp";
417 break;
418 case Res_value::COMPLEX_UNIT_SP:
419 str += "sp";
420 break;
421 case Res_value::COMPLEX_UNIT_PT:
422 str += "pt";
423 break;
424 case Res_value::COMPLEX_UNIT_IN:
425 str += "in";
426 break;
427 case Res_value::COMPLEX_UNIT_MM:
428 str += "mm";
429 break;
430 default:
431 str += "???";
432 break;
433 }
434 }
435 return str;
436}
437
438void BinaryPrimitive::PrettyPrint(Printer* printer) const {
439 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700441 case Res_value::TYPE_NULL:
442 if (value.data == Res_value::DATA_NULL_EMPTY) {
443 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700444 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700445 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700446 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700448
449 case Res_value::TYPE_INT_DEC:
450 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700452
453 case Res_value::TYPE_INT_HEX:
454 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700456
457 case Res_value::TYPE_INT_BOOLEAN:
458 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700460
461 case Res_value::TYPE_INT_COLOR_ARGB8:
462 case Res_value::TYPE_INT_COLOR_RGB8:
463 case Res_value::TYPE_INT_COLOR_ARGB4:
464 case Res_value::TYPE_INT_COLOR_RGB4:
465 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700467
468 case Res_value::TYPE_FLOAT:
469 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
470 break;
471
472 case Res_value::TYPE_DIMENSION:
473 printer->Print(ComplexToString(value.data, false /*fraction*/));
474 break;
475
476 case Res_value::TYPE_FRACTION:
477 printer->Print(ComplexToString(value.data, true /*fraction*/));
478 break;
479
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700481 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 break;
483 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484}
485
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800486Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 : type_mask(t),
488 min_int(std::numeric_limits<int32_t>::min()),
489 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490}
491
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700492std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
493 if (s.symbol.name) {
494 out << s.symbol.name.value().entry;
495 } else {
496 out << "???";
497 }
498 return out << "=" << s.value;
499}
500
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700501template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800502constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700504}
505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506bool Attribute::Equals(const Value* value) const {
507 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 if (!other) {
509 return false;
510 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700511
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 if (symbols.size() != other->symbols.size()) {
513 return false;
514 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700515
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700516 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 return false;
518 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 std::vector<const Symbol*> sorted_a;
521 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800522 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700523 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
524 return a->symbol.name < b->symbol.name;
525 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700528 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
529 add_pointer<const Symbol>);
530 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
531 return a->symbol.name < b->symbol.name;
532 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700536 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700538}
539
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800540bool Attribute::IsCompatibleWith(const Attribute& attr) const {
541 // If the high bits are set on any of these attribute type masks, then they are incompatible.
542 // We don't check that flags and enums are identical.
543 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
544 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
545 return false;
546 }
547
548 // Every attribute accepts a reference.
549 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
550 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
551 return this_type_mask == that_type_mask;
552}
553
Adam Lesinski93190b72017-11-03 15:20:17 -0700554std::string Attribute::MaskString() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700556 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800558
Adam Lesinski93190b72017-11-03 15:20:17 -0700559 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 if (!set) {
563 set = true;
564 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700565 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800566 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700567 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 if (!set) {
572 set = true;
573 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700574 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700576 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 if (!set) {
581 set = true;
582 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700583 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700585 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 if (!set) {
590 set = true;
591 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700592 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800593 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700594 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 if (!set) {
599 set = true;
600 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700601 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700603 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 if (!set) {
608 set = true;
609 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700610 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800611 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700612 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800614
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 if (!set) {
617 set = true;
618 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700619 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800620 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700621 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 if (!set) {
626 set = true;
627 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700628 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800629 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700630 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800632
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 if (!set) {
635 set = true;
636 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700637 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800638 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700639 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 if (!set) {
644 set = true;
645 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700646 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800647 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700648 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700650 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700651}
652
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700654 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800655
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 if (min_int != std::numeric_limits<int32_t>::min()) {
661 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 if (max_int != std::numeric_limits<int32_t>::max()) {
665 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 *out << " [weak]";
670 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800671}
672
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700673static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
674 DiagMessage* out_msg) {
675 *out_msg << "expected";
676 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
677 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800679
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700680 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
681 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800683
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700684 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
685 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800687
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700688 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
689 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800691
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700692 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
693 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800695
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700696 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
697 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800699
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700700 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
701 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800703
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700704 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
705 *out_msg << " integer";
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_REFERENCE) {
709 *out_msg << " reference";
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_STRING) {
713 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800715
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700716 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800717}
718
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700719bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
720 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
721 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
722 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
723 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
724
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700726 item.Flatten(&val);
727
728 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800729
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700731 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
732
733 // Only one type must match between the actual and expected.
734 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700736 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800737 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700739 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700741 // Enums and flags are encoded as integers, so check them first before doing any range checks.
742 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
743 for (const Symbol& s : symbols) {
744 if (flattened_data == s.value) {
745 return true;
746 }
747 }
748
749 // If the attribute accepts integers, we can't fail here.
750 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700752 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 }
754 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700755 }
756 }
757
758 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
759 uint32_t mask = 0u;
760 for (const Symbol& s : symbols) {
761 mask |= s.value;
762 }
763
764 // Check if the flattened data is covered by the flag bit mask.
765 // If the attribute accepts integers, we can't fail here.
766 if ((mask & flattened_data) == flattened_data) {
767 return true;
768 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700770 *out_msg << item << " is not a valid flag";
771 }
772 return false;
773 }
774 }
775
776 // Finally check the integer range of the value.
777 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
778 if (static_cast<int32_t>(flattened_data) < min_int) {
779 if (out_msg) {
780 *out_msg << item << " is less than minimum integer " << min_int;
781 }
782 return false;
783 } else if (static_cast<int32_t>(flattened_data) > max_int) {
784 if (out_msg) {
785 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 }
787 return false;
788 }
789 }
790 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800791}
792
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700793std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
794 if (entry.key.name) {
795 out << entry.key.name.value();
796 } else if (entry.key.id) {
797 out << entry.key.id.value();
798 } else {
799 out << "???";
800 }
801 out << " = " << entry.value;
802 return out;
803}
804
805template <typename T>
806std::vector<T*> ToPointerVec(std::vector<T>& src) {
807 std::vector<T*> dst;
808 dst.reserve(src.size());
809 for (T& in : src) {
810 dst.push_back(&in);
811 }
812 return dst;
813}
814
815template <typename T>
816std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
817 std::vector<const T*> dst;
818 dst.reserve(src.size());
819 for (const T& in : src) {
820 dst.push_back(&in);
821 }
822 return dst;
823}
824
825static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
826 return a->key.name < b->key.name;
827}
828
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829bool Style::Equals(const Value* value) const {
830 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 if (!other) {
832 return false;
833 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700834
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700836 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 return false;
838 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700839
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 if (entries.size() != other->entries.size()) {
841 return false;
842 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700843
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700844 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
845 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700846
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700847 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
848 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700849
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700852 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700854}
855
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700856void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 *out << "(style) ";
858 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700859 const Reference& parent_ref = parent.value();
860 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700863 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800866}
867
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700868Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
869 Style::Entry cloned_entry{entry.key};
870 if (entry.value != nullptr) {
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700871 CloningValueTransformer cloner(pool);
872 cloned_entry.value = entry.value->Transform(cloner);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700874 return cloned_entry;
875}
876
877void Style::MergeWith(Style* other, StringPool* pool) {
878 if (other->parent) {
879 parent = other->parent;
880 }
881
882 // We can't assume that the entries are sorted alphabetically since they're supposed to be
883 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
884 // them keying off that.
885 //
886 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
887 // those.
888
889 std::vector<Entry*> this_sorted = ToPointerVec(entries);
890 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
891
892 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
893 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
894
895 auto this_iter = this_sorted.begin();
896 const auto this_end = this_sorted.end();
897
898 auto other_iter = other_sorted.begin();
899 const auto other_end = other_sorted.end();
900
901 std::vector<Entry> merged_entries;
902 while (this_iter != this_end) {
903 if (other_iter != other_end) {
904 if ((*this_iter)->key.name < (*other_iter)->key.name) {
905 merged_entries.push_back(std::move(**this_iter));
906 ++this_iter;
907 } else {
908 // The other overrides.
909 merged_entries.push_back(CloneEntry(**other_iter, pool));
910 if ((*this_iter)->key.name == (*other_iter)->key.name) {
911 ++this_iter;
912 }
913 ++other_iter;
914 }
915 } else {
916 merged_entries.push_back(std::move(**this_iter));
917 ++this_iter;
918 }
919 }
920
921 while (other_iter != other_end) {
922 merged_entries.push_back(CloneEntry(**other_iter, pool));
923 ++other_iter;
924 }
925
926 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927}
928
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929bool Array::Equals(const Value* value) const {
930 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 if (!other) {
932 return false;
933 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700934
Adam Lesinski4ffea042017-08-10 15:37:28 -0700935 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936 return false;
937 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700938
Adam Lesinski4ffea042017-08-10 15:37:28 -0700939 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
940 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700943}
944
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700945void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700946 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800947}
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949bool Plural::Equals(const Value* value) const {
950 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951 if (!other) {
952 return false;
953 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700954
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800955 auto one_iter = values.begin();
956 auto one_end_iter = values.end();
957 auto two_iter = other->values.begin();
958 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
959 const std::unique_ptr<Item>& a = *one_iter;
960 const std::unique_ptr<Item>& b = *two_iter;
961 if (a != nullptr && b != nullptr) {
962 if (!a->Equals(b.get())) {
963 return false;
964 }
965 } else if (a != b) {
966 return false;
967 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800969 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700970}
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 *out << "(plural)";
974 if (values[Zero]) {
975 *out << " zero=" << *values[Zero];
976 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700977
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 if (values[One]) {
979 *out << " one=" << *values[One];
980 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700981
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 if (values[Two]) {
983 *out << " two=" << *values[Two];
984 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700985
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 if (values[Few]) {
987 *out << " few=" << *values[Few];
988 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700989
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 if (values[Many]) {
991 *out << " many=" << *values[Many];
992 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800993
994 if (values[Other]) {
995 *out << " other=" << *values[Other];
996 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800997}
998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999bool Styleable::Equals(const Value* value) const {
1000 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 if (!other) {
1002 return false;
1003 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001004
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 if (entries.size() != other->entries.size()) {
1006 return false;
1007 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001008
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1010 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001013}
1014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018}
1019
Adam Lesinski8197cc462016-08-19 12:16:49 -07001020bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 if (cmp != 0) return cmp < 0;
1023 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001024}
1025
1026bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001028}
1029
1030bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001032}
1033
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001034struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 bool operator()(const Reference& a, const Reference& b) const {
1036 return a.name < b.name;
1037 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001038};
1039
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001042 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 std::set<Reference, NameOnlyComparator> references;
1044 references.insert(entries.begin(), entries.end());
1045 references.insert(other->entries.begin(), other->entries.end());
1046 entries.clear();
1047 entries.reserve(references.size());
1048 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001049}
1050
Ryan Mitchellefcdb952021-04-14 17:31:37 -07001051template <typename T>
1052std::unique_ptr<T> CopyValueFields(std::unique_ptr<T> new_value, const T* value) {
1053 new_value->SetSource(value->GetSource());
1054 new_value->SetComment(value->GetComment());
1055 return new_value;
1056}
1057
1058CloningValueTransformer::CloningValueTransformer(StringPool* new_pool)
1059 : ValueTransformer(new_pool) {
1060}
1061
1062std::unique_ptr<Reference> CloningValueTransformer::TransformDerived(const Reference* value) {
1063 return std::make_unique<Reference>(*value);
1064}
1065
1066std::unique_ptr<Id> CloningValueTransformer::TransformDerived(const Id* value) {
1067 return std::make_unique<Id>(*value);
1068}
1069
1070std::unique_ptr<RawString> CloningValueTransformer::TransformDerived(const RawString* value) {
1071 auto new_value = std::make_unique<RawString>(pool_->MakeRef(value->value));
1072 return CopyValueFields(std::move(new_value), value);
1073}
1074
1075std::unique_ptr<String> CloningValueTransformer::TransformDerived(const String* value) {
1076 auto new_value = std::make_unique<String>(pool_->MakeRef(value->value));
1077 new_value->untranslatable_sections = value->untranslatable_sections;
1078 return CopyValueFields(std::move(new_value), value);
1079}
1080
1081std::unique_ptr<StyledString> CloningValueTransformer::TransformDerived(const StyledString* value) {
1082 auto new_value = std::make_unique<StyledString>(pool_->MakeRef(value->value));
1083 new_value->untranslatable_sections = value->untranslatable_sections;
1084 return CopyValueFields(std::move(new_value), value);
1085}
1086
1087std::unique_ptr<FileReference> CloningValueTransformer::TransformDerived(
1088 const FileReference* value) {
1089 auto new_value = std::make_unique<FileReference>(pool_->MakeRef(value->path));
1090 new_value->file = value->file;
1091 new_value->type = value->type;
1092 return CopyValueFields(std::move(new_value), value);
1093}
1094
1095std::unique_ptr<BinaryPrimitive> CloningValueTransformer::TransformDerived(
1096 const BinaryPrimitive* value) {
1097 return std::make_unique<BinaryPrimitive>(*value);
1098}
1099
1100std::unique_ptr<Attribute> CloningValueTransformer::TransformDerived(const Attribute* value) {
1101 auto new_value = std::make_unique<Attribute>();
1102 new_value->type_mask = value->type_mask;
1103 new_value->min_int = value->min_int;
1104 new_value->max_int = value->max_int;
1105 for (const Attribute::Symbol& s : value->symbols) {
1106 new_value->symbols.emplace_back(Attribute::Symbol{
1107 .symbol = *s.symbol.Transform(*this),
1108 .value = s.value,
1109 .type = s.type,
1110 });
1111 }
1112 return CopyValueFields(std::move(new_value), value);
1113}
1114
1115std::unique_ptr<Style> CloningValueTransformer::TransformDerived(const Style* value) {
1116 auto new_value = std::make_unique<Style>();
1117 new_value->parent = value->parent;
1118 new_value->parent_inferred = value->parent_inferred;
1119 for (auto& entry : value->entries) {
1120 new_value->entries.push_back(Style::Entry{entry.key, entry.value->Transform(*this)});
1121 }
1122 return CopyValueFields(std::move(new_value), value);
1123}
1124
1125std::unique_ptr<Array> CloningValueTransformer::TransformDerived(const Array* value) {
1126 auto new_value = std::make_unique<Array>();
1127 for (auto& item : value->elements) {
1128 new_value->elements.emplace_back(item->Transform(*this));
1129 }
1130 return CopyValueFields(std::move(new_value), value);
1131}
1132
1133std::unique_ptr<Plural> CloningValueTransformer::TransformDerived(const Plural* value) {
1134 auto new_value = std::make_unique<Plural>();
1135 const size_t count = value->values.size();
1136 for (size_t i = 0; i < count; i++) {
1137 if (value->values[i]) {
1138 new_value->values[i] = value->values[i]->Transform(*this);
1139 }
1140 }
1141 return CopyValueFields(std::move(new_value), value);
1142}
1143
1144std::unique_ptr<Styleable> CloningValueTransformer::TransformDerived(const Styleable* value) {
1145 auto new_value = std::make_unique<Styleable>();
1146 for (const Reference& s : value->entries) {
1147 new_value->entries.emplace_back(*s.Transform(*this));
1148 }
1149 return CopyValueFields(std::move(new_value), value);
1150}
1151
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152} // namespace aapt