impl_lookup.cpp 10 KB

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