name_scope.cpp 770 B

12345678910111213141516171819202122232425
  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 "toolchain/check/name_scope.h"
  5. namespace Carbon::Check {
  6. auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
  7. -> std::optional<ClassScope> {
  8. if (!scope_id.has_value()) {
  9. return std::nullopt;
  10. }
  11. auto& scope = context.name_scopes().Get(scope_id);
  12. if (!scope.inst_id().has_value()) {
  13. return std::nullopt;
  14. }
  15. auto class_decl = context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id());
  16. if (!class_decl) {
  17. return std::nullopt;
  18. }
  19. return {{.class_decl = *class_decl, .name_scope = &scope}};
  20. }
  21. } // namespace Carbon::Check