Files
carbon-lang/explorer/interpreter/builtins.cpp
T
Richard Smith 8dd398807d Support user-defined implicit conversions via ImplicitAs (#1273)
Support is added for all of the non-type expression contexts where we currently accept built-in conversions, such as reordering the fields in a struct.
2022-05-20 17:10:30 -07:00

33 lines
997 B
C++

// 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)) {
if (interface->name() == GetName(Builtin::ImplicitAs)) {
builtins_[static_cast<int>(Builtin::ImplicitAs)] = 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