operator.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/member_access.h"
  9. #include "toolchain/check/name_lookup.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/typed_insts.h"
  12. namespace Carbon::Check {
  13. // Returns the `Op` function for the specified operator.
  14. static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
  15. Operator op) -> SemIR::InstId {
  16. auto implicit_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
  17. // Look up the interface, and pass it any generic arguments.
  18. auto interface_id =
  19. LookupNameInCore(context, implicit_loc_id, op.interface_name);
  20. if (!op.interface_args_ref.empty()) {
  21. interface_id = PerformCall(context, implicit_loc_id, interface_id,
  22. op.interface_args_ref);
  23. }
  24. // Look up the interface member.
  25. auto op_name_id =
  26. SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
  27. return PerformMemberAccess(context, implicit_loc_id, interface_id,
  28. op_name_id);
  29. }
  30. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  31. SemIR::InstId operand_id,
  32. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  33. -> SemIR::InstId {
  34. // Look up the operator function.
  35. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  36. // Form `operand.(Op)`.
  37. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  38. op_fn, missing_impl_diagnoser);
  39. if (bound_op_id == SemIR::ErrorInst::InstId) {
  40. return SemIR::ErrorInst::InstId;
  41. }
  42. // Form `bound_op()`.
  43. return PerformCall(context, loc_id, bound_op_id, {});
  44. }
  45. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  46. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  47. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  48. -> SemIR::InstId {
  49. // Look up the operator function.
  50. auto op_fn = GetOperatorOpFunction(context, loc_id, op);
  51. // Form `lhs.(Op)`.
  52. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  53. missing_impl_diagnoser);
  54. if (bound_op_id == SemIR::ErrorInst::InstId) {
  55. return SemIR::ErrorInst::InstId;
  56. }
  57. // Form `bound_op(rhs)`.
  58. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  59. }
  60. } // namespace Carbon::Check