member.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_AST_MEMBER_H_
  5. #define CARBON_EXPLORER_AST_MEMBER_H_
  6. #include <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include "explorer/common/nonnull.h"
  10. #include "llvm/ADT/PointerUnion.h"
  11. namespace Carbon {
  12. class Declaration;
  13. class Value;
  14. // A NamedValue represents a value with a name, such as a single struct field.
  15. struct NamedValue {
  16. // The field name.
  17. std::string name;
  18. // The field's value.
  19. Nonnull<const Value*> value;
  20. };
  21. // A IndexedValue represents a value identified by an index, such as a tuple
  22. // field.
  23. struct IndexedValue {
  24. // The field index.
  25. int index;
  26. // The field's value.
  27. Nonnull<const Value*> value;
  28. };
  29. // A member of a type.
  30. //
  31. // This is either a declared member of a class, interface, or similar, or a
  32. // member of a struct with no declaration.
  33. class Member {
  34. public:
  35. explicit Member(Nonnull<const Declaration*> declaration);
  36. explicit Member(Nonnull<const NamedValue*> struct_member);
  37. explicit Member(Nonnull<const IndexedValue*> tuple_member);
  38. // Return whether the member's name matches `name`.
  39. auto IsNamed(std::string_view name) const -> bool;
  40. // Prints the Member
  41. void Print(llvm::raw_ostream& out) const;
  42. // Return whether the member is positional, i.e. has an index.
  43. auto HasPosition() const -> bool;
  44. // Return whether the member is named, i.e. has a name.
  45. auto HasName() const -> bool;
  46. // The name of the member. Requires *this to represent a named member.
  47. auto name() const -> std::string_view;
  48. // The index of the member. Requires *this to represent a positional member.
  49. auto index() const -> int;
  50. // The declared type of the member, which might include type variables.
  51. auto type() const -> const Value&;
  52. // A declaration of the member, if any exists.
  53. auto declaration() const -> std::optional<Nonnull<const Declaration*>>;
  54. private:
  55. llvm::PointerUnion<Nonnull<const Declaration*>, Nonnull<const NamedValue*>,
  56. Nonnull<const IndexedValue*>>
  57. member_;
  58. };
  59. } // namespace Carbon
  60. #endif // CARBON_EXPLORER_AST_MEMBER_H_