impl_lookup.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // CoreInterface::CppUnsafeDeref
  59. CppUnsafeDeref = llvm::to_underlying(CoreInterface::CppUnsafeDeref),
  60. };
  61. // Maps a `CoreInterface` to its corresponding set of `CppCoreFunction`s.
  62. static auto GetCppAssociatedFunctions(const CoreInterface core_interface)
  63. -> std::bitset<8> {
  64. switch (core_interface) {
  65. case CoreInterface::Copy:
  66. return {llvm::to_underlying(AssociatedFunction::CopyConstructor)};
  67. case CoreInterface::Destroy:
  68. return {llvm::to_underlying(AssociatedFunction::Destructor)};
  69. case CoreInterface::CppUnsafeDeref:
  70. return {llvm::to_underlying(AssociatedFunction::CppUnsafeDeref)};
  71. case CoreInterface::Unknown:
  72. case CoreInterface::IntFitsIn:
  73. CARBON_FATAL("No AssociatedFunction mapping for this interface");
  74. }
  75. }
  76. // Retrieves a `core_interface`'s corresponding `NamedDecl`, also with the
  77. // expected number of parameters. May return a null decl to indicate nothing was
  78. // found, or nullopt to indicate `SemIR::ErrInst::InstId` should be propagated.
  79. auto GetDeclForCoreInterface(Context& context, SemIR::LocId loc_id,
  80. AssociatedFunction associated_function,
  81. clang::CXXRecordDecl* class_decl)
  82. -> std::optional<DeclInfo> {
  83. auto& clang_sema = context.clang_sema();
  84. CARBON_CHECK(class_decl != nullptr);
  85. // TODO: Handle other interfaces.
  86. switch (associated_function) {
  87. case AssociatedFunction::CopyConstructor:
  88. return DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  89. class_decl, clang::Qualifiers::Const),
  90. .signature = {.num_params = 1}};
  91. case AssociatedFunction::Destructor:
  92. return DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  93. .signature = {.num_params = 0}};
  94. case AssociatedFunction::CppUnsafeDeref: {
  95. auto candidates = class_decl->lookup(
  96. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  97. clang::OO_Star));
  98. if (candidates.empty()) {
  99. return DeclInfo{};
  100. }
  101. if (!candidates.isSingleResult()) {
  102. context.TODO(loc_id, "operator* overload sets not implemented yet");
  103. return std::nullopt;
  104. }
  105. return DeclInfo{.decl = *candidates.begin(),
  106. .signature = {.num_params = 0}};
  107. }
  108. }
  109. }
  110. static auto FindCppAssociatedFunction(Context& context, SemIR::LocId loc_id,
  111. AssociatedFunction associated_function,
  112. clang::CXXRecordDecl* class_decl)
  113. -> SemIR::InstId {
  114. // TODO: This should provide `Destroy` for enums and other trivially
  115. // destructible types.
  116. auto decl_info =
  117. GetDeclForCoreInterface(context, loc_id, associated_function, class_decl);
  118. if (!decl_info.has_value()) {
  119. return SemIR::ErrorInst::InstId;
  120. }
  121. if (!decl_info->decl) {
  122. // TODO: If the impl lookup failure is an error, we should produce a
  123. // diagnostic explaining why the class does not satisfy the core interface.
  124. return SemIR::InstId::None;
  125. }
  126. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info->decl);
  127. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  128. cpp_fn, GetCppLocation(context, loc_id))) {
  129. return SemIR::ErrorInst::InstId;
  130. }
  131. auto fn_id =
  132. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info->signature);
  133. if (fn_id == SemIR::ErrorInst::InstId) {
  134. return SemIR::ErrorInst::InstId;
  135. }
  136. CheckCppOverloadAccess(
  137. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  138. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  139. return fn_id;
  140. }
  141. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  142. CoreInterface core_interface,
  143. SemIR::ConstantId query_self_const_id,
  144. SemIR::SpecificInterfaceId query_specific_interface_id,
  145. const TypeStructure* best_impl_type_structure,
  146. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  147. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  148. if (!class_decl) {
  149. return SemIR::InstId::None;
  150. }
  151. auto witness_id = SemIR::ErrorInst::InstId;
  152. switch (core_interface) {
  153. case CoreInterface::Copy:
  154. case CoreInterface::Destroy:
  155. case CoreInterface::CppUnsafeDeref: {
  156. auto associated_functions = GetCppAssociatedFunctions(core_interface);
  157. CARBON_CHECK(associated_functions.count() == 1);
  158. witness_id = FindCppAssociatedFunction(
  159. context, loc_id,
  160. static_cast<AssociatedFunction>(associated_functions.to_ullong()),
  161. class_decl);
  162. } break;
  163. case CoreInterface::IntFitsIn:
  164. return SemIR::InstId::None;
  165. case CoreInterface::Unknown:
  166. CARBON_FATAL("shouldn't be called with `Unknown`");
  167. }
  168. if (witness_id == SemIR::InstId::None ||
  169. witness_id == SemIR::ErrorInst::InstId) {
  170. return witness_id;
  171. }
  172. // TODO: Infer a C++ type structure and check whether it's less strict than
  173. // the best Carbon type structure.
  174. static_cast<void>(best_impl_type_structure);
  175. static_cast<void>(best_impl_loc_id);
  176. return BuildCustomWitness(context, loc_id, query_self_const_id,
  177. query_specific_interface_id, {witness_id});
  178. }
  179. } // namespace Carbon::Check