• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
TypeOracle(Declarations * declarations)20   explicit TypeOracle(Declarations* declarations)
21       : declarations_(declarations) {}
22 
GetAbstractType(const Type * parent,std::string name,std::string generated,base::Optional<const AbstractType * > non_constexpr_version)23   static const AbstractType* GetAbstractType(
24       const Type* parent, std::string name, std::string generated,
25       base::Optional<const AbstractType*> non_constexpr_version) {
26     AbstractType* result = new AbstractType(
27         parent, std::move(name), std::move(generated), non_constexpr_version);
28     Get().nominal_types_.push_back(std::unique_ptr<AbstractType>(result));
29     return result;
30   }
31 
GetStructType(Module * module,const std::string & name,const std::vector<NameAndType> & fields)32   static const StructType* GetStructType(
33       Module* module, const std::string& name,
34       const std::vector<NameAndType>& fields) {
35     StructType* result = new StructType(module, name, fields);
36     Get().struct_types_.push_back(std::unique_ptr<StructType>(result));
37     return result;
38   }
39 
GetFunctionPointerType(TypeVector argument_types,const Type * return_type)40   static const FunctionPointerType* GetFunctionPointerType(
41       TypeVector argument_types, const Type* return_type) {
42     const Type* code_type = Get().GetBuiltinType(CODE_TYPE_STRING);
43     return Get().function_pointer_types_.Add(
44         FunctionPointerType(code_type, argument_types, return_type));
45   }
46 
GetUnionType(UnionType type)47   static const Type* GetUnionType(UnionType type) {
48     if (base::Optional<const Type*> single = type.GetSingleMember()) {
49       return *single;
50     }
51     return Get().union_types_.Add(std::move(type));
52   }
53 
GetUnionType(const Type * a,const Type * b)54   static const Type* GetUnionType(const Type* a, const Type* b) {
55     if (a->IsSubtypeOf(b)) return b;
56     if (b->IsSubtypeOf(a)) return a;
57     UnionType result = UnionType::FromType(a);
58     result.Extend(b);
59     return GetUnionType(std::move(result));
60   }
61 
GetArgumentsType()62   static const Type* GetArgumentsType() {
63     return Get().GetBuiltinType(ARGUMENTS_TYPE_STRING);
64   }
65 
GetBoolType()66   static const Type* GetBoolType() {
67     return Get().GetBuiltinType(BOOL_TYPE_STRING);
68   }
69 
GetConstexprBoolType()70   static const Type* GetConstexprBoolType() {
71     return Get().GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
72   }
73 
GetVoidType()74   static const Type* GetVoidType() {
75     return Get().GetBuiltinType(VOID_TYPE_STRING);
76   }
77 
GetObjectType()78   static const Type* GetObjectType() {
79     return Get().GetBuiltinType(OBJECT_TYPE_STRING);
80   }
81 
GetConstStringType()82   static const Type* GetConstStringType() {
83     return Get().GetBuiltinType(CONST_STRING_TYPE_STRING);
84   }
85 
GetIntPtrType()86   static const Type* GetIntPtrType() {
87     return Get().GetBuiltinType(INTPTR_TYPE_STRING);
88   }
89 
GetNeverType()90   static const Type* GetNeverType() {
91     return Get().GetBuiltinType(NEVER_TYPE_STRING);
92   }
93 
GetConstInt31Type()94   static const Type* GetConstInt31Type() {
95     return Get().GetBuiltinType(CONST_INT31_TYPE_STRING);
96   }
97 
IsImplicitlyConvertableFrom(const Type * to,const Type * from)98   static bool IsImplicitlyConvertableFrom(const Type* to, const Type* from) {
99     std::string name = GetGeneratedCallableName(kFromConstexprMacroName, {to});
100     return Get().declarations_->TryLookupMacro(name, {from}) != nullptr;
101   }
102 
103  private:
GetBuiltinType(const std::string & name)104   const Type* GetBuiltinType(const std::string& name) {
105     return declarations_->LookupGlobalType(name);
106   }
107 
108   Declarations* declarations_;
109   Deduplicator<FunctionPointerType> function_pointer_types_;
110   Deduplicator<UnionType> union_types_;
111   std::vector<std::unique_ptr<Type>> nominal_types_;
112   std::vector<std::unique_ptr<Type>> struct_types_;
113 };
114 
115 }  // namespace torque
116 }  // namespace internal
117 }  // namespace v8
118 
119 #endif  // V8_TORQUE_TYPE_ORACLE_H_
120