handle_literal.cpp 5.7 KB

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