semantics_ir_factory.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_ir_factory.h"
  5. #include <stack>
  6. #include "toolchain/lexer/token_kind.h"
  7. #include "toolchain/lexer/tokenized_buffer.h"
  8. #include "toolchain/parser/parse_node_kind.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. namespace Carbon {
  11. auto SemanticsIRFactory::Build(const TokenizedBuffer& tokens,
  12. const ParseTree& parse_tree) -> SemanticsIR {
  13. SemanticsIRFactory builder(tokens, parse_tree);
  14. builder.Build();
  15. return builder.semantics_;
  16. }
  17. void SemanticsIRFactory::Build() {
  18. auto range = parse_tree().postorder();
  19. for (auto it = range.begin();; ++it) {
  20. auto parse_node = *it;
  21. switch (auto parse_kind = parse_tree().node_kind(parse_node)) {
  22. case ParseNodeKind::DeclaredName(): {
  23. auto text = parse_tree().GetNodeText(parse_node);
  24. auto identifier_id = semantics_.AddIdentifier(text);
  25. Push(parse_node, SemanticsNode::MakeIdentifier(identifier_id));
  26. break;
  27. }
  28. case ParseNodeKind::FunctionDefinition(): {
  29. // Merges code block children up under the FunctionDefinitionStart.
  30. while (parse_tree().node_kind(node_stack_.back().parse_node) !=
  31. ParseNodeKind::FunctionDefinitionStart()) {
  32. node_stack_.pop_back();
  33. }
  34. Pop(ParseNodeKind::FunctionDefinitionStart());
  35. semantics_.AddNode(SemanticsNode::MakeFunctionDefinitionEnd());
  36. Push(parse_node);
  37. break;
  38. }
  39. case ParseNodeKind::FunctionDefinitionStart(): {
  40. Pop(ParseNodeKind::ParameterList());
  41. auto name_node_id = PopWithResult(ParseNodeKind::DeclaredName());
  42. Pop(ParseNodeKind::FunctionIntroducer());
  43. auto decl_id = semantics_.AddNode(
  44. SemanticsNode::MakeFunctionDeclaration(name_node_id));
  45. semantics_.AddNode(SemanticsNode::MakeFunctionDefinitionStart(decl_id));
  46. Push(parse_node);
  47. break;
  48. }
  49. case ParseNodeKind::FileEnd(): {
  50. ++it;
  51. CARBON_CHECK(it == range.end())
  52. << "FileEnd should always be last, found "
  53. << parse_tree().node_kind(*it);
  54. return;
  55. }
  56. case ParseNodeKind::InfixOperator(): {
  57. auto rhs_id = PopWithResult();
  58. auto lhs_id = PopWithResult();
  59. // Figure out the operator for the token.
  60. auto token = parse_tree().node_token(parse_node);
  61. switch (auto token_kind = tokens_->GetKind(token)) {
  62. case TokenKind::Plus():
  63. Push(parse_node,
  64. SemanticsNode::MakeBinaryOperatorAdd(lhs_id, rhs_id));
  65. break;
  66. default:
  67. CARBON_FATAL() << "Unrecognized token kind: " << token_kind.Name();
  68. }
  69. break;
  70. }
  71. case ParseNodeKind::Literal(): {
  72. auto token = parse_tree().node_token(parse_node);
  73. switch (auto token_kind = tokens_->GetKind(token)) {
  74. case TokenKind::IntegerLiteral(): {
  75. auto id =
  76. semantics_.AddIntegerLiteral(tokens_->GetIntegerLiteral(token));
  77. Push(parse_node, SemanticsNode::MakeIntegerLiteral(id));
  78. break;
  79. }
  80. default:
  81. CARBON_FATAL() << "Unhandled kind: " << token_kind.Name();
  82. }
  83. break;
  84. }
  85. case ParseNodeKind::ReturnStatement(): {
  86. Pop(ParseNodeKind::StatementEnd());
  87. // TODO: Restructure ReturnStatement so that we can do this without
  88. // looking at the subtree size.
  89. if (parse_tree().node_subtree_size(parse_node) == 2) {
  90. Push(parse_node, SemanticsNode::MakeReturn());
  91. } else {
  92. auto arg = PopWithResult();
  93. Push(parse_node, SemanticsNode::MakeReturnExpression(arg));
  94. }
  95. break;
  96. }
  97. case ParseNodeKind::ParameterList(): {
  98. // TODO: This should transform into a usable parameter list. For now
  99. // it's unused and only stored so that node counts match.
  100. // TODO: Reorder with ParameterListStart so that we can traverse without
  101. // subtree_size.
  102. Pop(ParseNodeKind::ParameterListEnd());
  103. Push(parse_node);
  104. break;
  105. }
  106. case ParseNodeKind::FunctionIntroducer():
  107. case ParseNodeKind::ParameterListEnd():
  108. case ParseNodeKind::StatementEnd(): {
  109. // The token has no action, but we still track it for the stack.
  110. Push(parse_node);
  111. break;
  112. }
  113. default: {
  114. CARBON_FATAL() << "In ParseTree at index " << parse_node.index()
  115. << ", unhandled NodeKind " << parse_kind;
  116. }
  117. }
  118. }
  119. llvm_unreachable("Should always end at FileEnd");
  120. }
  121. } // namespace Carbon