blob: 3b2135c2cda667d4d25d87f9387a656adf4c3ddf [file] [log] [blame]
Ian Rogers4f6ad8a2013-03-18 15:27:28 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_FILE_INL_H_
18#define ART_RUNTIME_DEX_FILE_INL_H_
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070019
20#include "base/logging.h"
21#include "dex_file.h"
22#include "leb128.h"
23#include "utils.h"
24
25namespace art {
26
27inline int32_t DexFile::GetStringLength(const StringId& string_id) const {
28 const byte* ptr = begin_ + string_id.string_data_off_;
29 return DecodeUnsignedLeb128(&ptr);
30}
31
Ian Rogersdfb325e2013-10-30 01:00:44 -070032inline const char* DexFile::GetStringDataAndUtf16Length(const StringId& string_id,
33 uint32_t* utf16_length) const {
34 DCHECK(utf16_length != NULL) << GetLocation();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035 const byte* ptr = begin_ + string_id.string_data_off_;
Ian Rogersdfb325e2013-10-30 01:00:44 -070036 *utf16_length = DecodeUnsignedLeb128(&ptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037 return reinterpret_cast<const char*>(ptr);
38}
39
Ian Rogersd91d6d62013-09-25 20:26:14 -070040inline const Signature DexFile::GetMethodSignature(const MethodId& method_id) const {
41 return Signature(this, GetProtoId(method_id.proto_idx_));
42}
43
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070044inline const DexFile::TryItem* DexFile::GetTryItems(const CodeItem& code_item, uint32_t offset) {
45 const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
46 return reinterpret_cast<const TryItem*>
47 (RoundUp(reinterpret_cast<uint32_t>(insns_end_), 4)) + offset;
48}
49
Ian Rogersdfb325e2013-10-30 01:00:44 -070050static inline bool DexFileStringEquals(const DexFile* df1, uint32_t sidx1,
51 const DexFile* df2, uint32_t sidx2) {
52 uint32_t s1_len; // Note: utf16 length != mutf8 length.
53 const char* s1_data = df1->StringDataAndUtf16LengthByIdx(sidx1, &s1_len);
54 uint32_t s2_len;
55 const char* s2_data = df2->StringDataAndUtf16LengthByIdx(sidx2, &s2_len);
56 return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0);
57}
58
59inline bool Signature::operator==(const Signature& rhs) const {
60 if (dex_file_ == nullptr) {
61 return rhs.dex_file_ == nullptr;
62 }
63 if (rhs.dex_file_ == nullptr) {
64 return false;
65 }
66 if (dex_file_ == rhs.dex_file_) {
67 return proto_id_ == rhs.proto_id_;
68 }
69 uint32_t lhs_shorty_len; // For a shorty utf16 length == mutf8 length.
70 const char* lhs_shorty_data = dex_file_->StringDataAndUtf16LengthByIdx(proto_id_->shorty_idx_,
71 &lhs_shorty_len);
72 StringPiece lhs_shorty(lhs_shorty_data, lhs_shorty_len);
73 {
74 uint32_t rhs_shorty_len;
75 const char* rhs_shorty_data =
76 rhs.dex_file_->StringDataAndUtf16LengthByIdx(rhs.proto_id_->shorty_idx_,
77 &rhs_shorty_len);
78 StringPiece rhs_shorty(rhs_shorty_data, rhs_shorty_len);
79 if (lhs_shorty != rhs_shorty) {
80 return false; // Shorty mismatch.
81 }
82 }
83 if (lhs_shorty[0] == 'L') {
84 const DexFile::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_);
85 const DexFile::TypeId& rhs_return_type_id =
86 rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_);
87 if (!DexFileStringEquals(dex_file_, return_type_id.descriptor_idx_,
88 rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) {
89 return false; // Return type mismatch.
90 }
91 }
92 if (lhs_shorty.find('L', 1) != StringPiece::npos) {
93 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
94 const DexFile::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_);
95 // Both lists are empty or have contents, or else shorty is broken.
96 DCHECK_EQ(params == nullptr, rhs_params == nullptr);
97 if (params != nullptr) {
98 uint32_t params_size = params->Size();
99 DCHECK_EQ(params_size, rhs_params->Size()); // Parameter list size must match.
100 for (uint32_t i = 0; i < params_size; ++i) {
101 const DexFile::TypeId& param_id = dex_file_->GetTypeId(params->GetTypeItem(i).type_idx_);
102 const DexFile::TypeId& rhs_param_id =
103 rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_);
104 if (!DexFileStringEquals(dex_file_, param_id.descriptor_idx_,
105 rhs.dex_file_, rhs_param_id.descriptor_idx_)) {
106 return false; // Parameter type mismatch.
107 }
108 }
109 }
110 }
111 return true;
112}
113
114
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700115} // namespace art
116
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700117#endif // ART_RUNTIME_DEX_FILE_INL_H_