handle_literal.cpp 3.3 KB

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