operator.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/cpp/call.h"
  9. #include "toolchain/check/cpp/operators.h"
  10. #include "toolchain/check/generic.h"
  11. #include "toolchain/check/member_access.h"
  12. #include "toolchain/check/name_lookup.h"
  13. #include "toolchain/sem_ir/class.h"
  14. #include "toolchain/sem_ir/ids.h"
  15. #include "toolchain/sem_ir/name_scope.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Check {
  18. // Returns the `Op` function for the specified operator.
  19. static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
  20. Operator op) -> SemIR::InstId {
  21. auto implicit_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
  22. // Look up the interface, and pass it any generic arguments.
  23. // TODO: Improve diagnostics when the found `interface_id` isn't callable.
  24. auto interface_id =
  25. LookupNameInCore(context, implicit_loc_id, op.interface_name);
  26. if (!op.interface_args_ref.empty()) {
  27. interface_id = PerformCall(context, implicit_loc_id, interface_id,
  28. op.interface_args_ref);
  29. }
  30. // Look up the interface member.
  31. auto op_name_id = context.core_identifiers().AddNameId(op.op_name);
  32. return PerformMemberAccess(context, implicit_loc_id, interface_id,
  33. op_name_id);
  34. }
  35. // Returns whether the instruction is a C++ class.
  36. static auto IsCppClassType(Context& context, SemIR::InstId inst_id) -> bool {
  37. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(
  38. context.types().GetInstId(context.insts().Get(inst_id).type_id()));
  39. if (!class_type) {
  40. // Not a class.
  41. return false;
  42. }
  43. SemIR::NameScopeId class_scope_id =
  44. context.classes().Get(class_type->class_id).scope_id;
  45. return class_scope_id.has_value() &&
  46. context.name_scopes().Get(class_scope_id).is_cpp_scope();
  47. }
  48. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  49. SemIR::InstId operand_id,
  50. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  51. -> SemIR::InstId {
  52. if (operand_id == SemIR::ErrorInst::InstId) {
  53. // Exit early for errors, which prevent forming an `Op` function.
  54. return SemIR::ErrorInst::InstId;
  55. }
  56. // Operator operands don't require `ref` tags.
  57. context.ref_tags().Insert(operand_id, Context::RefTag::NotRequired);
  58. SemIR::InstId op_fn_id = SemIR::InstId::None;
  59. // For unary operators with a C++ class as the operand, try to import and call
  60. // the C++ operator.
  61. // TODO: Change impl lookup instead. See
  62. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  63. if (IsCppClassType(context, operand_id)) {
  64. op_fn_id = LookupCppOperator(context, loc_id, op, {operand_id});
  65. // If C++ operator lookup found a non-method operator, call it with one call
  66. // argument. Otherwise fall through to call it with a self argument.
  67. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  68. return PerformCall(context, loc_id, op_fn_id, {operand_id});
  69. }
  70. }
  71. if (!op_fn_id.has_value()) {
  72. // Look up the operator function.
  73. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  74. }
  75. // Form `operand.(Op)`.
  76. auto bound_op_id = PerformCompoundMemberAccess(
  77. context, loc_id, operand_id, op_fn_id, missing_impl_diagnoser);
  78. if (bound_op_id == SemIR::ErrorInst::InstId) {
  79. return SemIR::ErrorInst::InstId;
  80. }
  81. // Form `bound_op()`.
  82. return PerformCall(context, loc_id, bound_op_id, {});
  83. }
  84. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  85. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  86. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  87. -> SemIR::InstId {
  88. if (lhs_id == SemIR::ErrorInst::InstId) {
  89. // Exit early for errors, which prevent forming an `Op` function.
  90. return SemIR::ErrorInst::InstId;
  91. }
  92. // Operator operands don't require `ref` tags.
  93. context.ref_tags().Insert(lhs_id, Context::RefTag::NotRequired);
  94. context.ref_tags().Insert(rhs_id, Context::RefTag::NotRequired);
  95. SemIR::InstId op_fn_id = SemIR::InstId::None;
  96. // For binary operators with a C++ class as at least one of the operands, try
  97. // to import and call the C++ operator.
  98. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  99. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  100. // addable. See
  101. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  102. // and
  103. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  104. if (IsCppClassType(context, lhs_id) || IsCppClassType(context, rhs_id)) {
  105. op_fn_id = LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  106. // If C++ operator lookup found a non-method operator, call it with two call
  107. // arguments. Otherwise fall through to call it with a self argument and one
  108. // call argument.
  109. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  110. return PerformCall(context, loc_id, op_fn_id, {lhs_id, rhs_id});
  111. }
  112. }
  113. if (!op_fn_id.has_value()) {
  114. // Look up the operator function.
  115. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  116. }
  117. // Form `lhs.(Op)`.
  118. auto bound_op_id = PerformCompoundMemberAccess(
  119. context, loc_id, lhs_id, op_fn_id, missing_impl_diagnoser);
  120. if (bound_op_id == SemIR::ErrorInst::InstId) {
  121. return SemIR::ErrorInst::InstId;
  122. }
  123. // Form `bound_op(rhs)`.
  124. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  125. }
  126. } // namespace Carbon::Check