element_path.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 {
  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 {
  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. auto element() const -> Nonnull<const Element*> { return element_; }
  46. auto IsNamed(std::string_view name) const -> bool {
  47. return element_->IsNamed(name);
  48. }
  49. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  50. return interface_;
  51. }
  52. auto witness() const -> std::optional<Nonnull<const Witness*>> {
  53. return witness_;
  54. }
  55. void Print(llvm::raw_ostream& out) const { return element_->Print(out); }
  56. private:
  57. Nonnull<const Element*> element_;
  58. std::optional<Nonnull<const InterfaceType*>> interface_;
  59. std::optional<Nonnull<const Witness*>> witness_;
  60. };
  61. // Constructs a ElementPath consisting of a single step.
  62. explicit ElementPath(Nonnull<const Element*> element)
  63. : components_({Component(element)}) {}
  64. explicit ElementPath(const Component& f) : components_({f}) {}
  65. ElementPath(const ElementPath&) = default;
  66. ElementPath(ElementPath&&) = default;
  67. auto operator=(const ElementPath&) -> ElementPath& = default;
  68. auto operator=(ElementPath&&) -> ElementPath& = default;
  69. // Returns whether *this is empty.
  70. auto IsEmpty() const -> bool { return components_.empty(); }
  71. // Appends `element` to the end of *this.
  72. auto Append(Nonnull<const Element*> element) -> void {
  73. components_.push_back(Component(element));
  74. }
  75. // Removes all trailing `BaseElement`s, errors if there are no base elements.
  76. auto RemoveTrailingBaseElements() -> void {
  77. CARBON_CHECK(!components_.empty() && components_.back().element()->kind() ==
  78. ElementKind::BaseElement)
  79. << "No base elements to remove.";
  80. const auto r_it = std::find_if(
  81. components_.rbegin(), components_.rend(), [](const Component& c) {
  82. return c.element()->kind() != ElementKind::BaseElement;
  83. });
  84. components_.erase(r_it.base(), components_.end());
  85. }
  86. void Print(llvm::raw_ostream& out) const {
  87. for (const Component& component : components_) {
  88. out << "." << component;
  89. }
  90. }
  91. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  92. private:
  93. // The representation of ElementPath describes how to locate a Value within
  94. // another Value, so its implementation details are tied to the implementation
  95. // details of Value.
  96. friend class Value;
  97. std::vector<Component> components_;
  98. };
  99. } // namespace Carbon
  100. #endif // CARBON_EXPLORER_AST_ELEMENT_PATH_H_