impl_lookup.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "clang/Sema/Sema.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/cpp/import.h"
  8. #include "toolchain/check/cpp/location.h"
  9. #include "toolchain/check/cpp/overload_resolution.h"
  10. #include "toolchain/check/custom_witness.h"
  11. #include "toolchain/check/impl.h"
  12. #include "toolchain/check/impl_lookup.h"
  13. #include "toolchain/check/import_ref.h"
  14. #include "toolchain/check/inst.h"
  15. #include "toolchain/check/type.h"
  16. #include "toolchain/sem_ir/ids.h"
  17. #include "toolchain/sem_ir/typed_insts.h"
  18. namespace Carbon::Check {
  19. // If the given type is a C++ class type, returns the corresponding class
  20. // declaration. Otherwise returns nullptr.
  21. // TODO: Handle qualified types.
  22. static auto TypeAsClassDecl(Context& context,
  23. SemIR::ConstantId query_self_const_id)
  24. -> clang::CXXRecordDecl* {
  25. auto self_inst_id = context.constant_values().GetInstId(query_self_const_id);
  26. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(self_inst_id);
  27. if (!class_type) {
  28. // Not a class.
  29. return nullptr;
  30. }
  31. SemIR::NameScopeId class_scope_id =
  32. context.classes().Get(class_type->class_id).scope_id;
  33. if (!class_scope_id.has_value()) {
  34. return nullptr;
  35. }
  36. const auto& scope = context.name_scopes().Get(class_scope_id);
  37. auto decl_id = scope.clang_decl_context_id();
  38. if (!decl_id.has_value()) {
  39. return nullptr;
  40. }
  41. return dyn_cast<clang::CXXRecordDecl>(
  42. context.clang_decls().Get(decl_id).key.decl);
  43. }
  44. namespace {
  45. struct DeclInfo {
  46. // If null, no C++ decl was found and no witness can be created.
  47. clang::NamedDecl* decl = nullptr;
  48. SemIR::ClangDeclKey::Signature signature;
  49. };
  50. } // namespace
  51. // Finds the InstId for the C++ function that is called by a specific interface.
  52. // Returns SemIR::InstId::None if a C++ function is not found, and
  53. // SemIR::ErrorInst::InstId if an error occurs.
  54. static auto GetFunctionId(Context& context, SemIR::LocId loc_id,
  55. DeclInfo decl_info,
  56. const TypeStructure* best_impl_type_structure,
  57. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  58. if (!decl_info.decl) {
  59. // The C++ type is not able to implement the interface.
  60. return SemIR::InstId::None;
  61. }
  62. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  63. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  64. cpp_fn, GetCppLocation(context, loc_id))) {
  65. return SemIR::ErrorInst::InstId;
  66. }
  67. auto fn_id =
  68. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  69. if (fn_id == SemIR::ErrorInst::InstId) {
  70. return SemIR::ErrorInst::InstId;
  71. }
  72. CheckCppOverloadAccess(
  73. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  74. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  75. // TODO: Infer a C++ type structure and check whether it's less strict than
  76. // the best Carbon type structure.
  77. static_cast<void>(best_impl_type_structure);
  78. static_cast<void>(best_impl_loc_id);
  79. return fn_id;
  80. }
  81. static auto BuildCopyWitness(
  82. Context& context, SemIR::LocId loc_id,
  83. SemIR::ConstantId query_self_const_id,
  84. SemIR::SpecificInterfaceId query_specific_interface_id,
  85. const TypeStructure* best_impl_type_structure,
  86. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  87. auto& clang_sema = context.clang_sema();
  88. // TODO: This should provide `Copy` for enums and other trivially copyable
  89. // types.
  90. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  91. if (!class_decl) {
  92. return SemIR::InstId::None;
  93. }
  94. auto decl_info = DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  95. class_decl, clang::Qualifiers::Const),
  96. .signature = {.num_params = 1}};
  97. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  98. best_impl_type_structure, best_impl_loc_id);
  99. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  100. return fn_id;
  101. }
  102. return BuildCustomWitness(context, loc_id, query_self_const_id,
  103. query_specific_interface_id, {fn_id});
  104. }
  105. static auto BuildDestroyWitness(
  106. Context& context, SemIR::LocId loc_id,
  107. SemIR::ConstantId query_self_const_id,
  108. SemIR::SpecificInterfaceId query_specific_interface_id,
  109. const TypeStructure* best_impl_type_structure,
  110. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  111. auto& clang_sema = context.clang_sema();
  112. // TODO: This should provide `Destroy` for enums and other trivially
  113. // destructible types.
  114. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  115. if (!class_decl) {
  116. return SemIR::InstId::None;
  117. }
  118. auto decl_info = DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  119. .signature = {.num_params = 0}};
  120. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  121. best_impl_type_structure, best_impl_loc_id);
  122. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  123. return fn_id;
  124. }
  125. return BuildCustomWitness(context, loc_id, query_self_const_id,
  126. query_specific_interface_id, {fn_id});
  127. }
  128. static auto BuildCppUnsafeDerefWitness(
  129. Context& context, SemIR::LocId loc_id,
  130. SemIR::ConstantId query_self_const_id,
  131. SemIR::SpecificInterfaceId query_specific_interface_id,
  132. const TypeStructure* best_impl_type_structure,
  133. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  134. auto& clang_sema = context.clang_sema();
  135. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  136. if (!class_decl) {
  137. return SemIR::InstId::None;
  138. }
  139. auto candidates = class_decl->lookup(
  140. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  141. clang::OO_Star));
  142. if (candidates.empty()) {
  143. return SemIR::InstId::None;
  144. }
  145. if (!candidates.isSingleResult()) {
  146. context.TODO(loc_id, "operator* overload sets not implemented yet");
  147. return SemIR::ErrorInst::InstId;
  148. }
  149. auto decl_info =
  150. DeclInfo{.decl = *candidates.begin(), .signature = {.num_params = 0}};
  151. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  152. best_impl_type_structure, best_impl_loc_id);
  153. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  154. return fn_id;
  155. }
  156. auto result_type_id =
  157. context.functions()
  158. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  159. .return_type_inst_id;
  160. if (result_type_id == SemIR::ErrorInst::InstId) {
  161. return SemIR::ErrorInst::InstId;
  162. }
  163. return BuildCustomWitness(context, loc_id, query_self_const_id,
  164. query_specific_interface_id,
  165. {result_type_id, fn_id});
  166. }
  167. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  168. CoreInterface core_interface,
  169. SemIR::ConstantId query_self_const_id,
  170. SemIR::SpecificInterfaceId query_specific_interface_id,
  171. const TypeStructure* best_impl_type_structure,
  172. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  173. switch (core_interface) {
  174. case CoreInterface::Copy:
  175. return BuildCopyWitness(context, loc_id, query_self_const_id,
  176. query_specific_interface_id,
  177. best_impl_type_structure, best_impl_loc_id);
  178. case CoreInterface::Destroy:
  179. return BuildDestroyWitness(context, loc_id, query_self_const_id,
  180. query_specific_interface_id,
  181. best_impl_type_structure, best_impl_loc_id);
  182. case CoreInterface::CppUnsafeDeref:
  183. return BuildCppUnsafeDerefWitness(
  184. context, loc_id, query_self_const_id, query_specific_interface_id,
  185. best_impl_type_structure, best_impl_loc_id);
  186. // IntFitsIn is for Carbon integer types only.
  187. case CoreInterface::IntFitsIn:
  188. return SemIR::InstId::None;
  189. case CoreInterface::Unknown:
  190. CARBON_FATAL("unexpected CoreInterface `{0}`", core_interface);
  191. }
  192. }
  193. } // namespace Carbon::Check