operator.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/operator.h"
  5. #include <optional>
  6. #include "toolchain/check/call.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/import_cpp.h"
  10. #include "toolchain/check/member_access.h"
  11. #include "toolchain/check/name_lookup.h"
  12. #include "toolchain/sem_ir/class.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/name_scope.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Check {
  17. // Returns the `Op` function for the specified operator.
  18. static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
  19. Operator op) -> SemIR::InstId {
  20. auto implicit_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
  21. // Look up the interface, and pass it any generic arguments.
  22. auto interface_id =
  23. LookupNameInCore(context, implicit_loc_id, op.interface_name);
  24. if (!op.interface_args_ref.empty()) {
  25. interface_id = PerformCall(context, implicit_loc_id, interface_id,
  26. op.interface_args_ref);
  27. }
  28. // Look up the interface member.
  29. auto op_name_id =
  30. SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
  31. return PerformMemberAccess(context, implicit_loc_id, interface_id,
  32. op_name_id);
  33. }
  34. // If the instruction is a C++ class, returns its parent scope id. Otherwise
  35. // returns `std::nullopt`.
  36. static auto GetCppClassTypeParentScope(Context& context, SemIR::InstId inst_id)
  37. -> std::optional<SemIR::NameScopeId> {
  38. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(
  39. context.types().GetInstId(context.insts().Get(inst_id).type_id()));
  40. if (!class_type) {
  41. // Not a class.
  42. return std::nullopt;
  43. }
  44. const SemIR::Class& class_info = context.classes().Get(class_type->class_id);
  45. if (!class_info.is_complete() ||
  46. !context.name_scopes().Get(class_info.scope_id).is_cpp_scope()) {
  47. // Not a C++ class.
  48. return std::nullopt;
  49. }
  50. SemIR::NameScopeId parent_scope_id = class_info.parent_scope_id;
  51. do {
  52. SemIR::NameScope& scope = context.name_scopes().Get(parent_scope_id);
  53. if (context.insts().Is<SemIR::Namespace>(scope.inst_id())) {
  54. break;
  55. }
  56. parent_scope_id = scope.parent_scope_id();
  57. } while (parent_scope_id.has_value());
  58. return parent_scope_id;
  59. }
  60. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  61. SemIR::InstId operand_id,
  62. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  63. -> SemIR::InstId {
  64. // For unary operators with a C++ class as the operand, try to import and call
  65. // the C++ operator.
  66. // TODO: Change impl lookup instead. See
  67. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  68. // TODO: We should do ADL-only lookup for operators
  69. // (`Sema::ArgumentDependentLookup`), when we support mapping Carbon types
  70. // into C++ types.
  71. auto cpp_parent_scope_id = GetCppClassTypeParentScope(context, operand_id);
  72. if (cpp_parent_scope_id) {
  73. SemIR::ScopeLookupResult cpp_lookup_result =
  74. ImportOperatorFromCpp(context, loc_id, *cpp_parent_scope_id, op);
  75. if (cpp_lookup_result.is_found()) {
  76. return PerformCall(context, loc_id, cpp_lookup_result.target_inst_id(),
  77. {operand_id});
  78. }
  79. }
  80. // Look up the operator function.
  81. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  82. // Form `operand.(Op)`.
  83. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  84. op_fn, missing_impl_diagnoser);
  85. if (bound_op_id == SemIR::ErrorInst::InstId) {
  86. return SemIR::ErrorInst::InstId;
  87. }
  88. // Form `bound_op()`.
  89. return PerformCall(context, loc_id, bound_op_id, {});
  90. }
  91. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  92. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  93. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  94. -> SemIR::InstId {
  95. // For binary operators with a C++ class as at least one of the operands, try
  96. // to import and call the C++ operator.
  97. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  98. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  99. // addable. See
  100. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  101. // and
  102. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  103. // TODO: We should do ADL-only lookup for operators
  104. // (`Sema::ArgumentDependentLookup`), when we support mapping Carbon types
  105. // into C++ types.
  106. llvm::SmallVector<SemIR::NameScopeId, 2> cpp_operand_parent_scope_ids;
  107. for (SemIR::InstId operand_id : {lhs_id, rhs_id}) {
  108. auto cpp_parent_scope_id = GetCppClassTypeParentScope(context, operand_id);
  109. if (!cpp_parent_scope_id || llvm::is_contained(cpp_operand_parent_scope_ids,
  110. *cpp_parent_scope_id)) {
  111. continue;
  112. }
  113. cpp_operand_parent_scope_ids.push_back(*cpp_parent_scope_id);
  114. SemIR::ScopeLookupResult cpp_lookup_result =
  115. ImportOperatorFromCpp(context, loc_id, *cpp_parent_scope_id, op);
  116. if (cpp_lookup_result.is_found()) {
  117. return PerformCall(context, loc_id, cpp_lookup_result.target_inst_id(),
  118. {lhs_id, rhs_id});
  119. }
  120. }
  121. // Look up the operator function.
  122. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  123. // Form `lhs.(Op)`.
  124. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  125. missing_impl_diagnoser);
  126. if (bound_op_id == SemIR::ErrorInst::InstId) {
  127. return SemIR::ErrorInst::InstId;
  128. }
  129. // Form `bound_op(rhs)`.
  130. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  131. }
  132. } // namespace Carbon::Check