semantics_ir_factory.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_
  6. #include "toolchain/parser/parse_tree.h"
  7. #include "toolchain/semantics/semantics_ir.h"
  8. namespace Carbon {
  9. // The main semantic analysis entry.
  10. class SemanticsIRFactory {
  11. public:
  12. // Builds the SemanticsIR without doing any substantial semantic analysis.
  13. static auto Build(const ParseTree& parse_tree) -> SemanticsIR;
  14. private:
  15. explicit SemanticsIRFactory(const ParseTree& parse_tree)
  16. : semantics_(parse_tree) {}
  17. void Build();
  18. // Requires that a node have no children, to emphasize why the subtree isn't
  19. // otherwise checked.
  20. void RequireNodeEmpty(ParseTree::Node node);
  21. // Each of these takes a parse tree node and does a transformation based on
  22. // its type. These functions are per ParseNodeKind.
  23. auto TransformCodeBlock(ParseTree::Node node) -> Semantics::StatementBlock;
  24. auto TransformDeclaredName(ParseTree::Node node) -> Semantics::DeclaredName;
  25. auto TransformExpression(ParseTree::Node node) -> Semantics::Expression;
  26. auto TransformExpressionStatement(ParseTree::Node node)
  27. -> Semantics::Statement;
  28. auto TransformFunctionDeclaration(ParseTree::Node node)
  29. -> std::tuple<llvm::StringRef, Semantics::Declaration>;
  30. auto TransformInfixOperator(ParseTree::Node node) -> Semantics::InfixOperator;
  31. auto TransformParameterList(ParseTree::Node node)
  32. -> llvm::SmallVector<Semantics::PatternBinding, 0>;
  33. auto TransformPatternBinding(ParseTree::Node node)
  34. -> Semantics::PatternBinding;
  35. auto TransformReturnType(ParseTree::Node node) -> Semantics::Expression;
  36. auto TransformReturnStatement(ParseTree::Node node) -> Semantics::Statement;
  37. // Convenience accessor.
  38. auto parse_tree() -> const ParseTree& { return *semantics_.parse_tree_; }
  39. SemanticsIR semantics_;
  40. };
  41. } // namespace Carbon
  42. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_