address.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "llvm/Support/Compiler.h"
  13. namespace Carbon {
  14. // An AllocationId identifies an _allocation_ produced by a Heap. An allocation
  15. // is analogous to the C++ notion of a complete object: the `Value` in an
  16. // allocation is not a sub-part of any other `Value`.
  17. class AllocationId {
  18. public:
  19. AllocationId(const AllocationId&) = default;
  20. auto operator=(const AllocationId&) -> AllocationId& = default;
  21. // Prints a human-readable representation of *this to `out`.
  22. //
  23. // Currently that representation consists of an integer index.
  24. void Print(llvm::raw_ostream& out) const {
  25. out << "Allocation(" << index_ << ")";
  26. }
  27. private:
  28. // The representation of AllocationId describes how to locate an object within
  29. // a Heap, so its implementation details are tied to the implementation
  30. // details of Heap.
  31. friend class Heap;
  32. explicit AllocationId(size_t index) : index_(index) {}
  33. size_t index_;
  34. };
  35. // An Address represents a memory address in the Carbon virtual machine.
  36. // Addresses are used to access values stored in a Heap. Unlike an
  37. // AllocationId, an Address can refer to a sub-Value of some larger Value.
  38. class Address {
  39. public:
  40. // Constructs an `Address` that refers to the value stored in `allocation`.
  41. explicit Address(AllocationId allocation) : allocation_(allocation) {}
  42. Address(const Address&) = default;
  43. Address(Address&&) = default;
  44. auto operator=(const Address&) -> Address& = default;
  45. auto operator=(Address&&) -> Address& = default;
  46. // Prints a human-readable representation of `a` to `out`.
  47. //
  48. // Currently, that representation consists of an AllocationId followed by an
  49. // optional ElementPath specifying a particular field within that allocation.
  50. void Print(llvm::raw_ostream& out) const {
  51. out << allocation_ << element_path_;
  52. }
  53. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  54. // If *this represents the address of an object with a field named
  55. // `field_name`, this method returns the address of that field.
  56. auto ElementAddress(Nonnull<const Element*> element) const -> Address {
  57. Address result = *this;
  58. result.element_path_.Append(element);
  59. return result;
  60. }
  61. // Drop all trailing BaseElements from the element path, returning the
  62. // downcasted address.
  63. auto DowncastedAddress() const -> Address {
  64. Address address = *this;
  65. address.element_path_.RemoveTrailingBaseElements();
  66. return address;
  67. }
  68. private:
  69. // The representation of Address describes how to locate an object within
  70. // the Heap, so its implementation details are tied to the implementation
  71. // details of the Heap.
  72. friend class Heap;
  73. AllocationId allocation_;
  74. ElementPath element_path_;
  75. };
  76. } // namespace Carbon
  77. #endif // CARBON_EXPLORER_AST_ADDRESS_H_