handle_literal.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/handle.h"
  8. #include "toolchain/check/inst.h"
  9. #include "toolchain/check/literal.h"
  10. #include "toolchain/check/name_lookup.h"
  11. #include "toolchain/check/type.h"
  12. #include "toolchain/diagnostics/format_providers.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. auto HandleParseNode(Context& context, Parse::BoolLiteralFalseId node_id)
  17. -> bool {
  18. AddInstAndPush<SemIR::BoolLiteral>(
  19. context, node_id,
  20. {.type_id = GetSingletonType(context, SemIR::BoolType::TypeInstId),
  21. .value = SemIR::BoolValue::False});
  22. return true;
  23. }
  24. auto HandleParseNode(Context& context, Parse::BoolLiteralTrueId node_id)
  25. -> bool {
  26. AddInstAndPush<SemIR::BoolLiteral>(
  27. context, node_id,
  28. {.type_id = GetSingletonType(context, SemIR::BoolType::TypeInstId),
  29. .value = SemIR::BoolValue::True});
  30. return true;
  31. }
  32. auto HandleParseNode(Context& context, Parse::CharLiteralId node_id) -> bool {
  33. auto value = context.tokens().GetCharLiteralValue(
  34. context.parse_tree().node_token(node_id));
  35. auto inst_id = AddInst<SemIR::CharLiteralValue>(
  36. context, node_id,
  37. {.type_id = GetSingletonType(context, SemIR::CharLiteralType::TypeInstId),
  38. .value = SemIR::CharId(value.value)});
  39. context.node_stack().Push(node_id, inst_id);
  40. return true;
  41. }
  42. auto HandleParseNode(Context& context, Parse::IntLiteralId node_id) -> bool {
  43. auto int_literal_id = MakeIntLiteral(
  44. context, node_id,
  45. context.tokens().GetIntLiteral(context.parse_tree().node_token(node_id)));
  46. context.node_stack().Push(node_id, int_literal_id);
  47. return true;
  48. }
  49. auto HandleParseNode(Context& context, Parse::RealLiteralId node_id) -> bool {
  50. // Convert the real literal to an llvm::APFloat and add it to the floats
  51. // ValueStore. In the future this would use an arbitrary precision Rational
  52. // type.
  53. //
  54. // TODO: Implement Carbon's actual implicit conversion rules for
  55. // floating-point constants, as per the design
  56. // docs/design/expressions/implicit_conversions.md
  57. auto real_id =
  58. context.tokens().GetRealLiteral(context.parse_tree().node_token(node_id));
  59. auto real_value = context.sem_ir().reals().Get(real_id);
  60. if (real_value.mantissa.getActiveBits() > 64) {
  61. CARBON_DIAGNOSTIC(RealMantissaTooLargeForI64, Error,
  62. "real mantissa with value {0} does not fit in i64",
  63. llvm::APSInt);
  64. context.emitter().Emit(node_id, RealMantissaTooLargeForI64,
  65. llvm::APSInt(real_value.mantissa, true));
  66. context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
  67. return true;
  68. }
  69. if (real_value.exponent.getSignificantBits() > 64) {
  70. CARBON_DIAGNOSTIC(RealExponentTooLargeForI64, Error,
  71. "real exponent with value {0} does not fit in i64",
  72. llvm::APSInt);
  73. context.emitter().Emit(node_id, RealExponentTooLargeForI64,
  74. llvm::APSInt(real_value.exponent, false));
  75. context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
  76. return true;
  77. }
  78. double double_val = real_value.mantissa.getZExtValue() *
  79. std::pow((real_value.is_decimal ? 10 : 2),
  80. real_value.exponent.getSExtValue());
  81. auto float_id = context.sem_ir().floats().Add(llvm::APFloat(double_val));
  82. AddInstAndPush<SemIR::FloatValue>(
  83. context, node_id,
  84. {.type_id = GetSingletonType(context, SemIR::LegacyFloatType::TypeInstId),
  85. .float_id = float_id});
  86. return true;
  87. }
  88. auto HandleParseNode(Context& context, Parse::StringLiteralId node_id) -> bool {
  89. AddInstAndPush<SemIR::StringLiteral>(
  90. context, node_id,
  91. {.type_id = GetSingletonType(context, SemIR::StringType::TypeInstId),
  92. .string_literal_id = context.tokens().GetStringLiteralValue(
  93. context.parse_tree().node_token(node_id))});
  94. return true;
  95. }
  96. auto HandleParseNode(Context& context, Parse::BoolTypeLiteralId node_id)
  97. -> bool {
  98. auto fn_inst_id = LookupNameInCore(context, node_id, "Bool");
  99. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  100. context.node_stack().Push(node_id, type_inst_id);
  101. return true;
  102. }
  103. // Shared implementation for handling `iN` and `uN` literals.
  104. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  105. Parse::NodeId node_id,
  106. SemIR::IntKind int_kind,
  107. IntId size_id) -> bool {
  108. if (!(context.ints().Get(size_id) & 3).isZero()) {
  109. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  110. "bit width of integer type literal must be a multiple of "
  111. "8; use `Core.{0:Int|UInt}({1})` instead",
  112. Diagnostics::BoolAsSelect, llvm::APSInt);
  113. context.emitter().Emit(
  114. node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
  115. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  116. }
  117. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  118. context.node_stack().Push(node_id, type_inst_id);
  119. return true;
  120. }
  121. auto HandleParseNode(Context& context, Parse::IntTypeLiteralId node_id)
  122. -> bool {
  123. auto tok_id = context.parse_tree().node_token(node_id);
  124. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  125. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  126. SemIR::IntKind::Signed, size_id);
  127. }
  128. auto HandleParseNode(Context& context, Parse::UnsignedIntTypeLiteralId node_id)
  129. -> bool {
  130. auto tok_id = context.parse_tree().node_token(node_id);
  131. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  132. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  133. SemIR::IntKind::Unsigned, size_id);
  134. }
  135. auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
  136. -> bool {
  137. auto text =
  138. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  139. if (text != "f64") {
  140. return context.TODO(node_id, "Currently only f64 is allowed");
  141. }
  142. auto tok_id = context.parse_tree().node_token(node_id);
  143. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  144. auto type_inst_id =
  145. MakeFloatTypeLiteral(context, node_id, SemIR::FloatKind::None, size_id);
  146. context.node_stack().Push(node_id, type_inst_id);
  147. return true;
  148. }
  149. auto HandleParseNode(Context& context, Parse::StringTypeLiteralId node_id)
  150. -> bool {
  151. context.node_stack().Push(node_id, SemIR::StringType::TypeInstId);
  152. return true;
  153. }
  154. auto HandleParseNode(Context& context, Parse::TypeTypeLiteralId node_id)
  155. -> bool {
  156. context.node_stack().Push(node_id, SemIR::TypeType::TypeInstId);
  157. return true;
  158. }
  159. auto HandleParseNode(Context& context, Parse::AutoTypeLiteralId node_id)
  160. -> bool {
  161. return context.TODO(node_id, "HandleAutoTypeLiteral");
  162. }
  163. } // namespace Carbon::Check