blob: ea73615e372a4e8c6be14db3f064c2d94f1782af [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"
25
Adam Lesinskia5870652015-11-20 15:32:30 -080026#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "Resource.h"
28#include "StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080029#include "io/File.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080030#include "util/Maybe.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034struct RawValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
36/**
37 * A resource value. This is an all-encompassing representation
38 * of Item and Map and their subclasses. The way to do
39 * type specific operations is to check the Value's type() and
40 * cast it to the appropriate subclass. This isn't super clean,
41 * but it is the simplest strategy.
42 */
43struct Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 /**
47 * Whether this value is weak and can be overridden without
48 * warning or error. Default is false.
49 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 bool IsWeak() const { return weak_; }
Adam Lesinski393b5f02015-12-17 13:03:11 -080051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 void SetWeak(bool val) { weak_ = val; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 // Whether the value is marked as translateable.
55 // This does not persist when flattened.
56 // It is only used during compilation phase.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 void SetTranslateable(bool val) { translateable_ = val; }
Adam Lesinski458b8772016-04-25 14:20:21 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 // Default true.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 bool IsTranslateable() const { return translateable_; }
Adam Lesinski458b8772016-04-25 14:20:21 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 /**
63 * Returns the source where this value was defined.
64 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 const Source& GetSource() const { return source_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 void SetSource(const Source& source) { source_ = source; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 void SetSource(Source&& source) { source_ = std::move(source); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 /**
72 * Returns the comment that was associated with this resource.
73 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 const std::string& GetComment() const { return comment_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 void SetComment(const StringPiece& str) { comment_ = str.ToString(); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 void SetComment(std::string&& str) { comment_ = std::move(str); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 /**
83 * Calls the appropriate overload of ValueVisitor.
84 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 virtual void Accept(RawValueVisitor* visitor) = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 * Clone the value. new_pool is the new StringPool that
89 * any resources with strings should use when copying their string.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 virtual Value* Clone(StringPool* new_pool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 /**
94 * Human readable printout of this value.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 Source source_;
100 std::string comment_;
101 bool weak_ = false;
102 bool translateable_ = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103};
104
105/**
106 * Inherit from this to get visitor accepting implementations for free.
107 */
108template <typename Derived>
109struct BaseValue : public Value {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111};
112
113/**
114 * A resource item with a single value. This maps to android::ResTable_entry.
115 */
116struct Item : public Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 /**
118 * Clone the Item.
119 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 virtual Item* Clone(StringPool* new_pool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 /**
123 * Fills in an android::Res_value structure with this Item's binary
124 * representation.
125 * Returns false if an error occurred.
126 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 virtual bool Flatten(android::Res_value* out_value) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128};
129
130/**
131 * Inherit from this to get visitor accepting implementations for free.
132 */
133template <typename Derived>
134struct BaseItem : public Item {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136};
137
138/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 * A reference to another resource. This maps to
140 * android::Res_value::TYPE_REFERENCE.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 * A reference can be symbolic (with the name set to a valid resource name) or
143 * be
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144 * numeric (the id is set to a valid resource ID).
145 */
146struct Reference : public BaseItem<Reference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 enum class Type {
148 kResource,
149 kAttribute,
150 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 Maybe<ResourceName> name;
153 Maybe<ResourceId> id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 Reference::Type reference_type;
155 bool private_reference = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 Reference();
158 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
159 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 bool Equals(const Value* value) const override;
163 bool Flatten(android::Res_value* out_value) const override;
164 Reference* Clone(StringPool* new_pool) const override;
165 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166};
167
Adam Lesinski8197cc462016-08-19 12:16:49 -0700168bool operator<(const Reference&, const Reference&);
169bool operator==(const Reference&, const Reference&);
170
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171/**
172 * An ID resource. Has no real value, just a place holder.
173 */
174struct Id : public BaseItem<Id> {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 Id() { weak_ = true; }
176 bool Equals(const Value* value) const override;
177 bool Flatten(android::Res_value* out) const override;
178 Id* Clone(StringPool* new_pool) const override;
179 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180};
181
182/**
183 * A raw, unprocessed string. This may contain quotations,
184 * escape sequences, and whitespace. This shall *NOT*
185 * end up in the final resource table.
186 */
187struct RawString : public BaseItem<RawString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 bool Equals(const Value* value) const override;
193 bool Flatten(android::Res_value* out_value) const override;
194 RawString* Clone(StringPool* new_pool) const override;
195 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196};
197
198struct String : public BaseItem<String> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 explicit String(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 bool Equals(const Value* value) const override;
204 bool Flatten(android::Res_value* out_value) const override;
205 String* Clone(StringPool* new_pool) const override;
206 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207};
208
209struct StyledString : public BaseItem<StyledString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 bool Equals(const Value* value) const override;
215 bool Flatten(android::Res_value* out_value) const override;
216 StyledString* Clone(StringPool* new_pool) const override;
217 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218};
219
220struct FileReference : public BaseItem<FileReference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 /**
224 * A handle to the file object from which this file can be read.
225 */
226 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 FileReference() = default;
229 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 bool Equals(const Value* value) const override;
232 bool Flatten(android::Res_value* out_value) const override;
233 FileReference* Clone(StringPool* new_pool) const override;
234 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235};
236
237/**
238 * Represents any other android::Res_value.
239 */
240struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 BinaryPrimitive() = default;
244 explicit BinaryPrimitive(const android::Res_value& val);
245 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 bool Equals(const Value* value) const override;
248 bool Flatten(android::Res_value* out_value) const override;
249 BinaryPrimitive* Clone(StringPool* new_pool) const override;
250 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251};
252
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253struct Attribute : public BaseValue<Attribute> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 struct Symbol {
255 Reference symbol;
256 uint32_t value;
257 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 uint32_t type_mask;
260 int32_t min_int;
261 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 explicit Attribute(bool w, uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 bool Equals(const Value* value) const override;
267 Attribute* Clone(StringPool* new_pool) const override;
268 void PrintMask(std::ostream* out) const;
269 void Print(std::ostream* out) const override;
270 bool Matches(const Item* item, DiagMessage* out_msg) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271};
272
273struct Style : public BaseValue<Style> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 struct Entry {
275 Reference key;
276 std::unique_ptr<Item> value;
277 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 /**
282 * If set to true, the parent was auto inferred from the
283 * style's name.
284 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 bool Equals(const Value* value) const override;
290 Style* Clone(StringPool* new_pool) const override;
291 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292};
293
294struct Array : public BaseValue<Array> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 std::vector<std::unique_ptr<Item>> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 bool Equals(const Value* value) const override;
298 Array* Clone(StringPool* new_pool) const override;
299 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300};
301
302struct Plural : public BaseValue<Plural> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 bool Equals(const Value* value) const override;
308 Plural* Clone(StringPool* new_pool) const override;
309 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310};
311
312struct Styleable : public BaseValue<Styleable> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 bool Equals(const Value* value) const override;
316 Styleable* Clone(StringPool* newPool) const override;
317 void Print(std::ostream* out) const override;
318 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319};
320
321/**
322 * Stream operator for printing Value objects.
323 */
324inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 value.Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329inline ::std::ostream& operator<<(::std::ostream& out,
330 const Attribute::Symbol& s) {
331 if (s.symbol.name) {
332 out << s.symbol.name.value().entry;
333 } else {
334 out << "???";
335 }
336 return out << "=" << s.value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800340
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341#endif // AAPT_RESOURCE_VALUES_H