address.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 Address represents a memory address in the Carbon virtual machine.
  14. // Addresses are used to access values stored in a Heap, and are obtained
  15. // from a Heap (or by deriving them from other Addresses).
  16. class Address {
  17. public:
  18. Address(const Address&) = default;
  19. Address(Address&&) = default;
  20. auto operator=(const Address&) -> Address& = default;
  21. auto operator=(Address&&) -> Address& = default;
  22. // Returns true if the two addresses refer to the same memory location.
  23. friend auto operator==(const Address& lhs, const Address& rhs) -> bool {
  24. return lhs.index == rhs.index;
  25. }
  26. friend auto operator!=(const Address& lhs, const Address& rhs) -> bool {
  27. return !(lhs == rhs);
  28. }
  29. // Prints a human-readable representation of `a` to `out`.
  30. //
  31. // Currently, that representation consists of an integer index identifying
  32. // the whole memory allocation, and an optional FieldPath specifying a
  33. // particular field within that allocation.
  34. void Print(llvm::raw_ostream& out) const {
  35. out << "Address(" << index << ")" << field_path;
  36. }
  37. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  38. // If *this represents the address of an object with a field named
  39. // `field_name`, this method returns the address of that field.
  40. auto SubobjectAddress(std::string field_name) const -> Address {
  41. Address result = *this;
  42. result.field_path.Append(std::move(field_name));
  43. return result;
  44. }
  45. private:
  46. // The representation of Address describes how to locate an object within
  47. // the Heap, so its implementation details are tied to the implementation
  48. // details of the Heap.
  49. friend class Heap;
  50. explicit Address(uint64_t index) : index(index) {}
  51. uint64_t index;
  52. FieldPath field_path;
  53. };
  54. } // namespace Carbon
  55. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ADDRESS_H_