impl_lookup.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, SemIR::TypeId type_id)
  23. -> clang::CXXRecordDecl* {
  24. auto class_type = context.types().TryGetAs<SemIR::ClassType>(type_id);
  25. if (!class_type) {
  26. // Not a class.
  27. return nullptr;
  28. }
  29. SemIR::NameScopeId class_scope_id =
  30. context.classes().Get(class_type->class_id).scope_id;
  31. if (!class_scope_id.has_value()) {
  32. return nullptr;
  33. }
  34. const auto& scope = context.name_scopes().Get(class_scope_id);
  35. auto decl_id = scope.clang_decl_context_id();
  36. if (!decl_id.has_value()) {
  37. return nullptr;
  38. }
  39. return dyn_cast<clang::CXXRecordDecl>(
  40. context.clang_decls().Get(decl_id).key.decl);
  41. }
  42. static auto BuildSingleFunctionWitness(
  43. Context& context, SemIR::LocId loc_id, clang::FunctionDecl* cpp_fn,
  44. clang::DeclAccessPair found_decl, int num_params,
  45. SemIR::TypeId self_type_id, SemIR::SpecificInterface specific_interface)
  46. -> SemIR::InstId {
  47. auto fn_id = context.clang_sema().DiagnoseUseOfOverloadedDecl(
  48. cpp_fn, GetCppLocation(context, loc_id))
  49. ? SemIR::ErrorInst::InstId
  50. : ImportCppFunctionDecl(context, loc_id, cpp_fn, num_params);
  51. if (auto fn_decl =
  52. context.insts().TryGetAsWithId<SemIR::FunctionDecl>(fn_id)) {
  53. CheckCppOverloadAccess(context, loc_id, found_decl, fn_decl->inst_id);
  54. } else {
  55. CARBON_CHECK(fn_id == SemIR::ErrorInst::InstId);
  56. return SemIR::ErrorInst::InstId;
  57. }
  58. return BuildCustomWitness(context, loc_id, self_type_id, specific_interface,
  59. {fn_id});
  60. }
  61. static auto LookupCopyImpl(Context& context, SemIR::LocId loc_id,
  62. SemIR::TypeId self_type_id,
  63. SemIR::SpecificInterface specific_interface)
  64. -> SemIR::InstId {
  65. auto* class_decl = TypeAsClassDecl(context, self_type_id);
  66. if (!class_decl) {
  67. // TODO: Should we also provide a `Copy` implementation for enumerations?
  68. return SemIR::InstId::None;
  69. }
  70. auto* ctor = context.clang_sema().LookupCopyingConstructor(
  71. class_decl, clang::Qualifiers::Const);
  72. if (!ctor) {
  73. // TODO: If the impl lookup failure is an error, we should produce a
  74. // diagnostic explaining why the class is not copyable.
  75. return SemIR::InstId::None;
  76. }
  77. return BuildSingleFunctionWitness(
  78. context, loc_id, ctor,
  79. clang::DeclAccessPair::make(ctor, ctor->getAccess()), /*num_params=*/1,
  80. self_type_id, specific_interface);
  81. }
  82. static auto LookupDestroyImpl(Context& context, SemIR::LocId loc_id,
  83. SemIR::TypeId self_type_id,
  84. SemIR::SpecificInterface specific_interface)
  85. -> SemIR::InstId {
  86. auto* class_decl = TypeAsClassDecl(context, self_type_id);
  87. if (!class_decl) {
  88. return SemIR::InstId::None;
  89. }
  90. auto* dtor = context.clang_sema().LookupDestructor(class_decl);
  91. if (!dtor) {
  92. // TODO: If the impl lookup failure is an error, we should produce a
  93. // diagnostic explaining why the class is not destructible.
  94. return SemIR::InstId::None;
  95. }
  96. return BuildSingleFunctionWitness(
  97. context, loc_id, dtor,
  98. clang::DeclAccessPair::make(dtor, dtor->getAccess()), /*num_params=*/0,
  99. self_type_id, specific_interface);
  100. }
  101. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  102. SemIR::TypeId self_type_id, CoreInterface core_interface,
  103. SemIR::SpecificInterface specific_interface,
  104. const TypeStructure* best_impl_type_structure,
  105. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  106. // TODO: Handle other interfaces.
  107. switch (core_interface) {
  108. case CoreInterface::Copy:
  109. return LookupCopyImpl(context, loc_id, self_type_id, specific_interface);
  110. case CoreInterface::Destroy:
  111. return LookupDestroyImpl(context, loc_id, self_type_id,
  112. specific_interface);
  113. case CoreInterface::Unknown:
  114. CARBON_FATAL("shouldn't be called with `Unknown`");
  115. }
  116. // TODO: Infer a C++ type structure and check whether it's less strict than
  117. // the best Carbon type structure.
  118. static_cast<void>(best_impl_type_structure);
  119. static_cast<void>(best_impl_loc_id);
  120. return SemIR::InstId::None;
  121. }
  122. } // namespace Carbon::Check