semantics_handle_literal.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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::Type: {
  58. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinTypeType);
  59. break;
  60. }
  61. case TokenKind::Bool: {
  62. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinBoolType);
  63. break;
  64. }
  65. case TokenKind::IntegerTypeLiteral: {
  66. auto text = context.tokens().GetTokenText(token);
  67. if (text != "i32") {
  68. return context.TODO(parse_node, "Currently only i32 is allowed");
  69. }
  70. context.node_stack().Push(parse_node,
  71. SemanticsNodeId::BuiltinIntegerType);
  72. break;
  73. }
  74. case TokenKind::FloatingPointTypeLiteral: {
  75. auto text = context.tokens().GetTokenText(token);
  76. if (text != "f64") {
  77. return context.TODO(parse_node, "Currently only f64 is allowed");
  78. }
  79. context.node_stack().Push(parse_node,
  80. SemanticsNodeId::BuiltinFloatingPointType);
  81. break;
  82. }
  83. case TokenKind::StringTypeLiteral: {
  84. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinStringType);
  85. break;
  86. }
  87. default: {
  88. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  89. }
  90. }
  91. return true;
  92. }
  93. } // namespace Carbon