semantics_ir.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/parser/parse_tree.h"
  8. #include "toolchain/semantics/meta_node_block.h"
  9. #include "toolchain/semantics/nodes/expression_statement.h"
  10. #include "toolchain/semantics/nodes/function.h"
  11. #include "toolchain/semantics/nodes/infix_operator.h"
  12. #include "toolchain/semantics/nodes/literal.h"
  13. #include "toolchain/semantics/nodes/pattern_binding.h"
  14. #include "toolchain/semantics/nodes/return.h"
  15. namespace Carbon::Testing {
  16. class SemanticsIRForTest;
  17. } // namespace Carbon::Testing
  18. namespace Carbon {
  19. // Provides semantic analysis on a ParseTree.
  20. class SemanticsIR {
  21. public:
  22. // File-level declarations.
  23. auto root_block() const -> const Semantics::DeclarationBlock& {
  24. return *root_block_;
  25. }
  26. // Debug printer for the parse tree.
  27. void Print(llvm::raw_ostream& out, ParseTree::Node node) const;
  28. // Debug printers for meta nodes.
  29. void Print(llvm::raw_ostream& out, Semantics::Declaration decl) const;
  30. void Print(llvm::raw_ostream& out, Semantics::Expression expr) const;
  31. void Print(llvm::raw_ostream& out, Semantics::Statement stmt) const;
  32. // Debug printers for other nodes.
  33. void Print(llvm::raw_ostream& out, const Semantics::DeclaredName& name) const;
  34. void Print(llvm::raw_ostream& out,
  35. const Semantics::ExpressionStatement& expr) const;
  36. void Print(llvm::raw_ostream& out, const Semantics::Function& function) const;
  37. void Print(llvm::raw_ostream& out, const Semantics::InfixOperator& op) const;
  38. void Print(llvm::raw_ostream& out, const Semantics::Literal& literal) const;
  39. void Print(llvm::raw_ostream& out,
  40. const Semantics::PatternBinding& binding) const;
  41. void Print(llvm::raw_ostream& out, const Semantics::Return& ret) const;
  42. void Print(llvm::raw_ostream& out,
  43. const Semantics::StatementBlock& block) const;
  44. private:
  45. friend class SemanticsIRFactory;
  46. friend class Testing::SemanticsIRForTest;
  47. explicit SemanticsIR(const ParseTree& parse_tree)
  48. : parse_tree_(&parse_tree) {}
  49. Semantics::DeclarationStore declarations_;
  50. Semantics::ExpressionStore expressions_;
  51. Semantics::StatementStore statements_;
  52. // The file-level block. Only assigned after initialization is complete.
  53. llvm::Optional<Semantics::DeclarationBlock> root_block_;
  54. const ParseTree* parse_tree_;
  55. };
  56. } // namespace Carbon
  57. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_