literal.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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. namespace Carbon::Check {
  9. auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
  10. -> SemIR::InstId {
  11. return context.AddInst<SemIR::IntValue>(
  12. node_id, {.type_id = context.GetSingletonType(
  13. SemIR::IntLiteralType::SingletonInstId),
  14. .int_id = int_id});
  15. }
  16. auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
  17. SemIR::IntKind int_kind, IntId size_id)
  18. -> SemIR::InstId {
  19. auto width_id = MakeIntLiteral(context, node_id, size_id);
  20. auto fn_inst_id = context.LookupNameInCore(
  21. node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
  22. return PerformCall(context, node_id, fn_inst_id, {width_id});
  23. }
  24. auto MakeIntType(Context& context, Parse::NodeId node_id,
  25. SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId {
  26. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  27. return ExprAsType(context, node_id, type_inst_id).type_id;
  28. }
  29. } // namespace Carbon::Check