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