blob: 6ae9f1c938518bfef5f7fa8ced3818e5e709847a [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
David Sehr7629f602016-08-07 16:01:51 -070022#include <vector>
23#include <stdint.h>
24
David Sehr853a8e12016-09-01 13:03:50 -070025#include "dex_file-inl.h"
Jeff Hao3ab96b42016-09-09 18:35:01 -070026#include "leb128.h"
David Sehr7629f602016-08-07 16:01:51 -070027
28namespace art {
29namespace dex_ir {
30
31// Forward declarations for classes used in containers or pointed to.
Jeff Hao3ab96b42016-09-09 18:35:01 -070032class AnnotationItem;
David Sehr7629f602016-08-07 16:01:51 -070033class AnnotationsDirectoryItem;
34class AnnotationSetItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070035class AnnotationSetRefList;
David Sehr7629f602016-08-07 16:01:51 -070036class ClassData;
37class ClassDef;
38class CodeItem;
39class DebugInfoItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070040class EncodedAnnotation;
41class EncodedArrayItem;
42class EncodedValue;
David Sehr7629f602016-08-07 16:01:51 -070043class FieldId;
44class FieldItem;
45class Header;
46class MapList;
47class MapItem;
48class MethodId;
49class MethodItem;
Jeff Hao3ab96b42016-09-09 18:35:01 -070050class ParameterAnnotation;
David Sehr7629f602016-08-07 16:01:51 -070051class ProtoId;
Jeff Hao3ab96b42016-09-09 18:35:01 -070052class StringData;
David Sehr7629f602016-08-07 16:01:51 -070053class StringId;
54class TryItem;
55class TypeId;
Jeff Hao3ab96b42016-09-09 18:35:01 -070056class TypeList;
57
58// Item size constants.
59static constexpr size_t kHeaderItemSize = 112;
60static constexpr size_t kStringIdItemSize = 4;
61static constexpr size_t kTypeIdItemSize = 4;
62static constexpr size_t kProtoIdItemSize = 12;
63static constexpr size_t kFieldIdItemSize = 8;
64static constexpr size_t kMethodIdItemSize = 8;
65static constexpr size_t kClassDefItemSize = 32;
David Sehr7629f602016-08-07 16:01:51 -070066
67// Visitor support
68class AbstractDispatcher {
69 public:
70 AbstractDispatcher() = default;
71 virtual ~AbstractDispatcher() { }
72
73 virtual void Dispatch(Header* header) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070074 virtual void Dispatch(const StringData* string_data) = 0;
David Sehr7629f602016-08-07 16:01:51 -070075 virtual void Dispatch(const StringId* string_id) = 0;
76 virtual void Dispatch(const TypeId* type_id) = 0;
77 virtual void Dispatch(const ProtoId* proto_id) = 0;
78 virtual void Dispatch(const FieldId* field_id) = 0;
79 virtual void Dispatch(const MethodId* method_id) = 0;
80 virtual void Dispatch(ClassData* class_data) = 0;
81 virtual void Dispatch(ClassDef* class_def) = 0;
82 virtual void Dispatch(FieldItem* field_item) = 0;
83 virtual void Dispatch(MethodItem* method_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070084 virtual void Dispatch(EncodedArrayItem* array_item) = 0;
David Sehr7629f602016-08-07 16:01:51 -070085 virtual void Dispatch(CodeItem* code_item) = 0;
86 virtual void Dispatch(TryItem* try_item) = 0;
87 virtual void Dispatch(DebugInfoItem* debug_info_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070088 virtual void Dispatch(AnnotationItem* annotation_item) = 0;
David Sehr7629f602016-08-07 16:01:51 -070089 virtual void Dispatch(AnnotationSetItem* annotation_set_item) = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -070090 virtual void Dispatch(AnnotationSetRefList* annotation_set_ref_list) = 0;
David Sehr7629f602016-08-07 16:01:51 -070091 virtual void Dispatch(AnnotationsDirectoryItem* annotations_directory_item) = 0;
92 virtual void Dispatch(MapList* map_list) = 0;
93 virtual void Dispatch(MapItem* map_item) = 0;
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher);
97};
98
99// Collections become owners of the objects added by moving them into unique pointers.
100template<class T> class CollectionWithOffset {
101 public:
102 CollectionWithOffset() = default;
103 std::vector<std::unique_ptr<T>>& Collection() { return collection_; }
104 // Read-time support methods
Jeff Hao3ab96b42016-09-09 18:35:01 -0700105 void AddItem(T* object, uint32_t offset) {
106 object->SetOffset(offset);
David Sehr7629f602016-08-07 16:01:51 -0700107 collection_.push_back(std::unique_ptr<T>(object));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700108 }
109 void AddIndexedItem(T* object, uint32_t offset, uint32_t index) {
110 object->SetOffset(offset);
111 object->SetIndex(index);
112 collection_.push_back(std::unique_ptr<T>(object));
David Sehr7629f602016-08-07 16:01:51 -0700113 }
114 // Ordinary object insertion into collection.
115 void Insert(T object ATTRIBUTE_UNUSED) {
116 // TODO(sehr): add ordered insertion support.
117 UNIMPLEMENTED(FATAL) << "Insertion not ready";
118 }
119 uint32_t GetOffset() const { return offset_; }
120 void SetOffset(uint32_t new_offset) { offset_ = new_offset; }
121 uint32_t Size() const { return collection_.size(); }
122
123 private:
124 std::vector<std::unique_ptr<T>> collection_;
125 uint32_t offset_ = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700126
David Sehr7629f602016-08-07 16:01:51 -0700127 DISALLOW_COPY_AND_ASSIGN(CollectionWithOffset);
128};
129
Jeff Hao3ab96b42016-09-09 18:35:01 -0700130class Collections {
131 public:
132 Collections() = default;
133
134 std::vector<std::unique_ptr<StringId>>& StringIds() { return string_ids_.Collection(); }
135 std::vector<std::unique_ptr<TypeId>>& TypeIds() { return type_ids_.Collection(); }
136 std::vector<std::unique_ptr<ProtoId>>& ProtoIds() { return proto_ids_.Collection(); }
137 std::vector<std::unique_ptr<FieldId>>& FieldIds() { return field_ids_.Collection(); }
138 std::vector<std::unique_ptr<MethodId>>& MethodIds() { return method_ids_.Collection(); }
139 std::vector<std::unique_ptr<ClassDef>>& ClassDefs() { return class_defs_.Collection(); }
140
141 std::vector<std::unique_ptr<TypeList>>& TypeLists() { return type_lists_.Collection(); }
142 std::vector<std::unique_ptr<EncodedArrayItem>>& EncodedArrayItems()
143 { return encoded_array_items_.Collection(); }
144
145 void CreateStringId(const DexFile& dex_file, uint32_t i);
146 void CreateTypeId(const DexFile& dex_file, uint32_t i);
147 void CreateProtoId(const DexFile& dex_file, uint32_t i);
148 void CreateFieldId(const DexFile& dex_file, uint32_t i);
149 void CreateMethodId(const DexFile& dex_file, uint32_t i);
150 void CreateClassDef(const DexFile& dex_file, uint32_t i);
151
152 TypeList* CreateTypeList(const DexFile::TypeList* type_list, uint32_t offset, bool allow_empty);
153 EncodedArrayItem* CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset);
154 AnnotationItem* CreateAnnotationItem(const DexFile::AnnotationItem* annotation, uint32_t offset);
155 AnnotationSetItem* CreateAnnotationSetItem(const DexFile& dex_file,
156 const DexFile::AnnotationSetItem& disk_annotations_item, uint32_t offset);
157 AnnotationsDirectoryItem* CreateAnnotationsDirectoryItem(const DexFile& dex_file,
158 const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset);
159 CodeItem* CreateCodeItem(
160 const DexFile& dex_file, const DexFile::CodeItem& disk_code_item, uint32_t offset);
161 ClassData* CreateClassData(const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset);
162
163 StringId* GetStringId(uint32_t index) { return StringIds()[index].get(); }
164 TypeId* GetTypeId(uint32_t index) { return TypeIds()[index].get(); }
165 ProtoId* GetProtoId(uint32_t index) { return ProtoIds()[index].get(); }
166 FieldId* GetFieldId(uint32_t index) { return FieldIds()[index].get(); }
167 MethodId* GetMethodId(uint32_t index) { return MethodIds()[index].get(); }
168 ClassDef* GetClassDef(uint32_t index) { return ClassDefs()[index].get(); }
169
170 StringId* GetStringIdOrNullPtr(uint32_t index) {
171 return index == DexFile::kDexNoIndex ? nullptr : GetStringId(index);
172 }
173 TypeId* GetTypeIdOrNullPtr(uint16_t index) {
174 return index == DexFile::kDexNoIndex16 ? nullptr : GetTypeId(index);
175 }
176
177 uint32_t StringIdsOffset() const { return string_ids_.GetOffset(); }
178 uint32_t TypeIdsOffset() const { return type_ids_.GetOffset(); }
179 uint32_t ProtoIdsOffset() const { return proto_ids_.GetOffset(); }
180 uint32_t FieldIdsOffset() const { return field_ids_.GetOffset(); }
181 uint32_t MethodIdsOffset() const { return method_ids_.GetOffset(); }
182 uint32_t ClassDefsOffset() const { return class_defs_.GetOffset(); }
183 uint32_t StringDatasOffset() const { return string_datas_.GetOffset(); }
184 uint32_t TypeListsOffset() const { return type_lists_.GetOffset(); }
185 uint32_t EncodedArrayOffset() const { return encoded_array_items_.GetOffset(); }
186 uint32_t AnnotationOffset() const { return annotation_items_.GetOffset(); }
187 uint32_t AnnotationSetOffset() const { return annotation_set_items_.GetOffset(); }
188 uint32_t AnnotationSetRefListsOffset() const { return annotation_set_ref_lists_.GetOffset(); }
189 uint32_t AnnotationsDirectoryOffset() const { return annotations_directory_items_.GetOffset(); }
190 uint32_t DebugInfoOffset() const { return debug_info_items_.GetOffset(); }
191 uint32_t CodeItemsOffset() const { return code_items_.GetOffset(); }
192 uint32_t ClassDatasOffset() const { return class_datas_.GetOffset(); }
193
194 void SetStringIdsOffset(uint32_t new_offset) { string_ids_.SetOffset(new_offset); }
195 void SetTypeIdsOffset(uint32_t new_offset) { type_ids_.SetOffset(new_offset); }
196 void SetProtoIdsOffset(uint32_t new_offset) { proto_ids_.SetOffset(new_offset); }
197 void SetFieldIdsOffset(uint32_t new_offset) { field_ids_.SetOffset(new_offset); }
198 void SetMethodIdsOffset(uint32_t new_offset) { method_ids_.SetOffset(new_offset); }
199 void SetClassDefsOffset(uint32_t new_offset) { class_defs_.SetOffset(new_offset); }
200 void SetStringDatasOffset(uint32_t new_offset) { string_datas_.SetOffset(new_offset); }
201 void SetTypeListsOffset(uint32_t new_offset) { type_lists_.SetOffset(new_offset); }
202 void SetEncodedArrayOffset(uint32_t new_offset) { encoded_array_items_.SetOffset(new_offset); }
203 void SetAnnotationOffset(uint32_t new_offset) { annotation_items_.SetOffset(new_offset); }
204 void SetAnnotationSetOffset(uint32_t new_offset) { annotation_set_items_.SetOffset(new_offset); }
205 void SetAnnotationSetRefListsOffset(uint32_t new_offset)
206 { annotation_set_ref_lists_.SetOffset(new_offset); }
207 void SetAnnotationsDirectoryOffset(uint32_t new_offset)
208 { annotations_directory_items_.SetOffset(new_offset); }
209 void SetDebugInfoOffset(uint32_t new_offset) { debug_info_items_.SetOffset(new_offset); }
210 void SetCodeItemsOffset(uint32_t new_offset) { code_items_.SetOffset(new_offset); }
211 void SetClassDatasOffset(uint32_t new_offset) { class_datas_.SetOffset(new_offset); }
212
213 uint32_t StringIdsSize() const { return string_ids_.Size(); }
214 uint32_t TypeIdsSize() const { return type_ids_.Size(); }
215 uint32_t ProtoIdsSize() const { return proto_ids_.Size(); }
216 uint32_t FieldIdsSize() const { return field_ids_.Size(); }
217 uint32_t MethodIdsSize() const { return method_ids_.Size(); }
218 uint32_t ClassDefsSize() const { return class_defs_.Size(); }
219
220 private:
221 EncodedValue* ReadEncodedValue(const uint8_t** data);
222 EncodedValue* ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length);
223 void ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item);
224
225 ParameterAnnotation* GenerateParameterAnnotation(const DexFile& dex_file, MethodId* method_id,
226 const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset);
227 MethodItem* GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii);
228
229 CollectionWithOffset<StringId> string_ids_;
230 CollectionWithOffset<TypeId> type_ids_;
231 CollectionWithOffset<ProtoId> proto_ids_;
232 CollectionWithOffset<FieldId> field_ids_;
233 CollectionWithOffset<MethodId> method_ids_;
234 CollectionWithOffset<ClassDef> class_defs_;
235
236 CollectionWithOffset<StringData> string_datas_;
237 CollectionWithOffset<TypeList> type_lists_;
238 CollectionWithOffset<EncodedArrayItem> encoded_array_items_;
239 CollectionWithOffset<AnnotationItem> annotation_items_;
240 CollectionWithOffset<AnnotationSetItem> annotation_set_items_;
241 CollectionWithOffset<AnnotationSetRefList> annotation_set_ref_lists_;
242 CollectionWithOffset<AnnotationsDirectoryItem> annotations_directory_items_;
243 CollectionWithOffset<DebugInfoItem> debug_info_items_;
244 CollectionWithOffset<CodeItem> code_items_;
245 CollectionWithOffset<ClassData> class_datas_;
246
247 DISALLOW_COPY_AND_ASSIGN(Collections);
248};
249
David Sehr7629f602016-08-07 16:01:51 -0700250class Item {
251 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700252 Item() { }
David Sehr7629f602016-08-07 16:01:51 -0700253 virtual ~Item() { }
David Sehr853a8e12016-09-01 13:03:50 -0700254
David Sehr7629f602016-08-07 16:01:51 -0700255 uint32_t GetOffset() const { return offset_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700256 uint32_t GetSize() const { return size_; }
David Sehr7629f602016-08-07 16:01:51 -0700257 void SetOffset(uint32_t offset) { offset_ = offset; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700258 void SetSize(uint32_t size) { size_ = size; }
David Sehr853a8e12016-09-01 13:03:50 -0700259
David Sehr7629f602016-08-07 16:01:51 -0700260 protected:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700261 Item(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
262
David Sehr7629f602016-08-07 16:01:51 -0700263 uint32_t offset_ = 0;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700264 uint32_t size_ = 0;
265};
266
267class IndexedItem : public Item {
268 public:
269 IndexedItem() { }
270 virtual ~IndexedItem() { }
271
272 uint32_t GetIndex() const { return index_; }
273 void SetIndex(uint32_t index) { index_ = index; }
274
275 protected:
276 IndexedItem(uint32_t offset, uint32_t size, uint32_t index)
277 : Item(offset, size), index_(index) { }
278
279 uint32_t index_ = 0;
David Sehr7629f602016-08-07 16:01:51 -0700280};
281
282class Header : public Item {
283 public:
David Sehr853a8e12016-09-01 13:03:50 -0700284 Header(const uint8_t* magic,
285 uint32_t checksum,
286 const uint8_t* signature,
287 uint32_t endian_tag,
288 uint32_t file_size,
289 uint32_t header_size,
290 uint32_t link_size,
291 uint32_t link_offset,
292 uint32_t data_size,
293 uint32_t data_offset)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700294 : Item(0, kHeaderItemSize),
295 checksum_(checksum),
David Sehr853a8e12016-09-01 13:03:50 -0700296 endian_tag_(endian_tag),
297 file_size_(file_size),
298 header_size_(header_size),
299 link_size_(link_size),
300 link_offset_(link_offset),
301 data_size_(data_size),
302 data_offset_(data_offset) {
303 memcpy(magic_, magic, sizeof(magic_));
304 memcpy(signature_, signature, sizeof(signature_));
305 }
David Sehr7629f602016-08-07 16:01:51 -0700306 ~Header() OVERRIDE { }
307
Jeff Hao3ab96b42016-09-09 18:35:01 -0700308 static size_t ItemSize() { return kHeaderItemSize; }
309
David Sehr7629f602016-08-07 16:01:51 -0700310 const uint8_t* Magic() const { return magic_; }
311 uint32_t Checksum() const { return checksum_; }
312 const uint8_t* Signature() const { return signature_; }
313 uint32_t EndianTag() const { return endian_tag_; }
314 uint32_t FileSize() const { return file_size_; }
315 uint32_t HeaderSize() const { return header_size_; }
316 uint32_t LinkSize() const { return link_size_; }
317 uint32_t LinkOffset() const { return link_offset_; }
318 uint32_t DataSize() const { return data_size_; }
319 uint32_t DataOffset() const { return data_offset_; }
320
321 void SetChecksum(uint32_t new_checksum) { checksum_ = new_checksum; }
322 void SetSignature(const uint8_t* new_signature) {
323 memcpy(signature_, new_signature, sizeof(signature_));
324 }
325 void SetFileSize(uint32_t new_file_size) { file_size_ = new_file_size; }
326 void SetHeaderSize(uint32_t new_header_size) { header_size_ = new_header_size; }
327 void SetLinkSize(uint32_t new_link_size) { link_size_ = new_link_size; }
328 void SetLinkOffset(uint32_t new_link_offset) { link_offset_ = new_link_offset; }
329 void SetDataSize(uint32_t new_data_size) { data_size_ = new_data_size; }
330 void SetDataOffset(uint32_t new_data_offset) { data_offset_ = new_data_offset; }
331
Jeff Hao3ab96b42016-09-09 18:35:01 -0700332 Collections& GetCollections() { return collections_; }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700333
David Sehr7629f602016-08-07 16:01:51 -0700334 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
335
336 private:
David Sehr7629f602016-08-07 16:01:51 -0700337 uint8_t magic_[8];
338 uint32_t checksum_;
339 uint8_t signature_[DexFile::kSha1DigestSize];
340 uint32_t endian_tag_;
341 uint32_t file_size_;
342 uint32_t header_size_;
343 uint32_t link_size_;
344 uint32_t link_offset_;
345 uint32_t data_size_;
346 uint32_t data_offset_;
347
Jeff Hao3ab96b42016-09-09 18:35:01 -0700348 Collections collections_;
349
David Sehr7629f602016-08-07 16:01:51 -0700350 DISALLOW_COPY_AND_ASSIGN(Header);
351};
352
Jeff Hao3ab96b42016-09-09 18:35:01 -0700353class StringData : public Item {
David Sehr7629f602016-08-07 16:01:51 -0700354 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700355 explicit StringData(const char* data) : data_(strdup(data)) {
356 size_ = UnsignedLeb128Size(strlen(data)) + strlen(data);
357 }
David Sehr7629f602016-08-07 16:01:51 -0700358
359 const char* Data() const { return data_.get(); }
360
361 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
362
363 private:
364 std::unique_ptr<const char> data_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700365
366 DISALLOW_COPY_AND_ASSIGN(StringData);
367};
368
369class StringId : public IndexedItem {
370 public:
371 explicit StringId(StringData* string_data) : string_data_(string_data) {
372 size_ = kStringIdItemSize;
373 }
374 ~StringId() OVERRIDE { }
375
376 static size_t ItemSize() { return kStringIdItemSize; }
377
378 const char* Data() const { return string_data_->Data(); }
379 StringData* DataItem() const { return string_data_; }
380
381 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
382
383 private:
384 StringData* string_data_;
385
David Sehr7629f602016-08-07 16:01:51 -0700386 DISALLOW_COPY_AND_ASSIGN(StringId);
387};
388
Jeff Hao3ab96b42016-09-09 18:35:01 -0700389class TypeId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700390 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700391 explicit TypeId(StringId* string_id) : string_id_(string_id) { size_ = kTypeIdItemSize; }
David Sehr7629f602016-08-07 16:01:51 -0700392 ~TypeId() OVERRIDE { }
393
Jeff Hao3ab96b42016-09-09 18:35:01 -0700394 static size_t ItemSize() { return kTypeIdItemSize; }
395
David Sehr7629f602016-08-07 16:01:51 -0700396 StringId* GetStringId() const { return string_id_; }
397
398 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
399
400 private:
401 StringId* string_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700402
David Sehr7629f602016-08-07 16:01:51 -0700403 DISALLOW_COPY_AND_ASSIGN(TypeId);
404};
405
David Sehr853a8e12016-09-01 13:03:50 -0700406using TypeIdVector = std::vector<const TypeId*>;
407
Jeff Hao3ab96b42016-09-09 18:35:01 -0700408class TypeList : public Item {
David Sehr7629f602016-08-07 16:01:51 -0700409 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700410 explicit TypeList(TypeIdVector* type_list) : type_list_(type_list) {
411 size_ = sizeof(uint32_t) + (type_list->size() * sizeof(uint16_t));
412 }
413 ~TypeList() OVERRIDE { }
414
415 const TypeIdVector* GetTypeList() const { return type_list_.get(); }
416
417 private:
418 std::unique_ptr<TypeIdVector> type_list_;
419
420 DISALLOW_COPY_AND_ASSIGN(TypeList);
421};
422
423class ProtoId : public IndexedItem {
424 public:
425 ProtoId(const StringId* shorty, const TypeId* return_type, TypeList* parameters)
426 : shorty_(shorty), return_type_(return_type), parameters_(parameters)
427 { size_ = kProtoIdItemSize; }
David Sehr7629f602016-08-07 16:01:51 -0700428 ~ProtoId() OVERRIDE { }
429
Jeff Hao3ab96b42016-09-09 18:35:01 -0700430 static size_t ItemSize() { return kProtoIdItemSize; }
431
David Sehr7629f602016-08-07 16:01:51 -0700432 const StringId* Shorty() const { return shorty_; }
433 const TypeId* ReturnType() const { return return_type_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700434 const TypeIdVector& Parameters() const { return *parameters_->GetTypeList(); }
David Sehr7629f602016-08-07 16:01:51 -0700435
436 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
437
438 private:
439 const StringId* shorty_;
440 const TypeId* return_type_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700441 TypeList* parameters_;
442
David Sehr7629f602016-08-07 16:01:51 -0700443 DISALLOW_COPY_AND_ASSIGN(ProtoId);
444};
445
Jeff Hao3ab96b42016-09-09 18:35:01 -0700446class FieldId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700447 public:
David Sehr853a8e12016-09-01 13:03:50 -0700448 FieldId(const TypeId* klass, const TypeId* type, const StringId* name)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700449 : class_(klass), type_(type), name_(name) { size_ = kFieldIdItemSize; }
David Sehr7629f602016-08-07 16:01:51 -0700450 ~FieldId() OVERRIDE { }
451
Jeff Hao3ab96b42016-09-09 18:35:01 -0700452 static size_t ItemSize() { return kFieldIdItemSize; }
453
David Sehr7629f602016-08-07 16:01:51 -0700454 const TypeId* Class() const { return class_; }
455 const TypeId* Type() const { return type_; }
456 const StringId* Name() const { return name_; }
457
458 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
459
460 private:
461 const TypeId* class_;
462 const TypeId* type_;
463 const StringId* name_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700464
David Sehr7629f602016-08-07 16:01:51 -0700465 DISALLOW_COPY_AND_ASSIGN(FieldId);
466};
467
Jeff Hao3ab96b42016-09-09 18:35:01 -0700468class MethodId : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700469 public:
David Sehr853a8e12016-09-01 13:03:50 -0700470 MethodId(const TypeId* klass, const ProtoId* proto, const StringId* name)
Jeff Hao3ab96b42016-09-09 18:35:01 -0700471 : class_(klass), proto_(proto), name_(name) { size_ = kMethodIdItemSize; }
David Sehr7629f602016-08-07 16:01:51 -0700472 ~MethodId() OVERRIDE { }
473
Jeff Hao3ab96b42016-09-09 18:35:01 -0700474 static size_t ItemSize() { return kMethodIdItemSize; }
475
David Sehr7629f602016-08-07 16:01:51 -0700476 const TypeId* Class() const { return class_; }
477 const ProtoId* Proto() const { return proto_; }
478 const StringId* Name() const { return name_; }
479
480 void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
481
482 private:
483 const TypeId* class_;
484 const ProtoId* proto_;
485 const StringId* name_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700486
David Sehr7629f602016-08-07 16:01:51 -0700487 DISALLOW_COPY_AND_ASSIGN(MethodId);
488};
489
490class FieldItem : public Item {
491 public:
David Sehr853a8e12016-09-01 13:03:50 -0700492 FieldItem(uint32_t access_flags, const FieldId* field_id)
493 : access_flags_(access_flags), field_id_(field_id) { }
David Sehr7629f602016-08-07 16:01:51 -0700494 ~FieldItem() OVERRIDE { }
495
496 uint32_t GetAccessFlags() const { return access_flags_; }
497 const FieldId* GetFieldId() const { return field_id_; }
498
499 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
500
501 private:
502 uint32_t access_flags_;
503 const FieldId* field_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700504
David Sehr7629f602016-08-07 16:01:51 -0700505 DISALLOW_COPY_AND_ASSIGN(FieldItem);
506};
507
David Sehr853a8e12016-09-01 13:03:50 -0700508using FieldItemVector = std::vector<std::unique_ptr<FieldItem>>;
509
David Sehr7629f602016-08-07 16:01:51 -0700510class MethodItem : public Item {
511 public:
David Sehr853a8e12016-09-01 13:03:50 -0700512 MethodItem(uint32_t access_flags, const MethodId* method_id, const CodeItem* code)
513 : access_flags_(access_flags), method_id_(method_id), code_(code) { }
David Sehr7629f602016-08-07 16:01:51 -0700514 ~MethodItem() OVERRIDE { }
515
516 uint32_t GetAccessFlags() const { return access_flags_; }
517 const MethodId* GetMethodId() const { return method_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700518 const CodeItem* GetCodeItem() const { return code_; }
David Sehr7629f602016-08-07 16:01:51 -0700519
520 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
521
522 private:
523 uint32_t access_flags_;
524 const MethodId* method_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700525 const CodeItem* code_;
526
David Sehr7629f602016-08-07 16:01:51 -0700527 DISALLOW_COPY_AND_ASSIGN(MethodItem);
528};
529
David Sehr853a8e12016-09-01 13:03:50 -0700530using MethodItemVector = std::vector<std::unique_ptr<MethodItem>>;
531
Jeff Hao3ab96b42016-09-09 18:35:01 -0700532class EncodedValue {
David Sehr7629f602016-08-07 16:01:51 -0700533 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700534 explicit EncodedValue(uint8_t type) : type_(type) { }
David Sehr7629f602016-08-07 16:01:51 -0700535
536 int8_t Type() const { return type_; }
David Sehr7629f602016-08-07 16:01:51 -0700537
Jeff Hao3ab96b42016-09-09 18:35:01 -0700538 void SetBoolean(bool z) { u_.bool_val_ = z; }
539 void SetByte(int8_t b) { u_.byte_val_ = b; }
540 void SetShort(int16_t s) { u_.short_val_ = s; }
541 void SetChar(uint16_t c) { u_.char_val_ = c; }
542 void SetInt(int32_t i) { u_.int_val_ = i; }
543 void SetLong(int64_t l) { u_.long_val_ = l; }
544 void SetFloat(float f) { u_.float_val_ = f; }
545 void SetDouble(double d) { u_.double_val_ = d; }
546 void SetStringId(StringId* string_id) { u_.string_val_ = string_id; }
547 void SetTypeId(TypeId* type_id) { u_.type_val_ = type_id; }
548 void SetFieldId(FieldId* field_id) { u_.field_val_ = field_id; }
549 void SetMethodId(MethodId* method_id) { u_.method_val_ = method_id; }
550 void SetEncodedArray(EncodedArrayItem* encoded_array) { encoded_array_.reset(encoded_array); }
551 void SetEncodedAnnotation(EncodedAnnotation* encoded_annotation)
552 { encoded_annotation_.reset(encoded_annotation); }
553
554 bool GetBoolean() const { return u_.bool_val_; }
555 int8_t GetByte() const { return u_.byte_val_; }
556 int16_t GetShort() const { return u_.short_val_; }
557 uint16_t GetChar() const { return u_.char_val_; }
558 int32_t GetInt() const { return u_.int_val_; }
559 int64_t GetLong() const { return u_.long_val_; }
560 float GetFloat() const { return u_.float_val_; }
561 double GetDouble() const { return u_.double_val_; }
562 StringId* GetStringId() const { return u_.string_val_; }
563 TypeId* GetTypeId() const { return u_.type_val_; }
564 FieldId* GetFieldId() const { return u_.field_val_; }
565 MethodId* GetMethodId() const { return u_.method_val_; }
566 EncodedArrayItem* GetEncodedArray() const { return encoded_array_.get(); }
567 EncodedAnnotation* GetEncodedAnnotation() const { return encoded_annotation_.get(); }
568
569 EncodedAnnotation* ReleaseEncodedAnnotation() { return encoded_annotation_.release(); }
David Sehr7629f602016-08-07 16:01:51 -0700570
571 private:
David Sehr7629f602016-08-07 16:01:51 -0700572 uint8_t type_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700573 union {
574 bool bool_val_;
575 int8_t byte_val_;
576 int16_t short_val_;
577 uint16_t char_val_;
578 int32_t int_val_;
579 int64_t long_val_;
580 float float_val_;
581 double double_val_;
582 StringId* string_val_;
583 TypeId* type_val_;
584 FieldId* field_val_;
585 MethodId* method_val_;
586 } u_;
587 std::unique_ptr<EncodedArrayItem> encoded_array_;
588 std::unique_ptr<EncodedAnnotation> encoded_annotation_;
589
590 DISALLOW_COPY_AND_ASSIGN(EncodedValue);
David Sehr7629f602016-08-07 16:01:51 -0700591};
592
Jeff Hao3ab96b42016-09-09 18:35:01 -0700593using EncodedValueVector = std::vector<std::unique_ptr<EncodedValue>>;
594
595class AnnotationElement {
596 public:
597 AnnotationElement(StringId* name, EncodedValue* value) : name_(name), value_(value) { }
598
599 StringId* GetName() const { return name_; }
600 EncodedValue* GetValue() const { return value_.get(); }
601
602 private:
603 StringId* name_;
604 std::unique_ptr<EncodedValue> value_;
605
606 DISALLOW_COPY_AND_ASSIGN(AnnotationElement);
607};
608
609using AnnotationElementVector = std::vector<std::unique_ptr<AnnotationElement>>;
610
611class EncodedAnnotation {
612 public:
613 EncodedAnnotation(TypeId* type, AnnotationElementVector* elements)
614 : type_(type), elements_(elements) { }
615
616 TypeId* GetType() const { return type_; }
617 AnnotationElementVector* GetAnnotationElements() const { return elements_.get(); }
618
619 private:
620 TypeId* type_;
621 std::unique_ptr<AnnotationElementVector> elements_;
622
623 DISALLOW_COPY_AND_ASSIGN(EncodedAnnotation);
624};
625
626class EncodedArrayItem : public Item {
627 public:
628 explicit EncodedArrayItem(EncodedValueVector* encoded_values)
629 : encoded_values_(encoded_values) { }
630
631 EncodedValueVector* GetEncodedValues() const { return encoded_values_.get(); }
632
633 private:
634 std::unique_ptr<EncodedValueVector> encoded_values_;
635
636 DISALLOW_COPY_AND_ASSIGN(EncodedArrayItem);
637};
David Sehr853a8e12016-09-01 13:03:50 -0700638
David Sehr7629f602016-08-07 16:01:51 -0700639class ClassData : public Item {
640 public:
David Sehr853a8e12016-09-01 13:03:50 -0700641 ClassData(FieldItemVector* static_fields,
642 FieldItemVector* instance_fields,
643 MethodItemVector* direct_methods,
644 MethodItemVector* virtual_methods)
645 : static_fields_(static_fields),
646 instance_fields_(instance_fields),
647 direct_methods_(direct_methods),
648 virtual_methods_(virtual_methods) { }
649
David Sehr7629f602016-08-07 16:01:51 -0700650 ~ClassData() OVERRIDE = default;
David Sehr853a8e12016-09-01 13:03:50 -0700651 FieldItemVector* StaticFields() { return static_fields_.get(); }
652 FieldItemVector* InstanceFields() { return instance_fields_.get(); }
653 MethodItemVector* DirectMethods() { return direct_methods_.get(); }
654 MethodItemVector* VirtualMethods() { return virtual_methods_.get(); }
David Sehr7629f602016-08-07 16:01:51 -0700655
656 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
657
658 private:
David Sehr853a8e12016-09-01 13:03:50 -0700659 std::unique_ptr<FieldItemVector> static_fields_;
660 std::unique_ptr<FieldItemVector> instance_fields_;
661 std::unique_ptr<MethodItemVector> direct_methods_;
662 std::unique_ptr<MethodItemVector> virtual_methods_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700663
David Sehr7629f602016-08-07 16:01:51 -0700664 DISALLOW_COPY_AND_ASSIGN(ClassData);
665};
666
Jeff Hao3ab96b42016-09-09 18:35:01 -0700667class ClassDef : public IndexedItem {
David Sehr7629f602016-08-07 16:01:51 -0700668 public:
David Sehr853a8e12016-09-01 13:03:50 -0700669 ClassDef(const TypeId* class_type,
670 uint32_t access_flags,
671 const TypeId* superclass,
Jeff Hao3ab96b42016-09-09 18:35:01 -0700672 TypeList* interfaces,
David Sehr853a8e12016-09-01 13:03:50 -0700673 const StringId* source_file,
674 AnnotationsDirectoryItem* annotations,
Jeff Hao3ab96b42016-09-09 18:35:01 -0700675 EncodedArrayItem* static_values,
David Sehr853a8e12016-09-01 13:03:50 -0700676 ClassData* class_data)
677 : class_type_(class_type),
678 access_flags_(access_flags),
679 superclass_(superclass),
680 interfaces_(interfaces),
David Sehr853a8e12016-09-01 13:03:50 -0700681 source_file_(source_file),
682 annotations_(annotations),
683 static_values_(static_values),
Jeff Hao3ab96b42016-09-09 18:35:01 -0700684 class_data_(class_data) { size_ = kClassDefItemSize; }
David Sehr853a8e12016-09-01 13:03:50 -0700685
David Sehr7629f602016-08-07 16:01:51 -0700686 ~ClassDef() OVERRIDE { }
687
Jeff Hao3ab96b42016-09-09 18:35:01 -0700688 static size_t ItemSize() { return kClassDefItemSize; }
689
David Sehr7629f602016-08-07 16:01:51 -0700690 const TypeId* ClassType() const { return class_type_; }
691 uint32_t GetAccessFlags() const { return access_flags_; }
692 const TypeId* Superclass() const { return superclass_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700693 const TypeIdVector* Interfaces()
694 { return interfaces_ == nullptr ? nullptr: interfaces_->GetTypeList(); }
695 uint32_t InterfacesOffset() { return interfaces_ == nullptr ? 0 : interfaces_->GetOffset(); }
David Sehr7629f602016-08-07 16:01:51 -0700696 const StringId* SourceFile() const { return source_file_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700697 AnnotationsDirectoryItem* Annotations() const { return annotations_; }
698 EncodedArrayItem* StaticValues() { return static_values_; }
699 ClassData* GetClassData() { return class_data_; }
David Sehr7629f602016-08-07 16:01:51 -0700700
Jeff Haoc3acfc52016-08-29 14:18:26 -0700701 MethodItem* GenerateMethodItem(Header& header, ClassDataItemIterator& cdii);
702
David Sehr7629f602016-08-07 16:01:51 -0700703 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
704
705 private:
706 const TypeId* class_type_;
707 uint32_t access_flags_;
708 const TypeId* superclass_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700709 TypeList* interfaces_;
David Sehr7629f602016-08-07 16:01:51 -0700710 const StringId* source_file_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700711 AnnotationsDirectoryItem* annotations_;
712 EncodedArrayItem* static_values_;
713 ClassData* class_data_;
714
David Sehr7629f602016-08-07 16:01:51 -0700715 DISALLOW_COPY_AND_ASSIGN(ClassDef);
716};
717
David Sehr853a8e12016-09-01 13:03:50 -0700718class CatchHandler {
719 public:
720 CatchHandler(const TypeId* type_id, uint32_t address) : type_id_(type_id), address_(address) { }
721
722 const TypeId* GetTypeId() const { return type_id_; }
723 uint32_t GetAddress() const { return address_; }
724
725 private:
726 const TypeId* type_id_;
727 uint32_t address_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700728
David Sehr853a8e12016-09-01 13:03:50 -0700729 DISALLOW_COPY_AND_ASSIGN(CatchHandler);
730};
731
732using CatchHandlerVector = std::vector<std::unique_ptr<const CatchHandler>>;
733
734class TryItem : public Item {
735 public:
736 TryItem(uint32_t start_addr, uint16_t insn_count, CatchHandlerVector* handlers)
737 : start_addr_(start_addr), insn_count_(insn_count), handlers_(handlers) { }
738 ~TryItem() OVERRIDE { }
739
740 uint32_t StartAddr() const { return start_addr_; }
741 uint16_t InsnCount() const { return insn_count_; }
742 const CatchHandlerVector& GetHandlers() const { return *handlers_.get(); }
743
744 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
745
746 private:
747 uint32_t start_addr_;
748 uint16_t insn_count_;
749 std::unique_ptr<CatchHandlerVector> handlers_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700750
David Sehr853a8e12016-09-01 13:03:50 -0700751 DISALLOW_COPY_AND_ASSIGN(TryItem);
752};
753
754using TryItemVector = std::vector<std::unique_ptr<const TryItem>>;
755
David Sehr7629f602016-08-07 16:01:51 -0700756class CodeItem : public Item {
757 public:
David Sehr853a8e12016-09-01 13:03:50 -0700758 CodeItem(uint16_t registers_size,
759 uint16_t ins_size,
760 uint16_t outs_size,
761 DebugInfoItem* debug_info,
762 uint32_t insns_size,
763 uint16_t* insns,
764 TryItemVector* tries)
765 : registers_size_(registers_size),
766 ins_size_(ins_size),
767 outs_size_(outs_size),
768 debug_info_(debug_info),
769 insns_size_(insns_size),
770 insns_(insns),
771 tries_(tries) { }
772
David Sehr7629f602016-08-07 16:01:51 -0700773 ~CodeItem() OVERRIDE { }
774
775 uint16_t RegistersSize() const { return registers_size_; }
776 uint16_t InsSize() const { return ins_size_; }
777 uint16_t OutsSize() const { return outs_size_; }
David Sehr853a8e12016-09-01 13:03:50 -0700778 uint16_t TriesSize() const { return tries_ == nullptr ? 0 : tries_->size(); }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700779 DebugInfoItem* DebugInfo() const { return debug_info_; }
David Sehr7629f602016-08-07 16:01:51 -0700780 uint32_t InsnsSize() const { return insns_size_; }
781 uint16_t* Insns() const { return insns_.get(); }
David Sehr853a8e12016-09-01 13:03:50 -0700782 TryItemVector* Tries() const { return tries_.get(); }
David Sehr7629f602016-08-07 16:01:51 -0700783
784 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
785
786 private:
787 uint16_t registers_size_;
788 uint16_t ins_size_;
789 uint16_t outs_size_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700790 DebugInfoItem* debug_info_;
David Sehr7629f602016-08-07 16:01:51 -0700791 uint32_t insns_size_;
792 std::unique_ptr<uint16_t[]> insns_;
David Sehr853a8e12016-09-01 13:03:50 -0700793 std::unique_ptr<TryItemVector> tries_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700794
David Sehr7629f602016-08-07 16:01:51 -0700795 DISALLOW_COPY_AND_ASSIGN(CodeItem);
796};
797
David Sehr7629f602016-08-07 16:01:51 -0700798struct PositionInfo {
799 PositionInfo(uint32_t address, uint32_t line) : address_(address), line_(line) { }
800
801 uint32_t address_;
802 uint32_t line_;
803};
804
David Sehr853a8e12016-09-01 13:03:50 -0700805using PositionInfoVector = std::vector<std::unique_ptr<PositionInfo>>;
806
David Sehr7629f602016-08-07 16:01:51 -0700807struct LocalInfo {
David Sehr853a8e12016-09-01 13:03:50 -0700808 LocalInfo(const char* name,
809 const char* descriptor,
810 const char* signature,
811 uint32_t start_address,
812 uint32_t end_address,
813 uint16_t reg)
814 : name_(name),
815 descriptor_(descriptor),
816 signature_(signature),
817 start_address_(start_address),
818 end_address_(end_address),
819 reg_(reg) { }
David Sehr7629f602016-08-07 16:01:51 -0700820
821 std::string name_;
822 std::string descriptor_;
823 std::string signature_;
824 uint32_t start_address_;
825 uint32_t end_address_;
826 uint16_t reg_;
827};
828
David Sehr853a8e12016-09-01 13:03:50 -0700829using LocalInfoVector = std::vector<std::unique_ptr<LocalInfo>>;
830
David Sehr7629f602016-08-07 16:01:51 -0700831class DebugInfoItem : public Item {
832 public:
833 DebugInfoItem() = default;
834
David Sehr853a8e12016-09-01 13:03:50 -0700835 PositionInfoVector& GetPositionInfo() { return positions_; }
836 LocalInfoVector& GetLocalInfo() { return locals_; }
David Sehr7629f602016-08-07 16:01:51 -0700837
838 private:
David Sehr853a8e12016-09-01 13:03:50 -0700839 PositionInfoVector positions_;
840 LocalInfoVector locals_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700841
David Sehr7629f602016-08-07 16:01:51 -0700842 DISALLOW_COPY_AND_ASSIGN(DebugInfoItem);
843};
844
Jeff Hao3ab96b42016-09-09 18:35:01 -0700845class AnnotationItem : public Item {
David Sehr853a8e12016-09-01 13:03:50 -0700846 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700847 AnnotationItem(uint8_t visibility, EncodedAnnotation* annotation)
848 : visibility_(visibility), annotation_(annotation) { }
David Sehr853a8e12016-09-01 13:03:50 -0700849
850 uint8_t GetVisibility() const { return visibility_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700851 EncodedAnnotation* GetAnnotation() const { return annotation_.get(); }
David Sehr7629f602016-08-07 16:01:51 -0700852
853 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
854
855 private:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700856 uint8_t visibility_;
857 std::unique_ptr<EncodedAnnotation> annotation_;
858
859 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
860};
861
862class AnnotationSetItem : public Item {
863 public:
864 explicit AnnotationSetItem(std::vector<AnnotationItem*>* items) : items_(items) {
865 size_ = sizeof(uint32_t) + items->size() * sizeof(uint32_t);
866 }
867 ~AnnotationSetItem() OVERRIDE { }
868
869 std::vector<AnnotationItem*>* GetItems() { return items_.get(); }
870
871 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
872
873 private:
874 std::unique_ptr<std::vector<AnnotationItem*>> items_;
875
David Sehr7629f602016-08-07 16:01:51 -0700876 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
877};
878
Jeff Hao3ab96b42016-09-09 18:35:01 -0700879class AnnotationSetRefList : public Item {
880 public:
881 explicit AnnotationSetRefList(std::vector<AnnotationSetItem*>* items) : items_(items) {
882 size_ = sizeof(uint32_t) + items->size() * sizeof(uint32_t);
883 }
884 ~AnnotationSetRefList() OVERRIDE { }
885
886 std::vector<AnnotationSetItem*>* GetItems() { return items_.get(); }
887
888 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
889
890 private:
891 std::unique_ptr<std::vector<AnnotationSetItem*>> items_;
892
893 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
894};
David Sehr853a8e12016-09-01 13:03:50 -0700895
896class FieldAnnotation {
897 public:
898 FieldAnnotation(FieldId* field_id, AnnotationSetItem* annotation_set_item)
899 : field_id_(field_id), annotation_set_item_(annotation_set_item) { }
900
901 FieldId* GetFieldId() const { return field_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700902 AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_; }
David Sehr853a8e12016-09-01 13:03:50 -0700903
904 private:
905 FieldId* field_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700906 AnnotationSetItem* annotation_set_item_;
907
David Sehr853a8e12016-09-01 13:03:50 -0700908 DISALLOW_COPY_AND_ASSIGN(FieldAnnotation);
909};
910
911using FieldAnnotationVector = std::vector<std::unique_ptr<FieldAnnotation>>;
912
913class MethodAnnotation {
914 public:
915 MethodAnnotation(MethodId* method_id, AnnotationSetItem* annotation_set_item)
916 : method_id_(method_id), annotation_set_item_(annotation_set_item) { }
917
918 MethodId* GetMethodId() const { return method_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700919 AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_; }
David Sehr853a8e12016-09-01 13:03:50 -0700920
921 private:
922 MethodId* method_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700923 AnnotationSetItem* annotation_set_item_;
924
David Sehr853a8e12016-09-01 13:03:50 -0700925 DISALLOW_COPY_AND_ASSIGN(MethodAnnotation);
926};
927
928using MethodAnnotationVector = std::vector<std::unique_ptr<MethodAnnotation>>;
929
930class ParameterAnnotation {
931 public:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700932 ParameterAnnotation(MethodId* method_id, AnnotationSetRefList* annotations)
David Sehr853a8e12016-09-01 13:03:50 -0700933 : method_id_(method_id), annotations_(annotations) { }
934
935 MethodId* GetMethodId() const { return method_id_; }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700936 AnnotationSetRefList* GetAnnotations() { return annotations_; }
David Sehr853a8e12016-09-01 13:03:50 -0700937
938 private:
939 MethodId* method_id_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700940 AnnotationSetRefList* annotations_;
941
David Sehr853a8e12016-09-01 13:03:50 -0700942 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotation);
943};
944
945using ParameterAnnotationVector = std::vector<std::unique_ptr<ParameterAnnotation>>;
946
David Sehr7629f602016-08-07 16:01:51 -0700947class AnnotationsDirectoryItem : public Item {
948 public:
David Sehr853a8e12016-09-01 13:03:50 -0700949 AnnotationsDirectoryItem(AnnotationSetItem* class_annotation,
950 FieldAnnotationVector* field_annotations,
951 MethodAnnotationVector* method_annotations,
952 ParameterAnnotationVector* parameter_annotations)
953 : class_annotation_(class_annotation),
954 field_annotations_(field_annotations),
955 method_annotations_(method_annotations),
956 parameter_annotations_(parameter_annotations) { }
David Sehr7629f602016-08-07 16:01:51 -0700957
Jeff Hao3ab96b42016-09-09 18:35:01 -0700958 AnnotationSetItem* GetClassAnnotation() const { return class_annotation_; }
David Sehr853a8e12016-09-01 13:03:50 -0700959 FieldAnnotationVector* GetFieldAnnotations() { return field_annotations_.get(); }
960 MethodAnnotationVector* GetMethodAnnotations() { return method_annotations_.get(); }
961 ParameterAnnotationVector* GetParameterAnnotations() { return parameter_annotations_.get(); }
David Sehr7629f602016-08-07 16:01:51 -0700962
963 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
964
965 private:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700966 AnnotationSetItem* class_annotation_;
David Sehr853a8e12016-09-01 13:03:50 -0700967 std::unique_ptr<FieldAnnotationVector> field_annotations_;
968 std::unique_ptr<MethodAnnotationVector> method_annotations_;
969 std::unique_ptr<ParameterAnnotationVector> parameter_annotations_;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700970
David Sehr7629f602016-08-07 16:01:51 -0700971 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
972};
973
974// TODO(sehr): implement MapList.
975class MapList : public Item {
976 public:
977 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
978
979 private:
980 DISALLOW_COPY_AND_ASSIGN(MapList);
981};
982
983class MapItem : public Item {
984 public:
985 void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
986
987 private:
988 DISALLOW_COPY_AND_ASSIGN(MapItem);
989};
990
991} // namespace dex_ir
992} // namespace art
993
994#endif // ART_DEXLAYOUT_DEX_IR_H_