operator.cpp 4.7 KB

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