impl_lookup.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // See `GetDeclForCoreInterface`.
  46. struct DeclInfo {
  47. clang::NamedDecl* decl;
  48. SemIR::ClangDeclKey::Signature signature;
  49. };
  50. } // namespace
  51. // Retrieves a `core_interface`'s corresponding `NamedDecl`, also with the
  52. // expected number of parameters. May return a null decl.
  53. auto GetDeclForCoreInterface(clang::Sema& clang_sema,
  54. CoreInterface core_interface,
  55. clang::CXXRecordDecl* class_decl) -> DeclInfo {
  56. // TODO: Handle other interfaces.
  57. switch (core_interface) {
  58. case CoreInterface::Copy:
  59. return {.decl = clang_sema.LookupCopyingConstructor(
  60. class_decl, clang::Qualifiers::Const),
  61. .signature = {.num_params = 1}};
  62. case CoreInterface::Destroy:
  63. return {.decl = clang_sema.LookupDestructor(class_decl),
  64. .signature = {.num_params = 0}};
  65. case CoreInterface::Unknown:
  66. CARBON_FATAL("shouldn't be called with `Unknown`");
  67. }
  68. }
  69. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  70. CoreInterface core_interface,
  71. SemIR::ConstantId query_self_const_id,
  72. SemIR::SpecificInterfaceId query_specific_interface_id,
  73. const TypeStructure* best_impl_type_structure,
  74. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  75. // TODO: This should provide `Destroy` for enums and other trivially
  76. // destructible types.
  77. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  78. if (!class_decl) {
  79. return SemIR::InstId::None;
  80. }
  81. auto decl_info =
  82. GetDeclForCoreInterface(context.clang_sema(), core_interface, class_decl);
  83. if (!decl_info.decl) {
  84. // TODO: If the impl lookup failure is an error, we should produce a
  85. // diagnostic explaining why the class is not copyable/destructible.
  86. return SemIR::InstId::None;
  87. }
  88. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  89. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  90. cpp_fn, GetCppLocation(context, loc_id))) {
  91. return SemIR::ErrorInst::InstId;
  92. }
  93. auto fn_id =
  94. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.signature);
  95. if (fn_id == SemIR::ErrorInst::InstId) {
  96. return SemIR::ErrorInst::InstId;
  97. }
  98. CheckCppOverloadAccess(
  99. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  100. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  101. // TODO: Infer a C++ type structure and check whether it's less strict than
  102. // the best Carbon type structure.
  103. static_cast<void>(best_impl_type_structure);
  104. static_cast<void>(best_impl_loc_id);
  105. return BuildCustomWitness(context, loc_id, query_self_const_id,
  106. query_specific_interface_id, {fn_id});
  107. }
  108. } // namespace Carbon::Check