handle_literal.cpp 5.5 KB

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