1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_TYPE_TABLE_H_
6#define RUNTIME_VM_TYPE_TABLE_H_
7
8#include "platform/assert.h"
9#include "vm/hash_table.h"
10#include "vm/object.h"
11
12namespace dart {
13
14class CanonicalTypeKey {
15 public:
16 explicit CanonicalTypeKey(const Type& key) : key_(key) {}
17 bool Matches(const Type& arg) const { return key_.Equals(arg); }
18 uword Hash() const { return key_.Hash(); }
19 const Type& key_;
20
21 private:
22 DISALLOW_ALLOCATION();
23};
24
25// Traits for looking up Canonical Type based on its hash.
26class CanonicalTypeTraits {
27 public:
28 static const char* Name() { return "CanonicalTypeTraits"; }
29 static bool ReportStats() { return false; }
30
31 // Called when growing the table.
32 static bool IsMatch(const Object& a, const Object& b) {
33 ASSERT(a.IsType() && b.IsType());
34 const Type& arg1 = Type::Cast(a);
35 const Type& arg2 = Type::Cast(b);
36 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
37 }
38 static bool IsMatch(const CanonicalTypeKey& a, const Object& b) {
39 ASSERT(b.IsType());
40 return a.Matches(Type::Cast(b));
41 }
42 static uword Hash(const Object& key) {
43 ASSERT(key.IsType());
44 return Type::Cast(key).Hash();
45 }
46 static uword Hash(const CanonicalTypeKey& key) { return key.Hash(); }
47 static ObjectPtr NewKey(const CanonicalTypeKey& obj) {
48 return obj.key_.raw();
49 }
50};
51typedef UnorderedHashSet<CanonicalTypeTraits> CanonicalTypeSet;
52
53class CanonicalTypeParameterKey {
54 public:
55 explicit CanonicalTypeParameterKey(const TypeParameter& key) : key_(key) {}
56 bool Matches(const TypeParameter& arg) const { return key_.Equals(arg); }
57 uword Hash() const { return key_.Hash(); }
58 const TypeParameter& key_;
59
60 private:
61 DISALLOW_ALLOCATION();
62};
63
64// Traits for looking up Canonical TypeParameter based on its hash.
65class CanonicalTypeParameterTraits {
66 public:
67 static const char* Name() { return "CanonicalTypeParameterTraits"; }
68 static bool ReportStats() { return false; }
69
70 // Called when growing the table.
71 static bool IsMatch(const Object& a, const Object& b) {
72 ASSERT(a.IsTypeParameter() && b.IsTypeParameter());
73 const TypeParameter& arg1 = TypeParameter::Cast(a);
74 const TypeParameter& arg2 = TypeParameter::Cast(b);
75 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
76 }
77 static bool IsMatch(const CanonicalTypeParameterKey& a, const Object& b) {
78 ASSERT(b.IsTypeParameter());
79 return a.Matches(TypeParameter::Cast(b));
80 }
81 static uword Hash(const Object& key) {
82 ASSERT(key.IsTypeParameter());
83 return TypeParameter::Cast(key).Hash();
84 }
85 static uword Hash(const CanonicalTypeParameterKey& key) { return key.Hash(); }
86 static ObjectPtr NewKey(const CanonicalTypeParameterKey& obj) {
87 return obj.key_.raw();
88 }
89};
90typedef UnorderedHashSet<CanonicalTypeParameterTraits>
91 CanonicalTypeParameterSet;
92
93class CanonicalTypeArgumentsKey {
94 public:
95 explicit CanonicalTypeArgumentsKey(const TypeArguments& key) : key_(key) {}
96 bool Matches(const TypeArguments& arg) const {
97 return key_.Equals(arg) && (key_.Hash() == arg.Hash());
98 }
99 uword Hash() const { return key_.Hash(); }
100 const TypeArguments& key_;
101
102 private:
103 DISALLOW_ALLOCATION();
104};
105
106// Traits for looking up Canonical TypeArguments based on its hash.
107class CanonicalTypeArgumentsTraits {
108 public:
109 static const char* Name() { return "CanonicalTypeArgumentsTraits"; }
110 static bool ReportStats() { return false; }
111
112 // Called when growing the table.
113 static bool IsMatch(const Object& a, const Object& b) {
114 ASSERT(a.IsTypeArguments() && b.IsTypeArguments());
115 const TypeArguments& arg1 = TypeArguments::Cast(a);
116 const TypeArguments& arg2 = TypeArguments::Cast(b);
117 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
118 }
119 static bool IsMatch(const CanonicalTypeArgumentsKey& a, const Object& b) {
120 ASSERT(b.IsTypeArguments());
121 return a.Matches(TypeArguments::Cast(b));
122 }
123 static uword Hash(const Object& key) {
124 ASSERT(key.IsTypeArguments());
125 return TypeArguments::Cast(key).Hash();
126 }
127 static uword Hash(const CanonicalTypeArgumentsKey& key) { return key.Hash(); }
128 static ObjectPtr NewKey(const CanonicalTypeArgumentsKey& obj) {
129 return obj.key_.raw();
130 }
131};
132typedef UnorderedHashSet<CanonicalTypeArgumentsTraits>
133 CanonicalTypeArgumentsSet;
134
135} // namespace dart
136
137#endif // RUNTIME_VM_TYPE_TABLE_H_
138