dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 15 | #include "src/tint/type/pointer.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 16 | |
dan sinclair | dfb6215 | 2023-01-23 20:33:42 +0000 | [diff] [blame] | 17 | #include "src/tint/debug.h" |
| 18 | #include "src/tint/diagnostic/diagnostic.h" |
| 19 | #include "src/tint/type/manager.h" |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 20 | #include "src/tint/type/reference.h" |
Ben Clayton | 4391975 | 2022-03-07 17:05:28 +0000 | [diff] [blame] | 21 | #include "src/tint/utils/hash.h" |
dan sinclair | dee884c | 2023-02-28 22:22:58 +0000 | [diff] [blame] | 22 | #include "src/tint/utils/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 24 | TINT_INSTANTIATE_TYPEINFO(tint::type::Pointer); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 26 | namespace tint::type { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
Ben Clayton | 1a8a19d | 2023-06-13 17:01:16 +0000 | [diff] [blame] | 28 | Pointer::Pointer(builtin::AddressSpace address_space, const Type* subtype, builtin::Access access) |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 29 | : Base( |
| 30 | utils::Hash(utils::TypeInfo::Of<Pointer>().full_hashcode, address_space, subtype, access), |
| 31 | type::Flags{}), |
Ben Clayton | ce93a6b | 2022-12-19 17:07:29 +0000 | [diff] [blame] | 32 | subtype_(subtype), |
| 33 | address_space_(address_space), |
| 34 | access_(access) { |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 35 | TINT_ASSERT(Type, !subtype->Is<Reference>()); |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 36 | TINT_ASSERT(Type, access != builtin::Access::kUndefined); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 39 | bool Pointer::Equals(const UniqueNode& other) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | if (auto* o = other.As<Pointer>()) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 41 | return o->address_space_ == address_space_ && o->subtype_ == subtype_ && |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | o->access_ == access_; |
| 43 | } |
| 44 | return false; |
Ben Clayton | 4391975 | 2022-03-07 17:05:28 +0000 | [diff] [blame] | 45 | } |
| 46 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 47 | std::string Pointer::FriendlyName() const { |
dan sinclair | dee884c | 2023-02-28 22:22:58 +0000 | [diff] [blame] | 48 | utils::StringStream out; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | out << "ptr<"; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 50 | if (address_space_ != builtin::AddressSpace::kUndefined) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 51 | out << address_space_ << ", "; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | } |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 53 | out << subtype_->FriendlyName() << ", " << access_; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | out << ">"; |
| 55 | return out.str(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | Pointer::~Pointer() = default; |
| 59 | |
dan sinclair | f8abdc7 | 2023-01-05 21:07:15 +0000 | [diff] [blame] | 60 | Pointer* Pointer::Clone(CloneContext& ctx) const { |
| 61 | auto* ty = subtype_->Clone(ctx); |
Ben Clayton | 1a8a19d | 2023-06-13 17:01:16 +0000 | [diff] [blame] | 62 | return ctx.dst.mgr->Get<Pointer>(address_space_, ty, access_); |
dan sinclair | f8abdc7 | 2023-01-05 21:07:15 +0000 | [diff] [blame] | 63 | } |
| 64 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 65 | } // namespace tint::type |