impl_lookup.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 BuildCppUnsafeDerefWitness(
  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. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  113. if (!class_decl) {
  114. return SemIR::InstId::None;
  115. }
  116. auto candidates = class_decl->lookup(
  117. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  118. clang::OO_Star));
  119. if (candidates.empty()) {
  120. return SemIR::InstId::None;
  121. }
  122. if (!candidates.isSingleResult()) {
  123. context.TODO(loc_id, "operator* overload sets not implemented yet");
  124. return SemIR::ErrorInst::InstId;
  125. }
  126. auto decl_info =
  127. DeclInfo{.decl = *candidates.begin(), .signature = {.num_params = 0}};
  128. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  129. best_impl_type_structure, best_impl_loc_id);
  130. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  131. return fn_id;
  132. }
  133. auto result_type_id =
  134. context.functions()
  135. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  136. .return_type_inst_id;
  137. if (result_type_id == SemIR::ErrorInst::InstId) {
  138. return SemIR::ErrorInst::InstId;
  139. }
  140. return BuildCustomWitness(context, loc_id, query_self_const_id,
  141. query_specific_interface_id,
  142. {result_type_id, fn_id});
  143. }
  144. static auto BuildDefaultWitness(
  145. Context& context, SemIR::LocId loc_id,
  146. SemIR::ConstantId query_self_const_id,
  147. SemIR::SpecificInterfaceId query_specific_interface_id,
  148. const TypeStructure* best_impl_type_structure,
  149. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  150. auto& clang_sema = context.clang_sema();
  151. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  152. if (!class_decl) {
  153. return SemIR::InstId::None;
  154. }
  155. // Clang would produce a warning for classes with uninitialized
  156. // [[clang::requires_init]] fields for which default initialization is
  157. // performed, and we don't have a good place to produce that warning.
  158. // That happens if class_decl->hasUninitializedExplicitInitFields() is true.
  159. //
  160. // TODO: Consider treating such types as not implementing `Default`.
  161. auto decl_info =
  162. DeclInfo{.decl = clang_sema.LookupDefaultConstructor(class_decl),
  163. .signature = {.num_params = 0}};
  164. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  165. best_impl_type_structure, best_impl_loc_id);
  166. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  167. return fn_id;
  168. }
  169. return BuildCustomWitness(context, loc_id, query_self_const_id,
  170. query_specific_interface_id, {fn_id});
  171. }
  172. static auto BuildDestroyWitness(
  173. Context& context, SemIR::LocId loc_id,
  174. SemIR::ConstantId query_self_const_id,
  175. SemIR::SpecificInterfaceId query_specific_interface_id,
  176. const TypeStructure* best_impl_type_structure,
  177. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  178. auto& clang_sema = context.clang_sema();
  179. // TODO: This should provide `Destroy` for enums and other trivially
  180. // destructible types.
  181. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  182. if (!class_decl) {
  183. return SemIR::InstId::None;
  184. }
  185. auto decl_info = DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  186. .signature = {.num_params = 0}};
  187. auto fn_id = GetFunctionId(context, loc_id, decl_info,
  188. best_impl_type_structure, best_impl_loc_id);
  189. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  190. return fn_id;
  191. }
  192. return BuildCustomWitness(context, loc_id, query_self_const_id,
  193. query_specific_interface_id, {fn_id});
  194. }
  195. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  196. CoreInterface core_interface,
  197. SemIR::ConstantId query_self_const_id,
  198. SemIR::SpecificInterfaceId query_specific_interface_id,
  199. const TypeStructure* best_impl_type_structure,
  200. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  201. switch (core_interface) {
  202. case CoreInterface::Copy:
  203. return BuildCopyWitness(context, loc_id, query_self_const_id,
  204. query_specific_interface_id,
  205. best_impl_type_structure, best_impl_loc_id);
  206. case CoreInterface::CppUnsafeDeref:
  207. return BuildCppUnsafeDerefWitness(
  208. context, loc_id, query_self_const_id, query_specific_interface_id,
  209. best_impl_type_structure, best_impl_loc_id);
  210. case CoreInterface::Default:
  211. return BuildDefaultWitness(context, loc_id, query_self_const_id,
  212. query_specific_interface_id,
  213. best_impl_type_structure, best_impl_loc_id);
  214. case CoreInterface::Destroy:
  215. return BuildDestroyWitness(context, loc_id, query_self_const_id,
  216. query_specific_interface_id,
  217. best_impl_type_structure, best_impl_loc_id);
  218. // IntFitsIn is for Carbon integer types only.
  219. case CoreInterface::IntFitsIn:
  220. return SemIR::InstId::None;
  221. case CoreInterface::Unknown:
  222. CARBON_FATAL("unexpected CoreInterface `{0}`", core_interface);
  223. }
  224. }
  225. } // namespace Carbon::Check