integer_literal.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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_INTEGER_LITERAL_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODES_INTEGER_LITERAL_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 all kinds of literals: `1`, `i32`, etc.
  11. class IntegerLiteral {
  12. public:
  13. static constexpr NodeKind Kind = NodeKind::IntegerLiteral;
  14. explicit IntegerLiteral(ParseTree::Node node, int32_t id,
  15. const llvm::APInt& value)
  16. : node_(node), id_(id), value_(&value) {}
  17. void Print(llvm::raw_ostream& out) const {
  18. out << "IntegerLiteral(%" << id_ << ", " << *value_ << ")";
  19. }
  20. auto node() const -> ParseTree::Node { return node_; }
  21. auto id() const -> int32_t { return id_; }
  22. auto value() const -> const llvm::APInt& { return *value_; }
  23. private:
  24. ParseTree::Node node_;
  25. int32_t id_;
  26. const llvm::APInt* value_;
  27. };
  28. } // namespace Carbon::Semantics
  29. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_INTEGER_LITERAL_H_