v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
type-inference.h
Go to the documentation of this file.
1// Copyright 2019 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_INFERENCE_H_
6#define V8_TORQUE_TYPE_INFERENCE_H_
7
8#include <optional>
9#include <string>
10#include <unordered_map>
11
12#include "src/torque/ast.h"
14#include "src/torque/types.h"
15
16namespace v8::internal::torque {
17
18// Type argument inference computes a potential instantiation of a generic
19// callable given some concrete argument types. As an example, consider the
20// generic macro
21//
22// macro Pick<T: type>(x: T, y: T): T
23//
24// along with a given call site, such as
25//
26// Pick(1, 2);
27//
28// The inference proceeds by matching the term argument types (`constexpr
29// int31`, in case of `1` and `2`) against the formal parameter types (`T` in
30// both cases). During this matching we discover that `T` must equal `constexpr
31// int31`.
32//
33// The inference will not perform any comprehensive type checking of its own,
34// but *does* fail if type parameters cannot be soundly instantiated given the
35// call site. For instance, for the following call site
36//
37// const aSmi: Smi = ...;
38// Pick(1, aSmi); // inference fails
39//
40// inference would fail, since `constexpr int31` is distinct from `Smi`. To
41// allow for implicit conversions to be tried in a separate step after type
42// argument inference, a number of type arguments may be given explicitly:
43//
44// Pick<Smi>(1, aSmi); // inference succeeds (doing nothing)
45//
46// In the above case the inference simply ignores inconsistent constraints on
47// `T`. Similarly, we ignore all constraints arising from formal parameters
48// that are function- or union-typed.
49//
50// Finally, note that term parameters are passed as type expressions, since
51// we have no way of expressing a reference to type parameter as a Type. These
52// type expressions are resolved during matching, so TypeArgumentInference
53// should be instantiated in the appropriate scope.
55 public:
57 const GenericParameters& type_parameters,
58 const TypeVector& explicit_type_arguments,
59 const std::vector<TypeExpression*>& term_parameters,
60 const std::vector<std::optional<const Type*>>& term_argument_types);
61
62 bool HasFailed() const { return failure_reason_.has_value(); }
63 const std::string& GetFailureReason() { return *failure_reason_; }
64 TypeVector GetResult() const;
65 void Fail(std::string reason) { failure_reason_ = {reason}; }
66
67 private:
68 void Match(TypeExpression* parameter, const Type* argument_type);
69 void MatchGeneric(BasicTypeExpression* parameter, const Type* argument_type);
70
72 std::unordered_map<std::string, size_t> type_parameter_from_name_;
73 std::vector<std::optional<const Type*>> inferred_;
74 std::optional<std::string> failure_reason_;
75};
76
77} // namespace v8::internal::torque
78
79#endif // V8_TORQUE_TYPE_INFERENCE_H_
std::unordered_map< std::string, size_t > type_parameter_from_name_
TypeArgumentInference(const GenericParameters &type_parameters, const TypeVector &explicit_type_arguments, const std::vector< TypeExpression * > &term_parameters, const std::vector< std::optional< const Type * > > &term_argument_types)
std::optional< std::string > failure_reason_
std::vector< std::optional< const Type * > > inferred_
void MatchGeneric(BasicTypeExpression *parameter, const Type *argument_type)
void Match(TypeExpression *parameter, const Type *argument_type)
std::vector< GenericParameter > GenericParameters
Definition ast.h:1129
std::vector< const Type * > TypeVector
Definition types.h:85