handle_literal.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // TODO: Migrate once functions can be in prelude.carbon.
  104. // auto fn_inst_id = context.LookupNameInCore(node_id, "Bool");
  105. // auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  106. // context.node_stack().Push(node_id, type_inst_id);
  107. context.node_stack().Push(node_id, SemIR::InstId::BuiltinBoolType);
  108. return true;
  109. }
  110. // Shared implementation for handling `iN` and `uN` literals.
  111. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  112. Parse::NodeId node_id,
  113. SemIR::IntKind int_kind,
  114. IntId size_id) -> bool {
  115. if (!(context.ints().Get(size_id) & 3).isZero()) {
  116. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  117. "Bit width of integer type literal must be a multiple of "
  118. "8. Use `Core.{0}({1})` instead.",
  119. std::string, llvm::APSInt);
  120. context.emitter().Emit(
  121. node_id, IntWidthNotMultipleOf8, int_kind.is_signed() ? "Int" : "UInt",
  122. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  123. }
  124. // TODO: Migrate to a call to `Core.Int` or `Core.UInt`.
  125. auto width_id = MakeI32Literal(context, node_id, size_id);
  126. context.AddInstAndPush(
  127. {node_id, SemIR::IntType{.type_id = context.GetBuiltinType(
  128. SemIR::BuiltinKind::TypeType),
  129. .int_kind = int_kind,
  130. .bit_width_id = width_id}});
  131. return true;
  132. }
  133. auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId node_id)
  134. -> bool {
  135. auto tok_id = context.parse_tree().node_token(node_id);
  136. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  137. // Special case: `i32` has a custom builtin for now.
  138. // TODO: Remove this special case.
  139. if (context.ints().Get(size_id) == 32) {
  140. context.node_stack().Push(node_id, SemIR::InstId::BuiltinIntType);
  141. return true;
  142. }
  143. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  144. SemIR::IntKind::Signed, size_id);
  145. }
  146. auto HandleUnsignedIntTypeLiteral(Context& context,
  147. Parse::UnsignedIntTypeLiteralId node_id)
  148. -> bool {
  149. auto tok_id = context.parse_tree().node_token(node_id);
  150. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  151. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  152. SemIR::IntKind::Unsigned, size_id);
  153. }
  154. auto HandleFloatTypeLiteral(Context& context, Parse::FloatTypeLiteralId node_id)
  155. -> bool {
  156. auto text =
  157. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  158. if (text != "f64") {
  159. return context.TODO(node_id, "Currently only f64 is allowed");
  160. }
  161. // TODO: Migrate once functions can be in prelude.carbon.
  162. // auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
  163. // auto width_inst_id = context.AddInstInNoBlock(
  164. // {node_id,
  165. // SemIR::IntLiteral{
  166. // context.GetBuiltinType(SemIR::BuiltinKind::IntType),
  167. // context.ints().Add(llvm::APInt(/*numBits=*/32, /*val=*/64))}});
  168. // auto type_inst_id =
  169. // PerformCall(context, node_id, fn_inst_id, {width_inst_id});
  170. // context.node_stack().Push(node_id, type_inst_id);
  171. context.node_stack().Push(node_id, SemIR::InstId::BuiltinFloatType);
  172. return true;
  173. }
  174. auto HandleStringTypeLiteral(Context& context,
  175. Parse::StringTypeLiteralId node_id) -> bool {
  176. context.node_stack().Push(node_id, SemIR::InstId::BuiltinStringType);
  177. return true;
  178. }
  179. auto HandleTypeTypeLiteral(Context& context, Parse::TypeTypeLiteralId node_id)
  180. -> bool {
  181. context.node_stack().Push(node_id, SemIR::InstId::BuiltinTypeType);
  182. return true;
  183. }
  184. auto HandleAutoTypeLiteral(Context& context, Parse::AutoTypeLiteralId node_id)
  185. -> bool {
  186. return context.TODO(node_id, "HandleAutoTypeLiteral");
  187. }
  188. } // namespace Carbon::Check