semantics_handle_literal.cpp 3.4 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/semantics/semantics_context.h"
  5. namespace Carbon {
  6. auto SemanticsHandleLiteral(SemanticsContext& context,
  7. ParseTree::Node parse_node) -> bool {
  8. auto token = context.parse_tree().node_token(parse_node);
  9. switch (auto token_kind = context.tokens().GetKind(token)) {
  10. case TokenKind::False:
  11. case TokenKind::True: {
  12. context.AddNodeAndPush(
  13. parse_node,
  14. SemanticsNode::BoolLiteral::Make(
  15. parse_node,
  16. context.CanonicalizeType(SemanticsNodeId::BuiltinBoolType),
  17. token_kind == TokenKind::True ? SemanticsBoolValue::True
  18. : SemanticsBoolValue::False));
  19. break;
  20. }
  21. case TokenKind::IntegerLiteral: {
  22. auto id = context.semantics_ir().AddIntegerLiteral(
  23. context.tokens().GetIntegerLiteral(token));
  24. context.AddNodeAndPush(
  25. parse_node,
  26. SemanticsNode::IntegerLiteral::Make(
  27. parse_node,
  28. context.CanonicalizeType(SemanticsNodeId::BuiltinIntegerType),
  29. id));
  30. break;
  31. }
  32. case TokenKind::RealLiteral: {
  33. auto token_value = context.tokens().GetRealLiteral(token);
  34. auto id = context.semantics_ir().AddRealLiteral(
  35. {.mantissa = token_value.Mantissa(),
  36. .exponent = token_value.Exponent(),
  37. .is_decimal = token_value.IsDecimal()});
  38. context.AddNodeAndPush(parse_node,
  39. SemanticsNode::RealLiteral::Make(
  40. parse_node,
  41. context.CanonicalizeType(
  42. SemanticsNodeId::BuiltinFloatingPointType),
  43. id));
  44. break;
  45. }
  46. case TokenKind::StringLiteral: {
  47. auto id = context.semantics_ir().AddString(
  48. context.tokens().GetStringLiteral(token));
  49. context.AddNodeAndPush(
  50. parse_node,
  51. SemanticsNode::StringLiteral::Make(
  52. parse_node,
  53. context.CanonicalizeType(SemanticsNodeId::BuiltinStringType),
  54. id));
  55. break;
  56. }
  57. case TokenKind::Bool: {
  58. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinBoolType);
  59. break;
  60. }
  61. case 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,
  67. SemanticsNodeId::BuiltinIntegerType);
  68. break;
  69. }
  70. case 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. SemanticsNodeId::BuiltinFloatingPointType);
  77. break;
  78. }
  79. case TokenKind::StringTypeLiteral: {
  80. context.node_stack().Push(parse_node, SemanticsNodeId::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