field_path.h 3.5 KB

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