mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:21:16 +01:00
Add partial support for initializing expressions for variable declaration. 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. ## Functional changes * Initializing expressions initialize directly the provided storage when used to initialize a variable. * Allows initializing expressions to avoid a copy when using `[var|let] name: type = call_expression(...)` by initializing `name` in-place. * Support `returned var: ...` and `return <expr>` * Support nested initializing expressions ## Main implementation changes * Updated PatternMatch logic to handle expression categories * Updated `VariableDefinition` interpreter statement to allocate and pass a location to initializing expressions * Update statement actions to allow passing an allocation, used by return expr or returned var * Modified the RuntimeScope API to be one step closer to the memory model we want to have * Remove `GetAllocationId` and older `Bind` which don't apply * New set of tests to highlight those different situations * Added a new intrinsic to print the allocation stack (and make sure we behave correctly, beyond visible side effects) ## Next work * Dedicated `Action` to retrieve expression category information in the interpreter (https://github.com/carbon-language/carbon-lang/pull/2927) * Avoid copies when initializing value expression from reference expression and prevent mutations for the duration of the "pinning" (https://github.com/carbon-language/carbon-lang/pull/2927) * Avoid unnecessary copies from value expression to value expression, after ensuring that even value expression temporaries are registered for destruction. * Avoid unnecessary copies when binding function arguments
98 lines
3.1 KiB
C++
98 lines
3.1 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_AST_ADDRESS_H_
|
|
#define CARBON_EXPLORER_AST_ADDRESS_H_
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "common/check.h"
|
|
#include "common/ostream.h"
|
|
#include "explorer/ast/element_path.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// An AllocationId identifies an _allocation_ produced by a Heap. An allocation
|
|
// is analogous to the C++ notion of a complete object: the `Value` in an
|
|
// allocation is not a sub-part of any other `Value`.
|
|
class AllocationId {
|
|
public:
|
|
AllocationId(const AllocationId&) = default;
|
|
auto operator=(const AllocationId&) -> AllocationId& = default;
|
|
|
|
// Prints a human-readable representation of *this to `out`.
|
|
//
|
|
// Currently that representation consists of an integer index.
|
|
void Print(llvm::raw_ostream& out) const {
|
|
out << "Allocation(" << index_ << ")";
|
|
}
|
|
|
|
private:
|
|
// The representation of AllocationId describes how to locate an object within
|
|
// a Heap, so its implementation details are tied to the implementation
|
|
// details of Heap.
|
|
friend class Heap;
|
|
|
|
explicit AllocationId(size_t index) : index_(index) {}
|
|
|
|
size_t index_;
|
|
};
|
|
|
|
// An Address represents a memory address in the Carbon virtual machine.
|
|
// Addresses are used to access values stored in a Heap. Unlike an
|
|
// AllocationId, an Address can refer to a sub-Value of some larger Value.
|
|
class Address {
|
|
public:
|
|
// Constructs an `Address` that refers to the value stored in `allocation`.
|
|
explicit Address(AllocationId allocation) : allocation_(allocation) {}
|
|
|
|
Address(const Address&) = default;
|
|
Address(Address&&) = default;
|
|
auto operator=(const Address&) -> Address& = default;
|
|
auto operator=(Address&&) -> Address& = default;
|
|
|
|
// Prints a human-readable representation of `a` to `out`.
|
|
//
|
|
// Currently, that representation consists of an AllocationId followed by an
|
|
// optional ElementPath specifying a particular field within that allocation.
|
|
void Print(llvm::raw_ostream& out) const {
|
|
out << allocation_ << element_path_;
|
|
}
|
|
|
|
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
|
|
|
// If *this represents the address of an object with a field named
|
|
// `field_name`, this method returns the address of that field.
|
|
auto ElementAddress(Nonnull<const Element*> element) const -> Address {
|
|
Address result = *this;
|
|
result.element_path_.Append(element);
|
|
return result;
|
|
}
|
|
|
|
// Drop all trailing BaseElements from the element path, returning the
|
|
// downcasted address.
|
|
auto DowncastedAddress() const -> Address {
|
|
Address address = *this;
|
|
address.element_path_.RemoveTrailingBaseElements();
|
|
return address;
|
|
}
|
|
|
|
private:
|
|
// The representation of Address describes how to locate an object within
|
|
// the Heap, so its implementation details are tied to the implementation
|
|
// details of the Heap.
|
|
friend class Heap;
|
|
friend class RuntimeScope;
|
|
|
|
AllocationId allocation_;
|
|
ElementPath element_path_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_EXPLORER_AST_ADDRESS_H_
|