handle_literal.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/sem_ir/typed_insts.h"
  8. namespace Carbon::Check {
  9. auto HandleBoolLiteralFalse(Context& context, Parse::BoolLiteralFalseId node_id)
  10. -> bool {
  11. context.AddInstAndPush<SemIR::BoolLiteral>(
  12. node_id, {.type_id = context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  13. .value = SemIR::BoolValue::False});
  14. return true;
  15. }
  16. auto HandleBoolLiteralTrue(Context& context, Parse::BoolLiteralTrueId node_id)
  17. -> bool {
  18. context.AddInstAndPush<SemIR::BoolLiteral>(
  19. node_id, {.type_id = context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  20. .value = SemIR::BoolValue::True});
  21. return true;
  22. }
  23. // Forms an IntLiteral instruction with type `i32` for a given literal integer
  24. // value, which is assumed to be unsigned.
  25. static auto MakeI32Literal(Context& context, Parse::NodeId node_id,
  26. IntId int_id) -> SemIR::InstId {
  27. auto val = context.ints().Get(int_id);
  28. if (val.getActiveBits() > 31) {
  29. CARBON_DIAGNOSTIC(IntLiteralTooLargeForI32, Error,
  30. "Integer literal with value {0} does not fit in i32.",
  31. llvm::APSInt);
  32. context.emitter().Emit(node_id, IntLiteralTooLargeForI32,
  33. llvm::APSInt(val, /*isUnsigned=*/true));
  34. return SemIR::InstId::BuiltinError;
  35. }
  36. // Literals are always represented as unsigned, so zero-extend if needed.
  37. auto i32_val = val.zextOrTrunc(32);
  38. return context.AddInst<SemIR::IntLiteral>(
  39. node_id, {.type_id = context.GetBuiltinType(SemIR::BuiltinKind::IntType),
  40. .int_id = context.ints().Add(i32_val)});
  41. }
  42. auto HandleIntLiteral(Context& context, Parse::IntLiteralId node_id) -> bool {
  43. // Convert the literal to i32.
  44. // TODO: Form an integer literal value and a corresponding type here instead.
  45. auto int_literal_id = MakeI32Literal(
  46. context, node_id,
  47. context.tokens().GetIntLiteral(context.parse_tree().node_token(node_id)));
  48. context.node_stack().Push(node_id, int_literal_id);
  49. return true;
  50. }
  51. auto HandleRealLiteral(Context& context, Parse::RealLiteralId node_id) -> bool {
  52. // Convert the real literal to an llvm::APFloat and add it to the floats
  53. // ValueStore. In the future this would use an arbitrary precision Rational
  54. // type.
  55. //
  56. // TODO: Implement Carbon's actual implicit conversion rules for
  57. // floating-point constants, as per the design
  58. // docs/design/expressions/implicit_conversions.md
  59. auto real_id =
  60. context.tokens().GetRealLiteral(context.parse_tree().node_token(node_id));
  61. auto real_value = context.sem_ir().reals().Get(real_id);
  62. if (real_value.mantissa.getActiveBits() > 64) {
  63. CARBON_DIAGNOSTIC(RealMantissaTooLargeForI64, Error,
  64. "Real mantissa with value {0} does not fit in i64.",
  65. llvm::APSInt);
  66. context.emitter().Emit(node_id, RealMantissaTooLargeForI64,
  67. llvm::APSInt(real_value.mantissa, true));
  68. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  69. return true;
  70. }
  71. if (real_value.exponent.getSignificantBits() > 64) {
  72. CARBON_DIAGNOSTIC(RealExponentTooLargeForI64, Error,
  73. "Real exponent with value {0} does not fit in i64.",
  74. llvm::APSInt);
  75. context.emitter().Emit(node_id, RealExponentTooLargeForI64,
  76. llvm::APSInt(real_value.exponent, false));
  77. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  78. return true;
  79. }
  80. double double_val = real_value.mantissa.getZExtValue() *
  81. std::pow((real_value.is_decimal ? 10 : 2),
  82. real_value.exponent.getSExtValue());
  83. auto float_id = context.sem_ir().floats().Add(llvm::APFloat(double_val));
  84. context.AddInstAndPush<SemIR::FloatLiteral>(
  85. node_id,
  86. {.type_id = context.GetBuiltinType(SemIR::BuiltinKind::FloatType),
  87. .float_id = float_id});
  88. return true;
  89. }
  90. auto HandleStringLiteral(Context& context, Parse::StringLiteralId node_id)
  91. -> bool {
  92. context.AddInstAndPush<SemIR::StringLiteral>(
  93. node_id,
  94. {.type_id = context.GetBuiltinType(SemIR::BuiltinKind::StringType),
  95. .string_literal_id = context.tokens().GetStringLiteralValue(
  96. context.parse_tree().node_token(node_id))});
  97. return true;
  98. }
  99. auto HandleBoolTypeLiteral(Context& context, Parse::BoolTypeLiteralId node_id)
  100. -> bool {
  101. auto fn_inst_id = context.LookupNameInCore(node_id, "Bool");
  102. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  103. context.node_stack().Push(node_id, type_inst_id);
  104. return true;
  105. }
  106. // Shared implementation for handling `iN` and `uN` literals.
  107. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  108. Parse::NodeId node_id,
  109. SemIR::IntKind int_kind,
  110. IntId size_id) -> bool {
  111. if (!(context.ints().Get(size_id) & 3).isZero()) {
  112. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  113. "Bit width of integer type literal must be a multiple of "
  114. "8. Use `Core.{0}({1})` instead.",
  115. std::string, llvm::APSInt);
  116. context.emitter().Emit(
  117. node_id, IntWidthNotMultipleOf8, int_kind.is_signed() ? "Int" : "UInt",
  118. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  119. }
  120. auto width_id = MakeI32Literal(context, node_id, size_id);
  121. auto fn_inst_id = context.LookupNameInCore(
  122. node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
  123. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
  124. context.node_stack().Push(node_id, type_inst_id);
  125. return true;
  126. }
  127. auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId node_id)
  128. -> bool {
  129. auto tok_id = context.parse_tree().node_token(node_id);
  130. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  131. // Special case: `i32` has a custom builtin for now.
  132. // TODO: Remove this special case.
  133. if (context.ints().Get(size_id) == 32) {
  134. auto fn_inst_id = context.LookupNameInCore(node_id, "Int32");
  135. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  136. context.node_stack().Push(node_id, type_inst_id);
  137. return true;
  138. }
  139. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  140. SemIR::IntKind::Signed, size_id);
  141. }
  142. auto HandleUnsignedIntTypeLiteral(Context& context,
  143. Parse::UnsignedIntTypeLiteralId node_id)
  144. -> bool {
  145. auto tok_id = context.parse_tree().node_token(node_id);
  146. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  147. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  148. SemIR::IntKind::Unsigned, size_id);
  149. }
  150. auto HandleFloatTypeLiteral(Context& context, Parse::FloatTypeLiteralId node_id)
  151. -> bool {
  152. auto text =
  153. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  154. if (text != "f64") {
  155. return context.TODO(node_id, "Currently only f64 is allowed");
  156. }
  157. auto tok_id = context.parse_tree().node_token(node_id);
  158. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  159. auto width_id = MakeI32Literal(context, node_id, size_id);
  160. auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
  161. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
  162. context.node_stack().Push(node_id, type_inst_id);
  163. return true;
  164. }
  165. auto HandleStringTypeLiteral(Context& context,
  166. Parse::StringTypeLiteralId node_id) -> bool {
  167. context.node_stack().Push(node_id, SemIR::InstId::BuiltinStringType);
  168. return true;
  169. }
  170. auto HandleTypeTypeLiteral(Context& context, Parse::TypeTypeLiteralId node_id)
  171. -> bool {
  172. context.node_stack().Push(node_id, SemIR::InstId::BuiltinTypeType);
  173. return true;
  174. }
  175. auto HandleAutoTypeLiteral(Context& context, Parse::AutoTypeLiteralId node_id)
  176. -> bool {
  177. return context.TODO(node_id, "HandleAutoTypeLiteral");
  178. }
  179. } // namespace Carbon::Check