impl_lookup.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp/impl_lookup.h"
  5. #include <type_traits>
  6. #include "clang/Sema/Sema.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/check/cpp/import.h"
  9. #include "toolchain/check/cpp/location.h"
  10. #include "toolchain/check/cpp/overload_resolution.h"
  11. #include "toolchain/check/custom_witness.h"
  12. #include "toolchain/check/impl.h"
  13. #include "toolchain/check/impl_lookup.h"
  14. #include "toolchain/check/import_ref.h"
  15. #include "toolchain/check/inst.h"
  16. #include "toolchain/check/type.h"
  17. #include "toolchain/sem_ir/ids.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::Check {
  20. // If the given type is a C++ class type, returns the corresponding class
  21. // declaration. Otherwise returns nullptr.
  22. // TODO: Handle qualified types.
  23. static auto TypeAsClassDecl(Context& context,
  24. SemIR::ConstantId query_self_const_id)
  25. -> clang::CXXRecordDecl* {
  26. auto self_inst_id = context.constant_values().GetInstId(query_self_const_id);
  27. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(self_inst_id);
  28. if (!class_type) {
  29. // Not a class.
  30. return nullptr;
  31. }
  32. SemIR::NameScopeId class_scope_id =
  33. context.classes().Get(class_type->class_id).scope_id;
  34. if (!class_scope_id.has_value()) {
  35. return nullptr;
  36. }
  37. const auto& scope = context.name_scopes().Get(class_scope_id);
  38. auto decl_id = scope.clang_decl_context_id();
  39. if (!decl_id.has_value()) {
  40. return nullptr;
  41. }
  42. return dyn_cast<clang::CXXRecordDecl>(
  43. context.clang_decls().Get(decl_id).key.decl);
  44. }
  45. namespace {
  46. // See `GetDeclForCoreInterface`.
  47. struct DeclInfo {
  48. clang::NamedDecl* decl;
  49. SemIR::ClangDeclKey::Signature signature;
  50. };
  51. } // namespace
  52. // Describes the function that needs to be looked up.
  53. enum class AssociatedFunction : std::underlying_type_t<CoreInterface> {
  54. // CoreInterface::Copy
  55. CopyConstructor = llvm::to_underlying(CoreInterface::Copy),
  56. // CoreInterface::Destroy
  57. Destructor = llvm::to_underlying(CoreInterface::Destroy),
  58. };
  59. // Maps a `CoreInterface` to its corresponding set of `CppCoreFunction`s.
  60. static auto GetCppAssociatedFunctions(const CoreInterface core_interface)
  61. -> std::bitset<8> {
  62. switch (core_interface) {
  63. case CoreInterface::Copy:
  64. return {llvm::to_underlying(AssociatedFunction::CopyConstructor)};
  65. case CoreInterface::Destroy:
  66. return {llvm::to_underlying(AssociatedFunction::Destructor)};
  67. case CoreInterface::Unknown:
  68. CARBON_FATAL(
  69. "`CoreInterface::Unknown` doesn't have a `CppCoreFunction` mapping");
  70. }
  71. }
  72. // Retrieves a `core_interface`'s corresponding `NamedDecl`, also with the
  73. // expected number of parameters. May return a null decl.
  74. auto GetDeclForCoreInterface(clang::Sema& clang_sema,
  75. AssociatedFunction associated_function,
  76. clang::CXXRecordDecl* class_decl) -> DeclInfo {
  77. // TODO: Handle other interfaces.
  78. switch (associated_function) {
  79. case AssociatedFunction::CopyConstructor:
  80. return {.decl = clang_sema.LookupCopyingConstructor(
  81. class_decl, clang::Qualifiers::Const),
  82. .signature = {.num_params = 1}};
  83. case AssociatedFunction::Destructor:
  84. return {.decl = clang_sema.LookupDestructor(class_decl),
  85. .signature = {.num_params = 0}};
  86. }
  87. }
  88. static auto FindCppAssociatedFunction(Context& context, SemIR::LocId loc_id,
  89. AssociatedFunction associated_function,
  90. clang::CXXRecordDecl* class_decl)
  91. -> SemIR::InstId {
  92. // TODO: This should provide `Destroy` for enums and other trivially
  93. // destructible types.
  94. auto decl_info = GetDeclForCoreInterface(context.clang_sema(),
  95. associated_function, class_decl);
  96. if (!decl_info.decl) {
  97. // TODO: If the impl lookup failure is an error, we should produce a
  98. // diagnostic explaining why the class is not copyable/destructible.
  99. return SemIR::InstId::None;
  100. }
  101. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  102. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  103. cpp_fn, GetCppLocation(context, loc_id))) {
  104. return SemIR::ErrorInst::InstId;
  105. }
  106. auto fn_id =
  107. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  108. if (fn_id == SemIR::ErrorInst::InstId) {
  109. return SemIR::ErrorInst::InstId;
  110. }
  111. CheckCppOverloadAccess(
  112. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  113. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  114. return fn_id;
  115. }
  116. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  117. CoreInterface core_interface,
  118. SemIR::ConstantId query_self_const_id,
  119. SemIR::SpecificInterfaceId query_specific_interface_id,
  120. const TypeStructure* best_impl_type_structure,
  121. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  122. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  123. if (!class_decl) {
  124. return SemIR::InstId::None;
  125. }
  126. auto associated_functions = GetCppAssociatedFunctions(core_interface);
  127. auto witness_id = SemIR::ErrorInst::InstId;
  128. switch (core_interface) {
  129. case CoreInterface::Copy:
  130. case CoreInterface::Destroy: {
  131. CARBON_CHECK(associated_functions.count() == 1);
  132. witness_id = FindCppAssociatedFunction(
  133. context, loc_id,
  134. static_cast<AssociatedFunction>(associated_functions.to_ullong()),
  135. class_decl);
  136. } break;
  137. case CoreInterface::Unknown:
  138. CARBON_FATAL("shouldn't be called with `Unknown`");
  139. }
  140. if (witness_id == SemIR::InstId::None ||
  141. witness_id == SemIR::ErrorInst::InstId) {
  142. return witness_id;
  143. }
  144. // TODO: Infer a C++ type structure and check whether it's less strict than
  145. // the best Carbon type structure.
  146. static_cast<void>(best_impl_type_structure);
  147. static_cast<void>(best_impl_loc_id);
  148. return BuildCustomWitness(context, loc_id, query_self_const_id,
  149. query_specific_interface_id, {witness_id});
  150. }
  151. } // namespace Carbon::Check