field_path.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 EXECUTABLE_SEMANTICS_INTERPRETER_FIELD_PATH_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_FIELD_PATH_H_
  6. #include <string>
  7. #include <vector>
  8. #include "common/ostream.h"
  9. #include "llvm/Support/Compiler.h"
  10. namespace Carbon {
  11. // Given some initial Value, a FieldPath identifies a sub-Value within it,
  12. // in much the same way that a file path identifies a file within some
  13. // directory. FieldPaths are relative rather than absolute: the initial
  14. // Value is specified by the context in which the FieldPath is used, not
  15. // by the FieldPath itself.
  16. //
  17. // A FieldPath consists of a series of steps, which specify how to
  18. // incrementally navigate from a Value to one of its fields. Currently
  19. // there is only one kind of step, a string specifying a child field by name,
  20. // but that may change as Carbon develops. Note that an empty FieldPath
  21. // refers to the initial Value itself.
  22. class FieldPath {
  23. public:
  24. // Constructs an empty FieldPath.
  25. FieldPath() = default;
  26. // Constructs a FieldPath consisting of a single step.
  27. explicit FieldPath(std::string name) : components_({std::move(name)}) {}
  28. FieldPath(const FieldPath&) = default;
  29. FieldPath(FieldPath&&) = default;
  30. auto operator=(const FieldPath&) -> FieldPath& = default;
  31. auto operator=(FieldPath&&) -> FieldPath& = default;
  32. // Returns whether *this is empty.
  33. auto IsEmpty() const -> bool { return components_.empty(); }
  34. // Appends `name` to the end of *this.
  35. auto Append(std::string name) -> void {
  36. components_.push_back(std::move(name));
  37. }
  38. void Print(llvm::raw_ostream& out) const {
  39. for (const std::string& component : components_) {
  40. out << "." << component;
  41. }
  42. }
  43. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  44. private:
  45. // The representation of FieldPath describes how to locate a Value within
  46. // another Value, so its implementation details are tied to the implementation
  47. // details of Value.
  48. friend class Value;
  49. std::vector<std::string> components_;
  50. };
  51. } // namespace Carbon
  52. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_FIELD_PATH_H_