blob: 20ebc1733cacd4a86fa845decad73aa004f886b0 [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
2 * Copyright (C) 2016 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 * Header file of an in-memory representation of DEX files.
17 */
18
19#ifndef ART_DEXLAYOUT_DEX_IR_H_
20#define ART_DEXLAYOUT_DEX_IR_H_
21
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include <stdint.h>
23
David Sehr7629f602016-08-07 16:01:51 -070024#include <vector>
David Sehr7629f602016-08-07 16:01:51 -070025
David Sehr2b5a38f2018-06-14 15:13:04 -070026#include "base/iteration_range.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Brazdil20c765f2018-10-27 21:45:15 +000028#include "base/safe_map.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070029#include "base/stl_util.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file-inl.h"
31#include "dex/dex_file_types.h"
David Sehr0225f8e2018-01-31 08:52:24 +000032#include "dex/utf.h"
David Sehr7629f602016-08-07 16:01:51 -070033
34namespace art {
35namespace dex_ir {
36
37// Forward declarations for classes used in containers or pointed to.
Jeff Hao3ab96b42016-09-09 18:35:01 -070038class AnnotationItem;
David Sehr7629f602016-08-07 16:01:51 -070039class AnnotationsDirectoryItem;
40class AnnotationSetItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070041class AnnotationSetRefList;
Jeff Hao5daee902017-04-27 18:00:38 -070042class CallSiteId;
David Sehr7629f602016-08-07 16:01:51 -070043class ClassData;
44class ClassDef;
45class CodeItem;
46class DebugInfoItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070047class EncodedAnnotation;
48class EncodedArrayItem;
49class EncodedValue;
David Sehr7629f602016-08-07 16:01:51 -070050class FieldId;
51class FieldItem;
52class Header;
David Brazdil20c765f2018-10-27 21:45:15 +000053class HiddenapiClassData;
David Sehr7629f602016-08-07 16:01:51 -070054class MapList;
55class MapItem;
Jeff Hao5daee902017-04-27 18:00:38 -070056class MethodHandleItem;
David Sehr7629f602016-08-07 16:01:51 -070057class MethodId;
58class MethodItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070059class ParameterAnnotation;
David Sehr7629f602016-08-07 16:01:51 -070060class ProtoId;
Jeff Hao3ab96b42016-09-09 18:35:01 -070061class StringData;
David Sehr7629f602016-08-07 16:01:51 -070062class StringId;
63class TryItem;
64class TypeId;
Jeff Hao3ab96b42016-09-09 18:35:01 -070065class TypeList;
66
67// Item size constants.
68static constexpr size_t kHeaderItemSize = 112;
69static constexpr size_t kStringIdItemSize = 4;
70static constexpr size_t kTypeIdItemSize = 4;
71static constexpr size_t kProtoIdItemSize = 12;
72static constexpr size_t kFieldIdItemSize = 8;
73static constexpr size_t kMethodIdItemSize = 8;
74static constexpr size_t kClassDefItemSize = 32;
Jeff Hao5daee902017-04-27 18:00:38 -070075static constexpr size_t kCallSiteIdItemSize = 4;
76static constexpr size_t kMethodHandleItemSize = 8;
David Sehr7629f602016-08-07 16:01:51 -070077
78// Visitor support
79class AbstractDispatcher {
80 public:
81 AbstractDispatcher() = default;
82 virtual ~AbstractDispatcher() { }
83
84 virtual void Dispatch(Header* header) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070085 virtual void Dispatch(const StringData* string_data) = 0;
David Sehr7629f602016-08-07 16:01:51 -070086 virtual void Dispatch(const StringId* string_id) = 0;
87 virtual void Dispatch(const TypeId* type_id) = 0;
88 virtual void Dispatch(const ProtoId* proto_id) = 0;
89 virtual void Dispatch(const FieldId* field_id) = 0;
90 virtual void Dispatch(const MethodId* method_id) = 0;
Jeff Hao5daee902017-04-27 18:00:38 -070091 virtual void Dispatch(const CallSiteId* call_site_id) = 0;
92 virtual void Dispatch(const MethodHandleItem* method_handle_item) = 0;
David Sehr7629f602016-08-07 16:01:51 -070093 virtual void Dispatch(ClassData* class_data) = 0;
94 virtual void Dispatch(ClassDef* class_def) = 0;
95 virtual void Dispatch(FieldItem* field_item) = 0;
96 virtual void Dispatch(MethodItem* method_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070097 virtual void Dispatch(EncodedArrayItem* array_item) = 0;
David Sehr7629f602016-08-07 16:01:51 -070098 virtual void Dispatch(CodeItem* code_item) = 0;
99 virtual void Dispatch(TryItem* try_item) = 0;
100 virtual void Dispatch(DebugInfoItem* debug_info_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700101 virtual void Dispatch(AnnotationItem* annotation_item) = 0;
David Sehr7629f602016-08-07 16:01:51 -0700102 virtual void Dispatch(AnnotationSetItem* annotation_set_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700103 virtual void Dispatch(AnnotationSetRefList* annotation_set_ref_list) = 0;
David Sehr7629f602016-08-07 16:01:51 -0700104 virtual void Dispatch(AnnotationsDirectoryItem* annotations_directory_item) = 0;
David Brazdil20c765f2018-10-27 21:45:15 +0000105 virtual void Dispatch(HiddenapiClassData* hiddenapi_class_data) = 0;
David Sehr7629f602016-08-07 16:01:51 -0700106 virtual void Dispatch(MapList* map_list) = 0;
107 virtual void Dispatch(MapItem* map_item) = 0;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher);
111};
112
David Sehr2b5a38f2018-06-14 15:13:04 -0700113template<class T> class Iterator : public std::iterator<std::random_access_iterator_tag, T> {
114 public:
115 using value_type = typename std::iterator<std::random_access_iterator_tag, T>::value_type;
116 using difference_type =
117 typename std::iterator<std::random_access_iterator_tag, value_type>::difference_type;
118 using pointer = typename std::iterator<std::random_access_iterator_tag, value_type>::pointer;
119 using reference = typename std::iterator<std::random_access_iterator_tag, value_type>::reference;
120
121 Iterator(const Iterator&) = default;
122 Iterator(Iterator&&) = default;
123 Iterator& operator=(const Iterator&) = default;
124 Iterator& operator=(Iterator&&) = default;
125
126 Iterator(const std::vector<T>& vector,
127 uint32_t position,
128 uint32_t iterator_end)
129 : vector_(&vector),
130 position_(position),
131 iterator_end_(iterator_end) { }
132 Iterator() : vector_(nullptr), position_(0U), iterator_end_(0U) { }
133
134 bool IsValid() const { return position_ < iterator_end_; }
135
136 bool operator==(const Iterator& rhs) const { return position_ == rhs.position_; }
137 bool operator!=(const Iterator& rhs) const { return !(*this == rhs); }
138 bool operator<(const Iterator& rhs) const { return position_ < rhs.position_; }
139 bool operator>(const Iterator& rhs) const { return rhs < *this; }
140 bool operator<=(const Iterator& rhs) const { return !(rhs < *this); }
141 bool operator>=(const Iterator& rhs) const { return !(*this < rhs); }
142
143 Iterator& operator++() { // Value after modification.
144 ++position_;
145 return *this;
146 }
147
148 Iterator operator++(int) {
149 Iterator temp = *this;
150 ++position_;
151 return temp;
152 }
153
154 Iterator& operator+=(difference_type delta) {
155 position_ += delta;
156 return *this;
157 }
158
159 Iterator operator+(difference_type delta) const {
160 Iterator temp = *this;
161 temp += delta;
162 return temp;
163 }
164
165 Iterator& operator--() { // Value after modification.
166 --position_;
167 return *this;
168 }
169
170 Iterator operator--(int) {
171 Iterator temp = *this;
172 --position_;
173 return temp;
174 }
175
176 Iterator& operator-=(difference_type delta) {
177 position_ -= delta;
178 return *this;
179 }
180
181 Iterator operator-(difference_type delta) const {
182 Iterator temp = *this;
183 temp -= delta;
184 return temp;
185 }
186
187 difference_type operator-(const Iterator& rhs) {
188 return position_ - rhs.position_;
189 }
190
191 reference operator*() const {
192 return const_cast<reference>((*vector_)[position_]);
193 }
194
195 pointer operator->() const {
196 return const_cast<pointer>(&((*vector_)[position_]));
197 }
198
199 reference operator[](difference_type n) const {
200 return (*vector_)[position_ + n];
201 }
202
203 private:
204 const std::vector<T>* vector_;
205 uint32_t position_;
206 uint32_t iterator_end_;
207
208 template <typename U>
209 friend bool operator<(const Iterator<U>& lhs, const Iterator<U>& rhs);
210};
211
David Sehr7629f602016-08-07 16:01:51 -0700212// Collections become owners of the objects added by moving them into unique pointers.
David Sehr2b5a38f2018-06-14 15:13:04 -0700213class CollectionBase {
David Sehr7629f602016-08-07 16:01:51 -0700214 public:
Jeff Haoea7c6292016-11-14 18:10:16 -0800215 CollectionBase() = default;
David Sehr2b5a38f2018-06-14 15:13:04 -0700216 virtual ~CollectionBase() { }
Jeff Haoea7c6292016-11-14 18:10:16 -0800217
David Sehr2b5a38f2018-06-14 15:13:04 -0700218 uint32_t GetOffset() const { return offset_; }
219 void SetOffset(uint32_t new_offset) { offset_ = new_offset; }
Andreas Gampe693eee92018-09-06 19:51:10 -0700220 virtual uint32_t Size() const = 0;
David Brazdil20c765f2018-10-27 21:45:15 +0000221 bool Empty() const { return Size() == 0u; }
Jeff Haoea7c6292016-11-14 18:10:16 -0800222
223 private:
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800224 // Start out unassigned.
225 uint32_t offset_ = 0u;
Jeff Haoea7c6292016-11-14 18:10:16 -0800226
227 DISALLOW_COPY_AND_ASSIGN(CollectionBase);
228};
229
David Sehr2b5a38f2018-06-14 15:13:04 -0700230template<class T> class CollectionVector : public CollectionBase {
Jeff Haoea7c6292016-11-14 18:10:16 -0800231 public:
David Sehr2b5a38f2018-06-14 15:13:04 -0700232 using ElementType = std::unique_ptr<T>;
233
David Sehrd83437c2018-06-11 14:06:23 -0700234 CollectionVector() { }
235 explicit CollectionVector(size_t size) {
236 // Preallocate so that assignment does not invalidate pointers into the vector.
237 collection_.reserve(size);
238 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100239 ~CollectionVector() override { }
Jeff Haoea7c6292016-11-14 18:10:16 -0800240
David Sehr2b5a38f2018-06-14 15:13:04 -0700241 template<class... Args>
242 T* CreateAndAddItem(Args&&... args) {
243 T* object = new T(std::forward<Args>(args)...);
244 collection_.push_back(std::unique_ptr<T>(object));
245 return object;
246 }
247
Roland Levillainf73caca2018-08-24 17:19:07 +0100248 uint32_t Size() const override { return collection_.size(); }
David Sehr2b5a38f2018-06-14 15:13:04 -0700249
250 Iterator<ElementType> begin() const { return Iterator<ElementType>(collection_, 0U, Size()); }
251 Iterator<ElementType> end() const { return Iterator<ElementType>(collection_, Size(), Size()); }
252
253 const ElementType& operator[](size_t index) const {
254 DCHECK_LT(index, Size());
255 return collection_[index];
256 }
257 ElementType& operator[](size_t index) {
258 DCHECK_LT(index, Size());
259 return collection_[index];
260 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800261
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800262 // Sort the vector by copying pointers over.
263 template <typename MapType>
264 void SortByMapOrder(const MapType& map) {
265 auto it = map.begin();
266 CHECK_EQ(map.size(), Size());
267 for (size_t i = 0; i < Size(); ++i) {
268 // There are times when the array will temporarily contain the same pointer twice, doing the
269 // release here sure there is no double free errors.
David Sehr2b5a38f2018-06-14 15:13:04 -0700270 collection_[i].release();
271 collection_[i].reset(it->second);
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800272 ++it;
273 }
274 }
275
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800276 protected:
David Sehr2b5a38f2018-06-14 15:13:04 -0700277 std::vector<ElementType> collection_;
David Sehr7629f602016-08-07 16:01:51 -0700278
279 private:
Jeff Haoea7c6292016-11-14 18:10:16 -0800280 DISALLOW_COPY_AND_ASSIGN(CollectionVector);
281};
282
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800283template<class T> class IndexedCollectionVector : public CollectionVector<T> {
284 public:
285 using Vector = std::vector<std::unique_ptr<T>>;
286 IndexedCollectionVector() = default;
David Sehrd83437c2018-06-11 14:06:23 -0700287 explicit IndexedCollectionVector(size_t size) : CollectionVector<T>(size) { }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800288
David Sehrd83437c2018-06-11 14:06:23 -0700289 template <class... Args>
290 T* CreateAndAddIndexedItem(uint32_t index, Args&&... args) {
291 T* object = CollectionVector<T>::CreateAndAddItem(std::forward<Args>(args)...);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800292 object->SetIndex(index);
David Sehrd83437c2018-06-11 14:06:23 -0700293 return object;
294 }
295
David Sehr2b5a38f2018-06-14 15:13:04 -0700296 T* operator[](size_t index) const {
David Sehrd83437c2018-06-11 14:06:23 -0700297 DCHECK_NE(CollectionVector<T>::collection_[index].get(), static_cast<T*>(nullptr));
298 return CollectionVector<T>::collection_[index].get();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800299 }
300
David Sehr2b5a38f2018-06-14 15:13:04 -0700301 private:
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800302 DISALLOW_COPY_AND_ASSIGN(IndexedCollectionVector);
303};
304
David Sehr7629f602016-08-07 16:01:51 -0700305class Item {
306 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700307 Item() { }
David Sehr7629f602016-08-07 16:01:51 -0700308 virtual ~Item() { }
David Sehr853a8e12016-09-01 13:03:50 -0700309
David Sehrd83437c2018-06-11 14:06:23 -0700310 Item(Item&&) = default;
311
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800312 // Return the assigned offset.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800313 uint32_t GetOffset() const WARN_UNUSED {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800314 CHECK(OffsetAssigned());
315 return offset_;
316 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800317 uint32_t GetSize() const WARN_UNUSED { return size_; }
David Sehr7629f602016-08-07 16:01:51 -0700318 void SetOffset(uint32_t offset) { offset_ = offset; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700319 void SetSize(uint32_t size) { size_ = size; }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800320 bool OffsetAssigned() const {
321 return offset_ != kOffsetUnassigned;
322 }
David Sehr853a8e12016-09-01 13:03:50 -0700323
David Sehr7629f602016-08-07 16:01:51 -0700324 protected:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700325 Item(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
326
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800327 // 0 is the dex file header and shouldn't be a valid offset for any part of the dex file.
328 static constexpr uint32_t kOffsetUnassigned = 0u;
329
330 // Start out unassigned.
331 uint32_t offset_ = kOffsetUnassigned;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700332 uint32_t size_ = 0;
333};
334
335class IndexedItem : public Item {
336 public:
337 IndexedItem() { }
338 virtual ~IndexedItem() { }
339
340 uint32_t GetIndex() const { return index_; }
341 void SetIndex(uint32_t index) { index_ = index; }
342
343 protected:
344 IndexedItem(uint32_t offset, uint32_t size, uint32_t index)
345 : Item(offset, size), index_(index) { }
346
347 uint32_t index_ = 0;
David Sehr7629f602016-08-07 16:01:51 -0700348};
349
350class Header : public Item {
351 public:
David Sehr853a8e12016-09-01 13:03:50 -0700352 Header(const uint8_t* magic,
353 uint32_t checksum,
354 const uint8_t* signature,
355 uint32_t endian_tag,
356 uint32_t file_size,
357 uint32_t header_size,
358 uint32_t link_size,
359 uint32_t link_offset,
360 uint32_t data_size,
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800361 uint32_t data_offset,
362 bool support_default_methods)
David Sehrd83437c2018-06-11 14:06:23 -0700363 : Item(0, kHeaderItemSize), support_default_methods_(support_default_methods) {
364 ConstructorHelper(magic,
365 checksum,
366 signature,
367 endian_tag,
368 file_size,
369 header_size,
370 link_size,
371 link_offset,
372 data_size,
373 data_offset);
374 }
375
376 Header(const uint8_t* magic,
377 uint32_t checksum,
378 const uint8_t* signature,
379 uint32_t endian_tag,
380 uint32_t file_size,
381 uint32_t header_size,
382 uint32_t link_size,
383 uint32_t link_offset,
384 uint32_t data_size,
385 uint32_t data_offset,
386 bool support_default_methods,
387 uint32_t num_string_ids,
388 uint32_t num_type_ids,
389 uint32_t num_proto_ids,
390 uint32_t num_field_ids,
391 uint32_t num_method_ids,
392 uint32_t num_class_defs)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700393 : Item(0, kHeaderItemSize),
David Sehrd83437c2018-06-11 14:06:23 -0700394 support_default_methods_(support_default_methods),
David Sehr2b5a38f2018-06-14 15:13:04 -0700395 string_ids_(num_string_ids),
396 type_ids_(num_type_ids),
397 proto_ids_(num_proto_ids),
398 field_ids_(num_field_ids),
399 method_ids_(num_method_ids),
400 class_defs_(num_class_defs) {
David Sehrd83437c2018-06-11 14:06:23 -0700401 ConstructorHelper(magic,
402 checksum,
403 signature,
404 endian_tag,
405 file_size,
406 header_size,
407 link_size,
408 link_offset,
409 data_size,
410 data_offset);
David Sehr853a8e12016-09-01 13:03:50 -0700411 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100412 ~Header() override { }
David Sehr7629f602016-08-07 16:01:51 -0700413
Jeff Hao3ab96b42016-09-09 18:35:01 -0700414 static size_t ItemSize() { return kHeaderItemSize; }
415
David Sehr7629f602016-08-07 16:01:51 -0700416 const uint8_t* Magic() const { return magic_; }
417 uint32_t Checksum() const { return checksum_; }
418 const uint8_t* Signature() const { return signature_; }
419 uint32_t EndianTag() const { return endian_tag_; }
420 uint32_t FileSize() const { return file_size_; }
421 uint32_t HeaderSize() const { return header_size_; }
422 uint32_t LinkSize() const { return link_size_; }
423 uint32_t LinkOffset() const { return link_offset_; }
424 uint32_t DataSize() const { return data_size_; }
425 uint32_t DataOffset() const { return data_offset_; }
426
427 void SetChecksum(uint32_t new_checksum) { checksum_ = new_checksum; }
428 void SetSignature(const uint8_t* new_signature) {
429 memcpy(signature_, new_signature, sizeof(signature_));
430 }
431 void SetFileSize(uint32_t new_file_size) { file_size_ = new_file_size; }
432 void SetHeaderSize(uint32_t new_header_size) { header_size_ = new_header_size; }
433 void SetLinkSize(uint32_t new_link_size) { link_size_ = new_link_size; }
434 void SetLinkOffset(uint32_t new_link_offset) { link_offset_ = new_link_offset; }
435 void SetDataSize(uint32_t new_data_size) { data_size_ = new_data_size; }
436 void SetDataOffset(uint32_t new_data_offset) { data_offset_ = new_data_offset; }
437
David Sehr2b5a38f2018-06-14 15:13:04 -0700438 IndexedCollectionVector<StringId>& StringIds() { return string_ids_; }
439 const IndexedCollectionVector<StringId>& StringIds() const { return string_ids_; }
440 IndexedCollectionVector<TypeId>& TypeIds() { return type_ids_; }
441 const IndexedCollectionVector<TypeId>& TypeIds() const { return type_ids_; }
442 IndexedCollectionVector<ProtoId>& ProtoIds() { return proto_ids_; }
443 const IndexedCollectionVector<ProtoId>& ProtoIds() const { return proto_ids_; }
444 IndexedCollectionVector<FieldId>& FieldIds() { return field_ids_; }
445 const IndexedCollectionVector<FieldId>& FieldIds() const { return field_ids_; }
446 IndexedCollectionVector<MethodId>& MethodIds() { return method_ids_; }
447 const IndexedCollectionVector<MethodId>& MethodIds() const { return method_ids_; }
448 IndexedCollectionVector<ClassDef>& ClassDefs() { return class_defs_; }
449 const IndexedCollectionVector<ClassDef>& ClassDefs() const { return class_defs_; }
450 IndexedCollectionVector<CallSiteId>& CallSiteIds() { return call_site_ids_; }
451 const IndexedCollectionVector<CallSiteId>& CallSiteIds() const { return call_site_ids_; }
452 IndexedCollectionVector<MethodHandleItem>& MethodHandleItems() { return method_handle_items_; }
453 const IndexedCollectionVector<MethodHandleItem>& MethodHandleItems() const {
454 return method_handle_items_;
455 }
456 CollectionVector<StringData>& StringDatas() { return string_datas_; }
457 const CollectionVector<StringData>& StringDatas() const { return string_datas_; }
458 CollectionVector<TypeList>& TypeLists() { return type_lists_; }
459 const CollectionVector<TypeList>& TypeLists() const { return type_lists_; }
460 CollectionVector<EncodedArrayItem>& EncodedArrayItems() { return encoded_array_items_; }
461 const CollectionVector<EncodedArrayItem>& EncodedArrayItems() const {
462 return encoded_array_items_;
463 }
464 CollectionVector<AnnotationItem>& AnnotationItems() { return annotation_items_; }
465 const CollectionVector<AnnotationItem>& AnnotationItems() const { return annotation_items_; }
466 CollectionVector<AnnotationSetItem>& AnnotationSetItems() { return annotation_set_items_; }
467 const CollectionVector<AnnotationSetItem>& AnnotationSetItems() const {
468 return annotation_set_items_;
469 }
470 CollectionVector<AnnotationSetRefList>& AnnotationSetRefLists() {
471 return annotation_set_ref_lists_;
472 }
473 const CollectionVector<AnnotationSetRefList>& AnnotationSetRefLists() const {
474 return annotation_set_ref_lists_;
475 }
476 CollectionVector<AnnotationsDirectoryItem>& AnnotationsDirectoryItems() {
477 return annotations_directory_items_;
478 }
479 const CollectionVector<AnnotationsDirectoryItem>& AnnotationsDirectoryItems() const {
480 return annotations_directory_items_;
481 }
David Brazdil20c765f2018-10-27 21:45:15 +0000482 IndexedCollectionVector<HiddenapiClassData>& HiddenapiClassDatas() {
483 return hiddenapi_class_datas_;
484 }
485 const IndexedCollectionVector<HiddenapiClassData>& HiddenapiClassDatas() const {
486 return hiddenapi_class_datas_;
487 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700488 CollectionVector<DebugInfoItem>& DebugInfoItems() { return debug_info_items_; }
489 const CollectionVector<DebugInfoItem>& DebugInfoItems() const { return debug_info_items_; }
490 CollectionVector<CodeItem>& CodeItems() { return code_items_; }
491 const CollectionVector<CodeItem>& CodeItems() const { return code_items_; }
492 CollectionVector<ClassData>& ClassDatas() { return class_datas_; }
493 const CollectionVector<ClassData>& ClassDatas() const { return class_datas_; }
494
495 StringId* GetStringIdOrNullPtr(uint32_t index) {
496 return index == dex::kDexNoIndex ? nullptr : StringIds()[index];
497 }
498 TypeId* GetTypeIdOrNullPtr(uint16_t index) {
499 return index == DexFile::kDexNoIndex16 ? nullptr : TypeIds()[index];
500 }
501
502 uint32_t MapListOffset() const { return map_list_offset_; }
503 void SetMapListOffset(uint32_t new_offset) { map_list_offset_ = new_offset; }
504
505 const std::vector<uint8_t>& LinkData() const { return link_data_; }
506 void SetLinkData(std::vector<uint8_t>&& link_data) { link_data_ = std::move(link_data); }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700507
David Sehr7629f602016-08-07 16:01:51 -0700508 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
509
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800510 bool SupportDefaultMethods() const {
511 return support_default_methods_;
512 }
513
David Sehr7629f602016-08-07 16:01:51 -0700514 private:
David Sehr7629f602016-08-07 16:01:51 -0700515 uint8_t magic_[8];
516 uint32_t checksum_;
517 uint8_t signature_[DexFile::kSha1DigestSize];
518 uint32_t endian_tag_;
519 uint32_t file_size_;
520 uint32_t header_size_;
521 uint32_t link_size_;
522 uint32_t link_offset_;
523 uint32_t data_size_;
524 uint32_t data_offset_;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800525 const bool support_default_methods_;
David Sehr7629f602016-08-07 16:01:51 -0700526
David Sehrd83437c2018-06-11 14:06:23 -0700527 void ConstructorHelper(const uint8_t* magic,
528 uint32_t checksum,
529 const uint8_t* signature,
530 uint32_t endian_tag,
531 uint32_t file_size,
532 uint32_t header_size,
533 uint32_t link_size,
534 uint32_t link_offset,
535 uint32_t data_size,
536 uint32_t data_offset) {
537 checksum_ = checksum;
538 endian_tag_ = endian_tag;
539 file_size_ = file_size;
540 header_size_ = header_size;
541 link_size_ = link_size;
542 link_offset_ = link_offset;
543 data_size_ = data_size;
544 data_offset_ = data_offset;
545 memcpy(magic_, magic, sizeof(magic_));
546 memcpy(signature_, signature, sizeof(signature_));
547 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700548
549 // Collection vectors own the IR data.
550 IndexedCollectionVector<StringId> string_ids_;
551 IndexedCollectionVector<TypeId> type_ids_;
552 IndexedCollectionVector<ProtoId> proto_ids_;
553 IndexedCollectionVector<FieldId> field_ids_;
554 IndexedCollectionVector<MethodId> method_ids_;
555 IndexedCollectionVector<ClassDef> class_defs_;
556 IndexedCollectionVector<CallSiteId> call_site_ids_;
557 IndexedCollectionVector<MethodHandleItem> method_handle_items_;
558 IndexedCollectionVector<StringData> string_datas_;
559 IndexedCollectionVector<TypeList> type_lists_;
560 IndexedCollectionVector<EncodedArrayItem> encoded_array_items_;
561 IndexedCollectionVector<AnnotationItem> annotation_items_;
562 IndexedCollectionVector<AnnotationSetItem> annotation_set_items_;
563 IndexedCollectionVector<AnnotationSetRefList> annotation_set_ref_lists_;
564 IndexedCollectionVector<AnnotationsDirectoryItem> annotations_directory_items_;
David Brazdil20c765f2018-10-27 21:45:15 +0000565 IndexedCollectionVector<HiddenapiClassData> hiddenapi_class_datas_;
David Sehr2b5a38f2018-06-14 15:13:04 -0700566 // The order of the vectors controls the layout of the output file by index order, to change the
567 // layout just sort the vector. Note that you may only change the order of the non indexed vectors
568 // below. Indexed vectors are accessed by indices in other places, changing the sorting order will
569 // invalidate the existing indices and is not currently supported.
570 CollectionVector<DebugInfoItem> debug_info_items_;
571 CollectionVector<CodeItem> code_items_;
572 CollectionVector<ClassData> class_datas_;
573
574 uint32_t map_list_offset_ = 0;
575
576 // Link data.
577 std::vector<uint8_t> link_data_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700578
David Sehr7629f602016-08-07 16:01:51 -0700579 DISALLOW_COPY_AND_ASSIGN(Header);
580};
581
Jeff Hao3ab96b42016-09-09 18:35:01 -0700582class StringData : public Item {
David Sehr7629f602016-08-07 16:01:51 -0700583 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700584 explicit StringData(const char* data) : data_(strdup(data)) {
Jeff Haoa8621002016-10-04 18:13:44 +0000585 size_ = UnsignedLeb128Size(CountModifiedUtf8Chars(data)) + strlen(data);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700586 }
David Sehr7629f602016-08-07 16:01:51 -0700587
588 const char* Data() const { return data_.get(); }
589
590 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
591
592 private:
Jeff Haoa8621002016-10-04 18:13:44 +0000593 UniqueCPtr<const char> data_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700594
595 DISALLOW_COPY_AND_ASSIGN(StringData);
596};
597
598class StringId : public IndexedItem {
599 public:
600 explicit StringId(StringData* string_data) : string_data_(string_data) {
601 size_ = kStringIdItemSize;
602 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100603 ~StringId() override { }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700604
605 static size_t ItemSize() { return kStringIdItemSize; }
606
607 const char* Data() const { return string_data_->Data(); }
608 StringData* DataItem() const { return string_data_; }
609
610 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
611
612 private:
613 StringData* string_data_;
614
David Sehr7629f602016-08-07 16:01:51 -0700615 DISALLOW_COPY_AND_ASSIGN(StringId);
616};
617
Jeff Hao3ab96b42016-09-09 18:35:01 -0700618class TypeId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700619 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700620 explicit TypeId(StringId* string_id) : string_id_(string_id) { size_ = kTypeIdItemSize; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100621 ~TypeId() override { }
David Sehr7629f602016-08-07 16:01:51 -0700622
Jeff Hao3ab96b42016-09-09 18:35:01 -0700623 static size_t ItemSize() { return kTypeIdItemSize; }
624
David Sehr7629f602016-08-07 16:01:51 -0700625 StringId* GetStringId() const { return string_id_; }
626
627 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
628
629 private:
630 StringId* string_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700631
David Sehr7629f602016-08-07 16:01:51 -0700632 DISALLOW_COPY_AND_ASSIGN(TypeId);
633};
634
David Sehr853a8e12016-09-01 13:03:50 -0700635using TypeIdVector = std::vector<const TypeId*>;
636
Jeff Hao3ab96b42016-09-09 18:35:01 -0700637class TypeList : public Item {
David Sehr7629f602016-08-07 16:01:51 -0700638 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700639 explicit TypeList(TypeIdVector* type_list) : type_list_(type_list) {
640 size_ = sizeof(uint32_t) + (type_list->size() * sizeof(uint16_t));
641 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100642 ~TypeList() override { }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700643
644 const TypeIdVector* GetTypeList() const { return type_list_.get(); }
645
646 private:
647 std::unique_ptr<TypeIdVector> type_list_;
648
649 DISALLOW_COPY_AND_ASSIGN(TypeList);
650};
651
652class ProtoId : public IndexedItem {
653 public:
654 ProtoId(const StringId* shorty, const TypeId* return_type, TypeList* parameters)
655 : shorty_(shorty), return_type_(return_type), parameters_(parameters)
656 { size_ = kProtoIdItemSize; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100657 ~ProtoId() override { }
David Sehr7629f602016-08-07 16:01:51 -0700658
Jeff Hao3ab96b42016-09-09 18:35:01 -0700659 static size_t ItemSize() { return kProtoIdItemSize; }
660
David Sehr7629f602016-08-07 16:01:51 -0700661 const StringId* Shorty() const { return shorty_; }
662 const TypeId* ReturnType() const { return return_type_; }
Jeff Haoa8621002016-10-04 18:13:44 +0000663 const TypeList* Parameters() const { return parameters_; }
David Sehr7629f602016-08-07 16:01:51 -0700664
665 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
666
667 private:
668 const StringId* shorty_;
669 const TypeId* return_type_;
Jeff Haoa8621002016-10-04 18:13:44 +0000670 TypeList* parameters_; // This can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -0700671
David Sehr7629f602016-08-07 16:01:51 -0700672 DISALLOW_COPY_AND_ASSIGN(ProtoId);
673};
674
Jeff Hao3ab96b42016-09-09 18:35:01 -0700675class FieldId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700676 public:
David Sehr853a8e12016-09-01 13:03:50 -0700677 FieldId(const TypeId* klass, const TypeId* type, const StringId* name)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700678 : class_(klass), type_(type), name_(name) { size_ = kFieldIdItemSize; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100679 ~FieldId() override { }
David Sehr7629f602016-08-07 16:01:51 -0700680
Jeff Hao3ab96b42016-09-09 18:35:01 -0700681 static size_t ItemSize() { return kFieldIdItemSize; }
682
David Sehr7629f602016-08-07 16:01:51 -0700683 const TypeId* Class() const { return class_; }
684 const TypeId* Type() const { return type_; }
685 const StringId* Name() const { return name_; }
686
687 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
688
689 private:
690 const TypeId* class_;
691 const TypeId* type_;
692 const StringId* name_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700693
David Sehr7629f602016-08-07 16:01:51 -0700694 DISALLOW_COPY_AND_ASSIGN(FieldId);
695};
696
Jeff Hao3ab96b42016-09-09 18:35:01 -0700697class MethodId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700698 public:
David Sehr853a8e12016-09-01 13:03:50 -0700699 MethodId(const TypeId* klass, const ProtoId* proto, const StringId* name)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700700 : class_(klass), proto_(proto), name_(name) { size_ = kMethodIdItemSize; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100701 ~MethodId() override { }
David Sehr7629f602016-08-07 16:01:51 -0700702
Jeff Hao3ab96b42016-09-09 18:35:01 -0700703 static size_t ItemSize() { return kMethodIdItemSize; }
704
David Sehr7629f602016-08-07 16:01:51 -0700705 const TypeId* Class() const { return class_; }
706 const ProtoId* Proto() const { return proto_; }
707 const StringId* Name() const { return name_; }
708
709 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
710
711 private:
712 const TypeId* class_;
713 const ProtoId* proto_;
714 const StringId* name_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700715
David Sehr7629f602016-08-07 16:01:51 -0700716 DISALLOW_COPY_AND_ASSIGN(MethodId);
717};
718
719class FieldItem : public Item {
720 public:
David Sehr853a8e12016-09-01 13:03:50 -0700721 FieldItem(uint32_t access_flags, const FieldId* field_id)
722 : access_flags_(access_flags), field_id_(field_id) { }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100723 ~FieldItem() override { }
David Sehr7629f602016-08-07 16:01:51 -0700724
David Sehrd83437c2018-06-11 14:06:23 -0700725 FieldItem(FieldItem&&) = default;
726
David Sehr7629f602016-08-07 16:01:51 -0700727 uint32_t GetAccessFlags() const { return access_flags_; }
728 const FieldId* GetFieldId() const { return field_id_; }
729
730 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
731
732 private:
733 uint32_t access_flags_;
734 const FieldId* field_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700735
David Sehr7629f602016-08-07 16:01:51 -0700736 DISALLOW_COPY_AND_ASSIGN(FieldItem);
737};
738
David Sehrd83437c2018-06-11 14:06:23 -0700739using FieldItemVector = std::vector<FieldItem>;
David Sehr853a8e12016-09-01 13:03:50 -0700740
David Sehr7629f602016-08-07 16:01:51 -0700741class MethodItem : public Item {
742 public:
Jeff Haoea7c6292016-11-14 18:10:16 -0800743 MethodItem(uint32_t access_flags, const MethodId* method_id, CodeItem* code)
David Sehr853a8e12016-09-01 13:03:50 -0700744 : access_flags_(access_flags), method_id_(method_id), code_(code) { }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100745 ~MethodItem() override { }
David Sehr7629f602016-08-07 16:01:51 -0700746
David Sehrd83437c2018-06-11 14:06:23 -0700747 MethodItem(MethodItem&&) = default;
748
David Sehr7629f602016-08-07 16:01:51 -0700749 uint32_t GetAccessFlags() const { return access_flags_; }
750 const MethodId* GetMethodId() const { return method_id_; }
Jeff Haoea7c6292016-11-14 18:10:16 -0800751 CodeItem* GetCodeItem() { return code_; }
David Sehr7629f602016-08-07 16:01:51 -0700752
753 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
754
755 private:
756 uint32_t access_flags_;
757 const MethodId* method_id_;
Jeff Haoea7c6292016-11-14 18:10:16 -0800758 CodeItem* code_; // This can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -0700759
David Sehr7629f602016-08-07 16:01:51 -0700760 DISALLOW_COPY_AND_ASSIGN(MethodItem);
761};
762
David Sehrd83437c2018-06-11 14:06:23 -0700763using MethodItemVector = std::vector<MethodItem>;
David Sehr853a8e12016-09-01 13:03:50 -0700764
Jeff Hao3ab96b42016-09-09 18:35:01 -0700765class EncodedValue {
David Sehr7629f602016-08-07 16:01:51 -0700766 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700767 explicit EncodedValue(uint8_t type) : type_(type) { }
David Sehr7629f602016-08-07 16:01:51 -0700768
769 int8_t Type() const { return type_; }
David Sehr7629f602016-08-07 16:01:51 -0700770
Jeff Hao3ab96b42016-09-09 18:35:01 -0700771 void SetBoolean(bool z) { u_.bool_val_ = z; }
772 void SetByte(int8_t b) { u_.byte_val_ = b; }
773 void SetShort(int16_t s) { u_.short_val_ = s; }
774 void SetChar(uint16_t c) { u_.char_val_ = c; }
775 void SetInt(int32_t i) { u_.int_val_ = i; }
776 void SetLong(int64_t l) { u_.long_val_ = l; }
777 void SetFloat(float f) { u_.float_val_ = f; }
778 void SetDouble(double d) { u_.double_val_ = d; }
779 void SetStringId(StringId* string_id) { u_.string_val_ = string_id; }
780 void SetTypeId(TypeId* type_id) { u_.type_val_ = type_id; }
Jeff Hao5daee902017-04-27 18:00:38 -0700781 void SetProtoId(ProtoId* proto_id) { u_.proto_val_ = proto_id; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700782 void SetFieldId(FieldId* field_id) { u_.field_val_ = field_id; }
783 void SetMethodId(MethodId* method_id) { u_.method_val_ = method_id; }
Jeff Hao5daee902017-04-27 18:00:38 -0700784 void SetMethodHandle(MethodHandleItem* method_handle) { u_.method_handle_val_ = method_handle; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700785 void SetEncodedArray(EncodedArrayItem* encoded_array) { encoded_array_.reset(encoded_array); }
786 void SetEncodedAnnotation(EncodedAnnotation* encoded_annotation)
787 { encoded_annotation_.reset(encoded_annotation); }
788
789 bool GetBoolean() const { return u_.bool_val_; }
790 int8_t GetByte() const { return u_.byte_val_; }
791 int16_t GetShort() const { return u_.short_val_; }
792 uint16_t GetChar() const { return u_.char_val_; }
793 int32_t GetInt() const { return u_.int_val_; }
794 int64_t GetLong() const { return u_.long_val_; }
795 float GetFloat() const { return u_.float_val_; }
796 double GetDouble() const { return u_.double_val_; }
797 StringId* GetStringId() const { return u_.string_val_; }
798 TypeId* GetTypeId() const { return u_.type_val_; }
Jeff Hao5daee902017-04-27 18:00:38 -0700799 ProtoId* GetProtoId() const { return u_.proto_val_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700800 FieldId* GetFieldId() const { return u_.field_val_; }
801 MethodId* GetMethodId() const { return u_.method_val_; }
Jeff Hao5daee902017-04-27 18:00:38 -0700802 MethodHandleItem* GetMethodHandle() const { return u_.method_handle_val_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700803 EncodedArrayItem* GetEncodedArray() const { return encoded_array_.get(); }
804 EncodedAnnotation* GetEncodedAnnotation() const { return encoded_annotation_.get(); }
805
806 EncodedAnnotation* ReleaseEncodedAnnotation() { return encoded_annotation_.release(); }
David Sehr7629f602016-08-07 16:01:51 -0700807
808 private:
David Sehr7629f602016-08-07 16:01:51 -0700809 uint8_t type_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700810 union {
811 bool bool_val_;
812 int8_t byte_val_;
813 int16_t short_val_;
814 uint16_t char_val_;
815 int32_t int_val_;
816 int64_t long_val_;
817 float float_val_;
818 double double_val_;
819 StringId* string_val_;
820 TypeId* type_val_;
Jeff Hao5daee902017-04-27 18:00:38 -0700821 ProtoId* proto_val_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700822 FieldId* field_val_;
823 MethodId* method_val_;
Jeff Hao5daee902017-04-27 18:00:38 -0700824 MethodHandleItem* method_handle_val_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700825 } u_;
826 std::unique_ptr<EncodedArrayItem> encoded_array_;
827 std::unique_ptr<EncodedAnnotation> encoded_annotation_;
828
829 DISALLOW_COPY_AND_ASSIGN(EncodedValue);
David Sehr7629f602016-08-07 16:01:51 -0700830};
831
Jeff Hao3ab96b42016-09-09 18:35:01 -0700832using EncodedValueVector = std::vector<std::unique_ptr<EncodedValue>>;
833
834class AnnotationElement {
835 public:
836 AnnotationElement(StringId* name, EncodedValue* value) : name_(name), value_(value) { }
837
838 StringId* GetName() const { return name_; }
839 EncodedValue* GetValue() const { return value_.get(); }
840
841 private:
842 StringId* name_;
843 std::unique_ptr<EncodedValue> value_;
844
845 DISALLOW_COPY_AND_ASSIGN(AnnotationElement);
846};
847
848using AnnotationElementVector = std::vector<std::unique_ptr<AnnotationElement>>;
849
850class EncodedAnnotation {
851 public:
852 EncodedAnnotation(TypeId* type, AnnotationElementVector* elements)
853 : type_(type), elements_(elements) { }
854
855 TypeId* GetType() const { return type_; }
856 AnnotationElementVector* GetAnnotationElements() const { return elements_.get(); }
857
858 private:
859 TypeId* type_;
860 std::unique_ptr<AnnotationElementVector> elements_;
861
862 DISALLOW_COPY_AND_ASSIGN(EncodedAnnotation);
863};
864
865class EncodedArrayItem : public Item {
866 public:
867 explicit EncodedArrayItem(EncodedValueVector* encoded_values)
868 : encoded_values_(encoded_values) { }
869
870 EncodedValueVector* GetEncodedValues() const { return encoded_values_.get(); }
871
872 private:
873 std::unique_ptr<EncodedValueVector> encoded_values_;
874
875 DISALLOW_COPY_AND_ASSIGN(EncodedArrayItem);
876};
David Sehr853a8e12016-09-01 13:03:50 -0700877
David Sehr7629f602016-08-07 16:01:51 -0700878class ClassData : public Item {
879 public:
David Sehr853a8e12016-09-01 13:03:50 -0700880 ClassData(FieldItemVector* static_fields,
881 FieldItemVector* instance_fields,
882 MethodItemVector* direct_methods,
883 MethodItemVector* virtual_methods)
884 : static_fields_(static_fields),
885 instance_fields_(instance_fields),
886 direct_methods_(direct_methods),
887 virtual_methods_(virtual_methods) { }
888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100889 ~ClassData() override = default;
David Sehr853a8e12016-09-01 13:03:50 -0700890 FieldItemVector* StaticFields() { return static_fields_.get(); }
891 FieldItemVector* InstanceFields() { return instance_fields_.get(); }
892 MethodItemVector* DirectMethods() { return direct_methods_.get(); }
893 MethodItemVector* VirtualMethods() { return virtual_methods_.get(); }
David Sehr7629f602016-08-07 16:01:51 -0700894
895 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
896
897 private:
David Sehr853a8e12016-09-01 13:03:50 -0700898 std::unique_ptr<FieldItemVector> static_fields_;
899 std::unique_ptr<FieldItemVector> instance_fields_;
900 std::unique_ptr<MethodItemVector> direct_methods_;
901 std::unique_ptr<MethodItemVector> virtual_methods_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700902
David Sehr7629f602016-08-07 16:01:51 -0700903 DISALLOW_COPY_AND_ASSIGN(ClassData);
904};
905
Jeff Hao3ab96b42016-09-09 18:35:01 -0700906class ClassDef : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700907 public:
David Sehr853a8e12016-09-01 13:03:50 -0700908 ClassDef(const TypeId* class_type,
909 uint32_t access_flags,
910 const TypeId* superclass,
Jeff Hao3ab96b42016-09-09 18:35:01 -0700911 TypeList* interfaces,
David Sehr853a8e12016-09-01 13:03:50 -0700912 const StringId* source_file,
913 AnnotationsDirectoryItem* annotations,
Jeff Hao3ab96b42016-09-09 18:35:01 -0700914 EncodedArrayItem* static_values,
David Sehr853a8e12016-09-01 13:03:50 -0700915 ClassData* class_data)
916 : class_type_(class_type),
917 access_flags_(access_flags),
918 superclass_(superclass),
919 interfaces_(interfaces),
David Sehr853a8e12016-09-01 13:03:50 -0700920 source_file_(source_file),
921 annotations_(annotations),
Jeff Haoa8621002016-10-04 18:13:44 +0000922 class_data_(class_data),
923 static_values_(static_values) { size_ = kClassDefItemSize; }
David Sehr853a8e12016-09-01 13:03:50 -0700924
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100925 ~ClassDef() override { }
David Sehr7629f602016-08-07 16:01:51 -0700926
Jeff Hao3ab96b42016-09-09 18:35:01 -0700927 static size_t ItemSize() { return kClassDefItemSize; }
928
David Sehr7629f602016-08-07 16:01:51 -0700929 const TypeId* ClassType() const { return class_type_; }
930 uint32_t GetAccessFlags() const { return access_flags_; }
931 const TypeId* Superclass() const { return superclass_; }
Jeff Haocc829592017-03-14 16:13:39 -0700932 const TypeList* Interfaces() { return interfaces_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700933 uint32_t InterfacesOffset() { return interfaces_ == nullptr ? 0 : interfaces_->GetOffset(); }
David Sehr7629f602016-08-07 16:01:51 -0700934 const StringId* SourceFile() const { return source_file_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700935 AnnotationsDirectoryItem* Annotations() const { return annotations_; }
Nicolas Geoffrayfd1a6c22016-10-04 11:01:17 +0000936 ClassData* GetClassData() { return class_data_; }
Jeff Haoa8621002016-10-04 18:13:44 +0000937 EncodedArrayItem* StaticValues() { return static_values_; }
David Sehr7629f602016-08-07 16:01:51 -0700938
939 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
940
941 private:
942 const TypeId* class_type_;
943 uint32_t access_flags_;
Jeff Haoa8621002016-10-04 18:13:44 +0000944 const TypeId* superclass_; // This can be nullptr.
945 TypeList* interfaces_; // This can be nullptr.
946 const StringId* source_file_; // This can be nullptr.
947 AnnotationsDirectoryItem* annotations_; // This can be nullptr.
948 ClassData* class_data_; // This can be nullptr.
949 EncodedArrayItem* static_values_; // This can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -0700950
David Sehr7629f602016-08-07 16:01:51 -0700951 DISALLOW_COPY_AND_ASSIGN(ClassDef);
952};
953
Jeff Haoa8621002016-10-04 18:13:44 +0000954class TypeAddrPair {
David Sehr853a8e12016-09-01 13:03:50 -0700955 public:
Jeff Haoa8621002016-10-04 18:13:44 +0000956 TypeAddrPair(const TypeId* type_id, uint32_t address) : type_id_(type_id), address_(address) { }
David Sehr853a8e12016-09-01 13:03:50 -0700957
958 const TypeId* GetTypeId() const { return type_id_; }
959 uint32_t GetAddress() const { return address_; }
960
961 private:
Jeff Haocc829592017-03-14 16:13:39 -0700962 const TypeId* type_id_; // This can be nullptr.
David Sehr853a8e12016-09-01 13:03:50 -0700963 uint32_t address_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700964
Jeff Haoa8621002016-10-04 18:13:44 +0000965 DISALLOW_COPY_AND_ASSIGN(TypeAddrPair);
966};
967
968using TypeAddrPairVector = std::vector<std::unique_ptr<const TypeAddrPair>>;
969
970class CatchHandler {
971 public:
972 explicit CatchHandler(bool catch_all, uint16_t list_offset, TypeAddrPairVector* handlers)
973 : catch_all_(catch_all), list_offset_(list_offset), handlers_(handlers) { }
974
975 bool HasCatchAll() const { return catch_all_; }
976 uint16_t GetListOffset() const { return list_offset_; }
977 TypeAddrPairVector* GetHandlers() const { return handlers_.get(); }
978
979 private:
980 bool catch_all_;
981 uint16_t list_offset_;
982 std::unique_ptr<TypeAddrPairVector> handlers_;
983
David Sehr853a8e12016-09-01 13:03:50 -0700984 DISALLOW_COPY_AND_ASSIGN(CatchHandler);
985};
986
987using CatchHandlerVector = std::vector<std::unique_ptr<const CatchHandler>>;
988
989class TryItem : public Item {
990 public:
Jeff Haoa8621002016-10-04 18:13:44 +0000991 TryItem(uint32_t start_addr, uint16_t insn_count, const CatchHandler* handlers)
David Sehr853a8e12016-09-01 13:03:50 -0700992 : start_addr_(start_addr), insn_count_(insn_count), handlers_(handlers) { }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100993 ~TryItem() override { }
David Sehr853a8e12016-09-01 13:03:50 -0700994
995 uint32_t StartAddr() const { return start_addr_; }
996 uint16_t InsnCount() const { return insn_count_; }
Jeff Haoa8621002016-10-04 18:13:44 +0000997 const CatchHandler* GetHandlers() const { return handlers_; }
David Sehr853a8e12016-09-01 13:03:50 -0700998
999 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1000
1001 private:
1002 uint32_t start_addr_;
1003 uint16_t insn_count_;
Jeff Haoa8621002016-10-04 18:13:44 +00001004 const CatchHandler* handlers_;
Jeff Hao3ab96b42016-09-09 18:35:01 -07001005
David Sehr853a8e12016-09-01 13:03:50 -07001006 DISALLOW_COPY_AND_ASSIGN(TryItem);
1007};
1008
1009using TryItemVector = std::vector<std::unique_ptr<const TryItem>>;
1010
David Sehrd1e44e22016-10-06 17:09:32 -07001011class CodeFixups {
1012 public:
Vladimir Marko219cb902017-12-07 16:20:39 +00001013 CodeFixups(std::vector<TypeId*> type_ids,
1014 std::vector<StringId*> string_ids,
1015 std::vector<MethodId*> method_ids,
1016 std::vector<FieldId*> field_ids)
1017 : type_ids_(std::move(type_ids)),
1018 string_ids_(std::move(string_ids)),
1019 method_ids_(std::move(method_ids)),
1020 field_ids_(std::move(field_ids)) { }
David Sehrd1e44e22016-10-06 17:09:32 -07001021
Vladimir Marko219cb902017-12-07 16:20:39 +00001022 const std::vector<TypeId*>& TypeIds() const { return type_ids_; }
1023 const std::vector<StringId*>& StringIds() const { return string_ids_; }
1024 const std::vector<MethodId*>& MethodIds() const { return method_ids_; }
1025 const std::vector<FieldId*>& FieldIds() const { return field_ids_; }
David Sehrd1e44e22016-10-06 17:09:32 -07001026
1027 private:
Vladimir Marko219cb902017-12-07 16:20:39 +00001028 std::vector<TypeId*> type_ids_;
1029 std::vector<StringId*> string_ids_;
1030 std::vector<MethodId*> method_ids_;
1031 std::vector<FieldId*> field_ids_;
David Sehrd1e44e22016-10-06 17:09:32 -07001032
1033 DISALLOW_COPY_AND_ASSIGN(CodeFixups);
1034};
1035
David Sehr7629f602016-08-07 16:01:51 -07001036class CodeItem : public Item {
1037 public:
David Sehr853a8e12016-09-01 13:03:50 -07001038 CodeItem(uint16_t registers_size,
1039 uint16_t ins_size,
1040 uint16_t outs_size,
1041 DebugInfoItem* debug_info,
1042 uint32_t insns_size,
1043 uint16_t* insns,
Jeff Haoa8621002016-10-04 18:13:44 +00001044 TryItemVector* tries,
1045 CatchHandlerVector* handlers)
David Sehr853a8e12016-09-01 13:03:50 -07001046 : registers_size_(registers_size),
1047 ins_size_(ins_size),
1048 outs_size_(outs_size),
1049 debug_info_(debug_info),
1050 insns_size_(insns_size),
1051 insns_(insns),
Jeff Haoa8621002016-10-04 18:13:44 +00001052 tries_(tries),
1053 handlers_(handlers) { }
David Sehr853a8e12016-09-01 13:03:50 -07001054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001055 ~CodeItem() override { }
David Sehr7629f602016-08-07 16:01:51 -07001056
1057 uint16_t RegistersSize() const { return registers_size_; }
1058 uint16_t InsSize() const { return ins_size_; }
1059 uint16_t OutsSize() const { return outs_size_; }
David Sehr853a8e12016-09-01 13:03:50 -07001060 uint16_t TriesSize() const { return tries_ == nullptr ? 0 : tries_->size(); }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001061 DebugInfoItem* DebugInfo() const { return debug_info_; }
David Sehr7629f602016-08-07 16:01:51 -07001062 uint32_t InsnsSize() const { return insns_size_; }
1063 uint16_t* Insns() const { return insns_.get(); }
David Sehr853a8e12016-09-01 13:03:50 -07001064 TryItemVector* Tries() const { return tries_.get(); }
Jeff Haoa8621002016-10-04 18:13:44 +00001065 CatchHandlerVector* Handlers() const { return handlers_.get(); }
David Sehr7629f602016-08-07 16:01:51 -07001066
David Sehrd1e44e22016-10-06 17:09:32 -07001067 void SetCodeFixups(CodeFixups* fixups) { fixups_.reset(fixups); }
1068 CodeFixups* GetCodeFixups() const { return fixups_.get(); }
1069
David Sehr7629f602016-08-07 16:01:51 -07001070 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1071
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001072 IterationRange<DexInstructionIterator> Instructions() const {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001073 return MakeIterationRange(DexInstructionIterator(Insns(), 0u),
1074 DexInstructionIterator(Insns(), InsnsSize()));
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001075 }
1076
David Sehr7629f602016-08-07 16:01:51 -07001077 private:
1078 uint16_t registers_size_;
1079 uint16_t ins_size_;
1080 uint16_t outs_size_;
Jeff Haoa8621002016-10-04 18:13:44 +00001081 DebugInfoItem* debug_info_; // This can be nullptr.
David Sehr7629f602016-08-07 16:01:51 -07001082 uint32_t insns_size_;
1083 std::unique_ptr<uint16_t[]> insns_;
Jeff Haoa8621002016-10-04 18:13:44 +00001084 std::unique_ptr<TryItemVector> tries_; // This can be nullptr.
1085 std::unique_ptr<CatchHandlerVector> handlers_; // This can be nullptr.
David Sehrd1e44e22016-10-06 17:09:32 -07001086 std::unique_ptr<CodeFixups> fixups_; // This can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001087
David Sehr7629f602016-08-07 16:01:51 -07001088 DISALLOW_COPY_AND_ASSIGN(CodeItem);
1089};
1090
David Sehr7629f602016-08-07 16:01:51 -07001091class DebugInfoItem : public Item {
1092 public:
Jeff Haoa8621002016-10-04 18:13:44 +00001093 DebugInfoItem(uint32_t debug_info_size, uint8_t* debug_info)
1094 : debug_info_size_(debug_info_size), debug_info_(debug_info) { }
1095
1096 uint32_t GetDebugInfoSize() const { return debug_info_size_; }
1097 uint8_t* GetDebugInfo() const { return debug_info_.get(); }
David Sehr7629f602016-08-07 16:01:51 -07001098
David Sehr7629f602016-08-07 16:01:51 -07001099 private:
Jeff Haoa8621002016-10-04 18:13:44 +00001100 uint32_t debug_info_size_;
1101 std::unique_ptr<uint8_t[]> debug_info_;
1102
David Sehr7629f602016-08-07 16:01:51 -07001103 DISALLOW_COPY_AND_ASSIGN(DebugInfoItem);
1104};
1105
Jeff Hao3ab96b42016-09-09 18:35:01 -07001106class AnnotationItem : public Item {
David Sehr853a8e12016-09-01 13:03:50 -07001107 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -07001108 AnnotationItem(uint8_t visibility, EncodedAnnotation* annotation)
1109 : visibility_(visibility), annotation_(annotation) { }
David Sehr853a8e12016-09-01 13:03:50 -07001110
1111 uint8_t GetVisibility() const { return visibility_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001112 EncodedAnnotation* GetAnnotation() const { return annotation_.get(); }
David Sehr7629f602016-08-07 16:01:51 -07001113
1114 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1115
1116 private:
Jeff Hao3ab96b42016-09-09 18:35:01 -07001117 uint8_t visibility_;
1118 std::unique_ptr<EncodedAnnotation> annotation_;
1119
1120 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
1121};
1122
1123class AnnotationSetItem : public Item {
1124 public:
1125 explicit AnnotationSetItem(std::vector<AnnotationItem*>* items) : items_(items) {
1126 size_ = sizeof(uint32_t) + items->size() * sizeof(uint32_t);
1127 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001128 ~AnnotationSetItem() override { }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001129
1130 std::vector<AnnotationItem*>* GetItems() { return items_.get(); }
1131
1132 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1133
1134 private:
1135 std::unique_ptr<std::vector<AnnotationItem*>> items_;
1136
David Sehr7629f602016-08-07 16:01:51 -07001137 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
1138};
1139
Jeff Hao3ab96b42016-09-09 18:35:01 -07001140class AnnotationSetRefList : public Item {
1141 public:
1142 explicit AnnotationSetRefList(std::vector<AnnotationSetItem*>* items) : items_(items) {
1143 size_ = sizeof(uint32_t) + items->size() * sizeof(uint32_t);
1144 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001145 ~AnnotationSetRefList() override { }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001146
1147 std::vector<AnnotationSetItem*>* GetItems() { return items_.get(); }
1148
1149 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1150
1151 private:
Jeff Haoa8621002016-10-04 18:13:44 +00001152 std::unique_ptr<std::vector<AnnotationSetItem*>> items_; // Elements of vector can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001153
1154 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
1155};
David Sehr853a8e12016-09-01 13:03:50 -07001156
1157class FieldAnnotation {
1158 public:
1159 FieldAnnotation(FieldId* field_id, AnnotationSetItem* annotation_set_item)
1160 : field_id_(field_id), annotation_set_item_(annotation_set_item) { }
1161
1162 FieldId* GetFieldId() const { return field_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001163 AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_; }
David Sehr853a8e12016-09-01 13:03:50 -07001164
1165 private:
1166 FieldId* field_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -07001167 AnnotationSetItem* annotation_set_item_;
1168
David Sehr853a8e12016-09-01 13:03:50 -07001169 DISALLOW_COPY_AND_ASSIGN(FieldAnnotation);
1170};
1171
1172using FieldAnnotationVector = std::vector<std::unique_ptr<FieldAnnotation>>;
1173
1174class MethodAnnotation {
1175 public:
1176 MethodAnnotation(MethodId* method_id, AnnotationSetItem* annotation_set_item)
1177 : method_id_(method_id), annotation_set_item_(annotation_set_item) { }
1178
1179 MethodId* GetMethodId() const { return method_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001180 AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_; }
David Sehr853a8e12016-09-01 13:03:50 -07001181
1182 private:
1183 MethodId* method_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -07001184 AnnotationSetItem* annotation_set_item_;
1185
David Sehr853a8e12016-09-01 13:03:50 -07001186 DISALLOW_COPY_AND_ASSIGN(MethodAnnotation);
1187};
1188
1189using MethodAnnotationVector = std::vector<std::unique_ptr<MethodAnnotation>>;
1190
1191class ParameterAnnotation {
1192 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -07001193 ParameterAnnotation(MethodId* method_id, AnnotationSetRefList* annotations)
David Sehr853a8e12016-09-01 13:03:50 -07001194 : method_id_(method_id), annotations_(annotations) { }
1195
1196 MethodId* GetMethodId() const { return method_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001197 AnnotationSetRefList* GetAnnotations() { return annotations_; }
David Sehr853a8e12016-09-01 13:03:50 -07001198
1199 private:
1200 MethodId* method_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -07001201 AnnotationSetRefList* annotations_;
1202
David Sehr853a8e12016-09-01 13:03:50 -07001203 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotation);
1204};
1205
1206using ParameterAnnotationVector = std::vector<std::unique_ptr<ParameterAnnotation>>;
1207
David Sehr7629f602016-08-07 16:01:51 -07001208class AnnotationsDirectoryItem : public Item {
1209 public:
David Sehr853a8e12016-09-01 13:03:50 -07001210 AnnotationsDirectoryItem(AnnotationSetItem* class_annotation,
1211 FieldAnnotationVector* field_annotations,
1212 MethodAnnotationVector* method_annotations,
1213 ParameterAnnotationVector* parameter_annotations)
1214 : class_annotation_(class_annotation),
1215 field_annotations_(field_annotations),
1216 method_annotations_(method_annotations),
1217 parameter_annotations_(parameter_annotations) { }
David Sehr7629f602016-08-07 16:01:51 -07001218
Jeff Hao3ab96b42016-09-09 18:35:01 -07001219 AnnotationSetItem* GetClassAnnotation() const { return class_annotation_; }
David Sehr853a8e12016-09-01 13:03:50 -07001220 FieldAnnotationVector* GetFieldAnnotations() { return field_annotations_.get(); }
1221 MethodAnnotationVector* GetMethodAnnotations() { return method_annotations_.get(); }
1222 ParameterAnnotationVector* GetParameterAnnotations() { return parameter_annotations_.get(); }
David Sehr7629f602016-08-07 16:01:51 -07001223
1224 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1225
1226 private:
Jeff Haoa8621002016-10-04 18:13:44 +00001227 AnnotationSetItem* class_annotation_; // This can be nullptr.
1228 std::unique_ptr<FieldAnnotationVector> field_annotations_; // This can be nullptr.
1229 std::unique_ptr<MethodAnnotationVector> method_annotations_; // This can be nullptr.
1230 std::unique_ptr<ParameterAnnotationVector> parameter_annotations_; // This can be nullptr.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001231
David Sehr7629f602016-08-07 16:01:51 -07001232 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
1233};
1234
Jeff Hao5daee902017-04-27 18:00:38 -07001235class CallSiteId : public IndexedItem {
1236 public:
1237 explicit CallSiteId(EncodedArrayItem* call_site_item) : call_site_item_(call_site_item) {
1238 size_ = kCallSiteIdItemSize;
1239 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001240 ~CallSiteId() override { }
Jeff Hao5daee902017-04-27 18:00:38 -07001241
1242 static size_t ItemSize() { return kCallSiteIdItemSize; }
1243
1244 EncodedArrayItem* CallSiteItem() const { return call_site_item_; }
1245
1246 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
1247
1248 private:
1249 EncodedArrayItem* call_site_item_;
1250
1251 DISALLOW_COPY_AND_ASSIGN(CallSiteId);
1252};
1253
1254class MethodHandleItem : public IndexedItem {
1255 public:
1256 MethodHandleItem(DexFile::MethodHandleType method_handle_type, IndexedItem* field_or_method_id)
1257 : method_handle_type_(method_handle_type),
1258 field_or_method_id_(field_or_method_id) {
1259 size_ = kMethodHandleItemSize;
1260 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001261 ~MethodHandleItem() override { }
Jeff Hao5daee902017-04-27 18:00:38 -07001262
1263 static size_t ItemSize() { return kMethodHandleItemSize; }
1264
1265 DexFile::MethodHandleType GetMethodHandleType() const { return method_handle_type_; }
1266 IndexedItem* GetFieldOrMethodId() const { return field_or_method_id_; }
1267
1268 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
1269
1270 private:
1271 DexFile::MethodHandleType method_handle_type_;
1272 IndexedItem* field_or_method_id_;
1273
1274 DISALLOW_COPY_AND_ASSIGN(MethodHandleItem);
1275};
1276
David Brazdil20c765f2018-10-27 21:45:15 +00001277using HiddenapiFlagsMap = SafeMap<const Item*, uint32_t>;
1278
1279class HiddenapiClassData : public IndexedItem {
1280 public:
1281 HiddenapiClassData(const ClassDef* class_def, std::unique_ptr<HiddenapiFlagsMap> flags)
1282 : class_def_(class_def), flags_(std::move(flags)) { }
1283 ~HiddenapiClassData() override { }
1284
1285 const ClassDef* GetClassDef() const { return class_def_; }
1286
1287 uint32_t GetFlags(const Item* field_or_method_item) const {
1288 return (flags_ == nullptr) ? 0u : flags_->Get(field_or_method_item);
1289 }
1290
1291 static uint32_t GetFlags(Header* header, ClassDef* class_def, const Item* field_or_method_item) {
1292 DCHECK(header != nullptr);
1293 DCHECK(class_def != nullptr);
1294 return (header->HiddenapiClassDatas().Empty())
1295 ? 0u
1296 : header->HiddenapiClassDatas()[class_def->GetIndex()]->GetFlags(field_or_method_item);
1297 }
1298
1299 uint32_t ItemSize() const {
1300 uint32_t size = 0u;
1301 bool has_non_zero_entries = false;
1302 if (flags_ != nullptr) {
1303 for (const auto& entry : *flags_) {
1304 size += UnsignedLeb128Size(entry.second);
1305 has_non_zero_entries |= (entry.second != 0u);
1306 }
1307 }
1308 return has_non_zero_entries ? size : 0u;
1309 }
1310
1311 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1312
1313 private:
1314 const ClassDef* class_def_;
1315 std::unique_ptr<HiddenapiFlagsMap> flags_;
1316
1317 DISALLOW_COPY_AND_ASSIGN(HiddenapiClassData);
1318};
1319
David Sehr7629f602016-08-07 16:01:51 -07001320// TODO(sehr): implement MapList.
1321class MapList : public Item {
1322 public:
1323 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1324
1325 private:
1326 DISALLOW_COPY_AND_ASSIGN(MapList);
1327};
1328
1329class MapItem : public Item {
1330 public:
1331 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
1332
1333 private:
1334 DISALLOW_COPY_AND_ASSIGN(MapItem);
1335};
1336
David Sehr9037a3a2017-03-30 17:50:24 -07001337// Interface for building a vector of file sections for use by other clients.
1338struct DexFileSection {
1339 public:
1340 DexFileSection(const std::string& name, uint16_t type, uint32_t size, uint32_t offset)
1341 : name(name), type(type), size(size), offset(offset) { }
1342 std::string name;
1343 // The type (DexFile::MapItemType).
1344 uint16_t type;
1345 // The size (in elements, not bytes).
1346 uint32_t size;
1347 // The byte offset from the start of the file.
1348 uint32_t offset;
1349};
1350
1351enum class SortDirection {
1352 kSortAscending,
1353 kSortDescending
1354};
1355
1356std::vector<DexFileSection> GetSortedDexFileSections(dex_ir::Header* header,
1357 SortDirection direction);
1358
David Sehr7629f602016-08-07 16:01:51 -07001359} // namespace dex_ir
1360} // namespace art
1361
1362#endif // ART_DEXLAYOUT_DEX_IR_H_