handle_literal.cpp 3.4 KB

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