blob: 95f4556c89456da6f86461b43dff75052e3b13b3 [file] [log] [blame]
dan sinclair4d56b482022-12-08 17:50:50 +00001// Copyright 2022 The Tint Authors.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
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
dan sinclair4d56b482022-12-08 17:50:50 +000015#include "src/tint/type/pointer.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000016
dan sinclairdfb62152023-01-23 20:33:42 +000017#include "src/tint/debug.h"
18#include "src/tint/diagnostic/diagnostic.h"
19#include "src/tint/type/manager.h"
dan sinclair4d56b482022-12-08 17:50:50 +000020#include "src/tint/type/reference.h"
Ben Clayton43919752022-03-07 17:05:28 +000021#include "src/tint/utils/hash.h"
dan sinclairdee884c2023-02-28 22:22:58 +000022#include "src/tint/utils/string_stream.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000023
dan sinclair4d56b482022-12-08 17:50:50 +000024TINT_INSTANTIATE_TYPEINFO(tint::type::Pointer);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000025
dan sinclair4d56b482022-12-08 17:50:50 +000026namespace tint::type {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
Ben Clayton1a8a19d2023-06-13 17:01:16 +000028Pointer::Pointer(builtin::AddressSpace address_space, const Type* subtype, builtin::Access access)
dan sinclair12fa3032023-04-19 23:52:33 +000029 : Base(
30 utils::Hash(utils::TypeInfo::Of<Pointer>().full_hashcode, address_space, subtype, access),
31 type::Flags{}),
Ben Claytonce93a6b2022-12-19 17:07:29 +000032 subtype_(subtype),
33 address_space_(address_space),
34 access_(access) {
dan sinclair4d56b482022-12-08 17:50:50 +000035 TINT_ASSERT(Type, !subtype->Is<Reference>());
dan sinclairb6cc4cb2023-02-19 04:01:29 +000036 TINT_ASSERT(Type, access != builtin::Access::kUndefined);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037}
38
Ben Clayton7c3e9a62022-12-16 15:31:19 +000039bool Pointer::Equals(const UniqueNode& other) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 if (auto* o = other.As<Pointer>()) {
dan sinclairff7cf212022-10-03 14:05:23 +000041 return o->address_space_ == address_space_ && o->subtype_ == subtype_ &&
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 o->access_ == access_;
43 }
44 return false;
Ben Clayton43919752022-03-07 17:05:28 +000045}
46
dan sinclaird026e132023-04-18 19:38:25 +000047std::string Pointer::FriendlyName() const {
dan sinclairdee884c2023-02-28 22:22:58 +000048 utils::StringStream out;
dan sinclair41e4d9a2022-05-01 14:40:55 +000049 out << "ptr<";
dan sinclair2a651632023-02-19 04:03:55 +000050 if (address_space_ != builtin::AddressSpace::kUndefined) {
dan sinclairff7cf212022-10-03 14:05:23 +000051 out << address_space_ << ", ";
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 }
dan sinclaird026e132023-04-18 19:38:25 +000053 out << subtype_->FriendlyName() << ", " << access_;
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 out << ">";
55 return out.str();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056}
57
Ryan Harrisondbc13af2022-02-21 15:19:07 +000058Pointer::~Pointer() = default;
59
dan sinclairf8abdc72023-01-05 21:07:15 +000060Pointer* Pointer::Clone(CloneContext& ctx) const {
61 auto* ty = subtype_->Clone(ctx);
Ben Clayton1a8a19d2023-06-13 17:01:16 +000062 return ctx.dst.mgr->Get<Pointer>(address_space_, ty, access_);
dan sinclairf8abdc72023-01-05 21:07:15 +000063}
64
dan sinclair4d56b482022-12-08 17:50:50 +000065} // namespace tint::type