handle_literal.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. context.AddNodeAndPush(
  21. parse_node,
  22. SemIR::IntegerLiteral{
  23. parse_node,
  24. context.GetBuiltinType(SemIR::BuiltinKind::IntegerType),
  25. context.tokens().GetIntegerLiteral(token)});
  26. break;
  27. }
  28. case Lex::TokenKind::RealLiteral: {
  29. context.AddNodeAndPush(
  30. parse_node,
  31. SemIR::RealLiteral{
  32. parse_node,
  33. context.GetBuiltinType(SemIR::BuiltinKind::FloatingPointType),
  34. context.tokens().GetRealLiteral(token)});
  35. break;
  36. }
  37. case Lex::TokenKind::StringLiteral: {
  38. auto id = context.tokens().GetStringLiteral(token);
  39. context.AddNodeAndPush(
  40. parse_node,
  41. SemIR::StringLiteral{
  42. parse_node,
  43. context.GetBuiltinType(SemIR::BuiltinKind::StringType), id});
  44. break;
  45. }
  46. case Lex::TokenKind::Type: {
  47. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinTypeType);
  48. break;
  49. }
  50. case Lex::TokenKind::Bool: {
  51. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinBoolType);
  52. break;
  53. }
  54. case Lex::TokenKind::IntegerTypeLiteral: {
  55. auto text = context.tokens().GetTokenText(token);
  56. if (text != "i32") {
  57. return context.TODO(parse_node, "Currently only i32 is allowed");
  58. }
  59. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinIntegerType);
  60. break;
  61. }
  62. case Lex::TokenKind::FloatingPointTypeLiteral: {
  63. auto text = context.tokens().GetTokenText(token);
  64. if (text != "f64") {
  65. return context.TODO(parse_node, "Currently only f64 is allowed");
  66. }
  67. context.node_stack().Push(parse_node,
  68. SemIR::NodeId::BuiltinFloatingPointType);
  69. break;
  70. }
  71. case Lex::TokenKind::StringTypeLiteral: {
  72. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinStringType);
  73. break;
  74. }
  75. default: {
  76. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  77. }
  78. }
  79. return true;
  80. }
  81. } // namespace Carbon::Check