• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_VERIFICATION_TYPE_TYPE_INFO_H_
17 #define PANDA_VERIFICATION_TYPE_TYPE_INFO_H_
18 
19 #include "verification/util/lazy.h"
20 #include "type_sort.h"
21 #include "type_index.h"
22 
23 namespace panda::verifier {
24 class TypeInfo {
25 public:
TypeInfo(SortIdx sort,const TypeParamsIdx & params)26     TypeInfo(SortIdx sort, const TypeParamsIdx &params) : Sort_(sort), ParamsIdx_(params) {}
TypeInfo(SortIdx sort,TypeParamsIdx && params)27     TypeInfo(SortIdx sort, TypeParamsIdx &&params) : Sort_(sort), ParamsIdx_(std::move(params)) {}
28     ~TypeInfo() = default;
29     bool operator==(const TypeInfo &rhs) const
30     {
31         return Sort_ == rhs.Sort_ && ParamsIdx_ == rhs.ParamsIdx_;
32     }
Arity()33     size_t Arity() const
34     {
35         return ParamsIdx_.size();
36     }
Sort()37     const SortIdx &Sort() const
38     {
39         return Sort_;
40     }
ParamsIdx()41     const TypeParamsIdx &ParamsIdx() const
42     {
43         return ParamsIdx_;
44     }
45 
46 private:
47     SortIdx Sort_;
48     TypeParamsIdx ParamsIdx_;
49 };
50 }  // namespace panda::verifier
51 
52 namespace std {
53 template <>
54 struct hash<panda::verifier::TypeInfo> {
55     size_t operator()(const panda::verifier::TypeInfo &ti) const
56     {
57         size_t result = ti.Sort();
58         auto hash_func = [&result](size_t v) {
59 #ifdef PANDA_TARGET_64
60             result ^= (v ^ (v << 17U)) + (v << 39U);      // NOLINT(readability-magic-numbers)
61             result ^= (result >> 33U) | (result << 31U);  // NOLINT(readability-magic-numbers)
62             result *= 0xff51afd7ed558ccdULL;              // NOLINT(readability-magic-numbers)
63             result ^= (result >> 33U) | (result << 31U);  // NOLINT(readability-magic-numbers)
64             result *= 0xc4ceb9fe1a85ec53ULL;              // NOLINT(readability-magic-numbers)
65             result ^= (result >> 33U) | (result << 31U);  // NOLINT(readability-magic-numbers)
66 #else
67             result ^= (v ^ (v << 9U)) + (v << 25U);       // NOLINT(readability-magic-numbers)
68             result ^= (result >> 17U) | (result << 16U);  // NOLINT(readability-magic-numbers)
69             result *= 0xed558ccdUL;                       // NOLINT(readability-magic-numbers)
70             result ^= (result >> 17U) | (result << 16U);  // NOLINT(readability-magic-numbers)
71             result *= 0x1a85ec53UL;                       // NOLINT(readability-magic-numbers)
72             result ^= (result >> 17U) | (result << 16U);  // NOLINT(readability-magic-numbers)
73 #endif
74         };
75         hash_func(ti.ParamsIdx().size());
76         for (const auto &v : ti.ParamsIdx()) {
77             hash_func(v.GetIndex());
78             hash_func(static_cast<size_t>(v.Variance()));
79         }
80         return result;
81     }
82 };
83 }  // namespace std
84 
85 #endif  // PANDA_VERIFICATION_TYPE_TYPE_INFO_H_
86