Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <array> |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 21 | #include <limits> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include <ostream> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 26 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 28 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include "Resource.h" |
| 30 | #include "StringPool.h" |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 31 | #include "ValueTransformer.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 32 | #include "io/File.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 33 | #include "text/Printer.h" |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 34 | #include "util/Maybe.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | namespace aapt { |
| 37 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 38 | class ValueVisitor; |
| 39 | class ConstValueVisitor; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 41 | // 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 Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 46 | class Value { |
| 47 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | virtual ~Value() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 50 | // Whether this value is weak and can be overridden without warning or error. Default is false. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 51 | bool IsWeak() const { |
| 52 | return weak_; |
| 53 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 55 | void SetWeak(bool val) { |
| 56 | weak_ = val; |
| 57 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 59 | // Whether the value is marked as translatable. This does not persist when flattened to binary. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | // It is only used during compilation phase. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 61 | void SetTranslatable(bool val) { |
| 62 | translatable_ = val; |
| 63 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | // Default true. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 66 | bool IsTranslatable() const { |
| 67 | return translatable_; |
| 68 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 70 | // Returns the source where this value was defined. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 71 | const Source& GetSource() const { |
| 72 | return source_; |
| 73 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 75 | void SetSource(const Source& source) { |
| 76 | source_ = source; |
| 77 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 79 | void SetSource(Source&& source) { |
| 80 | source_ = std::move(source); |
| 81 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 83 | // Returns the comment that was associated with this resource. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 84 | const std::string& GetComment() const { |
| 85 | return comment_; |
| 86 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 88 | void SetComment(const android::StringPiece& str) { |
| 89 | comment_ = str.to_string(); |
| 90 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 92 | void SetComment(std::string&& str) { |
| 93 | comment_ = std::move(str); |
| 94 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | virtual bool Equals(const Value* value) const = 0; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 98 | // Calls the appropriate overload of ValueVisitor. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 99 | virtual void Accept(ValueVisitor* visitor) = 0; |
| 100 | |
| 101 | // Calls the appropriate overload of ConstValueVisitor. |
| 102 | virtual void Accept(ConstValueVisitor* visitor) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 104 | // Transform this Value into another Value using the transformer. |
| 105 | std::unique_ptr<Value> Transform(ValueTransformer& transformer) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 107 | // Human readable printout of this value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | virtual void Print(std::ostream* out) const = 0; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 110 | // 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 Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 114 | friend std::ostream& operator<<(std::ostream& out, const Value& value); |
| 115 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | protected: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | Source source_; |
| 118 | std::string comment_; |
| 119 | bool weak_ = false; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 120 | bool translatable_ = true; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 126 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | template <typename Derived> |
| 128 | struct BaseValue : public Value { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 129 | void Accept(ValueVisitor* visitor) override; |
| 130 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 133 | // A resource item with a single value. This maps to android::ResTable_entry. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | struct Item : public Value { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 135 | // Fills in an android::Res_value structure with this Item's binary representation. |
| 136 | // Returns false if an error occurred. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | virtual bool Flatten(android::Res_value* out_value) const = 0; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 138 | |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 146 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | template <typename Derived> |
| 148 | struct BaseItem : public Item { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 149 | void Accept(ValueVisitor* visitor) override; |
| 150 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 153 | // 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 Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 156 | struct Reference : public TransformableItem<Reference, BaseItem<Reference>> { |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame^] | 157 | enum class Type : uint8_t { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | kResource, |
| 159 | kAttribute, |
| 160 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 161 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | Maybe<ResourceName> name; |
| 163 | Maybe<ResourceId> id; |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame^] | 164 | std::optional<uint32_t> type_flags; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | Reference::Type reference_type; |
| 166 | bool private_reference = false; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 167 | bool is_dynamic = false; |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame^] | 168 | bool allow_raw = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | Reference(); |
| 171 | explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 172 | explicit Reference(const ResourceId& i, Type type = Type::kResource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | Reference(const ResourceNameRef& n, const ResourceId& i); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 174 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | bool Equals(const Value* value) const override; |
| 176 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 178 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 184 | bool operator<(const Reference&, const Reference&); |
| 185 | bool operator==(const Reference&, const Reference&); |
| 186 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 187 | // An ID resource. Has no real value, just a place holder. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 188 | struct Id : public TransformableItem<Id, BaseItem<Id>> { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 189 | Id() { |
| 190 | weak_ = true; |
| 191 | } |
| 192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | bool Equals(const Value* value) const override; |
| 194 | bool Flatten(android::Res_value* out) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 198 | // A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace. |
| 199 | // This shall *NOT* end up in the final resource table. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 200 | struct RawString : public TransformableItem<RawString, BaseItem<RawString>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | explicit RawString(const StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 204 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | bool Equals(const Value* value) const override; |
| 206 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 210 | // 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. |
| 212 | struct UntranslatableSection { |
| 213 | // Start offset inclusive. |
| 214 | size_t start; |
| 215 | |
| 216 | // End offset exclusive. |
| 217 | size_t end; |
| 218 | }; |
| 219 | |
| 220 | inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 221 | return a.start == b.start && a.end == b.end; |
| 222 | } |
| 223 | |
| 224 | inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 225 | return a.start != b.start || a.end != b.end; |
| 226 | } |
| 227 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 228 | struct String : public TransformableItem<String, BaseItem<String>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 231 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 236 | explicit String(const StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 237 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | bool Equals(const Value* value) const override; |
| 239 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 241 | void PrettyPrint(text::Printer* printer) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 242 | }; |
| 243 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 244 | struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 245 | StringPool::StyleRef value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 246 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 247 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | explicit StyledString(const StringPool::StyleRef& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 253 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | bool Equals(const Value* value) const override; |
| 255 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 259 | struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 260 | StringPool::Ref path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 262 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | io::IFile* file = nullptr; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 265 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 266 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | FileReference() = default; |
| 271 | explicit FileReference(const StringPool::Ref& path); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | bool Equals(const Value* value) const override; |
| 274 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 278 | // Represents any other android::Res_value. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 279 | struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | android::Res_value value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 281 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | BinaryPrimitive() = default; |
| 283 | explicit BinaryPrimitive(const android::Res_value& val); |
| 284 | BinaryPrimitive(uint8_t dataType, uint32_t data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 285 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | bool Equals(const Value* value) const override; |
| 287 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 289 | void PrettyPrint(text::Printer* printer) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 290 | }; |
| 291 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 292 | struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | struct Symbol { |
| 294 | Reference symbol; |
| 295 | uint32_t value; |
Ryan Mitchell | c167680 | 2019-05-20 16:47:08 -0700 | [diff] [blame] | 296 | uint8_t type; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 297 | |
| 298 | friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 301 | uint32_t type_mask; |
| 302 | int32_t min_int; |
| 303 | int32_t max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | std::vector<Symbol> symbols; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 305 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 306 | explicit Attribute(uint32_t t = 0u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 307 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | bool Equals(const Value* value) const override; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 309 | |
| 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 Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 315 | std::string MaskString() const; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 316 | static std::string MaskString(uint32_t type_mask); |
| 317 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | void Print(std::ostream* out) const override; |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 319 | bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 320 | }; |
| 321 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 322 | struct Style : public TransformableValue<Style, BaseValue<Style>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | struct Entry { |
| 324 | Reference key; |
| 325 | std::unique_ptr<Item> value; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 326 | |
| 327 | friend std::ostream& operator<<(std::ostream& out, const Entry& entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 328 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 329 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | Maybe<Reference> parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 331 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 332 | // If set to true, the parent was auto inferred from the style's name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | bool parent_inferred = false; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 334 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | std::vector<Entry> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | void Print(std::ostream* out) const override; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 339 | |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 343 | }; |
| 344 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 345 | struct Array : public TransformableValue<Array, BaseValue<Array>> { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 346 | std::vector<std::unique_ptr<Item>> elements; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 347 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 350 | }; |
| 351 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 352 | struct Plural : public TransformableValue<Plural, BaseValue<Plural>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | enum { Zero = 0, One, Two, Few, Many, Other, Count }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 354 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 355 | std::array<std::unique_ptr<Item>, Count> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 356 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 359 | }; |
| 360 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 361 | struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | std::vector<Reference> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 363 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | void Print(std::ostream* out) const override; |
| 366 | void MergeWith(Styleable* styleable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 369 | struct 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 Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 391 | template <typename T> |
| 392 | typename 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | } else { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 397 | value->Print(&out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 399 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 402 | struct 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 Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 417 | std::unique_ptr<Macro> TransformDerived(const Macro* value) override; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 420 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 421 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | #endif // AAPT_RESOURCE_VALUES_H |