semantics_ir_factory.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, int32_t target_id);
  35. void TransformExpression(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  36. ParseTree::Node node, int32_t 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, int32_t target_id);
  43. // auto TransformParameterList(ParseTree::Node node)
  44. // -> llvm::SmallVector<Semantics::PatternBinding, 0>
  45. // auto TransformPatternBinding(ParseTree::Node node)
  46. // -> Semantics::PatternBinding;
  47. // auto TransformReturnType(ParseTree::Node node) -> Semantics::Statement;
  48. void TransformReturnStatement(llvm::SmallVector<Semantics::NodeRef, 0>& nodes,
  49. ParseTree::Node node);
  50. // Returns a unique ID for the SemanticsIR.
  51. auto next_id() -> int32_t { return id_counter_++; }
  52. // Convenience accessor.
  53. auto parse_tree() -> const ParseTree& { return *semantics_.parse_tree_; }
  54. // Tokens for getting data on literals.
  55. const TokenizedBuffer* tokens_;
  56. // The SemanticsIR being constructed.
  57. SemanticsIR semantics_;
  58. // A counter for unique IDs.
  59. int32_t id_counter_ = 0;
  60. };
  61. } // namespace Carbon
  62. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_