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 10 #include "src/torque/contextual.h" 11 #include "src/torque/declarable.h" 12 #include "src/torque/declarations.h" 13 #include "src/torque/types.h" 14 #include "src/torque/utils.h" 15 16 namespace v8 { 17 namespace internal { 18 namespace torque { 19 20 class TypeOracle : public ContextualClass<TypeOracle> { 21 public: GetAbstractType(const Type * parent,std::string name,AbstractTypeFlags flags,std::string generated,const Type * non_constexpr_version,MaybeSpecializationKey specialized_from)22 static const AbstractType* GetAbstractType( 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 GetStructType(const StructDeclaration * decl,MaybeSpecializationKey specialized_from)38 static StructType* GetStructType(const StructDeclaration* decl, 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 GetBitFieldStructType(const Type * parent,const BitFieldStructDeclaration * decl)47 static BitFieldStructType* GetBitFieldStructType( 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 GetClassType(const Type * parent,const std::string & name,ClassFlags flags,const std::string & generates,ClassDeclaration * decl,const TypeAlias * alias)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 GetBuiltinPointerType(TypeVector argument_types,const Type * return_type)67 static const BuiltinPointerType* GetBuiltinPointerType( 68 TypeVector argument_types, const Type* return_type) { 69 TypeOracle& self = Get(); 70 const Type* builtin_type = self.GetBuiltinType(BUILTIN_POINTER_TYPE_STRING); 71 const BuiltinPointerType* result = self.function_pointer_types_.Add( 72 BuiltinPointerType(builtin_type, 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 GetReferenceGeneric(bool is_const)84 static GenericType* GetReferenceGeneric(bool is_const) { 85 return Declarations::LookupUniqueGenericType( 86 QualifiedName({TORQUE_INTERNAL_NAMESPACE_STRING}, 87 is_const ? CONST_REFERENCE_TYPE_STRING 88 : MUTABLE_REFERENCE_TYPE_STRING)); 89 } GetConstReferenceGeneric()90 static GenericType* GetConstReferenceGeneric() { 91 return GetReferenceGeneric(true); 92 } GetMutableReferenceGeneric()93 static GenericType* GetMutableReferenceGeneric() { 94 return GetReferenceGeneric(false); 95 } 96 97 static base::Optional<const Type*> MatchReferenceGeneric( 98 const Type* reference_type, bool* is_const = nullptr); 99 GetSliceGeneric()100 static GenericType* GetSliceGeneric() { 101 return Declarations::LookupUniqueGenericType( 102 QualifiedName({TORQUE_INTERNAL_NAMESPACE_STRING}, SLICE_TYPE_STRING)); 103 } 104 GetWeakGeneric()105 static GenericType* GetWeakGeneric() { 106 return Declarations::LookupGlobalUniqueGenericType(WEAK_TYPE_STRING); 107 } 108 GetSmiTaggedGeneric()109 static GenericType* GetSmiTaggedGeneric() { 110 return Declarations::LookupGlobalUniqueGenericType(SMI_TAGGED_TYPE_STRING); 111 } 112 GetReferenceType(const Type * referenced_type,bool is_const)113 static const Type* GetReferenceType(const Type* referenced_type, 114 bool is_const) { 115 return GetGenericTypeInstance(GetReferenceGeneric(is_const), 116 {referenced_type}); 117 } GetConstReferenceType(const Type * referenced_type)118 static const Type* GetConstReferenceType(const Type* referenced_type) { 119 return GetReferenceType(referenced_type, true); 120 } GetMutableReferenceType(const Type * referenced_type)121 static const Type* GetMutableReferenceType(const Type* referenced_type) { 122 return GetReferenceType(referenced_type, false); 123 } 124 GetSliceType(const Type * referenced_type)125 static const Type* GetSliceType(const Type* referenced_type) { 126 return GetGenericTypeInstance(GetSliceGeneric(), {referenced_type}); 127 } 128 129 static const std::vector<const BuiltinPointerType*>& AllBuiltinPointerTypes()130 AllBuiltinPointerTypes() { 131 return Get().all_builtin_pointer_types_; 132 } 133 GetUnionType(UnionType type)134 static const Type* GetUnionType(UnionType type) { 135 if (base::Optional<const Type*> single = type.GetSingleMember()) { 136 return *single; 137 } 138 return Get().union_types_.Add(std::move(type)); 139 } 140 GetUnionType(const Type * a,const Type * b)141 static const Type* GetUnionType(const Type* a, const Type* b) { 142 if (a->IsSubtypeOf(b)) return b; 143 if (b->IsSubtypeOf(a)) return a; 144 UnionType result = UnionType::FromType(a); 145 result.Extend(b); 146 return GetUnionType(std::move(result)); 147 } 148 GetTopType(std::string reason,const Type * source_type)149 static const TopType* GetTopType(std::string reason, 150 const Type* source_type) { 151 std::unique_ptr<TopType> type(new TopType(std::move(reason), source_type)); 152 TopType* result = type.get(); 153 Get().top_types_.push_back(std::move(type)); 154 return result; 155 } 156 GetArgumentsType()157 static const Type* GetArgumentsType() { 158 return Get().GetBuiltinType(ARGUMENTS_TYPE_STRING); 159 } 160 GetBoolType()161 static const Type* GetBoolType() { 162 return Get().GetBuiltinType(BOOL_TYPE_STRING); 163 } 164 GetConstexprBoolType()165 static const Type* GetConstexprBoolType() { 166 return Get().GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING); 167 } 168 GetConstexprStringType()169 static const Type* GetConstexprStringType() { 170 return Get().GetBuiltinType(CONSTEXPR_STRING_TYPE_STRING); 171 } 172 GetConstexprIntPtrType()173 static const Type* GetConstexprIntPtrType() { 174 return Get().GetBuiltinType(CONSTEXPR_INTPTR_TYPE_STRING); 175 } 176 GetConstexprInstanceTypeType()177 static const Type* GetConstexprInstanceTypeType() { 178 return Get().GetBuiltinType(CONSTEXPR_INSTANCE_TYPE_TYPE_STRING); 179 } 180 GetVoidType()181 static const Type* GetVoidType() { 182 return Get().GetBuiltinType(VOID_TYPE_STRING); 183 } 184 GetRawPtrType()185 static const Type* GetRawPtrType() { 186 return Get().GetBuiltinType(RAWPTR_TYPE_STRING); 187 } 188 GetExternalPointerType()189 static const Type* GetExternalPointerType() { 190 return Get().GetBuiltinType(EXTERNALPTR_TYPE_STRING); 191 } 192 GetMapType()193 static const Type* GetMapType() { 194 return Get().GetBuiltinType(MAP_TYPE_STRING); 195 } 196 GetObjectType()197 static const Type* GetObjectType() { 198 return Get().GetBuiltinType(OBJECT_TYPE_STRING); 199 } 200 GetHeapObjectType()201 static const Type* GetHeapObjectType() { 202 return Get().GetBuiltinType(HEAP_OBJECT_TYPE_STRING); 203 } 204 GetJSAnyType()205 static const Type* GetJSAnyType() { 206 return Get().GetBuiltinType(JSANY_TYPE_STRING); 207 } 208 GetJSObjectType()209 static const Type* GetJSObjectType() { 210 return Get().GetBuiltinType(JSOBJECT_TYPE_STRING); 211 } 212 GetTaggedType()213 static const Type* GetTaggedType() { 214 return Get().GetBuiltinType(TAGGED_TYPE_STRING); 215 } 216 GetStrongTaggedType()217 static const Type* GetStrongTaggedType() { 218 return Get().GetBuiltinType(STRONG_TAGGED_TYPE_STRING); 219 } 220 GetUninitializedType()221 static const Type* GetUninitializedType() { 222 return Get().GetBuiltinType(UNINITIALIZED_TYPE_STRING); 223 } 224 GetUninitializedHeapObjectType()225 static const Type* GetUninitializedHeapObjectType() { 226 return Get().GetBuiltinType( 227 QualifiedName({TORQUE_INTERNAL_NAMESPACE_STRING}, 228 UNINITIALIZED_HEAP_OBJECT_TYPE_STRING)); 229 } 230 GetSmiType()231 static const Type* GetSmiType() { 232 return Get().GetBuiltinType(SMI_TYPE_STRING); 233 } 234 GetConstStringType()235 static const Type* GetConstStringType() { 236 return Get().GetBuiltinType(CONST_STRING_TYPE_STRING); 237 } 238 GetStringType()239 static const Type* GetStringType() { 240 return Get().GetBuiltinType(STRING_TYPE_STRING); 241 } 242 GetNumberType()243 static const Type* GetNumberType() { 244 return Get().GetBuiltinType(NUMBER_TYPE_STRING); 245 } 246 GetIntPtrType()247 static const Type* GetIntPtrType() { 248 return Get().GetBuiltinType(INTPTR_TYPE_STRING); 249 } 250 GetUIntPtrType()251 static const Type* GetUIntPtrType() { 252 return Get().GetBuiltinType(UINTPTR_TYPE_STRING); 253 } 254 GetInt32Type()255 static const Type* GetInt32Type() { 256 return Get().GetBuiltinType(INT32_TYPE_STRING); 257 } 258 GetUint32Type()259 static const Type* GetUint32Type() { 260 return Get().GetBuiltinType(UINT32_TYPE_STRING); 261 } 262 GetUint31Type()263 static const Type* GetUint31Type() { 264 return Get().GetBuiltinType(UINT31_TYPE_STRING); 265 } 266 GetInt16Type()267 static const Type* GetInt16Type() { 268 return Get().GetBuiltinType(INT16_TYPE_STRING); 269 } 270 GetUint16Type()271 static const Type* GetUint16Type() { 272 return Get().GetBuiltinType(UINT16_TYPE_STRING); 273 } 274 GetInt8Type()275 static const Type* GetInt8Type() { 276 return Get().GetBuiltinType(INT8_TYPE_STRING); 277 } 278 GetUint8Type()279 static const Type* GetUint8Type() { 280 return Get().GetBuiltinType(UINT8_TYPE_STRING); 281 } 282 GetFloat64Type()283 static const Type* GetFloat64Type() { 284 return Get().GetBuiltinType(FLOAT64_TYPE_STRING); 285 } 286 GetFloat64OrHoleType()287 static const Type* GetFloat64OrHoleType() { 288 return Get().GetBuiltinType(FLOAT64_OR_HOLE_TYPE_STRING); 289 } 290 GetConstFloat64Type()291 static const Type* GetConstFloat64Type() { 292 return Get().GetBuiltinType(CONST_FLOAT64_TYPE_STRING); 293 } 294 GetNeverType()295 static const Type* GetNeverType() { 296 return Get().GetBuiltinType(NEVER_TYPE_STRING); 297 } 298 GetConstInt31Type()299 static const Type* GetConstInt31Type() { 300 return Get().GetBuiltinType(CONST_INT31_TYPE_STRING); 301 } 302 GetConstInt32Type()303 static const Type* GetConstInt32Type() { 304 return Get().GetBuiltinType(CONST_INT32_TYPE_STRING); 305 } 306 GetContextType()307 static const Type* GetContextType() { 308 return Get().GetBuiltinType(CONTEXT_TYPE_STRING); 309 } 310 GetNoContextType()311 static const Type* GetNoContextType() { 312 return Get().GetBuiltinType(NO_CONTEXT_TYPE_STRING); 313 } 314 GetNativeContextType()315 static const Type* GetNativeContextType() { 316 return Get().GetBuiltinType(NATIVE_CONTEXT_TYPE_STRING); 317 } 318 GetJSFunctionType()319 static const Type* GetJSFunctionType() { 320 return Get().GetBuiltinType(JS_FUNCTION_TYPE_STRING); 321 } 322 GetUninitializedIteratorType()323 static const Type* GetUninitializedIteratorType() { 324 return Get().GetBuiltinType(UNINITIALIZED_ITERATOR_TYPE_STRING); 325 } 326 GetFixedArrayBaseType()327 static const Type* GetFixedArrayBaseType() { 328 return Get().GetBuiltinType(FIXED_ARRAY_BASE_TYPE_STRING); 329 } 330 ImplicitlyConvertableFrom(const Type * to,const Type * from)331 static base::Optional<const Type*> ImplicitlyConvertableFrom( 332 const Type* to, const Type* from) { 333 while (from != nullptr) { 334 for (GenericCallable* from_constexpr : 335 Declarations::LookupGeneric(kFromConstexprMacroName)) { 336 if (base::Optional<const Callable*> specialization = 337 from_constexpr->GetSpecialization({to, from})) { 338 if ((*specialization)->signature().GetExplicitTypes() == 339 TypeVector{from}) { 340 return from; 341 } 342 } 343 } 344 from = from->parent(); 345 } 346 return base::nullopt; 347 } 348 349 static const std::vector<std::unique_ptr<AggregateType>>& GetAggregateTypes(); 350 static const std::vector<std::unique_ptr<BitFieldStructType>>& 351 GetBitFieldStructTypes(); 352 353 // By construction, this list of all classes is topologically sorted w.r.t. 354 // inheritance. 355 static std::vector<const ClassType*> GetClasses(); 356 357 static void FinalizeAggregateTypes(); 358 FreshTypeId()359 static size_t FreshTypeId() { return Get().next_type_id_++; } 360 361 static Namespace* CreateGenericTypeInstantiationNamespace(); 362 363 private: GetBuiltinType(const QualifiedName & name)364 const Type* GetBuiltinType(const QualifiedName& name) { 365 return Declarations::LookupGlobalType(name); 366 } GetBuiltinType(const std::string & name)367 const Type* GetBuiltinType(const std::string& name) { 368 return GetBuiltinType(QualifiedName(name)); 369 } 370 371 Deduplicator<BuiltinPointerType> function_pointer_types_; 372 std::vector<const BuiltinPointerType*> all_builtin_pointer_types_; 373 Deduplicator<UnionType> union_types_; 374 std::vector<std::unique_ptr<Type>> nominal_types_; 375 std::vector<std::unique_ptr<AggregateType>> aggregate_types_; 376 std::vector<std::unique_ptr<BitFieldStructType>> bit_field_struct_types_; 377 std::vector<std::unique_ptr<Type>> top_types_; 378 std::vector<std::unique_ptr<Namespace>> 379 generic_type_instantiation_namespaces_; 380 size_t next_type_id_ = 0; 381 }; 382 383 } // namespace torque 384 } // namespace internal 385 } // namespace v8 386 387 #endif // V8_TORQUE_TYPE_ORACLE_H_ 388