literal.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/literal.h"
  5. #include "toolchain/check/call.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/name_lookup.h"
  9. #include "toolchain/check/type.h"
  10. #include "toolchain/lex/token_info.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. namespace Carbon::Check {
  13. auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
  14. -> SemIR::InstId {
  15. return AddInst<SemIR::IntValue>(
  16. context, node_id,
  17. {.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
  18. .int_id = int_id});
  19. }
  20. auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
  21. SemIR::IntKind int_kind, IntId size_id)
  22. -> SemIR::InstId {
  23. auto width_id = MakeIntLiteral(context, node_id, size_id);
  24. auto fn_inst_id = LookupNameInCore(
  25. context, node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
  26. return PerformCall(context, node_id, fn_inst_id, {width_id});
  27. }
  28. auto MakeIntType(Context& context, Parse::NodeId node_id,
  29. SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId {
  30. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  31. return ExprAsType(context, node_id, type_inst_id).type_id;
  32. }
  33. auto MakeFloatTypeLiteral(Context& context, Parse::NodeId node_id,
  34. SemIR::FloatKind float_kind, IntId size_id)
  35. -> SemIR::InstId {
  36. CARBON_CHECK(float_kind == SemIR::FloatKind::None);
  37. auto width_id = MakeIntLiteral(context, node_id, size_id);
  38. auto fn_inst_id = LookupNameInCore(context, node_id, "Float");
  39. return PerformCall(context, node_id, fn_inst_id, {width_id});
  40. }
  41. } // namespace Carbon::Check