handle_literal.cpp 6.7 KB

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