impl_lookup.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 class_type = context.constant_values().TryGetInstAs<SemIR::ClassType>(
  26. query_self_const_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) -> SemIR::InstId {
  56. if (!decl_info.decl) {
  57. // The C++ type is not able to implement the interface.
  58. return SemIR::InstId::None;
  59. }
  60. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  61. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  62. cpp_fn, GetCppLocation(context, loc_id))) {
  63. return SemIR::ErrorInst::InstId;
  64. }
  65. auto fn_id =
  66. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  67. if (fn_id == SemIR::ErrorInst::InstId) {
  68. return SemIR::ErrorInst::InstId;
  69. }
  70. CheckCppOverloadAccess(
  71. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  72. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  73. return fn_id;
  74. }
  75. static auto BuildCopyWitness(
  76. Context& context, SemIR::LocId loc_id,
  77. SemIR::ConstantId query_self_const_id,
  78. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  79. auto& clang_sema = context.clang_sema();
  80. // TODO: This should provide `Copy` for enums and other trivially copyable
  81. // types.
  82. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  83. if (!class_decl) {
  84. return SemIR::InstId::None;
  85. }
  86. auto decl_info = DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  87. class_decl, clang::Qualifiers::Const),
  88. .signature = {.num_params = 1}};
  89. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  90. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  91. return fn_id;
  92. }
  93. return BuildCustomWitness(context, loc_id, query_self_const_id,
  94. query_specific_interface_id, {fn_id});
  95. }
  96. static auto BuildCppUnsafeDerefWitness(
  97. Context& context, SemIR::LocId loc_id,
  98. SemIR::ConstantId query_self_const_id,
  99. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  100. auto& clang_sema = context.clang_sema();
  101. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  102. if (!class_decl) {
  103. return SemIR::InstId::None;
  104. }
  105. auto candidates = class_decl->lookup(
  106. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  107. clang::OO_Star));
  108. if (candidates.empty()) {
  109. return SemIR::InstId::None;
  110. }
  111. if (!candidates.isSingleResult()) {
  112. context.TODO(loc_id, "operator* overload sets not implemented yet");
  113. return SemIR::ErrorInst::InstId;
  114. }
  115. auto decl_info =
  116. DeclInfo{.decl = *candidates.begin(), .signature = {.num_params = 0}};
  117. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  118. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  119. return fn_id;
  120. }
  121. auto result_type_id =
  122. context.functions()
  123. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  124. .return_type_inst_id;
  125. if (result_type_id == SemIR::ErrorInst::InstId) {
  126. return SemIR::ErrorInst::InstId;
  127. }
  128. return BuildCustomWitness(context, loc_id, query_self_const_id,
  129. query_specific_interface_id,
  130. {result_type_id, fn_id});
  131. }
  132. static auto BuildDefaultWitness(
  133. Context& context, SemIR::LocId loc_id,
  134. SemIR::ConstantId query_self_const_id,
  135. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  136. auto& clang_sema = context.clang_sema();
  137. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  138. if (!class_decl) {
  139. return SemIR::InstId::None;
  140. }
  141. // Clang would produce a warning for classes with uninitialized
  142. // [[clang::requires_init]] fields for which default initialization is
  143. // performed, and we don't have a good place to produce that warning.
  144. // That happens if class_decl->hasUninitializedExplicitInitFields() is true.
  145. //
  146. // TODO: Consider treating such types as not implementing `Default`.
  147. auto decl_info =
  148. DeclInfo{.decl = clang_sema.LookupDefaultConstructor(class_decl),
  149. .signature = {.num_params = 0}};
  150. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  151. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  152. return fn_id;
  153. }
  154. return BuildCustomWitness(context, loc_id, query_self_const_id,
  155. query_specific_interface_id, {fn_id});
  156. }
  157. static auto BuildDestroyWitness(
  158. Context& context, SemIR::LocId loc_id,
  159. SemIR::ConstantId query_self_const_id,
  160. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  161. auto& clang_sema = context.clang_sema();
  162. // TODO: This should provide `Destroy` for enums and other trivially
  163. // destructible types.
  164. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  165. if (!class_decl) {
  166. return SemIR::InstId::None;
  167. }
  168. auto decl_info = DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  169. .signature = {.num_params = 0}};
  170. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  171. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  172. return fn_id;
  173. }
  174. return BuildCustomWitness(context, loc_id, query_self_const_id,
  175. query_specific_interface_id, {fn_id});
  176. }
  177. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  178. CoreInterface core_interface,
  179. SemIR::ConstantId query_self_const_id,
  180. SemIR::SpecificInterfaceId query_specific_interface_id,
  181. const TypeStructure* best_impl_type_structure,
  182. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  183. // TODO: Infer a C++ type structure and check whether it's less strict than
  184. // the best Carbon type structure.
  185. static_cast<void>(best_impl_type_structure);
  186. static_cast<void>(best_impl_loc_id);
  187. switch (core_interface) {
  188. case CoreInterface::Copy:
  189. return BuildCopyWitness(context, loc_id, query_self_const_id,
  190. query_specific_interface_id);
  191. case CoreInterface::CppUnsafeDeref:
  192. return BuildCppUnsafeDerefWitness(context, loc_id, query_self_const_id,
  193. query_specific_interface_id);
  194. case CoreInterface::Default:
  195. return BuildDefaultWitness(context, loc_id, query_self_const_id,
  196. query_specific_interface_id);
  197. case CoreInterface::Destroy:
  198. return BuildDestroyWitness(context, loc_id, query_self_const_id,
  199. query_specific_interface_id);
  200. // IntFitsIn is for Carbon integer types only.
  201. case CoreInterface::IntFitsIn:
  202. return SemIR::InstId::None;
  203. case CoreInterface::Unknown:
  204. CARBON_FATAL("unexpected CoreInterface `{0}`", core_interface);
  205. }
  206. }
  207. } // namespace Carbon::Check