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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | template <typename Derived> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | void BaseValue<Derived>::Accept(RawValueVisitor* visitor) { |
| 34 | visitor->Visit(static_cast<Derived*>(this)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | template <typename Derived> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | void BaseItem<Derived>::Accept(RawValueVisitor* visitor) { |
| 39 | visitor->Visit(static_cast<Derived*>(this)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | RawString::RawString(const StringPool::Ref& ref) : value(ref) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | bool RawString::Equals(const Value* value) const { |
| 45 | const RawString* other = ValueCast<RawString>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | if (!other) { |
| 47 | return false; |
| 48 | } |
| 49 | return *this->value == *other->value; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | RawString* RawString::Clone(StringPool* new_pool) const { |
| 53 | RawString* rs = new RawString(new_pool->MakeRef(*value)); |
| 54 | rs->comment_ = comment_; |
| 55 | rs->source_ = source_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | return rs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | bool RawString::Flatten(android::Res_value* out_value) const { |
| 60 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 61 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | void RawString::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | *out << "(raw string) " << *value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | Reference::Reference() : reference_type(Type::kResource) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | Reference::Reference(const ResourceNameRef& n, Type t) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | : name(n.ToResourceName()), reference_type(t) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | Reference::Reference(const ResourceId& i, Type type) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | : id(i), reference_type(type) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | Reference::Reference(const ResourceNameRef& n, const ResourceId& i) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | : name(n.ToResourceName()), id(i), reference_type(Type::kResource) {} |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | bool Reference::Equals(const Value* value) const { |
| 81 | const Reference* other = ValueCast<Reference>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | if (!other) { |
| 83 | return false; |
| 84 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | return reference_type == other->reference_type && |
| 86 | private_reference == other->private_reference && id == other->id && |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | name == other->name; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | bool Reference::Flatten(android::Res_value* out_value) const { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 91 | const ResourceId resid = id.value_or_default(ResourceId(0)); |
| 92 | const bool dynamic = |
| 93 | (resid.package_id() != kFrameworkPackageId && resid.package_id() != kAppPackageId); |
| 94 | |
| 95 | if (reference_type == Reference::Type::kResource) { |
| 96 | if (dynamic) { |
| 97 | out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE; |
| 98 | } else { |
| 99 | out_value->dataType = android::Res_value::TYPE_REFERENCE; |
| 100 | } |
| 101 | } else { |
| 102 | if (dynamic) { |
| 103 | out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE; |
| 104 | } else { |
| 105 | out_value->dataType = android::Res_value::TYPE_ATTRIBUTE; |
| 106 | } |
| 107 | } |
| 108 | out_value->data = util::HostToDevice32(resid.id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | Reference* Reference::Clone(StringPool* /*new_pool*/) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | return new Reference(*this); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | void Reference::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | *out << "(reference) "; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | if (reference_type == Reference::Type::kResource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | *out << "@"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | if (private_reference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | *out << "*"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | } else { |
| 124 | *out << "?"; |
| 125 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | if (name) { |
| 128 | *out << name.value(); |
| 129 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 130 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | if (id && !Res_INTERNALID(id.value().id)) { |
| 132 | *out << " " << id.value(); |
| 133 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | bool Id::Equals(const Value* value) const { |
| 137 | return ValueCast<Id>(value) != nullptr; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | bool Id::Flatten(android::Res_value* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | out->dataType = android::Res_value::TYPE_INT_BOOLEAN; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | out->data = util::HostToDevice32(0); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | void Id::Print(std::ostream* out) const { *out << "(id)"; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 149 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | String::String(const StringPool::Ref& ref) : value(ref) {} |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 151 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | bool String::Equals(const Value* value) const { |
| 153 | const String* other = ValueCast<String>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | if (!other) { |
| 155 | return false; |
| 156 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 157 | |
| 158 | if (this->value != other->value) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if (untranslatable_sections.size() != other->untranslatable_sections.size()) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | auto other_iter = other->untranslatable_sections.begin(); |
| 167 | for (const UntranslatableSection& this_section : untranslatable_sections) { |
| 168 | if (this_section != *other_iter) { |
| 169 | return false; |
| 170 | } |
| 171 | ++other_iter; |
| 172 | } |
| 173 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | bool String::Flatten(android::Res_value* out_value) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | // Verify that our StringPool index is within encode-able limits. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | if (value.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 183 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | String* String::Clone(StringPool* new_pool) const { |
| 188 | String* str = new String(new_pool->MakeRef(*value)); |
| 189 | str->comment_ = comment_; |
| 190 | str->source_ = source_; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 191 | str->untranslatable_sections = untranslatable_sections; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | return str; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | void String::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | *out << "(string) \"" << *value << "\""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {} |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 200 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | bool StyledString::Equals(const Value* value) const { |
| 202 | const StyledString* other = ValueCast<StyledString>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | if (!other) { |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 204 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 207 | if (this->value != other->value) { |
| 208 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 210 | |
| 211 | if (untranslatable_sections.size() != other->untranslatable_sections.size()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | auto other_iter = other->untranslatable_sections.begin(); |
| 216 | for (const UntranslatableSection& this_section : untranslatable_sections) { |
| 217 | if (this_section != *other_iter) { |
| 218 | return false; |
| 219 | } |
| 220 | ++other_iter; |
| 221 | } |
| 222 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | bool StyledString::Flatten(android::Res_value* out_value) const { |
| 226 | if (value.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 229 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 231 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | StyledString* StyledString::Clone(StringPool* new_pool) const { |
| 236 | StyledString* str = new StyledString(new_pool->MakeRef(value)); |
| 237 | str->comment_ = comment_; |
| 238 | str->source_ = source_; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 239 | str->untranslatable_sections = untranslatable_sections; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | return str; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | void StyledString::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | *out << "(styled string) \"" << *value->str << "\""; |
| 245 | for (const StringPool::Span& span : value->spans) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | *out << " " << *span.name << ":" << span.first_char << "," |
| 247 | << span.last_char; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | bool FileReference::Equals(const Value* value) const { |
| 254 | const FileReference* other = ValueCast<FileReference>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 255 | if (!other) { |
| 256 | return false; |
| 257 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 258 | return path == other->path; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | bool FileReference::Flatten(android::Res_value* out_value) const { |
| 262 | if (path.index() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | out_value->dataType = android::Res_value::TYPE_STRING; |
| 267 | out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 268 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | FileReference* FileReference::Clone(StringPool* new_pool) const { |
| 272 | FileReference* fr = new FileReference(new_pool->MakeRef(*path)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | fr->file = file; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | fr->comment_ = comment_; |
| 275 | fr->source_ = source_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | return fr; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | void FileReference::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | *out << "(file) " << *path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 284 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 285 | BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | value.dataType = dataType; |
| 287 | value.data = data; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | bool BinaryPrimitive::Equals(const Value* value) const { |
| 291 | const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | if (!other) { |
| 293 | return false; |
| 294 | } |
| 295 | return this->value.dataType == other->value.dataType && |
| 296 | this->value.data == other->value.data; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | bool BinaryPrimitive::Flatten(android::Res_value* out_value) const { |
| 300 | out_value->dataType = value.dataType; |
| 301 | out_value->data = util::HostToDevice32(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | return new BinaryPrimitive(*this); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | void BinaryPrimitive::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | switch (value.dataType) { |
| 311 | case android::Res_value::TYPE_NULL: |
| 312 | *out << "(null)"; |
| 313 | break; |
| 314 | case android::Res_value::TYPE_INT_DEC: |
| 315 | *out << "(integer) " << static_cast<int32_t>(value.data); |
| 316 | break; |
| 317 | case android::Res_value::TYPE_INT_HEX: |
| 318 | *out << "(integer) 0x" << std::hex << value.data << std::dec; |
| 319 | break; |
| 320 | case android::Res_value::TYPE_INT_BOOLEAN: |
| 321 | *out << "(boolean) " << (value.data != 0 ? "true" : "false"); |
| 322 | break; |
| 323 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 324 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 325 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 326 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
| 327 | *out << "(color) #" << std::hex << value.data << std::dec; |
| 328 | break; |
| 329 | default: |
| 330 | *out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x" |
| 331 | << std::hex << value.data << std::dec; |
| 332 | break; |
| 333 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | Attribute::Attribute(bool w, uint32_t t) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | : type_mask(t), |
| 338 | min_int(std::numeric_limits<int32_t>::min()), |
| 339 | max_int(std::numeric_limits<int32_t>::max()) { |
| 340 | weak_ = w; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 343 | template <typename T> |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 344 | constexpr T* add_pointer(T& val) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | return &val; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | bool Attribute::Equals(const Value* value) const { |
| 349 | const Attribute* other = ValueCast<Attribute>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | if (!other) { |
| 351 | return false; |
| 352 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 353 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 354 | if (symbols.size() != other->symbols.size()) { |
| 355 | return false; |
| 356 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | if (type_mask != other->type_mask || min_int != other->min_int || |
| 359 | max_int != other->max_int) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | return false; |
| 361 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 362 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | std::vector<const Symbol*> sorted_a; |
| 364 | std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a), |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 365 | add_pointer<const Symbol>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 366 | std::sort(sorted_a.begin(), sorted_a.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | [](const Symbol* a, const Symbol* b) -> bool { |
| 368 | return a->symbol.name < b->symbol.name; |
| 369 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 370 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 371 | std::vector<const Symbol*> sorted_b; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | std::transform(other->symbols.begin(), other->symbols.end(), |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 373 | std::back_inserter(sorted_b), add_pointer<const Symbol>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 374 | std::sort(sorted_b.begin(), sorted_b.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | [](const Symbol* a, const Symbol* b) -> bool { |
| 376 | return a->symbol.name < b->symbol.name; |
| 377 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 378 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 379 | return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | [](const Symbol* a, const Symbol* b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | return a->symbol.Equals(&b->symbol) && |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | a->value == b->value; |
| 383 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 386 | Attribute* Attribute::Clone(StringPool* /*new_pool*/) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | return new Attribute(*this); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | void Attribute::PrintMask(std::ostream* out) const { |
| 391 | if (type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 392 | *out << "any"; |
| 393 | return; |
| 394 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 395 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | bool set = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | if (!set) { |
| 399 | set = true; |
| 400 | } else { |
| 401 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 402 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 403 | *out << "reference"; |
| 404 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 405 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 407 | if (!set) { |
| 408 | set = true; |
| 409 | } else { |
| 410 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 411 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | *out << "string"; |
| 413 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 414 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 415 | if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | if (!set) { |
| 417 | set = true; |
| 418 | } else { |
| 419 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 420 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | *out << "integer"; |
| 422 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 423 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 424 | if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 425 | if (!set) { |
| 426 | set = true; |
| 427 | } else { |
| 428 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 429 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 430 | *out << "boolean"; |
| 431 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 432 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 | if ((type_mask & android::ResTable_map::TYPE_COLOR) != 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 << "color"; |
| 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_FLOAT) != 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 << "float"; |
| 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_DIMENSION) != 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 << "dimension"; |
| 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_FRACTION) != 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 << "fraction"; |
| 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_ENUM) != 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 << "enum"; |
| 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_FLAGS) != 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 << "flags"; |
| 485 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 488 | void Attribute::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 489 | *out << "(attr) "; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | PrintMask(out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 491 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 492 | if (!symbols.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 493 | *out << " [" << util::Joiner(symbols, ", ") << "]"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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 (min_int != std::numeric_limits<int32_t>::min()) { |
| 497 | *out << " min=" << min_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 498 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 499 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 500 | if (max_int != std::numeric_limits<int32_t>::max()) { |
| 501 | *out << " max=" << max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 502 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 503 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 504 | if (IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | *out << " [weak]"; |
| 506 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | static void BuildAttributeMismatchMessage(DiagMessage* msg, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 510 | const Attribute* attr, |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 511 | const Item* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | *msg << "expected"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 513 | if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 514 | *msg << " boolean"; |
| 515 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 516 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 517 | if (attr->type_mask & android::ResTable_map::TYPE_COLOR) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 518 | *msg << " color"; |
| 519 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 520 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 521 | if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 522 | *msg << " dimension"; |
| 523 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 524 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 525 | if (attr->type_mask & android::ResTable_map::TYPE_ENUM) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 526 | *msg << " enum"; |
| 527 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 528 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 529 | if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 530 | *msg << " flags"; |
| 531 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 532 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 533 | if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 534 | *msg << " float"; |
| 535 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 536 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 537 | if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 538 | *msg << " fraction"; |
| 539 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 540 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 541 | if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 542 | *msg << " integer"; |
| 543 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 544 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 546 | *msg << " reference"; |
| 547 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | if (attr->type_mask & android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 550 | *msg << " string"; |
| 551 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 552 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 553 | *msg << " but got " << *value; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 556 | bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 557 | android::Res_value val = {}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 558 | item->Flatten(&val); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 559 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 560 | // Always allow references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 561 | const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE; |
| 562 | if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) { |
| 563 | if (out_msg) { |
| 564 | BuildAttributeMismatchMessage(out_msg, this, item); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 565 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 566 | return false; |
| 567 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 568 | } else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) & |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 569 | android::ResTable_map::TYPE_INTEGER) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 570 | if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) { |
| 571 | if (out_msg) { |
| 572 | *out_msg << *item << " is less than minimum integer " << min_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 573 | } |
| 574 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | } else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) { |
| 576 | if (out_msg) { |
| 577 | *out_msg << *item << " is greater than maximum integer " << max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | return true; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 585 | bool Style::Equals(const Value* value) const { |
| 586 | const Style* other = ValueCast<Style>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 587 | if (!other) { |
| 588 | return false; |
| 589 | } |
| 590 | if (bool(parent) != bool(other->parent) || |
| 591 | (parent && other->parent && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 592 | !parent.value().Equals(&other->parent.value()))) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | return false; |
| 594 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 595 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 596 | if (entries.size() != other->entries.size()) { |
| 597 | return false; |
| 598 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 599 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 600 | std::vector<const Entry*> sorted_a; |
| 601 | std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a), |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 602 | add_pointer<const Entry>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 603 | std::sort(sorted_a.begin(), sorted_a.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | [](const Entry* a, const Entry* b) -> bool { |
| 605 | return a->key.name < b->key.name; |
| 606 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 607 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 608 | std::vector<const Entry*> sorted_b; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 609 | std::transform(other->entries.begin(), other->entries.end(), |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 610 | std::back_inserter(sorted_b), add_pointer<const Entry>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 611 | std::sort(sorted_b.begin(), sorted_b.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 612 | [](const Entry* a, const Entry* b) -> bool { |
| 613 | return a->key.name < b->key.name; |
| 614 | }); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 615 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 616 | return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 617 | [](const Entry* a, const Entry* b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 618 | return a->key.Equals(&b->key) && |
| 619 | a->value->Equals(b->value.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 620 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 623 | Style* Style::Clone(StringPool* new_pool) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 624 | Style* style = new Style(); |
| 625 | style->parent = parent; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 626 | style->parent_inferred = parent_inferred; |
| 627 | style->comment_ = comment_; |
| 628 | style->source_ = source_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 629 | for (auto& entry : entries) { |
| 630 | style->entries.push_back( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 631 | Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 632 | } |
| 633 | return style; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 636 | void Style::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 637 | *out << "(style) "; |
| 638 | if (parent && parent.value().name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 639 | if (parent.value().private_reference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 640 | *out << "*"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 641 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 642 | *out << parent.value().name.value(); |
| 643 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 644 | *out << " [" << util::Joiner(entries, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 647 | static ::std::ostream& operator<<(::std::ostream& out, |
| 648 | const Style::Entry& value) { |
| 649 | if (value.key.name) { |
| 650 | out << value.key.name.value(); |
| 651 | } else if (value.key.id) { |
| 652 | out << value.key.id.value(); |
| 653 | } else { |
| 654 | out << "???"; |
| 655 | } |
| 656 | out << " = "; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 657 | value.value->Print(&out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 658 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 661 | bool Array::Equals(const Value* value) const { |
| 662 | const Array* other = ValueCast<Array>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 663 | if (!other) { |
| 664 | return false; |
| 665 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 666 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 667 | if (items.size() != other->items.size()) { |
| 668 | return false; |
| 669 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 670 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 671 | return std::equal(items.begin(), items.end(), other->items.begin(), |
| 672 | [](const std::unique_ptr<Item>& a, |
| 673 | const std::unique_ptr<Item>& b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 674 | return a->Equals(b.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 675 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 678 | Array* Array::Clone(StringPool* new_pool) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 679 | Array* array = new Array(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 680 | array->comment_ = comment_; |
| 681 | array->source_ = source_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 682 | for (auto& item : items) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 683 | array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 684 | } |
| 685 | return array; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 688 | void Array::Print(std::ostream* out) const { |
| 689 | *out << "(array) [" << util::Joiner(items, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 692 | bool Plural::Equals(const Value* value) const { |
| 693 | const Plural* other = ValueCast<Plural>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 694 | if (!other) { |
| 695 | return false; |
| 696 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 697 | |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 698 | auto one_iter = values.begin(); |
| 699 | auto one_end_iter = values.end(); |
| 700 | auto two_iter = other->values.begin(); |
| 701 | for (; one_iter != one_end_iter; ++one_iter, ++two_iter) { |
| 702 | const std::unique_ptr<Item>& a = *one_iter; |
| 703 | const std::unique_ptr<Item>& b = *two_iter; |
| 704 | if (a != nullptr && b != nullptr) { |
| 705 | if (!a->Equals(b.get())) { |
| 706 | return false; |
| 707 | } |
| 708 | } else if (a != b) { |
| 709 | return false; |
| 710 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 711 | } |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 712 | return true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 715 | Plural* Plural::Clone(StringPool* new_pool) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 716 | Plural* p = new Plural(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 717 | p->comment_ = comment_; |
| 718 | p->source_ = source_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 719 | const size_t count = values.size(); |
| 720 | for (size_t i = 0; i < count; i++) { |
| 721 | if (values[i]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 722 | p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 723 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 724 | } |
| 725 | return p; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 728 | void Plural::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 729 | *out << "(plural)"; |
| 730 | if (values[Zero]) { |
| 731 | *out << " zero=" << *values[Zero]; |
| 732 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 733 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 734 | if (values[One]) { |
| 735 | *out << " one=" << *values[One]; |
| 736 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 737 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 738 | if (values[Two]) { |
| 739 | *out << " two=" << *values[Two]; |
| 740 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 741 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 742 | if (values[Few]) { |
| 743 | *out << " few=" << *values[Few]; |
| 744 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 745 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 746 | if (values[Many]) { |
| 747 | *out << " many=" << *values[Many]; |
| 748 | } |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 749 | |
| 750 | if (values[Other]) { |
| 751 | *out << " other=" << *values[Other]; |
| 752 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 755 | static ::std::ostream& operator<<(::std::ostream& out, |
| 756 | const std::unique_ptr<Item>& item) { |
| 757 | return out << *item; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 760 | bool Styleable::Equals(const Value* value) const { |
| 761 | const Styleable* other = ValueCast<Styleable>(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 762 | if (!other) { |
| 763 | return false; |
| 764 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 765 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 766 | if (entries.size() != other->entries.size()) { |
| 767 | return false; |
| 768 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 769 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 770 | return std::equal(entries.begin(), entries.end(), other->entries.begin(), |
| 771 | [](const Reference& a, const Reference& b) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 772 | return a.Equals(&b); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 773 | }); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 776 | Styleable* Styleable::Clone(StringPool* /*new_pool*/) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 777 | return new Styleable(*this); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 780 | void Styleable::Print(std::ostream* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 781 | *out << "(styleable) " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 782 | << " [" << util::Joiner(entries, ", ") << "]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 785 | bool operator<(const Reference& a, const Reference& b) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 786 | 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] | 787 | if (cmp != 0) return cmp < 0; |
| 788 | return a.id < b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | bool operator==(const Reference& a, const Reference& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 792 | return a.name == b.name && a.id == b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | bool operator!=(const Reference& a, const Reference& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 796 | return a.name != b.name || a.id != b.id; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 799 | struct NameOnlyComparator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 800 | bool operator()(const Reference& a, const Reference& b) const { |
| 801 | return a.name < b.name; |
| 802 | } |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 803 | }; |
| 804 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 805 | void Styleable::MergeWith(Styleable* other) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 806 | // Compare only names, because some References may already have their IDs |
| 807 | // assigned |
| 808 | // (framework IDs that don't change). |
| 809 | std::set<Reference, NameOnlyComparator> references; |
| 810 | references.insert(entries.begin(), entries.end()); |
| 811 | references.insert(other->entries.begin(), other->entries.end()); |
| 812 | entries.clear(); |
| 813 | entries.reserve(references.size()); |
| 814 | entries.insert(entries.end(), references.begin(), references.end()); |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 817 | } // namespace aapt |