blob: c71c738892dadacd963a240b420e51cfaf3d2e2d [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
Adam Lesinski75421622017-01-06 15:20:04 -080037// 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.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042struct Value {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044
Adam Lesinski75421622017-01-06 15:20:04 -080045 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 bool IsWeak() const { return weak_; }
Adam Lesinski393b5f02015-12-17 13:03:11 -080047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 void SetWeak(bool val) { weak_ = val; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinski75421622017-01-06 15:20:04 -080050 // Whether the value is marked as translatable.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 // This does not persist when flattened.
52 // It is only used during compilation phase.
Adam Lesinski75421622017-01-06 15:20:04 -080053 void SetTranslatable(bool val) { translatable_ = val; }
Adam Lesinski458b8772016-04-25 14:20:21 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 // Default true.
Adam Lesinski75421622017-01-06 15:20:04 -080056 bool IsTranslatable() const { return translatable_; }
Adam Lesinski458b8772016-04-25 14:20:21 -070057
Adam Lesinski75421622017-01-06 15:20:04 -080058 // Returns the source where this value was defined.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 const Source& GetSource() const { return source_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070060
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 void SetSource(const Source& source) { source_ = source; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 void SetSource(Source&& source) { source_ = std::move(source); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070064
Adam Lesinski75421622017-01-06 15:20:04 -080065 // Returns the comment that was associated with this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 const std::string& GetComment() const { return comment_; }
Adam Lesinskie78fd612015-10-22 12:48:43 -070067
Adam Lesinskid5083f62017-01-16 15:07:21 -080068 void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 void SetComment(std::string&& str) { comment_ = std::move(str); }
Adam Lesinskie78fd612015-10-22 12:48:43 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070073
Adam Lesinski75421622017-01-06 15:20:04 -080074 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 virtual void Accept(RawValueVisitor* visitor) = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinski75421622017-01-06 15:20:04 -080077 // Clone the value. `new_pool` is the new StringPool that
78 // any resources with strings should use when copying their string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 virtual Value* Clone(StringPool* new_pool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
Adam Lesinski75421622017-01-06 15:20:04 -080081 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 Source source_;
86 std::string comment_;
87 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -080088 bool translatable_ = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089};
90
Adam Lesinski75421622017-01-06 15:20:04 -080091// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092template <typename Derived>
93struct BaseValue : public Value {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095};
96
Adam Lesinski75421622017-01-06 15:20:04 -080097// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -080099 // Clone the Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 virtual Item* Clone(StringPool* new_pool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinski75421622017-01-06 15:20:04 -0800102 // Fills in an android::Res_value structure with this Item's binary representation.
103 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 virtual bool Flatten(android::Res_value* out_value) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105};
106
Adam Lesinski75421622017-01-06 15:20:04 -0800107// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108template <typename Derived>
109struct BaseItem : public Item {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 void Accept(RawValueVisitor* visitor) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111};
112
Adam Lesinski75421622017-01-06 15:20:04 -0800113// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
114// A reference can be symbolic (with the name set to a valid resource name) or be
115// numeric (the id is set to a valid resource ID).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116struct Reference : public BaseItem<Reference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 enum class Type {
118 kResource,
119 kAttribute,
120 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 Maybe<ResourceName> name;
123 Maybe<ResourceId> id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 Reference::Type reference_type;
125 bool private_reference = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 Reference();
128 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
129 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 bool Equals(const Value* value) const override;
133 bool Flatten(android::Res_value* out_value) const override;
134 Reference* Clone(StringPool* new_pool) const override;
135 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136};
137
Adam Lesinski8197cc462016-08-19 12:16:49 -0700138bool operator<(const Reference&, const Reference&);
139bool operator==(const Reference&, const Reference&);
140
Adam Lesinski75421622017-01-06 15:20:04 -0800141// An ID resource. Has no real value, just a place holder.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142struct Id : public BaseItem<Id> {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 Id() { weak_ = true; }
144 bool Equals(const Value* value) const override;
145 bool Flatten(android::Res_value* out) const override;
146 Id* Clone(StringPool* new_pool) const override;
147 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148};
149
Adam Lesinski75421622017-01-06 15:20:04 -0800150// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
151// This shall *NOT* end up in the final resource table.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152struct RawString : public BaseItem<RawString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 bool Equals(const Value* value) const override;
158 bool Flatten(android::Res_value* out_value) const override;
159 RawString* Clone(StringPool* new_pool) const override;
160 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161};
162
Adam Lesinski75421622017-01-06 15:20:04 -0800163// Identifies a range of characters in a string that are untranslatable.
164// These should not be pseudolocalized. The start and end indices are measured in bytes.
165struct UntranslatableSection {
166 // Start offset inclusive.
167 size_t start;
168
169 // End offset exclusive.
170 size_t end;
171};
172
173inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
174 return a.start == b.start && a.end == b.end;
175}
176
177inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
178 return a.start != b.start || a.end != b.end;
179}
180
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181struct String : public BaseItem<String> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinski75421622017-01-06 15:20:04 -0800184 // Sections of the string to NOT translate. Mainly used
185 // for pseudolocalization. This data is NOT persisted
186 // in any format.
187 std::vector<UntranslatableSection> untranslatable_sections;
188
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 explicit String(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 bool Equals(const Value* value) const override;
192 bool Flatten(android::Res_value* out_value) const override;
193 String* Clone(StringPool* new_pool) const override;
194 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195};
196
197struct StyledString : public BaseItem<StyledString> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski75421622017-01-06 15:20:04 -0800200 // Sections of the string to NOT translate. Mainly used
201 // for pseudolocalization. This data is NOT persisted
202 // in any format.
203 std::vector<UntranslatableSection> untranslatable_sections;
204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 bool Equals(const Value* value) const override;
208 bool Flatten(android::Res_value* out_value) const override;
209 StyledString* Clone(StringPool* new_pool) const override;
210 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211};
212
213struct FileReference : public BaseItem<FileReference> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinski75421622017-01-06 15:20:04 -0800216 // A handle to the file object from which this file can be read.
217 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 FileReference() = default;
221 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 bool Equals(const Value* value) const override;
224 bool Flatten(android::Res_value* out_value) const override;
225 FileReference* Clone(StringPool* new_pool) const override;
226 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227};
228
Adam Lesinski75421622017-01-06 15:20:04 -0800229// Represents any other android::Res_value.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 BinaryPrimitive() = default;
234 explicit BinaryPrimitive(const android::Res_value& val);
235 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 bool Equals(const Value* value) const override;
238 bool Flatten(android::Res_value* out_value) const override;
239 BinaryPrimitive* Clone(StringPool* new_pool) const override;
240 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241};
242
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243struct Attribute : public BaseValue<Attribute> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 struct Symbol {
245 Reference symbol;
246 uint32_t value;
247 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 uint32_t type_mask;
250 int32_t min_int;
251 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 explicit Attribute(bool w, uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 bool Equals(const Value* value) const override;
257 Attribute* Clone(StringPool* new_pool) const override;
258 void PrintMask(std::ostream* out) const;
259 void Print(std::ostream* out) const override;
260 bool Matches(const Item* item, DiagMessage* out_msg) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261};
262
263struct Style : public BaseValue<Style> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 struct Entry {
265 Reference key;
266 std::unique_ptr<Item> value;
267 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700270
Adam Lesinski75421622017-01-06 15:20:04 -0800271 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700273
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 bool Equals(const Value* value) const override;
277 Style* Clone(StringPool* new_pool) const override;
278 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279};
280
281struct Array : public BaseValue<Array> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 std::vector<std::unique_ptr<Item>> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 bool Equals(const Value* value) const override;
285 Array* Clone(StringPool* new_pool) const override;
286 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287};
288
289struct Plural : public BaseValue<Plural> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 bool Equals(const Value* value) const override;
295 Plural* Clone(StringPool* new_pool) const override;
296 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297};
298
299struct Styleable : public BaseValue<Styleable> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 bool Equals(const Value* value) const override;
303 Styleable* Clone(StringPool* newPool) const override;
304 void Print(std::ostream* out) const override;
305 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306};
307
Adam Lesinski75421622017-01-06 15:20:04 -0800308// Stream operator for printing Value objects.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800309inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 value.Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314inline ::std::ostream& operator<<(::std::ostream& out,
315 const Attribute::Symbol& s) {
316 if (s.symbol.name) {
317 out << s.symbol.name.value().entry;
318 } else {
319 out << "???";
320 }
321 return out << "=" << s.value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322}
323
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800325
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326#endif // AAPT_RESOURCE_VALUES_H