shared_value_stores.h 3.3 KB

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