semantics_ir_factory.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "common/check.h"
  6. #include "llvm/Support/FormatVariadic.h"
  7. #include "toolchain/lexer/tokenized_buffer.h"
  8. #include "toolchain/parser/parse_node_kind.h"
  9. namespace Carbon {
  10. auto SemanticsIRFactory::Build(const ParseTree& parse_tree) -> SemanticsIR {
  11. SemanticsIRFactory builder(parse_tree);
  12. builder.ProcessRoots();
  13. return builder.semantics_;
  14. }
  15. void SemanticsIRFactory::ProcessRoots() {
  16. for (ParseTree::Node node : semantics_.parse_tree_->roots()) {
  17. switch (semantics_.parse_tree_->node_kind(node)) {
  18. case ParseNodeKind::FunctionDeclaration():
  19. ProcessFunctionNode(semantics_.root_block_, node);
  20. break;
  21. case ParseNodeKind::FileEnd():
  22. // No action needed.
  23. break;
  24. default:
  25. CARBON_FATAL() << "Unhandled node kind: "
  26. << semantics_.parse_tree_->node_kind(node).name();
  27. }
  28. }
  29. }
  30. void SemanticsIRFactory::ProcessFunctionNode(SemanticsIR::Block& block,
  31. ParseTree::Node decl_node) {
  32. llvm::Optional<Semantics::Function> fn;
  33. for (ParseTree::Node node : semantics_.parse_tree_->children(decl_node)) {
  34. switch (semantics_.parse_tree_->node_kind(node)) {
  35. case ParseNodeKind::DeclaredName():
  36. fn = semantics_.AddFunction(block, decl_node, node);
  37. break;
  38. case ParseNodeKind::ParameterList():
  39. // TODO: Maybe something like Semantics::AddVariable passed to
  40. // Function::AddParameter.
  41. break;
  42. case ParseNodeKind::CodeBlock():
  43. // TODO: Should accumulate the definition into the code block.
  44. break;
  45. default:
  46. CARBON_FATAL() << "Unhandled node kind: "
  47. << semantics_.parse_tree_->node_kind(node).name();
  48. }
  49. }
  50. }
  51. } // namespace Carbon