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