operator.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 type. Assumes the argument is
  36. // in canonical form and does not look through the constant value.
  37. static auto IsCppClassType(Context& context, SemIR::InstId inst_id) -> bool {
  38. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(inst_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. // Returns whether the instruction is a value of C++ class type.
  49. static auto HasCppClassType(Context& context, SemIR::InstId inst_id) -> bool {
  50. return IsCppClassType(context, context.types().GetTypeInstId(
  51. context.insts().Get(inst_id).type_id()));
  52. }
  53. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  54. SemIR::InstId operand_id, bool diagnose,
  55. DiagnosticContextFn missing_impl_diagnostic_context)
  56. -> SemIR::InstId {
  57. if (operand_id == SemIR::ErrorInst::InstId) {
  58. // Exit early for errors, which prevent forming an `Op` function.
  59. return SemIR::ErrorInst::InstId;
  60. }
  61. SemIR::InstId op_fn_id = SemIR::InstId::None;
  62. // For unary operators with a C++ class as the operand, try to import and call
  63. // the C++ operator.
  64. // TODO: Change impl lookup instead. See
  65. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  66. if (HasCppClassType(context, operand_id) ||
  67. llvm::any_of(op.interface_args_ref, [&](SemIR::InstId arg_id) {
  68. return IsCppClassType(context, arg_id);
  69. })) {
  70. op_fn_id = LookupCppOperator(context, loc_id, op, {operand_id});
  71. // If C++ operator lookup found a non-method operator, call it with one call
  72. // argument. Otherwise fall through to call it with a self argument.
  73. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  74. return PerformCall(context, loc_id, op_fn_id, {operand_id},
  75. /*is_operator_syntax=*/true);
  76. }
  77. }
  78. if (!op_fn_id.has_value()) {
  79. // Look up the operator function.
  80. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  81. }
  82. // Form `operand.(Op)`.
  83. auto bound_op_id =
  84. PerformCompoundMemberAccess(context, loc_id, operand_id, op_fn_id,
  85. diagnose, missing_impl_diagnostic_context);
  86. if (bound_op_id == SemIR::ErrorInst::InstId) {
  87. return SemIR::ErrorInst::InstId;
  88. }
  89. // Form `bound_op()`.
  90. return PerformCall(context, loc_id, bound_op_id, {},
  91. /*is_operator_syntax=*/true);
  92. }
  93. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  94. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  95. bool diagnose,
  96. DiagnosticContextFn missing_impl_diagnostic_context)
  97. -> SemIR::InstId {
  98. if (lhs_id == SemIR::ErrorInst::InstId) {
  99. // Exit early for errors, which prevent forming an `Op` function.
  100. return SemIR::ErrorInst::InstId;
  101. }
  102. SemIR::InstId op_fn_id = SemIR::InstId::None;
  103. // For binary operators with a C++ class as at least one of the operands, try
  104. // to import and call the C++ operator.
  105. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  106. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  107. // addable. See
  108. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  109. // and
  110. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  111. if (HasCppClassType(context, lhs_id) || HasCppClassType(context, rhs_id) ||
  112. llvm::any_of(op.interface_args_ref, [&](SemIR::InstId arg_id) {
  113. return IsCppClassType(context, arg_id);
  114. })) {
  115. op_fn_id = LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  116. // If C++ operator lookup found a non-method operator, call it with two call
  117. // arguments. Otherwise fall through to call it with a self argument and one
  118. // call argument.
  119. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  120. return PerformCall(context, loc_id, op_fn_id, {lhs_id, rhs_id},
  121. /*is_operator_syntax=*/true);
  122. }
  123. }
  124. if (!op_fn_id.has_value()) {
  125. // Look up the operator function.
  126. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  127. }
  128. // Form `lhs.(Op)`.
  129. auto bound_op_id =
  130. PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn_id, diagnose,
  131. missing_impl_diagnostic_context);
  132. if (bound_op_id == SemIR::ErrorInst::InstId) {
  133. return SemIR::ErrorInst::InstId;
  134. }
  135. // Form `bound_op(rhs)`.
  136. return PerformCall(context, loc_id, bound_op_id, {rhs_id},
  137. /*is_operator_syntax=*/true);
  138. }
  139. } // namespace Carbon::Check