field_path.h 2.1 KB

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