binary_operator.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #ifndef CARBON_TOOLCHAIN_SEMANTICS_NODES_BINARY_OPERATOR_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODES_BINARY_OPERATOR_H_
  6. #include "common/ostream.h"
  7. #include "toolchain/parser/parse_tree.h"
  8. #include "toolchain/semantics/node_kind.h"
  9. namespace Carbon::Semantics {
  10. // Represents a binary operator, such as `+` in `1 + 2`.
  11. class BinaryOperator {
  12. public:
  13. enum class Op {
  14. Add,
  15. };
  16. static constexpr NodeKind Kind = NodeKind::BinaryOperator;
  17. explicit BinaryOperator(ParseTree::Node node, NodeId id, Op op, NodeId lhs_id,
  18. NodeId rhs_id)
  19. : node_(node), id_(id), op_(op), lhs_id_(lhs_id), rhs_id_(rhs_id) {}
  20. void Print(llvm::raw_ostream& out) const {
  21. out << "BinaryOperator(" << id_ << ", ";
  22. switch (op_) {
  23. case Op::Add:
  24. out << "+";
  25. break;
  26. }
  27. out << ", " << lhs_id_ << ", " << rhs_id_ << ")";
  28. }
  29. auto node() const -> ParseTree::Node { return node_; }
  30. auto id() const -> NodeId { return id_; }
  31. auto op() const -> Op { return op_; }
  32. auto lhs_id() const -> NodeId { return lhs_id_; }
  33. auto rhs_id() const -> NodeId { return rhs_id_; }
  34. private:
  35. ParseTree::Node node_;
  36. NodeId id_;
  37. Op op_;
  38. NodeId lhs_id_;
  39. NodeId rhs_id_;
  40. };
  41. } // namespace Carbon::Semantics
  42. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_BINARY_OPERATOR_H_