handle_literal.cpp 3.6 KB

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