semantics_ir_factory.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/parse_subtree_consumer.h"
  8. #include "toolchain/semantics/semantics_ir.h"
  9. namespace Carbon {
  10. // The main semantic analysis entry.
  11. class SemanticsIRFactory {
  12. public:
  13. // Builds the SemanticsIR without doing any substantial semantic analysis.
  14. static auto Build(const TokenizedBuffer& tokens, const ParseTree& parse_tree)
  15. -> SemanticsIR;
  16. private:
  17. explicit SemanticsIRFactory(const TokenizedBuffer& tokens,
  18. const ParseTree& parse_tree)
  19. : tokens_(&tokens), semantics_(parse_tree) {}
  20. void Build();
  21. // Requires that a node have no children, to emphasize why the subtree isn't
  22. // otherwise checked.
  23. void RequireNodeEmpty(ParseTree::Node node);
  24. // Transforms a block subtree, such as a file or CodeBlock, into its semantic
  25. // nodes.
  26. auto TransformBlockSubtree(ParseSubtreeConsumer& subtree,
  27. ParseNodeKind end_kind)
  28. -> llvm::SmallVector<Semantics::NodeRef, 0>;
  29. // Each of these takes a parse tree node and does a transformation based on
  30. // its type. These functions are per ParseNodeKind.
  31. auto TransformCodeBlock(ParseTree::Node node)
  32. -> llvm::SmallVector<Semantics::NodeRef, 0>;
  33. void TransformDeclaredName(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  34. ParseTree::Node node, Semantics::NodeId target_id);
  35. void TransformExpression(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  36. ParseTree::Node node, Semantics::NodeId target_id);
  37. // auto TransformExpressionStatement(ParseTree::Node node)
  38. // -> Semantics::Statement;
  39. void TransformFunctionDeclaration(
  40. llvm::SmallVector<Semantics::NodeRef, 0>& nodes, ParseTree::Node node);
  41. void TransformInfixOperator(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  42. ParseTree::Node node,
  43. Semantics::NodeId target_id);
  44. // auto TransformParameterList(ParseTree::Node node)
  45. // -> llvm::SmallVector<Semantics::PatternBinding, 0>
  46. // auto TransformPatternBinding(ParseTree::Node node)
  47. // -> Semantics::PatternBinding;
  48. // auto TransformReturnType(ParseTree::Node node) -> Semantics::Statement;
  49. void TransformReturnStatement(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  50. ParseTree::Node node);
  51. // Returns a unique ID for the SemanticsIR.
  52. auto next_id() -> Semantics::NodeId {
  53. return Semantics::NodeId(id_counter_++);
  54. }
  55. // Convenience accessor.
  56. auto parse_tree() -> const ParseTree& { return *semantics_.parse_tree_; }
  57. // Tokens for getting data on literals.
  58. const TokenizedBuffer* tokens_;
  59. // The SemanticsIR being constructed.
  60. SemanticsIR semantics_;
  61. // A counter for unique IDs.
  62. int32_t id_counter_ = 0;
  63. };
  64. } // namespace Carbon
  65. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_