handle_literal.cpp 3.5 KB

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