-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathname_scope.cpp
More file actions
59 lines (53 loc) · 1.78 KB
/
name_scope.cpp
File metadata and controls
59 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/name_scope.h"
namespace Carbon::Check {
auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
-> std::optional<ClassScope> {
if (!scope_id.has_value()) {
return std::nullopt;
}
auto& scope = context.name_scopes().Get(scope_id);
if (!scope.inst_id().has_value()) {
return std::nullopt;
}
auto class_decl = context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id());
if (!class_decl) {
return std::nullopt;
}
return {{.class_decl = *class_decl, .name_scope = &scope}};
}
auto TryAsInterfaceScope(Context& context, SemIR::NameScopeId scope_id)
-> std::optional<InterfaceScope> {
if (!scope_id.has_value()) {
return std::nullopt;
}
auto& scope = context.name_scopes().Get(scope_id);
if (!scope.inst_id().has_value()) {
return std::nullopt;
}
auto interface_decl =
context.insts().TryGetAs<SemIR::InterfaceDecl>(scope.inst_id());
if (!interface_decl) {
return std::nullopt;
}
return {{.interface_decl = *interface_decl, .name_scope = &scope}};
}
auto TryAsNamedConstraintScope(Context& context, SemIR::NameScopeId scope_id)
-> std::optional<NamedConstraintScope> {
if (!scope_id.has_value()) {
return std::nullopt;
}
auto& scope = context.name_scopes().Get(scope_id);
if (!scope.inst_id().has_value()) {
return std::nullopt;
}
auto constraint_decl =
context.insts().TryGetAs<SemIR::NamedConstraintDecl>(scope.inst_id());
if (!constraint_decl) {
return std::nullopt;
}
return {{.constraint_decl = *constraint_decl, .name_scope = &scope}};
}
} // namespace Carbon::Check