Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_METHOD_REFERENCE_H_ |
| 18 | #define ART_RUNTIME_METHOD_REFERENCE_H_ |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 19 | |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 20 | #include <stdint.h> |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include "dex_file.h" |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 23 | #include "dex_file_reference.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 24 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 27 | // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 28 | class MethodReference : public DexFileReference { |
| 29 | public: |
| 30 | MethodReference(const DexFile* file, uint32_t index) : DexFileReference(file, index) {} |
| 31 | std::string PrettyMethod(bool with_signature = true) const { |
| 32 | return dex_file->PrettyMethod(index, with_signature); |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 33 | } |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 34 | const DexFile::MethodId& GetMethodId() const { |
| 35 | return dex_file->GetMethodId(index); |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 39 | // Compare the actual referenced method signatures. Used for method reference deduplication. |
| 40 | struct MethodReferenceValueComparator { |
| 41 | bool operator()(MethodReference mr1, MethodReference mr2) const { |
| 42 | if (mr1.dex_file == mr2.dex_file) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 43 | DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2)); |
| 44 | return mr1.index < mr2.index; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 45 | } else { |
| 46 | return SlowCompare(mr1, mr2); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bool SlowCompare(MethodReference mr1, MethodReference mr2) const { |
| 51 | // The order is the same as for method ids in a single dex file. |
| 52 | // Compare the class descriptors first. |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 53 | const DexFile::MethodId& mid1 = mr1.GetMethodId(); |
| 54 | const DexFile::MethodId& mid2 = mr2.GetMethodId(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 55 | int descriptor_diff = strcmp(mr1.dex_file->StringByTypeIdx(mid1.class_idx_), |
| 56 | mr2.dex_file->StringByTypeIdx(mid2.class_idx_)); |
| 57 | if (descriptor_diff != 0) { |
| 58 | return descriptor_diff < 0; |
| 59 | } |
| 60 | // Compare names second. |
| 61 | int name_diff = strcmp(mr1.dex_file->GetMethodName(mid1), mr2.dex_file->GetMethodName(mid2)); |
| 62 | if (name_diff != 0) { |
| 63 | return name_diff < 0; |
| 64 | } |
| 65 | // And then compare proto ids, starting with return type comparison. |
| 66 | const DexFile::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_); |
| 67 | const DexFile::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_); |
| 68 | int return_type_diff = strcmp(mr1.dex_file->StringByTypeIdx(prid1.return_type_idx_), |
| 69 | mr2.dex_file->StringByTypeIdx(prid2.return_type_idx_)); |
| 70 | if (return_type_diff != 0) { |
| 71 | return return_type_diff < 0; |
| 72 | } |
| 73 | // And finishing with lexicographical parameter comparison. |
| 74 | const DexFile::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1); |
| 75 | size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u; |
| 76 | const DexFile::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2); |
| 77 | size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u; |
| 78 | for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) { |
| 79 | int param_diff = strcmp(mr1.dex_file->StringByTypeIdx(params1->GetTypeItem(i).type_idx_), |
| 80 | mr2.dex_file->StringByTypeIdx(params2->GetTypeItem(i).type_idx_)); |
| 81 | if (param_diff != 0) { |
| 82 | return param_diff < 0; |
| 83 | } |
| 84 | } |
| 85 | return param1_size < param2_size; |
| 86 | } |
| 87 | }; |
| 88 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 89 | } // namespace art |
| 90 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 91 | #endif // ART_RUNTIME_METHOD_REFERENCE_H_ |