|
| 1 | +/* vim: set sts=4 ts=8 sw=4 tw=99 et: */ |
| 2 | +// |
| 3 | +// Copyright (c) AlliedModders LLC, 2024 |
| 4 | +// |
| 5 | +// This software is provided "as-is", without any express or implied warranty. |
| 6 | +// In no event will the authors be held liable for any damages arising from |
| 7 | +// the use of this software. |
| 8 | +// |
| 9 | +// Permission is granted to anyone to use this software for any purpose, |
| 10 | +// including commercial applications, and to alter it and redistribute it |
| 11 | +// freely, subject to the following restrictions: |
| 12 | +// |
| 13 | +// 1. The origin of this software must not be misrepresented; you must not |
| 14 | +// claim that you wrote the original software. If you use this software in |
| 15 | +// a product, an acknowledgment in the product documentation would be |
| 16 | +// appreciated but is not required. |
| 17 | +// 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | +// misrepresented as being the original software. |
| 19 | +// 3. This notice may not be removed or altered from any source distribution. |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <amtl/am-bits.h> |
| 23 | +#include <amtl/am-hashtable.h> |
| 24 | + |
| 25 | +namespace sp { |
| 26 | +namespace cc { |
| 27 | + |
| 28 | +class Type; |
| 29 | + |
| 30 | +// Compact encoding of type + constness. |
| 31 | +class QualType { |
| 32 | + public: |
| 33 | + explicit QualType(Type* type) { |
| 34 | + impl_ = type; |
| 35 | + } |
| 36 | + explicit QualType(Type* type, bool is_const) { |
| 37 | + impl_ = ke::SetPointerBits(type, is_const ? 1 : 0); |
| 38 | + } |
| 39 | + |
| 40 | + bool is_const() const { |
| 41 | + return ke::GetPointerBits<2>(impl_) == 1; |
| 42 | + } |
| 43 | + |
| 44 | + Type* operator *() const { return ptr(); } |
| 45 | + Type* operator ->() const { return ptr(); } |
| 46 | + Type* ptr() const { |
| 47 | + return ke::ClearPointerBits<2>(impl_); |
| 48 | + } |
| 49 | + QualType unqualified() const { return QualType(ptr()); } |
| 50 | + |
| 51 | + uint32_t hash() const { return ke::HashPointer(impl_); } |
| 52 | + |
| 53 | + bool operator ==(const QualType& other) const { return impl_ == other.impl_; } |
| 54 | + bool operator !=(const QualType& other) const { return impl_ != other.impl_; } |
| 55 | + |
| 56 | + private: |
| 57 | + Type* impl_; |
| 58 | +}; |
| 59 | + |
| 60 | +} // namespace cc |
| 61 | +} // namespace sp |
0 commit comments