semantics_ir.h 2.0 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 TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
  5. #define TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "llvm/ADT/StringMap.h"
  8. #include "toolchain/parser/parse_tree.h"
  9. #include "toolchain/semantics/function.h"
  10. namespace Carbon {
  11. // Provides semantic analysis on a ParseTree.
  12. class SemanticsIR {
  13. public:
  14. // Provides a link back to a semantic node in a name scope.
  15. class Node {
  16. public:
  17. Node() : Node(Kind::Invalid, -1) {}
  18. private:
  19. friend class SemanticsIR;
  20. // The kind of token. These correspond to the lists on SemanticsIR which
  21. // will be indexed into.
  22. enum class Kind {
  23. Invalid,
  24. Function,
  25. };
  26. Node(Kind kind, int32_t index) : kind_(kind), index_(index) {
  27. // TODO: kind_ and index_ are currently unused, this suppresses the
  28. // warning.
  29. kind_ = kind;
  30. index_ = index;
  31. }
  32. Kind kind_;
  33. // The index of the named entity within its list.
  34. int32_t index_;
  35. };
  36. struct Block {
  37. public:
  38. void Add(llvm::StringRef name, Node named_entity);
  39. private:
  40. llvm::SmallVector<Node> ordering_;
  41. llvm::StringMap<Node> name_lookup_;
  42. };
  43. private:
  44. friend class SemanticsIRFactory;
  45. explicit SemanticsIR(const ParseTree& parse_tree)
  46. : parse_tree_(&parse_tree) {}
  47. // Creates a function, adds it to the enclosing scope, and returns a reference
  48. // for further mutations. On a name collision, it will not be added to the
  49. // scope, but will still be returned.
  50. auto AddFunction(Block& block, ParseTree::Node decl_node,
  51. ParseTree::Node name_node) -> Semantics::Function&;
  52. // Indexed by Token::Function.
  53. llvm::SmallVector<Semantics::Function, 0> functions_;
  54. // The file-level block.
  55. Block root_block_;
  56. const ParseTree* parse_tree_;
  57. };
  58. } // namespace Carbon
  59. #endif // TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_