blob: 1694d6b6fe4a980ed7ecc8c5ef6099bd2ac0c17f [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"
Ryan Mitchellefcdb952021-04-14 17:31:37 -070031#include "ValueTransformer.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080032#include "io/File.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070033#include "text/Printer.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035namespace aapt {
36
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070037class ValueVisitor;
38class ConstValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinski75421622017-01-06 15:20:04 -080040// A resource value. This is an all-encompassing representation
41// of Item and Map and their subclasses. The way to do
42// type specific operations is to check the Value's type() and
43// cast it to the appropriate subclass. This isn't super clean,
44// but it is the simplest strategy.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070045class Value {
46 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinski75421622017-01-06 15:20:04 -080049 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070050 bool IsWeak() const {
51 return weak_;
52 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080053
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070054 void SetWeak(bool val) {
55 weak_ = val;
56 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070058 // Whether the value is marked as translatable. This does not persist when flattened to binary.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 // It is only used during compilation phase.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070060 void SetTranslatable(bool val) {
61 translatable_ = val;
62 }
Adam Lesinski458b8772016-04-25 14:20:21 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 // Default true.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070065 bool IsTranslatable() const {
66 return translatable_;
67 }
Adam Lesinski458b8772016-04-25 14:20:21 -070068
Adam Lesinski75421622017-01-06 15:20:04 -080069 // Returns the source where this value was defined.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070070 const Source& GetSource() const {
71 return source_;
72 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070073
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070074 void SetSource(const Source& source) {
75 source_ = source;
76 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070077
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070078 void SetSource(Source&& source) {
79 source_ = std::move(source);
80 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070081
Adam Lesinski75421622017-01-06 15:20:04 -080082 // Returns the comment that was associated with this resource.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070083 const std::string& GetComment() const {
84 return comment_;
85 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070086
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070087 void SetComment(const android::StringPiece& str) {
88 comment_ = str.to_string();
89 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070090
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070091 void SetComment(std::string&& str) {
92 comment_ = std::move(str);
93 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070096
Adam Lesinski75421622017-01-06 15:20:04 -080097 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070098 virtual void Accept(ValueVisitor* visitor) = 0;
99
100 // Calls the appropriate overload of ConstValueVisitor.
101 virtual void Accept(ConstValueVisitor* visitor) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700103 // Transform this Value into another Value using the transformer.
104 std::unique_ptr<Value> Transform(ValueTransformer& transformer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinski75421622017-01-06 15:20:04 -0800106 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700108
Adam Lesinski93190b72017-11-03 15:20:17 -0700109 // Human readable printout of this value that may omit some information for the sake
110 // of brevity and readability. Default implementation just calls Print().
111 virtual void PrettyPrint(text::Printer* printer) const;
112
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700113 friend std::ostream& operator<<(std::ostream& out, const Value& value);
114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 Source source_;
117 std::string comment_;
118 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -0800119 bool translatable_ = true;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700120
121 private:
122 virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123};
124
Adam Lesinski75421622017-01-06 15:20:04 -0800125// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126template <typename Derived>
127struct BaseValue : public Value {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700128 void Accept(ValueVisitor* visitor) override;
129 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130};
131
Adam Lesinski75421622017-01-06 15:20:04 -0800132// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800134 // Fills in an android::Res_value structure with this Item's binary representation.
135 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 virtual bool Flatten(android::Res_value* out_value) const = 0;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700137
138 // Transform this Item into another Item using the transformer.
139 std::unique_ptr<Item> Transform(ValueTransformer& transformer) const;
140
141 private:
142 virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143};
144
Adam Lesinski75421622017-01-06 15:20:04 -0800145// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146template <typename Derived>
147struct BaseItem : public Item {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700148 void Accept(ValueVisitor* visitor) override;
149 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150};
151
Adam Lesinski75421622017-01-06 15:20:04 -0800152// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
153// A reference can be symbolic (with the name set to a valid resource name) or be
154// numeric (the id is set to a valid resource ID).
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700155struct Reference : public TransformableItem<Reference, BaseItem<Reference>> {
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700156 enum class Type : uint8_t {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 kResource,
158 kAttribute,
159 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160
Ryan Mitchell4382e442021-07-14 12:53:01 -0700161 std::optional<ResourceName> name;
162 std::optional<ResourceId> id;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700163 std::optional<uint32_t> type_flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 Reference::Type reference_type;
165 bool private_reference = false;
Todd Kennedy32512992018-04-25 16:45:59 -0700166 bool is_dynamic = false;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700167 bool allow_raw = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 Reference();
170 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
171 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 bool Equals(const Value* value) const override;
175 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700177 void PrettyPrint(text::Printer* printer) const override;
178
179 // Prints the reference without a package name if the package name matches the one given.
180 void PrettyPrint(const android::StringPiece& package, text::Printer* printer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181};
182
Adam Lesinski8197cc462016-08-19 12:16:49 -0700183bool operator<(const Reference&, const Reference&);
184bool operator==(const Reference&, const Reference&);
185
Adam Lesinski75421622017-01-06 15:20:04 -0800186// An ID resource. Has no real value, just a place holder.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700187struct Id : public TransformableItem<Id, BaseItem<Id>> {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700188 Id() {
189 weak_ = true;
190 }
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 bool Equals(const Value* value) const override;
193 bool Flatten(android::Res_value* out) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195};
196
Adam Lesinski75421622017-01-06 15:20:04 -0800197// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
198// This shall *NOT* end up in the final resource table.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700199struct RawString : public TransformableItem<RawString, BaseItem<RawString>> {
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 RawString(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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207};
208
Adam Lesinski75421622017-01-06 15:20:04 -0800209// Identifies a range of characters in a string that are untranslatable.
210// These should not be pseudolocalized. The start and end indices are measured in bytes.
211struct UntranslatableSection {
212 // Start offset inclusive.
213 size_t start;
214
215 // End offset exclusive.
216 size_t end;
217};
218
219inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
220 return a.start == b.start && a.end == b.end;
221}
222
223inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
224 return a.start != b.start || a.end != b.end;
225}
226
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700227struct String : public TransformableItem<String, BaseItem<String>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinski75421622017-01-06 15:20:04 -0800230 // Sections of the string to NOT translate. Mainly used
231 // for pseudolocalization. This data is NOT persisted
232 // in any format.
233 std::vector<UntranslatableSection> untranslatable_sections;
234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 explicit String(const StringPool::Ref& ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700240 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241};
242
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700243struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245
Adam Lesinski75421622017-01-06 15:20:04 -0800246 // Sections of the string to NOT translate. Mainly used
247 // for pseudolocalization. This data is NOT persisted
248 // in any format.
249 std::vector<UntranslatableSection> untranslatable_sections;
250
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 bool Equals(const Value* value) const override;
254 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256};
257
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700258struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Adam Lesinski75421622017-01-06 15:20:04 -0800261 // A handle to the file object from which this file can be read.
262 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800264
Adam Lesinski00451162017-10-03 07:44:08 -0700265 // FileType of the file pointed to by `file`. This is used to know how to inflate the file,
266 // or if to inflate at all (just copy).
267 ResourceFile::Type type = ResourceFile::Type::kUnknown;
268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 FileReference() = default;
270 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 bool Equals(const Value* value) const override;
273 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275};
276
Adam Lesinski75421622017-01-06 15:20:04 -0800277// Represents any other android::Res_value.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700278struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 BinaryPrimitive() = default;
282 explicit BinaryPrimitive(const android::Res_value& val);
283 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 bool Equals(const Value* value) const override;
286 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700288 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289};
290
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700291struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 struct Symbol {
293 Reference symbol;
294 uint32_t value;
Ryan Mitchellc1676802019-05-20 16:47:08 -0700295 uint8_t type;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700296
297 friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 uint32_t type_mask;
301 int32_t min_int;
302 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800305 explicit Attribute(uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 bool Equals(const Value* value) const override;
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800308
309 // Returns true if this Attribute's format is compatible with the given Attribute. The basic
310 // rule is that TYPE_REFERENCE can be ignored for both of the Attributes, and TYPE_FLAGS and
311 // TYPE_ENUMS are never compatible.
312 bool IsCompatibleWith(const Attribute& attr) const;
313
Adam Lesinski93190b72017-11-03 15:20:17 -0700314 std::string MaskString() const;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700315 static std::string MaskString(uint32_t type_mask);
316
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 void Print(std::ostream* out) const override;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700318 bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319};
320
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700321struct Style : public TransformableValue<Style, BaseValue<Style>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 struct Entry {
323 Reference key;
324 std::unique_ptr<Item> value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700325
326 friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328
Ryan Mitchell4382e442021-07-14 12:53:01 -0700329 std::optional<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700330
Adam Lesinski75421622017-01-06 15:20:04 -0800331 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700333
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 void Print(std::ostream* out) const override;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700338
339 // Merges `style` into this Style. All identical attributes of `style` take precedence, including
340 // the parent, if there is one.
341 void MergeWith(Style* style, StringPool* pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342};
343
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700344struct Array : public TransformableValue<Array, BaseValue<Array>> {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700345 std::vector<std::unique_ptr<Item>> elements;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349};
350
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700351struct Plural : public TransformableValue<Plural, BaseValue<Plural>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358};
359
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700360struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 void Print(std::ostream* out) const override;
365 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800366};
367
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700368struct Macro : public TransformableValue<Macro, BaseValue<Macro>> {
369 std::string raw_value;
370 StyleString style_string;
371 std::vector<UntranslatableSection> untranslatable_sections;
372
373 struct Namespace {
374 std::string alias;
375 std::string package_name;
376 bool is_private;
377
378 bool operator==(const Namespace& right) const {
379 return alias == right.alias && package_name == right.package_name &&
380 is_private == right.is_private;
381 }
382 };
383
384 std::vector<Namespace> alias_namespaces;
385
386 bool Equals(const Value* value) const override;
387 void Print(std::ostream* out) const override;
388};
389
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700390template <typename T>
391typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
392 std::ostream& out, const std::unique_ptr<T>& value) {
393 if (value == nullptr) {
394 out << "NULL";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700396 value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700398 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800399}
400
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700401struct CloningValueTransformer : public ValueTransformer {
402 explicit CloningValueTransformer(StringPool* new_pool);
403
404 std::unique_ptr<Reference> TransformDerived(const Reference* value) override;
405 std::unique_ptr<Id> TransformDerived(const Id* value) override;
406 std::unique_ptr<RawString> TransformDerived(const RawString* value) override;
407 std::unique_ptr<String> TransformDerived(const String* value) override;
408 std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override;
409 std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override;
410 std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override;
411 std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override;
412 std::unique_ptr<Style> TransformDerived(const Style* value) override;
413 std::unique_ptr<Array> TransformDerived(const Array* value) override;
414 std::unique_ptr<Plural> TransformDerived(const Plural* value) override;
415 std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700416 std::unique_ptr<Macro> TransformDerived(const Macro* value) override;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700417};
418
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421#endif // AAPT_RESOURCE_VALUES_H