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 #ifndef SRC_SEM_CALL_TARGET_H_ 16 #define SRC_SEM_CALL_TARGET_H_ 17 18 #include <vector> 19 20 #include "src/sem/node.h" 21 #include "src/sem/sampler_type.h" 22 #include "src/sem/variable.h" 23 #include "src/utils/hash.h" 24 25 namespace tint { 26 namespace sem { 27 // Forward declarations 28 class Type; 29 30 /// CallTargetSignature holds the return type and parameters for a call target 31 struct CallTargetSignature { 32 /// Constructor 33 /// @param ret_ty the call target return type 34 /// @param params the call target parameters 35 CallTargetSignature(const sem::Type* ret_ty, const ParameterList& params); 36 37 /// Copy constructor 38 CallTargetSignature(const CallTargetSignature&); 39 40 /// Destructor 41 ~CallTargetSignature(); 42 43 /// The type of the call target return value 44 const sem::Type* const return_type = nullptr; 45 /// The parameters of the call target 46 const ParameterList parameters; 47 48 /// Equality operator 49 /// @param other the signature to compare this to 50 /// @returns true if this signature is equal to other 51 bool operator==(const CallTargetSignature& other) const; 52 53 /// @param usage the parameter usage to find 54 /// @returns the index of the parameter with the given usage, or -1 if no 55 /// parameter with the given usage exists. 56 int IndexOf(ParameterUsage usage) const; 57 }; 58 59 /// CallTarget is the base for callable functions, intrinsics, type constructors 60 /// and type casts. 61 class CallTarget : public Castable<CallTarget, Node> { 62 public: 63 /// Constructor 64 /// @param return_type the return type of the call target 65 /// @param parameters the parameters for the call target 66 CallTarget(const sem::Type* return_type, const ParameterList& parameters); 67 68 /// Copy constructor 69 CallTarget(const CallTarget&); 70 71 /// Destructor 72 ~CallTarget() override; 73 74 /// @return the return type of the call target ReturnType()75 const sem::Type* ReturnType() const { return signature_.return_type; } 76 77 /// @return the parameters of the call target Parameters()78 const ParameterList& Parameters() const { return signature_.parameters; } 79 80 /// @return the signature of the call target Signature()81 const CallTargetSignature& Signature() const { return signature_; } 82 83 private: 84 CallTargetSignature signature_; 85 }; 86 87 } // namespace sem 88 } // namespace tint 89 90 namespace std { 91 92 /// Custom std::hash specialization for tint::sem::CallTargetSignature so 93 /// CallTargetSignature can be used as keys for std::unordered_map and 94 /// std::unordered_set. 95 template <> 96 class hash<tint::sem::CallTargetSignature> { 97 public: 98 /// @param sig the CallTargetSignature to hash 99 /// @return the hash value 100 std::size_t operator()(const tint::sem::CallTargetSignature& sig) const; 101 }; 102 103 } // namespace std 104 105 #endif // SRC_SEM_CALL_TARGET_H_ 106