blob: b796eb07f076a073121b4df5d67a5944fae12c8a [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 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700114 return reference_type == other->reference_type && private_reference == other->private_reference &&
115 id == other->id && name == other->name && type_flags == other->type_flags;
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 {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000119 if (name && name.value().type.type == ResourceType::kMacro) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700120 return false;
121 }
122
Ryan Mitchell4382e442021-07-14 12:53:01 -0700123 const ResourceId resid = id.value_or(ResourceId(0));
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800124 const bool dynamic = resid.is_valid() && is_dynamic;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800125
126 if (reference_type == Reference::Type::kResource) {
127 if (dynamic) {
128 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
129 } else {
130 out_value->dataType = android::Res_value::TYPE_REFERENCE;
131 }
132 } else {
133 if (dynamic) {
134 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
135 } else {
136 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
137 }
138 }
139 out_value->data = util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143void Reference::Print(std::ostream* out) const {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700144 if (reference_type == Type::kResource) {
145 *out << "(reference) @";
146 if (!name && !id) {
147 *out << "null";
148 return;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 } else {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700151 *out << "(attr-reference) ?";
152 }
153
154 if (private_reference) {
155 *out << "*";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 if (name) {
159 *out << name.value();
160 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800162 if (id && id.value().is_valid()) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700163 if (name) {
164 *out << " ";
165 }
166 *out << id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168}
169
Adam Lesinski93190b72017-11-03 15:20:17 -0700170static void PrettyPrintReferenceImpl(const Reference& ref, bool print_package, Printer* printer) {
171 switch (ref.reference_type) {
172 case Reference::Type::kResource:
173 printer->Print("@");
174 break;
175
176 case Reference::Type::kAttribute:
177 printer->Print("?");
178 break;
179 }
180
181 if (!ref.name && !ref.id) {
182 printer->Print("null");
183 return;
184 }
185
186 if (ref.private_reference) {
187 printer->Print("*");
188 }
189
190 if (ref.name) {
191 const ResourceName& name = ref.name.value();
192 if (print_package) {
193 printer->Print(name.to_string());
194 } else {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000195 printer->Print(name.type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700196 printer->Print("/");
197 printer->Print(name.entry);
198 }
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800199 } else if (ref.id && ref.id.value().is_valid()) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700200 printer->Print(ref.id.value().to_string());
201 }
202}
203
204void Reference::PrettyPrint(Printer* printer) const {
205 PrettyPrintReferenceImpl(*this, true /*print_package*/, printer);
206}
207
208void Reference::PrettyPrint(const StringPiece& package, Printer* printer) const {
209 const bool print_package = name ? package != name.value().package : true;
210 PrettyPrintReferenceImpl(*this, print_package, printer);
211}
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213bool Id::Equals(const Value* value) const {
214 return ValueCast<Id>(value) != nullptr;
Adam Lesinski458b8772016-04-25 14:20:21 -0700215}
216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217bool Id::Flatten(android::Res_value* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 out->data = util::HostToDevice32(0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221}
222
Adam Lesinski93190b72017-11-03 15:20:17 -0700223void Id::Print(std::ostream* out) const {
224 *out << "(id)";
225}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski93190b72017-11-03 15:20:17 -0700227String::String(const StringPool::Ref& ref) : value(ref) {
228}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800229
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230bool String::Equals(const Value* value) const {
231 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 if (!other) {
233 return false;
234 }
Adam Lesinski75421622017-01-06 15:20:04 -0800235
236 if (this->value != other->value) {
237 return false;
238 }
239
240 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
241 return false;
242 }
243
244 auto other_iter = other->untranslatable_sections.begin();
245 for (const UntranslatableSection& this_section : untranslatable_sections) {
246 if (this_section != *other_iter) {
247 return false;
248 }
249 ++other_iter;
250 }
251 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252}
253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 return false;
258 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 out_value->dataType = android::Res_value::TYPE_STRING;
261 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263}
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267}
268
Adam Lesinski93190b72017-11-03 15:20:17 -0700269void String::PrettyPrint(Printer* printer) const {
270 printer->Print("\"");
271 printer->Print(*value);
272 printer->Print("\"");
273}
274
275StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
276}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278bool StyledString::Equals(const Value* value) const {
279 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700281 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 }
283
Adam Lesinski75421622017-01-06 15:20:04 -0800284 if (this->value != other->value) {
285 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 }
Adam Lesinski75421622017-01-06 15:20:04 -0800287
288 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
289 return false;
290 }
291
292 auto other_iter = other->untranslatable_sections.begin();
293 for (const UntranslatableSection& this_section : untranslatable_sections) {
294 if (this_section != *other_iter) {
295 return false;
296 }
297 ++other_iter;
298 }
299 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300}
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302bool StyledString::Flatten(android::Res_value* out_value) const {
303 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return false;
305 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 out_value->dataType = android::Res_value::TYPE_STRING;
308 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310}
311
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312void StyledString::Print(std::ostream* out) const {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700313 *out << "(styled string) \"" << value->value << "\"";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 for (const StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700315 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317}
318
Adam Lesinski93190b72017-11-03 15:20:17 -0700319FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
320}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322bool FileReference::Equals(const Value* value) const {
323 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 if (!other) {
325 return false;
326 }
Adam Lesinski75421622017-01-06 15:20:04 -0800327 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700328}
329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330bool FileReference::Flatten(android::Res_value* out_value) const {
331 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 return false;
333 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 out_value->dataType = android::Res_value::TYPE_STRING;
336 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338}
339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 *out << "(file) " << *path;
Adam Lesinskia65bbdf2018-02-15 12:39:44 -0800342 switch (type) {
343 case ResourceFile::Type::kBinaryXml:
344 *out << " type=XML";
345 break;
346 case ResourceFile::Type::kProtoXml:
347 *out << " type=protoXML";
348 break;
349 case ResourceFile::Type::kPng:
350 *out << " type=PNG";
351 break;
352 default:
353 break;
354 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355}
356
Adam Lesinski93190b72017-11-03 15:20:17 -0700357BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
358}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700360BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 value.dataType = dataType;
362 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700363}
364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365bool BinaryPrimitive::Equals(const Value* value) const {
366 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 if (!other) {
368 return false;
369 }
370 return this->value.dataType == other->value.dataType &&
371 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700372}
373
Adam Lesinski93190b72017-11-03 15:20:17 -0700374bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 out_value->dataType = value.dataType;
376 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800378}
379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700381 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
382}
383
384static std::string ComplexToString(uint32_t complex_value, bool fraction) {
385 using ::android::Res_value;
386
387 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
388
389 // Determine the radix that was used.
390 const uint32_t radix =
391 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
392 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
393 Res_value::COMPLEX_MANTISSA_MASK}
394 << kRadixShifts[radix];
395 const float value = mantissa * (1.0f / (1 << 23));
396
397 std::string str = StringPrintf("%f", value);
398
399 const int unit_type =
400 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
401 if (fraction) {
402 switch (unit_type) {
403 case Res_value::COMPLEX_UNIT_FRACTION:
404 str += "%";
405 break;
406 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
407 str += "%p";
408 break;
409 default:
410 str += "???";
411 break;
412 }
413 } else {
414 switch (unit_type) {
415 case Res_value::COMPLEX_UNIT_PX:
416 str += "px";
417 break;
418 case Res_value::COMPLEX_UNIT_DIP:
419 str += "dp";
420 break;
421 case Res_value::COMPLEX_UNIT_SP:
422 str += "sp";
423 break;
424 case Res_value::COMPLEX_UNIT_PT:
425 str += "pt";
426 break;
427 case Res_value::COMPLEX_UNIT_IN:
428 str += "in";
429 break;
430 case Res_value::COMPLEX_UNIT_MM:
431 str += "mm";
432 break;
433 default:
434 str += "???";
435 break;
436 }
437 }
438 return str;
439}
440
441void BinaryPrimitive::PrettyPrint(Printer* printer) const {
442 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700444 case Res_value::TYPE_NULL:
445 if (value.data == Res_value::DATA_NULL_EMPTY) {
446 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700447 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700448 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700449 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700451
452 case Res_value::TYPE_INT_DEC:
453 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700455
456 case Res_value::TYPE_INT_HEX:
457 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700459
460 case Res_value::TYPE_INT_BOOLEAN:
461 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700463
464 case Res_value::TYPE_INT_COLOR_ARGB8:
465 case Res_value::TYPE_INT_COLOR_RGB8:
466 case Res_value::TYPE_INT_COLOR_ARGB4:
467 case Res_value::TYPE_INT_COLOR_RGB4:
468 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700470
471 case Res_value::TYPE_FLOAT:
472 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
473 break;
474
475 case Res_value::TYPE_DIMENSION:
476 printer->Print(ComplexToString(value.data, false /*fraction*/));
477 break;
478
479 case Res_value::TYPE_FRACTION:
480 printer->Print(ComplexToString(value.data, true /*fraction*/));
481 break;
482
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700484 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 break;
486 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487}
488
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800489Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 : type_mask(t),
491 min_int(std::numeric_limits<int32_t>::min()),
492 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493}
494
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700495std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
496 if (s.symbol.name) {
497 out << s.symbol.name.value().entry;
498 } else {
499 out << "???";
500 }
501 return out << "=" << s.value;
502}
503
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700504template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800505constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700507}
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509bool Attribute::Equals(const Value* value) const {
510 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 if (!other) {
512 return false;
513 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700514
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 if (symbols.size() != other->symbols.size()) {
516 return false;
517 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700518
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700519 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 return false;
521 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 std::vector<const Symbol*> sorted_a;
524 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800525 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700526 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
527 return a->symbol.name < b->symbol.name;
528 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700531 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
532 add_pointer<const Symbol>);
533 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
534 return a->symbol.name < b->symbol.name;
535 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700536
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700539 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700541}
542
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800543bool Attribute::IsCompatibleWith(const Attribute& attr) const {
544 // If the high bits are set on any of these attribute type masks, then they are incompatible.
545 // We don't check that flags and enums are identical.
546 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
547 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
548 return false;
549 }
550
551 // Every attribute accepts a reference.
552 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
553 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
554 return this_type_mask == that_type_mask;
555}
556
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700557std::string Attribute::MaskString(uint32_t type_mask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700559 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800561
Adam Lesinski93190b72017-11-03 15:20:17 -0700562 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 if (!set) {
566 set = true;
567 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700568 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700570 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 if (!set) {
575 set = true;
576 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700577 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700579 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 if (!set) {
584 set = true;
585 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700586 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700588 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 if (!set) {
593 set = true;
594 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700595 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800596 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700597 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 if (!set) {
602 set = true;
603 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700604 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800605 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700606 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 if (!set) {
611 set = true;
612 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700613 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800614 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700615 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 if (!set) {
620 set = true;
621 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700622 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800623 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700624 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 if (!set) {
629 set = true;
630 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700631 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800632 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700633 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 if (!set) {
638 set = true;
639 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700640 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800641 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700642 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 if (!set) {
647 set = true;
648 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700649 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800650 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700651 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700653 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700654}
655
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700656std::string Attribute::MaskString() const {
657 return MaskString(type_mask);
658}
659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700661 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800666
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 if (min_int != std::numeric_limits<int32_t>::min()) {
668 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 if (max_int != std::numeric_limits<int32_t>::max()) {
672 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 *out << " [weak]";
677 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800678}
679
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700680static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
681 DiagMessage* out_msg) {
682 *out_msg << "expected";
683 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
684 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800686
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700687 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
688 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800690
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700691 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
692 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800694
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700695 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
696 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800698
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700699 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
700 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800702
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700703 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
704 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800706
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700707 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
708 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800710
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700711 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
712 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800714
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700715 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
716 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800718
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700719 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
720 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800722
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700723 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800724}
725
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700726bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
727 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
728 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
729 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
730 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
731
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700733 item.Flatten(&val);
734
735 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800736
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700738 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
739
740 // Only one type must match between the actual and expected.
741 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700743 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800744 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700746 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700748 // Enums and flags are encoded as integers, so check them first before doing any range checks.
749 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
750 for (const Symbol& s : symbols) {
751 if (flattened_data == s.value) {
752 return true;
753 }
754 }
755
756 // If the attribute accepts integers, we can't fail here.
757 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700759 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 }
761 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700762 }
763 }
764
765 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
766 uint32_t mask = 0u;
767 for (const Symbol& s : symbols) {
768 mask |= s.value;
769 }
770
771 // Check if the flattened data is covered by the flag bit mask.
772 // If the attribute accepts integers, we can't fail here.
773 if ((mask & flattened_data) == flattened_data) {
774 return true;
775 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700777 *out_msg << item << " is not a valid flag";
778 }
779 return false;
780 }
781 }
782
783 // Finally check the integer range of the value.
784 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
785 if (static_cast<int32_t>(flattened_data) < min_int) {
786 if (out_msg) {
787 *out_msg << item << " is less than minimum integer " << min_int;
788 }
789 return false;
790 } else if (static_cast<int32_t>(flattened_data) > max_int) {
791 if (out_msg) {
792 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794 return false;
795 }
796 }
797 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800798}
799
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700800std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
801 if (entry.key.name) {
802 out << entry.key.name.value();
803 } else if (entry.key.id) {
804 out << entry.key.id.value();
805 } else {
806 out << "???";
807 }
808 out << " = " << entry.value;
809 return out;
810}
811
812template <typename T>
813std::vector<T*> ToPointerVec(std::vector<T>& src) {
814 std::vector<T*> dst;
815 dst.reserve(src.size());
816 for (T& in : src) {
817 dst.push_back(&in);
818 }
819 return dst;
820}
821
822template <typename T>
823std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
824 std::vector<const T*> dst;
825 dst.reserve(src.size());
826 for (const T& in : src) {
827 dst.push_back(&in);
828 }
829 return dst;
830}
831
832static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
833 return a->key.name < b->key.name;
834}
835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836bool Style::Equals(const Value* value) const {
837 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 if (!other) {
839 return false;
840 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700841
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700843 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 return false;
845 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700846
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 if (entries.size() != other->entries.size()) {
848 return false;
849 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700850
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700851 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
852 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700853
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700854 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
855 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700859 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700861}
862
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700863void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 *out << "(style) ";
865 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700866 const Reference& parent_ref = parent.value();
867 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800869 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700870 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700871 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873}
874
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700875Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
876 Style::Entry cloned_entry{entry.key};
877 if (entry.value != nullptr) {
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700878 CloningValueTransformer cloner(pool);
879 cloned_entry.value = entry.value->Transform(cloner);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700881 return cloned_entry;
882}
883
884void Style::MergeWith(Style* other, StringPool* pool) {
885 if (other->parent) {
886 parent = other->parent;
887 }
888
889 // We can't assume that the entries are sorted alphabetically since they're supposed to be
890 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
891 // them keying off that.
892 //
893 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
894 // those.
895
896 std::vector<Entry*> this_sorted = ToPointerVec(entries);
897 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
898
899 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
900 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
901
902 auto this_iter = this_sorted.begin();
903 const auto this_end = this_sorted.end();
904
905 auto other_iter = other_sorted.begin();
906 const auto other_end = other_sorted.end();
907
908 std::vector<Entry> merged_entries;
909 while (this_iter != this_end) {
910 if (other_iter != other_end) {
911 if ((*this_iter)->key.name < (*other_iter)->key.name) {
912 merged_entries.push_back(std::move(**this_iter));
913 ++this_iter;
914 } else {
915 // The other overrides.
916 merged_entries.push_back(CloneEntry(**other_iter, pool));
917 if ((*this_iter)->key.name == (*other_iter)->key.name) {
918 ++this_iter;
919 }
920 ++other_iter;
921 }
922 } else {
923 merged_entries.push_back(std::move(**this_iter));
924 ++this_iter;
925 }
926 }
927
928 while (other_iter != other_end) {
929 merged_entries.push_back(CloneEntry(**other_iter, pool));
930 ++other_iter;
931 }
932
933 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934}
935
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700936bool Array::Equals(const Value* value) const {
937 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938 if (!other) {
939 return false;
940 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700941
Adam Lesinski4ffea042017-08-10 15:37:28 -0700942 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 return false;
944 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700945
Adam Lesinski4ffea042017-08-10 15:37:28 -0700946 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
947 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700950}
951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700953 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800954}
955
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956bool Plural::Equals(const Value* value) const {
957 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 if (!other) {
959 return false;
960 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700961
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800962 auto one_iter = values.begin();
963 auto one_end_iter = values.end();
964 auto two_iter = other->values.begin();
965 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
966 const std::unique_ptr<Item>& a = *one_iter;
967 const std::unique_ptr<Item>& b = *two_iter;
968 if (a != nullptr && b != nullptr) {
969 if (!a->Equals(b.get())) {
970 return false;
971 }
972 } else if (a != b) {
973 return false;
974 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800976 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700977}
978
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 *out << "(plural)";
981 if (values[Zero]) {
982 *out << " zero=" << *values[Zero];
983 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700984
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 if (values[One]) {
986 *out << " one=" << *values[One];
987 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700988
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 if (values[Two]) {
990 *out << " two=" << *values[Two];
991 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700992
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 if (values[Few]) {
994 *out << " few=" << *values[Few];
995 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700996
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997 if (values[Many]) {
998 *out << " many=" << *values[Many];
999 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001000
1001 if (values[Other]) {
1002 *out << " other=" << *values[Other];
1003 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004}
1005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006bool Styleable::Equals(const Value* value) const {
1007 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008 if (!other) {
1009 return false;
1010 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001011
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 if (entries.size() != other->entries.size()) {
1013 return false;
1014 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001015
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1017 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001020}
1021
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001022void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001025}
1026
Ryan Mitchell326e35ff2021-04-12 07:50:42 -07001027bool Macro::Equals(const Value* value) const {
1028 const Macro* other = ValueCast<Macro>(value);
1029 if (!other) {
1030 return false;
1031 }
1032 return other->raw_value == raw_value && other->style_string.spans == style_string.spans &&
1033 other->style_string.str == style_string.str &&
1034 other->untranslatable_sections == untranslatable_sections &&
1035 other->alias_namespaces == alias_namespaces;
1036}
1037
1038void Macro::Print(std::ostream* out) const {
1039 *out << "(macro) ";
1040}
1041
Adam Lesinski8197cc462016-08-19 12:16:49 -07001042bool operator<(const Reference& a, const Reference& b) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001043 int cmp = a.name.value_or(ResourceName{}).compare(b.name.value_or(ResourceName{}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 if (cmp != 0) return cmp < 0;
1045 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001046}
1047
1048bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001050}
1051
1052bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001054}
1055
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001056struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 bool operator()(const Reference& a, const Reference& b) const {
1058 return a.name < b.name;
1059 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001060};
1061
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001064 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 std::set<Reference, NameOnlyComparator> references;
1066 references.insert(entries.begin(), entries.end());
1067 references.insert(other->entries.begin(), other->entries.end());
1068 entries.clear();
1069 entries.reserve(references.size());
1070 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001071}
1072
Ryan Mitchellefcdb952021-04-14 17:31:37 -07001073template <typename T>
1074std::unique_ptr<T> CopyValueFields(std::unique_ptr<T> new_value, const T* value) {
1075 new_value->SetSource(value->GetSource());
1076 new_value->SetComment(value->GetComment());
1077 return new_value;
1078}
1079
1080CloningValueTransformer::CloningValueTransformer(StringPool* new_pool)
1081 : ValueTransformer(new_pool) {
1082}
1083
1084std::unique_ptr<Reference> CloningValueTransformer::TransformDerived(const Reference* value) {
1085 return std::make_unique<Reference>(*value);
1086}
1087
1088std::unique_ptr<Id> CloningValueTransformer::TransformDerived(const Id* value) {
1089 return std::make_unique<Id>(*value);
1090}
1091
1092std::unique_ptr<RawString> CloningValueTransformer::TransformDerived(const RawString* value) {
1093 auto new_value = std::make_unique<RawString>(pool_->MakeRef(value->value));
1094 return CopyValueFields(std::move(new_value), value);
1095}
1096
1097std::unique_ptr<String> CloningValueTransformer::TransformDerived(const String* value) {
1098 auto new_value = std::make_unique<String>(pool_->MakeRef(value->value));
1099 new_value->untranslatable_sections = value->untranslatable_sections;
1100 return CopyValueFields(std::move(new_value), value);
1101}
1102
1103std::unique_ptr<StyledString> CloningValueTransformer::TransformDerived(const StyledString* value) {
1104 auto new_value = std::make_unique<StyledString>(pool_->MakeRef(value->value));
1105 new_value->untranslatable_sections = value->untranslatable_sections;
1106 return CopyValueFields(std::move(new_value), value);
1107}
1108
1109std::unique_ptr<FileReference> CloningValueTransformer::TransformDerived(
1110 const FileReference* value) {
1111 auto new_value = std::make_unique<FileReference>(pool_->MakeRef(value->path));
1112 new_value->file = value->file;
1113 new_value->type = value->type;
1114 return CopyValueFields(std::move(new_value), value);
1115}
1116
1117std::unique_ptr<BinaryPrimitive> CloningValueTransformer::TransformDerived(
1118 const BinaryPrimitive* value) {
1119 return std::make_unique<BinaryPrimitive>(*value);
1120}
1121
1122std::unique_ptr<Attribute> CloningValueTransformer::TransformDerived(const Attribute* value) {
1123 auto new_value = std::make_unique<Attribute>();
1124 new_value->type_mask = value->type_mask;
1125 new_value->min_int = value->min_int;
1126 new_value->max_int = value->max_int;
1127 for (const Attribute::Symbol& s : value->symbols) {
1128 new_value->symbols.emplace_back(Attribute::Symbol{
1129 .symbol = *s.symbol.Transform(*this),
1130 .value = s.value,
1131 .type = s.type,
1132 });
1133 }
1134 return CopyValueFields(std::move(new_value), value);
1135}
1136
1137std::unique_ptr<Style> CloningValueTransformer::TransformDerived(const Style* value) {
1138 auto new_value = std::make_unique<Style>();
1139 new_value->parent = value->parent;
1140 new_value->parent_inferred = value->parent_inferred;
1141 for (auto& entry : value->entries) {
1142 new_value->entries.push_back(Style::Entry{entry.key, entry.value->Transform(*this)});
1143 }
1144 return CopyValueFields(std::move(new_value), value);
1145}
1146
1147std::unique_ptr<Array> CloningValueTransformer::TransformDerived(const Array* value) {
1148 auto new_value = std::make_unique<Array>();
1149 for (auto& item : value->elements) {
1150 new_value->elements.emplace_back(item->Transform(*this));
1151 }
1152 return CopyValueFields(std::move(new_value), value);
1153}
1154
1155std::unique_ptr<Plural> CloningValueTransformer::TransformDerived(const Plural* value) {
1156 auto new_value = std::make_unique<Plural>();
1157 const size_t count = value->values.size();
1158 for (size_t i = 0; i < count; i++) {
1159 if (value->values[i]) {
1160 new_value->values[i] = value->values[i]->Transform(*this);
1161 }
1162 }
1163 return CopyValueFields(std::move(new_value), value);
1164}
1165
1166std::unique_ptr<Styleable> CloningValueTransformer::TransformDerived(const Styleable* value) {
1167 auto new_value = std::make_unique<Styleable>();
1168 for (const Reference& s : value->entries) {
1169 new_value->entries.emplace_back(*s.Transform(*this));
1170 }
1171 return CopyValueFields(std::move(new_value), value);
1172}
1173
Ryan Mitchell326e35ff2021-04-12 07:50:42 -07001174std::unique_ptr<Macro> CloningValueTransformer::TransformDerived(const Macro* value) {
1175 auto new_value = std::make_unique<Macro>(*value);
1176 return CopyValueFields(std::move(new_value), value);
1177}
1178
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001179} // namespace aapt