blob: 06f949f9555c7b147bf1ffe2c82b8636699c4ce6 [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>
Adam Lesinskic744ae82017-05-17 19:28:38 -070021#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include <ostream>
23#include <vector>
24
25#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027
Adam Lesinskia5870652015-11-20 15:32:30 -080028#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include "Resource.h"
30#include "StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080031#include "io/File.h"
Adam Lesinskia5870652015-11-20 15:32:30 -080032#include "util/Maybe.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034namespace aapt {
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036struct RawValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski75421622017-01-06 15:20:04 -080038// 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.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043struct Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinski75421622017-01-06 15:20:04 -080046 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 bool IsWeak() const { return weak_; }
Adam Lesinski393b5f02015-12-17 13:03:11 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void SetWeak(bool val) { weak_ = val; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinski75421622017-01-06 15:20:04 -080051 // Whether the value is marked as translatable.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // This does not persist when flattened.
53 // It is only used during compilation phase.
Adam Lesinski75421622017-01-06 15:20:04 -080054 void SetTranslatable(bool val) { translatable_ = val; }
Adam Lesinski458b8772016-04-25 14:20:21 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 // Default true.
Adam Lesinski75421622017-01-06 15:20:04 -080057 bool IsTranslatable() const { return translatable_; }
Adam Lesinski458b8772016-04-25 14:20:21 -070058
Adam Lesinski75421622017-01-06 15:20:04 -080059 // Returns the source where this value was defined.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 const Source& GetSource() const { return source_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 void SetSource(const Source& source) { source_ = source; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 void SetSource(Source&& source) { source_ = std::move(source); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070065
Adam Lesinski75421622017-01-06 15:20:04 -080066 // Returns the comment that was associated with this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 const std::string& GetComment() const { return comment_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070068
Adam Lesinskid5083f62017-01-16 15:07:21 -080069 void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 void SetComment(std::string&& str) { comment_ = std::move(str); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070074
Adam Lesinski75421622017-01-06 15:20:04 -080075 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 virtual void Accept(RawValueVisitor* visitor) = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinski75421622017-01-06 15:20:04 -080078 // Clone the value. `new_pool` is the new StringPool that
79 // any resources with strings should use when copying their string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 virtual Value* Clone(StringPool* new_pool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinski75421622017-01-06 15:20:04 -080082 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 Source source_;
87 std::string comment_;
88 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -080089 bool translatable_ = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090};
91
Adam Lesinski75421622017-01-06 15:20:04 -080092// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093template <typename Derived>
94struct BaseValue : public Value {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096};
97
Adam Lesinski75421622017-01-06 15:20:04 -080098// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800100 // Clone the Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 virtual Item* Clone(StringPool* new_pool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinski75421622017-01-06 15:20:04 -0800103 // Fills in an android::Res_value structure with this Item's binary representation.
104 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 virtual bool Flatten(android::Res_value* out_value) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106};
107
Adam Lesinski75421622017-01-06 15:20:04 -0800108// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109template <typename Derived>
110struct BaseItem : public Item {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112};
113
Adam Lesinski75421622017-01-06 15:20:04 -0800114// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
115// A reference can be symbolic (with the name set to a valid resource name) or be
116// numeric (the id is set to a valid resource ID).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117struct Reference : public BaseItem<Reference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 enum class Type {
119 kResource,
120 kAttribute,
121 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 Maybe<ResourceName> name;
124 Maybe<ResourceId> id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 Reference::Type reference_type;
126 bool private_reference = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 Reference();
129 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
130 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 bool Equals(const Value* value) const override;
134 bool Flatten(android::Res_value* out_value) const override;
135 Reference* Clone(StringPool* new_pool) const override;
136 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137};
138
Adam Lesinski8197cc462016-08-19 12:16:49 -0700139bool operator<(const Reference&, const Reference&);
140bool operator==(const Reference&, const Reference&);
141
Adam Lesinski75421622017-01-06 15:20:04 -0800142// An ID resource. Has no real value, just a place holder.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143struct Id : public BaseItem<Id> {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 Id() { weak_ = true; }
145 bool Equals(const Value* value) const override;
146 bool Flatten(android::Res_value* out) const override;
147 Id* Clone(StringPool* new_pool) const override;
148 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149};
150
Adam Lesinski75421622017-01-06 15:20:04 -0800151// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
152// This shall *NOT* end up in the final resource table.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800153struct RawString : public BaseItem<RawString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 bool Equals(const Value* value) const override;
159 bool Flatten(android::Res_value* out_value) const override;
160 RawString* Clone(StringPool* new_pool) const override;
161 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162};
163
Adam Lesinski75421622017-01-06 15:20:04 -0800164// Identifies a range of characters in a string that are untranslatable.
165// These should not be pseudolocalized. The start and end indices are measured in bytes.
166struct UntranslatableSection {
167 // Start offset inclusive.
168 size_t start;
169
170 // End offset exclusive.
171 size_t end;
172};
173
174inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
175 return a.start == b.start && a.end == b.end;
176}
177
178inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
179 return a.start != b.start || a.end != b.end;
180}
181
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182struct String : public BaseItem<String> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinski75421622017-01-06 15:20:04 -0800185 // Sections of the string to NOT translate. Mainly used
186 // for pseudolocalization. This data is NOT persisted
187 // in any format.
188 std::vector<UntranslatableSection> untranslatable_sections;
189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 explicit String(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 String* Clone(StringPool* new_pool) const override;
195 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196};
197
198struct StyledString : public BaseItem<StyledString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinski75421622017-01-06 15:20:04 -0800201 // Sections of the string to NOT translate. Mainly used
202 // for pseudolocalization. This data is NOT persisted
203 // in any format.
204 std::vector<UntranslatableSection> untranslatable_sections;
205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 bool Equals(const Value* value) const override;
209 bool Flatten(android::Res_value* out_value) const override;
210 StyledString* Clone(StringPool* new_pool) const override;
211 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212};
213
214struct FileReference : public BaseItem<FileReference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216
Adam Lesinski75421622017-01-06 15:20:04 -0800217 // A handle to the file object from which this file can be read.
218 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 FileReference() = default;
222 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 bool Equals(const Value* value) const override;
225 bool Flatten(android::Res_value* out_value) const override;
226 FileReference* Clone(StringPool* new_pool) const override;
227 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228};
229
Adam Lesinski75421622017-01-06 15:20:04 -0800230// Represents any other android::Res_value.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 BinaryPrimitive() = default;
235 explicit BinaryPrimitive(const android::Res_value& val);
236 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 bool Equals(const Value* value) const override;
239 bool Flatten(android::Res_value* out_value) const override;
240 BinaryPrimitive* Clone(StringPool* new_pool) const override;
241 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242};
243
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244struct Attribute : public BaseValue<Attribute> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 struct Symbol {
246 Reference symbol;
247 uint32_t value;
248 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 uint32_t type_mask;
251 int32_t min_int;
252 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254
Adam Lesinskic744ae82017-05-17 19:28:38 -0700255 Attribute();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 explicit Attribute(bool w, uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 bool Equals(const Value* value) const override;
259 Attribute* Clone(StringPool* new_pool) const override;
260 void PrintMask(std::ostream* out) const;
261 void Print(std::ostream* out) const override;
262 bool Matches(const Item* item, DiagMessage* out_msg) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263};
264
265struct Style : public BaseValue<Style> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 struct Entry {
267 Reference key;
268 std::unique_ptr<Item> value;
269 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700272
Adam Lesinski75421622017-01-06 15:20:04 -0800273 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700275
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 bool Equals(const Value* value) const override;
279 Style* Clone(StringPool* new_pool) const override;
280 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281};
282
283struct Array : public BaseValue<Array> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 std::vector<std::unique_ptr<Item>> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 bool Equals(const Value* value) const override;
287 Array* Clone(StringPool* new_pool) const override;
288 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289};
290
291struct Plural : public BaseValue<Plural> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 bool Equals(const Value* value) const override;
297 Plural* Clone(StringPool* new_pool) const override;
298 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299};
300
301struct Styleable : public BaseValue<Styleable> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 bool Equals(const Value* value) const override;
305 Styleable* Clone(StringPool* newPool) const override;
306 void Print(std::ostream* out) const override;
307 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308};
309
Adam Lesinski75421622017-01-06 15:20:04 -0800310// Stream operator for printing Value objects.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800311inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 value.Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316inline ::std::ostream& operator<<(::std::ostream& out,
317 const Attribute::Symbol& s) {
318 if (s.symbol.name) {
319 out << s.symbol.name.value().entry;
320 } else {
321 out << "???";
322 }
323 return out << "=" << s.value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324}
325
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328#endif // AAPT_RESOURCE_VALUES_H