operator.cpp 2.6 KB

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