blob: d380f8d0583d6c8c5010dc11134663604e1fbd11 [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#ifndef AAPT_RESOURCE_VALUES_H
18#define AAPT_RESOURCE_VALUES_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <array>
21#include <ostream>
22#include <vector>
23
24#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
Adam Lesinskia5870652015-11-20 15:32:30 -080027#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "Resource.h"
29#include "StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080030#include "io/File.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080031#include "util/Maybe.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035struct RawValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
37/**
38 * A resource value. This is an all-encompassing representation
39 * of Item and Map and their subclasses. The way to do
40 * type specific operations is to check the Value's type() and
41 * cast it to the appropriate subclass. This isn't super clean,
42 * but it is the simplest strategy.
43 */
44struct Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 /**
48 * Whether this value is weak and can be overridden without
49 * warning or error. Default is false.
50 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 bool IsWeak() const { return weak_; }
Adam Lesinski393b5f02015-12-17 13:03:11 -080052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 void SetWeak(bool val) { weak_ = val; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 // Whether the value is marked as translateable.
56 // This does not persist when flattened.
57 // It is only used during compilation phase.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 void SetTranslateable(bool val) { translateable_ = val; }
Adam Lesinski458b8772016-04-25 14:20:21 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // Default true.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 bool IsTranslateable() const { return translateable_; }
Adam Lesinski458b8772016-04-25 14:20:21 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 /**
64 * Returns the source where this value was defined.
65 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 const Source& GetSource() const { return source_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 void SetSource(const Source& source) { source_ = source; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 void SetSource(Source&& source) { source_ = std::move(source); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 /**
73 * Returns the comment that was associated with this resource.
74 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 const std::string& GetComment() const { return comment_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070076
Adam Lesinskid5083f62017-01-16 15:07:21 -080077 void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 void SetComment(std::string&& str) { comment_ = std::move(str); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 /**
84 * Calls the appropriate overload of ValueVisitor.
85 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 virtual void Accept(RawValueVisitor* visitor) = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 * Clone the value. new_pool is the new StringPool that
90 * any resources with strings should use when copying their string.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 virtual Value* Clone(StringPool* new_pool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 /**
95 * Human readable printout of this value.
96 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 Source source_;
101 std::string comment_;
102 bool weak_ = false;
103 bool translateable_ = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104};
105
106/**
107 * Inherit from this to get visitor accepting implementations for free.
108 */
109template <typename Derived>
110struct BaseValue : public Value {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112};
113
114/**
115 * A resource item with a single value. This maps to android::ResTable_entry.
116 */
117struct Item : public Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 /**
119 * Clone the Item.
120 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 virtual Item* Clone(StringPool* new_pool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 /**
124 * Fills in an android::Res_value structure with this Item's binary
125 * representation.
126 * Returns false if an error occurred.
127 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 virtual bool Flatten(android::Res_value* out_value) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129};
130
131/**
132 * Inherit from this to get visitor accepting implementations for free.
133 */
134template <typename Derived>
135struct BaseItem : public Item {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137};
138
139/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 * A reference to another resource. This maps to
141 * android::Res_value::TYPE_REFERENCE.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 * A reference can be symbolic (with the name set to a valid resource name) or
144 * be
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145 * numeric (the id is set to a valid resource ID).
146 */
147struct Reference : public BaseItem<Reference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 enum class Type {
149 kResource,
150 kAttribute,
151 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 Maybe<ResourceName> name;
154 Maybe<ResourceId> id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 Reference::Type reference_type;
156 bool private_reference = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 Reference();
159 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
160 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 bool Equals(const Value* value) const override;
164 bool Flatten(android::Res_value* out_value) const override;
165 Reference* Clone(StringPool* new_pool) const override;
166 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167};
168
Adam Lesinski8197cc462016-08-19 12:16:49 -0700169bool operator<(const Reference&, const Reference&);
170bool operator==(const Reference&, const Reference&);
171
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172/**
173 * An ID resource. Has no real value, just a place holder.
174 */
175struct Id : public BaseItem<Id> {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 Id() { weak_ = true; }
177 bool Equals(const Value* value) const override;
178 bool Flatten(android::Res_value* out) const override;
179 Id* Clone(StringPool* new_pool) const override;
180 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181};
182
183/**
184 * A raw, unprocessed string. This may contain quotations,
185 * escape sequences, and whitespace. This shall *NOT*
186 * end up in the final resource table.
187 */
188struct RawString : public BaseItem<RawString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 bool Equals(const Value* value) const override;
194 bool Flatten(android::Res_value* out_value) const override;
195 RawString* Clone(StringPool* new_pool) const override;
196 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197};
198
199struct String : public BaseItem<String> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 explicit String(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 bool Equals(const Value* value) const override;
205 bool Flatten(android::Res_value* out_value) const override;
206 String* Clone(StringPool* new_pool) const override;
207 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208};
209
210struct StyledString : public BaseItem<StyledString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 bool Equals(const Value* value) const override;
216 bool Flatten(android::Res_value* out_value) const override;
217 StyledString* Clone(StringPool* new_pool) const override;
218 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219};
220
221struct FileReference : public BaseItem<FileReference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 /**
225 * A handle to the file object from which this file can be read.
226 */
227 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 FileReference() = default;
230 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 bool Equals(const Value* value) const override;
233 bool Flatten(android::Res_value* out_value) const override;
234 FileReference* Clone(StringPool* new_pool) const override;
235 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236};
237
238/**
239 * Represents any other android::Res_value.
240 */
241struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 BinaryPrimitive() = default;
245 explicit BinaryPrimitive(const android::Res_value& val);
246 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 bool Equals(const Value* value) const override;
249 bool Flatten(android::Res_value* out_value) const override;
250 BinaryPrimitive* Clone(StringPool* new_pool) const override;
251 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252};
253
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254struct Attribute : public BaseValue<Attribute> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 struct Symbol {
256 Reference symbol;
257 uint32_t value;
258 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 uint32_t type_mask;
261 int32_t min_int;
262 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 explicit Attribute(bool w, uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 bool Equals(const Value* value) const override;
268 Attribute* Clone(StringPool* new_pool) const override;
269 void PrintMask(std::ostream* out) const;
270 void Print(std::ostream* out) const override;
271 bool Matches(const Item* item, DiagMessage* out_msg) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272};
273
274struct Style : public BaseValue<Style> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 struct Entry {
276 Reference key;
277 std::unique_ptr<Item> value;
278 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 /**
283 * If set to true, the parent was auto inferred from the
284 * style's name.
285 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700287
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 bool Equals(const Value* value) const override;
291 Style* Clone(StringPool* new_pool) const override;
292 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293};
294
295struct Array : public BaseValue<Array> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 std::vector<std::unique_ptr<Item>> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 bool Equals(const Value* value) const override;
299 Array* Clone(StringPool* new_pool) const override;
300 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301};
302
303struct Plural : public BaseValue<Plural> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 bool Equals(const Value* value) const override;
309 Plural* Clone(StringPool* new_pool) const override;
310 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800311};
312
313struct Styleable : public BaseValue<Styleable> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 bool Equals(const Value* value) const override;
317 Styleable* Clone(StringPool* newPool) const override;
318 void Print(std::ostream* out) const override;
319 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800320};
321
322/**
323 * Stream operator for printing Value objects.
324 */
325inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 value.Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330inline ::std::ostream& operator<<(::std::ostream& out,
331 const Attribute::Symbol& s) {
332 if (s.symbol.name) {
333 out << s.symbol.name.value().entry;
334 } else {
335 out << "???";
336 }
337 return out << "=" << s.value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338}
339
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342#endif // AAPT_RESOURCE_VALUES_H