node_ref.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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_NODE_REF_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODE_REF_H_
  6. #include <cstdint>
  7. #include "common/ostream.h"
  8. #include "toolchain/semantics/node_kind.h"
  9. namespace Carbon::Semantics {
  10. // The standard structure for nodes.
  11. //
  12. // This flyweight pattern is used so that each subtype can be stored in its own
  13. // vector, minimizing memory consumption and heap fragmentation when large
  14. // quantities are being created.
  15. class NodeRef {
  16. public:
  17. NodeRef() : NodeRef(NodeKind::Invalid, -1) {}
  18. auto kind() -> NodeKind { return kind_; }
  19. private:
  20. template <typename... StoredNodeT>
  21. friend class NodeStoreBase;
  22. NodeRef(NodeKind kind, int32_t index) : kind_(kind), index_(index) {}
  23. NodeKind kind_;
  24. // The index of the named entity within its list.
  25. int32_t index_;
  26. };
  27. } // namespace Carbon::Semantics
  28. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODE_REF_H_