v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
type-oracle.cc
Go to the documentation of this file.
1// Copyright 2018 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
6
7#include <optional>
8
10#include "src/torque/types.h"
11
12namespace v8::internal::torque {
13
14// static
15const std::vector<std::unique_ptr<AggregateType>>&
17 return Get().aggregate_types_;
18}
19
20// static
21const std::vector<std::unique_ptr<BitFieldStructType>>&
23 return Get().bit_field_struct_types_;
24}
25
26// static
28 size_t current = 0;
29 while (current != Get().aggregate_types_.size()) {
30 auto& p = Get().aggregate_types_[current++];
31 p->Finalize();
32 }
33}
34
35// static
37 TypeVector arg_types) {
38 auto& params = generic_type->generic_parameters();
39
40 if (params.size() != arg_types.size()) {
41 ReportError("Generic struct takes ", params.size(), " parameters, but ",
42 arg_types.size(), " were given");
43 }
44
45 if (auto specialization = generic_type->GetSpecialization(arg_types)) {
46 return *specialization;
47 } else {
48 const Type* type = nullptr;
49 // AddSpecialization can raise an error, which should be reported in the
50 // scope of the code requesting the specialization, not the generic type's
51 // parent scope, hence the following block.
52 {
53 v8::internal::torque::Scope* requester_scope = CurrentScope::Get();
54 CurrentScope::Scope generic_scope(generic_type->ParentScope());
55 type = TypeVisitor::ComputeType(generic_type->declaration(),
56 {{generic_type, arg_types}},
57 requester_scope);
58 }
59 generic_type->AddSpecialization(arg_types, type);
60 return type;
61 }
62}
63
64// static
65Namespace* TypeOracle::CreateGenericTypeInstantiationNamespace() {
66 Get().generic_type_instantiation_namespaces_.push_back(
67 std::make_unique<Namespace>(GENERIC_TYPE_INSTANTIATION_NAMESPACE_STRING));
68 return Get().generic_type_instantiation_namespaces_.back().get();
69}
70
71// static
72std::vector<const ClassType*> TypeOracle::GetClasses() {
73 std::vector<const ClassType*> result;
74 for (const std::unique_ptr<AggregateType>& t : Get().aggregate_types_) {
75 if (auto* class_type = ClassType::DynamicCast(t.get())) {
76 result.push_back(class_type);
77 }
78 }
79 return result;
80}
81
82std::optional<const Type*> TypeOracle::MatchReferenceGeneric(
83 const Type* reference_type, bool* is_const) {
84 if (auto type = Type::MatchUnaryGeneric(reference_type,
85 GetMutableReferenceGeneric())) {
86 if (is_const) *is_const = false;
87 return type;
88 }
89 if (auto type =
90 Type::MatchUnaryGeneric(reference_type, GetConstReferenceGeneric())) {
91 if (is_const) *is_const = true;
92 return type;
93 }
94 return std::nullopt;
95}
96
97} // namespace v8::internal::torque
static VarType & Get()
Definition contextual.h:64
void push_back(const T &value)
void AddSpecialization(const TypeVector &type_arguments, SpecializationType specialization)
Definition declarable.h:589
std::optional< SpecializationType > GetSpecialization(const TypeVector &type_arguments) const
Definition declarable.h:598
const GenericParameters & generic_parameters() const
Definition declarable.h:611
std::vector< std::unique_ptr< AggregateType > > aggregate_types_
static const std::vector< std::unique_ptr< AggregateType > > & GetAggregateTypes()
static const Type * GetGenericTypeInstance(GenericType *generic_type, TypeVector arg_types)
static const std::vector< std::unique_ptr< BitFieldStructType > > & GetBitFieldStructTypes()
static const Type * ComputeType(TypeExpression *type_expression)
ZoneVector< RpoNumber > & result
V8_INLINE const Operation & Get(const Graph &graph, OpIndex index)
Definition graph.h:1231
void ReportError(Args &&... args)
Definition utils.h:96
std::vector< const Type * > TypeVector
Definition types.h:85
wasm::ValueType type