V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
type-oracle.h
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 "src/torque/contextual.h"
9 #include "src/torque/declarable.h"
10 #include "src/torque/declarations.h"
11 #include "src/torque/types.h"
12 #include "src/torque/utils.h"
13 
14 namespace v8 {
15 namespace internal {
16 namespace torque {
17 
18 class TypeOracle : public ContextualClass<TypeOracle> {
19  public:
20  static const AbstractType* GetAbstractType(
21  const Type* parent, std::string name, bool transient,
22  std::string generated,
23  base::Optional<const AbstractType*> non_constexpr_version) {
24  AbstractType* result =
25  new AbstractType(parent, transient, std::move(name),
26  std::move(generated), non_constexpr_version);
27  Get().nominal_types_.push_back(std::unique_ptr<AbstractType>(result));
28  return result;
29  }
30 
31  static const StructType* GetStructType(
32  const std::string& name, const std::vector<NameAndType>& fields) {
33  StructType* result = new StructType(CurrentNamespace(), name, fields);
34  Get().struct_types_.push_back(std::unique_ptr<StructType>(result));
35  return result;
36  }
37 
38  static const FunctionPointerType* GetFunctionPointerType(
39  TypeVector argument_types, const Type* return_type) {
40  TypeOracle& self = Get();
41  const Type* code_type = self.GetBuiltinType(CODE_TYPE_STRING);
42  const FunctionPointerType* result = self.function_pointer_types_.Add(
43  FunctionPointerType(code_type, argument_types, return_type,
44  self.all_function_pointer_types_.size()));
45  if (result->function_pointer_type_id() ==
46  self.all_function_pointer_types_.size()) {
47  self.all_function_pointer_types_.push_back(result);
48  }
49  return result;
50  }
51 
52  static const std::vector<const FunctionPointerType*>&
53  AllFunctionPointerTypes() {
54  return Get().all_function_pointer_types_;
55  }
56 
57  static const Type* GetUnionType(UnionType type) {
58  if (base::Optional<const Type*> single = type.GetSingleMember()) {
59  return *single;
60  }
61  return Get().union_types_.Add(std::move(type));
62  }
63 
64  static const Type* GetUnionType(const Type* a, const Type* b) {
65  if (a->IsSubtypeOf(b)) return b;
66  if (b->IsSubtypeOf(a)) return a;
67  UnionType result = UnionType::FromType(a);
68  result.Extend(b);
69  return GetUnionType(std::move(result));
70  }
71 
72  static const TopType* GetTopType(std::string reason,
73  const Type* source_type) {
74  TopType* result = new TopType(std::move(reason), source_type);
75  Get().top_types_.push_back(std::unique_ptr<TopType>(result));
76  return result;
77  }
78 
79  static const Type* GetArgumentsType() {
80  return Get().GetBuiltinType(ARGUMENTS_TYPE_STRING);
81  }
82 
83  static const Type* GetBoolType() {
84  return Get().GetBuiltinType(BOOL_TYPE_STRING);
85  }
86 
87  static const Type* GetConstexprBoolType() {
88  return Get().GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
89  }
90 
91  static const Type* GetVoidType() {
92  return Get().GetBuiltinType(VOID_TYPE_STRING);
93  }
94 
95  static const Type* GetObjectType() {
96  return Get().GetBuiltinType(OBJECT_TYPE_STRING);
97  }
98 
99  static const Type* GetConstStringType() {
100  return Get().GetBuiltinType(CONST_STRING_TYPE_STRING);
101  }
102 
103  static const Type* GetIntPtrType() {
104  return Get().GetBuiltinType(INTPTR_TYPE_STRING);
105  }
106 
107  static const Type* GetNeverType() {
108  return Get().GetBuiltinType(NEVER_TYPE_STRING);
109  }
110 
111  static const Type* GetConstInt31Type() {
112  return Get().GetBuiltinType(CONST_INT31_TYPE_STRING);
113  }
114 
115  static bool IsImplicitlyConvertableFrom(const Type* to, const Type* from) {
116  for (Generic* from_constexpr :
117  Declarations::LookupGeneric(kFromConstexprMacroName)) {
118  if (base::Optional<Callable*> specialization =
119  from_constexpr->GetSpecialization({to})) {
120  if ((*specialization)->signature().GetExplicitTypes() ==
121  TypeVector{from}) {
122  return true;
123  }
124  }
125  }
126  return false;
127  }
128 
129  private:
130  const Type* GetBuiltinType(const std::string& name) {
131  return Declarations::LookupGlobalType(name);
132  }
133 
134  Deduplicator<FunctionPointerType> function_pointer_types_;
135  std::vector<const FunctionPointerType*> all_function_pointer_types_;
136  Deduplicator<UnionType> union_types_;
137  std::vector<std::unique_ptr<Type>> nominal_types_;
138  std::vector<std::unique_ptr<Type>> struct_types_;
139  std::vector<std::unique_ptr<Type>> top_types_;
140 };
141 
142 } // namespace torque
143 } // namespace internal
144 } // namespace v8
145 
146 #endif // V8_TORQUE_TYPE_ORACLE_H_
Definition: libplatform.h:13