handle_literal.cpp 6.7 KB

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