operator.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. SemIR::InstId op_fn_id = SemIR::InstId::None;
  57. // For unary operators with a C++ class as the operand, try to import and call
  58. // the C++ operator.
  59. // TODO: Change impl lookup instead. See
  60. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  61. if (IsCppClassType(context, operand_id)) {
  62. op_fn_id = LookupCppOperator(context, loc_id, op, {operand_id});
  63. // If C++ operator lookup found a non-method operator, call it with one call
  64. // argument. Otherwise fall through to call it with a self argument.
  65. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  66. return PerformCall(context, loc_id, op_fn_id, {operand_id},
  67. /*is_operator_syntax=*/true);
  68. }
  69. }
  70. if (!op_fn_id.has_value()) {
  71. // Look up the operator function.
  72. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  73. }
  74. // Form `operand.(Op)`.
  75. auto bound_op_id = PerformCompoundMemberAccess(
  76. context, loc_id, operand_id, op_fn_id, missing_impl_diagnoser);
  77. if (bound_op_id == SemIR::ErrorInst::InstId) {
  78. return SemIR::ErrorInst::InstId;
  79. }
  80. // Form `bound_op()`.
  81. return PerformCall(context, loc_id, bound_op_id, {},
  82. /*is_operator_syntax=*/true);
  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. SemIR::InstId op_fn_id = SemIR::InstId::None;
  93. // For binary operators with a C++ class as at least one of the operands, try
  94. // to import and call the C++ operator.
  95. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  96. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  97. // addable. See
  98. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  99. // and
  100. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  101. if (IsCppClassType(context, lhs_id) || IsCppClassType(context, rhs_id)) {
  102. op_fn_id = LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  103. // If C++ operator lookup found a non-method operator, call it with two call
  104. // arguments. Otherwise fall through to call it with a self argument and one
  105. // call argument.
  106. if (op_fn_id.has_value() && !IsCppOperatorMethod(context, op_fn_id)) {
  107. return PerformCall(context, loc_id, op_fn_id, {lhs_id, rhs_id},
  108. /*is_operator_syntax=*/true);
  109. }
  110. }
  111. if (!op_fn_id.has_value()) {
  112. // Look up the operator function.
  113. op_fn_id = GetOperatorOpFunction(context, loc_id, op);
  114. }
  115. // Form `lhs.(Op)`.
  116. auto bound_op_id = PerformCompoundMemberAccess(
  117. context, loc_id, lhs_id, op_fn_id, missing_impl_diagnoser);
  118. if (bound_op_id == SemIR::ErrorInst::InstId) {
  119. return SemIR::ErrorInst::InstId;
  120. }
  121. // Form `bound_op(rhs)`.
  122. return PerformCall(context, loc_id, bound_op_id, {rhs_id},
  123. /*is_operator_syntax=*/true);
  124. }
  125. } // namespace Carbon::Check