handle_literal.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 HandleParseNode(Context& context, Parse::BoolLiteralFalseId node_id)
  10. -> bool {
  11. context.AddInstAndPush<SemIR::BoolLiteral>(
  12. node_id,
  13. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::BoolType),
  14. .value = SemIR::BoolValue::False});
  15. return true;
  16. }
  17. auto HandleParseNode(Context& context, Parse::BoolLiteralTrueId node_id)
  18. -> bool {
  19. context.AddInstAndPush<SemIR::BoolLiteral>(
  20. node_id,
  21. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::BoolType),
  22. .value = SemIR::BoolValue::True});
  23. return true;
  24. }
  25. // Forms an IntLiteral instruction with type `i32` for a given literal integer
  26. // value, which is assumed to be unsigned.
  27. static auto MakeI32Literal(Context& context, Parse::NodeId node_id,
  28. IntId int_id) -> SemIR::InstId {
  29. auto val = context.ints().Get(int_id);
  30. if (val.getActiveBits() > 31) {
  31. CARBON_DIAGNOSTIC(IntLiteralTooLargeForI32, Error,
  32. "integer literal with value {0} does not fit in i32",
  33. llvm::APSInt);
  34. context.emitter().Emit(node_id, IntLiteralTooLargeForI32,
  35. llvm::APSInt(val, /*isUnsigned=*/true));
  36. return SemIR::InstId::BuiltinError;
  37. }
  38. // Literals are always represented as unsigned, so zero-extend if needed.
  39. auto i32_val = val.zextOrTrunc(32);
  40. return context.AddInst<SemIR::IntLiteral>(
  41. node_id,
  42. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::IntType),
  43. .int_id = context.ints().Add(i32_val)});
  44. }
  45. auto HandleParseNode(Context& context, Parse::IntLiteralId node_id) -> bool {
  46. // Convert the literal to i32.
  47. // TODO: Form an integer literal value and a corresponding type here instead.
  48. auto int_literal_id = MakeI32Literal(
  49. context, node_id,
  50. context.tokens().GetIntLiteral(context.parse_tree().node_token(node_id)));
  51. context.node_stack().Push(node_id, int_literal_id);
  52. return true;
  53. }
  54. auto HandleParseNode(Context& context, Parse::RealLiteralId node_id) -> bool {
  55. // Convert the real literal to an llvm::APFloat and add it to the floats
  56. // ValueStore. In the future this would use an arbitrary precision Rational
  57. // type.
  58. //
  59. // TODO: Implement Carbon's actual implicit conversion rules for
  60. // floating-point constants, as per the design
  61. // docs/design/expressions/implicit_conversions.md
  62. auto real_id =
  63. context.tokens().GetRealLiteral(context.parse_tree().node_token(node_id));
  64. auto real_value = context.sem_ir().reals().Get(real_id);
  65. if (real_value.mantissa.getActiveBits() > 64) {
  66. CARBON_DIAGNOSTIC(RealMantissaTooLargeForI64, Error,
  67. "real mantissa with value {0} does not fit in i64",
  68. llvm::APSInt);
  69. context.emitter().Emit(node_id, RealMantissaTooLargeForI64,
  70. llvm::APSInt(real_value.mantissa, true));
  71. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  72. return true;
  73. }
  74. if (real_value.exponent.getSignificantBits() > 64) {
  75. CARBON_DIAGNOSTIC(RealExponentTooLargeForI64, Error,
  76. "real exponent with value {0} does not fit in i64",
  77. llvm::APSInt);
  78. context.emitter().Emit(node_id, RealExponentTooLargeForI64,
  79. llvm::APSInt(real_value.exponent, false));
  80. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  81. return true;
  82. }
  83. double double_val = real_value.mantissa.getZExtValue() *
  84. std::pow((real_value.is_decimal ? 10 : 2),
  85. real_value.exponent.getSExtValue());
  86. auto float_id = context.sem_ir().floats().Add(llvm::APFloat(double_val));
  87. context.AddInstAndPush<SemIR::FloatLiteral>(
  88. node_id,
  89. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::FloatType),
  90. .float_id = float_id});
  91. return true;
  92. }
  93. auto HandleParseNode(Context& context, Parse::StringLiteralId node_id) -> bool {
  94. context.AddInstAndPush<SemIR::StringLiteral>(
  95. node_id,
  96. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::StringType),
  97. .string_literal_id = context.tokens().GetStringLiteralValue(
  98. context.parse_tree().node_token(node_id))});
  99. return true;
  100. }
  101. auto HandleParseNode(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 HandleParseNode(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 HandleParseNode(Context& context, Parse::UnsignedIntTypeLiteralId node_id)
  145. -> bool {
  146. auto tok_id = context.parse_tree().node_token(node_id);
  147. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  148. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  149. SemIR::IntKind::Unsigned, size_id);
  150. }
  151. auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
  152. -> bool {
  153. auto text =
  154. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  155. if (text != "f64") {
  156. return context.TODO(node_id, "Currently only f64 is allowed");
  157. }
  158. auto tok_id = context.parse_tree().node_token(node_id);
  159. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  160. auto width_id = MakeI32Literal(context, node_id, size_id);
  161. auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
  162. auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
  163. context.node_stack().Push(node_id, type_inst_id);
  164. return true;
  165. }
  166. auto HandleParseNode(Context& context, Parse::StringTypeLiteralId node_id)
  167. -> bool {
  168. context.node_stack().Push(node_id, SemIR::InstId::BuiltinStringType);
  169. return true;
  170. }
  171. auto HandleParseNode(Context& context, Parse::TypeTypeLiteralId node_id)
  172. -> bool {
  173. context.node_stack().Push(node_id, SemIR::InstId::BuiltinTypeType);
  174. return true;
  175. }
  176. auto HandleParseNode(Context& context, Parse::AutoTypeLiteralId node_id)
  177. -> bool {
  178. return context.TODO(node_id, "HandleAutoTypeLiteral");
  179. }
  180. } // namespace Carbon::Check