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 type_inst_id = MakeBoolTypeLiteral(context, node_id);
  67. context.node_stack().Push(node_id, type_inst_id);
  68. return true;
  69. }
  70. auto HandleParseNode(Context& context, Parse::CharTypeLiteralId node_id)
  71. -> bool {
  72. auto type_inst_id = MakeCharTypeLiteral(context, node_id);
  73. context.node_stack().Push(node_id, type_inst_id);
  74. return true;
  75. }
  76. // Shared implementation for handling `iN` and `uN` literals.
  77. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  78. Parse::NodeId node_id,
  79. SemIR::IntKind int_kind,
  80. IntId size_id) -> bool {
  81. if (!(context.ints().Get(size_id) & 7).isZero()) {
  82. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  83. "bit width of integer type literal must be a multiple of "
  84. "8; use `Core.{0:Int|UInt}({1})` instead",
  85. Diagnostics::BoolAsSelect, llvm::APSInt);
  86. context.emitter().Emit(
  87. node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
  88. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  89. }
  90. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  91. context.node_stack().Push(node_id, type_inst_id);
  92. return true;
  93. }
  94. auto HandleParseNode(Context& context, Parse::IntTypeLiteralId node_id)
  95. -> bool {
  96. auto tok_id = context.parse_tree().node_token(node_id);
  97. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  98. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  99. SemIR::IntKind::Signed, size_id);
  100. }
  101. auto HandleParseNode(Context& context, Parse::UnsignedIntTypeLiteralId node_id)
  102. -> bool {
  103. auto tok_id = context.parse_tree().node_token(node_id);
  104. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  105. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  106. SemIR::IntKind::Unsigned, size_id);
  107. }
  108. auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
  109. -> bool {
  110. auto tok_id = context.parse_tree().node_token(node_id);
  111. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  112. auto type_inst_id = MakeFloatTypeLiteral(context, node_id, size_id);
  113. context.node_stack().Push(node_id, type_inst_id);
  114. return true;
  115. }
  116. auto HandleParseNode(Context& context, Parse::StringTypeLiteralId node_id)
  117. -> bool {
  118. auto type_inst_id = MakeStringTypeLiteral(context, node_id);
  119. context.node_stack().Push(node_id, type_inst_id);
  120. return true;
  121. }
  122. auto HandleParseNode(Context& context, Parse::TypeTypeLiteralId node_id)
  123. -> bool {
  124. auto type_inst_id = MakeTypeTypeLiteral(context, node_id);
  125. context.node_stack().Push(node_id, type_inst_id);
  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