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