impl_lookup.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 to indicate nothing was
  53. // found, or nullopt to indicate `SemIR::ErrInst::InstId` should be propagated.
  54. auto GetDeclForCoreInterface(Context& context, SemIR::LocId loc_id,
  55. CoreInterface core_interface,
  56. clang::CXXRecordDecl* class_decl)
  57. -> std::optional<DeclInfo> {
  58. auto& clang_sema = context.clang_sema();
  59. CARBON_CHECK(class_decl != nullptr);
  60. // TODO: Handle other interfaces.
  61. switch (core_interface) {
  62. case CoreInterface::Copy:
  63. return DeclInfo{.decl = clang_sema.LookupCopyingConstructor(
  64. class_decl, clang::Qualifiers::Const),
  65. .signature = {.num_params = 1}};
  66. case CoreInterface::Destroy:
  67. return DeclInfo{.decl = clang_sema.LookupDestructor(class_decl),
  68. .signature = {.num_params = 0}};
  69. case CoreInterface::CppUnsafeDeref: {
  70. auto candidates = class_decl->lookup(
  71. clang_sema.getASTContext().DeclarationNames.getCXXOperatorName(
  72. clang::OO_Star));
  73. if (candidates.empty()) {
  74. return DeclInfo{};
  75. }
  76. if (!candidates.isSingleResult()) {
  77. context.TODO(loc_id, "operator* overload sets not implemented yet");
  78. return std::nullopt;
  79. }
  80. return DeclInfo{.decl = *candidates.begin(),
  81. .signature = {.num_params = 0}};
  82. }
  83. case CoreInterface::IntFitsIn:
  84. case CoreInterface::Unknown:
  85. CARBON_FATAL("shouldn't be called with `{}`", core_interface);
  86. }
  87. }
  88. auto LookupCppImpl(Context& context, SemIR::LocId loc_id,
  89. CoreInterface core_interface,
  90. SemIR::ConstantId query_self_const_id,
  91. SemIR::SpecificInterfaceId query_specific_interface_id,
  92. const TypeStructure* best_impl_type_structure,
  93. SemIR::LocId best_impl_loc_id) -> SemIR::InstId {
  94. // TODO: This should provide `Destroy` for enums and other trivially
  95. // destructible types.
  96. auto* class_decl = TypeAsClassDecl(context, query_self_const_id);
  97. if (!class_decl) {
  98. return SemIR::InstId::None;
  99. }
  100. auto decl_info =
  101. GetDeclForCoreInterface(context, loc_id, core_interface, class_decl);
  102. if (!decl_info.has_value()) {
  103. return SemIR::ErrorInst::InstId;
  104. }
  105. if (!decl_info->decl) {
  106. // TODO: If the impl lookup failure is an error, we should produce a
  107. // diagnostic explaining why the class does not satisfy the core interface.
  108. return SemIR::InstId::None;
  109. }
  110. auto* cpp_fn = cast<clang::FunctionDecl>(decl_info->decl);
  111. if (context.clang_sema().DiagnoseUseOfOverloadedDecl(
  112. cpp_fn, GetCppLocation(context, loc_id))) {
  113. return SemIR::ErrorInst::InstId;
  114. }
  115. auto fn_id =
  116. ImportCppFunctionDecl(context, loc_id, cpp_fn, decl_info->signature);
  117. if (fn_id == SemIR::ErrorInst::InstId) {
  118. return SemIR::ErrorInst::InstId;
  119. }
  120. CheckCppOverloadAccess(
  121. context, loc_id, clang::DeclAccessPair::make(cpp_fn, cpp_fn->getAccess()),
  122. context.insts().GetAsKnownInstId<SemIR::FunctionDecl>(fn_id));
  123. // TODO: Infer a C++ type structure and check whether it's less strict than
  124. // the best Carbon type structure.
  125. static_cast<void>(best_impl_type_structure);
  126. static_cast<void>(best_impl_loc_id);
  127. return BuildCustomWitness(context, loc_id, query_self_const_id,
  128. query_specific_interface_id, {fn_id});
  129. }
  130. } // namespace Carbon::Check