Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
canonical_tables.h
Go to the documentation of this file.
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_CANONICAL_TABLES_H_
6#define RUNTIME_VM_CANONICAL_TABLES_H_
7
8#include "platform/assert.h"
9#include "vm/hash_table.h"
10#include "vm/object.h"
11
12namespace dart {
13
14template <typename CharType>
15class CharArray {
16 public:
17 CharArray(const CharType* data, intptr_t len) : data_(data), len_(len) {
18 hash_ = String::Hash(data, len);
19 }
20 StringPtr ToSymbol() const {
22 result.SetCanonical();
23 result.SetHash(hash_);
24 return result.ptr();
25 }
26 bool Equals(const String& other) const {
27 ASSERT(other.HasHash());
28 if (other.Hash() != hash_) {
29 return false;
30 }
31 return other.Equals(data_, len_);
32 }
33 uword Hash() const { return hash_; }
34
35 private:
36 const CharType* data_;
37 intptr_t len_;
38 uword hash_;
39};
43
45 public:
46 StringSlice(const String& str, intptr_t begin_index, intptr_t length)
47 : str_(str), begin_index_(begin_index), len_(length) {
48 hash_ = is_all() ? str.Hash() : String::Hash(str, begin_index, length);
49 }
50 StringPtr ToSymbol() const;
51 bool Equals(const String& other) const {
52 ASSERT(other.HasHash());
53 if (other.Hash() != hash_) {
54 return false;
55 }
56 return other.Equals(str_, begin_index_, len_);
57 }
58 uword Hash() const { return hash_; }
59
60 private:
61 bool is_all() const { return begin_index_ == 0 && len_ == str_.Length(); }
62 const String& str_;
63 intptr_t begin_index_;
64 intptr_t len_;
65 uword hash_;
66};
67
69 public:
70 ConcatString(const String& str1, const String& str2)
71 : str1_(str1), str2_(str2), hash_(String::HashConcat(str1, str2)) {}
72 StringPtr ToSymbol() const;
73 bool Equals(const String& other) const {
74 ASSERT(other.HasHash());
75 if (other.Hash() != hash_) {
76 return false;
77 }
78 return other.EqualsConcat(str1_, str2_);
79 }
80 uword Hash() const { return hash_; }
81
82 private:
83 const String& str1_;
84 const String& str2_;
85 uword hash_;
86};
87
89 public:
90 static const char* Name() { return "SymbolTraits"; }
91 static bool ReportStats() { return false; }
92
93 static bool IsMatch(const Object& a, const Object& b) {
94 const String& a_str = String::Cast(a);
95 const String& b_str = String::Cast(b);
96 ASSERT(a_str.HasHash());
97 ASSERT(b_str.HasHash());
98 if (a_str.Hash() != b_str.Hash()) {
99 return false;
100 }
101 intptr_t a_len = a_str.Length();
102 if (a_len != b_str.Length()) {
103 return false;
104 }
105 // Use a comparison which does not consider the state of the canonical bit.
106 return a_str.Equals(b_str, 0, a_len);
107 }
108 template <typename CharType>
109 static bool IsMatch(const CharArray<CharType>& array, const Object& obj) {
110 return array.Equals(String::Cast(obj));
111 }
112 static bool IsMatch(const StringSlice& slice, const Object& obj) {
113 return slice.Equals(String::Cast(obj));
114 }
115 static bool IsMatch(const ConcatString& concat, const Object& obj) {
116 return concat.Equals(String::Cast(obj));
117 }
118 static uword Hash(const Object& key) { return String::Cast(key).Hash(); }
119 template <typename CharType>
120 static uword Hash(const CharArray<CharType>& array) {
121 return array.Hash();
122 }
123 static uword Hash(const StringSlice& slice) { return slice.Hash(); }
124 static uword Hash(const ConcatString& concat) { return concat.Hash(); }
125 template <typename CharType>
126 static ObjectPtr NewKey(const CharArray<CharType>& array) {
127 return array.ToSymbol();
128 }
129 static ObjectPtr NewKey(const StringSlice& slice) { return slice.ToSymbol(); }
130 static ObjectPtr NewKey(const ConcatString& concat) {
131 return concat.ToSymbol();
132 }
133};
134
135typedef UnorderedHashSet<SymbolTraits, WeakAcqRelStorageTraits>
137
139 public:
140 explicit CanonicalTypeKey(const Type& key) : key_(key) {}
141 bool Matches(const Type& arg) const { return key_.Equals(arg); }
142 uword Hash() const { return key_.Hash(); }
143 const Type& key_;
144
145 private:
146 DISALLOW_ALLOCATION();
147};
148
149// Traits for looking up Canonical Type based on its hash.
151 public:
152 static const char* Name() { return "CanonicalTypeTraits"; }
153 static bool ReportStats() { return false; }
154
155 // Called when growing the table.
156 static bool IsMatch(const Object& a, const Object& b) {
157 ASSERT(a.IsType() && b.IsType());
158 const Type& arg1 = Type::Cast(a);
159 const Type& arg2 = Type::Cast(b);
160 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
161 }
162 static bool IsMatch(const CanonicalTypeKey& a, const Object& b) {
163 ASSERT(b.IsType());
164 return a.Matches(Type::Cast(b));
165 }
166 static uword Hash(const Object& key) {
167 ASSERT(key.IsType());
168 return Type::Cast(key).Hash();
169 }
170 static uword Hash(const CanonicalTypeKey& key) { return key.Hash(); }
171 static ObjectPtr NewKey(const CanonicalTypeKey& obj) {
172 return obj.key_.ptr();
173 }
174};
176
178 public:
180 bool Matches(const FunctionType& arg) const { return key_.Equals(arg); }
181 uword Hash() const { return key_.Hash(); }
183
184 private:
185 DISALLOW_ALLOCATION();
186};
187
188// Traits for looking up Canonical FunctionType based on its hash.
190 public:
191 static const char* Name() { return "CanonicalFunctionTypeTraits"; }
192 static bool ReportStats() { return false; }
193
194 // Called when growing the table.
195 static bool IsMatch(const Object& a, const Object& b) {
196 ASSERT(a.IsFunctionType() && b.IsFunctionType());
197 const FunctionType& arg1 = FunctionType::Cast(a);
198 const FunctionType& arg2 = FunctionType::Cast(b);
199 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
200 }
201 static bool IsMatch(const CanonicalFunctionTypeKey& a, const Object& b) {
202 ASSERT(b.IsFunctionType());
203 return a.Matches(FunctionType::Cast(b));
204 }
205 static uword Hash(const Object& key) {
206 ASSERT(key.IsFunctionType());
207 return FunctionType::Cast(key).Hash();
208 }
209 static uword Hash(const CanonicalFunctionTypeKey& key) { return key.Hash(); }
211 return obj.key_.ptr();
212 }
213};
215
217 public:
219 bool Matches(const RecordType& arg) const { return key_.Equals(arg); }
220 uword Hash() const { return key_.Hash(); }
222
223 private:
224 DISALLOW_ALLOCATION();
225};
226
227// Traits for looking up Canonical RecordType based on its hash.
229 public:
230 static const char* Name() { return "CanonicalRecordTypeTraits"; }
231 static bool ReportStats() { return false; }
232
233 // Called when growing the table.
234 static bool IsMatch(const Object& a, const Object& b) {
235 ASSERT(a.IsRecordType() && b.IsRecordType());
236 const RecordType& arg1 = RecordType::Cast(a);
237 const RecordType& arg2 = RecordType::Cast(b);
238 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
239 }
240 static bool IsMatch(const CanonicalRecordTypeKey& a, const Object& b) {
241 ASSERT(b.IsRecordType());
242 return a.Matches(RecordType::Cast(b));
243 }
244 static uword Hash(const Object& key) {
245 ASSERT(key.IsRecordType());
246 return RecordType::Cast(key).Hash();
247 }
248 static uword Hash(const CanonicalRecordTypeKey& key) { return key.Hash(); }
250 return obj.key_.ptr();
251 }
252};
254
256 public:
258 bool Matches(const TypeParameter& arg) const { return key_.Equals(arg); }
259 uword Hash() const { return key_.Hash(); }
261
262 private:
263 DISALLOW_ALLOCATION();
264};
265
266// Traits for looking up Canonical TypeParameter based on its hash.
268 public:
269 static const char* Name() { return "CanonicalTypeParameterTraits"; }
270 static bool ReportStats() { return false; }
271
272 // Called when growing the table.
273 static bool IsMatch(const Object& a, const Object& b) {
274 ASSERT(a.IsTypeParameter() && b.IsTypeParameter());
275 const TypeParameter& arg1 = TypeParameter::Cast(a);
276 const TypeParameter& arg2 = TypeParameter::Cast(b);
277 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
278 }
279 static bool IsMatch(const CanonicalTypeParameterKey& a, const Object& b) {
280 ASSERT(b.IsTypeParameter());
281 return a.Matches(TypeParameter::Cast(b));
282 }
283 static uword Hash(const Object& key) {
284 ASSERT(key.IsTypeParameter());
285 return TypeParameter::Cast(key).Hash();
286 }
287 static uword Hash(const CanonicalTypeParameterKey& key) { return key.Hash(); }
289 return obj.key_.ptr();
290 }
291};
292typedef UnorderedHashSet<CanonicalTypeParameterTraits>
294
296 public:
298 bool Matches(const TypeArguments& arg) const {
299 return key_.Equals(arg) && (key_.Hash() == arg.Hash());
300 }
301 uword Hash() const { return key_.Hash(); }
303
304 private:
305 DISALLOW_ALLOCATION();
306};
307
308// Traits for looking up Canonical TypeArguments based on its hash.
310 public:
311 static const char* Name() { return "CanonicalTypeArgumentsTraits"; }
312 static bool ReportStats() { return false; }
313
314 // Called when growing the table.
315 static bool IsMatch(const Object& a, const Object& b) {
316 ASSERT(a.IsTypeArguments() && b.IsTypeArguments());
317 const TypeArguments& arg1 = TypeArguments::Cast(a);
318 const TypeArguments& arg2 = TypeArguments::Cast(b);
319 return arg1.Equals(arg2) && (arg1.Hash() == arg2.Hash());
320 }
321 static bool IsMatch(const CanonicalTypeArgumentsKey& a, const Object& b) {
322 ASSERT(b.IsTypeArguments());
323 return a.Matches(TypeArguments::Cast(b));
324 }
325 static uword Hash(const Object& key) {
326 ASSERT(key.IsTypeArguments());
327 return TypeArguments::Cast(key).Hash();
328 }
329 static uword Hash(const CanonicalTypeArgumentsKey& key) { return key.Hash(); }
331 return obj.key_.ptr();
332 }
333};
334typedef UnorderedHashSet<CanonicalTypeArgumentsTraits>
336
338 public:
339 static const char* Name() { return "MetadataMapTraits"; }
340 static bool ReportStats() { return false; }
341 static bool IsMatch(const Object& a, const Object& b);
342 static uword Hash(const Object& key);
343};
345
347 public:
349 const Array& args_desc,
351 : name_(name), args_desc_(args_desc), kind_(kind) {}
352 bool Equals(const Function& other) const {
353 return (name_.ptr() == other.name()) &&
354 (args_desc_.ptr() == other.saved_args_desc()) &&
355 (kind_ == other.kind());
356 }
357 uword Hash() const { return CombineHashes(name_.Hash(), kind_); }
358
359 private:
360 const String& name_;
361 const Array& args_desc_;
363};
364
366 public:
367 static const char* Name() { return "DispatcherTraits"; }
368 static bool ReportStats() { return false; }
369
370 // Called when growing the table.
371 static bool IsMatch(const Object& a, const Object& b) {
372 const Function& a_func = Function::Cast(a);
373 const Function& b_func = Function::Cast(b);
374 return (a_func.name() == b_func.name()) &&
375 (a_func.kind() == b_func.kind()) &&
376 (a_func.saved_args_desc() == b_func.saved_args_desc());
377 }
378 static bool IsMatch(const DispatcherKey& key, const Object& obj) {
379 return key.Equals(Function::Cast(obj));
380 }
381 static uword Hash(const Object& key) {
382 const Function& func = Function::Cast(key);
383 return CombineHashes(String::Hash(func.name()), func.kind());
384 }
385 static uword Hash(const DispatcherKey& key) { return key.Hash(); }
387};
388
390
392 public:
393 explicit CanonicalInstanceKey(const Instance& key);
394 bool Matches(const Instance& obj) const;
395 uword Hash() const;
397
398 private:
399 DISALLOW_ALLOCATION();
400};
401
402// Traits for looking up Canonical Instances based on a hash of the fields.
404 public:
405 static const char* Name() { return "CanonicalInstanceTraits"; }
406 static bool ReportStats() { return false; }
407
408 // Called when growing the table.
409 static bool IsMatch(const Object& a, const Object& b);
410 static bool IsMatch(const CanonicalInstanceKey& a, const Object& b);
411 static uword Hash(const Object& key);
412 static uword Hash(const CanonicalInstanceKey& key);
413 static ObjectPtr NewKey(const CanonicalInstanceKey& obj);
414};
415
417
419 static uint32_t Hash(const Object& key) { return Function::Cast(key).Hash(); }
420 static const char* Name() { return "CanonicalFfiCallbackFunctionTraits"; }
421 static bool IsMatch(const Object& x, const Object& y) {
422 const auto& f1 = Function::Cast(x);
423 const auto& f2 = Function::Cast(y);
424 return (f1.FfiCallbackTarget() == f2.FfiCallbackTarget() &&
425 f1.FfiCSignature() == f2.FfiCSignature() &&
426 f1.FfiCallbackExceptionalReturn() ==
427 f2.FfiCallbackExceptionalReturn() &&
428 f1.GetFfiCallbackKind() == f2.GetFfiCallbackKind());
429 }
430 static bool ReportStats() { return false; }
431};
432
435
437 public:
439 : pattern_(pattern), flags_(flags) {}
440
441 bool Equals(const RegExp& other) const {
442 return pattern_.Equals(String::Handle(other.pattern())) &&
443 (flags_ == other.flags());
444 }
445 uword Hash() const {
446 // Must agree with RegExp::CanonicalizeHash.
448 }
449
452
453 private:
454 DISALLOW_ALLOCATION();
455};
456
458 public:
459 static const char* Name() { return "CanonicalRegExpTraits"; }
460 static bool ReportStats() { return false; }
461 static bool IsMatch(const Object& a, const Object& b) {
462 return RegExp::Cast(a).CanonicalizeEquals(RegExp::Cast(b));
463 }
464 static bool IsMatch(const RegExpKey& a, const Object& b) {
465 return a.Equals(RegExp::Cast(b));
466 }
467 static uword Hash(const Object& key) {
468 return RegExp::Cast(key).CanonicalizeHash();
469 }
470 static uword Hash(const RegExpKey& key) { return key.Hash(); }
471 static ObjectPtr NewKey(const RegExpKey& key);
472};
473
474typedef UnorderedHashSet<CanonicalRegExpTraits, WeakAcqRelStorageTraits>
476
477} // namespace dart
478
479#endif // RUNTIME_VM_CANONICAL_TABLES_H_
#define UNREACHABLE()
Definition assert.h:248
virtual bool Equals(const Instance &other) const
Definition object.h:9074
uword Hash() const
Definition object.h:13351
bool Matches(const FunctionType &arg) const
CanonicalFunctionTypeKey(const FunctionType &key)
static uword Hash(const Object &key)
static bool IsMatch(const Object &a, const Object &b)
static ObjectPtr NewKey(const CanonicalFunctionTypeKey &obj)
static uword Hash(const CanonicalFunctionTypeKey &key)
static bool IsMatch(const CanonicalFunctionTypeKey &a, const Object &b)
bool Matches(const Instance &obj) const
static bool IsMatch(const Object &a, const Object &b)
static ObjectPtr NewKey(const CanonicalInstanceKey &obj)
static uword Hash(const Object &key)
CanonicalRecordTypeKey(const RecordType &key)
bool Matches(const RecordType &arg) const
static bool IsMatch(const Object &a, const Object &b)
static uword Hash(const Object &key)
static uword Hash(const CanonicalRecordTypeKey &key)
static ObjectPtr NewKey(const CanonicalRecordTypeKey &obj)
static bool IsMatch(const CanonicalRecordTypeKey &a, const Object &b)
static bool IsMatch(const Object &a, const Object &b)
static uword Hash(const RegExpKey &key)
static bool IsMatch(const RegExpKey &a, const Object &b)
static const char * Name()
static ObjectPtr NewKey(const RegExpKey &key)
static uword Hash(const Object &key)
bool Matches(const TypeArguments &arg) const
CanonicalTypeArgumentsKey(const TypeArguments &key)
static uword Hash(const CanonicalTypeArgumentsKey &key)
static bool IsMatch(const CanonicalTypeArgumentsKey &a, const Object &b)
static bool IsMatch(const Object &a, const Object &b)
static ObjectPtr NewKey(const CanonicalTypeArgumentsKey &obj)
static uword Hash(const Object &key)
CanonicalTypeKey(const Type &key)
bool Matches(const Type &arg) const
CanonicalTypeParameterKey(const TypeParameter &key)
bool Matches(const TypeParameter &arg) const
static ObjectPtr NewKey(const CanonicalTypeParameterKey &obj)
static uword Hash(const Object &key)
static bool IsMatch(const CanonicalTypeParameterKey &a, const Object &b)
static uword Hash(const CanonicalTypeParameterKey &key)
static bool IsMatch(const Object &a, const Object &b)
static ObjectPtr NewKey(const CanonicalTypeKey &obj)
static bool IsMatch(const CanonicalTypeKey &a, const Object &b)
static bool IsMatch(const Object &a, const Object &b)
static const char * Name()
static uword Hash(const Object &key)
static uword Hash(const CanonicalTypeKey &key)
StringPtr ToSymbol() const
CharArray(const CharType *data, intptr_t len)
bool Equals(const String &other) const
uword Hash() const
bool Equals(const String &other) const
StringPtr ToSymbol() const
Definition symbols.cc:59
ConcatString(const String &str1, const String &str2)
DispatcherKey(const String &name, const Array &args_desc, UntaggedFunction::Kind kind)
bool Equals(const Function &other) const
static uword Hash(const DispatcherKey &key)
static const char * Name()
static bool IsMatch(const DispatcherKey &key, const Object &obj)
static bool IsMatch(const Object &a, const Object &b)
static ObjectPtr NewKey(const DispatcherKey &key)
static uword Hash(const Object &key)
StringPtr name() const
Definition object.h:2972
UntaggedFunction::Kind kind() const
Definition object.h:3329
ArrayPtr saved_args_desc() const
Definition object.cc:8191
@ kOld
Definition heap.h:39
static uword Hash(const Object &key)
static bool IsMatch(const Object &a, const Object &b)
static const char * Name()
ObjectPtr ptr() const
Definition object.h:332
static Object & Handle()
Definition object.h:407
int value() const
Definition object.h:12716
const String & pattern_
bool Equals(const RegExp &other) const
uword Hash() const
RegExpKey(const String &pattern, RegExpFlags flags)
StringPtr pattern() const
Definition object.h:12771
RegExpFlags flags() const
Definition object.h:12865
bool Equals(const String &other) const
StringPtr ToSymbol() const
Definition symbols.cc:46
StringSlice(const String &str, intptr_t begin_index, intptr_t length)
intptr_t Length() const
Definition object.h:10189
bool HasHash() const
Definition object.h:10208
bool Equals(const String &str) const
Definition object.h:13311
uword Hash() const
Definition object.h:10195
bool EqualsConcat(const String &str1, const String &str2) const
Definition object.cc:23711
static uword Hash(const ConcatString &concat)
static ObjectPtr NewKey(const CharArray< CharType > &array)
static const char * Name()
static ObjectPtr NewKey(const ConcatString &concat)
static uword Hash(const StringSlice &slice)
static uword Hash(const Object &key)
static ObjectPtr NewKey(const StringSlice &slice)
static bool ReportStats()
static uword Hash(const CharArray< CharType > &array)
static bool IsMatch(const StringSlice &slice, const Object &obj)
static bool IsMatch(const ConcatString &concat, const Object &obj)
static bool IsMatch(const Object &a, const Object &b)
static bool IsMatch(const CharArray< CharType > &array, const Object &obj)
uword Hash() const
Definition object.h:13370
bool Equals(const TypeArguments &other) const
Definition object.h:8660
#define ASSERT(E)
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
GAsyncResult * result
size_t length
double y
double x
UnorderedHashSet< CanonicalFunctionTypeTraits > CanonicalFunctionTypeSet
UnorderedHashSet< CanonicalRegExpTraits, WeakAcqRelStorageTraits > CanonicalRegExpSet
const char *const name
UnorderedHashSet< CanonicalTypeArgumentsTraits > CanonicalTypeArgumentsSet
UnorderedHashSet< CanonicalTypeParameterTraits > CanonicalTypeParameterSet
uint32_t CombineHashes(uint32_t hash, uint32_t other_hash)
Definition hash.h:12
CharArray< uint8_t > Latin1Array
UnorderedHashSet< CanonicalInstanceTraits > CanonicalInstancesSet
uintptr_t uword
Definition globals.h:501
CharArray< uint16_t > UTF16Array
UnorderedHashSet< CanonicalTypeTraits > CanonicalTypeSet
StringPtr StringFrom(const uint8_t *data, intptr_t len, Heap::Space space)
Definition symbols.cc:38
CharArray< int32_t > UTF32Array
UnorderedHashSet< DispatcherTraits, AcqRelStorageTraits > DispatcherSet
UnorderedHashMap< MetadataMapTraits > MetadataMap
static int8_t data[kExtLength]
UnorderedHashSet< SymbolTraits, WeakAcqRelStorageTraits > CanonicalStringSet
UnorderedHashSet< CanonicalRecordTypeTraits > CanonicalRecordTypeSet
static bool IsMatch(const Object &x, const Object &y)
static uint32_t Hash(const Object &key)