summaryrefslogtreecommitdiff
path: root/libdexfile/dex/proto_reference.h
blob: dc9a447e63ffa0310c6ec27bcdf67fce041797a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_LIBDEXFILE_DEX_PROTO_REFERENCE_H_
#define ART_LIBDEXFILE_DEX_PROTO_REFERENCE_H_

#include <stdint.h>

#include <android-base/logging.h>
#include <string_view>

#include "dex/dex_file-inl.h"
#include "dex/dex_file_reference.h"
#include "dex/dex_file_types.h"

namespace art {

// A proto is located by its DexFile and the proto_ids_ table index into that DexFile.
class ProtoReference : public DexFileReference {
 public:
  ProtoReference(const DexFile* file, dex::ProtoIndex index)
     : DexFileReference(file, index.index_) {}

  dex::ProtoIndex ProtoIndex() const {
    return dex::ProtoIndex(index);
  }

  const dex::ProtoId& ProtoId() const {
    return dex_file->GetProtoId(ProtoIndex());
  }

  std::string_view ReturnType() const {
    return dex_file->GetTypeDescriptorView(dex_file->GetTypeId(ProtoId().return_type_idx_));
  }
};

struct ProtoReferenceValueComparator {
  bool operator()(const ProtoReference& lhs, const ProtoReference& rhs) const {
    if (lhs.dex_file == rhs.dex_file) {
      DCHECK_EQ(lhs.index < rhs.index, SlowCompare(lhs, rhs));

      return lhs.index < rhs.index;
    } else {
      return SlowCompare(lhs, rhs);
    }
  }

  bool SlowCompare(const ProtoReference& lhs, const ProtoReference& rhs) const {
    // Compare return type first.
    const dex::ProtoId& prid1 = lhs.ProtoId();
    const dex::ProtoId& prid2 = rhs.ProtoId();
    int return_type_diff = lhs.ReturnType().compare(rhs.ReturnType());
    if (return_type_diff != 0) {
      return return_type_diff < 0;
    }
    // And then compare parameters lexicographically.
    const dex::TypeList* params1 = lhs.dex_file->GetProtoParameters(prid1);
    size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
    const dex::TypeList* params2 = rhs.dex_file->GetProtoParameters(prid2);
    size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u;
    for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) {
      std::string_view l_param = lhs.dex_file->GetTypeDescriptorView(
          lhs.dex_file->GetTypeId(params1->GetTypeItem(i).type_idx_));
      std::string_view r_param = rhs.dex_file->GetTypeDescriptorView(
          rhs.dex_file->GetTypeId(params2->GetTypeItem(i).type_idx_));

      int param_diff = l_param.compare(r_param);
      if (param_diff != 0) {
        return param_diff < 0;
      }
    }
    return param1_size < param2_size;
  }
};

}  // namespace art

#endif  // ART_LIBDEXFILE_DEX_PROTO_REFERENCE_H_