handle_literal.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <cmath>
  5. #include "toolchain/check/call.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/handle.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/check/literal.h"
  11. #include "toolchain/check/name_lookup.h"
  12. #include "toolchain/check/type.h"
  13. #include "toolchain/diagnostics/format_providers.h"
  14. #include "toolchain/sem_ir/ids.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Check {
  17. auto HandleParseNode(Context& context, Parse::BoolLiteralFalseId node_id)
  18. -> bool {
  19. context.node_stack().Push(
  20. node_id, MakeBoolLiteral(context, node_id, SemIR::BoolValue::False));
  21. return true;
  22. }
  23. auto HandleParseNode(Context& context, Parse::BoolLiteralTrueId node_id)
  24. -> bool {
  25. context.node_stack().Push(
  26. node_id, MakeBoolLiteral(context, node_id, SemIR::BoolValue::True));
  27. return true;
  28. }
  29. auto HandleParseNode(Context& context, Parse::CharLiteralId node_id) -> bool {
  30. auto value = context.tokens().GetCharLiteralValue(
  31. context.parse_tree().node_token(node_id));
  32. auto inst_id = AddInst<SemIR::CharLiteralValue>(
  33. context, node_id,
  34. {.type_id = GetSingletonType(context, SemIR::CharLiteralType::TypeInstId),
  35. .value = SemIR::CharId(value.value)});
  36. context.node_stack().Push(node_id, inst_id);
  37. return true;
  38. }
  39. auto HandleParseNode(Context& context, Parse::IntLiteralId node_id) -> bool {
  40. auto int_literal_id = MakeIntLiteral(
  41. context, node_id,
  42. context.tokens().GetIntLiteral(context.parse_tree().node_token(node_id)));
  43. context.node_stack().Push(node_id, int_literal_id);
  44. return true;
  45. }
  46. auto HandleParseNode(Context& context, Parse::RealLiteralId node_id) -> bool {
  47. auto real_id =
  48. context.tokens().GetRealLiteral(context.parse_tree().node_token(node_id));
  49. AddInstAndPush<SemIR::FloatLiteralValue>(
  50. context, node_id,
  51. {.type_id =
  52. GetSingletonType(context, SemIR::FloatLiteralType::TypeInstId),
  53. .real_id = real_id});
  54. return true;
  55. }
  56. auto HandleParseNode(Context& context, Parse::StringLiteralId node_id) -> bool {
  57. auto str_literal_id =
  58. MakeStringLiteral(context, node_id,
  59. context.tokens().GetStringLiteralValue(
  60. context.parse_tree().node_token(node_id)));
  61. context.node_stack().Push(node_id, str_literal_id);
  62. return true;
  63. }
  64. auto HandleParseNode(Context& context, Parse::BoolTypeLiteralId node_id)
  65. -> bool {
  66. auto fn_inst_id = LookupNameInCore(context, node_id, CoreIdentifier::Bool);
  67. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  68. context.node_stack().Push(node_id, type_inst_id);
  69. return true;
  70. }
  71. auto HandleParseNode(Context& context, Parse::CharTypeLiteralId node_id)
  72. -> bool {
  73. auto type_inst_id = MakeCharTypeLiteral(context, node_id);
  74. context.node_stack().Push(node_id, type_inst_id);
  75. return true;
  76. }
  77. // Shared implementation for handling `iN` and `uN` literals.
  78. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  79. Parse::NodeId node_id,
  80. SemIR::IntKind int_kind,
  81. IntId size_id) -> bool {
  82. if (!(context.ints().Get(size_id) & 3).isZero()) {
  83. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  84. "bit width of integer type literal must be a multiple of "
  85. "8; use `Core.{0:Int|UInt}({1})` instead",
  86. Diagnostics::BoolAsSelect, llvm::APSInt);
  87. context.emitter().Emit(
  88. node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
  89. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  90. }
  91. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  92. context.node_stack().Push(node_id, type_inst_id);
  93. return true;
  94. }
  95. auto HandleParseNode(Context& context, Parse::IntTypeLiteralId node_id)
  96. -> bool {
  97. auto tok_id = context.parse_tree().node_token(node_id);
  98. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  99. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  100. SemIR::IntKind::Signed, size_id);
  101. }
  102. auto HandleParseNode(Context& context, Parse::UnsignedIntTypeLiteralId node_id)
  103. -> bool {
  104. auto tok_id = context.parse_tree().node_token(node_id);
  105. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  106. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  107. SemIR::IntKind::Unsigned, size_id);
  108. }
  109. auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
  110. -> bool {
  111. auto tok_id = context.parse_tree().node_token(node_id);
  112. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  113. auto type_inst_id = MakeFloatTypeLiteral(context, node_id, size_id);
  114. context.node_stack().Push(node_id, type_inst_id);
  115. return true;
  116. }
  117. auto HandleParseNode(Context& context, Parse::StringTypeLiteralId node_id)
  118. -> bool {
  119. auto type_inst_id = MakeStringTypeLiteral(context, node_id);
  120. context.node_stack().Push(node_id, type_inst_id);
  121. return true;
  122. }
  123. auto HandleParseNode(Context& context, Parse::TypeTypeLiteralId node_id)
  124. -> bool {
  125. context.node_stack().Push(node_id, SemIR::TypeType::TypeInstId);
  126. return true;
  127. }
  128. auto HandleParseNode(Context& context, Parse::AutoTypeLiteralId node_id)
  129. -> bool {
  130. return context.TODO(node_id, "HandleAutoTypeLiteral");
  131. }
  132. } // namespace Carbon::Check