handle_literal.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 HandleLiteral(Context& context, Parse::Node parse_node) -> bool {
  7. auto token = context.parse_tree().node_token(parse_node);
  8. switch (auto token_kind = context.tokens().GetKind(token)) {
  9. case Lex::TokenKind::False:
  10. case Lex::TokenKind::True: {
  11. context.AddNodeAndPush(
  12. parse_node,
  13. SemIR::BoolLiteral(
  14. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
  15. token_kind == Lex::TokenKind::True ? SemIR::BoolValue::True
  16. : SemIR::BoolValue::False));
  17. break;
  18. }
  19. case Lex::TokenKind::IntegerLiteral: {
  20. auto id = context.semantics_ir().AddInteger(
  21. context.tokens().GetIntegerLiteral(token));
  22. context.AddNodeAndPush(
  23. parse_node,
  24. SemIR::IntegerLiteral(
  25. parse_node,
  26. context.GetBuiltinType(SemIR::BuiltinKind::IntegerType), id));
  27. break;
  28. }
  29. case Lex::TokenKind::RealLiteral: {
  30. auto token_value = context.tokens().GetRealLiteral(token);
  31. auto id = context.semantics_ir().AddReal(
  32. {.mantissa = token_value.mantissa,
  33. .exponent = token_value.exponent,
  34. .is_decimal = token_value.is_decimal});
  35. context.AddNodeAndPush(
  36. parse_node,
  37. SemIR::RealLiteral(
  38. parse_node,
  39. context.GetBuiltinType(SemIR::BuiltinKind::FloatingPointType),
  40. id));
  41. break;
  42. }
  43. case Lex::TokenKind::StringLiteral: {
  44. auto id = context.semantics_ir().AddString(
  45. context.tokens().GetStringLiteral(token));
  46. context.AddNodeAndPush(
  47. parse_node,
  48. SemIR::StringLiteral(
  49. parse_node,
  50. context.GetBuiltinType(SemIR::BuiltinKind::StringType), id));
  51. break;
  52. }
  53. case Lex::TokenKind::Type: {
  54. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinTypeType);
  55. break;
  56. }
  57. case Lex::TokenKind::Bool: {
  58. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinBoolType);
  59. break;
  60. }
  61. case Lex::TokenKind::IntegerTypeLiteral: {
  62. auto text = context.tokens().GetTokenText(token);
  63. if (text != "i32") {
  64. return context.TODO(parse_node, "Currently only i32 is allowed");
  65. }
  66. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinIntegerType);
  67. break;
  68. }
  69. case Lex::TokenKind::FloatingPointTypeLiteral: {
  70. auto text = context.tokens().GetTokenText(token);
  71. if (text != "f64") {
  72. return context.TODO(parse_node, "Currently only f64 is allowed");
  73. }
  74. context.node_stack().Push(parse_node,
  75. SemIR::NodeId::BuiltinFloatingPointType);
  76. break;
  77. }
  78. case Lex::TokenKind::StringTypeLiteral: {
  79. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinStringType);
  80. break;
  81. }
  82. default: {
  83. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  84. }
  85. }
  86. return true;
  87. }
  88. } // namespace Carbon::Check