semantics_ir.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/ArrayRef.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/parser/parse_tree.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. namespace Carbon::Testing {
  11. class SemanticsIRForTest;
  12. } // namespace Carbon::Testing
  13. namespace Carbon {
  14. // Provides semantic analysis on a ParseTree.
  15. class SemanticsIR {
  16. public:
  17. // Prints the full IR.
  18. auto Print(llvm::raw_ostream& out) const -> void;
  19. private:
  20. friend class SemanticsIRFactory;
  21. explicit SemanticsIR(const ParseTree& parse_tree)
  22. : parse_tree_(&parse_tree) {}
  23. auto AddIdentifier(llvm::StringRef identifier) -> SemanticsIdentifierId {
  24. SemanticsIdentifierId id(identifiers_.size());
  25. identifiers_.push_back(identifier);
  26. return id;
  27. }
  28. auto AddIntegerLiteral(llvm::APInt integer_literal)
  29. -> SemanticsIntegerLiteralId {
  30. SemanticsIntegerLiteralId id(integer_literals_.size());
  31. integer_literals_.push_back(integer_literal);
  32. return id;
  33. }
  34. auto AddNode(SemanticsNode node) -> SemanticsNodeId {
  35. SemanticsNodeId id(nodes_.size());
  36. nodes_.push_back(node);
  37. return id;
  38. }
  39. llvm::SmallVector<llvm::StringRef> identifiers_;
  40. llvm::SmallVector<llvm::APInt> integer_literals_;
  41. llvm::SmallVector<SemanticsNode> nodes_;
  42. const ParseTree* parse_tree_;
  43. };
  44. } // namespace Carbon
  45. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_