operator.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/operators.h"
  9. #include "toolchain/check/generic.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. // TODO: Improve diagnostics when the found `interface_id` isn't callable.
  23. auto interface_id =
  24. LookupNameInCore(context, implicit_loc_id, op.interface_name);
  25. if (!op.interface_args_ref.empty()) {
  26. interface_id = PerformCall(context, implicit_loc_id, interface_id,
  27. op.interface_args_ref);
  28. }
  29. // Look up the interface member.
  30. auto op_name_id =
  31. SemIR::NameId::ForIdentifier(context.identifiers().Add(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. // For unary operators with a C++ class as the operand, try to import and call
  59. // the C++ operator.
  60. // TODO: Change impl lookup instead. See
  61. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  62. if (IsCppClassType(context, operand_id)) {
  63. SemIR::InstId cpp_inst_id =
  64. LookupCppOperator(context, loc_id, op, {operand_id});
  65. if (cpp_inst_id.has_value()) {
  66. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  67. return SemIR::ErrorInst::InstId;
  68. }
  69. return PerformCall(context, loc_id, cpp_inst_id, {operand_id});
  70. }
  71. }
  72. // Look up the operator function.
  73. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  74. // Form `operand.(Op)`.
  75. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  76. op_fn, 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. }
  83. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  84. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  85. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  86. -> SemIR::InstId {
  87. if (lhs_id == SemIR::ErrorInst::InstId) {
  88. // Exit early for errors, which prevent forming an `Op` function.
  89. return SemIR::ErrorInst::InstId;
  90. }
  91. // Operator operands don't require `ref` tags.
  92. context.ref_tags().Insert(lhs_id, Context::RefTag::NotRequired);
  93. context.ref_tags().Insert(rhs_id, Context::RefTag::NotRequired);
  94. // For binary operators with a C++ class as at least one of the operands, try
  95. // to import and call the C++ operator.
  96. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  97. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  98. // addable. See
  99. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  100. // and
  101. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  102. if (IsCppClassType(context, lhs_id) || IsCppClassType(context, rhs_id)) {
  103. SemIR::InstId cpp_inst_id =
  104. LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  105. if (cpp_inst_id.has_value()) {
  106. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  107. return SemIR::ErrorInst::InstId;
  108. }
  109. return PerformCall(context, loc_id, cpp_inst_id, {lhs_id, rhs_id});
  110. }
  111. }
  112. // Look up the operator function.
  113. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  114. // Form `lhs.(Op)`.
  115. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  116. missing_impl_diagnoser);
  117. if (bound_op_id == SemIR::ErrorInst::InstId) {
  118. return SemIR::ErrorInst::InstId;
  119. }
  120. // Form `bound_op(rhs)`.
  121. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  122. }
  123. } // namespace Carbon::Check