blob: e000c653b87a2b3c79c96b4f5b050014f01d669e [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025#include "Resource.h"
26#include "ValueTransformer.h"
27#include "androidfw/IDiagnostics.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#include "androidfw/StringPiece.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000030#include "androidfw/StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080031#include "io/File.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070032#include "text/Printer.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034namespace aapt {
35
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070036class ValueVisitor;
37class ConstValueVisitor;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinski75421622017-01-06 15:20:04 -080039// A resource value. This is an all-encompassing representation
40// of Item and Map and their subclasses. The way to do
41// type specific operations is to check the Value's type() and
42// cast it to the appropriate subclass. This isn't super clean,
43// but it is the simplest strategy.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070044class Value {
45 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 virtual ~Value() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinski75421622017-01-06 15:20:04 -080048 // Whether this value is weak and can be overridden without warning or error. Default is false.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070049 bool IsWeak() const {
50 return weak_;
51 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080052
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070053 void SetWeak(bool val) {
54 weak_ = val;
55 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070057 // Whether the value is marked as translatable. This does not persist when flattened to binary.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 // It is only used during compilation phase.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070059 void SetTranslatable(bool val) {
60 translatable_ = val;
61 }
Adam Lesinski458b8772016-04-25 14:20:21 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 // Default true.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070064 bool IsTranslatable() const {
65 return translatable_;
66 }
Adam Lesinski458b8772016-04-25 14:20:21 -070067
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070068 void SetFlag(std::optional<FeatureFlagAttribute> val) {
69 flag_ = val;
70 }
71
72 std::optional<FeatureFlagAttribute> GetFlag() const {
73 return flag_;
74 }
75
Jeremy Meyerd52bd682024-08-14 11:16:58 -070076 void SetFlagStatus(FlagStatus val) {
77 flag_status_ = val;
78 }
79
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070080 // If the value is behind a flag this returns whether that flag was enabled when the value was
81 // parsed by comparing it to the flags passed on the command line to aapt2 (taking into account
82 // negation if necessary). If there was no flag, FlagStatus::NoFlag is returned instead.
Jeremy Meyerd52bd682024-08-14 11:16:58 -070083 FlagStatus GetFlagStatus() const {
84 return flag_status_;
85 }
86
Adam Lesinski75421622017-01-06 15:20:04 -080087 // Returns the source where this value was defined.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000088 const android::Source& GetSource() const {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070089 return source_;
90 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070091
Jeremy Meyer56f36e82022-05-20 20:35:42 +000092 void SetSource(const android::Source& source) {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070093 source_ = source;
94 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070095
Jeremy Meyer56f36e82022-05-20 20:35:42 +000096 void SetSource(android::Source&& source) {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070097 source_ = std::move(source);
98 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070099
Adam Lesinski75421622017-01-06 15:20:04 -0800100 // Returns the comment that was associated with this resource.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700101 const std::string& GetComment() const {
102 return comment_;
103 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700104
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700105 void SetComment(android::StringPiece str) {
106 comment_.assign(str);
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700107 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700108
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700109 void SetComment(std::string&& str) {
110 comment_ = std::move(str);
111 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -0700114
Adam Lesinski75421622017-01-06 15:20:04 -0800115 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700116 virtual void Accept(ValueVisitor* visitor) = 0;
117
118 // Calls the appropriate overload of ConstValueVisitor.
119 virtual void Accept(ConstValueVisitor* visitor) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700121 // Transform this Value into another Value using the transformer.
122 std::unique_ptr<Value> Transform(ValueTransformer& transformer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123
Adam Lesinski75421622017-01-06 15:20:04 -0800124 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700126
Adam Lesinski93190b72017-11-03 15:20:17 -0700127 // Human readable printout of this value that may omit some information for the sake
128 // of brevity and readability. Default implementation just calls Print().
129 virtual void PrettyPrint(text::Printer* printer) const;
130
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700131 // Removes any part of the value that is beind a disabled flag.
132 virtual void RemoveFlagDisabledElements() {
133 }
134
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700135 friend std::ostream& operator<<(std::ostream& out, const Value& value);
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 protected:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000138 android::Source source_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 std::string comment_;
140 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -0800141 bool translatable_ = true;
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700142 std::optional<FeatureFlagAttribute> flag_;
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700143 FlagStatus flag_status_ = FlagStatus::NoFlag;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700144
145 private:
146 virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147};
148
Adam Lesinski75421622017-01-06 15:20:04 -0800149// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150template <typename Derived>
151struct BaseValue : public Value {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700152 void Accept(ValueVisitor* visitor) override;
153 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154};
155
Adam Lesinski75421622017-01-06 15:20:04 -0800156// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800158 // Fills in an android::Res_value structure with this Item's binary representation.
159 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 virtual bool Flatten(android::Res_value* out_value) const = 0;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700161
162 // Transform this Item into another Item using the transformer.
163 std::unique_ptr<Item> Transform(ValueTransformer& transformer) const;
164
165 private:
166 virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167};
168
Adam Lesinski75421622017-01-06 15:20:04 -0800169// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170template <typename Derived>
171struct BaseItem : public Item {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700172 void Accept(ValueVisitor* visitor) override;
173 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174};
175
Adam Lesinski75421622017-01-06 15:20:04 -0800176// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
177// A reference can be symbolic (with the name set to a valid resource name) or be
178// numeric (the id is set to a valid resource ID).
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700179struct Reference : public TransformableItem<Reference, BaseItem<Reference>> {
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700180 enum class Type : uint8_t {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 kResource,
182 kAttribute,
183 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Ryan Mitchell4382e442021-07-14 12:53:01 -0700185 std::optional<ResourceName> name;
186 std::optional<ResourceId> id;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700187 std::optional<uint32_t> type_flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 Reference::Type reference_type;
189 bool private_reference = false;
Todd Kennedy32512992018-04-25 16:45:59 -0700190 bool is_dynamic = false;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700191 bool allow_raw = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 Reference();
194 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
195 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 bool Equals(const Value* value) const override;
199 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700201 void PrettyPrint(text::Printer* printer) const override;
202
203 // Prints the reference without a package name if the package name matches the one given.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700204 void PrettyPrint(android::StringPiece package, text::Printer* printer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205};
206
Adam Lesinski8197cc462016-08-19 12:16:49 -0700207bool operator<(const Reference&, const Reference&);
208bool operator==(const Reference&, const Reference&);
209
Adam Lesinski75421622017-01-06 15:20:04 -0800210// An ID resource. Has no real value, just a place holder.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700211struct Id : public TransformableItem<Id, BaseItem<Id>> {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700212 Id() {
213 weak_ = true;
214 }
215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 bool Equals(const Value* value) const override;
217 bool Flatten(android::Res_value* out) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219};
220
Adam Lesinski75421622017-01-06 15:20:04 -0800221// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
222// This shall *NOT* end up in the final resource table.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700223struct RawString : public TransformableItem<RawString, BaseItem<RawString>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000224 android::StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000226 explicit RawString(const android::StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 bool Equals(const Value* value) const override;
229 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231};
232
Adam Lesinski75421622017-01-06 15:20:04 -0800233// Identifies a range of characters in a string that are untranslatable.
234// These should not be pseudolocalized. The start and end indices are measured in bytes.
235struct UntranslatableSection {
236 // Start offset inclusive.
237 size_t start;
238
239 // End offset exclusive.
240 size_t end;
241};
242
243inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
244 return a.start == b.start && a.end == b.end;
245}
246
247inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
248 return a.start != b.start || a.end != b.end;
249}
250
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700251struct String : public TransformableItem<String, BaseItem<String>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000252 android::StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253
Adam Lesinski75421622017-01-06 15:20:04 -0800254 // Sections of the string to NOT translate. Mainly used
255 // for pseudolocalization. This data is NOT persisted
256 // in any format.
257 std::vector<UntranslatableSection> untranslatable_sections;
258
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000259 explicit String(const android::StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 bool Equals(const Value* value) const override;
262 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700264 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265};
266
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700267struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000268 android::StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269
Adam Lesinski75421622017-01-06 15:20:04 -0800270 // Sections of the string to NOT translate. Mainly used
271 // for pseudolocalization. This data is NOT persisted
272 // in any format.
273 std::vector<UntranslatableSection> untranslatable_sections;
274
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000275 explicit StyledString(const android::StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 bool Equals(const Value* value) const override;
278 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280};
281
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700282struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000283 android::StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
Adam Lesinski75421622017-01-06 15:20:04 -0800285 // A handle to the file object from which this file can be read.
286 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800288
Adam Lesinski00451162017-10-03 07:44:08 -0700289 // FileType of the file pointed to by `file`. This is used to know how to inflate the file,
290 // or if to inflate at all (just copy).
291 ResourceFile::Type type = ResourceFile::Type::kUnknown;
292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 FileReference() = default;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000294 explicit FileReference(const android::StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 bool Equals(const Value* value) const override;
297 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299};
300
Adam Lesinski75421622017-01-06 15:20:04 -0800301// Represents any other android::Res_value.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700302struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 BinaryPrimitive() = default;
306 explicit BinaryPrimitive(const android::Res_value& val);
307 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 bool Equals(const Value* value) const override;
310 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 void Print(std::ostream* out) const override;
Brandon Liuc674d382023-03-31 22:37:42 +0000312 static const char* DecideFormat(float f);
Adam Lesinski93190b72017-11-03 15:20:17 -0700313 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314};
315
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700316struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 struct Symbol {
318 Reference symbol;
319 uint32_t value;
Ryan Mitchellc1676802019-05-20 16:47:08 -0700320 uint8_t type;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700321
322 friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 uint32_t type_mask;
326 int32_t min_int;
327 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800330 explicit Attribute(uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 bool Equals(const Value* value) const override;
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800333
334 // Returns true if this Attribute's format is compatible with the given Attribute. The basic
335 // rule is that TYPE_REFERENCE can be ignored for both of the Attributes, and TYPE_FLAGS and
336 // TYPE_ENUMS are never compatible.
337 bool IsCompatibleWith(const Attribute& attr) const;
338
Adam Lesinski93190b72017-11-03 15:20:17 -0700339 std::string MaskString() const;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700340 static std::string MaskString(uint32_t type_mask);
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 void Print(std::ostream* out) const override;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000343 bool Matches(const Item& item, android::DiagMessage* out_msg = nullptr) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800344};
345
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700346struct Style : public TransformableValue<Style, BaseValue<Style>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 struct Entry {
348 Reference key;
349 std::unique_ptr<Item> value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700350
351 friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800353
Ryan Mitchell4382e442021-07-14 12:53:01 -0700354 std::optional<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700355
Adam Lesinski75421622017-01-06 15:20:04 -0800356 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700358
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 void Print(std::ostream* out) const override;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700363
364 // Merges `style` into this Style. All identical attributes of `style` take precedence, including
365 // the parent, if there is one.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000366 void MergeWith(Style* style, android::StringPool* pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367};
368
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700369struct Array : public TransformableValue<Array, BaseValue<Array>> {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700370 std::vector<std::unique_ptr<Item>> elements;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 void Print(std::ostream* out) const override;
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700374 void RemoveFlagDisabledElements() override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800375};
376
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700377struct Plural : public TransformableValue<Plural, BaseValue<Plural>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384};
385
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700386struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 void Print(std::ostream* out) const override;
391 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392};
393
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700394struct Macro : public TransformableValue<Macro, BaseValue<Macro>> {
395 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000396 android::StyleString style_string;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700397 std::vector<UntranslatableSection> untranslatable_sections;
398
399 struct Namespace {
400 std::string alias;
401 std::string package_name;
402 bool is_private;
403
404 bool operator==(const Namespace& right) const {
405 return alias == right.alias && package_name == right.package_name &&
406 is_private == right.is_private;
407 }
408 };
409
410 std::vector<Namespace> alias_namespaces;
411
412 bool Equals(const Value* value) const override;
413 void Print(std::ostream* out) const override;
414};
415
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700416template <typename T>
417typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
418 std::ostream& out, const std::unique_ptr<T>& value) {
419 if (value == nullptr) {
420 out << "NULL";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700422 value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700424 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425}
426
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700427struct CloningValueTransformer : public ValueTransformer {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000428 explicit CloningValueTransformer(android::StringPool* new_pool);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700429
430 std::unique_ptr<Reference> TransformDerived(const Reference* value) override;
431 std::unique_ptr<Id> TransformDerived(const Id* value) override;
432 std::unique_ptr<RawString> TransformDerived(const RawString* value) override;
433 std::unique_ptr<String> TransformDerived(const String* value) override;
434 std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override;
435 std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override;
436 std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override;
437 std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override;
438 std::unique_ptr<Style> TransformDerived(const Style* value) override;
439 std::unique_ptr<Array> TransformDerived(const Array* value) override;
440 std::unique_ptr<Plural> TransformDerived(const Plural* value) override;
441 std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700442 std::unique_ptr<Macro> TransformDerived(const Macro* value) override;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700443};
444
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447#endif // AAPT_RESOURCE_VALUES_H