impl_lookup.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. int num_params;
  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. .num_params = 1};
  62. case CoreInterface::Destroy:
  63. return {.decl = clang_sema.LookupDestructor(class_decl), .num_params = 0};
  64. case CoreInterface::Unknown:
  65. CARBON_FATAL("shouldn't be called with `Unknown`");
  66. }
  67. }
  68. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  69. CoreInterface core_interface,
  70. SemIR::ConstantId query_self_const_id,
  71. SemIR::SpecificInterfaceId query_specific_interface_id,
  72. const TypeStructure* best_impl_type_structure,
  73. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  74. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  75. if (!class_decl) {
  76. return SemIR::InstId::None;
  77. }
  78. auto decl_info =
  79. GetDeclForCoreInterface(context.clang_sema(), core_interface, class_decl);
  80. if (!decl_info.decl) {
  81. // TODO: If the impl lookup failure is an error, we should produce a
  82. // diagnostic explaining why the class is not copyable/destructible.
  83. return SemIR::InstId::None;
  84. }
  85. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info.decl);
  86. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  87. cpp_fn, GetCppLocation(context, loc_id))) {
  88. return SemIR::ErrorInst::InstId;
  89. }
  90. auto fn_id =
  91. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info.num_params);
  92. if (fn_id == SemIR::ErrorInst::InstId) {
  93. return SemIR::ErrorInst::InstId;
  94. }
  95. CheckCppOverloadAccess(
  96. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  97. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  98. // TODO: Infer a C++ type structure and check whether it's less strict than
  99. // the best Carbon type structure.
  100. static_cast<void>(best_impl_type_structure);
  101. static_cast<void>(best_impl_loc_id);
  102. return BuildCustomWitness(context, loc_id, query_self_const_id,
  103. query_specific_interface_id, {fn_id});
  104. }
  105. } // namespace Carbon::Check