forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.cpp
More file actions
41 lines (33 loc) · 1.29 KB
/
Copy pathbuiltins.cpp
File metadata and controls
41 lines (33 loc) · 1.29 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
// 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 "explorer/interpreter/builtins.h"
#include "explorer/common/error_builders.h"
using llvm::dyn_cast;
namespace Carbon {
void Builtins::Register(Nonnull<const Declaration*> decl) {
if (auto* interface = dyn_cast<InterfaceDeclaration>(decl)) {
static std::map<std::string, int>* builtin_indexes = [] {
std::map<std::string, int> builtin_indexes;
for (int index = 0; index <= static_cast<int>(Builtin::Last); ++index) {
builtin_indexes.emplace(BuiltinNames[index], index);
}
return new auto(std::move(builtin_indexes));
}();
auto it = builtin_indexes->find(interface->name());
if (it != builtin_indexes->end()) {
builtins_[it->second] = interface;
}
}
}
auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const
-> ErrorOr<Nonnull<const Declaration*>> {
std::optional<const Declaration*> result =
builtins_[static_cast<int>(builtin)];
if (!result.has_value()) {
return CompilationError(source_loc)
<< "missing declaration for builtin `" << GetName(builtin) << "`";
}
return result.value();
}
} // namespace Carbon