impl_lookup.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. static auto BuildSingleFunctionWitness(
  45. Context& context, SemIR::LocId loc_id, clang::FunctionDecl* cpp_fn,
  46. clang::DeclAccessPair found_decl, int num_params,
  47. SemIR::ConstantId query_self_const_id,
  48. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  49. auto fn_id = context.clang_sema().DiagnoseUseOfOverloadedDecl(
  50. cpp_fn, GetCppLocation(context, loc_id))
  51. ? SemIR::ErrorInst::InstId
  52. : ImportCppFunctionDecl(context, loc_id, cpp_fn, num_params);
  53. if (auto fn_decl =
  54. context.insts().TryGetAsWithId<SemIR::FunctionDecl>(fn_id)) {
  55. CheckCppOverloadAccess(context, loc_id, found_decl, fn_decl->inst_id);
  56. } else {
  57. CARBON_CHECK(fn_id == SemIR::ErrorInst::InstId);
  58. return SemIR::ErrorInst::InstId;
  59. }
  60. return BuildCustomWitness(context, loc_id, query_self_const_id,
  61. query_specific_interface_id, {fn_id});
  62. }
  63. static auto LookupCopyImpl(
  64. Context& context, SemIR::LocId loc_id,
  65. SemIR::ConstantId query_self_const_id,
  66. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  67. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  68. if (!class_decl) {
  69. // TODO: Should we also provide a `Copy` implementation for enumerations?
  70. return SemIR::InstId::None;
  71. }
  72. auto* ctor = context.clang_sema().LookupCopyingConstructor(
  73. class_decl, clang::Qualifiers::Const);
  74. if (!ctor) {
  75. // TODO: If the impl lookup failure is an error, we should produce a
  76. // diagnostic explaining why the class is not copyable.
  77. return SemIR::InstId::None;
  78. }
  79. return BuildSingleFunctionWitness(
  80. context, loc_id, ctor,
  81. clang::DeclAccessPair::make(ctor, ctor->getAccess()), /*num_params=*/1,
  82. query_self_const_id, query_specific_interface_id);
  83. }
  84. static auto LookupDestroyImpl(
  85. Context& context, SemIR::LocId loc_id,
  86. SemIR::ConstantId query_self_const_id,
  87. SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
  88. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  89. if (!class_decl) {
  90. return SemIR::InstId::None;
  91. }
  92. auto* dtor = context.clang_sema().LookupDestructor(class_decl);
  93. if (!dtor) {
  94. // TODO: If the impl lookup failure is an error, we should produce a
  95. // diagnostic explaining why the class is not destructible.
  96. return SemIR::InstId::None;
  97. }
  98. return BuildSingleFunctionWitness(
  99. context, loc_id, dtor,
  100. clang::DeclAccessPair::make(dtor, dtor->getAccess()), /*num_params=*/0,
  101. query_self_const_id, query_specific_interface_id);
  102. }
  103. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  104. CoreInterface core_interface,
  105. SemIR::ConstantId query_self_const_id,
  106. SemIR::SpecificInterfaceId query_specific_interface_id,
  107. const TypeStructure* best_impl_type_structure,
  108. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  109. // TODO: Handle other interfaces.
  110. switch (core_interface) {
  111. case CoreInterface::Copy:
  112. return LookupCopyImpl(context, loc_id, query_self_const_id,
  113. query_specific_interface_id);
  114. case CoreInterface::Destroy:
  115. return LookupDestroyImpl(context, loc_id, query_self_const_id,
  116. query_specific_interface_id);
  117. case CoreInterface::Unknown:
  118. CARBON_FATAL("shouldn't be called with `Unknown`");
  119. }
  120. // TODO: Infer a C++ type structure and check whether it's less strict than
  121. // the best Carbon type structure.
  122. static_cast<void>(best_impl_type_structure);
  123. static_cast<void>(best_impl_loc_id);
  124. return SemIR::InstId::None;
  125. }
  126. } // namespace Carbon::Check