handle_literal.cpp 8.2 KB

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