Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "ResourceValues.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 20 | #include <cinttypes> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include <limits> |
| 22 | #include <set> |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 23 | #include <sstream> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
| 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | #include "Resource.h" |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 29 | #include "ResourceUtils.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "ValueVisitor.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 31 | #include "util/Util.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 33 | using ::aapt::text::Printer; |
| 34 | using ::android::StringPiece; |
| 35 | using ::android::base::StringPrintf; |
| 36 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | namespace aapt { |
| 38 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 39 | void Value::PrettyPrint(Printer* printer) const { |
| 40 | std::ostringstream str_stream; |
| 41 | Print(&str_stream); |
| 42 | printer->Print(str_stream.str()); |
| 43 | } |
| 44 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 45 | std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 46 | value.Print(&out); |
| 47 | return out; |
| 48 | } |
| 49 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 50 | std::unique_ptr<Value> Value::Transform(ValueTransformer& transformer) const { |
| 51 | return std::unique_ptr<Value>(this->TransformValueImpl(transformer)); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<Item> Item::Transform(ValueTransformer& transformer) const { |
| 55 | return std::unique_ptr<Item>(this->TransformItemImpl(transformer)); |
| 56 | } |
| 57 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | template <typename Derived> |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 59 | void BaseValue<Derived>::Accept(ValueVisitor* visitor) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | visitor->Visit(static_cast<Derived*>(this)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | template <typename Derived> |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 64 | void BaseValue<Derived>::Accept(ConstValueVisitor* visitor) const { |
| 65 | visitor->Visit(static_cast<const Derived*>(this)); |
| 66 | } |
| 67 | |
| 68 | template <typename Derived> |
| 69 | void BaseItem<Derived>::Accept(ValueVisitor* visitor) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | visitor->Visit(static_cast<Derived*>(this)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 73 | template <typename Derived> |
| 74 | void BaseItem<Derived>::Accept(ConstValueVisitor* visitor) const { |
| 75 | visitor->Visit(static_cast<const Derived*>(this)); |
| 76 | } |
| 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | RawString::RawString(const StringPool::Ref& ref) : value(ref) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | bool RawString::Equals(const Value* value) const { |
| 81 | const RawString* other = ValueCast<RawString>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | if (!other) { |
| 83 | return false; |
| 84 | } |
| 85 | return *this->value == *other->value; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | bool 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | void RawString::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | *out << "(raw string) " << *value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | Reference::Reference() : reference_type(Type::kResource) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | Reference::Reference(const ResourceNameRef& n, Type t) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | : name(n.ToResourceName()), reference_type(t) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | Reference::Reference(const ResourceId& i, Type type) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | : id(i), reference_type(type) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | Reference::Reference(const ResourceNameRef& n, const ResourceId& i) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {} |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | bool Reference::Equals(const Value* value) const { |
| 110 | const Reference* other = ValueCast<Reference>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | if (!other) { |
| 112 | return false; |
| 113 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | return reference_type == other->reference_type && |
| 115 | private_reference == other->private_reference && id == other->id && |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | name == other->name; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | bool Reference::Flatten(android::Res_value* out_value) const { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 120 | const ResourceId resid = id.value_or_default(ResourceId(0)); |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 121 | const bool dynamic = resid.is_valid() && is_dynamic; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 122 | |
| 123 | if (reference_type == Reference::Type::kResource) { |
| 124 | if (dynamic) { |
| 125 | out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE; |
| 126 | } else { |
| 127 | out_value->dataType = android::Res_value::TYPE_REFERENCE; |
| 128 | } |
| 129 | } else { |
| 130 | if (dynamic) { |
| 131 | out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE; |
| 132 | } else { |
| 133 | out_value->dataType = android::Res_value::TYPE_ATTRIBUTE; |
| 134 | } |
| 135 | } |
| 136 | out_value->data = util::HostToDevice32(resid.id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | void Reference::Print(std::ostream* out) const { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 141 | if (reference_type == Type::kResource) { |
| 142 | *out << "(reference) @"; |
| 143 | if (!name && !id) { |
| 144 | *out << "null"; |
| 145 | return; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 146 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | } else { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 148 | *out << "(attr-reference) ?"; |
| 149 | } |
| 150 | |
| 151 | if (private_reference) { |
| 152 | *out << "*"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | if (name) { |
| 156 | *out << name.value(); |
| 157 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 159 | if (id && id.value().is_valid()) { |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 160 | if (name) { |
| 161 | *out << " "; |
| 162 | } |
| 163 | *out << id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 167 | static void PrettyPrintReferenceImpl(const Reference& ref, bool print_package, Printer* printer) { |
| 168 | switch (ref.reference_type) { |
| 169 | case Reference::Type::kResource: |
| 170 | printer->Print("@"); |
| 171 | break; |
| 172 | |
| 173 | case Reference::Type::kAttribute: |
| 174 | printer->Print("?"); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | if (!ref.name && !ref.id) { |
| 179 | printer->Print("null"); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (ref.private_reference) { |
| 184 | printer->Print("*"); |
| 185 | } |
| 186 | |
| 187 | if (ref.name) { |
| 188 | const ResourceName& name = ref.name.value(); |
| 189 | if (print_package) { |
| 190 | printer->Print(name.to_string()); |
| 191 | } else { |
| 192 | printer->Print(to_string(name.type)); |
| 193 | printer->Print("/"); |
| 194 | printer->Print(name.entry); |
| 195 | } |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 196 | } else if (ref.id && ref.id.value().is_valid()) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 197 | printer->Print(ref.id.value().to_string()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void Reference::PrettyPrint(Printer* printer) const { |
| 202 | PrettyPrintReferenceImpl(*this, true /*print_package*/, printer); |
| 203 | } |
| 204 | |
| 205 | void Reference::PrettyPrint(const StringPiece& package, Printer* printer) const { |
| 206 | const bool print_package = name ? package != name.value().package : true; |
| 207 | PrettyPrintReferenceImpl(*this, print_package, printer); |
| 208 | } |
| 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | bool Id::Equals(const Value* value) const { |
| 211 | return ValueCast<Id>(value) != nullptr; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | bool Id::Flatten(android::Res_value* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | out->dataType = android::Res_value::TYPE_INT_BOOLEAN; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 216 | out->data = util::HostToDevice32(0); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 220 | void Id::Print(std::ostream* out) const { |
| 221 | *out << "(id)"; |
| 222 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 224 | String::String(const StringPool::Ref& ref) : value(ref) { |
| 225 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 226 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | bool String::Equals(const Value* value) const { |
| 228 | const String* other = ValueCast<String>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | if (!other) { |
| 230 | return false; |
| 231 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 232 | |
| 233 | if (this->value != other->value) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | if (untranslatable_sections.size() != other->untranslatable_sections.size()) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | auto other_iter = other->untranslatable_sections.begin(); |
| 242 | for (const UntranslatableSection& this_section : untranslatable_sections) { |
| 243 | if (this_section != *other_iter) { |
| 244 | return false; |
| 245 | } |
| 246 | ++other_iter; |
| 247 | } |
| 248 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | bool String::Flatten(android::Res_value* out_value) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | // Verify that our StringPool index is within encode-able limits. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | if (value.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | return false; |
| 255 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 258 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | void String::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | *out << "(string) \"" << *value << "\""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 266 | void String::PrettyPrint(Printer* printer) const { |
| 267 | printer->Print("\""); |
| 268 | printer->Print(*value); |
| 269 | printer->Print("\""); |
| 270 | } |
| 271 | |
| 272 | StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) { |
| 273 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 274 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | bool StyledString::Equals(const Value* value) const { |
| 276 | const StyledString* other = ValueCast<StyledString>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | if (!other) { |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 278 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 281 | if (this->value != other->value) { |
| 282 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 284 | |
| 285 | if (untranslatable_sections.size() != other->untranslatable_sections.size()) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | auto other_iter = other->untranslatable_sections.begin(); |
| 290 | for (const UntranslatableSection& this_section : untranslatable_sections) { |
| 291 | if (this_section != *other_iter) { |
| 292 | return false; |
| 293 | } |
| 294 | ++other_iter; |
| 295 | } |
| 296 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | bool StyledString::Flatten(android::Res_value* out_value) const { |
| 300 | if (value.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | return false; |
| 302 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 303 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 304 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 305 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | void StyledString::Print(std::ostream* out) const { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 310 | *out << "(styled string) \"" << value->value << "\""; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | for (const StringPool::Span& span : value->spans) { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 312 | *out << " " << *span.name << ":" << span.first_char << "," << span.last_char; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 313 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 316 | FileReference::FileReference(const StringPool::Ref& _path) : path(_path) { |
| 317 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 318 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | bool FileReference::Equals(const Value* value) const { |
| 320 | const FileReference* other = ValueCast<FileReference>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 321 | if (!other) { |
| 322 | return false; |
| 323 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 324 | return path == other->path; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 327 | bool FileReference::Flatten(android::Res_value* out_value) const { |
| 328 | if (path.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | return false; |
| 330 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 333 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | void FileReference::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 338 | *out << "(file) " << *path; |
Adam Lesinski | a65bbdf | 2018-02-15 12:39:44 -0800 | [diff] [blame] | 339 | switch (type) { |
| 340 | case ResourceFile::Type::kBinaryXml: |
| 341 | *out << " type=XML"; |
| 342 | break; |
| 343 | case ResourceFile::Type::kProtoXml: |
| 344 | *out << " type=protoXML"; |
| 345 | break; |
| 346 | case ResourceFile::Type::kPng: |
| 347 | *out << " type=PNG"; |
| 348 | break; |
| 349 | default: |
| 350 | break; |
| 351 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 354 | BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) { |
| 355 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 356 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 357 | BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | value.dataType = dataType; |
| 359 | value.data = data; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | bool BinaryPrimitive::Equals(const Value* value) const { |
| 363 | const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | if (!other) { |
| 365 | return false; |
| 366 | } |
| 367 | return this->value.dataType == other->value.dataType && |
| 368 | this->value.data == other->value.data; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 371 | bool BinaryPrimitive::Flatten(::android::Res_value* out_value) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | out_value->dataType = value.dataType; |
| 373 | out_value->data = util::HostToDevice32(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 377 | void BinaryPrimitive::Print(std::ostream* out) const { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 378 | *out << StringPrintf("(primitive) type=0x%02x data=0x%08x", value.dataType, value.data); |
| 379 | } |
| 380 | |
| 381 | static std::string ComplexToString(uint32_t complex_value, bool fraction) { |
| 382 | using ::android::Res_value; |
| 383 | |
| 384 | constexpr std::array<int, 4> kRadixShifts = {{23, 16, 8, 0}}; |
| 385 | |
| 386 | // Determine the radix that was used. |
| 387 | const uint32_t radix = |
| 388 | (complex_value >> Res_value::COMPLEX_RADIX_SHIFT) & Res_value::COMPLEX_RADIX_MASK; |
| 389 | const uint64_t mantissa = uint64_t{(complex_value >> Res_value::COMPLEX_MANTISSA_SHIFT) & |
| 390 | Res_value::COMPLEX_MANTISSA_MASK} |
| 391 | << kRadixShifts[radix]; |
| 392 | const float value = mantissa * (1.0f / (1 << 23)); |
| 393 | |
| 394 | std::string str = StringPrintf("%f", value); |
| 395 | |
| 396 | const int unit_type = |
| 397 | (complex_value >> Res_value::COMPLEX_UNIT_SHIFT) & Res_value::COMPLEX_UNIT_MASK; |
| 398 | if (fraction) { |
| 399 | switch (unit_type) { |
| 400 | case Res_value::COMPLEX_UNIT_FRACTION: |
| 401 | str += "%"; |
| 402 | break; |
| 403 | case Res_value::COMPLEX_UNIT_FRACTION_PARENT: |
| 404 | str += "%p"; |
| 405 | break; |
| 406 | default: |
| 407 | str += "???"; |
| 408 | break; |
| 409 | } |
| 410 | } else { |
| 411 | switch (unit_type) { |
| 412 | case Res_value::COMPLEX_UNIT_PX: |
| 413 | str += "px"; |
| 414 | break; |
| 415 | case Res_value::COMPLEX_UNIT_DIP: |
| 416 | str += "dp"; |
| 417 | break; |
| 418 | case Res_value::COMPLEX_UNIT_SP: |
| 419 | str += "sp"; |
| 420 | break; |
| 421 | case Res_value::COMPLEX_UNIT_PT: |
| 422 | str += "pt"; |
| 423 | break; |
| 424 | case Res_value::COMPLEX_UNIT_IN: |
| 425 | str += "in"; |
| 426 | break; |
| 427 | case Res_value::COMPLEX_UNIT_MM: |
| 428 | str += "mm"; |
| 429 | break; |
| 430 | default: |
| 431 | str += "???"; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | return str; |
| 436 | } |
| 437 | |
| 438 | void BinaryPrimitive::PrettyPrint(Printer* printer) const { |
| 439 | using ::android::Res_value; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 440 | switch (value.dataType) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 441 | case Res_value::TYPE_NULL: |
| 442 | if (value.data == Res_value::DATA_NULL_EMPTY) { |
| 443 | printer->Print("@empty"); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 444 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 445 | printer->Print("@null"); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 446 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 447 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 448 | |
| 449 | case Res_value::TYPE_INT_DEC: |
| 450 | printer->Print(StringPrintf("%" PRIi32, static_cast<int32_t>(value.data))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 452 | |
| 453 | case Res_value::TYPE_INT_HEX: |
| 454 | printer->Print(StringPrintf("0x%08x", value.data)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 455 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 456 | |
| 457 | case Res_value::TYPE_INT_BOOLEAN: |
| 458 | printer->Print(value.data != 0 ? "true" : "false"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 459 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 460 | |
| 461 | case Res_value::TYPE_INT_COLOR_ARGB8: |
| 462 | case Res_value::TYPE_INT_COLOR_RGB8: |
| 463 | case Res_value::TYPE_INT_COLOR_ARGB4: |
| 464 | case Res_value::TYPE_INT_COLOR_RGB4: |
| 465 | printer->Print(StringPrintf("#%08x", value.data)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 466 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 467 | |
| 468 | case Res_value::TYPE_FLOAT: |
| 469 | printer->Print(StringPrintf("%g", *reinterpret_cast<const float*>(&value.data))); |
| 470 | break; |
| 471 | |
| 472 | case Res_value::TYPE_DIMENSION: |
| 473 | printer->Print(ComplexToString(value.data, false /*fraction*/)); |
| 474 | break; |
| 475 | |
| 476 | case Res_value::TYPE_FRACTION: |
| 477 | printer->Print(ComplexToString(value.data, true /*fraction*/)); |
| 478 | break; |
| 479 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | default: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 481 | printer->Print(StringPrintf("(unknown 0x%02x) 0x%08x", value.dataType, value.data)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 482 | break; |
| 483 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 486 | Attribute::Attribute(uint32_t t) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | : type_mask(t), |
| 488 | min_int(std::numeric_limits<int32_t>::min()), |
| 489 | max_int(std::numeric_limits<int32_t>::max()) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 492 | std::ostream& operator<<(std::ostream& out, const Attribute::Symbol& s) { |
| 493 | if (s.symbol.name) { |
| 494 | out << s.symbol.name.value().entry; |
| 495 | } else { |
| 496 | out << "???"; |
| 497 | } |
| 498 | return out << "=" << s.value; |
| 499 | } |
| 500 | |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 501 | template <typename T> |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 502 | constexpr T* add_pointer(T& val) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | return &val; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 506 | bool Attribute::Equals(const Value* value) const { |
| 507 | const Attribute* other = ValueCast<Attribute>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 508 | if (!other) { |
| 509 | return false; |
| 510 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | if (symbols.size() != other->symbols.size()) { |
| 513 | return false; |
| 514 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 515 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 516 | if (type_mask != other->type_mask || min_int != other->min_int || max_int != other->max_int) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 517 | return false; |
| 518 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 519 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | std::vector<const Symbol*> sorted_a; |
| 521 | std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a), |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 522 | add_pointer<const Symbol>); |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 523 | std::sort(sorted_a.begin(), sorted_a.end(), [](const Symbol* a, const Symbol* b) -> bool { |
| 524 | return a->symbol.name < b->symbol.name; |
| 525 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 526 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 527 | std::vector<const Symbol*> sorted_b; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 528 | std::transform(other->symbols.begin(), other->symbols.end(), std::back_inserter(sorted_b), |
| 529 | add_pointer<const Symbol>); |
| 530 | std::sort(sorted_b.begin(), sorted_b.end(), [](const Symbol* a, const Symbol* b) -> bool { |
| 531 | return a->symbol.name < b->symbol.name; |
| 532 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 533 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 535 | [](const Symbol* a, const Symbol* b) -> bool { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 536 | return a->symbol.Equals(&b->symbol) && a->value == b->value; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 537 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 540 | bool Attribute::IsCompatibleWith(const Attribute& attr) const { |
| 541 | // If the high bits are set on any of these attribute type masks, then they are incompatible. |
| 542 | // We don't check that flags and enums are identical. |
| 543 | if ((type_mask & ~android::ResTable_map::TYPE_ANY) != 0 || |
| 544 | (attr.type_mask & ~android::ResTable_map::TYPE_ANY) != 0) { |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | // Every attribute accepts a reference. |
| 549 | uint32_t this_type_mask = type_mask | android::ResTable_map::TYPE_REFERENCE; |
| 550 | uint32_t that_type_mask = attr.type_mask | android::ResTable_map::TYPE_REFERENCE; |
| 551 | return this_type_mask == that_type_mask; |
| 552 | } |
| 553 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 554 | std::string Attribute::MaskString() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 555 | if (type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 556 | return "any"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 557 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 558 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 559 | std::ostringstream out; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 560 | bool set = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 561 | if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 562 | if (!set) { |
| 563 | set = true; |
| 564 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 565 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 566 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 567 | out << "reference"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 568 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 569 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 570 | if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 571 | if (!set) { |
| 572 | set = true; |
| 573 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 574 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 575 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 576 | out << "string"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 577 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 578 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | if (!set) { |
| 581 | set = true; |
| 582 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 583 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 584 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 585 | out << "integer"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 586 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 587 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 588 | if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 589 | if (!set) { |
| 590 | set = true; |
| 591 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 592 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 593 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 594 | out << "boolean"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 596 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 597 | if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 598 | if (!set) { |
| 599 | set = true; |
| 600 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 601 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 602 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 603 | out << "color"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 605 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 606 | if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 607 | if (!set) { |
| 608 | set = true; |
| 609 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 610 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 611 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 612 | out << "float"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 614 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 615 | if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 616 | if (!set) { |
| 617 | set = true; |
| 618 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 619 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 620 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 621 | out << "dimension"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 622 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 623 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 624 | if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 625 | if (!set) { |
| 626 | set = true; |
| 627 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 628 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 629 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 630 | out << "fraction"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 631 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 632 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 633 | if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | if (!set) { |
| 635 | set = true; |
| 636 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 637 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 638 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 639 | out << "enum"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 640 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 641 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 642 | if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 643 | if (!set) { |
| 644 | set = true; |
| 645 | } else { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 646 | out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 647 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 648 | out << "flags"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 649 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 650 | return out.str(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 653 | void Attribute::Print(std::ostream* out) const { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 654 | *out << "(attr) " << MaskString(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 655 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 656 | if (!symbols.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 657 | *out << " [" << util::Joiner(symbols, ", ") << "]"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 658 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 659 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 660 | if (min_int != std::numeric_limits<int32_t>::min()) { |
| 661 | *out << " min=" << min_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 662 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 663 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 664 | if (max_int != std::numeric_limits<int32_t>::max()) { |
| 665 | *out << " max=" << max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 667 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 668 | if (IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 669 | *out << " [weak]"; |
| 670 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 673 | static void BuildAttributeMismatchMessage(const Attribute& attr, const Item& value, |
| 674 | DiagMessage* out_msg) { |
| 675 | *out_msg << "expected"; |
| 676 | if (attr.type_mask & android::ResTable_map::TYPE_BOOLEAN) { |
| 677 | *out_msg << " boolean"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 678 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 679 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 680 | if (attr.type_mask & android::ResTable_map::TYPE_COLOR) { |
| 681 | *out_msg << " color"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 682 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 683 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 684 | if (attr.type_mask & android::ResTable_map::TYPE_DIMENSION) { |
| 685 | *out_msg << " dimension"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 686 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 687 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 688 | if (attr.type_mask & android::ResTable_map::TYPE_ENUM) { |
| 689 | *out_msg << " enum"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 690 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 691 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 692 | if (attr.type_mask & android::ResTable_map::TYPE_FLAGS) { |
| 693 | *out_msg << " flags"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 694 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 695 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 696 | if (attr.type_mask & android::ResTable_map::TYPE_FLOAT) { |
| 697 | *out_msg << " float"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 698 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 699 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 700 | if (attr.type_mask & android::ResTable_map::TYPE_FRACTION) { |
| 701 | *out_msg << " fraction"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 702 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 703 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 704 | if (attr.type_mask & android::ResTable_map::TYPE_INTEGER) { |
| 705 | *out_msg << " integer"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 706 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 707 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 708 | if (attr.type_mask & android::ResTable_map::TYPE_REFERENCE) { |
| 709 | *out_msg << " reference"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 710 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 711 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 712 | if (attr.type_mask & android::ResTable_map::TYPE_STRING) { |
| 713 | *out_msg << " string"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 714 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 715 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 716 | *out_msg << " but got " << value; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 719 | bool Attribute::Matches(const Item& item, DiagMessage* out_msg) const { |
| 720 | constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM; |
| 721 | constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS; |
| 722 | constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER; |
| 723 | constexpr const uint32_t TYPE_REFERENCE = android::ResTable_map::TYPE_REFERENCE; |
| 724 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 725 | android::Res_value val = {}; |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 726 | item.Flatten(&val); |
| 727 | |
| 728 | const uint32_t flattened_data = util::DeviceToHost32(val.data); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 729 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 730 | // Always allow references. |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 731 | const uint32_t actual_type = ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType); |
| 732 | |
| 733 | // Only one type must match between the actual and expected. |
| 734 | if ((actual_type & (type_mask | TYPE_REFERENCE)) == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 735 | if (out_msg) { |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 736 | BuildAttributeMismatchMessage(*this, item, out_msg); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 737 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 738 | return false; |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 739 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 740 | |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 741 | // Enums and flags are encoded as integers, so check them first before doing any range checks. |
| 742 | if ((type_mask & TYPE_ENUM) != 0 && (actual_type & TYPE_ENUM) != 0) { |
| 743 | for (const Symbol& s : symbols) { |
| 744 | if (flattened_data == s.value) { |
| 745 | return true; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // If the attribute accepts integers, we can't fail here. |
| 750 | if ((type_mask & TYPE_INTEGER) == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 751 | if (out_msg) { |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 752 | *out_msg << item << " is not a valid enum"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 753 | } |
| 754 | return false; |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | if ((type_mask & TYPE_FLAGS) != 0 && (actual_type & TYPE_FLAGS) != 0) { |
| 759 | uint32_t mask = 0u; |
| 760 | for (const Symbol& s : symbols) { |
| 761 | mask |= s.value; |
| 762 | } |
| 763 | |
| 764 | // Check if the flattened data is covered by the flag bit mask. |
| 765 | // If the attribute accepts integers, we can't fail here. |
| 766 | if ((mask & flattened_data) == flattened_data) { |
| 767 | return true; |
| 768 | } else if ((type_mask & TYPE_INTEGER) == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 769 | if (out_msg) { |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 770 | *out_msg << item << " is not a valid flag"; |
| 771 | } |
| 772 | return false; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // Finally check the integer range of the value. |
| 777 | if ((type_mask & TYPE_INTEGER) != 0 && (actual_type & TYPE_INTEGER) != 0) { |
| 778 | if (static_cast<int32_t>(flattened_data) < min_int) { |
| 779 | if (out_msg) { |
| 780 | *out_msg << item << " is less than minimum integer " << min_int; |
| 781 | } |
| 782 | return false; |
| 783 | } else if (static_cast<int32_t>(flattened_data) > max_int) { |
| 784 | if (out_msg) { |
| 785 | *out_msg << item << " is greater than maximum integer " << max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 786 | } |
| 787 | return false; |
| 788 | } |
| 789 | } |
| 790 | return true; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 793 | std::ostream& operator<<(std::ostream& out, const Style::Entry& entry) { |
| 794 | if (entry.key.name) { |
| 795 | out << entry.key.name.value(); |
| 796 | } else if (entry.key.id) { |
| 797 | out << entry.key.id.value(); |
| 798 | } else { |
| 799 | out << "???"; |
| 800 | } |
| 801 | out << " = " << entry.value; |
| 802 | return out; |
| 803 | } |
| 804 | |
| 805 | template <typename T> |
| 806 | std::vector<T*> ToPointerVec(std::vector<T>& src) { |
| 807 | std::vector<T*> dst; |
| 808 | dst.reserve(src.size()); |
| 809 | for (T& in : src) { |
| 810 | dst.push_back(&in); |
| 811 | } |
| 812 | return dst; |
| 813 | } |
| 814 | |
| 815 | template <typename T> |
| 816 | std::vector<const T*> ToPointerVec(const std::vector<T>& src) { |
| 817 | std::vector<const T*> dst; |
| 818 | dst.reserve(src.size()); |
| 819 | for (const T& in : src) { |
| 820 | dst.push_back(&in); |
| 821 | } |
| 822 | return dst; |
| 823 | } |
| 824 | |
| 825 | static bool KeyNameComparator(const Style::Entry* a, const Style::Entry* b) { |
| 826 | return a->key.name < b->key.name; |
| 827 | } |
| 828 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 829 | bool Style::Equals(const Value* value) const { |
| 830 | const Style* other = ValueCast<Style>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 831 | if (!other) { |
| 832 | return false; |
| 833 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 834 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 835 | if (bool(parent) != bool(other->parent) || |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 836 | (parent && other->parent && !parent.value().Equals(&other->parent.value()))) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 837 | return false; |
| 838 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 839 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 840 | if (entries.size() != other->entries.size()) { |
| 841 | return false; |
| 842 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 843 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 844 | std::vector<const Entry*> sorted_a = ToPointerVec(entries); |
| 845 | std::sort(sorted_a.begin(), sorted_a.end(), KeyNameComparator); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 846 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 847 | std::vector<const Entry*> sorted_b = ToPointerVec(other->entries); |
| 848 | std::sort(sorted_b.begin(), sorted_b.end(), KeyNameComparator); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 849 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 850 | return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 851 | [](const Entry* a, const Entry* b) -> bool { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 852 | return a->key.Equals(&b->key) && a->value->Equals(b->value.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 853 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 856 | void Style::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 857 | *out << "(style) "; |
| 858 | if (parent && parent.value().name) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 859 | const Reference& parent_ref = parent.value(); |
| 860 | if (parent_ref.private_reference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 861 | *out << "*"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 862 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 863 | *out << parent_ref.name.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 864 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 865 | *out << " [" << util::Joiner(entries, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 868 | Style::Entry CloneEntry(const Style::Entry& entry, StringPool* pool) { |
| 869 | Style::Entry cloned_entry{entry.key}; |
| 870 | if (entry.value != nullptr) { |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 871 | CloningValueTransformer cloner(pool); |
| 872 | cloned_entry.value = entry.value->Transform(cloner); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 873 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 874 | return cloned_entry; |
| 875 | } |
| 876 | |
| 877 | void Style::MergeWith(Style* other, StringPool* pool) { |
| 878 | if (other->parent) { |
| 879 | parent = other->parent; |
| 880 | } |
| 881 | |
| 882 | // We can't assume that the entries are sorted alphabetically since they're supposed to be |
| 883 | // sorted by Resource Id. Not all Resource Ids may be set though, so we can't sort and merge |
| 884 | // them keying off that. |
| 885 | // |
| 886 | // Instead, sort the entries of each Style by their name in a separate structure. Then merge |
| 887 | // those. |
| 888 | |
| 889 | std::vector<Entry*> this_sorted = ToPointerVec(entries); |
| 890 | std::sort(this_sorted.begin(), this_sorted.end(), KeyNameComparator); |
| 891 | |
| 892 | std::vector<Entry*> other_sorted = ToPointerVec(other->entries); |
| 893 | std::sort(other_sorted.begin(), other_sorted.end(), KeyNameComparator); |
| 894 | |
| 895 | auto this_iter = this_sorted.begin(); |
| 896 | const auto this_end = this_sorted.end(); |
| 897 | |
| 898 | auto other_iter = other_sorted.begin(); |
| 899 | const auto other_end = other_sorted.end(); |
| 900 | |
| 901 | std::vector<Entry> merged_entries; |
| 902 | while (this_iter != this_end) { |
| 903 | if (other_iter != other_end) { |
| 904 | if ((*this_iter)->key.name < (*other_iter)->key.name) { |
| 905 | merged_entries.push_back(std::move(**this_iter)); |
| 906 | ++this_iter; |
| 907 | } else { |
| 908 | // The other overrides. |
| 909 | merged_entries.push_back(CloneEntry(**other_iter, pool)); |
| 910 | if ((*this_iter)->key.name == (*other_iter)->key.name) { |
| 911 | ++this_iter; |
| 912 | } |
| 913 | ++other_iter; |
| 914 | } |
| 915 | } else { |
| 916 | merged_entries.push_back(std::move(**this_iter)); |
| 917 | ++this_iter; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | while (other_iter != other_end) { |
| 922 | merged_entries.push_back(CloneEntry(**other_iter, pool)); |
| 923 | ++other_iter; |
| 924 | } |
| 925 | |
| 926 | entries = std::move(merged_entries); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 927 | } |
| 928 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 929 | bool Array::Equals(const Value* value) const { |
| 930 | const Array* other = ValueCast<Array>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 931 | if (!other) { |
| 932 | return false; |
| 933 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 934 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 935 | if (elements.size() != other->elements.size()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 936 | return false; |
| 937 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 938 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 939 | return std::equal(elements.begin(), elements.end(), other->elements.begin(), |
| 940 | [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 941 | return a->Equals(b.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 942 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 945 | void Array::Print(std::ostream* out) const { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 946 | *out << "(array) [" << util::Joiner(elements, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 949 | bool Plural::Equals(const Value* value) const { |
| 950 | const Plural* other = ValueCast<Plural>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 951 | if (!other) { |
| 952 | return false; |
| 953 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 954 | |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 955 | auto one_iter = values.begin(); |
| 956 | auto one_end_iter = values.end(); |
| 957 | auto two_iter = other->values.begin(); |
| 958 | for (; one_iter != one_end_iter; ++one_iter, ++two_iter) { |
| 959 | const std::unique_ptr<Item>& a = *one_iter; |
| 960 | const std::unique_ptr<Item>& b = *two_iter; |
| 961 | if (a != nullptr && b != nullptr) { |
| 962 | if (!a->Equals(b.get())) { |
| 963 | return false; |
| 964 | } |
| 965 | } else if (a != b) { |
| 966 | return false; |
| 967 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 968 | } |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 969 | return true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 972 | void Plural::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 973 | *out << "(plural)"; |
| 974 | if (values[Zero]) { |
| 975 | *out << " zero=" << *values[Zero]; |
| 976 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 977 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 978 | if (values[One]) { |
| 979 | *out << " one=" << *values[One]; |
| 980 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 981 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 982 | if (values[Two]) { |
| 983 | *out << " two=" << *values[Two]; |
| 984 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 985 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 986 | if (values[Few]) { |
| 987 | *out << " few=" << *values[Few]; |
| 988 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 989 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 990 | if (values[Many]) { |
| 991 | *out << " many=" << *values[Many]; |
| 992 | } |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 993 | |
| 994 | if (values[Other]) { |
| 995 | *out << " other=" << *values[Other]; |
| 996 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 999 | bool Styleable::Equals(const Value* value) const { |
| 1000 | const Styleable* other = ValueCast<Styleable>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1001 | if (!other) { |
| 1002 | return false; |
| 1003 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 1004 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1005 | if (entries.size() != other->entries.size()) { |
| 1006 | return false; |
| 1007 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 1008 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1009 | return std::equal(entries.begin(), entries.end(), other->entries.begin(), |
| 1010 | [](const Reference& a, const Reference& b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1011 | return a.Equals(&b); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1012 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1015 | void Styleable::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1016 | *out << "(styleable) " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1017 | << " [" << util::Joiner(entries, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 1020 | bool operator<(const Reference& a, const Reference& b) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1021 | int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({})); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1022 | if (cmp != 0) return cmp < 0; |
| 1023 | return a.id < b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | bool operator==(const Reference& a, const Reference& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1027 | return a.name == b.name && a.id == b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | bool operator!=(const Reference& a, const Reference& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1031 | return a.name != b.name || a.id != b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 1034 | struct NameOnlyComparator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1035 | bool operator()(const Reference& a, const Reference& b) const { |
| 1036 | return a.name < b.name; |
| 1037 | } |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 1038 | }; |
| 1039 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1040 | void Styleable::MergeWith(Styleable* other) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1041 | // Compare only names, because some References may already have their IDs |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 1042 | // assigned (framework IDs that don't change). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1043 | std::set<Reference, NameOnlyComparator> references; |
| 1044 | references.insert(entries.begin(), entries.end()); |
| 1045 | references.insert(other->entries.begin(), other->entries.end()); |
| 1046 | entries.clear(); |
| 1047 | entries.reserve(references.size()); |
| 1048 | entries.insert(entries.end(), references.begin(), references.end()); |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 1051 | template <typename T> |
| 1052 | std::unique_ptr<T> CopyValueFields(std::unique_ptr<T> new_value, const T* value) { |
| 1053 | new_value->SetSource(value->GetSource()); |
| 1054 | new_value->SetComment(value->GetComment()); |
| 1055 | return new_value; |
| 1056 | } |
| 1057 | |
| 1058 | CloningValueTransformer::CloningValueTransformer(StringPool* new_pool) |
| 1059 | : ValueTransformer(new_pool) { |
| 1060 | } |
| 1061 | |
| 1062 | std::unique_ptr<Reference> CloningValueTransformer::TransformDerived(const Reference* value) { |
| 1063 | return std::make_unique<Reference>(*value); |
| 1064 | } |
| 1065 | |
| 1066 | std::unique_ptr<Id> CloningValueTransformer::TransformDerived(const Id* value) { |
| 1067 | return std::make_unique<Id>(*value); |
| 1068 | } |
| 1069 | |
| 1070 | std::unique_ptr<RawString> CloningValueTransformer::TransformDerived(const RawString* value) { |
| 1071 | auto new_value = std::make_unique<RawString>(pool_->MakeRef(value->value)); |
| 1072 | return CopyValueFields(std::move(new_value), value); |
| 1073 | } |
| 1074 | |
| 1075 | std::unique_ptr<String> CloningValueTransformer::TransformDerived(const String* value) { |
| 1076 | auto new_value = std::make_unique<String>(pool_->MakeRef(value->value)); |
| 1077 | new_value->untranslatable_sections = value->untranslatable_sections; |
| 1078 | return CopyValueFields(std::move(new_value), value); |
| 1079 | } |
| 1080 | |
| 1081 | std::unique_ptr<StyledString> CloningValueTransformer::TransformDerived(const StyledString* value) { |
| 1082 | auto new_value = std::make_unique<StyledString>(pool_->MakeRef(value->value)); |
| 1083 | new_value->untranslatable_sections = value->untranslatable_sections; |
| 1084 | return CopyValueFields(std::move(new_value), value); |
| 1085 | } |
| 1086 | |
| 1087 | std::unique_ptr<FileReference> CloningValueTransformer::TransformDerived( |
| 1088 | const FileReference* value) { |
| 1089 | auto new_value = std::make_unique<FileReference>(pool_->MakeRef(value->path)); |
| 1090 | new_value->file = value->file; |
| 1091 | new_value->type = value->type; |
| 1092 | return CopyValueFields(std::move(new_value), value); |
| 1093 | } |
| 1094 | |
| 1095 | std::unique_ptr<BinaryPrimitive> CloningValueTransformer::TransformDerived( |
| 1096 | const BinaryPrimitive* value) { |
| 1097 | return std::make_unique<BinaryPrimitive>(*value); |
| 1098 | } |
| 1099 | |
| 1100 | std::unique_ptr<Attribute> CloningValueTransformer::TransformDerived(const Attribute* value) { |
| 1101 | auto new_value = std::make_unique<Attribute>(); |
| 1102 | new_value->type_mask = value->type_mask; |
| 1103 | new_value->min_int = value->min_int; |
| 1104 | new_value->max_int = value->max_int; |
| 1105 | for (const Attribute::Symbol& s : value->symbols) { |
| 1106 | new_value->symbols.emplace_back(Attribute::Symbol{ |
| 1107 | .symbol = *s.symbol.Transform(*this), |
| 1108 | .value = s.value, |
| 1109 | .type = s.type, |
| 1110 | }); |
| 1111 | } |
| 1112 | return CopyValueFields(std::move(new_value), value); |
| 1113 | } |
| 1114 | |
| 1115 | std::unique_ptr<Style> CloningValueTransformer::TransformDerived(const Style* value) { |
| 1116 | auto new_value = std::make_unique<Style>(); |
| 1117 | new_value->parent = value->parent; |
| 1118 | new_value->parent_inferred = value->parent_inferred; |
| 1119 | for (auto& entry : value->entries) { |
| 1120 | new_value->entries.push_back(Style::Entry{entry.key, entry.value->Transform(*this)}); |
| 1121 | } |
| 1122 | return CopyValueFields(std::move(new_value), value); |
| 1123 | } |
| 1124 | |
| 1125 | std::unique_ptr<Array> CloningValueTransformer::TransformDerived(const Array* value) { |
| 1126 | auto new_value = std::make_unique<Array>(); |
| 1127 | for (auto& item : value->elements) { |
| 1128 | new_value->elements.emplace_back(item->Transform(*this)); |
| 1129 | } |
| 1130 | return CopyValueFields(std::move(new_value), value); |
| 1131 | } |
| 1132 | |
| 1133 | std::unique_ptr<Plural> CloningValueTransformer::TransformDerived(const Plural* value) { |
| 1134 | auto new_value = std::make_unique<Plural>(); |
| 1135 | const size_t count = value->values.size(); |
| 1136 | for (size_t i = 0; i < count; i++) { |
| 1137 | if (value->values[i]) { |
| 1138 | new_value->values[i] = value->values[i]->Transform(*this); |
| 1139 | } |
| 1140 | } |
| 1141 | return CopyValueFields(std::move(new_value), value); |
| 1142 | } |
| 1143 | |
| 1144 | std::unique_ptr<Styleable> CloningValueTransformer::TransformDerived(const Styleable* value) { |
| 1145 | auto new_value = std::make_unique<Styleable>(); |
| 1146 | for (const Reference& s : value->entries) { |
| 1147 | new_value->entries.emplace_back(*s.Transform(*this)); |
| 1148 | } |
| 1149 | return CopyValueFields(std::move(new_value), value); |
| 1150 | } |
| 1151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1152 | } // namespace aapt |