operator.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Look up the interface, and pass it any generic arguments.
  17. auto interface_id = LookupNameInCore(context, loc_id, op.interface_name);
  18. if (!op.interface_args_ref.empty()) {
  19. interface_id =
  20. PerformCall(context, loc_id, interface_id, op.interface_args_ref);
  21. }
  22. // Look up the interface member.
  23. auto op_name_id =
  24. SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
  25. return PerformMemberAccess(context, loc_id, interface_id, op_name_id);
  26. }
  27. auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  28. SemIR::InstId operand_id,
  29. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  30. -> SemIR::InstId {
  31. // Look up the operator function.
  32. auto op_fn = GetOperatorOpFunction(context, loc_id.ToImplicit(), op);
  33. // Form `operand.(Op)`.
  34. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
  35. op_fn, missing_impl_diagnoser);
  36. if (bound_op_id == SemIR::ErrorInst::InstId) {
  37. return SemIR::ErrorInst::InstId;
  38. }
  39. // Form `bound_op()`.
  40. return PerformCall(context, loc_id, bound_op_id, {});
  41. }
  42. auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
  43. SemIR::InstId lhs_id, SemIR::InstId rhs_id,
  44. MakeDiagnosticBuilderFn missing_impl_diagnoser)
  45. -> SemIR::InstId {
  46. // Look up the operator function.
  47. auto op_fn = GetOperatorOpFunction(context, loc_id.ToImplicit(), op);
  48. // Form `lhs.(Op)`.
  49. auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
  50. missing_impl_diagnoser);
  51. if (bound_op_id == SemIR::ErrorInst::InstId) {
  52. return SemIR::ErrorInst::InstId;
  53. }
  54. // Form `bound_op(rhs)`.
  55. return PerformCall(context, loc_id, bound_op_id, {rhs_id});
  56. }
  57. } // namespace Carbon::Check