Files
carbon-lang/explorer/interpreter/address.h
T
Jon Meow 309ec35f95 Rename executable_semantics to explorer (#1188)
Change generated with:

```
#!/usr/bin/bash -eux

# Helper script for renaming pending work.
# Run from the repo root.

# Rename executable_semantics in code.
sed -i 's/executable_semantics/explorer/g' \
  $(git grep -l 'executable_semantics' . | grep -v proposals)
sed -i 's/executable semantics/explorer/g' \
  $(git grep -l 'executable semantics' . | grep -v proposals)
sed -i 's/Executable semantics/Explorer/g' \
  $(git grep -l 'Executable semantics' . |  grep -v proposals)
sed -i 's/Executable Semantics/Explorer/g' \
  $(git grep -l 'Executable Semantics' . |  grep -v proposals)
sed -i 's/EXECUTABLE_SEMANTICS/EXPLORER/g' \
  $(git grep -l 'EXECUTABLE_SEMANTICS' . | grep -v proposals)
sed -i 's/ExecutableSemantics/Explorer/g' \
  $(git grep -l 'ExecutableSemantics' . | grep -v proposals)
sed -i 's/executable-semantics/explorer/g' \
  $(git grep -l 'executable-semantics' . | grep -v proposals)

# This is only needed for the initial move.
mv executable_semantics explorer
mv explorer/fuzzing/executable_semantics_fuzzer.cpp explorer/fuzzing/explorer_fuzzer.cpp
```

Verified with `bazel test ...`
2022-04-29 13:20:25 -07:00

88 lines
2.8 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 EXPLORER_INTERPRETER_ADDRESS_H_
#define EXPLORER_INTERPRETER_ADDRESS_H_
#include <cstdint>
#include <string>
#include <vector>
#include "common/ostream.h"
#include "explorer/interpreter/field_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 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 FieldPath specifying a particular field within that allocation.
void Print(llvm::raw_ostream& out) const {
out << allocation_ << field_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 SubobjectAddress(std::string field_name) const -> Address {
Address result = *this;
result.field_path_.Append(std::move(field_name));
return result;
}
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;
AllocationId allocation_;
FieldPath field_path_;
};
} // namespace Carbon
#endif // EXPLORER_INTERPRETER_ADDRESS_H_