blob: ab9c792876b3acf39769491a6ab06aa3b2ff2c15 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinskia5870652015-11-20 15:32:30 -080018#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "ValueVisitor.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070021#include "util/Util.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070022
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <androidfw/ResourceTypes.h>
24#include <limits>
25
26namespace aapt {
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028template <typename Derived>
29void BaseValue<Derived>::accept(RawValueVisitor* visitor) {
30 visitor->visit(static_cast<Derived*>(this));
31}
32
33template <typename Derived>
34void BaseItem<Derived>::accept(RawValueVisitor* visitor) {
35 visitor->visit(static_cast<Derived*>(this));
36}
37
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038RawString::RawString(const StringPool::Ref& ref) : value(ref) {
39}
40
Adam Lesinski769de982015-04-10 19:43:55 -070041RawString* RawString::clone(StringPool* newPool) const {
Adam Lesinskib274e352015-11-06 15:14:35 -080042 RawString* rs = new RawString(newPool->makeRef(*value));
43 rs->mComment = mComment;
44 rs->mSource = mSource;
45 return rs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046}
47
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048bool RawString::flatten(android::Res_value* outValue) const {
Adam Lesinski59e04c62016-02-04 15:59:23 -080049 outValue->dataType = android::Res_value::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051 return true;
52}
53
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054void RawString::print(std::ostream* out) const {
55 *out << "(raw string) " << *value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056}
57
58Reference::Reference() : referenceType(Reference::Type::kResource) {
59}
60
61Reference::Reference(const ResourceNameRef& n, Type t) :
62 name(n.toResourceName()), referenceType(t) {
63}
64
65Reference::Reference(const ResourceId& i, Type type) : id(i), referenceType(type) {
66}
67
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068bool Reference::flatten(android::Res_value* outValue) const {
Adam Lesinski467f1712015-11-16 17:35:44 -080069 outValue->dataType = (referenceType == Reference::Type::kResource) ?
70 android::Res_value::TYPE_REFERENCE : android::Res_value::TYPE_ATTRIBUTE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 outValue->data = util::hostToDevice32(id ? id.value().id : 0);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072 return true;
73}
74
Adam Lesinski769de982015-04-10 19:43:55 -070075Reference* Reference::clone(StringPool* /*newPool*/) const {
Adam Lesinski467f1712015-11-16 17:35:44 -080076 return new Reference(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077}
78
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079void Reference::print(std::ostream* out) const {
80 *out << "(reference) ";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081 if (referenceType == Reference::Type::kResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 *out << "@";
Adam Lesinski467f1712015-11-16 17:35:44 -080083 if (privateReference) {
84 *out << "*";
85 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 *out << "?";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088 }
89
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090 if (name) {
91 *out << name.value();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092 }
93
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 if (id && !Res_INTERNALID(id.value().id)) {
95 *out << " " << id.value();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096 }
97}
98
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099bool Id::flatten(android::Res_value* out) const {
100 out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
101 out->data = util::hostToDevice32(0);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102 return true;
103}
104
Adam Lesinski769de982015-04-10 19:43:55 -0700105Id* Id::clone(StringPool* /*newPool*/) const {
Adam Lesinski467f1712015-11-16 17:35:44 -0800106 return new Id(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107}
108
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109void Id::print(std::ostream* out) const {
110 *out << "(id)";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111}
112
Adam Lesinski393b5f02015-12-17 13:03:11 -0800113String::String(const StringPool::Ref& ref) : value(ref), mTranslateable(true) {
114}
115
116void String::setTranslateable(bool val) {
117 mTranslateable = val;
118}
119
120bool String::isTranslateable() const {
121 return mTranslateable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124bool String::flatten(android::Res_value* outValue) const {
125 // Verify that our StringPool index is within encode-able limits.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
127 return false;
128 }
129
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130 outValue->dataType = android::Res_value::TYPE_STRING;
131 outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132 return true;
133}
134
Adam Lesinski769de982015-04-10 19:43:55 -0700135String* String::clone(StringPool* newPool) const {
Adam Lesinskib274e352015-11-06 15:14:35 -0800136 String* str = new String(newPool->makeRef(*value));
137 str->mComment = mComment;
138 str->mSource = mSource;
139 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140}
141
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142void String::print(std::ostream* out) const {
143 *out << "(string) \"" << *value << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144}
145
Adam Lesinski393b5f02015-12-17 13:03:11 -0800146StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref), mTranslateable(true) {
147}
148
149void StyledString::setTranslateable(bool val) {
150 mTranslateable = val;
151}
152
153bool StyledString::isTranslateable() const {
154 return mTranslateable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155}
156
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157bool StyledString::flatten(android::Res_value* outValue) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
159 return false;
160 }
161
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 outValue->dataType = android::Res_value::TYPE_STRING;
163 outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164 return true;
165}
166
Adam Lesinski769de982015-04-10 19:43:55 -0700167StyledString* StyledString::clone(StringPool* newPool) const {
Adam Lesinskib274e352015-11-06 15:14:35 -0800168 StyledString* str = new StyledString(newPool->makeRef(value));
169 str->mComment = mComment;
170 str->mSource = mSource;
171 return str;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172}
173
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174void StyledString::print(std::ostream* out) const {
175 *out << "(styled string) \"" << *value->str << "\"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176}
177
178FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
179}
180
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181bool FileReference::flatten(android::Res_value* outValue) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182 if (path.getIndex() > std::numeric_limits<uint32_t>::max()) {
183 return false;
184 }
185
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 outValue->dataType = android::Res_value::TYPE_STRING;
187 outValue->data = util::hostToDevice32(static_cast<uint32_t>(path.getIndex()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188 return true;
189}
190
Adam Lesinski769de982015-04-10 19:43:55 -0700191FileReference* FileReference::clone(StringPool* newPool) const {
Adam Lesinskib274e352015-11-06 15:14:35 -0800192 FileReference* fr = new FileReference(newPool->makeRef(*path));
193 fr->mComment = mComment;
194 fr->mSource = mSource;
195 return fr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196}
197
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198void FileReference::print(std::ostream* out) const {
199 *out << "(file) " << *path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200}
201
202BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
203}
204
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
206 value.dataType = dataType;
207 value.data = data;
208}
209
210bool BinaryPrimitive::flatten(android::Res_value* outValue) const {
211 outValue->dataType = value.dataType;
212 outValue->data = util::hostToDevice32(value.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213 return true;
214}
215
Adam Lesinski769de982015-04-10 19:43:55 -0700216BinaryPrimitive* BinaryPrimitive::clone(StringPool* /*newPool*/) const {
Adam Lesinski467f1712015-11-16 17:35:44 -0800217 return new BinaryPrimitive(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218}
219
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700220void BinaryPrimitive::print(std::ostream* out) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 switch (value.dataType) {
222 case android::Res_value::TYPE_NULL:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223 *out << "(null)";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 break;
225 case android::Res_value::TYPE_INT_DEC:
Adam Lesinskia5870652015-11-20 15:32:30 -0800226 *out << "(integer) " << static_cast<int32_t>(value.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 break;
228 case android::Res_value::TYPE_INT_HEX:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229 *out << "(integer) " << std::hex << value.data << std::dec;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230 break;
231 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232 *out << "(boolean) " << (value.data != 0 ? "true" : "false");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233 break;
234 case android::Res_value::TYPE_INT_COLOR_ARGB8:
235 case android::Res_value::TYPE_INT_COLOR_RGB8:
236 case android::Res_value::TYPE_INT_COLOR_ARGB4:
237 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238 *out << "(color) #" << std::hex << value.data << std::dec;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239 break;
240 default:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241 *out << "(unknown 0x" << std::hex << (int) value.dataType << ") 0x"
242 << std::hex << value.data << std::dec;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243 break;
244 }
245}
246
Adam Lesinskia5870652015-11-20 15:32:30 -0800247Attribute::Attribute(bool w, uint32_t t) :
Adam Lesinski393b5f02015-12-17 13:03:11 -0800248 typeMask(t),
Adam Lesinskia5870652015-11-20 15:32:30 -0800249 minInt(std::numeric_limits<int32_t>::min()),
250 maxInt(std::numeric_limits<int32_t>::max()) {
Adam Lesinski393b5f02015-12-17 13:03:11 -0800251 mWeak = w;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252}
253
Adam Lesinski769de982015-04-10 19:43:55 -0700254Attribute* Attribute::clone(StringPool* /*newPool*/) const {
Adam Lesinski467f1712015-11-16 17:35:44 -0800255 return new Attribute(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256}
257
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258void Attribute::printMask(std::ostream* out) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259 if (typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 *out << "any";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 return;
262 }
263
264 bool set = false;
265 if ((typeMask & android::ResTable_map::TYPE_REFERENCE) != 0) {
266 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267 set = true;
268 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 *out << "reference";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272 }
273
274 if ((typeMask & android::ResTable_map::TYPE_STRING) != 0) {
275 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276 set = true;
277 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 *out << "string";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281 }
282
283 if ((typeMask & android::ResTable_map::TYPE_INTEGER) != 0) {
284 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285 set = true;
286 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 *out << "integer";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290 }
291
292 if ((typeMask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
293 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294 set = true;
295 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298 *out << "boolean";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299 }
300
301 if ((typeMask & android::ResTable_map::TYPE_COLOR) != 0) {
302 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303 set = true;
304 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 *out << "color";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308 }
309
310 if ((typeMask & android::ResTable_map::TYPE_FLOAT) != 0) {
311 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312 set = true;
313 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700316 *out << "float";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317 }
318
319 if ((typeMask & android::ResTable_map::TYPE_DIMENSION) != 0) {
320 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321 set = true;
322 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700323 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700325 *out << "dimension";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800326 }
327
328 if ((typeMask & android::ResTable_map::TYPE_FRACTION) != 0) {
329 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330 set = true;
331 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700332 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700334 *out << "fraction";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335 }
336
337 if ((typeMask & android::ResTable_map::TYPE_ENUM) != 0) {
338 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800339 set = true;
340 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700343 *out << "enum";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800344 }
345
346 if ((typeMask & android::ResTable_map::TYPE_FLAGS) != 0) {
347 if (!set) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800348 set = true;
349 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350 *out << "|";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700352 *out << "flags";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800353 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700354}
355
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700356void Attribute::print(std::ostream* out) const {
357 *out << "(attr) ";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700358 printMask(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700360 if (!symbols.empty()) {
361 *out << " ["
362 << util::joiner(symbols.begin(), symbols.end(), ", ")
363 << "]";
364 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800365
Adam Lesinski393b5f02015-12-17 13:03:11 -0800366 if (isWeak()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700367 *out << " [weak]";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368 }
369}
370
Adam Lesinskia5870652015-11-20 15:32:30 -0800371static void buildAttributeMismatchMessage(DiagMessage* msg, const Attribute* attr,
372 const Item* value) {
373 *msg << "expected";
374 if (attr->typeMask & android::ResTable_map::TYPE_BOOLEAN) {
375 *msg << " boolean";
376 }
377
378 if (attr->typeMask & android::ResTable_map::TYPE_COLOR) {
379 *msg << " color";
380 }
381
382 if (attr->typeMask & android::ResTable_map::TYPE_DIMENSION) {
383 *msg << " dimension";
384 }
385
386 if (attr->typeMask & android::ResTable_map::TYPE_ENUM) {
387 *msg << " enum";
388 }
389
390 if (attr->typeMask & android::ResTable_map::TYPE_FLAGS) {
391 *msg << " flags";
392 }
393
394 if (attr->typeMask & android::ResTable_map::TYPE_FLOAT) {
395 *msg << " float";
396 }
397
398 if (attr->typeMask & android::ResTable_map::TYPE_FRACTION) {
399 *msg << " fraction";
400 }
401
402 if (attr->typeMask & android::ResTable_map::TYPE_INTEGER) {
403 *msg << " integer";
404 }
405
406 if (attr->typeMask & android::ResTable_map::TYPE_REFERENCE) {
407 *msg << " reference";
408 }
409
410 if (attr->typeMask & android::ResTable_map::TYPE_STRING) {
411 *msg << " string";
412 }
413
414 *msg << " but got " << *value;
415}
416
417bool Attribute::matches(const Item* item, DiagMessage* outMsg) const {
418 android::Res_value val = {};
419 item->flatten(&val);
420
421 // Always allow references.
422 const uint32_t mask = typeMask | android::ResTable_map::TYPE_REFERENCE;
423 if (!(mask & ResourceUtils::androidTypeToAttributeTypeMask(val.dataType))) {
424 if (outMsg) {
425 buildAttributeMismatchMessage(outMsg, this, item);
426 }
427 return false;
428
429 } else if (ResourceUtils::androidTypeToAttributeTypeMask(val.dataType) &
430 android::ResTable_map::TYPE_INTEGER) {
431 if (static_cast<int32_t>(util::deviceToHost32(val.data)) < minInt) {
432 if (outMsg) {
433 *outMsg << *item << " is less than minimum integer " << minInt;
434 }
435 return false;
436 } else if (static_cast<int32_t>(util::deviceToHost32(val.data)) > maxInt) {
437 if (outMsg) {
438 *outMsg << *item << " is greater than maximum integer " << maxInt;
439 }
440 return false;
441 }
442 }
443 return true;
444}
445
Adam Lesinski769de982015-04-10 19:43:55 -0700446Style* Style::clone(StringPool* newPool) const {
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700447 Style* style = new Style();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448 style->parent = parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700449 style->parentInferred = parentInferred;
Adam Lesinskib274e352015-11-06 15:14:35 -0800450 style->mComment = mComment;
451 style->mSource = mSource;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800452 for (auto& entry : entries) {
453 style->entries.push_back(Entry{
454 entry.key,
Adam Lesinski769de982015-04-10 19:43:55 -0700455 std::unique_ptr<Item>(entry.value->clone(newPool))
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 });
457 }
458 return style;
459}
460
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461void Style::print(std::ostream* out) const {
462 *out << "(style) ";
463 if (parent && parent.value().name) {
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800464 if (parent.value().privateReference) {
465 *out << "*";
466 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700467 *out << parent.value().name.value();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700469 *out << " ["
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470 << util::joiner(entries.begin(), entries.end(), ", ")
471 << "]";
472}
473
474static ::std::ostream& operator<<(::std::ostream& out, const Style::Entry& value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700475 if (value.key.name) {
476 out << value.key.name.value();
477 } else {
478 out << "???";
479 }
480 out << " = ";
481 value.value->print(&out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482 return out;
483}
484
Adam Lesinski769de982015-04-10 19:43:55 -0700485Array* Array::clone(StringPool* newPool) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486 Array* array = new Array();
Adam Lesinskib274e352015-11-06 15:14:35 -0800487 array->mComment = mComment;
488 array->mSource = mSource;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800489 for (auto& item : items) {
Adam Lesinski769de982015-04-10 19:43:55 -0700490 array->items.emplace_back(std::unique_ptr<Item>(item->clone(newPool)));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491 }
492 return array;
493}
494
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700495void Array::print(std::ostream* out) const {
496 *out << "(array) ["
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497 << util::joiner(items.begin(), items.end(), ", ")
498 << "]";
499}
500
Adam Lesinski769de982015-04-10 19:43:55 -0700501Plural* Plural::clone(StringPool* newPool) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502 Plural* p = new Plural();
Adam Lesinskib274e352015-11-06 15:14:35 -0800503 p->mComment = mComment;
504 p->mSource = mSource;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505 const size_t count = values.size();
506 for (size_t i = 0; i < count; i++) {
507 if (values[i]) {
Adam Lesinski769de982015-04-10 19:43:55 -0700508 p->values[i] = std::unique_ptr<Item>(values[i]->clone(newPool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509 }
510 }
511 return p;
512}
513
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700514void Plural::print(std::ostream* out) const {
515 *out << "(plural)";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516}
517
518static ::std::ostream& operator<<(::std::ostream& out, const std::unique_ptr<Item>& item) {
519 return out << *item;
520}
521
Adam Lesinski769de982015-04-10 19:43:55 -0700522Styleable* Styleable::clone(StringPool* /*newPool*/) const {
Adam Lesinski467f1712015-11-16 17:35:44 -0800523 return new Styleable(*this);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524}
525
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526void Styleable::print(std::ostream* out) const {
527 *out << "(styleable) " << " ["
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528 << util::joiner(entries.begin(), entries.end(), ", ")
529 << "]";
530}
531
532} // namespace aapt