address.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_ADDRESS_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_ADDRESS_H_
  6. #include <cstdint>
  7. #include <string>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/interpreter/field_path.h"
  11. #include "llvm/Support/Compiler.h"
  12. namespace Carbon {
  13. // An AllocationId identifies an _allocation_ produced by a Heap. An allocation
  14. // is analogous to the C++ notion of a complete object: the the `Value` in an
  15. // allocation is not a sub-part of any other `Value`.
  16. class AllocationId {
  17. public:
  18. AllocationId(const AllocationId&) = default;
  19. auto operator=(const AllocationId&) -> AllocationId& = default;
  20. // Prints a human-readable representation of *this to `out`.
  21. //
  22. // Currently that representation consists of an integer index.
  23. void Print(llvm::raw_ostream& out) const {
  24. out << "Allocation(" << index_ << ")";
  25. }
  26. private:
  27. // The representation of AllocationId describes how to locate an object within
  28. // a Heap, so its implementation details are tied to the implementation
  29. // details of Heap.
  30. friend class Heap;
  31. explicit AllocationId(size_t index) : index_(index) {}
  32. size_t index_;
  33. };
  34. // An Address represents a memory address in the Carbon virtual machine.
  35. // Addresses are used to access values stored in a Heap. Unlike an
  36. // AllocationId, an Address can refer to a sub-Value of some larger Value.
  37. class Address {
  38. public:
  39. // Constructs an `Address` that refers to the value stored in `allocation`.
  40. explicit Address(AllocationId allocation) : allocation_(allocation) {}
  41. Address(const Address&) = default;
  42. Address(Address&&) = default;
  43. auto operator=(const Address&) -> Address& = default;
  44. auto operator=(Address&&) -> Address& = default;
  45. // Prints a human-readable representation of `a` to `out`.
  46. //
  47. // Currently, that representation consists of an AllocationId followed by an
  48. // optional FieldPath specifying a particular field within that allocation.
  49. void Print(llvm::raw_ostream& out) const {
  50. out << allocation_ << field_path_;
  51. }
  52. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  53. // If *this represents the address of an object with a field named
  54. // `field_name`, this method returns the address of that field.
  55. auto SubobjectAddress(std::string field_name) const -> Address {
  56. Address result = *this;
  57. result.field_path_.Append(std::move(field_name));
  58. return result;
  59. }
  60. private:
  61. // The representation of Address describes how to locate an object within
  62. // the Heap, so its implementation details are tied to the implementation
  63. // details of the Heap.
  64. friend class Heap;
  65. AllocationId allocation_;
  66. FieldPath field_path_;
  67. };
  68. } // namespace Carbon
  69. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ADDRESS_H_