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