element_path.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_INTERPRETER_ELEMENT_PATH_H_
  5. #define CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_
  6. #include <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "common/ostream.h"
  11. #include "explorer/ast/element.h"
  12. #include "explorer/ast/value_node.h"
  13. #include "llvm/Support/Compiler.h"
  14. namespace Carbon {
  15. class InterfaceType;
  16. class Witness;
  17. // Given some initial Value, a ElementPath identifies a sub-Value within it,
  18. // in much the same way that a file path identifies a file within some
  19. // directory. FieldPaths are relative rather than absolute: the initial
  20. // Value is specified by the context in which the ElementPath is used, not
  21. // by the ElementPath itself.
  22. //
  23. // A ElementPath consists of a series of steps, which specify how to
  24. // incrementally navigate from a Value to one of its fields. Currently
  25. // there is only one kind of step, a string specifying a child field by name,
  26. // but that may change as Carbon develops. Note that an empty ElementPath
  27. // refers to the initial Value itself.
  28. class ElementPath {
  29. public:
  30. // Constructs an empty ElementPath.
  31. ElementPath() = default;
  32. // A single component of the ElementPath, which is typically the name
  33. // of a field. However, inside a generic, when there is a field
  34. // access on something of a generic type, e.g., `T`, then we also
  35. // need `witness`, a pointer to the witness table containing that field.
  36. class Component {
  37. public:
  38. explicit Component(Nonnull<const Element*> element) : element_(element) {}
  39. Component(Nonnull<const Element*> element,
  40. std::optional<Nonnull<const InterfaceType*>> interface,
  41. std::optional<Nonnull<const Witness*>> witness)
  42. : element_(element), interface_(interface), witness_(witness) {}
  43. auto element() const -> Nonnull<const Element*> { return element_; }
  44. auto IsNamed(std::string_view name) const -> bool {
  45. return element_->IsNamed(name);
  46. }
  47. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  48. return interface_;
  49. }
  50. auto witness() const -> std::optional<Nonnull<const Witness*>> {
  51. return witness_;
  52. }
  53. void Print(llvm::raw_ostream& out) const { return element_->Print(out); }
  54. private:
  55. Nonnull<const Element*> element_;
  56. std::optional<Nonnull<const InterfaceType*>> interface_;
  57. std::optional<Nonnull<const Witness*>> witness_;
  58. };
  59. // Constructs a ElementPath consisting of a single step.
  60. explicit ElementPath(Nonnull<const Element*> element)
  61. : components_({Component(element)}) {}
  62. explicit ElementPath(const Component& f) : components_({f}) {}
  63. ElementPath(const ElementPath&) = default;
  64. ElementPath(ElementPath&&) = default;
  65. auto operator=(const ElementPath&) -> ElementPath& = default;
  66. auto operator=(ElementPath&&) -> ElementPath& = default;
  67. // Returns whether *this is empty.
  68. auto IsEmpty() const -> bool { return components_.empty(); }
  69. // Appends `element` to the end of *this.
  70. auto Append(Nonnull<const Element*> element) -> void {
  71. components_.push_back(Component(element));
  72. }
  73. void Print(llvm::raw_ostream& out) const {
  74. for (const Component& component : components_) {
  75. out << "." << component;
  76. }
  77. }
  78. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  79. private:
  80. // The representation of ElementPath describes how to locate a Value within
  81. // another Value, so its implementation details are tied to the implementation
  82. // details of Value.
  83. friend class Value;
  84. std::vector<Component> components_;
  85. };
  86. } // namespace Carbon
  87. #endif // CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_