blob: d903b7e1b8b39bddbf0e2727f5c2620aef40f2cb [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 Lesinskia5870652015-11-20 15:32:30 -080034#include "util/Maybe.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
37
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070038class ValueVisitor;
39class ConstValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinski75421622017-01-06 15:20:04 -080041// A resource value. This is an all-encompassing representation
42// of Item and Map and their subclasses. The way to do
43// type specific operations is to check the Value's type() and
44// cast it to the appropriate subclass. This isn't super clean,
45// but it is the simplest strategy.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070046class Value {
47 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinski75421622017-01-06 15:20:04 -080050 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070051 bool IsWeak() const {
52 return weak_;
53 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080054
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070055 void SetWeak(bool val) {
56 weak_ = val;
57 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070059 // Whether the value is marked as translatable. This does not persist when flattened to binary.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // It is only used during compilation phase.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070061 void SetTranslatable(bool val) {
62 translatable_ = val;
63 }
Adam Lesinski458b8772016-04-25 14:20:21 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 // Default true.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070066 bool IsTranslatable() const {
67 return translatable_;
68 }
Adam Lesinski458b8772016-04-25 14:20:21 -070069
Adam Lesinski75421622017-01-06 15:20:04 -080070 // Returns the source where this value was defined.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070071 const Source& GetSource() const {
72 return source_;
73 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070074
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070075 void SetSource(const Source& source) {
76 source_ = source;
77 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070078
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070079 void SetSource(Source&& source) {
80 source_ = std::move(source);
81 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070082
Adam Lesinski75421622017-01-06 15:20:04 -080083 // Returns the comment that was associated with this resource.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070084 const std::string& GetComment() const {
85 return comment_;
86 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070087
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070088 void SetComment(const android::StringPiece& str) {
89 comment_ = str.to_string();
90 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070091
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070092 void SetComment(std::string&& str) {
93 comment_ = std::move(str);
94 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070095
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -070097
Adam Lesinski75421622017-01-06 15:20:04 -080098 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070099 virtual void Accept(ValueVisitor* visitor) = 0;
100
101 // Calls the appropriate overload of ConstValueVisitor.
102 virtual void Accept(ConstValueVisitor* visitor) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700104 // Transform this Value into another Value using the transformer.
105 std::unique_ptr<Value> Transform(ValueTransformer& transformer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinski75421622017-01-06 15:20:04 -0800107 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700109
Adam Lesinski93190b72017-11-03 15:20:17 -0700110 // Human readable printout of this value that may omit some information for the sake
111 // of brevity and readability. Default implementation just calls Print().
112 virtual void PrettyPrint(text::Printer* printer) const;
113
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700114 friend std::ostream& operator<<(std::ostream& out, const Value& value);
115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 Source source_;
118 std::string comment_;
119 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -0800120 bool translatable_ = true;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700121
122 private:
123 virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124};
125
Adam Lesinski75421622017-01-06 15:20:04 -0800126// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127template <typename Derived>
128struct BaseValue : public Value {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700129 void Accept(ValueVisitor* visitor) override;
130 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131};
132
Adam Lesinski75421622017-01-06 15:20:04 -0800133// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800135 // Fills in an android::Res_value structure with this Item's binary representation.
136 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 virtual bool Flatten(android::Res_value* out_value) const = 0;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700138
139 // Transform this Item into another Item using the transformer.
140 std::unique_ptr<Item> Transform(ValueTransformer& transformer) const;
141
142 private:
143 virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144};
145
Adam Lesinski75421622017-01-06 15:20:04 -0800146// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147template <typename Derived>
148struct BaseItem : public Item {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700149 void Accept(ValueVisitor* visitor) override;
150 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151};
152
Adam Lesinski75421622017-01-06 15:20:04 -0800153// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
154// A reference can be symbolic (with the name set to a valid resource name) or be
155// numeric (the id is set to a valid resource ID).
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700156struct Reference : public TransformableItem<Reference, BaseItem<Reference>> {
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700157 enum class Type : uint8_t {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 kResource,
159 kAttribute,
160 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 Maybe<ResourceName> name;
163 Maybe<ResourceId> id;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700164 std::optional<uint32_t> type_flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 Reference::Type reference_type;
166 bool private_reference = false;
Todd Kennedy32512992018-04-25 16:45:59 -0700167 bool is_dynamic = false;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700168 bool allow_raw = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 Reference();
171 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
172 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 bool Equals(const Value* value) const override;
176 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700178 void PrettyPrint(text::Printer* printer) const override;
179
180 // Prints the reference without a package name if the package name matches the one given.
181 void PrettyPrint(const android::StringPiece& package, text::Printer* printer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182};
183
Adam Lesinski8197cc462016-08-19 12:16:49 -0700184bool operator<(const Reference&, const Reference&);
185bool operator==(const Reference&, const Reference&);
186
Adam Lesinski75421622017-01-06 15:20:04 -0800187// An ID resource. Has no real value, just a place holder.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700188struct Id : public TransformableItem<Id, BaseItem<Id>> {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700189 Id() {
190 weak_ = true;
191 }
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 bool Equals(const Value* value) const override;
194 bool Flatten(android::Res_value* out) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196};
197
Adam Lesinski75421622017-01-06 15:20:04 -0800198// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
199// This shall *NOT* end up in the final resource table.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700200struct RawString : public TransformableItem<RawString, BaseItem<RawString>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 explicit RawString(const StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 bool Equals(const Value* value) const override;
206 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208};
209
Adam Lesinski75421622017-01-06 15:20:04 -0800210// Identifies a range of characters in a string that are untranslatable.
211// These should not be pseudolocalized. The start and end indices are measured in bytes.
212struct UntranslatableSection {
213 // Start offset inclusive.
214 size_t start;
215
216 // End offset exclusive.
217 size_t end;
218};
219
220inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
221 return a.start == b.start && a.end == b.end;
222}
223
224inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
225 return a.start != b.start || a.end != b.end;
226}
227
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700228struct String : public TransformableItem<String, BaseItem<String>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinski75421622017-01-06 15:20:04 -0800231 // Sections of the string to NOT translate. Mainly used
232 // for pseudolocalization. This data is NOT persisted
233 // in any format.
234 std::vector<UntranslatableSection> untranslatable_sections;
235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 explicit String(const StringPool::Ref& ref);
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700241 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242};
243
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700244struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246
Adam Lesinski75421622017-01-06 15:20:04 -0800247 // Sections of the string to NOT translate. Mainly used
248 // for pseudolocalization. This data is NOT persisted
249 // in any format.
250 std::vector<UntranslatableSection> untranslatable_sections;
251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 explicit StyledString(const StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 bool Equals(const Value* value) const override;
255 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257};
258
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700259struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261
Adam Lesinski75421622017-01-06 15:20:04 -0800262 // A handle to the file object from which this file can be read.
263 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800265
Adam Lesinski00451162017-10-03 07:44:08 -0700266 // FileType of the file pointed to by `file`. This is used to know how to inflate the file,
267 // or if to inflate at all (just copy).
268 ResourceFile::Type type = ResourceFile::Type::kUnknown;
269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 FileReference() = default;
271 explicit FileReference(const StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 bool Equals(const Value* value) const override;
274 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276};
277
Adam Lesinski75421622017-01-06 15:20:04 -0800278// Represents any other android::Res_value.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700279struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 BinaryPrimitive() = default;
283 explicit BinaryPrimitive(const android::Res_value& val);
284 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 bool Equals(const Value* value) const override;
287 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700289 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290};
291
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700292struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 struct Symbol {
294 Reference symbol;
295 uint32_t value;
Ryan Mitchellc1676802019-05-20 16:47:08 -0700296 uint8_t type;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700297
298 friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 uint32_t type_mask;
302 int32_t min_int;
303 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800305
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800306 explicit Attribute(uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 bool Equals(const Value* value) const override;
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800309
310 // Returns true if this Attribute's format is compatible with the given Attribute. The basic
311 // rule is that TYPE_REFERENCE can be ignored for both of the Attributes, and TYPE_FLAGS and
312 // TYPE_ENUMS are never compatible.
313 bool IsCompatibleWith(const Attribute& attr) const;
314
Adam Lesinski93190b72017-11-03 15:20:17 -0700315 std::string MaskString() const;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700316 static std::string MaskString(uint32_t type_mask);
317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 void Print(std::ostream* out) const override;
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700319 bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800320};
321
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700322struct Style : public TransformableValue<Style, BaseValue<Style>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 struct Entry {
324 Reference key;
325 std::unique_ptr<Item> value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700326
327 friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 Maybe<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700331
Adam Lesinski75421622017-01-06 15:20:04 -0800332 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700334
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 void Print(std::ostream* out) const override;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700339
340 // Merges `style` into this Style. All identical attributes of `style` take precedence, including
341 // the parent, if there is one.
342 void MergeWith(Style* style, StringPool* pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800343};
344
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700345struct Array : public TransformableValue<Array, BaseValue<Array>> {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700346 std::vector<std::unique_ptr<Item>> elements;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350};
351
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700352struct Plural : public TransformableValue<Plural, BaseValue<Plural>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359};
360
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700361struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 void Print(std::ostream* out) const override;
366 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367};
368
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700369struct Macro : public TransformableValue<Macro, BaseValue<Macro>> {
370 std::string raw_value;
371 StyleString style_string;
372 std::vector<UntranslatableSection> untranslatable_sections;
373
374 struct Namespace {
375 std::string alias;
376 std::string package_name;
377 bool is_private;
378
379 bool operator==(const Namespace& right) const {
380 return alias == right.alias && package_name == right.package_name &&
381 is_private == right.is_private;
382 }
383 };
384
385 std::vector<Namespace> alias_namespaces;
386
387 bool Equals(const Value* value) const override;
388 void Print(std::ostream* out) const override;
389};
390
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700391template <typename T>
392typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
393 std::ostream& out, const std::unique_ptr<T>& value) {
394 if (value == nullptr) {
395 out << "NULL";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700397 value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700399 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400}
401
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700402struct CloningValueTransformer : public ValueTransformer {
403 explicit CloningValueTransformer(StringPool* new_pool);
404
405 std::unique_ptr<Reference> TransformDerived(const Reference* value) override;
406 std::unique_ptr<Id> TransformDerived(const Id* value) override;
407 std::unique_ptr<RawString> TransformDerived(const RawString* value) override;
408 std::unique_ptr<String> TransformDerived(const String* value) override;
409 std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override;
410 std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override;
411 std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override;
412 std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override;
413 std::unique_ptr<Style> TransformDerived(const Style* value) override;
414 std::unique_ptr<Array> TransformDerived(const Array* value) override;
415 std::unique_ptr<Plural> TransformDerived(const Plural* value) override;
416 std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700417 std::unique_ptr<Macro> TransformDerived(const Macro* value) override;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700418};
419
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422#endif // AAPT_RESOURCE_VALUES_H