David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | #include <stdint.h> |
| 24 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace dex_ir { |
| 29 | |
| 30 | // Forward declarations for classes used in containers or pointed to. |
| 31 | class AnnotationsDirectoryItem; |
| 32 | class AnnotationSetItem; |
| 33 | class ArrayItem; |
| 34 | class ClassData; |
| 35 | class ClassDef; |
| 36 | class CodeItem; |
| 37 | class DebugInfoItem; |
| 38 | class FieldId; |
| 39 | class FieldItem; |
| 40 | class Header; |
| 41 | class MapList; |
| 42 | class MapItem; |
| 43 | class MethodId; |
| 44 | class MethodItem; |
| 45 | class ProtoId; |
| 46 | class StringId; |
| 47 | class TryItem; |
| 48 | class TypeId; |
| 49 | |
| 50 | // Visitor support |
| 51 | class AbstractDispatcher { |
| 52 | public: |
| 53 | AbstractDispatcher() = default; |
| 54 | virtual ~AbstractDispatcher() { } |
| 55 | |
| 56 | virtual void Dispatch(Header* header) = 0; |
| 57 | virtual void Dispatch(const StringId* string_id) = 0; |
| 58 | virtual void Dispatch(const TypeId* type_id) = 0; |
| 59 | virtual void Dispatch(const ProtoId* proto_id) = 0; |
| 60 | virtual void Dispatch(const FieldId* field_id) = 0; |
| 61 | virtual void Dispatch(const MethodId* method_id) = 0; |
| 62 | virtual void Dispatch(ClassData* class_data) = 0; |
| 63 | virtual void Dispatch(ClassDef* class_def) = 0; |
| 64 | virtual void Dispatch(FieldItem* field_item) = 0; |
| 65 | virtual void Dispatch(MethodItem* method_item) = 0; |
| 66 | virtual void Dispatch(ArrayItem* array_item) = 0; |
| 67 | virtual void Dispatch(CodeItem* code_item) = 0; |
| 68 | virtual void Dispatch(TryItem* try_item) = 0; |
| 69 | virtual void Dispatch(DebugInfoItem* debug_info_item) = 0; |
| 70 | virtual void Dispatch(AnnotationSetItem* annotation_set_item) = 0; |
| 71 | virtual void Dispatch(AnnotationsDirectoryItem* annotations_directory_item) = 0; |
| 72 | virtual void Dispatch(MapList* map_list) = 0; |
| 73 | virtual void Dispatch(MapItem* map_item) = 0; |
| 74 | |
| 75 | private: |
| 76 | DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher); |
| 77 | }; |
| 78 | |
| 79 | // Collections become owners of the objects added by moving them into unique pointers. |
| 80 | template<class T> class CollectionWithOffset { |
| 81 | public: |
| 82 | CollectionWithOffset() = default; |
| 83 | std::vector<std::unique_ptr<T>>& Collection() { return collection_; } |
| 84 | // Read-time support methods |
| 85 | void AddWithPosition(uint32_t position, T* object) { |
| 86 | collection_.push_back(std::unique_ptr<T>(object)); |
| 87 | collection_.back()->SetOffset(position); |
| 88 | } |
| 89 | // Ordinary object insertion into collection. |
| 90 | void Insert(T object ATTRIBUTE_UNUSED) { |
| 91 | // TODO(sehr): add ordered insertion support. |
| 92 | UNIMPLEMENTED(FATAL) << "Insertion not ready"; |
| 93 | } |
| 94 | uint32_t GetOffset() const { return offset_; } |
| 95 | void SetOffset(uint32_t new_offset) { offset_ = new_offset; } |
| 96 | uint32_t Size() const { return collection_.size(); } |
| 97 | |
| 98 | private: |
| 99 | std::vector<std::unique_ptr<T>> collection_; |
| 100 | uint32_t offset_ = 0; |
| 101 | DISALLOW_COPY_AND_ASSIGN(CollectionWithOffset); |
| 102 | }; |
| 103 | |
| 104 | class Item { |
| 105 | public: |
| 106 | virtual ~Item() { } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 107 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 108 | uint32_t GetOffset() const { return offset_; } |
| 109 | void SetOffset(uint32_t offset) { offset_ = offset; } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 110 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 111 | protected: |
| 112 | uint32_t offset_ = 0; |
| 113 | }; |
| 114 | |
| 115 | class Header : public Item { |
| 116 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 117 | Header(const uint8_t* magic, |
| 118 | uint32_t checksum, |
| 119 | const uint8_t* signature, |
| 120 | uint32_t endian_tag, |
| 121 | uint32_t file_size, |
| 122 | uint32_t header_size, |
| 123 | uint32_t link_size, |
| 124 | uint32_t link_offset, |
| 125 | uint32_t data_size, |
| 126 | uint32_t data_offset) |
| 127 | : checksum_(checksum), |
| 128 | endian_tag_(endian_tag), |
| 129 | file_size_(file_size), |
| 130 | header_size_(header_size), |
| 131 | link_size_(link_size), |
| 132 | link_offset_(link_offset), |
| 133 | data_size_(data_size), |
| 134 | data_offset_(data_offset) { |
| 135 | memcpy(magic_, magic, sizeof(magic_)); |
| 136 | memcpy(signature_, signature, sizeof(signature_)); |
| 137 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 138 | ~Header() OVERRIDE { } |
| 139 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 140 | const uint8_t* Magic() const { return magic_; } |
| 141 | uint32_t Checksum() const { return checksum_; } |
| 142 | const uint8_t* Signature() const { return signature_; } |
| 143 | uint32_t EndianTag() const { return endian_tag_; } |
| 144 | uint32_t FileSize() const { return file_size_; } |
| 145 | uint32_t HeaderSize() const { return header_size_; } |
| 146 | uint32_t LinkSize() const { return link_size_; } |
| 147 | uint32_t LinkOffset() const { return link_offset_; } |
| 148 | uint32_t DataSize() const { return data_size_; } |
| 149 | uint32_t DataOffset() const { return data_offset_; } |
| 150 | |
| 151 | void SetChecksum(uint32_t new_checksum) { checksum_ = new_checksum; } |
| 152 | void SetSignature(const uint8_t* new_signature) { |
| 153 | memcpy(signature_, new_signature, sizeof(signature_)); |
| 154 | } |
| 155 | void SetFileSize(uint32_t new_file_size) { file_size_ = new_file_size; } |
| 156 | void SetHeaderSize(uint32_t new_header_size) { header_size_ = new_header_size; } |
| 157 | void SetLinkSize(uint32_t new_link_size) { link_size_ = new_link_size; } |
| 158 | void SetLinkOffset(uint32_t new_link_offset) { link_offset_ = new_link_offset; } |
| 159 | void SetDataSize(uint32_t new_data_size) { data_size_ = new_data_size; } |
| 160 | void SetDataOffset(uint32_t new_data_offset) { data_offset_ = new_data_offset; } |
| 161 | |
| 162 | // Collections. |
| 163 | std::vector<std::unique_ptr<StringId>>& StringIds() { return string_ids_.Collection(); } |
| 164 | std::vector<std::unique_ptr<TypeId>>& TypeIds() { return type_ids_.Collection(); } |
| 165 | std::vector<std::unique_ptr<ProtoId>>& ProtoIds() { return proto_ids_.Collection(); } |
| 166 | std::vector<std::unique_ptr<FieldId>>& FieldIds() { return field_ids_.Collection(); } |
| 167 | std::vector<std::unique_ptr<MethodId>>& MethodIds() { return method_ids_.Collection(); } |
| 168 | std::vector<std::unique_ptr<ClassDef>>& ClassDefs() { return class_defs_.Collection(); } |
| 169 | uint32_t StringIdsOffset() const { return string_ids_.GetOffset(); } |
| 170 | uint32_t TypeIdsOffset() const { return type_ids_.GetOffset(); } |
| 171 | uint32_t ProtoIdsOffset() const { return proto_ids_.GetOffset(); } |
| 172 | uint32_t FieldIdsOffset() const { return field_ids_.GetOffset(); } |
| 173 | uint32_t MethodIdsOffset() const { return method_ids_.GetOffset(); } |
| 174 | uint32_t ClassDefsOffset() const { return class_defs_.GetOffset(); } |
| 175 | void SetStringIdsOffset(uint32_t new_offset) { string_ids_.SetOffset(new_offset); } |
| 176 | void SetTypeIdsOffset(uint32_t new_offset) { type_ids_.SetOffset(new_offset); } |
| 177 | void SetProtoIdsOffset(uint32_t new_offset) { proto_ids_.SetOffset(new_offset); } |
| 178 | void SetFieldIdsOffset(uint32_t new_offset) { field_ids_.SetOffset(new_offset); } |
| 179 | void SetMethodIdsOffset(uint32_t new_offset) { method_ids_.SetOffset(new_offset); } |
| 180 | void SetClassDefsOffset(uint32_t new_offset) { class_defs_.SetOffset(new_offset); } |
| 181 | uint32_t StringIdsSize() const { return string_ids_.Size(); } |
| 182 | uint32_t TypeIdsSize() const { return type_ids_.Size(); } |
| 183 | uint32_t ProtoIdsSize() const { return proto_ids_.Size(); } |
| 184 | uint32_t FieldIdsSize() const { return field_ids_.Size(); } |
| 185 | uint32_t MethodIdsSize() const { return method_ids_.Size(); } |
| 186 | uint32_t ClassDefsSize() const { return class_defs_.Size(); } |
| 187 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 188 | TypeId* GetTypeIdOrNullPtr(uint16_t index) { |
| 189 | return index == DexFile::kDexNoIndex16 ? nullptr : TypeIds()[index].get(); |
| 190 | } |
| 191 | |
| 192 | StringId* GetStringIdOrNullPtr(uint32_t index) { |
| 193 | return index == DexFile::kDexNoIndex ? nullptr : StringIds()[index].get(); |
| 194 | } |
| 195 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 196 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 197 | |
| 198 | private: |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 199 | uint8_t magic_[8]; |
| 200 | uint32_t checksum_; |
| 201 | uint8_t signature_[DexFile::kSha1DigestSize]; |
| 202 | uint32_t endian_tag_; |
| 203 | uint32_t file_size_; |
| 204 | uint32_t header_size_; |
| 205 | uint32_t link_size_; |
| 206 | uint32_t link_offset_; |
| 207 | uint32_t data_size_; |
| 208 | uint32_t data_offset_; |
| 209 | |
| 210 | CollectionWithOffset<StringId> string_ids_; |
| 211 | CollectionWithOffset<TypeId> type_ids_; |
| 212 | CollectionWithOffset<ProtoId> proto_ids_; |
| 213 | CollectionWithOffset<FieldId> field_ids_; |
| 214 | CollectionWithOffset<MethodId> method_ids_; |
| 215 | CollectionWithOffset<ClassDef> class_defs_; |
| 216 | DISALLOW_COPY_AND_ASSIGN(Header); |
| 217 | }; |
| 218 | |
| 219 | class StringId : public Item { |
| 220 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 221 | explicit StringId(const char* data) : data_(strdup(data)) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 222 | ~StringId() OVERRIDE { } |
| 223 | |
| 224 | const char* Data() const { return data_.get(); } |
| 225 | |
| 226 | void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); } |
| 227 | |
| 228 | private: |
| 229 | std::unique_ptr<const char> data_; |
| 230 | DISALLOW_COPY_AND_ASSIGN(StringId); |
| 231 | }; |
| 232 | |
| 233 | class TypeId : public Item { |
| 234 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 235 | explicit TypeId(StringId* string_id) : string_id_(string_id) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 236 | ~TypeId() OVERRIDE { } |
| 237 | |
| 238 | StringId* GetStringId() const { return string_id_; } |
| 239 | |
| 240 | void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); } |
| 241 | |
| 242 | private: |
| 243 | StringId* string_id_; |
| 244 | DISALLOW_COPY_AND_ASSIGN(TypeId); |
| 245 | }; |
| 246 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 247 | using TypeIdVector = std::vector<const TypeId*>; |
| 248 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 249 | class ProtoId : public Item { |
| 250 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 251 | ProtoId(const StringId* shorty, const TypeId* return_type, TypeIdVector* parameters) |
| 252 | : shorty_(shorty), return_type_(return_type), parameters_(parameters) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 253 | ~ProtoId() OVERRIDE { } |
| 254 | |
| 255 | const StringId* Shorty() const { return shorty_; } |
| 256 | const TypeId* ReturnType() const { return return_type_; } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 257 | const std::vector<const TypeId*>& Parameters() const { return *parameters_; } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 258 | |
| 259 | void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); } |
| 260 | |
| 261 | private: |
| 262 | const StringId* shorty_; |
| 263 | const TypeId* return_type_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 264 | std::unique_ptr<TypeIdVector> parameters_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 265 | DISALLOW_COPY_AND_ASSIGN(ProtoId); |
| 266 | }; |
| 267 | |
| 268 | class FieldId : public Item { |
| 269 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 270 | FieldId(const TypeId* klass, const TypeId* type, const StringId* name) |
| 271 | : class_(klass), type_(type), name_(name) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 272 | ~FieldId() OVERRIDE { } |
| 273 | |
| 274 | const TypeId* Class() const { return class_; } |
| 275 | const TypeId* Type() const { return type_; } |
| 276 | const StringId* Name() const { return name_; } |
| 277 | |
| 278 | void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); } |
| 279 | |
| 280 | private: |
| 281 | const TypeId* class_; |
| 282 | const TypeId* type_; |
| 283 | const StringId* name_; |
| 284 | DISALLOW_COPY_AND_ASSIGN(FieldId); |
| 285 | }; |
| 286 | |
| 287 | class MethodId : public Item { |
| 288 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 289 | MethodId(const TypeId* klass, const ProtoId* proto, const StringId* name) |
| 290 | : class_(klass), proto_(proto), name_(name) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 291 | ~MethodId() OVERRIDE { } |
| 292 | |
| 293 | const TypeId* Class() const { return class_; } |
| 294 | const ProtoId* Proto() const { return proto_; } |
| 295 | const StringId* Name() const { return name_; } |
| 296 | |
| 297 | void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); } |
| 298 | |
| 299 | private: |
| 300 | const TypeId* class_; |
| 301 | const ProtoId* proto_; |
| 302 | const StringId* name_; |
| 303 | DISALLOW_COPY_AND_ASSIGN(MethodId); |
| 304 | }; |
| 305 | |
| 306 | class FieldItem : public Item { |
| 307 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 308 | FieldItem(uint32_t access_flags, const FieldId* field_id) |
| 309 | : access_flags_(access_flags), field_id_(field_id) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 310 | ~FieldItem() OVERRIDE { } |
| 311 | |
| 312 | uint32_t GetAccessFlags() const { return access_flags_; } |
| 313 | const FieldId* GetFieldId() const { return field_id_; } |
| 314 | |
| 315 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 316 | |
| 317 | private: |
| 318 | uint32_t access_flags_; |
| 319 | const FieldId* field_id_; |
| 320 | DISALLOW_COPY_AND_ASSIGN(FieldItem); |
| 321 | }; |
| 322 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 323 | using FieldItemVector = std::vector<std::unique_ptr<FieldItem>>; |
| 324 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 325 | class MethodItem : public Item { |
| 326 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 327 | MethodItem(uint32_t access_flags, const MethodId* method_id, const CodeItem* code) |
| 328 | : access_flags_(access_flags), method_id_(method_id), code_(code) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 329 | ~MethodItem() OVERRIDE { } |
| 330 | |
| 331 | uint32_t GetAccessFlags() const { return access_flags_; } |
| 332 | const MethodId* GetMethodId() const { return method_id_; } |
| 333 | const CodeItem* GetCodeItem() const { return code_.get(); } |
| 334 | |
| 335 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 336 | |
| 337 | private: |
| 338 | uint32_t access_flags_; |
| 339 | const MethodId* method_id_; |
| 340 | std::unique_ptr<const CodeItem> code_; |
| 341 | DISALLOW_COPY_AND_ASSIGN(MethodItem); |
| 342 | }; |
| 343 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 344 | using MethodItemVector = std::vector<std::unique_ptr<MethodItem>>; |
| 345 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 346 | class ArrayItem : public Item { |
| 347 | public: |
| 348 | class NameValuePair { |
| 349 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 350 | NameValuePair(StringId* name, ArrayItem* value) |
| 351 | : name_(name), value_(value) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 352 | |
| 353 | StringId* Name() const { return name_; } |
| 354 | ArrayItem* Value() const { return value_.get(); } |
| 355 | |
| 356 | private: |
| 357 | StringId* name_; |
| 358 | std::unique_ptr<ArrayItem> value_; |
| 359 | DISALLOW_COPY_AND_ASSIGN(NameValuePair); |
| 360 | }; |
| 361 | |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 362 | struct ArrayItemVariant { |
| 363 | public: |
| 364 | union { |
| 365 | bool bool_val_; |
| 366 | int8_t byte_val_; |
| 367 | int16_t short_val_; |
| 368 | uint16_t char_val_; |
| 369 | int32_t int_val_; |
| 370 | int64_t long_val_; |
| 371 | float float_val_; |
| 372 | double double_val_; |
| 373 | StringId* string_val_; |
| 374 | FieldId* field_val_; |
| 375 | MethodId* method_val_; |
| 376 | } u_; |
| 377 | std::unique_ptr<std::vector<std::unique_ptr<ArrayItem>>> annotation_array_val_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 378 | struct { |
| 379 | StringId* string_; |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 380 | std::unique_ptr<std::vector<std::unique_ptr<NameValuePair>>> array_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 381 | } annotation_annotation_val_; |
| 382 | }; |
| 383 | |
| 384 | explicit ArrayItem(uint8_t type) : type_(type) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 385 | ~ArrayItem() OVERRIDE { } |
| 386 | |
| 387 | int8_t Type() const { return type_; } |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 388 | bool GetBoolean() const { return item_.u_.bool_val_; } |
| 389 | int8_t GetByte() const { return item_.u_.byte_val_; } |
| 390 | int16_t GetShort() const { return item_.u_.short_val_; } |
| 391 | uint16_t GetChar() const { return item_.u_.char_val_; } |
| 392 | int32_t GetInt() const { return item_.u_.int_val_; } |
| 393 | int64_t GetLong() const { return item_.u_.long_val_; } |
| 394 | float GetFloat() const { return item_.u_.float_val_; } |
| 395 | double GetDouble() const { return item_.u_.double_val_; } |
| 396 | StringId* GetStringId() const { return item_.u_.string_val_; } |
| 397 | FieldId* GetFieldId() const { return item_.u_.field_val_; } |
| 398 | MethodId* GetMethodId() const { return item_.u_.method_val_; } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 399 | std::vector<std::unique_ptr<ArrayItem>>* GetAnnotationArray() const { |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 400 | return item_.annotation_array_val_.get(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 401 | } |
| 402 | StringId* GetAnnotationAnnotationString() const { |
| 403 | return item_.annotation_annotation_val_.string_; |
| 404 | } |
| 405 | std::vector<std::unique_ptr<NameValuePair>>* GetAnnotationAnnotationNameValuePairArray() const { |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 406 | return item_.annotation_annotation_val_.array_.get(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 407 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 408 | // Used to construct the item union. Ugly, but necessary. |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 409 | ArrayItemVariant* GetArrayItemVariant() { return &item_; } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 410 | |
| 411 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 412 | |
| 413 | private: |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 414 | uint8_t type_; |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 415 | ArrayItemVariant item_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 416 | DISALLOW_COPY_AND_ASSIGN(ArrayItem); |
| 417 | }; |
| 418 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 419 | using ArrayItemVector = std::vector<std::unique_ptr<ArrayItem>>; |
| 420 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 421 | class ClassData : public Item { |
| 422 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 423 | ClassData(FieldItemVector* static_fields, |
| 424 | FieldItemVector* instance_fields, |
| 425 | MethodItemVector* direct_methods, |
| 426 | MethodItemVector* virtual_methods) |
| 427 | : static_fields_(static_fields), |
| 428 | instance_fields_(instance_fields), |
| 429 | direct_methods_(direct_methods), |
| 430 | virtual_methods_(virtual_methods) { } |
| 431 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 432 | ~ClassData() OVERRIDE = default; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 433 | FieldItemVector* StaticFields() { return static_fields_.get(); } |
| 434 | FieldItemVector* InstanceFields() { return instance_fields_.get(); } |
| 435 | MethodItemVector* DirectMethods() { return direct_methods_.get(); } |
| 436 | MethodItemVector* VirtualMethods() { return virtual_methods_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 437 | |
| 438 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 439 | |
| 440 | private: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 441 | std::unique_ptr<FieldItemVector> static_fields_; |
| 442 | std::unique_ptr<FieldItemVector> instance_fields_; |
| 443 | std::unique_ptr<MethodItemVector> direct_methods_; |
| 444 | std::unique_ptr<MethodItemVector> virtual_methods_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 445 | DISALLOW_COPY_AND_ASSIGN(ClassData); |
| 446 | }; |
| 447 | |
| 448 | class ClassDef : public Item { |
| 449 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 450 | ClassDef(const TypeId* class_type, |
| 451 | uint32_t access_flags, |
| 452 | const TypeId* superclass, |
| 453 | TypeIdVector* interfaces, |
| 454 | uint32_t interfaces_offset, |
| 455 | const StringId* source_file, |
| 456 | AnnotationsDirectoryItem* annotations, |
| 457 | ArrayItemVector* static_values, |
| 458 | ClassData* class_data) |
| 459 | : class_type_(class_type), |
| 460 | access_flags_(access_flags), |
| 461 | superclass_(superclass), |
| 462 | interfaces_(interfaces), |
| 463 | interfaces_offset_(interfaces_offset), |
| 464 | source_file_(source_file), |
| 465 | annotations_(annotations), |
| 466 | static_values_(static_values), |
| 467 | class_data_(class_data) { } |
| 468 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 469 | ~ClassDef() OVERRIDE { } |
| 470 | |
| 471 | const TypeId* ClassType() const { return class_type_; } |
| 472 | uint32_t GetAccessFlags() const { return access_flags_; } |
| 473 | const TypeId* Superclass() const { return superclass_; } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 474 | TypeIdVector* Interfaces() { return interfaces_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 475 | uint32_t InterfacesOffset() const { return interfaces_offset_; } |
| 476 | void SetInterfacesOffset(uint32_t new_offset) { interfaces_offset_ = new_offset; } |
| 477 | const StringId* SourceFile() const { return source_file_; } |
| 478 | AnnotationsDirectoryItem* Annotations() const { return annotations_.get(); } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 479 | ArrayItemVector* StaticValues() { return static_values_.get(); } |
| 480 | ClassData* GetClassData() { return class_data_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 481 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 482 | MethodItem* GenerateMethodItem(Header& header, ClassDataItemIterator& cdii); |
| 483 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 484 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 485 | |
| 486 | private: |
| 487 | const TypeId* class_type_; |
| 488 | uint32_t access_flags_; |
| 489 | const TypeId* superclass_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 490 | std::unique_ptr<TypeIdVector> interfaces_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 491 | uint32_t interfaces_offset_; |
| 492 | const StringId* source_file_; |
| 493 | std::unique_ptr<AnnotationsDirectoryItem> annotations_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 494 | std::unique_ptr<ArrayItemVector> static_values_; |
| 495 | std::unique_ptr<ClassData> class_data_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 496 | DISALLOW_COPY_AND_ASSIGN(ClassDef); |
| 497 | }; |
| 498 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 499 | class CatchHandler { |
| 500 | public: |
| 501 | CatchHandler(const TypeId* type_id, uint32_t address) : type_id_(type_id), address_(address) { } |
| 502 | |
| 503 | const TypeId* GetTypeId() const { return type_id_; } |
| 504 | uint32_t GetAddress() const { return address_; } |
| 505 | |
| 506 | private: |
| 507 | const TypeId* type_id_; |
| 508 | uint32_t address_; |
| 509 | DISALLOW_COPY_AND_ASSIGN(CatchHandler); |
| 510 | }; |
| 511 | |
| 512 | using CatchHandlerVector = std::vector<std::unique_ptr<const CatchHandler>>; |
| 513 | |
| 514 | class TryItem : public Item { |
| 515 | public: |
| 516 | TryItem(uint32_t start_addr, uint16_t insn_count, CatchHandlerVector* handlers) |
| 517 | : start_addr_(start_addr), insn_count_(insn_count), handlers_(handlers) { } |
| 518 | ~TryItem() OVERRIDE { } |
| 519 | |
| 520 | uint32_t StartAddr() const { return start_addr_; } |
| 521 | uint16_t InsnCount() const { return insn_count_; } |
| 522 | const CatchHandlerVector& GetHandlers() const { return *handlers_.get(); } |
| 523 | |
| 524 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 525 | |
| 526 | private: |
| 527 | uint32_t start_addr_; |
| 528 | uint16_t insn_count_; |
| 529 | std::unique_ptr<CatchHandlerVector> handlers_; |
| 530 | DISALLOW_COPY_AND_ASSIGN(TryItem); |
| 531 | }; |
| 532 | |
| 533 | using TryItemVector = std::vector<std::unique_ptr<const TryItem>>; |
| 534 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 535 | class CodeItem : public Item { |
| 536 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 537 | CodeItem(uint16_t registers_size, |
| 538 | uint16_t ins_size, |
| 539 | uint16_t outs_size, |
| 540 | DebugInfoItem* debug_info, |
| 541 | uint32_t insns_size, |
| 542 | uint16_t* insns, |
| 543 | TryItemVector* tries) |
| 544 | : registers_size_(registers_size), |
| 545 | ins_size_(ins_size), |
| 546 | outs_size_(outs_size), |
| 547 | debug_info_(debug_info), |
| 548 | insns_size_(insns_size), |
| 549 | insns_(insns), |
| 550 | tries_(tries) { } |
| 551 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 552 | ~CodeItem() OVERRIDE { } |
| 553 | |
| 554 | uint16_t RegistersSize() const { return registers_size_; } |
| 555 | uint16_t InsSize() const { return ins_size_; } |
| 556 | uint16_t OutsSize() const { return outs_size_; } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 557 | uint16_t TriesSize() const { return tries_ == nullptr ? 0 : tries_->size(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 558 | DebugInfoItem* DebugInfo() const { return debug_info_.get(); } |
| 559 | uint32_t InsnsSize() const { return insns_size_; } |
| 560 | uint16_t* Insns() const { return insns_.get(); } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 561 | TryItemVector* Tries() const { return tries_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 562 | |
| 563 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 564 | |
| 565 | private: |
| 566 | uint16_t registers_size_; |
| 567 | uint16_t ins_size_; |
| 568 | uint16_t outs_size_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 569 | std::unique_ptr<DebugInfoItem> debug_info_; |
| 570 | uint32_t insns_size_; |
| 571 | std::unique_ptr<uint16_t[]> insns_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 572 | std::unique_ptr<TryItemVector> tries_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 573 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
| 574 | }; |
| 575 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 576 | |
| 577 | struct PositionInfo { |
| 578 | PositionInfo(uint32_t address, uint32_t line) : address_(address), line_(line) { } |
| 579 | |
| 580 | uint32_t address_; |
| 581 | uint32_t line_; |
| 582 | }; |
| 583 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 584 | using PositionInfoVector = std::vector<std::unique_ptr<PositionInfo>>; |
| 585 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 586 | struct LocalInfo { |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 587 | LocalInfo(const char* name, |
| 588 | const char* descriptor, |
| 589 | const char* signature, |
| 590 | uint32_t start_address, |
| 591 | uint32_t end_address, |
| 592 | uint16_t reg) |
| 593 | : name_(name), |
| 594 | descriptor_(descriptor), |
| 595 | signature_(signature), |
| 596 | start_address_(start_address), |
| 597 | end_address_(end_address), |
| 598 | reg_(reg) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 599 | |
| 600 | std::string name_; |
| 601 | std::string descriptor_; |
| 602 | std::string signature_; |
| 603 | uint32_t start_address_; |
| 604 | uint32_t end_address_; |
| 605 | uint16_t reg_; |
| 606 | }; |
| 607 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 608 | using LocalInfoVector = std::vector<std::unique_ptr<LocalInfo>>; |
| 609 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 610 | class DebugInfoItem : public Item { |
| 611 | public: |
| 612 | DebugInfoItem() = default; |
| 613 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 614 | PositionInfoVector& GetPositionInfo() { return positions_; } |
| 615 | LocalInfoVector& GetLocalInfo() { return locals_; } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 616 | |
| 617 | private: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 618 | PositionInfoVector positions_; |
| 619 | LocalInfoVector locals_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 620 | DISALLOW_COPY_AND_ASSIGN(DebugInfoItem); |
| 621 | }; |
| 622 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 623 | class AnnotationItem { |
| 624 | public: |
| 625 | AnnotationItem(uint8_t visibility, ArrayItem* item) : visibility_(visibility), item_(item) { } |
| 626 | |
| 627 | uint8_t GetVisibility() const { return visibility_; } |
| 628 | ArrayItem* GetItem() const { return item_.get(); } |
| 629 | |
| 630 | private: |
| 631 | uint8_t visibility_; |
| 632 | std::unique_ptr<ArrayItem> item_; |
| 633 | DISALLOW_COPY_AND_ASSIGN(AnnotationItem); |
| 634 | }; |
| 635 | |
| 636 | using AnnotationItemVector = std::vector<std::unique_ptr<AnnotationItem>>; |
| 637 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 638 | class AnnotationSetItem : public Item { |
| 639 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 640 | explicit AnnotationSetItem(AnnotationItemVector* items) : items_(items) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 641 | ~AnnotationSetItem() OVERRIDE { } |
| 642 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 643 | AnnotationItemVector* GetItems() { return items_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 644 | |
| 645 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 646 | |
| 647 | private: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 648 | std::unique_ptr<AnnotationItemVector> items_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 649 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem); |
| 650 | }; |
| 651 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 652 | using AnnotationSetItemVector = std::vector<std::unique_ptr<AnnotationSetItem>>; |
| 653 | |
| 654 | class FieldAnnotation { |
| 655 | public: |
| 656 | FieldAnnotation(FieldId* field_id, AnnotationSetItem* annotation_set_item) |
| 657 | : field_id_(field_id), annotation_set_item_(annotation_set_item) { } |
| 658 | |
| 659 | FieldId* GetFieldId() const { return field_id_; } |
| 660 | AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_.get(); } |
| 661 | |
| 662 | private: |
| 663 | FieldId* field_id_; |
| 664 | std::unique_ptr<AnnotationSetItem> annotation_set_item_; |
| 665 | DISALLOW_COPY_AND_ASSIGN(FieldAnnotation); |
| 666 | }; |
| 667 | |
| 668 | using FieldAnnotationVector = std::vector<std::unique_ptr<FieldAnnotation>>; |
| 669 | |
| 670 | class MethodAnnotation { |
| 671 | public: |
| 672 | MethodAnnotation(MethodId* method_id, AnnotationSetItem* annotation_set_item) |
| 673 | : method_id_(method_id), annotation_set_item_(annotation_set_item) { } |
| 674 | |
| 675 | MethodId* GetMethodId() const { return method_id_; } |
| 676 | AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_.get(); } |
| 677 | |
| 678 | private: |
| 679 | MethodId* method_id_; |
| 680 | std::unique_ptr<AnnotationSetItem> annotation_set_item_; |
| 681 | DISALLOW_COPY_AND_ASSIGN(MethodAnnotation); |
| 682 | }; |
| 683 | |
| 684 | using MethodAnnotationVector = std::vector<std::unique_ptr<MethodAnnotation>>; |
| 685 | |
| 686 | class ParameterAnnotation { |
| 687 | public: |
| 688 | ParameterAnnotation(MethodId* method_id, AnnotationSetItemVector* annotations) |
| 689 | : method_id_(method_id), annotations_(annotations) { } |
| 690 | |
| 691 | MethodId* GetMethodId() const { return method_id_; } |
| 692 | AnnotationSetItemVector* GetAnnotations() { return annotations_.get(); } |
| 693 | |
| 694 | private: |
| 695 | MethodId* method_id_; |
| 696 | std::unique_ptr<AnnotationSetItemVector> annotations_; |
| 697 | DISALLOW_COPY_AND_ASSIGN(ParameterAnnotation); |
| 698 | }; |
| 699 | |
| 700 | using ParameterAnnotationVector = std::vector<std::unique_ptr<ParameterAnnotation>>; |
| 701 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 702 | class AnnotationsDirectoryItem : public Item { |
| 703 | public: |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 704 | AnnotationsDirectoryItem(AnnotationSetItem* class_annotation, |
| 705 | FieldAnnotationVector* field_annotations, |
| 706 | MethodAnnotationVector* method_annotations, |
| 707 | ParameterAnnotationVector* parameter_annotations) |
| 708 | : class_annotation_(class_annotation), |
| 709 | field_annotations_(field_annotations), |
| 710 | method_annotations_(method_annotations), |
| 711 | parameter_annotations_(parameter_annotations) { } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 712 | |
| 713 | AnnotationSetItem* GetClassAnnotation() const { return class_annotation_.get(); } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 714 | FieldAnnotationVector* GetFieldAnnotations() { return field_annotations_.get(); } |
| 715 | MethodAnnotationVector* GetMethodAnnotations() { return method_annotations_.get(); } |
| 716 | ParameterAnnotationVector* GetParameterAnnotations() { return parameter_annotations_.get(); } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 717 | |
| 718 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 719 | |
| 720 | private: |
| 721 | std::unique_ptr<AnnotationSetItem> class_annotation_; |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 722 | std::unique_ptr<FieldAnnotationVector> field_annotations_; |
| 723 | std::unique_ptr<MethodAnnotationVector> method_annotations_; |
| 724 | std::unique_ptr<ParameterAnnotationVector> parameter_annotations_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 725 | DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem); |
| 726 | }; |
| 727 | |
| 728 | // TODO(sehr): implement MapList. |
| 729 | class MapList : public Item { |
| 730 | public: |
| 731 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 732 | |
| 733 | private: |
| 734 | DISALLOW_COPY_AND_ASSIGN(MapList); |
| 735 | }; |
| 736 | |
| 737 | class MapItem : public Item { |
| 738 | public: |
| 739 | void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); } |
| 740 | |
| 741 | private: |
| 742 | DISALLOW_COPY_AND_ASSIGN(MapItem); |
| 743 | }; |
| 744 | |
| 745 | } // namespace dex_ir |
| 746 | } // namespace art |
| 747 | |
| 748 | #endif // ART_DEXLAYOUT_DEX_IR_H_ |