shared_value_stores.h 3.4 KB

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