impl_lookup.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/core_identifier.h"
  8. #include "toolchain/check/cpp/import.h"
  9. #include "toolchain/check/cpp/location.h"
  10. #include "toolchain/check/cpp/operators.h"
  11. #include "toolchain/check/cpp/overload_resolution.h"
  12. #include "toolchain/check/custom_witness.h"
  13. #include "toolchain/check/impl.h"
  14. #include "toolchain/check/impl_lookup.h"
  15. #include "toolchain/check/import_ref.h"
  16. #include "toolchain/check/inst.h"
  17. #include "toolchain/check/type.h"
  18. #include "toolchain/check/type_completion.h"
  19. #include "toolchain/sem_ir/builtin_function_kind.h"
  20. #include "toolchain/sem_ir/ids.h"
  21. #include "toolchain/sem_ir/typed_insts.h"
  22. namespace Carbon::Check {
  23. // Given a type constant, return the corresponding class scope if there is one.
  24. static auto GetClassScope(Context& context,
  25. SemIR::ConstantId query_self_const_id)
  26. -> SemIR::NameScopeId {
  27. auto class_type = context.constant_values().TryGetInstAs<SemIR::ClassType>(
  28. query_self_const_id);
  29. if (!class_type) {
  30. // Not a class.
  31. return SemIR::NameScopeId::None;
  32. }
  33. return context.classes().Get(class_type->class_id).scope_id;
  34. }
  35. // If the given type is a C++ tag (class or enumeration) type, returns the
  36. // corresponding tag declaration. Otherwise returns nullptr.
  37. // TODO: Handle qualified types.
  38. static auto TypeAsTagDecl(Context& context,
  39. SemIR::ConstantId query_self_const_id)
  40. -> clang::TagDecl* {
  41. SemIR::NameScopeId class_scope_id =
  42. GetClassScope(context, query_self_const_id);
  43. if (!class_scope_id.has_value()) {
  44. return nullptr;
  45. }
  46. const auto& scope = context.name_scopes().Get(class_scope_id);
  47. auto decl_id = scope.clang_decl_context_id();
  48. if (!decl_id.has_value()) {
  49. return nullptr;
  50. }
  51. return dyn_cast<clang::TagDecl>(context.clang_decls().Get(decl_id).key.decl);
  52. }
  53. // If the given type is a C++ class type, returns the corresponding class
  54. // declaration. Otherwise returns nullptr.
  55. static auto TypeAsClassDecl(Context& context,
  56. SemIR::ConstantId query_self_const_id)
  57. -> clang::CXXRecordDecl* {
  58. return dyn_cast_or_null<clang::CXXRecordDecl>(
  59. TypeAsTagDecl(context, query_self_const_id));
  60. }
  61. namespace {
  62. struct DeclInfo {
  63. // If null, no C++ decl was found and no witness can be created.
  64. clang::NamedDecl* decl = nullptr;
  65. SemIR::ClangDeclKey::Signature signature;
  66. };
  67. } // namespace
  68. // Finds the InstId for the C++ function that is called by a specific interface.
  69. // Returns SemIR::InstId::None if a C++ function is not found, and
  70. // SemIR::ErrorInst::InstId if an error occurs.
  71. static auto GetFunctionId(Context& context, SemIR::LocId loc_id,
  72. DeclInfo decl_info) -> SemIR::InstId {
  73. if (!decl_info.decl) {
  74. // The C++ type is not able to implement the interface.
  75. return SemIR::InstId::None;
  76. }
  77. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  78. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  79. cpp_fn, GetCppLocation(context, loc_id))) {
  80. return SemIR::ErrorInst::InstId;
  81. }
  82. auto fn_id =
  83. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  84. if (fn_id == SemIR::ErrorInst::InstId) {
  85. return SemIR::ErrorInst::InstId;
  86. }
  87. CheckCppOverloadAccess(
  88. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  89. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  90. return fn_id;
  91. }
  92. static auto BuildCopyWitness(
  93. Context& context, SemIR::LocId loc_id,
  94. SemIR::ConstantId query_self_const_id,
  95. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  96. auto& clang_sema = context.clang_sema();
  97. auto* tag_decl = TypeAsTagDecl(context, query_self_const_id);
  98. if (!tag_decl) {
  99. return SemIR::InstId::None;
  100. }
  101. if (auto* class_decl = dyn_cast<clang::CXXRecordDecl>(tag_decl)) {
  102. auto class_type_id = SemIR::TypeId::ForTypeConstant(query_self_const_id);
  103. if (!Check::RequireCompleteType(
  104. context, class_type_id, SemIR::LocId::None, [&](auto& builder) {
  105. CARBON_DIAGNOSTIC(IncompleteTypeInCopyWitness, Context,
  106. "argument to C++ call has incomplete type {0}",
  107. SemIR::TypeId);
  108. builder.Context(loc_id, IncompleteTypeInCopyWitness,
  109. class_type_id);
  110. })) {
  111. return SemIR::ErrorInst::InstId;
  112. }
  113. auto decl_info = DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  114. class_decl, clang::Qualifiers::Const),
  115. .signature = {.num_params = 1}};
  116. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  117. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  118. return fn_id;
  119. }
  120. return BuildCustomWitness(context, loc_id, query_self_const_id,
  121. query_specific_interface_id, {fn_id});
  122. }
  123. // Otherwise it's an enum (or eventually a C struct type). Perform a primitive
  124. // copy.
  125. return BuildPrimitiveCopyWitness(
  126. context, loc_id, GetClassScope(context, query_self_const_id),
  127. query_self_const_id, query_specific_interface_id);
  128. }
  129. static auto BuildCppUnsafeDerefWitness(
  130. Context& context, SemIR::LocId loc_id,
  131. SemIR::ConstantId query_self_const_id,
  132. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  133. auto& clang_sema = context.clang_sema();
  134. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  135. if (!class_decl) {
  136. return SemIR::InstId::None;
  137. }
  138. auto candidates = class_decl->lookup(
  139. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  140. clang::OO_Star));
  141. if (candidates.empty()) {
  142. return SemIR::InstId::None;
  143. }
  144. if (!candidates.isSingleResult()) {
  145. context.TODO(loc_id, "operator* overload sets not implemented yet");
  146. return SemIR::ErrorInst::InstId;
  147. }
  148. auto decl_info =
  149. DeclInfo{.decl = *candidates.begin(), .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. auto result_type_id =
  155. context.functions()
  156. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  157. .return_type_inst_id;
  158. if (result_type_id == SemIR::ErrorInst::InstId) {
  159. return SemIR::ErrorInst::InstId;
  160. }
  161. return BuildCustomWitness(context, loc_id, query_self_const_id,
  162. query_specific_interface_id,
  163. {result_type_id, fn_id});
  164. }
  165. static auto BuildDefaultWitness(
  166. Context& context, SemIR::LocId loc_id,
  167. SemIR::ConstantId query_self_const_id,
  168. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  169. auto& clang_sema = context.clang_sema();
  170. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  171. if (!class_decl) {
  172. return SemIR::InstId::None;
  173. }
  174. // Clang would produce a warning for classes with uninitialized
  175. // [[clang::requires_init]] fields for which default initialization is
  176. // performed, and we don't have a good place to produce that warning.
  177. // That happens if class_decl->hasUninitializedExplicitInitFields() is true.
  178. //
  179. // TODO: Consider treating such types as not implementing `Default`.
  180. auto decl_info =
  181. DeclInfo{.decl = clang_sema.LookupDefaultConstructor(class_decl),
  182. .signature = {.num_params = 0}};
  183. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  184. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  185. return fn_id;
  186. }
  187. return BuildCustomWitness(context, loc_id, query_self_const_id,
  188. query_specific_interface_id, {fn_id});
  189. }
  190. static auto BuildDestroyWitness(
  191. Context& context, SemIR::LocId loc_id,
  192. SemIR::ConstantId query_self_const_id,
  193. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  194. auto& clang_sema = context.clang_sema();
  195. // TODO: This should provide `Destroy` for enums and other trivially
  196. // destructible types.
  197. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  198. if (!class_decl) {
  199. return SemIR::InstId::None;
  200. }
  201. auto decl_info = DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  202. .signature = {.num_params = 0}};
  203. auto fn_id = GetFunctionId(context, loc_id, decl_info);
  204. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  205. return fn_id;
  206. }
  207. return BuildCustomWitness(context, loc_id, query_self_const_id,
  208. query_specific_interface_id, {fn_id});
  209. }
  210. // Attempts to build a witness table entry for a C++ unary operator.
  211. static auto BuildCppUnaryOperatorWitness(
  212. Context& context, SemIR::LocId loc_id, SemIR::CoreInterface core_interface,
  213. bool has_associated_result_type, SemIR::ConstantId query_self_const_id,
  214. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  215. auto self_type_id =
  216. context.types().GetTypeIdForTypeConstantId(query_self_const_id);
  217. auto fn_id = LookupCppOperator(
  218. context, loc_id, {.interface_name = AsCoreIdentifier(core_interface)},
  219. {self_type_id});
  220. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  221. return fn_id;
  222. }
  223. if (has_associated_result_type) {
  224. auto result_type_id =
  225. context.functions()
  226. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  227. .return_type_inst_id;
  228. if (result_type_id == SemIR::ErrorInst::InstId) {
  229. return SemIR::ErrorInst::InstId;
  230. }
  231. return BuildCustomWitness(context, loc_id, query_self_const_id,
  232. query_specific_interface_id,
  233. {result_type_id, fn_id});
  234. }
  235. return BuildCustomWitness(context, loc_id, query_self_const_id,
  236. query_specific_interface_id, {fn_id});
  237. }
  238. // Attempts to build a witness table entry for a C++ binary operator.
  239. static auto BuildCppBinaryOperatorWitness(
  240. Context& context, SemIR::LocId loc_id, SemIR::CoreInterface core_interface,
  241. bool has_associated_result_type, SemIR::ConstantId query_self_const_id,
  242. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  243. auto self_type_id =
  244. context.types().GetTypeIdForTypeConstantId(query_self_const_id);
  245. auto args =
  246. context.inst_blocks().Get(context.specifics()
  247. .Get(context.specific_interfaces()
  248. .Get(query_specific_interface_id)
  249. .specific_id)
  250. .args_id);
  251. CARBON_CHECK(args.size() == 1, "Binary operator missing an argument");
  252. auto arg_type_id = context.types().GetTypeIdForTypeInstId(args.front());
  253. auto fn_id = LookupCppOperator(
  254. context, loc_id, {.interface_name = AsCoreIdentifier(core_interface)},
  255. {self_type_id, arg_type_id});
  256. if (fn_id == SemIR::ErrorInst::InstId || fn_id == SemIR::InstId::None) {
  257. return fn_id;
  258. }
  259. if (has_associated_result_type) {
  260. auto result_type_id =
  261. context.functions()
  262. .Get(context.insts().GetAs<SemIR::FunctionDecl>(fn_id).function_id)
  263. .return_type_inst_id;
  264. if (result_type_id == SemIR::ErrorInst::InstId) {
  265. return SemIR::ErrorInst::InstId;
  266. }
  267. return BuildCustomWitness(context, loc_id, query_self_const_id,
  268. query_specific_interface_id,
  269. {result_type_id, fn_id});
  270. }
  271. return BuildCustomWitness(context, loc_id, query_self_const_id,
  272. query_specific_interface_id, {fn_id});
  273. }
  274. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  275. SemIR::CoreInterface core_interface,
  276. SemIR::ConstantId query_self_const_id,
  277. SemIR::SpecificInterfaceId query_specific_interface_id,
  278. const TypeStructure* best_impl_type_structure,
  279. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  280. // TODO: Infer a C++ type structure and check whether it's less strict than
  281. // the best Carbon type structure.
  282. static_cast<void>(best_impl_type_structure);
  283. static_cast<void>(best_impl_loc_id);
  284. switch (core_interface) {
  285. case SemIR::CoreInterface::Inc:
  286. case SemIR::CoreInterface::Dec:
  287. return BuildCppUnaryOperatorWitness(context, loc_id, core_interface,
  288. /*has_associated_result_type=*/false,
  289. query_self_const_id,
  290. query_specific_interface_id);
  291. case SemIR::CoreInterface::Negate:
  292. return BuildCppUnaryOperatorWitness(
  293. context, loc_id, core_interface, /*has_associated_result_type=*/true,
  294. query_self_const_id, query_specific_interface_id);
  295. case SemIR::CoreInterface::AddWith:
  296. case SemIR::CoreInterface::SubWith:
  297. case SemIR::CoreInterface::MulWith:
  298. case SemIR::CoreInterface::DivWith:
  299. case SemIR::CoreInterface::ModWith:
  300. return BuildCppBinaryOperatorWitness(context, loc_id, core_interface,
  301. /*has_associated_result_type=*/true,
  302. query_self_const_id,
  303. query_specific_interface_id);
  304. case SemIR::CoreInterface::AddAssignWith:
  305. case SemIR::CoreInterface::SubAssignWith:
  306. case SemIR::CoreInterface::MulAssignWith:
  307. case SemIR::CoreInterface::DivAssignWith:
  308. case SemIR::CoreInterface::ModAssignWith:
  309. return BuildCppBinaryOperatorWitness(context, loc_id, core_interface,
  310. /*has_associated_result_type=*/false,
  311. query_self_const_id,
  312. query_specific_interface_id);
  313. case SemIR::CoreInterface::Copy:
  314. return BuildCopyWitness(context, loc_id, query_self_const_id,
  315. query_specific_interface_id);
  316. case SemIR::CoreInterface::CppUnsafeDeref:
  317. return BuildCppUnsafeDerefWitness(context, loc_id, query_self_const_id,
  318. query_specific_interface_id);
  319. case SemIR::CoreInterface::Default:
  320. return BuildDefaultWitness(context, loc_id, query_self_const_id,
  321. query_specific_interface_id);
  322. case SemIR::CoreInterface::Destroy:
  323. return BuildDestroyWitness(context, loc_id, query_self_const_id,
  324. query_specific_interface_id);
  325. // IntFitsIn is for Carbon integer types only.
  326. case SemIR::CoreInterface::IntFitsIn:
  327. return SemIR::InstId::None;
  328. case SemIR::CoreInterface::Unknown:
  329. CARBON_FATAL("unexpected CoreInterface `{0}`", core_interface);
  330. }
  331. }
  332. } // namespace Carbon::Check