blob: 4f0fa8ae29ba203acbc0dfe97a604a0299376537 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceValues.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
Adam Lesinski93190b72017-11-03 15:20:17 -070020#include <cinttypes>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <limits>
22#include <set>
Adam Lesinski93190b72017-11-03 15:20:17 -070023#include <sstream>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski93190b72017-11-03 15:20:17 -070025#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "Resource.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080029#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070031#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070032
Adam Lesinski93190b72017-11-03 15:20:17 -070033using ::aapt::text::Printer;
34using ::android::StringPiece;
35using ::android::base::StringPrintf;
36
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037namespace aapt {
38
Adam Lesinski93190b72017-11-03 15:20:17 -070039void Value::PrettyPrint(Printer* printer) const {
40 std::ostringstream str_stream;
41 Print(&str_stream);
42 printer->Print(str_stream.str());
43}
44
Adam Lesinski5924d8c2017-05-30 15:15:58 -070045std::ostream& operator<<(std::ostream& out, const Value& value) {
46 value.Print(&out);
47 return out;
48}
49
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070051void BaseValue<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053}
54
55template <typename Derived>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070056void BaseValue<Derived>::Accept(ConstValueVisitor* visitor) const {
57 visitor->Visit(static_cast<const Derived*>(this));
58}
59
60template <typename Derived>
61void BaseItem<Derived>::Accept(ValueVisitor* visitor) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063}
64
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070065template <typename Derived>
66void BaseItem<Derived>::Accept(ConstValueVisitor* visitor) const {
67 visitor->Visit(static_cast<const Derived*>(this));
68}
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool RawString::Equals(const Value* value) const {
73 const RawString* other = ValueCast<RawString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 if (!other) {
75 return false;
76 }
77 return *this->value == *other->value;
Adam Lesinski458b8772016-04-25 14:20:21 -070078}
79
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080RawString* RawString::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -070081 RawString* rs = new RawString(new_pool->MakeRef(value));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 rs->comment_ = comment_;
83 rs->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087bool RawString::Flatten(android::Res_value* out_value) const {
88 out_value->dataType = android::Res_value::TYPE_STRING;
89 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091}
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093void RawString::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097Reference::Reference() : reference_type(Type::kResource) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099Reference::Reference(const ResourceNameRef& n, Type t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 : name(n.ToResourceName()), reference_type(t) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102Reference::Reference(const ResourceId& i, Type type)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 : id(i), reference_type(type) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108bool Reference::Equals(const Value* value) const {
109 const Reference* other = ValueCast<Reference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 if (!other) {
111 return false;
112 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 return reference_type == other->reference_type &&
114 private_reference == other->private_reference && id == other->id &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 name == other->name;
Adam Lesinski458b8772016-04-25 14:20:21 -0700116}
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118bool Reference::Flatten(android::Res_value* out_value) const {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800119 const ResourceId resid = id.value_or_default(ResourceId(0));
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800120 const bool dynamic = resid.is_valid() && is_dynamic;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800121
122 if (reference_type == Reference::Type::kResource) {
123 if (dynamic) {
124 out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
125 } else {
126 out_value->dataType = android::Res_value::TYPE_REFERENCE;
127 }
128 } else {
129 if (dynamic) {
130 out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
131 } else {
132 out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
133 }
134 }
135 out_value->data = util::HostToDevice32(resid.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137}
138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139Reference* Reference::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 return new Reference(*this);
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 {
195 printer->Print(to_string(name.type));
196 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 -0700223Id* Id::Clone(StringPool* /*new_pool*/) const {
224 return new Id(*this);
225}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski93190b72017-11-03 15:20:17 -0700227void Id::Print(std::ostream* out) const {
228 *out << "(id)";
229}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinski93190b72017-11-03 15:20:17 -0700231String::String(const StringPool::Ref& ref) : value(ref) {
232}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234bool String::Equals(const Value* value) const {
235 const String* other = ValueCast<String>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 if (!other) {
237 return false;
238 }
Adam Lesinski75421622017-01-06 15:20:04 -0800239
240 if (this->value != other->value) {
241 return false;
242 }
243
244 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
245 return false;
246 }
247
248 auto other_iter = other->untranslatable_sections.begin();
249 for (const UntranslatableSection& this_section : untranslatable_sections) {
250 if (this_section != *other_iter) {
251 return false;
252 }
253 ++other_iter;
254 }
255 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256}
257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258bool String::Flatten(android::Res_value* out_value) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 // Verify that our StringPool index is within encode-able limits.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 return false;
262 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 out_value->dataType = android::Res_value::TYPE_STRING;
265 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267}
268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269String* String::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700270 String* str = new String(new_pool->MakeRef(value));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 str->comment_ = comment_;
272 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800273 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275}
276
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277void String::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279}
280
Adam Lesinski93190b72017-11-03 15:20:17 -0700281void String::PrettyPrint(Printer* printer) const {
282 printer->Print("\"");
283 printer->Print(*value);
284 printer->Print("\"");
285}
286
287StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
288}
Adam Lesinski393b5f02015-12-17 13:03:11 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290bool StyledString::Equals(const Value* value) const {
291 const StyledString* other = ValueCast<StyledString>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 if (!other) {
Adam Lesinski458b8772016-04-25 14:20:21 -0700293 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 }
295
Adam Lesinski75421622017-01-06 15:20:04 -0800296 if (this->value != other->value) {
297 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 }
Adam Lesinski75421622017-01-06 15:20:04 -0800299
300 if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
301 return false;
302 }
303
304 auto other_iter = other->untranslatable_sections.begin();
305 for (const UntranslatableSection& this_section : untranslatable_sections) {
306 if (this_section != *other_iter) {
307 return false;
308 }
309 ++other_iter;
310 }
311 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314bool StyledString::Flatten(android::Res_value* out_value) const {
315 if (value.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return false;
317 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 out_value->dataType = android::Res_value::TYPE_STRING;
320 out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322}
323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324StyledString* StyledString::Clone(StringPool* new_pool) const {
325 StyledString* str = new StyledString(new_pool->MakeRef(value));
326 str->comment_ = comment_;
327 str->source_ = source_;
Adam Lesinski75421622017-01-06 15:20:04 -0800328 str->untranslatable_sections = untranslatable_sections;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330}
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332void StyledString::Print(std::ostream* out) const {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700333 *out << "(styled string) \"" << value->value << "\"";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 for (const StringPool::Span& span : value->spans) {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700335 *out << " " << *span.name << ":" << span.first_char << "," << span.last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
Adam Lesinski93190b72017-11-03 15:20:17 -0700339FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
340}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342bool FileReference::Equals(const Value* value) const {
343 const FileReference* other = ValueCast<FileReference>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 if (!other) {
345 return false;
346 }
Adam Lesinski75421622017-01-06 15:20:04 -0800347 return path == other->path;
Adam Lesinski458b8772016-04-25 14:20:21 -0700348}
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350bool FileReference::Flatten(android::Res_value* out_value) const {
351 if (path.index() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 return false;
353 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 out_value->dataType = android::Res_value::TYPE_STRING;
356 out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358}
359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360FileReference* FileReference::Clone(StringPool* new_pool) const {
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700361 FileReference* fr = new FileReference(new_pool->MakeRef(path));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 fr->file = file;
Adam Lesinski00451162017-10-03 07:44:08 -0700363 fr->type = type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 fr->comment_ = comment_;
365 fr->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367}
368
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369void FileReference::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 *out << "(file) " << *path;
Adam Lesinskia65bbdf2018-02-15 12:39:44 -0800371 switch (type) {
372 case ResourceFile::Type::kBinaryXml:
373 *out << " type=XML";
374 break;
375 case ResourceFile::Type::kProtoXml:
376 *out << " type=protoXML";
377 break;
378 case ResourceFile::Type::kPng:
379 *out << " type=PNG";
380 break;
381 default:
382 break;
383 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384}
385
Adam Lesinski93190b72017-11-03 15:20:17 -0700386BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
387}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 value.dataType = dataType;
391 value.data = data;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700392}
393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394bool BinaryPrimitive::Equals(const Value* value) const {
395 const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 if (!other) {
397 return false;
398 }
399 return this->value.dataType == other->value.dataType &&
400 this->value.data == other->value.data;
Adam Lesinski458b8772016-04-25 14:20:21 -0700401}
402
Adam Lesinski93190b72017-11-03 15:20:17 -0700403bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 out_value->dataType = value.dataType;
405 out_value->data = util::HostToDevice32(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407}
408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411}
412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413void BinaryPrimitive::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700414 *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data);
415}
416
417static std::string ComplexToString(uint32_t complex_value, bool fraction) {
418 using ::android::Res_value;
419
420 constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}};
421
422 // Determine the radix that was used.
423 const uint32_t radix =
424 (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK;
425 const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) &
426 Res_value::COMPLEX_MANTISSA_MASK}
427 << kRadixShifts[radix];
428 const float value = mantissa * (1.0f / (1 << 23));
429
430 std::string str = StringPrintf("%f", value);
431
432 const int unit_type =
433 (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK;
434 if (fraction) {
435 switch (unit_type) {
436 case Res_value::COMPLEX_UNIT_FRACTION:
437 str += "%";
438 break;
439 case Res_value::COMPLEX_UNIT_FRACTION_PARENT:
440 str += "%p";
441 break;
442 default:
443 str += "???";
444 break;
445 }
446 } else {
447 switch (unit_type) {
448 case Res_value::COMPLEX_UNIT_PX:
449 str += "px";
450 break;
451 case Res_value::COMPLEX_UNIT_DIP:
452 str += "dp";
453 break;
454 case Res_value::COMPLEX_UNIT_SP:
455 str += "sp";
456 break;
457 case Res_value::COMPLEX_UNIT_PT:
458 str += "pt";
459 break;
460 case Res_value::COMPLEX_UNIT_IN:
461 str += "in";
462 break;
463 case Res_value::COMPLEX_UNIT_MM:
464 str += "mm";
465 break;
466 default:
467 str += "???";
468 break;
469 }
470 }
471 return str;
472}
473
474void BinaryPrimitive::PrettyPrint(Printer* printer) const {
475 using ::android::Res_value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 switch (value.dataType) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700477 case Res_value::TYPE_NULL:
478 if (value.data == Res_value::DATA_NULL_EMPTY) {
479 printer->Print("@empty");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700480 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700481 printer->Print("@null");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700482 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700484
485 case Res_value::TYPE_INT_DEC:
486 printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700488
489 case Res_value::TYPE_INT_HEX:
490 printer->Print(StringPrintf("0x%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700492
493 case Res_value::TYPE_INT_BOOLEAN:
494 printer->Print(value.data != 0 ? "true" : "false");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700496
497 case Res_value::TYPE_INT_COLOR_ARGB8:
498 case Res_value::TYPE_INT_COLOR_RGB8:
499 case Res_value::TYPE_INT_COLOR_ARGB4:
500 case Res_value::TYPE_INT_COLOR_RGB4:
501 printer->Print(StringPrintf("#%08x", value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 break;
Adam Lesinski93190b72017-11-03 15:20:17 -0700503
504 case Res_value::TYPE_FLOAT:
505 printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data)));
506 break;
507
508 case Res_value::TYPE_DIMENSION:
509 printer->Print(ComplexToString(value.data, false /*fraction*/));
510 break;
511
512 case Res_value::TYPE_FRACTION:
513 printer->Print(ComplexToString(value.data, true /*fraction*/));
514 break;
515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700517 printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 break;
519 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520}
521
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800522Attribute::Attribute(uint32_t t)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 : type_mask(t),
524 min_int(std::numeric_limits<int32_t>::min()),
525 max_int(std::numeric_limits<int32_t>::max()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800526}
527
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700528std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) {
529 if (s.symbol.name) {
530 out << s.symbol.name.value().entry;
531 } else {
532 out << "???";
533 }
534 return out << "=" << s.value;
535}
536
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700537template <typename T>
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800538constexpr T* add_pointer(T& val) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 return &val;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700540}
541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542bool Attribute::Equals(const Value* value) const {
543 const Attribute* other = ValueCast<Attribute>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 if (!other) {
545 return false;
546 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700547
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 if (symbols.size() != other->symbols.size()) {
549 return false;
550 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700551
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700552 if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 return false;
554 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 std::vector<const Symbol*> sorted_a;
557 std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800558 add_pointer<const Symbol>);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700559 std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool {
560 return a->symbol.name < b->symbol.name;
561 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700562
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563 std::vector<const Symbol*> sorted_b;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700564 std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b),
565 add_pointer<const Symbol>);
566 std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool {
567 return a->symbol.name < b->symbol.name;
568 });
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700569
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 [](const Symbol* a, const Symbol* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700572 return a->symbol.Equals(&b->symbol) && a->value == b->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700574}
575
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800576bool Attribute::IsCompatibleWith(const Attribute& attr) const {
577 // If the high bits are set on any of these attribute type masks, then they are incompatible.
578 // We don't check that flags and enums are identical.
579 if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 ||
580 (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) {
581 return false;
582 }
583
584 // Every attribute accepts a reference.
585 uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
586 uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE;
587 return this_type_mask == that_type_mask;
588}
589
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800592}
593
Adam Lesinski93190b72017-11-03 15:20:17 -0700594std::string Attribute::MaskString() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 if (type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700596 return "any";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800598
Adam Lesinski93190b72017-11-03 15:20:17 -0700599 std::ostringstream out;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 bool set = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 if (!set) {
603 set = true;
604 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700605 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700607 out << "reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 if (!set) {
612 set = true;
613 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700614 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700616 out << "string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 if (!set) {
621 set = true;
622 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700623 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700625 out << "integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 if (!set) {
630 set = true;
631 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700632 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700634 out << "boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 if (!set) {
639 set = true;
640 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700641 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800642 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700643 out << "color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800645
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 if (!set) {
648 set = true;
649 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700650 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800651 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700652 out << "float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 if (!set) {
657 set = true;
658 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700659 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800660 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700661 out << "dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 if (!set) {
666 set = true;
667 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700668 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800669 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700670 out << "fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 if (!set) {
675 set = true;
676 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700677 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800678 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700679 out << "enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 if (!set) {
684 set = true;
685 } else {
Adam Lesinski93190b72017-11-03 15:20:17 -0700686 out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800687 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700688 out << "flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700690 return out.str();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700691}
692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693void Attribute::Print(std::ostream* out) const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700694 *out << "(attr) " << MaskString();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800695
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 if (!symbols.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 *out << " [" << util::Joiner(symbols, ", ") << "]";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800699
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 if (min_int != std::numeric_limits<int32_t>::min()) {
701 *out << " min=" << min_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700703
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 if (max_int != std::numeric_limits<int32_t>::max()) {
705 *out << " max=" << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700707
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 if (IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 *out << " [weak]";
710 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800711}
712
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700713static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value,
714 DiagMessage* out_msg) {
715 *out_msg << "expected";
716 if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) {
717 *out_msg << " boolean";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800719
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700720 if (attr.type_mask & android::ResTable_map::TYPE_COLOR) {
721 *out_msg << " color";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800723
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700724 if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) {
725 *out_msg << " dimension";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800727
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700728 if (attr.type_mask & android::ResTable_map::TYPE_ENUM) {
729 *out_msg << " enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800731
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700732 if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) {
733 *out_msg << " flags";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800735
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700736 if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) {
737 *out_msg << " float";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800739
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700740 if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) {
741 *out_msg << " fraction";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800743
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700744 if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) {
745 *out_msg << " integer";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800747
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700748 if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) {
749 *out_msg << " reference";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800751
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700752 if (attr.type_mask & android::ResTable_map::TYPE_STRING) {
753 *out_msg << " string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800755
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700756 *out_msg << " but got " << value;
Adam Lesinskia5870652015-11-20 15:32:30 -0800757}
758
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700759bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const {
760 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
761 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
762 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
763 constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE;
764
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 android::Res_value val = {};
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700766 item.Flatten(&val);
767
768 const uint32_t flattened_data = util::DeviceToHost32(val.data);
Adam Lesinskia5870652015-11-20 15:32:30 -0800769
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 // Always allow references.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700771 const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType);
772
773 // Only one type must match between the actual and expected.
774 if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700776 BuildAttributeMismatchMessage(*this, item, out_msg);
Adam Lesinskia5870652015-11-20 15:32:30 -0800777 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700779 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700781 // Enums and flags are encoded as integers, so check them first before doing any range checks.
782 if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) {
783 for (const Symbol& s : symbols) {
784 if (flattened_data == s.value) {
785 return true;
786 }
787 }
788
789 // If the attribute accepts integers, we can't fail here.
790 if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700792 *out_msg << item << " is not a valid enum";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794 return false;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700795 }
796 }
797
798 if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) {
799 uint32_t mask = 0u;
800 for (const Symbol& s : symbols) {
801 mask |= s.value;
802 }
803
804 // Check if the flattened data is covered by the flag bit mask.
805 // If the attribute accepts integers, we can't fail here.
806 if ((mask & flattened_data) == flattened_data) {
807 return true;
808 } else if ((type_mask & TYPE_INTEGER) == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 if (out_msg) {
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700810 *out_msg << item << " is not a valid flag";
811 }
812 return false;
813 }
814 }
815
816 // Finally check the integer range of the value.
817 if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) {
818 if (static_cast<int32_t>(flattened_data) < min_int) {
819 if (out_msg) {
820 *out_msg << item << " is less than minimum integer " << min_int;
821 }
822 return false;
823 } else if (static_cast<int32_t>(flattened_data) > max_int) {
824 if (out_msg) {
825 *out_msg << item << " is greater than maximum integer " << max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 }
827 return false;
828 }
829 }
830 return true;
Adam Lesinskia5870652015-11-20 15:32:30 -0800831}
832
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700833std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) {
834 if (entry.key.name) {
835 out << entry.key.name.value();
836 } else if (entry.key.id) {
837 out << entry.key.id.value();
838 } else {
839 out << "???";
840 }
841 out << " = " << entry.value;
842 return out;
843}
844
845template <typename T>
846std::vector<T*> ToPointerVec(std::vector<T>& src) {
847 std::vector<T*> dst;
848 dst.reserve(src.size());
849 for (T& in : src) {
850 dst.push_back(&in);
851 }
852 return dst;
853}
854
855template <typename T>
856std::vector<const T*> ToPointerVec(const std::vector<T>& src) {
857 std::vector<const T*> dst;
858 dst.reserve(src.size());
859 for (const T& in : src) {
860 dst.push_back(&in);
861 }
862 return dst;
863}
864
865static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) {
866 return a->key.name < b->key.name;
867}
868
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869bool Style::Equals(const Value* value) const {
870 const Style* other = ValueCast<Style>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700871 if (!other) {
872 return false;
873 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700874
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 if (bool(parent) != bool(other->parent) ||
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700876 (parent && other->parent && !parent.value().Equals(&other->parent.value()))) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 return false;
878 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700879
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 if (entries.size() != other->entries.size()) {
881 return false;
882 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700883
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700884 std::vector<const Entry*> sorted_a = ToPointerVec(entries);
885 std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700886
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700887 std::vector<const Entry*> sorted_b = ToPointerVec(other->entries);
888 std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator);
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700889
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700890 return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 [](const Entry* a, const Entry* b) -> bool {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700892 return a->key.Equals(&b->key) && a->value->Equals(b->value.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700894}
895
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896Style* Style::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 Style* style = new Style();
898 style->parent = parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 style->parent_inferred = parent_inferred;
900 style->comment_ = comment_;
901 style->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 for (auto& entry : entries) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700903 style->entries.push_back(Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 }
905 return style;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906}
907
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700908void Style::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 *out << "(style) ";
910 if (parent && parent.value().name) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700911 const Reference& parent_ref = parent.value();
912 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913 *out << "*";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800914 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700915 *out << parent_ref.name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 *out << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918}
919
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700920Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) {
921 Style::Entry cloned_entry{entry.key};
922 if (entry.value != nullptr) {
923 cloned_entry.value.reset(entry.value->Clone(pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700925 return cloned_entry;
926}
927
928void Style::MergeWith(Style* other, StringPool* pool) {
929 if (other->parent) {
930 parent = other->parent;
931 }
932
933 // We can't assume that the entries are sorted alphabetically since they're supposed to be
934 // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge
935 // them keying off that.
936 //
937 // Instead, sort the entries of each Style by their name in a separate structure. Then merge
938 // those.
939
940 std::vector<Entry*> this_sorted = ToPointerVec(entries);
941 std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator);
942
943 std::vector<Entry*> other_sorted = ToPointerVec(other->entries);
944 std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator);
945
946 auto this_iter = this_sorted.begin();
947 const auto this_end = this_sorted.end();
948
949 auto other_iter = other_sorted.begin();
950 const auto other_end = other_sorted.end();
951
952 std::vector<Entry> merged_entries;
953 while (this_iter != this_end) {
954 if (other_iter != other_end) {
955 if ((*this_iter)->key.name < (*other_iter)->key.name) {
956 merged_entries.push_back(std::move(**this_iter));
957 ++this_iter;
958 } else {
959 // The other overrides.
960 merged_entries.push_back(CloneEntry(**other_iter, pool));
961 if ((*this_iter)->key.name == (*other_iter)->key.name) {
962 ++this_iter;
963 }
964 ++other_iter;
965 }
966 } else {
967 merged_entries.push_back(std::move(**this_iter));
968 ++this_iter;
969 }
970 }
971
972 while (other_iter != other_end) {
973 merged_entries.push_back(CloneEntry(**other_iter, pool));
974 ++other_iter;
975 }
976
977 entries = std::move(merged_entries);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978}
979
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980bool Array::Equals(const Value* value) const {
981 const Array* other = ValueCast<Array>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 if (!other) {
983 return false;
984 }
Adam Lesinski458b8772016-04-25 14:20:21 -0700985
Adam Lesinski4ffea042017-08-10 15:37:28 -0700986 if (elements.size() != other->elements.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 return false;
988 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700989
Adam Lesinski4ffea042017-08-10 15:37:28 -0700990 return std::equal(elements.begin(), elements.end(), other->elements.begin(),
991 [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 return a->Equals(b.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 });
Adam Lesinski458b8772016-04-25 14:20:21 -0700994}
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996Array* Array::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997 Array* array = new Array();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 array->comment_ = comment_;
999 array->source_ = source_;
Adam Lesinski4ffea042017-08-10 15:37:28 -07001000 for (auto& item : elements) {
1001 array->elements.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 }
1003 return array;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004}
1005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006void Array::Print(std::ostream* out) const {
Adam Lesinski4ffea042017-08-10 15:37:28 -07001007 *out << "(array) [" << util::Joiner(elements, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001008}
1009
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010bool Plural::Equals(const Value* value) const {
1011 const Plural* other = ValueCast<Plural>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 if (!other) {
1013 return false;
1014 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001015
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001016 auto one_iter = values.begin();
1017 auto one_end_iter = values.end();
1018 auto two_iter = other->values.begin();
1019 for (; one_iter != one_end_iter; ++one_iter, ++two_iter) {
1020 const std::unique_ptr<Item>& a = *one_iter;
1021 const std::unique_ptr<Item>& b = *two_iter;
1022 if (a != nullptr && b != nullptr) {
1023 if (!a->Equals(b.get())) {
1024 return false;
1025 }
1026 } else if (a != b) {
1027 return false;
1028 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001030 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -07001031}
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033Plural* Plural::Clone(StringPool* new_pool) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 Plural* p = new Plural();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 p->comment_ = comment_;
1036 p->source_ = source_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 const size_t count = values.size();
1038 for (size_t i = 0; i < count; i++) {
1039 if (values[i]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001041 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001042 }
1043 return p;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001044}
1045
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046void Plural::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 *out << "(plural)";
1048 if (values[Zero]) {
1049 *out << " zero=" << *values[Zero];
1050 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001051
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052 if (values[One]) {
1053 *out << " one=" << *values[One];
1054 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001055
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 if (values[Two]) {
1057 *out << " two=" << *values[Two];
1058 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001059
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 if (values[Few]) {
1061 *out << " few=" << *values[Few];
1062 }
Adam Lesinski458b8772016-04-25 14:20:21 -07001063
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 if (values[Many]) {
1065 *out << " many=" << *values[Many];
1066 }
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001067
1068 if (values[Other]) {
1069 *out << " other=" << *values[Other];
1070 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001071}
1072
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073bool Styleable::Equals(const Value* value) const {
1074 const Styleable* other = ValueCast<Styleable>(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 if (!other) {
1076 return false;
1077 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001078
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001079 if (entries.size() != other->entries.size()) {
1080 return false;
1081 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -07001082
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 return std::equal(entries.begin(), entries.end(), other->entries.begin(),
1084 [](const Reference& a, const Reference& b) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 return a.Equals(&b);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001086 });
Adam Lesinski458b8772016-04-25 14:20:21 -07001087}
1088
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001089Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001091}
1092
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093void Styleable::Print(std::ostream* out) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094 *out << "(styleable) "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 << " [" << util::Joiner(entries, ", ") << "]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001096}
1097
Adam Lesinski8197cc462016-08-19 12:16:49 -07001098bool operator<(const Reference& a, const Reference& b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099 int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 if (cmp != 0) return cmp < 0;
1101 return a.id < b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001102}
1103
1104bool operator==(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001105 return a.name == b.name && a.id == b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001106}
1107
1108bool operator!=(const Reference& a, const Reference& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 return a.name != b.name || a.id != b.id;
Adam Lesinski8197cc462016-08-19 12:16:49 -07001110}
1111
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001112struct NameOnlyComparator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 bool operator()(const Reference& a, const Reference& b) const {
1114 return a.name < b.name;
1115 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -07001116};
1117
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001118void Styleable::MergeWith(Styleable* other) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001119 // Compare only names, because some References may already have their IDs
Adam Lesinski5924d8c2017-05-30 15:15:58 -07001120 // assigned (framework IDs that don't change).
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 std::set<Reference, NameOnlyComparator> references;
1122 references.insert(entries.begin(), entries.end());
1123 references.insert(other->entries.begin(), other->entries.end());
1124 entries.clear();
1125 entries.reserve(references.size());
1126 entries.insert(entries.end(), references.begin(), references.end());
Adam Lesinski8197cc462016-08-19 12:16:49 -07001127}
1128
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129} // namespace aapt