blob: a1b1839b19ef4406edf5e6d845e9b650f5f1b0f3 [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 Meyerd52bd682024-08-14 11:16:58 -070068 void SetFlagStatus(FlagStatus val) {
69 flag_status_ = val;
70 }
71
72 FlagStatus GetFlagStatus() const {
73 return flag_status_;
74 }
75
Adam Lesinski75421622017-01-06 15:20:04 -080076 // Returns the source where this value was defined.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000077 const android::Source& GetSource() const {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070078 return source_;
79 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070080
Jeremy Meyer56f36e82022-05-20 20:35:42 +000081 void SetSource(const android::Source& source) {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070082 source_ = source;
83 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070084
Jeremy Meyer56f36e82022-05-20 20:35:42 +000085 void SetSource(android::Source&& source) {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070086 source_ = std::move(source);
87 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070088
Adam Lesinski75421622017-01-06 15:20:04 -080089 // Returns the comment that was associated with this resource.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070090 const std::string& GetComment() const {
91 return comment_;
92 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070093
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070094 void SetComment(android::StringPiece str) {
95 comment_.assign(str);
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070096 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070097
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070098 void SetComment(std::string&& str) {
99 comment_ = std::move(str);
100 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 virtual bool Equals(const Value* value) const = 0;
Adam Lesinski458b8772016-04-25 14:20:21 -0700103
Adam Lesinski75421622017-01-06 15:20:04 -0800104 // Calls the appropriate overload of ValueVisitor.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700105 virtual void Accept(ValueVisitor* visitor) = 0;
106
107 // Calls the appropriate overload of ConstValueVisitor.
108 virtual void Accept(ConstValueVisitor* visitor) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700110 // Transform this Value into another Value using the transformer.
111 std::unique_ptr<Value> Transform(ValueTransformer& transformer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinski75421622017-01-06 15:20:04 -0800113 // Human readable printout of this value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 virtual void Print(std::ostream* out) const = 0;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700115
Adam Lesinski93190b72017-11-03 15:20:17 -0700116 // Human readable printout of this value that may omit some information for the sake
117 // of brevity and readability. Default implementation just calls Print().
118 virtual void PrettyPrint(text::Printer* printer) const;
119
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700120 // Removes any part of the value that is beind a disabled flag.
121 virtual void RemoveFlagDisabledElements() {
122 }
123
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700124 friend std::ostream& operator<<(std::ostream& out, const Value& value);
125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 protected:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000127 android::Source source_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 std::string comment_;
129 bool weak_ = false;
Adam Lesinski75421622017-01-06 15:20:04 -0800130 bool translatable_ = true;
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700131 FlagStatus flag_status_ = FlagStatus::NoFlag;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700132
133 private:
134 virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135};
136
Adam Lesinski75421622017-01-06 15:20:04 -0800137// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138template <typename Derived>
139struct BaseValue : public Value {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700140 void Accept(ValueVisitor* visitor) override;
141 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142};
143
Adam Lesinski75421622017-01-06 15:20:04 -0800144// A resource item with a single value. This maps to android::ResTable_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145struct Item : public Value {
Adam Lesinski75421622017-01-06 15:20:04 -0800146 // Fills in an android::Res_value structure with this Item's binary representation.
147 // Returns false if an error occurred.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 virtual bool Flatten(android::Res_value* out_value) const = 0;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700149
150 // Transform this Item into another Item using the transformer.
151 std::unique_ptr<Item> Transform(ValueTransformer& transformer) const;
152
153 private:
154 virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155};
156
Adam Lesinski75421622017-01-06 15:20:04 -0800157// Inherit from this to get visitor accepting implementations for free.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158template <typename Derived>
159struct BaseItem : public Item {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700160 void Accept(ValueVisitor* visitor) override;
161 void Accept(ConstValueVisitor* visitor) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162};
163
Adam Lesinski75421622017-01-06 15:20:04 -0800164// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
165// A reference can be symbolic (with the name set to a valid resource name) or be
166// numeric (the id is set to a valid resource ID).
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700167struct Reference : public TransformableItem<Reference, BaseItem<Reference>> {
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700168 enum class Type : uint8_t {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 kResource,
170 kAttribute,
171 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Ryan Mitchell4382e442021-07-14 12:53:01 -0700173 std::optional<ResourceName> name;
174 std::optional<ResourceId> id;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700175 std::optional<uint32_t> type_flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 Reference::Type reference_type;
177 bool private_reference = false;
Todd Kennedy32512992018-04-25 16:45:59 -0700178 bool is_dynamic = false;
Ryan Mitchella2b4fcd2021-05-03 15:37:00 -0700179 bool allow_raw = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 Reference();
182 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
183 explicit Reference(const ResourceId& i, Type type = Type::kResource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 Reference(const ResourceNameRef& n, const ResourceId& i);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 bool Equals(const Value* value) const override;
187 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700189 void PrettyPrint(text::Printer* printer) const override;
190
191 // Prints the reference without a package name if the package name matches the one given.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700192 void PrettyPrint(android::StringPiece package, text::Printer* printer) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193};
194
Adam Lesinski8197cc462016-08-19 12:16:49 -0700195bool operator<(const Reference&, const Reference&);
196bool operator==(const Reference&, const Reference&);
197
Adam Lesinski75421622017-01-06 15:20:04 -0800198// An ID resource. Has no real value, just a place holder.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700199struct Id : public TransformableItem<Id, BaseItem<Id>> {
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700200 Id() {
201 weak_ = true;
202 }
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 bool Equals(const Value* value) const override;
205 bool Flatten(android::Res_value* out) 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// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
210// This shall *NOT* end up in the final resource table.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700211struct RawString : public TransformableItem<RawString, BaseItem<RawString>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000212 android::StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000214 explicit RawString(const android::StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 bool Equals(const Value* value) const override;
217 bool Flatten(android::Res_value* out_value) 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// Identifies a range of characters in a string that are untranslatable.
222// These should not be pseudolocalized. The start and end indices are measured in bytes.
223struct UntranslatableSection {
224 // Start offset inclusive.
225 size_t start;
226
227 // End offset exclusive.
228 size_t end;
229};
230
231inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
232 return a.start == b.start && a.end == b.end;
233}
234
235inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
236 return a.start != b.start || a.end != b.end;
237}
238
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700239struct String : public TransformableItem<String, BaseItem<String>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000240 android::StringPool::Ref value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Adam Lesinski75421622017-01-06 15:20:04 -0800242 // Sections of the string to NOT translate. Mainly used
243 // for pseudolocalization. This data is NOT persisted
244 // in any format.
245 std::vector<UntranslatableSection> untranslatable_sections;
246
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000247 explicit String(const android::StringPool::Ref& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 bool Equals(const Value* value) const override;
250 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 void Print(std::ostream* out) const override;
Adam Lesinski93190b72017-11-03 15:20:17 -0700252 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253};
254
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700255struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000256 android::StringPool::StyleRef value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257
Adam Lesinski75421622017-01-06 15:20:04 -0800258 // Sections of the string to NOT translate. Mainly used
259 // for pseudolocalization. This data is NOT persisted
260 // in any format.
261 std::vector<UntranslatableSection> untranslatable_sections;
262
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000263 explicit StyledString(const android::StringPool::StyleRef& ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 bool Equals(const Value* value) const override;
266 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268};
269
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700270struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000271 android::StringPool::Ref path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinski75421622017-01-06 15:20:04 -0800273 // A handle to the file object from which this file can be read.
274 // This field is NOT persisted in any format. It is transient.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 io::IFile* file = nullptr;
Adam Lesinski355f2852016-02-13 20:26:45 -0800276
Adam Lesinski00451162017-10-03 07:44:08 -0700277 // FileType of the file pointed to by `file`. This is used to know how to inflate the file,
278 // or if to inflate at all (just copy).
279 ResourceFile::Type type = ResourceFile::Type::kUnknown;
280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 FileReference() = default;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000282 explicit FileReference(const android::StringPool::Ref& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 bool Equals(const Value* value) const override;
285 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287};
288
Adam Lesinski75421622017-01-06 15:20:04 -0800289// Represents any other android::Res_value.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700290struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 android::Res_value value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 BinaryPrimitive() = default;
294 explicit BinaryPrimitive(const android::Res_value& val);
295 BinaryPrimitive(uint8_t dataType, uint32_t data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 bool Equals(const Value* value) const override;
298 bool Flatten(android::Res_value* out_value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 void Print(std::ostream* out) const override;
Brandon Liuc674d382023-03-31 22:37:42 +0000300 static const char* DecideFormat(float f);
Adam Lesinski93190b72017-11-03 15:20:17 -0700301 void PrettyPrint(text::Printer* printer) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302};
303
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700304struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 struct Symbol {
306 Reference symbol;
307 uint32_t value;
Ryan Mitchellc1676802019-05-20 16:47:08 -0700308 uint8_t type;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700309
310 friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 uint32_t type_mask;
314 int32_t min_int;
315 int32_t max_int;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 std::vector<Symbol> symbols;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800318 explicit Attribute(uint32_t t = 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 bool Equals(const Value* value) const override;
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800321
322 // Returns true if this Attribute's format is compatible with the given Attribute. The basic
323 // rule is that TYPE_REFERENCE can be ignored for both of the Attributes, and TYPE_FLAGS and
324 // TYPE_ENUMS are never compatible.
325 bool IsCompatibleWith(const Attribute& attr) const;
326
Adam Lesinski93190b72017-11-03 15:20:17 -0700327 std::string MaskString() const;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700328 static std::string MaskString(uint32_t type_mask);
329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 void Print(std::ostream* out) const override;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000331 bool Matches(const Item& item, android::DiagMessage* out_msg = nullptr) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800332};
333
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700334struct Style : public TransformableValue<Style, BaseValue<Style>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 struct Entry {
336 Reference key;
337 std::unique_ptr<Item> value;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700338
339 friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Ryan Mitchell4382e442021-07-14 12:53:01 -0700342 std::optional<Reference> parent;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700343
Adam Lesinski75421622017-01-06 15:20:04 -0800344 // If set to true, the parent was auto inferred from the style's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 bool parent_inferred = false;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700346
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 std::vector<Entry> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 void Print(std::ostream* out) const override;
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700351
352 // Merges `style` into this Style. All identical attributes of `style` take precedence, including
353 // the parent, if there is one.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000354 void MergeWith(Style* style, android::StringPool* pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355};
356
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700357struct Array : public TransformableValue<Array, BaseValue<Array>> {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700358 std::vector<std::unique_ptr<Item>> elements;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 void Print(std::ostream* out) const override;
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700362 void RemoveFlagDisabledElements() override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363};
364
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700365struct Plural : public TransformableValue<Plural, BaseValue<Plural>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 enum { Zero = 0, One, Two, Few, Many, Other, Count };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 std::array<std::unique_ptr<Item>, Count> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 void Print(std::ostream* out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372};
373
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700374struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 std::vector<Reference> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 bool Equals(const Value* value) const override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 void Print(std::ostream* out) const override;
379 void MergeWith(Styleable* styleable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800380};
381
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700382struct Macro : public TransformableValue<Macro, BaseValue<Macro>> {
383 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000384 android::StyleString style_string;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700385 std::vector<UntranslatableSection> untranslatable_sections;
386
387 struct Namespace {
388 std::string alias;
389 std::string package_name;
390 bool is_private;
391
392 bool operator==(const Namespace& right) const {
393 return alias == right.alias && package_name == right.package_name &&
394 is_private == right.is_private;
395 }
396 };
397
398 std::vector<Namespace> alias_namespaces;
399
400 bool Equals(const Value* value) const override;
401 void Print(std::ostream* out) const override;
402};
403
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700404template <typename T>
405typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
406 std::ostream& out, const std::unique_ptr<T>& value) {
407 if (value == nullptr) {
408 out << "NULL";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700410 value->Print(&out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700412 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413}
414
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700415struct CloningValueTransformer : public ValueTransformer {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000416 explicit CloningValueTransformer(android::StringPool* new_pool);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700417
418 std::unique_ptr<Reference> TransformDerived(const Reference* value) override;
419 std::unique_ptr<Id> TransformDerived(const Id* value) override;
420 std::unique_ptr<RawString> TransformDerived(const RawString* value) override;
421 std::unique_ptr<String> TransformDerived(const String* value) override;
422 std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override;
423 std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override;
424 std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override;
425 std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override;
426 std::unique_ptr<Style> TransformDerived(const Style* value) override;
427 std::unique_ptr<Array> TransformDerived(const Array* value) override;
428 std::unique_ptr<Plural> TransformDerived(const Plural* value) override;
429 std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700430 std::unique_ptr<Macro> TransformDerived(const Macro* value) override;
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700431};
432
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435#endif // AAPT_RESOURCE_VALUES_H