member.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. #include "explorer/ast/member.h"
  5. #include "explorer/ast/declaration.h"
  6. namespace Carbon {
  7. Member::Member(Nonnull<const Declaration*> declaration)
  8. : member_(declaration) {}
  9. Member::Member(Nonnull<const NamedValue*> struct_member)
  10. : member_(struct_member) {}
  11. auto Member::name() const -> std::string_view {
  12. if (const auto* decl = member_.dyn_cast<const Declaration*>()) {
  13. return GetName(*decl).value();
  14. } else {
  15. return member_.get<const NamedValue*>()->name;
  16. }
  17. }
  18. auto Member::type() const -> const Value& {
  19. if (const auto* decl = member_.dyn_cast<const Declaration*>()) {
  20. return decl->static_type();
  21. } else {
  22. return *member_.get<const NamedValue*>()->value;
  23. }
  24. }
  25. auto Member::declaration() const -> std::optional<Nonnull<const Declaration*>> {
  26. if (const auto* decl = member_.dyn_cast<const Declaration*>()) {
  27. return decl;
  28. }
  29. return std::nullopt;
  30. }
  31. } // namespace Carbon