handle_literal.cpp 6.7 KB

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