member.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "explorer/common/nonnull.h"
  9. #include "llvm/ADT/PointerUnion.h"
  10. namespace Carbon {
  11. class Declaration;
  12. class Value;
  13. // A NamedValue represents a value with a name, such as a single struct field.
  14. struct NamedValue {
  15. // The field name.
  16. std::string name;
  17. // The field's value.
  18. Nonnull<const Value*> value;
  19. };
  20. // A member of a type.
  21. //
  22. // This is either a declared member of a class, interface, or similar, or a
  23. // member of a struct with no declaration.
  24. class Member {
  25. public:
  26. explicit Member(Nonnull<const Declaration*> declaration);
  27. explicit Member(Nonnull<const NamedValue*> struct_member);
  28. // The name of the member.
  29. auto name() const -> std::string_view;
  30. // The declared type of the member, which might include type variables.
  31. auto type() const -> const Value&;
  32. // A declaration of the member, if any exists.
  33. auto declaration() const -> std::optional<Nonnull<const Declaration*>>;
  34. private:
  35. llvm::PointerUnion<Nonnull<const Declaration*>, Nonnull<const NamedValue*>>
  36. member_;
  37. };
  38. } // namespace Carbon
  39. #endif // CARBON_EXPLORER_AST_MEMBER_H_