v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
parameter-difference.h
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
5#ifndef V8_TORQUE_PARAMETER_DIFFERENCE_H_
6#define V8_TORQUE_PARAMETER_DIFFERENCE_H_
7
8#include <optional>
9#include <vector>
10
11#include "src/torque/types.h"
12
13namespace v8::internal::torque {
14
16 public:
17 ParameterDifference(const TypeVector& to, const TypeVector& from) {
18 DCHECK_EQ(to.size(), from.size());
19 for (size_t i = 0; i < to.size(); ++i) {
20 AddParameter(to[i], from[i]);
21 }
22 }
23
24 // An overload is selected if it is strictly better than all alternatives.
25 // This means that it has to be strictly better in at least one parameter,
26 // and better or equally good in all others.
27 //
28 // When comparing a pair of corresponding parameters of two overloads...
29 // ... they are considered equally good if:
30 // - They are equal.
31 // - Both require some implicit conversion.
32 // ... one is considered better if:
33 // - It is a strict subtype of the other.
34 // - It doesn't require an implicit conversion, while the other does.
35 bool StrictlyBetterThan(const ParameterDifference& other) const {
36 DCHECK_EQ(difference_.size(), other.difference_.size());
37 bool better_parameter_found = false;
38 for (size_t i = 0; i < difference_.size(); ++i) {
39 std::optional<const Type*> a = difference_[i];
40 std::optional<const Type*> b = other.difference_[i];
41 if (a == b) {
42 continue;
43 } else if (a && b && a != b && (*a)->IsSubtypeOf(*b)) {
44 DCHECK(!(*b)->IsSubtypeOf(*a));
45 better_parameter_found = true;
46 } else if (a && !b) {
47 better_parameter_found = true;
48 } else {
49 return false;
50 }
51 }
52 return better_parameter_found;
53 }
54
55 private:
56 // Pointwise difference between call arguments and a signature.
57 // {std::nullopt} means that an implicit conversion was necessary,
58 // otherwise we store the supertype found in the signature.
59 std::vector<std::optional<const Type*>> difference_;
60
61 void AddParameter(const Type* to, const Type* from) {
62 if (from->IsSubtypeOf(to)) {
63 difference_.push_back(to);
64 } else if (IsAssignableFrom(to, from)) {
65 difference_.push_back(std::nullopt);
66 } else {
68 }
69 }
70};
71
72} // namespace v8::internal::torque
73
74#endif // V8_TORQUE_PARAMETER_DIFFERENCE_H_
std::vector< std::optional< const Type * > > difference_
ParameterDifference(const TypeVector &to, const TypeVector &from)
void AddParameter(const Type *to, const Type *from)
bool StrictlyBetterThan(const ParameterDifference &other) const
bool IsAssignableFrom(const Type *to, const Type *from)
Definition types.cc:1169
std::vector< const Type * > TypeVector
Definition types.h:85
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485