element_path.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_ELEMENT_PATH_H_
  5. #define CARBON_EXPLORER_AST_ELEMENT_PATH_H_
  6. #include <algorithm>
  7. #include <optional>
  8. #include <string>
  9. #include <string_view>
  10. #include <vector>
  11. #include "common/check.h"
  12. #include "common/ostream.h"
  13. #include "explorer/ast/element.h"
  14. #include "explorer/ast/value_node.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. class InterfaceType;
  18. class Witness;
  19. // Given some initial Value, a ElementPath identifies a sub-Value within it,
  20. // in much the same way that a file path identifies a file within some
  21. // directory. FieldPaths are relative rather than absolute: the initial
  22. // Value is specified by the context in which the ElementPath is used, not
  23. // by the ElementPath itself.
  24. //
  25. // A ElementPath consists of a series of steps, which specify how to
  26. // incrementally navigate from a Value to one of its fields. Currently
  27. // there is only one kind of step, a string specifying a child field by name,
  28. // but that may change as Carbon develops. Note that an empty ElementPath
  29. // refers to the initial Value itself.
  30. class ElementPath : public Printable<ElementPath> {
  31. public:
  32. // Constructs an empty ElementPath.
  33. ElementPath() = default;
  34. // A single component of the ElementPath, which is typically the name
  35. // of a field. However, inside a generic, when there is a field
  36. // access on something of a generic type, e.g., `T`, then we also
  37. // need `witness`, a pointer to the witness table containing that field.
  38. class Component : public Printable<Component> {
  39. public:
  40. explicit Component(Nonnull<const Element*> element) : element_(element) {}
  41. Component(Nonnull<const Element*> element,
  42. std::optional<Nonnull<const InterfaceType*>> interface,
  43. std::optional<Nonnull<const Witness*>> witness)
  44. : element_(element), interface_(interface), witness_(witness) {}
  45. inline friend auto operator==(const Component& lhs, const Component& rhs)
  46. -> bool {
  47. return lhs.element_ == rhs.element_ && lhs.interface_ == rhs.interface_ &&
  48. lhs.witness_ == rhs.witness_;
  49. }
  50. inline friend auto hash_value(const Component& component)
  51. -> llvm::hash_code {
  52. return llvm::hash_combine(component.element_, component.interface_,
  53. component.witness_);
  54. }
  55. auto element() const -> Nonnull<const Element*> { return element_; }
  56. auto IsNamed(std::string_view name) const -> bool {
  57. return element_->IsNamed(name);
  58. }
  59. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  60. return interface_;
  61. }
  62. auto witness() const -> std::optional<Nonnull<const Witness*>> {
  63. return witness_;
  64. }
  65. void Print(llvm::raw_ostream& out) const { return element_->Print(out); }
  66. private:
  67. Nonnull<const Element*> element_;
  68. std::optional<Nonnull<const InterfaceType*>> interface_;
  69. std::optional<Nonnull<const Witness*>> witness_;
  70. };
  71. // Constructs a ElementPath consisting of a single step.
  72. explicit ElementPath(Nonnull<const Element*> element)
  73. : components_({Component(element)}) {}
  74. explicit ElementPath(const Component& f) : components_({f}) {}
  75. ElementPath(const ElementPath&) = default;
  76. ElementPath(ElementPath&&) = default;
  77. auto operator=(const ElementPath&) -> ElementPath& = default;
  78. auto operator=(ElementPath&&) -> ElementPath& = default;
  79. inline friend auto operator==(const ElementPath& lhs, const ElementPath& rhs)
  80. -> bool {
  81. return lhs.components_ == rhs.components_;
  82. }
  83. inline friend auto hash_value(const ElementPath& path) -> llvm::hash_code {
  84. return llvm::hash_combine_range(path.components_.begin(),
  85. path.components_.end());
  86. }
  87. // Returns whether *this is empty.
  88. auto IsEmpty() const -> bool { return components_.empty(); }
  89. // Appends `element` to the end of *this.
  90. auto Append(Nonnull<const Element*> element) -> void {
  91. components_.push_back(Component(element));
  92. }
  93. // Removes all trailing `BaseElement`s, errors if there are no base elements.
  94. auto RemoveTrailingBaseElements() -> void {
  95. CARBON_CHECK(!components_.empty() && components_.back().element()->kind() ==
  96. ElementKind::BaseElement)
  97. << "No base elements to remove.";
  98. const auto r_it = std::find_if(
  99. components_.rbegin(), components_.rend(), [](const Component& c) {
  100. return c.element()->kind() != ElementKind::BaseElement;
  101. });
  102. components_.erase(r_it.base(), components_.end());
  103. }
  104. void Print(llvm::raw_ostream& out) const {
  105. for (const Component& component : components_) {
  106. out << "." << component;
  107. }
  108. }
  109. private:
  110. // The representation of ElementPath describes how to locate a Value within
  111. // another Value, so its implementation details are tied to the implementation
  112. // details of Value.
  113. friend class Value;
  114. friend class Heap;
  115. std::vector<Component> components_;
  116. };
  117. } // namespace Carbon
  118. #endif // CARBON_EXPLORER_AST_ELEMENT_PATH_H_