shared_value_stores.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_BASE_SHARED_VALUE_STORES_H_
  5. #define CARBON_TOOLCHAIN_BASE_SHARED_VALUE_STORES_H_
  6. #include "toolchain/base/mem_usage.h"
  7. #include "toolchain/base/value_ids.h"
  8. #include "toolchain/base/value_store.h"
  9. #include "toolchain/base/yaml.h"
  10. namespace Carbon {
  11. // Stores that will be used across compiler phases for a given compilation unit.
  12. // This is provided mainly so that they don't need to be passed separately.
  13. class SharedValueStores : public Yaml::Printable<SharedValueStores> {
  14. public:
  15. // Provide types that can be used by APIs to forward access to these stores.
  16. using IntStore = CanonicalValueStore<IntId>;
  17. using RealStore = ValueStore<RealId>;
  18. using FloatStore = CanonicalValueStore<FloatId>;
  19. using IdentifierStore = CanonicalValueStore<IdentifierId>;
  20. using StringLiteralStore = CanonicalValueStore<StringLiteralValueId>;
  21. explicit SharedValueStores() = default;
  22. // Not copyable or movable.
  23. SharedValueStores(const SharedValueStores&) = delete;
  24. auto operator=(const SharedValueStores&) -> SharedValueStores& = delete;
  25. auto identifiers() -> IdentifierStore& { return identifiers_; }
  26. auto identifiers() const -> const IdentifierStore& { return identifiers_; }
  27. auto ints() -> IntStore& { return ints_; }
  28. auto ints() const -> const IntStore& { return ints_; }
  29. auto reals() -> RealStore& { return reals_; }
  30. auto reals() const -> const RealStore& { return reals_; }
  31. auto floats() -> FloatStore& { return floats_; }
  32. auto floats() const -> const FloatStore& { return floats_; }
  33. auto string_literal_values() -> StringLiteralStore& {
  34. return string_literals_;
  35. }
  36. auto string_literal_values() const -> const StringLiteralStore& {
  37. return string_literals_;
  38. }
  39. auto OutputYaml(std::optional<llvm::StringRef> filename = std::nullopt) const
  40. -> Yaml::OutputMapping {
  41. return Yaml::OutputMapping([&, filename](Yaml::OutputMapping::Map map) {
  42. if (filename) {
  43. map.Add("filename", *filename);
  44. }
  45. map.Add("shared_values",
  46. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  47. map.Add("ints", ints_.OutputYaml());
  48. map.Add("reals", reals_.OutputYaml());
  49. map.Add("identifiers", identifiers_.OutputYaml());
  50. map.Add("strings", string_literals_.OutputYaml());
  51. }));
  52. });
  53. }
  54. // Collects memory usage for the various shared stores.
  55. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  56. -> void {
  57. mem_usage.Collect(MemUsage::ConcatLabel(label, "ints_"), ints_);
  58. mem_usage.Collect(MemUsage::ConcatLabel(label, "reals_"), reals_);
  59. mem_usage.Collect(MemUsage::ConcatLabel(label, "floats_"), floats_);
  60. mem_usage.Collect(MemUsage::ConcatLabel(label, "identifiers_"),
  61. identifiers_);
  62. mem_usage.Collect(MemUsage::ConcatLabel(label, "string_literals_"),
  63. string_literals_);
  64. }
  65. private:
  66. IntStore ints_;
  67. RealStore reals_;
  68. FloatStore floats_;
  69. IdentifierStore identifiers_;
  70. StringLiteralStore string_literals_;
  71. };
  72. } // namespace Carbon
  73. #endif // CARBON_TOOLCHAIN_BASE_SHARED_VALUE_STORES_H_