impl_lookup.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/builtin_function_kind.h"
  17. #include "toolchain/sem_ir/ids.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::Check {
  20. // Given a type constant, return the corresponding class scope if there is one.
  21. static auto GetClassScope(Context& context,
  22. SemIR::ConstantId query_self_const_id)
  23. -> SemIR::NameScopeId {
  24. auto class_type = context.constant_values().TryGetInstAs<SemIR::ClassType>(
  25. query_self_const_id);
  26. if (!class_type) {
  27. // Not a class.
  28. return SemIR::NameScopeId::None;
  29. }
  30. return context.classes().Get(class_type->class_id).scope_id;
  31. }
  32. // If the given type is a C++ tag (class or enumeration) type, returns the
  33. // corresponding tag declaration. Otherwise returns nullptr.
  34. // TODO: Handle qualified types.
  35. static auto TypeAsTagDecl(Context& context,
  36. SemIR::ConstantId query_self_const_id)
  37. -> clang::TagDecl* {
  38. SemIR::NameScopeId class_scope_id =
  39. GetClassScope(context, query_self_const_id);
  40. if (!class_scope_id.has_value()) {
  41. return nullptr;
  42. }
  43. const auto& scope = context.name_scopes().Get(class_scope_id);
  44. auto decl_id = scope.clang_decl_context_id();
  45. if (!decl_id.has_value()) {
  46. return nullptr;
  47. }
  48. return dyn_cast<clang::TagDecl>(context.clang_decls().Get(decl_id).key.decl);
  49. }
  50. // If the given type is a C++ class type, returns the corresponding class
  51. // declaration. Otherwise returns nullptr.
  52. static auto TypeAsClassDecl(Context& context,
  53. SemIR::ConstantId query_self_const_id)
  54. -> clang::CXXRecordDecl* {
  55. return dyn_cast_or_null<clang::CXXRecordDecl>(
  56. TypeAsTagDecl(context, query_self_const_id));
  57. }
  58. namespace {
  59. struct DeclInfo {
  60. // If null, no C++ decl was found and no witness can be created.
  61. clang::NamedDecl* decl = nullptr;
  62. SemIR::ClangDeclKey::Signature signature;
  63. };
  64. } // namespace
  65. // Finds the InstId for the C++ function that is called by a specific interface.
  66. // Returns SemIR::InstId::None if a C++ function is not found, and
  67. // SemIR::ErrorInst::InstId if an error occurs.
  68. static auto GetFunctionId(Context& context, SemIR::LocId loc_id,
  69. DeclInfo decl_info) -> SemIR::InstId {
  70. if (!decl_info.decl) {
  71. // The C++ type is not able to implement the interface.
  72. return SemIR::InstId::None;
  73. }
  74. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  75. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  76. cpp_fn, GetCppLocation(context, loc_id))) {
  77. return SemIR::ErrorInst::InstId;
  78. }
  79. auto fn_id =
  80. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  81. if (fn_id == SemIR::ErrorInst::InstId) {
  82. return SemIR::ErrorInst::InstId;
  83. }
  84. CheckCppOverloadAccess(
  85. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  86. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  87. return fn_id;
  88. }
  89. static auto BuildCopyWitness(
  90. Context& context, SemIR::LocId loc_id,
  91. SemIR::ConstantId query_self_const_id,
  92. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  93. auto& clang_sema = context.clang_sema();
  94. auto* tag_decl = TypeAsTagDecl(context, query_self_const_id);
  95. if (!tag_decl) {
  96. return SemIR::InstId::None;
  97. }
  98. if (auto* class_decl = dyn_cast<clang::CXXRecordDecl>(tag_decl)) {
  99. auto decl_info = DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  100. class_decl, clang::Qualifiers::Const),
  101. .signature = {.num_params = 1}};
  102. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  103. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  104. return fn_id;
  105. }
  106. return BuildCustomWitness(context, loc_id, query_self_const_id,
  107. query_specific_interface_id, {fn_id});
  108. }
  109. // Otherwise it's an enum (or eventually a C struct type). Perform a primitive
  110. // copy.
  111. return BuildPrimitiveCopyWitness(
  112. context, loc_id, GetClassScope(context, query_self_const_id),
  113. query_self_const_id, query_specific_interface_id);
  114. }
  115. static auto BuildCppUnsafeDerefWitness(
  116. Context& context, SemIR::LocId loc_id,
  117. SemIR::ConstantId query_self_const_id,
  118. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  119. auto& clang_sema = context.clang_sema();
  120. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  121. if (!class_decl) {
  122. return SemIR::InstId::None;
  123. }
  124. auto candidates = class_decl->lookup(
  125. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  126. clang::OO_Star));
  127. if (candidates.empty()) {
  128. return SemIR::InstId::None;
  129. }
  130. if (!candidates.isSingleResult()) {
  131. context.TODO(loc_id, "operator* overload sets not implemented yet");
  132. return SemIR::ErrorInst::InstId;
  133. }
  134. auto decl_info =
  135. DeclInfo{.decl = *candidates.begin(), .signature = {.num_params = 0}};
  136. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  137. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  138. return fn_id;
  139. }
  140. auto result_type_id =
  141. context.functions()
  142. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  143. .return_type_inst_id;
  144. if (result_type_id == SemIR::ErrorInst::InstId) {
  145. return SemIR::ErrorInst::InstId;
  146. }
  147. return BuildCustomWitness(context, loc_id, query_self_const_id,
  148. query_specific_interface_id,
  149. {result_type_id, fn_id});
  150. }
  151. static auto BuildDefaultWitness(
  152. Context& context, SemIR::LocId loc_id,
  153. SemIR::ConstantId query_self_const_id,
  154. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  155. auto& clang_sema = context.clang_sema();
  156. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  157. if (!class_decl) {
  158. return SemIR::InstId::None;
  159. }
  160. // Clang would produce a warning for classes with uninitialized
  161. // [[clang::requires_init]] fields for which default initialization is
  162. // performed, and we don't have a good place to produce that warning.
  163. // That happens if class_decl->hasUninitializedExplicitInitFields() is true.
  164. //
  165. // TODO: Consider treating such types as not implementing `Default`.
  166. auto decl_info =
  167. DeclInfo{.decl = clang_sema.LookupDefaultConstructor(class_decl),
  168. .signature = {.num_params = 0}};
  169. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  170. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  171. return fn_id;
  172. }
  173. return BuildCustomWitness(context, loc_id, query_self_const_id,
  174. query_specific_interface_id, {fn_id});
  175. }
  176. static auto BuildDestroyWitness(
  177. Context& context, SemIR::LocId loc_id,
  178. SemIR::ConstantId query_self_const_id,
  179. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  180. auto& clang_sema = context.clang_sema();
  181. // TODO: This should provide `Destroy` for enums and other trivially
  182. // destructible types.
  183. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  184. if (!class_decl) {
  185. return SemIR::InstId::None;
  186. }
  187. auto decl_info = DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  188. .signature = {.num_params = 0}};
  189. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  190. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  191. return fn_id;
  192. }
  193. return BuildCustomWitness(context, loc_id, query_self_const_id,
  194. query_specific_interface_id, {fn_id});
  195. }
  196. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  197. CoreInterface core_interface,
  198. SemIR::ConstantId query_self_const_id,
  199. SemIR::SpecificInterfaceId query_specific_interface_id,
  200. const TypeStructure* best_impl_type_structure,
  201. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  202. // TODO: Infer a C++ type structure and check whether it's less strict than
  203. // the best Carbon type structure.
  204. static_cast<void>(best_impl_type_structure);
  205. static_cast<void>(best_impl_loc_id);
  206. switch (core_interface) {
  207. case CoreInterface::Copy:
  208. return BuildCopyWitness(context, loc_id, query_self_const_id,
  209. query_specific_interface_id);
  210. case CoreInterface::CppUnsafeDeref:
  211. return BuildCppUnsafeDerefWitness(context, loc_id, query_self_const_id,
  212. query_specific_interface_id);
  213. case CoreInterface::Default:
  214. return BuildDefaultWitness(context, loc_id, query_self_const_id,
  215. query_specific_interface_id);
  216. case CoreInterface::Destroy:
  217. return BuildDestroyWitness(context, loc_id, query_self_const_id,
  218. query_specific_interface_id);
  219. // IntFitsIn is for Carbon integer types only.
  220. case CoreInterface::IntFitsIn:
  221. return SemIR::InstId::None;
  222. case CoreInterface::Unknown:
  223. CARBON_FATAL("unexpected CoreInterface `{0}`", core_interface);
  224. }
  225. }
  226. } // namespace Carbon::Check