v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
type-oracle.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_TORQUE_TYPE_ORACLE_H_
6#define V8_TORQUE_TYPE_ORACLE_H_
7
8#include <memory>
9#include <optional>
10
11#include "src/base/contextual.h"
15#include "src/torque/types.h"
16#include "src/torque/utils.h"
17
18namespace v8::internal::torque {
19
20class TypeOracle : public base::ContextualClass<TypeOracle> {
21 public:
23 const Type* parent, std::string name, AbstractTypeFlags flags,
24 std::string generated, const Type* non_constexpr_version,
25 MaybeSpecializationKey specialized_from) {
26 auto ptr = std::unique_ptr<AbstractType>(
27 new AbstractType(parent, flags, std::move(name), std::move(generated),
28 non_constexpr_version, specialized_from));
29 const AbstractType* result = ptr.get();
30 if (non_constexpr_version) {
31 DCHECK(ptr->IsConstexpr());
32 non_constexpr_version->SetConstexprVersion(result);
33 }
34 Get().nominal_types_.push_back(std::move(ptr));
35 return result;
36 }
37
39 MaybeSpecializationKey specialized_from) {
40 auto ptr = std::unique_ptr<StructType>(
41 new StructType(CurrentNamespace(), decl, specialized_from));
42 StructType* result = ptr.get();
43 Get().aggregate_types_.push_back(std::move(ptr));
44 return result;
45 }
46
48 const Type* parent, const BitFieldStructDeclaration* decl) {
49 auto ptr = std::unique_ptr<BitFieldStructType>(
50 new BitFieldStructType(CurrentNamespace(), parent, decl));
51 BitFieldStructType* result = ptr.get();
52 Get().bit_field_struct_types_.push_back(std::move(ptr));
53 return result;
54 }
55
56 static ClassType* GetClassType(const Type* parent, const std::string& name,
57 ClassFlags flags, const std::string& generates,
58 ClassDeclaration* decl,
59 const TypeAlias* alias) {
60 std::unique_ptr<ClassType> type(new ClassType(
61 parent, CurrentNamespace(), name, flags, generates, decl, alias));
62 ClassType* result = type.get();
63 Get().aggregate_types_.push_back(std::move(type));
64 return result;
65 }
66
68 TypeVector argument_types, const Type* return_type) {
69 TypeOracle& self = Get();
70 const Type* builtin_type = self.GetBuiltinType(BUILTIN_POINTER_TYPE_STRING);
72 BuiltinPointerType(builtin_type, std::move(argument_types), return_type,
73 self.all_builtin_pointer_types_.size()));
74 if (result->function_pointer_type_id() ==
75 self.all_builtin_pointer_types_.size()) {
76 self.all_builtin_pointer_types_.push_back(result);
77 }
78 return result;
79 }
80
81 static const Type* GetGenericTypeInstance(GenericType* generic_type,
82 TypeVector arg_types);
83
91 return GetReferenceGeneric(true);
92 }
96
97 static std::optional<const Type*> MatchReferenceGeneric(
98 const Type* reference_type, bool* is_const = nullptr);
99
108
112
116
120
121 static const Type* GetReferenceType(const Type* referenced_type,
122 bool is_const) {
124 {referenced_type});
125 }
126 static const Type* GetConstReferenceType(const Type* referenced_type) {
127 return GetReferenceType(referenced_type, true);
128 }
129 static const Type* GetMutableReferenceType(const Type* referenced_type) {
130 return GetReferenceType(referenced_type, false);
131 }
132
133 static const Type* GetMutableSliceType(const Type* referenced_type) {
134 return GetGenericTypeInstance(GetMutableSliceGeneric(), {referenced_type});
135 }
136 static const Type* GetConstSliceType(const Type* referenced_type) {
137 return GetGenericTypeInstance(GetConstSliceGeneric(), {referenced_type});
138 }
139
140 static const std::vector<const BuiltinPointerType*>&
142 return Get().all_builtin_pointer_types_;
143 }
144
145 static const Type* GetUnionType(UnionType type) {
146 if (std::optional<const Type*> single = type.GetSingleMember()) {
147 return *single;
148 }
149 return Get().union_types_.Add(std::move(type));
150 }
151
152 static const Type* GetUnionType(const Type* a, const Type* b) {
153 if (a->IsSubtypeOf(b)) return b;
154 if (b->IsSubtypeOf(a)) return a;
156 result.Extend(b);
157 return GetUnionType(std::move(result));
158 }
159
160 static const TopType* GetTopType(std::string reason,
161 const Type* source_type) {
162 std::unique_ptr<TopType> type(new TopType(std::move(reason), source_type));
163 TopType* result = type.get();
164 Get().top_types_.push_back(std::move(type));
165 return result;
166 }
167
168 static const Type* GetArgumentsType() {
169 return Get().GetBuiltinType(ARGUMENTS_TYPE_STRING);
170 }
171
172 static const Type* GetBoolType() {
173 return Get().GetBuiltinType(BOOL_TYPE_STRING);
174 }
175
176 static const Type* GetConstexprBoolType() {
177 return Get().GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
178 }
179
180 static const Type* GetConstexprStringType() {
181 return Get().GetBuiltinType(CONSTEXPR_STRING_TYPE_STRING);
182 }
183
184 static const Type* GetConstexprIntPtrType() {
185 return Get().GetBuiltinType(CONSTEXPR_INTPTR_TYPE_STRING);
186 }
187
189 return Get().GetBuiltinType(CONSTEXPR_INSTANCE_TYPE_TYPE_STRING);
190 }
191
192 static const Type* GetVoidType() {
193 return Get().GetBuiltinType(VOID_TYPE_STRING);
194 }
195
196 static const Type* GetRawPtrType() {
197 return Get().GetBuiltinType(RAWPTR_TYPE_STRING);
198 }
199
200 static const Type* GetExternalPointerType() {
201 return Get().GetBuiltinType(EXTERNALPTR_TYPE_STRING);
202 }
203
204 static const Type* GetCppHeapPointerType() {
205 return Get().GetBuiltinType(CPPHEAPPTR_TYPE_STRING);
206 }
207
208 static const Type* GetTrustedPointerType() {
209 return Get().GetBuiltinType(TRUSTEDPTR_TYPE_STRING);
210 }
211
213 return Get().GetBuiltinType(PROTECTEDPTR_TYPE_STRING);
214 }
215
216 static const Type* GetDispatchHandleType() {
217 return Get().GetBuiltinType(DISPATCH_HANDLE_TYPE_STRING);
218 }
219
220 static const Type* GetMapType() {
221 return Get().GetBuiltinType(MAP_TYPE_STRING);
222 }
223
224 static const Type* GetObjectType() {
225 return Get().GetBuiltinType(OBJECT_TYPE_STRING);
226 }
227
228 static const Type* GetHeapObjectType() {
229 return Get().GetBuiltinType(HEAP_OBJECT_TYPE_STRING);
230 }
231
233 return Get().GetBuiltinType(TAGGED_ZERO_PATTERN_TYPE_STRING);
234 }
235
236 static const Type* GetJSAnyType() {
237 return Get().GetBuiltinType(JSANY_TYPE_STRING);
238 }
239
240 static const Type* GetJSObjectType() {
241 return Get().GetBuiltinType(JSOBJECT_TYPE_STRING);
242 }
243
244 static const Type* GetTaggedType() {
245 return Get().GetBuiltinType(TAGGED_TYPE_STRING);
246 }
247
248 static const Type* GetStrongTaggedType() {
249 return Get().GetBuiltinType(STRONG_TAGGED_TYPE_STRING);
250 }
251
252 static const Type* GetUninitializedType() {
253 return Get().GetBuiltinType(UNINITIALIZED_TYPE_STRING);
254 }
255
261
262 static const Type* GetSmiType() {
263 return Get().GetBuiltinType(SMI_TYPE_STRING);
264 }
265
266 static const Type* GetConstStringType() {
267 return Get().GetBuiltinType(CONST_STRING_TYPE_STRING);
268 }
269
270 static const Type* GetStringType() {
271 return Get().GetBuiltinType(STRING_TYPE_STRING);
272 }
273
274 static const Type* GetNumberType() {
275 return Get().GetBuiltinType(NUMBER_TYPE_STRING);
276 }
277
278 static const Type* GetIntPtrType() {
279 return Get().GetBuiltinType(INTPTR_TYPE_STRING);
280 }
281
282 static const Type* GetUIntPtrType() {
283 return Get().GetBuiltinType(UINTPTR_TYPE_STRING);
284 }
285
286 static const Type* GetInt64Type() {
287 return Get().GetBuiltinType(INT64_TYPE_STRING);
288 }
289
290 static const Type* GetUint64Type() {
291 return Get().GetBuiltinType(UINT64_TYPE_STRING);
292 }
293
294 static const Type* GetInt32Type() {
295 return Get().GetBuiltinType(INT32_TYPE_STRING);
296 }
297
298 static const Type* GetUint32Type() {
299 return Get().GetBuiltinType(UINT32_TYPE_STRING);
300 }
301
302 static const Type* GetUint31Type() {
303 return Get().GetBuiltinType(UINT31_TYPE_STRING);
304 }
305
306 static const Type* GetInt16Type() {
307 return Get().GetBuiltinType(INT16_TYPE_STRING);
308 }
309
310 static const Type* GetUint16Type() {
311 return Get().GetBuiltinType(UINT16_TYPE_STRING);
312 }
313
314 static const Type* GetInt8Type() {
315 return Get().GetBuiltinType(INT8_TYPE_STRING);
316 }
317
318 static const Type* GetUint8Type() {
319 return Get().GetBuiltinType(UINT8_TYPE_STRING);
320 }
321
322 static const Type* GetFloat64Type() {
323 return Get().GetBuiltinType(FLOAT64_TYPE_STRING);
324 }
325
327 return Get().GetBuiltinType(FLOAT64_OR_UNDEFINED_OR_HOLE_TYPE_STRING);
328 }
329
330 static const Type* GetConstFloat64Type() {
331 return Get().GetBuiltinType(CONST_FLOAT64_TYPE_STRING);
332 }
333
334 static const Type* GetIntegerLiteralType() {
335 return Get().GetBuiltinType(INTEGER_LITERAL_TYPE_STRING);
336 }
337
338 static const Type* GetNeverType() {
339 return Get().GetBuiltinType(NEVER_TYPE_STRING);
340 }
341
342 static const Type* GetConstInt31Type() {
343 return Get().GetBuiltinType(CONST_INT31_TYPE_STRING);
344 }
345
346 static const Type* GetConstInt32Type() {
347 return Get().GetBuiltinType(CONST_INT32_TYPE_STRING);
348 }
349
350 static const Type* GetContextType() {
351 return Get().GetBuiltinType(CONTEXT_TYPE_STRING);
352 }
353
354 static const Type* GetNoContextType() {
355 return Get().GetBuiltinType(NO_CONTEXT_TYPE_STRING);
356 }
357
358 static const Type* GetNativeContextType() {
359 return Get().GetBuiltinType(NATIVE_CONTEXT_TYPE_STRING);
360 }
361
362 static const Type* GetJSFunctionType() {
363 return Get().GetBuiltinType(JS_FUNCTION_TYPE_STRING);
364 }
365
367 return Get().GetBuiltinType(UNINITIALIZED_ITERATOR_TYPE_STRING);
368 }
369
370 static const Type* GetFixedArrayBaseType() {
371 return Get().GetBuiltinType(FIXED_ARRAY_BASE_TYPE_STRING);
372 }
373
374 static std::optional<const Type*> ImplicitlyConvertableFrom(
375 const Type* to, const Type* from) {
376 while (from != nullptr) {
377 for (GenericCallable* from_constexpr :
379 if (std::optional<const Callable*> specialization =
380 from_constexpr->GetSpecialization({to, from})) {
381 if ((*specialization)->signature().GetExplicitTypes() ==
382 TypeVector{from}) {
383 return from;
384 }
385 }
386 }
387 from = from->parent();
388 }
389 return std::nullopt;
390 }
391
392 static const std::vector<std::unique_ptr<AggregateType>>& GetAggregateTypes();
393 static const std::vector<std::unique_ptr<BitFieldStructType>>&
395
396 // By construction, this list of all classes is topologically sorted w.r.t.
397 // inheritance.
398 static std::vector<const ClassType*> GetClasses();
399
400 static void FinalizeAggregateTypes();
401
402 static size_t FreshTypeId() { return Get().next_type_id_++; }
403
405
406 private:
407 const Type* GetBuiltinType(const QualifiedName& name) {
409 }
410 const Type* GetBuiltinType(const std::string& name) {
411 return GetBuiltinType(QualifiedName(name));
412 }
413
415 std::vector<const BuiltinPointerType*> all_builtin_pointer_types_;
417 std::vector<std::unique_ptr<Type>> nominal_types_;
418 std::vector<std::unique_ptr<AggregateType>> aggregate_types_;
419 std::vector<std::unique_ptr<BitFieldStructType>> bit_field_struct_types_;
420 std::vector<std::unique_ptr<Type>> top_types_;
421 std::vector<std::unique_ptr<Namespace>>
423 size_t next_type_id_ = 0;
424};
425
426} // namespace v8::internal::torque
427
428#endif // V8_TORQUE_TYPE_ORACLE_H_
static VarType & Get()
Definition contextual.h:64
static std::vector< GenericCallable * > LookupGeneric(const std::string &name)
static const Type * LookupGlobalType(const QualifiedName &name)
static GenericType * LookupGlobalUniqueGenericType(const std::string &name)
static GenericType * LookupUniqueGenericType(const QualifiedName &name)
static const Type * GetConstexprStringType()
static const Type * GetStrongTaggedType()
static GenericType * GetSmiTaggedGeneric()
static Namespace * CreateGenericTypeInstantiationNamespace()
static const Type * GetUninitializedHeapObjectType()
static const Type * GetJSFunctionType()
static const Type * GetUint31Type()
static const Type * GetStringType()
static const Type * GetFloat64Type()
static const Type * GetConstexprInstanceTypeType()
static const Type * GetUnionType(UnionType type)
static const Type * GetConstexprIntPtrType()
Deduplicator< UnionType > union_types_
static const Type * GetUint32Type()
std::vector< std::unique_ptr< AggregateType > > aggregate_types_
static std::vector< const ClassType * > GetClasses()
static const Type * GetConstFloat64Type()
static const Type * GetInt8Type()
static const Type * GetRawPtrType()
static const AbstractType * GetAbstractType(const Type *parent, std::string name, AbstractTypeFlags flags, std::string generated, const Type *non_constexpr_version, MaybeSpecializationKey specialized_from)
Definition type-oracle.h:22
static const Type * GetNoContextType()
static const std::vector< std::unique_ptr< AggregateType > > & GetAggregateTypes()
static const Type * GetConstReferenceType(const Type *referenced_type)
static const Type * GetConstexprBoolType()
static const Type * GetUnionType(const Type *a, const Type *b)
static const Type * GetTaggedType()
static const Type * GetHeapObjectType()
static std::optional< const Type * > ImplicitlyConvertableFrom(const Type *to, const Type *from)
static const Type * GetUint8Type()
std::vector< const BuiltinPointerType * > all_builtin_pointer_types_
static const std::vector< const BuiltinPointerType * > & AllBuiltinPointerTypes()
static BitFieldStructType * GetBitFieldStructType(const Type *parent, const BitFieldStructDeclaration *decl)
Definition type-oracle.h:47
static GenericType * GetConstSliceGeneric()
static const Type * GetArgumentsType()
static const Type * GetUninitializedIteratorType()
static GenericType * GetMutableReferenceGeneric()
Definition type-oracle.h:93
static const Type * GetFixedArrayBaseType()
static const Type * GetUint64Type()
Deduplicator< BuiltinPointerType > function_pointer_types_
static const Type * GetUint16Type()
static const Type * GetExternalPointerType()
std::vector< std::unique_ptr< BitFieldStructType > > bit_field_struct_types_
static const BuiltinPointerType * GetBuiltinPointerType(TypeVector argument_types, const Type *return_type)
Definition type-oracle.h:67
static const Type * GetObjectType()
static const Type * GetNativeContextType()
static const Type * GetMutableSliceType(const Type *referenced_type)
static const Type * GetVoidType()
static const Type * GetInt32Type()
static const Type * GetNumberType()
std::vector< std::unique_ptr< Type > > top_types_
static const Type * GetInt64Type()
const Type * GetBuiltinType(const QualifiedName &name)
static std::optional< const Type * > MatchReferenceGeneric(const Type *reference_type, bool *is_const=nullptr)
static const Type * GetTaggedZeroPatternType()
static const Type * GetGenericTypeInstance(GenericType *generic_type, TypeVector arg_types)
static const Type * GetConstInt32Type()
static const Type * GetUIntPtrType()
static const Type * GetJSObjectType()
static const TopType * GetTopType(std::string reason, const Type *source_type)
static const Type * GetIntPtrType()
static GenericType * GetMutableSliceGeneric()
static const Type * GetReferenceType(const Type *referenced_type, bool is_const)
static const Type * GetBoolType()
static const Type * GetSmiType()
static const Type * GetMapType()
static const Type * GetContextType()
static const Type * GetTrustedPointerType()
static GenericType * GetReferenceGeneric(bool is_const)
Definition type-oracle.h:84
static ClassType * GetClassType(const Type *parent, const std::string &name, ClassFlags flags, const std::string &generates, ClassDeclaration *decl, const TypeAlias *alias)
Definition type-oracle.h:56
static GenericType * GetWeakGeneric()
static const Type * GetProtectedPointerType()
static const Type * GetJSAnyType()
static GenericType * GetLazyGeneric()
std::vector< std::unique_ptr< Namespace > > generic_type_instantiation_namespaces_
static GenericType * GetConstReferenceGeneric()
Definition type-oracle.h:90
static const Type * GetMutableReferenceType(const Type *referenced_type)
static const Type * GetConstSliceType(const Type *referenced_type)
const Type * GetBuiltinType(const std::string &name)
static const Type * GetNeverType()
static const Type * GetIntegerLiteralType()
static const Type * GetDispatchHandleType()
static const Type * GetConstStringType()
static const std::vector< std::unique_ptr< BitFieldStructType > > & GetBitFieldStructTypes()
static const Type * GetFloat64OrUndefinedOrHoleType()
static StructType * GetStructType(const StructDeclaration *decl, MaybeSpecializationKey specialized_from)
Definition type-oracle.h:38
static const Type * GetInt16Type()
static const Type * GetConstInt31Type()
static const Type * GetCppHeapPointerType()
std::vector< std::unique_ptr< Type > > nominal_types_
static const Type * GetUninitializedType()
virtual void SetConstexprVersion(const Type *type) const
Definition types.h:161
virtual bool IsSubtypeOf(const Type *supertype) const
Definition types.cc:101
static UnionType FromType(const Type *t)
Definition types.h:468
std::optional< TNode< JSArray > > a
ZoneVector< RpoNumber > & result
Point from
static const char *const JS_FUNCTION_TYPE_STRING
Definition constants.h:30
static const char *const SMI_TYPE_STRING
Definition constants.h:37
static const char *const NEVER_TYPE_STRING
Definition constants.h:18
static const char *const TORQUE_INTERNAL_NAMESPACE_STRING
Definition constants.h:78
static const char *const JSANY_TYPE_STRING
Definition constants.h:35
static const char *const NO_CONTEXT_TYPE_STRING
Definition constants.h:28
static const char *const TAGGED_TYPE_STRING
Definition constants.h:38
static const char *const JSOBJECT_TYPE_STRING
Definition constants.h:36
static const char *const BOOL_TYPE_STRING
Definition constants.h:24
static const char *const WEAK_TYPE_STRING
Definition constants.h:83
static const char *const BUILTIN_POINTER_TYPE_STRING
Definition constants.h:52
static const char *const HEAP_OBJECT_TYPE_STRING
Definition constants.h:33
static const char *const INT16_TYPE_STRING
Definition constants.h:61
static const char *const UINT64_TYPE_STRING
Definition constants.h:56
static const char *const PROTECTEDPTR_TYPE_STRING
Definition constants.h:47
static const char *const CONST_SLICE_TYPE_STRING
Definition constants.h:82
static constexpr const char *const kFromConstexprMacroName
static const char *const INT8_TYPE_STRING
Definition constants.h:63
static const char *const CPPHEAPPTR_TYPE_STRING
Definition constants.h:45
static const char *const INT32_TYPE_STRING
Definition constants.h:58
static const char *const UNINITIALIZED_HEAP_OBJECT_TYPE_STRING
Definition constants.h:41
static const char *const NATIVE_CONTEXT_TYPE_STRING
Definition constants.h:29
static const char *const CONSTEXPR_INTPTR_TYPE_STRING
Definition constants.h:21
static const char *const UINT8_TYPE_STRING
Definition constants.h:64
static const char *const FLOAT64_OR_UNDEFINED_OR_HOLE_TYPE_STRING
Definition constants.h:71
static const char *const VOID_TYPE_STRING
Definition constants.h:25
static const char *const UINTPTR_TYPE_STRING
Definition constants.h:54
static const char *const CONST_REFERENCE_TYPE_STRING
Definition constants.h:80
static const char *const MUTABLE_SLICE_TYPE_STRING
Definition constants.h:81
static const char *const TAGGED_ZERO_PATTERN_TYPE_STRING
Definition constants.h:34
static const char *const STRING_TYPE_STRING
Definition constants.h:50
static const char *const UINT16_TYPE_STRING
Definition constants.h:62
static const char *const UINT32_TYPE_STRING
Definition constants.h:60
static const char *const RAWPTR_TYPE_STRING
Definition constants.h:43
static const char *const EXTERNALPTR_TYPE_STRING
Definition constants.h:44
static const char *const NUMBER_TYPE_STRING
Definition constants.h:51
static const char *const INTPTR_TYPE_STRING
Definition constants.h:53
std::optional< SpecializationKey< GenericType > > MaybeSpecializationKey
Definition types.h:93
static const char *const INT64_TYPE_STRING
Definition constants.h:55
static const char *const CONSTEXPR_BOOL_TYPE_STRING
Definition constants.h:19
static const char *const MUTABLE_REFERENCE_TYPE_STRING
Definition constants.h:79
static const char *const CONSTEXPR_INSTANCE_TYPE_TYPE_STRING
Definition constants.h:22
static const char *const CONST_FLOAT64_TYPE_STRING
Definition constants.h:75
static const char *const UNINITIALIZED_TYPE_STRING
Definition constants.h:40
static const char *const CONSTEXPR_STRING_TYPE_STRING
Definition constants.h:20
static const char *const FIXED_ARRAY_BASE_TYPE_STRING
Definition constants.h:90
static const char *const SMI_TAGGED_TYPE_STRING
Definition constants.h:84
static const char *const TRUSTEDPTR_TYPE_STRING
Definition constants.h:46
static const char *const CONST_INT31_TYPE_STRING
Definition constants.h:73
static const char *const LAZY_TYPE_STRING
Definition constants.h:85
static const char *const INTEGER_LITERAL_TYPE_STRING
Definition constants.h:76
static const char *const DISPATCH_HANDLE_TYPE_STRING
Definition constants.h:48
std::vector< const Type * > TypeVector
Definition types.h:85
static const char *const CONST_STRING_TYPE_STRING
Definition constants.h:49
static const char *const STRONG_TAGGED_TYPE_STRING
Definition constants.h:39
Namespace * CurrentNamespace()
Definition declarable.h:230
static const char *const ARGUMENTS_TYPE_STRING
Definition constants.h:26
static const char *const UNINITIALIZED_ITERATOR_TYPE_STRING
Definition constants.h:86
static const char *const OBJECT_TYPE_STRING
Definition constants.h:32
static const char *const FLOAT64_TYPE_STRING
Definition constants.h:70
static const char *const CONTEXT_TYPE_STRING
Definition constants.h:27
static const char *const MAP_TYPE_STRING
Definition constants.h:31
static const char *const CONST_INT32_TYPE_STRING
Definition constants.h:74
static const char *const UINT31_TYPE_STRING
Definition constants.h:59
#define DCHECK(condition)
Definition logging.h:482
wasm::ValueType type