operator.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // For unary operators with a C++ class as the operand, try to import and call
  57. // the C++ operator.
  58. // TODO: Change impl lookup instead. See
  59. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  60. if (IsCppClassType(context, operand_id)) {
  61. SemIR::InstId cpp_inst_id =
  62. LookupCppOperator(context, loc_id, op, {operand_id});
  63. if (cpp_inst_id.has_value()) {
  64. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  65. return SemIR::ErrorInst::InstId;
  66. }
  67. return PerformCall(context, loc_id, cpp_inst_id, {operand_id});
  68. }
  69. }
  70. // Look up the operator function.
  71. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  72. // Form `operand.(Op)`.
  73. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  74. op_fn, missing_impl_diagnoser);
  75. if (bound_op_id == SemIR::ErrorInst::InstId) {
  76. return SemIR::ErrorInst::InstId;
  77. }
  78. // Form `bound_op()`.
  79. return PerformCall(context, loc_id, bound_op_id, {});
  80. }
  81. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  82. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  83. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  84. -> SemIR::InstId {
  85. if (lhs_id == SemIR::ErrorInst::InstId) {
  86. // Exit early for errors, which prevent forming an `Op` function.
  87. return SemIR::ErrorInst::InstId;
  88. }
  89. // For binary operators with a C++ class as at least one of the operands, try
  90. // to import and call the C++ operator.
  91. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  92. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  93. // addable. See
  94. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  95. // and
  96. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  97. if (IsCppClassType(context, lhs_id) || IsCppClassType(context, rhs_id)) {
  98. SemIR::InstId cpp_inst_id =
  99. LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  100. if (cpp_inst_id.has_value()) {
  101. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  102. return SemIR::ErrorInst::InstId;
  103. }
  104. return PerformCall(context, loc_id, cpp_inst_id, {lhs_id, rhs_id});
  105. }
  106. }
  107. // Look up the operator function.
  108. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  109. // Form `lhs.(Op)`.
  110. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  111. missing_impl_diagnoser);
  112. if (bound_op_id == SemIR::ErrorInst::InstId) {
  113. return SemIR::ErrorInst::InstId;
  114. }
  115. // Form `bound_op(rhs)`.
  116. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  117. }
  118. } // namespace Carbon::Check