operator.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. auto interface_id =
  23. LookupNameInCore(context, implicit_loc_id, op.interface_name);
  24. if (!op.interface_args_ref.empty()) {
  25. interface_id = PerformCall(context, implicit_loc_id, interface_id,
  26. op.interface_args_ref);
  27. }
  28. // Look up the interface member.
  29. auto op_name_id =
  30. SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
  31. return PerformMemberAccess(context, implicit_loc_id, interface_id,
  32. op_name_id);
  33. }
  34. // Returns whether the instruction is a C++ class.
  35. static auto IsCppClassType(Context& context, SemIR::InstId inst_id) -> bool {
  36. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(
  37. context.types().GetInstId(context.insts().Get(inst_id).type_id()));
  38. if (!class_type) {
  39. // Not a class.
  40. return false;
  41. }
  42. SemIR::NameScopeId class_scope_id =
  43. context.classes().Get(class_type->class_id).scope_id;
  44. return class_scope_id.has_value() &&
  45. context.name_scopes().Get(class_scope_id).is_cpp_scope();
  46. }
  47. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  48. SemIR::InstId operand_id,
  49. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  50. -> SemIR::InstId {
  51. // For unary operators with a C++ class as the operand, try to import and call
  52. // the C++ operator.
  53. // TODO: Change impl lookup instead. See
  54. // https://github.com/carbon-language/carbon-lang/blob/db0a00d713015436844c55e7ac190a0f95556499/toolchain/check/operator.cpp#L76
  55. if (IsCppClassType(context, operand_id)) {
  56. SemIR::InstId cpp_inst_id =
  57. LookupCppOperator(context, loc_id, op, {operand_id});
  58. if (cpp_inst_id.has_value()) {
  59. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  60. return SemIR::ErrorInst::InstId;
  61. }
  62. return PerformCall(context, loc_id, cpp_inst_id, {operand_id});
  63. }
  64. }
  65. // Look up the operator function.
  66. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  67. // Form `operand.(Op)`.
  68. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  69. op_fn, missing_impl_diagnoser);
  70. if (bound_op_id == SemIR::ErrorInst::InstId) {
  71. return SemIR::ErrorInst::InstId;
  72. }
  73. // Form `bound_op()`.
  74. return PerformCall(context, loc_id, bound_op_id, {});
  75. }
  76. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  77. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  78. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  79. -> SemIR::InstId {
  80. // For binary operators with a C++ class as at least one of the operands, try
  81. // to import and call the C++ operator.
  82. // TODO: Instead of hooking this here, change impl lookup, so that a generic
  83. // constraint such as `T:! Core.Add` is satisfied by C++ class types that are
  84. // addable. See
  85. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
  86. // and
  87. // https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
  88. if (IsCppClassType(context, lhs_id) || IsCppClassType(context, rhs_id)) {
  89. SemIR::InstId cpp_inst_id =
  90. LookupCppOperator(context, loc_id, op, {lhs_id, rhs_id});
  91. if (cpp_inst_id.has_value()) {
  92. if (cpp_inst_id == SemIR::ErrorInst::InstId) {
  93. return SemIR::ErrorInst::InstId;
  94. }
  95. return PerformCall(context, loc_id, cpp_inst_id, {lhs_id, rhs_id});
  96. }
  97. }
  98. // Look up the operator function.
  99. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  100. // Form `lhs.(Op)`.
  101. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  102. missing_impl_diagnoser);
  103. if (bound_op_id == SemIR::ErrorInst::InstId) {
  104. return SemIR::ErrorInst::InstId;
  105. }
  106. // Form `bound_op(rhs)`.
  107. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  108. }
  109. } // namespace Carbon::Check