shared_value_stores.h 3.5 KB

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