Rename Ptr<T> to Nonnull<T*> (#832)

Note that ptr.h also includes an enable_if change, to help avoid https://bugs.llvm.org/show_bug.cgi?id=51881
This commit is contained in:
Jon Meow
2021-09-16 19:22:57 -07:00
committed by GitHub
parent 5e66248db0
commit 56dc4ae375
35 changed files with 584 additions and 535 deletions
+3 -3
View File
@@ -16,10 +16,10 @@ class Arena {
public:
// Allocates an object in the arena, returning a pointer to it.
template <typename T, typename... Args>
auto New(Args&&... args) -> Ptr<T> {
auto New(Args&&... args) -> Nonnull<T*> {
auto smart_ptr =
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
Ptr<T> ptr = smart_ptr->Instance();
Nonnull<T*> ptr = smart_ptr->Instance();
arena.push_back(std::move(smart_ptr));
return ptr;
}
@@ -40,7 +40,7 @@ class Arena {
explicit ArenaEntryTyped(Args&&... args)
: instance(std::forward<Args>(args)...) {}
auto Instance() -> Ptr<T> { return Ptr<T>(&instance); }
auto Instance() -> Nonnull<T*> { return Nonnull<T*>(&instance); }
private:
T instance;
+10 -3
View File
@@ -5,11 +5,18 @@
#ifndef EXECUTABLE_SEMANTICS_COMMON_PTR_H_
#define EXECUTABLE_SEMANTICS_COMMON_PTR_H_
#include <type_traits>
namespace Carbon {
// A non-nullable pointer. Written as `Ptr<T>` instead of `T*`.
template <typename T>
using Ptr = T* _Nonnull __attribute__((nonnull));
// A non-nullable pointer. Written as `Nonnull<T*>` instead of `T*`.
//
// Note LLVM primarily enforces the attribute on function calls that can be
// proven to be called with nullptr; in other places, this is essentially a
// comment.
template <typename T,
typename std::enable_if_t<std::is_pointer_v<T>>* = nullptr>
using Nonnull = T _Nonnull __attribute__((nonnull));
} // namespace Carbon