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