mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:31:04 +01:00
Form the contents of the witness table. (#3756)
Check that functions in an `impl` are declared properly. For now, simply reuse the redeclaration checking logic. Longer-term, we need to check that the signatures match in a more approximate way, after substituting the `Self` type into the signature in the `interface`. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
551a6d385e
commit
a1317e61e0
+114
-6
@@ -5,11 +5,119 @@
|
||||
#include "toolchain/check/impl.h"
|
||||
|
||||
#include "toolchain/check/context.h"
|
||||
#include "toolchain/check/function.h"
|
||||
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
||||
#include "toolchain/sem_ir/ids.h"
|
||||
#include "toolchain/sem_ir/impl.h"
|
||||
#include "toolchain/sem_ir/typed_insts.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
// Adds the location of the associated function to a diagnostic.
|
||||
static auto NoteAssociatedFunction(Context& context,
|
||||
Context::DiagnosticBuilder& builder,
|
||||
SemIR::FunctionId function_id) -> void {
|
||||
CARBON_DIAGNOSTIC(ImplAssociatedFunctionHere, Note,
|
||||
"Associated function {0} declared here.", SemIR::NameId);
|
||||
const auto& function = context.functions().Get(function_id);
|
||||
builder.Note(function.decl_id, ImplAssociatedFunctionHere, function.name_id);
|
||||
}
|
||||
|
||||
// Checks that `impl_function_id` is a valid implementation of the function
|
||||
// described in the interface as `interface_function_id`. Returns the value to
|
||||
// put into the corresponding slot in the witness table, which can be
|
||||
// `BuiltinError` if the function is not usable.
|
||||
static auto CheckAssociatedFunctionImplementation(
|
||||
Context& context, SemIR::FunctionId interface_function_id,
|
||||
SemIR::InstId impl_decl_id) -> SemIR::InstId {
|
||||
auto impl_function_decl =
|
||||
context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
|
||||
if (!impl_function_decl) {
|
||||
CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
|
||||
"Associated function {0} implemented by non-function.",
|
||||
SemIR::NameId);
|
||||
auto builder = context.emitter().Build(
|
||||
impl_decl_id, ImplFunctionWithNonFunction,
|
||||
context.functions().Get(interface_function_id).name_id);
|
||||
NoteAssociatedFunction(context, builder, interface_function_id);
|
||||
builder.Emit();
|
||||
|
||||
return SemIR::InstId::BuiltinError;
|
||||
}
|
||||
|
||||
// TODO: Substitute the `Self` from the `impl` into the type in the interface
|
||||
// before checking. Also, this should be a semantic check rather than a
|
||||
// syntactic one. The functions should be allowed to have different signatures
|
||||
// as long as we can synthesize a suitable thunk.
|
||||
if (!CheckFunctionRedecl(context, impl_function_decl->function_id,
|
||||
interface_function_id)) {
|
||||
return SemIR::InstId::BuiltinError;
|
||||
}
|
||||
return impl_decl_id;
|
||||
}
|
||||
|
||||
// Builds a witness that the specified impl implements the given interface.
|
||||
static auto BuildInterfaceWitness(
|
||||
Context& context, const SemIR::Impl& impl, SemIR::InterfaceId interface_id,
|
||||
llvm::SmallVectorImpl<SemIR::InstId>& used_decl_ids) -> SemIR::InstId {
|
||||
const auto& interface = context.interfaces().Get(interface_id);
|
||||
if (!interface.is_defined()) {
|
||||
CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
|
||||
"Implementation of undefined interface {0}.",
|
||||
SemIR::NameId);
|
||||
auto builder = context.emitter().Build(
|
||||
impl.definition_id, ImplOfUndefinedInterface, interface.name_id);
|
||||
context.NoteUndefinedInterface(interface_id, builder);
|
||||
builder.Emit();
|
||||
return SemIR::InstId::BuiltinError;
|
||||
}
|
||||
|
||||
auto& impl_scope = context.name_scopes().Get(impl.scope_id);
|
||||
|
||||
llvm::SmallVector<SemIR::InstId> table;
|
||||
auto assoc_entities =
|
||||
context.inst_blocks().Get(interface.associated_entities_id);
|
||||
table.reserve(assoc_entities.size());
|
||||
|
||||
for (auto decl_id : assoc_entities) {
|
||||
auto decl = context.insts().Get(decl_id);
|
||||
if (auto fn_decl = decl.TryAs<SemIR::FunctionDecl>()) {
|
||||
auto& fn = context.functions().Get(fn_decl->function_id);
|
||||
auto impl_decl_id =
|
||||
context.LookupNameInExactScope(fn.name_id, impl_scope);
|
||||
if (impl_decl_id.is_valid()) {
|
||||
used_decl_ids.push_back(impl_decl_id);
|
||||
table.push_back(CheckAssociatedFunctionImplementation(
|
||||
context, fn_decl->function_id, impl_decl_id));
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(
|
||||
ImplMissingFunction, Error,
|
||||
"Missing implementation of {0} in impl of interface {1}.",
|
||||
SemIR::NameId, SemIR::NameId);
|
||||
auto builder =
|
||||
context.emitter().Build(impl.definition_id, ImplMissingFunction,
|
||||
fn.name_id, interface.name_id);
|
||||
NoteAssociatedFunction(context, builder, fn_decl->function_id);
|
||||
builder.Emit();
|
||||
|
||||
table.push_back(SemIR::InstId::BuiltinError);
|
||||
}
|
||||
} else if (auto const_decl = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
|
||||
// TODO: Check we have a value for this constant in the constraint.
|
||||
context.TODO(context.insts().GetParseNode(impl.definition_id),
|
||||
"impl of interface with associated constant");
|
||||
return SemIR::InstId::BuiltinError;
|
||||
} else {
|
||||
CARBON_FATAL() << "Unexpected kind of associated entity " << decl;
|
||||
}
|
||||
}
|
||||
|
||||
auto table_id = context.inst_blocks().Add(table);
|
||||
return context.AddInst(SemIR::InterfaceWitness{
|
||||
context.GetBuiltinType(SemIR::BuiltinKind::WitnessType), interface_id,
|
||||
table_id});
|
||||
}
|
||||
|
||||
auto BuildImplWitness(Context& context, SemIR::ImplId impl_id)
|
||||
-> SemIR::InstId {
|
||||
auto& impl = context.impls().Get(impl_id);
|
||||
@@ -24,14 +132,14 @@ auto BuildImplWitness(Context& context, SemIR::ImplId impl_id)
|
||||
return SemIR::InstId::BuiltinError;
|
||||
}
|
||||
|
||||
auto interface_id = interface_type->interface_id;
|
||||
llvm::SmallVector<SemIR::InstId> used_decl_ids;
|
||||
|
||||
// TODO: Form the witness table.
|
||||
auto witness_id = BuildInterfaceWitness(
|
||||
context, impl, interface_type->interface_id, used_decl_ids);
|
||||
|
||||
auto table_id = context.inst_blocks().Add({});
|
||||
return context.AddInst(SemIR::InterfaceWitness{
|
||||
context.GetBuiltinType(SemIR::BuiltinKind::WitnessType), interface_id,
|
||||
table_id});
|
||||
// TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
|
||||
|
||||
return witness_id;
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
|
||||
Reference in New Issue
Block a user