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