mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Add Ptr and RawNew for migration (#751)
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
1eeaa208f2
commit
c6ebe0db67
@@ -9,6 +9,7 @@ cc_library(
|
||||
srcs = ["arena.cpp"],
|
||||
hdrs = ["arena.h"],
|
||||
deps = [
|
||||
":ptr",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
@@ -31,6 +32,15 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "ptr",
|
||||
hdrs = ["ptr.h"],
|
||||
deps = [
|
||||
"//common:check",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "tracing_flag",
|
||||
srcs = ["tracing_flag.cpp"],
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "llvm/Support/ManagedStatic.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -16,10 +17,20 @@ class Arena {
|
||||
public:
|
||||
// Allocates an object in the arena, returning a pointer to it.
|
||||
template <typename T, typename... Args>
|
||||
auto New(Args&&... args) -> T* {
|
||||
auto New(Args&&... args) -> Ptr<T> {
|
||||
auto smart_ptr =
|
||||
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
|
||||
T* raw_ptr = smart_ptr->Instance();
|
||||
Ptr<T> ptr = smart_ptr->Instance();
|
||||
arena.push_back(std::move(smart_ptr));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// TODO: Remove. This is only to help findability during migration.
|
||||
template <typename T, typename... Args>
|
||||
auto RawNew(Args&&... args) -> T* {
|
||||
auto smart_ptr =
|
||||
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
|
||||
T* raw_ptr = smart_ptr->Instance().Get();
|
||||
arena.push_back(std::move(smart_ptr));
|
||||
return raw_ptr;
|
||||
}
|
||||
@@ -40,7 +51,7 @@ class Arena {
|
||||
explicit ArenaEntryTyped(Args&&... args)
|
||||
: instance(std::forward<Args>(args)...) {}
|
||||
|
||||
auto Instance() -> T* { return &instance; }
|
||||
auto Instance() -> Ptr<T> { return Ptr<T>(&instance); }
|
||||
|
||||
private:
|
||||
T instance;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "common/check.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A non-nullable pointer. Written as `Ptr<T>` instead of `T*`.
|
||||
template <typename T>
|
||||
class Ptr {
|
||||
public:
|
||||
explicit Ptr(T* ptr) : ptr(ptr) { CHECK(ptr != nullptr); }
|
||||
|
||||
template <typename OtherT,
|
||||
std::enable_if_t<std::is_convertible_v<OtherT*, T*>>* = nullptr>
|
||||
Ptr(Ptr<OtherT> other) {
|
||||
return Ptr<OtherT>(other.ptr);
|
||||
}
|
||||
|
||||
Ptr(std::nullptr_t) = delete;
|
||||
|
||||
Ptr(const Ptr& other) = default;
|
||||
Ptr& operator=(const Ptr& rhs) = default;
|
||||
|
||||
auto operator*() const -> T& { return *ptr; }
|
||||
auto operator->() const -> T* { return ptr; }
|
||||
|
||||
T* Get() const { return ptr; }
|
||||
|
||||
private:
|
||||
T* ptr;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
Reference in New Issue
Block a user