handle_literal.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/context.h"
  5. namespace Carbon::Check {
  6. auto HandleBoolLiteralFalse(Context& context, Parse::BoolLiteralFalseId node_id)
  7. -> bool {
  8. context.AddInstAndPush(
  9. {node_id,
  10. SemIR::BoolLiteral{context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  11. SemIR::BoolValue::False}});
  12. return true;
  13. }
  14. auto HandleBoolLiteralTrue(Context& context, Parse::BoolLiteralTrueId node_id)
  15. -> bool {
  16. context.AddInstAndPush(
  17. {node_id,
  18. SemIR::BoolLiteral{context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  19. SemIR::BoolValue::True}});
  20. return true;
  21. }
  22. auto HandleIntLiteral(Context& context, Parse::IntLiteralId node_id) -> bool {
  23. // Convert the literal to i32.
  24. // TODO: Form an integer literal value and a corresponding type here instead.
  25. auto literal_int_id =
  26. context.tokens().GetIntLiteral(context.parse_tree().node_token(node_id));
  27. auto literal_val = context.ints().Get(literal_int_id);
  28. if (literal_val.getActiveBits() > 31) {
  29. CARBON_DIAGNOSTIC(IntLiteralTooLargeForI32, Error,
  30. "Integer literal with value {0} does not fit in i32.",
  31. llvm::APSInt);
  32. context.emitter().Emit(node_id, IntLiteralTooLargeForI32,
  33. llvm::APSInt(literal_val, /*isUnsigned=*/true));
  34. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  35. return true;
  36. }
  37. // Literals are always represented as unsigned, so zero-extend if needed.
  38. auto i32_val = literal_val.zextOrTrunc(32);
  39. context.AddInstAndPush(
  40. {node_id,
  41. SemIR::IntLiteral{context.GetBuiltinType(SemIR::BuiltinKind::IntType),
  42. context.ints().Add(i32_val)}});
  43. return true;
  44. }
  45. auto HandleRealLiteral(Context& context, Parse::RealLiteralId node_id) -> bool {
  46. context.AddInstAndPush(
  47. {node_id,
  48. SemIR::RealLiteral{context.GetBuiltinType(SemIR::BuiltinKind::FloatType),
  49. context.tokens().GetRealLiteral(
  50. context.parse_tree().node_token(node_id))}});
  51. return true;
  52. }
  53. auto HandleStringLiteral(Context& context, Parse::StringLiteralId node_id)
  54. -> bool {
  55. context.AddInstAndPush(
  56. {node_id, SemIR::StringLiteral{
  57. context.GetBuiltinType(SemIR::BuiltinKind::StringType),
  58. context.tokens().GetStringLiteralValue(
  59. context.parse_tree().node_token(node_id))}});
  60. return true;
  61. }
  62. auto HandleBoolTypeLiteral(Context& context, Parse::BoolTypeLiteralId node_id)
  63. -> bool {
  64. context.node_stack().Push(node_id, SemIR::InstId::BuiltinBoolType);
  65. return true;
  66. }
  67. auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId node_id)
  68. -> bool {
  69. auto text =
  70. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  71. if (text != "i32") {
  72. return context.TODO(node_id, "Currently only i32 is allowed");
  73. }
  74. context.node_stack().Push(node_id, SemIR::InstId::BuiltinIntType);
  75. return true;
  76. }
  77. auto HandleUnsignedIntTypeLiteral(Context& context,
  78. Parse::UnsignedIntTypeLiteralId node_id)
  79. -> bool {
  80. return context.TODO(node_id, "Need to support unsigned type literals");
  81. }
  82. auto HandleFloatTypeLiteral(Context& context, Parse::FloatTypeLiteralId node_id)
  83. -> bool {
  84. auto text =
  85. context.tokens().GetTokenText(context.parse_tree().node_token(node_id));
  86. if (text != "f64") {
  87. return context.TODO(node_id, "Currently only f64 is allowed");
  88. }
  89. context.node_stack().Push(node_id, SemIR::InstId::BuiltinFloatType);
  90. return true;
  91. }
  92. auto HandleStringTypeLiteral(Context& context,
  93. Parse::StringTypeLiteralId node_id) -> bool {
  94. context.node_stack().Push(node_id, SemIR::InstId::BuiltinStringType);
  95. return true;
  96. }
  97. auto HandleTypeTypeLiteral(Context& context, Parse::TypeTypeLiteralId node_id)
  98. -> bool {
  99. context.node_stack().Push(node_id, SemIR::InstId::BuiltinTypeType);
  100. return true;
  101. }
  102. auto HandleAutoTypeLiteral(Context& context, Parse::AutoTypeLiteralId node_id)
  103. -> bool {
  104. return context.TODO(node_id, "HandleAutoTypeLiteral");
  105. }
  106. } // namespace Carbon::Check