blob: 7e7547fc1b94b71dcd848fa0fc0083bf0719e851 [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 Lesinski5924d8c2017-05-30 15:15:58 -070043class Value {
44 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinski75421622017-01-06 15:20:04 -080047 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 bool IsWeak() const { return weak_; }
Adam Lesinski393b5f02015-12-17 13:03:11 -080049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 void SetWeak(bool val) { weak_ = val; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinski75421622017-01-06 15:20:04 -080052 // Whether the value is marked as translatable.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 // This does not persist when flattened.
54 // It is only used during compilation phase.
Adam Lesinski75421622017-01-06 15:20:04 -080055 void SetTranslatable(bool val) { translatable_ = val; }
Adam Lesinski458b8772016-04-25 14:20:21 -070056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 // Default true.
Adam Lesinski75421622017-01-06 15:20:04 -080058 bool IsTranslatable() const { return translatable_; }
Adam Lesinski458b8772016-04-25 14:20:21 -070059
Adam Lesinski75421622017-01-06 15:20:04 -080060 // Returns the source where this value was defined.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 const Source& GetSource() const { return source_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 void SetSource(const Source& source) { source_ = source; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 void SetSource(Source&& source) { source_ = std::move(source); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070066
Adam Lesinski75421622017-01-06 15:20:04 -080067 // Returns the comment that was associated with this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 const std::string& GetComment() const { return comment_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070069
Adam Lesinskid5083f62017-01-16 15:07:21 -080070 void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 void SetComment(std::string&& str) { comment_ = std::move(str); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070075
Adam Lesinski75421622017-01-06 15:20:04 -080076 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 virtual void Accept(RawValueVisitor* visitor) = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinski75421622017-01-06 15:20:04 -080079 // Clone the value. `new_pool` is the new StringPool that
80 // any resources with strings should use when copying their string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 virtual Value* Clone(StringPool* new_pool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinski75421622017-01-06 15:20:04 -080083 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -070085
Adam Lesinski5924d8c2017-05-30 15:15:58 -070086 friend std::ostream& operator<<(std::ostream& out, const Value& value);
87
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 Source source_;
90 std::string comment_;
91 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -080092 bool translatable_ = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093};
94
Adam Lesinski75421622017-01-06 15:20:04 -080095// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096template <typename Derived>
97struct BaseValue : public Value {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099};
100
Adam Lesinski75421622017-01-06 15:20:04 -0800101// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800103 // Clone the Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 virtual Item* Clone(StringPool* new_pool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinski75421622017-01-06 15:20:04 -0800106 // Fills in an android::Res_value structure with this Item's binary representation.
107 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 virtual bool Flatten(android::Res_value* out_value) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109};
110
Adam Lesinski75421622017-01-06 15:20:04 -0800111// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112template <typename Derived>
113struct BaseItem : public Item {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115};
116
Adam Lesinski75421622017-01-06 15:20:04 -0800117// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
118// A reference can be symbolic (with the name set to a valid resource name) or be
119// numeric (the id is set to a valid resource ID).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120struct Reference : public BaseItem<Reference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 enum class Type {
122 kResource,
123 kAttribute,
124 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 Maybe<ResourceName> name;
127 Maybe<ResourceId> id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 Reference::Type reference_type;
129 bool private_reference = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 Reference();
132 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
133 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 bool Equals(const Value* value) const override;
137 bool Flatten(android::Res_value* out_value) const override;
138 Reference* Clone(StringPool* new_pool) const override;
139 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140};
141
Adam Lesinski8197cc462016-08-19 12:16:49 -0700142bool operator<(const Reference&, const Reference&);
143bool operator==(const Reference&, const Reference&);
144
Adam Lesinski75421622017-01-06 15:20:04 -0800145// An ID resource. Has no real value, just a place holder.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146struct Id : public BaseItem<Id> {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 Id() { weak_ = true; }
148 bool Equals(const Value* value) const override;
149 bool Flatten(android::Res_value* out) const override;
150 Id* Clone(StringPool* new_pool) const override;
151 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152};
153
Adam Lesinski75421622017-01-06 15:20:04 -0800154// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
155// This shall *NOT* end up in the final resource table.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156struct RawString : public BaseItem<RawString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 bool Equals(const Value* value) const override;
162 bool Flatten(android::Res_value* out_value) const override;
163 RawString* Clone(StringPool* new_pool) const override;
164 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165};
166
Adam Lesinski75421622017-01-06 15:20:04 -0800167// Identifies a range of characters in a string that are untranslatable.
168// These should not be pseudolocalized. The start and end indices are measured in bytes.
169struct UntranslatableSection {
170 // Start offset inclusive.
171 size_t start;
172
173 // End offset exclusive.
174 size_t end;
175};
176
177inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
178 return a.start == b.start && a.end == b.end;
179}
180
181inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
182 return a.start != b.start || a.end != b.end;
183}
184
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185struct String : public BaseItem<String> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski75421622017-01-06 15:20:04 -0800188 // Sections of the string to NOT translate. Mainly used
189 // for pseudolocalization. This data is NOT persisted
190 // in any format.
191 std::vector<UntranslatableSection> untranslatable_sections;
192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 explicit String(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 bool Equals(const Value* value) const override;
196 bool Flatten(android::Res_value* out_value) const override;
197 String* Clone(StringPool* new_pool) const override;
198 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199};
200
201struct StyledString : public BaseItem<StyledString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski75421622017-01-06 15:20:04 -0800204 // Sections of the string to NOT translate. Mainly used
205 // for pseudolocalization. This data is NOT persisted
206 // in any format.
207 std::vector<UntranslatableSection> untranslatable_sections;
208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 bool Equals(const Value* value) const override;
212 bool Flatten(android::Res_value* out_value) const override;
213 StyledString* Clone(StringPool* new_pool) const override;
214 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215};
216
217struct FileReference : public BaseItem<FileReference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219
Adam Lesinski75421622017-01-06 15:20:04 -0800220 // A handle to the file object from which this file can be read.
221 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 FileReference() = default;
225 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 bool Equals(const Value* value) const override;
228 bool Flatten(android::Res_value* out_value) const override;
229 FileReference* Clone(StringPool* new_pool) const override;
230 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231};
232
Adam Lesinski75421622017-01-06 15:20:04 -0800233// Represents any other android::Res_value.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 BinaryPrimitive() = default;
238 explicit BinaryPrimitive(const android::Res_value& val);
239 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 bool Equals(const Value* value) const override;
242 bool Flatten(android::Res_value* out_value) const override;
243 BinaryPrimitive* Clone(StringPool* new_pool) const override;
244 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245};
246
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247struct Attribute : public BaseValue<Attribute> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 struct Symbol {
249 Reference symbol;
250 uint32_t value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700251
252 friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 uint32_t type_mask;
256 int32_t min_int;
257 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259
Adam Lesinskic744ae82017-05-17 19:28:38 -0700260 Attribute();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 explicit Attribute(bool w, uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 bool Equals(const Value* value) const override;
264 Attribute* Clone(StringPool* new_pool) const override;
265 void PrintMask(std::ostream* out) const;
266 void Print(std::ostream* out) const override;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700267 bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268};
269
270struct Style : public BaseValue<Style> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 struct Entry {
272 Reference key;
273 std::unique_ptr<Item> value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700274
275 friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700279
Adam Lesinski75421622017-01-06 15:20:04 -0800280 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 bool Equals(const Value* value) const override;
286 Style* Clone(StringPool* new_pool) const override;
287 void Print(std::ostream* out) const override;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700288
289 // Merges `style` into this Style. All identical attributes of `style` take precedence, including
290 // the parent, if there is one.
291 void MergeWith(Style* style, StringPool* pool);
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
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700321template <typename T>
322typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
323 std::ostream& out, const std::unique_ptr<T>& value) {
324 if (value == nullptr) {
325 out << "NULL";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700327 value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700329 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330}
331
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334#endif // AAPT_RESOURCE_VALUES_H