field_path.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "common/ostream.h"
  11. #include "explorer/ast/member.h"
  12. #include "explorer/ast/static_scope.h"
  13. #include "llvm/Support/Compiler.h"
  14. namespace Carbon {
  15. class InterfaceType;
  16. class Witness;
  17. // Given some initial Value, a FieldPath 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 FieldPath is used, not
  21. // by the FieldPath itself.
  22. //
  23. // A FieldPath 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 FieldPath
  27. // refers to the initial Value itself.
  28. class FieldPath {
  29. public:
  30. // Constructs an empty FieldPath.
  31. FieldPath() = default;
  32. // A single component of the FieldPath, 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(Member member) : member_(member) {}
  39. Component(Member member,
  40. std::optional<Nonnull<const InterfaceType*>> interface,
  41. std::optional<Nonnull<const Witness*>> witness)
  42. : member_(member), interface_(interface), witness_(witness) {}
  43. auto member() const -> Member { return member_; }
  44. auto IsNamed(std::string_view name) const -> bool {
  45. return member_.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 member_.Print(out); }
  54. private:
  55. Member member_;
  56. std::optional<Nonnull<const InterfaceType*>> interface_;
  57. std::optional<Nonnull<const Witness*>> witness_;
  58. };
  59. // Constructs a FieldPath consisting of a single step.
  60. explicit FieldPath(Member member) : components_({Component(member)}) {}
  61. explicit FieldPath(const Component& f) : components_({f}) {}
  62. FieldPath(const FieldPath&) = default;
  63. FieldPath(FieldPath&&) = default;
  64. auto operator=(const FieldPath&) -> FieldPath& = default;
  65. auto operator=(FieldPath&&) -> FieldPath& = default;
  66. // Returns whether *this is empty.
  67. auto IsEmpty() const -> bool { return components_.empty(); }
  68. // Appends `member` to the end of *this.
  69. auto Append(Member member) -> void {
  70. components_.push_back(Component(member));
  71. }
  72. void Print(llvm::raw_ostream& out) const {
  73. for (const Component& component : components_) {
  74. out << "." << component;
  75. }
  76. }
  77. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  78. private:
  79. // The representation of FieldPath describes how to locate a Value within
  80. // another Value, so its implementation details are tied to the implementation
  81. // details of Value.
  82. friend class Value;
  83. std::vector<Component> components_;
  84. };
  85. } // namespace Carbon
  86. #endif // CARBON_EXPLORER_INTERPRETER_FIELD_PATH_H_