Files
carbon-lang/explorer/interpreter/heap_allocation_interface.h
T
Adrien Leravat d02366f881 Explorer: Prevent copies when initializing a let binding from reference expression (#2946)
Prevent copies when initializing value expression from reference
expression. This is based on
https://github.com/carbon-language/carbon-lang/pull/2006, which
introduces expression categories, and how it is possible to convert
to/from those different categories. Continuation of
https://github.com/carbon-language/carbon-lang/pull/2907

## Functional changes

* Initializing a value expression from a reference expression takes its
value without a copy
* Reading from the value expression causes an error if the value changed
from the time it was initialized
* In this situation, prevents a copy both for variable definitions, and
call parameter bindings

## Main implementation changes

* Add new `ExpressionCategoryAction`, which evaluates an expression and
returns an `ExpressionValue` containing its category and address (if
any), in addition to the resulting `Value*`
* `ExpressionAction`s now invokes `ExpressionCategoryAction` and unwraps
the returned `ExpressionValue`
* `RuntimeScope::BindAndPin` method, and corresponding when attempting
to read a `value_node`.

## Next work

* Avoid unnecessary copies from value expression to value expression,
after ensuring that even value expression temporaries are registered for
destruction (https://github.com/Pixep/carbon-lang/pull/9)
2023-07-17 23:26:01 +00:00

62 lines
2.3 KiB
C++

// 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 CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
#define CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
#include "common/error.h"
#include "explorer/ast/address.h"
#include "explorer/ast/value_node.h"
#include "explorer/common/arena.h"
#include "explorer/common/nonnull.h"
#include "explorer/common/source_location.h"
namespace Carbon {
class Value;
// The allocation interface for Heap, factored out as an interface in order to
// resolve a layering issue. No other class should derive from this.
class HeapAllocationInterface {
public:
HeapAllocationInterface(const HeapAllocationInterface&) = delete;
auto operator=(const HeapAllocationInterface&)
-> HeapAllocationInterface& = delete;
// Returns the value at the given address in the heap after
// checking that it is alive.
virtual auto Read(const Address& a, SourceLocation source_loc) const
-> ErrorOr<Nonnull<const Value*>> = 0;
// Writes the given value at the address in the heap after
// checking that the address is alive.
virtual auto Write(const Address& a, Nonnull<const Value*> v,
SourceLocation source_loc) -> ErrorOr<Success> = 0;
// Put the given value on the heap and mark it as alive.
virtual auto AllocateValue(Nonnull<const Value*> v) -> AllocationId = 0;
// Marks this allocation, and all of its sub-objects, as dead.
virtual auto Deallocate(AllocationId allocation) -> ErrorOr<Success> = 0;
// Returns the arena used to allocate the values in this heap.
virtual auto arena() const -> Arena& = 0;
// Binds a value node to a reference, and manages its lifetime.
virtual void BindValueToReference(const ValueNodeView& node,
const Address& a) = 0;
// Returns whether the value bound at the given node is still alive.
virtual auto is_bound_value_alive(const ValueNodeView& node,
const Address& a) const -> bool = 0;
protected:
HeapAllocationInterface() = default;
virtual ~HeapAllocationInterface() = default;
};
} // namespace Carbon
#endif // CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_