address.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 CARBON_EXPLORER_AST_ADDRESS_H_
  5. #define CARBON_EXPLORER_AST_ADDRESS_H_
  6. #include <cstdint>
  7. #include <string>
  8. #include <vector>
  9. #include "common/check.h"
  10. #include "common/ostream.h"
  11. #include "explorer/ast/element_path.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 `Value` in an
  15. // allocation is not a sub-part of any other `Value`.
  16. class AllocationId : public Printable<AllocationId> {
  17. public:
  18. AllocationId(const AllocationId&) = default;
  19. auto operator=(const AllocationId&) -> AllocationId& = default;
  20. inline friend auto operator==(AllocationId lhs, AllocationId rhs) -> bool {
  21. return lhs.index_ == rhs.index_;
  22. }
  23. inline friend auto hash_value(AllocationId id) {
  24. return llvm::hash_combine(id.index_);
  25. }
  26. // Prints a human-readable representation of *this to `out`.
  27. //
  28. // Currently that representation consists of an integer index.
  29. void Print(llvm::raw_ostream& out) const {
  30. out << "Allocation(" << index_ << ")";
  31. }
  32. private:
  33. // The representation of AllocationId describes how to locate an object within
  34. // a Heap, so its implementation details are tied to the implementation
  35. // details of Heap.
  36. friend class Heap;
  37. explicit AllocationId(size_t index) : index_(index) {}
  38. size_t index_;
  39. };
  40. // An Address represents a memory address in the Carbon virtual machine.
  41. // Addresses are used to access values stored in a Heap. Unlike an
  42. // AllocationId, an Address can refer to a sub-Value of some larger Value.
  43. class Address : public Printable<Address> {
  44. public:
  45. // Constructs an `Address` that refers to the value stored in `allocation`.
  46. explicit Address(AllocationId allocation) : allocation_(allocation) {}
  47. Address(const Address&) = default;
  48. Address(Address&&) = default;
  49. auto operator=(const Address&) -> Address& = default;
  50. auto operator=(Address&&) -> Address& = default;
  51. // Prints a human-readable representation of `a` to `out`.
  52. //
  53. // Currently, that representation consists of an AllocationId followed by an
  54. // optional ElementPath specifying a particular field within that allocation.
  55. void Print(llvm::raw_ostream& out) const {
  56. out << allocation_ << element_path_;
  57. }
  58. // If *this represents the address of an object with a field named
  59. // `field_name`, this method returns the address of that field.
  60. auto ElementAddress(Nonnull<const Element*> element) const -> Address {
  61. Address result = *this;
  62. result.element_path_.Append(element);
  63. return result;
  64. }
  65. // Drop all trailing BaseElements from the element path, returning the
  66. // downcasted address.
  67. auto DowncastedAddress() const -> Address {
  68. Address address = *this;
  69. address.element_path_.RemoveTrailingBaseElements();
  70. return address;
  71. }
  72. inline friend auto operator==(const Address& lhs, const Address& rhs)
  73. -> bool {
  74. return lhs.allocation_ == rhs.allocation_ &&
  75. lhs.element_path_ == rhs.element_path_;
  76. }
  77. inline friend auto hash_value(const Address& a) -> llvm::hash_code {
  78. return llvm::hash_combine(a.allocation_, a.element_path_);
  79. }
  80. private:
  81. // The representation of Address describes how to locate an object within
  82. // the Heap, so its implementation details are tied to the implementation
  83. // details of the Heap.
  84. friend class Heap;
  85. friend class RuntimeScope;
  86. AllocationId allocation_;
  87. ElementPath element_path_;
  88. };
  89. } // namespace Carbon
  90. #endif // CARBON_EXPLORER_AST_ADDRESS_H_