-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move impl lookup out into its own file. (#4435)
In preparation for adding more logic here. This code doesn't belong in member access.
- Loading branch information
Showing
4 changed files
with
86 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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/impl_lookup.h" | ||
|
||
#include "toolchain/check/deduce.h" | ||
#include "toolchain/check/generic.h" | ||
#include "toolchain/check/import_ref.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
auto LookupInterfaceWitness(Context& context, SemIR::LocId loc_id, | ||
SemIR::ConstantId type_const_id, | ||
SemIR::ConstantId interface_const_id) | ||
-> SemIR::InstId { | ||
// TODO: Add a better impl lookup system. At the very least, we should only be | ||
// considering impls that are for the same interface we're querying. We can | ||
// also skip impls that mention any types that aren't part of our impl query. | ||
for (const auto& impl : context.impls().array_ref()) { | ||
auto specific_id = SemIR::SpecificId::Invalid; | ||
if (impl.generic_id.is_valid()) { | ||
specific_id = DeduceImplArguments(context, loc_id, impl, type_const_id, | ||
interface_const_id); | ||
if (!specific_id.is_valid()) { | ||
continue; | ||
} | ||
} | ||
if (!context.constant_values().AreEqualAcrossDeclarations( | ||
SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id, | ||
impl.self_id), | ||
type_const_id)) { | ||
continue; | ||
} | ||
if (!context.constant_values().AreEqualAcrossDeclarations( | ||
SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id, | ||
impl.constraint_id), | ||
interface_const_id)) { | ||
// TODO: An impl of a constraint type should be treated as implementing | ||
// the constraint's interfaces. | ||
continue; | ||
} | ||
if (!impl.witness_id.is_valid()) { | ||
// TODO: Diagnose if the impl isn't defined yet? | ||
return SemIR::InstId::Invalid; | ||
} | ||
LoadImportRef(context, impl.witness_id); | ||
if (specific_id.is_valid()) { | ||
// We need a definition of the specific `impl` so we can access its | ||
// witness. | ||
ResolveSpecificDefinition(context, specific_id); | ||
} | ||
return context.constant_values().GetInstId( | ||
SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id, | ||
impl.witness_id)); | ||
} | ||
return SemIR::InstId::Invalid; | ||
} | ||
|
||
} // namespace Carbon::Check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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 | ||
|
||
#ifndef CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_ | ||
#define CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_ | ||
|
||
#include "toolchain/check/context.h" | ||
#include "toolchain/sem_ir/ids.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
// Looks up the witness to use for a particular type and interface. Returns the | ||
// witness, or `InstId::Invalid` if the type is not known to implement the | ||
// interface. | ||
auto LookupInterfaceWitness(Context& context, SemIR::LocId loc_id, | ||
SemIR::ConstantId type_const_id, | ||
SemIR::ConstantId interface_const_id) | ||
-> SemIR::InstId; | ||
|
||
} // namespace Carbon::Check | ||
|
||
#endif // CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters