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