1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/sem/call_target.h"
16
17 #include "src/symbol_table.h"
18 #include "src/utils/hash.h"
19
20 TINT_INSTANTIATE_TYPEINFO(tint::sem::CallTarget);
21
22 namespace tint {
23 namespace sem {
24
CallTarget(const sem::Type * return_type,const ParameterList & parameters)25 CallTarget::CallTarget(const sem::Type* return_type,
26 const ParameterList& parameters)
27 : signature_{return_type, parameters} {
28 TINT_ASSERT(Semantic, return_type);
29 }
30
31 CallTarget::CallTarget(const CallTarget&) = default;
32 CallTarget::~CallTarget() = default;
33
CallTargetSignature(const sem::Type * ret_ty,const ParameterList & params)34 CallTargetSignature::CallTargetSignature(const sem::Type* ret_ty,
35 const ParameterList& params)
36 : return_type(ret_ty), parameters(params) {}
37 CallTargetSignature::CallTargetSignature(const CallTargetSignature&) = default;
38 CallTargetSignature::~CallTargetSignature() = default;
39
IndexOf(ParameterUsage usage) const40 int CallTargetSignature::IndexOf(ParameterUsage usage) const {
41 for (size_t i = 0; i < parameters.size(); i++) {
42 if (parameters[i]->Usage() == usage) {
43 return static_cast<int>(i);
44 }
45 }
46 return -1;
47 }
48
operator ==(const CallTargetSignature & other) const49 bool CallTargetSignature::operator==(const CallTargetSignature& other) const {
50 if (return_type != other.return_type ||
51 parameters.size() != other.parameters.size()) {
52 return false;
53 }
54 for (size_t i = 0; i < parameters.size(); i++) {
55 auto* a = parameters[i];
56 auto* b = other.parameters[i];
57 if (a->Type() != b->Type() || a->Usage() != b->Usage()) {
58 return false;
59 }
60 }
61 return true;
62 }
63
64 } // namespace sem
65 } // namespace tint
66
67 namespace std {
68
operator ()(const tint::sem::CallTargetSignature & sig) const69 std::size_t hash<tint::sem::CallTargetSignature>::operator()(
70 const tint::sem::CallTargetSignature& sig) const {
71 size_t hash = tint::utils::Hash(sig.parameters.size());
72 for (auto* p : sig.parameters) {
73 tint::utils::HashCombine(&hash, p->Type(), p->Usage());
74 }
75 return tint::utils::Hash(hash, sig.return_type);
76 }
77
78 } // namespace std
79