Files
carbon-lang/toolchain/check/cpp/impl_lookup.h
T
372f632d9d Implement support for copying C++ classes. (#6434)
When performing impl lookup for `Core.Copy` for a C++ class type, look
for a copy constructor. If we find one, synthesize an impl witness that
calls the constructor.

This adds initial support for impl lookup to delegate to the C++ interop
logic for queries involving C++ types. For now, we don't implement the
rules from #6166 that compare a synthesized type structure for the C++
impl against the best Carbon type structure, but the framework for
building that support is established here.

Currently there is no caching of the lookup here, and we build unique
`ImplWitnessTable`s for each lookup, which leads to each impl lookup
producing a distinct facet value. This results in some errors in generic
contexts; this will be addressed in follow-up changes. This PR aims only
to support the non-generic case.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
2025-12-02 02:52:23 +00:00

45 lines
2.2 KiB
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
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_IMPL_LOOKUP_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_IMPL_LOOKUP_H_
#include "toolchain/check/context.h"
#include "toolchain/check/impl_lookup.h"
#include "toolchain/check/type_structure.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/specific_interface.h"
namespace Carbon::Check {
// Performs lookup for an impl witness for a query involving C++ types. Returns
// a witness value, or `None` if a synthesized C++ witness should not be used.
//
// If `interface` is an interface for which we can synthesize a witness based on
// C++ operator overloads or special member functions, performs the suitable C++
// lookup to determine if this interface should be considered implemented for
// the specified type, and if so, synthesizes and returns a suitable witness.
//
// `best_impl_type_structure` provides the type structure of the best-matching
// impl declaration. If this is better than every viable C++ candidate, a "none"
// result will be returned. If this is worse than the best viable C++ candidate
// according to C++ rules, a witness for the C++ candidate will be returned.
// Otherwise, it is at least as good as the best viable C++ candidate, but there
// is some C++ candidate that has a better type structure, in which case the
// result is ambiguous and we diagnose an error. This parameter can be null if
// there is no usable impl for this query.
//
// `best_impl_loc_id` gives the location of the impl corresponding to the best
// type structure, and can be `None` if `best_impl_type_structure` is null. This
// parameter is used only for ambiguity diagnostics.
auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
SemIR::TypeId self_type_id,
SemIR::SpecificInterface specific_interface,
const TypeStructure* best_impl_type_structure,
SemIR::LocId best_impl_loc_id) -> SemIR::InstId;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_IMPL_LOOKUP_H_