field_path.h 3.2 KB

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