handle_literal.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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,
  7. Parse::BoolLiteralFalseId parse_node) -> bool {
  8. context.AddInstAndPush(
  9. parse_node,
  10. SemIR::BoolLiteral{parse_node,
  11. context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  12. SemIR::BoolValue::False});
  13. return true;
  14. }
  15. auto HandleBoolLiteralTrue(Context& context,
  16. Parse::BoolLiteralTrueId parse_node) -> bool {
  17. context.AddInstAndPush(
  18. parse_node,
  19. SemIR::BoolLiteral{parse_node,
  20. context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  21. SemIR::BoolValue::True});
  22. return true;
  23. }
  24. auto HandleIntLiteral(Context& context, Parse::IntLiteralId parse_node)
  25. -> bool {
  26. context.AddInstAndPush(
  27. parse_node,
  28. SemIR::IntLiteral{parse_node,
  29. context.GetBuiltinType(SemIR::BuiltinKind::IntType),
  30. context.tokens().GetIntLiteral(
  31. context.parse_tree().node_token(parse_node))});
  32. return true;
  33. }
  34. auto HandleRealLiteral(Context& context, Parse::RealLiteralId parse_node)
  35. -> bool {
  36. context.AddInstAndPush(
  37. parse_node,
  38. SemIR::RealLiteral{parse_node,
  39. context.GetBuiltinType(SemIR::BuiltinKind::FloatType),
  40. context.tokens().GetRealLiteral(
  41. context.parse_tree().node_token(parse_node))});
  42. return true;
  43. }
  44. auto HandleStringLiteral(Context& context, Parse::StringLiteralId parse_node)
  45. -> bool {
  46. context.AddInstAndPush(
  47. parse_node,
  48. SemIR::StringLiteral{
  49. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::StringType),
  50. context.tokens().GetStringLiteralValue(
  51. context.parse_tree().node_token(parse_node))});
  52. return true;
  53. }
  54. auto HandleBoolTypeLiteral(Context& context,
  55. Parse::BoolTypeLiteralId parse_node) -> bool {
  56. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinBoolType);
  57. return true;
  58. }
  59. auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId parse_node)
  60. -> bool {
  61. auto text = context.tokens().GetTokenText(
  62. context.parse_tree().node_token(parse_node));
  63. if (text != "i32") {
  64. return context.TODO(parse_node, "Currently only i32 is allowed");
  65. }
  66. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinIntType);
  67. return true;
  68. }
  69. auto HandleUnsignedIntTypeLiteral(Context& context,
  70. Parse::UnsignedIntTypeLiteralId parse_node)
  71. -> bool {
  72. return context.TODO(parse_node, "Need to support unsigned type literals");
  73. }
  74. auto HandleFloatTypeLiteral(Context& context,
  75. Parse::FloatTypeLiteralId parse_node) -> bool {
  76. auto text = context.tokens().GetTokenText(
  77. context.parse_tree().node_token(parse_node));
  78. if (text != "f64") {
  79. return context.TODO(parse_node, "Currently only f64 is allowed");
  80. }
  81. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinFloatType);
  82. return true;
  83. }
  84. auto HandleStringTypeLiteral(Context& context,
  85. Parse::StringTypeLiteralId parse_node) -> bool {
  86. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinStringType);
  87. return true;
  88. }
  89. auto HandleTypeTypeLiteral(Context& context,
  90. Parse::TypeTypeLiteralId parse_node) -> bool {
  91. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinTypeType);
  92. return true;
  93. }
  94. } // namespace Carbon::Check