handle_literal.cpp 8.8 KB

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