handle_literal.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. context.AddInstAndPush(
  55. {node_id,
  56. SemIR::RealLiteral{context.GetBuiltinType(SemIR::BuiltinKind::FloatType),
  57. context.tokens().GetRealLiteral(
  58. context.parse_tree().node_token(node_id))}});
  59. return true;
  60. }
  61. auto HandleStringLiteral(Context& context, Parse::StringLiteralId node_id)
  62. -> bool {
  63. context.AddInstAndPush(
  64. {node_id, SemIR::StringLiteral{
  65. context.GetBuiltinType(SemIR::BuiltinKind::StringType),
  66. context.tokens().GetStringLiteralValue(
  67. context.parse_tree().node_token(node_id))}});
  68. return true;
  69. }
  70. auto HandleBoolTypeLiteral(Context& context, Parse::BoolTypeLiteralId node_id)
  71. -> bool {
  72. // TODO: Migrate once functions can be in prelude.carbon.
  73. // auto fn_inst_id = context.LookupNameInCore(node_id, "Bool");
  74. // auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {});
  75. // context.node_stack().Push(node_id, type_inst_id);
  76. context.node_stack().Push(node_id, SemIR::InstId::BuiltinBoolType);
  77. return true;
  78. }
  79. // Shared implementation for handling `iN` and `uN` literals.
  80. static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
  81. Parse::NodeId node_id,
  82. SemIR::IntKind int_kind,
  83. IntId size_id) -> bool {
  84. if (!(context.ints().Get(size_id) & 3).isZero()) {
  85. CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
  86. "Bit width of integer type literal must be a multiple of "
  87. "8. Use `Core.{0}({1})` instead.",
  88. std::string, llvm::APSInt);
  89. context.emitter().Emit(
  90. node_id, IntWidthNotMultipleOf8, int_kind.is_signed() ? "Int" : "UInt",
  91. llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
  92. }
  93. // TODO: Migrate to a call to `Core.Int` or `Core.UInt`.
  94. auto width_id = MakeI32Literal(context, node_id, size_id);
  95. context.AddInstAndPush(
  96. {node_id, SemIR::IntType{.type_id = context.GetBuiltinType(
  97. SemIR::BuiltinKind::TypeType),
  98. .int_kind = int_kind,
  99. .bit_width_id = width_id}});
  100. return true;
  101. }
  102. auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId node_id)
  103. -> bool {
  104. auto tok_id = context.parse_tree().node_token(node_id);
  105. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  106. // Special case: `i32` has a custom builtin for now.
  107. // TODO: Remove this special case.
  108. if (context.ints().Get(size_id) == 32) {
  109. context.node_stack().Push(node_id, SemIR::InstId::BuiltinIntType);
  110. return true;
  111. }
  112. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  113. SemIR::IntKind::Signed, size_id);
  114. }
  115. auto HandleUnsignedIntTypeLiteral(Context& context,
  116. Parse::UnsignedIntTypeLiteralId node_id)
  117. -> bool {
  118. auto tok_id = context.parse_tree().node_token(node_id);
  119. auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
  120. return HandleIntOrUnsignedIntTypeLiteral(context, node_id,
  121. SemIR::IntKind::Unsigned, size_id);
  122. }
  123. auto HandleFloatTypeLiteral(Context& context, Parse::FloatTypeLiteralId node_id)
  124. -> bool {
  125. auto text =
  126. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  127. if (text != "f64") {
  128. return context.TODO(node_id, "Currently only f64 is allowed");
  129. }
  130. // TODO: Migrate once functions can be in prelude.carbon.
  131. // auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
  132. // auto width_inst_id = context.AddInstInNoBlock(
  133. // {node_id,
  134. // SemIR::IntLiteral{
  135. // context.GetBuiltinType(SemIR::BuiltinKind::IntType),
  136. // context.ints().Add(llvm::APInt(/*numBits=*/32, /*val=*/64))}});
  137. // auto type_inst_id =
  138. // PerformCall(context, node_id, fn_inst_id, {width_inst_id});
  139. // context.node_stack().Push(node_id, type_inst_id);
  140. context.node_stack().Push(node_id, SemIR::InstId::BuiltinFloatType);
  141. return true;
  142. }
  143. auto HandleStringTypeLiteral(Context& context,
  144. Parse::StringTypeLiteralId node_id) -> bool {
  145. context.node_stack().Push(node_id, SemIR::InstId::BuiltinStringType);
  146. return true;
  147. }
  148. auto HandleTypeTypeLiteral(Context& context, Parse::TypeTypeLiteralId node_id)
  149. -> bool {
  150. context.node_stack().Push(node_id, SemIR::InstId::BuiltinTypeType);
  151. return true;
  152. }
  153. auto HandleAutoTypeLiteral(Context& context, Parse::AutoTypeLiteralId node_id)
  154. -> bool {
  155. return context.TODO(node_id, "HandleAutoTypeLiteral");
  156. }
  157. } // namespace Carbon::Check