value_store.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_VALUE_STORE_H_
  5. #define CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
  6. #include <type_traits>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "llvm/ADT/APInt.h"
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/STLFunctionalExtras.h"
  12. #include "llvm/ADT/Sequence.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/YAMLParser.h"
  16. #include "toolchain/base/index_base.h"
  17. #include "toolchain/base/yaml.h"
  18. namespace Carbon {
  19. // The value of a real literal.
  20. //
  21. // This is either a dyadic fraction (mantissa * 2^exponent) or a decadic
  22. // fraction (mantissa * 10^exponent).
  23. //
  24. // TODO: For SemIR, replace this with a Rational type, per the design:
  25. // docs/design/expressions/literals.md
  26. class Real : public Printable<Real> {
  27. public:
  28. auto Print(llvm::raw_ostream& output_stream) const -> void {
  29. mantissa.print(output_stream, /*isSigned=*/false);
  30. output_stream << "*" << (is_decimal ? "10" : "2") << "^" << exponent;
  31. }
  32. // The mantissa, represented as an unsigned integer.
  33. llvm::APInt mantissa;
  34. // The exponent, represented as a signed integer.
  35. llvm::APInt exponent;
  36. // If false, the value is mantissa * 2^exponent.
  37. // If true, the value is mantissa * 10^exponent.
  38. // TODO: This field increases Real from 32 bytes to 40 bytes. Consider
  39. // changing how it's tracked for space savings.
  40. bool is_decimal;
  41. };
  42. // Corresponds to an integer value represented by an APInt.
  43. struct IntegerId : public IndexBase, public Printable<IntegerId> {
  44. using IndexedType = const llvm::APInt;
  45. static const IntegerId Invalid;
  46. using IndexBase::IndexBase;
  47. auto Print(llvm::raw_ostream& out) const -> void {
  48. out << "int";
  49. IndexBase::Print(out);
  50. }
  51. };
  52. constexpr IntegerId IntegerId::Invalid(IntegerId::InvalidIndex);
  53. // Corresponds to a Real value.
  54. struct RealId : public IndexBase, public Printable<RealId> {
  55. using IndexedType = const Real;
  56. static const RealId Invalid;
  57. using IndexBase::IndexBase;
  58. auto Print(llvm::raw_ostream& out) const -> void {
  59. out << "real";
  60. IndexBase::Print(out);
  61. }
  62. };
  63. constexpr RealId RealId::Invalid(RealId::InvalidIndex);
  64. // Corresponds to a StringRef.
  65. struct StringId : public IndexBase, public Printable<StringId> {
  66. using IndexedType = const std::string;
  67. static const StringId Invalid;
  68. using IndexBase::IndexBase;
  69. auto Print(llvm::raw_ostream& out) const -> void {
  70. out << "str";
  71. IndexBase::Print(out);
  72. }
  73. };
  74. constexpr StringId StringId::Invalid(StringId::InvalidIndex);
  75. // Adapts StringId for identifiers.
  76. //
  77. // `NameId` relies on the values of this type other than `Invalid` all being
  78. // non-negative.
  79. struct IdentifierId : public IndexBase, public Printable<IdentifierId> {
  80. static const IdentifierId Invalid;
  81. using IndexBase::IndexBase;
  82. auto Print(llvm::raw_ostream& out) const -> void {
  83. out << "strId";
  84. IndexBase::Print(out);
  85. }
  86. };
  87. constexpr IdentifierId IdentifierId::Invalid(IdentifierId::InvalidIndex);
  88. // Adapts StringId for string literals.
  89. struct StringLiteralId : public IndexBase, public Printable<StringLiteralId> {
  90. static const StringLiteralId Invalid;
  91. using IndexBase::IndexBase;
  92. auto Print(llvm::raw_ostream& out) const -> void {
  93. out << "strLit";
  94. IndexBase::Print(out);
  95. }
  96. };
  97. constexpr StringLiteralId StringLiteralId::Invalid(
  98. StringLiteralId::InvalidIndex);
  99. namespace Internal {
  100. // Used as a parent class for non-printable types. This is just for
  101. // std::conditional, not as an API.
  102. class ValueStoreNotPrintable {};
  103. } // namespace Internal
  104. // A simple wrapper for accumulating values, providing IDs to later retrieve the
  105. // value. This does not do deduplication.
  106. template <typename IdT, typename ValueT = typename IdT::IndexedType>
  107. class ValueStore
  108. : public std::conditional<std::is_base_of_v<Printable<ValueT>, ValueT>,
  109. Yaml::Printable<ValueStore<IdT, ValueT>>,
  110. Internal::ValueStoreNotPrintable> {
  111. public:
  112. // Stores the value and returns an ID to reference it.
  113. auto Add(ValueT value) -> IdT {
  114. IdT id = IdT(values_.size());
  115. CARBON_CHECK(id.index >= 0) << "Id overflow";
  116. values_.push_back(std::move(value));
  117. return id;
  118. }
  119. // Adds a default constructed value and returns an ID to reference it.
  120. auto AddDefaultValue() -> IdT {
  121. auto id = IdT(values_.size());
  122. values_.resize(id.index + 1);
  123. return id;
  124. }
  125. // Returns a mutable value for an ID.
  126. auto Get(IdT id) -> ValueT& {
  127. CARBON_CHECK(id.index >= 0) << id.index;
  128. return values_[id.index];
  129. }
  130. // Returns the value for an ID.
  131. auto Get(IdT id) const -> const ValueT& {
  132. CARBON_CHECK(id.index >= 0) << id.index;
  133. return values_[id.index];
  134. }
  135. // Reserves space.
  136. auto Reserve(size_t size) -> void { values_.reserve(size); }
  137. // These are to support printable structures, and are not guaranteed.
  138. auto OutputYaml() const -> Yaml::OutputMapping {
  139. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  140. for (auto i : llvm::seq(values_.size())) {
  141. auto id = IdT(i);
  142. map.Add(PrintToString(id), Yaml::OutputScalar(Get(id)));
  143. }
  144. });
  145. }
  146. auto array_ref() const -> llvm::ArrayRef<ValueT> { return values_; }
  147. auto size() const -> int { return values_.size(); }
  148. private:
  149. llvm::SmallVector<std::decay_t<ValueT>> values_;
  150. };
  151. // Storage for StringRefs. The caller is responsible for ensuring storage is
  152. // allocated.
  153. template <>
  154. class ValueStore<StringId> : public Yaml::Printable<ValueStore<StringId>> {
  155. public:
  156. // Returns an ID to reference the value. May return an existing ID if the
  157. // string was previously added.
  158. auto Add(llvm::StringRef value) -> StringId {
  159. auto [it, inserted] = map_.insert({value, StringId(values_.size())});
  160. if (inserted) {
  161. CARBON_CHECK(it->second.index >= 0) << "Too many unique strings";
  162. values_.push_back(value);
  163. }
  164. return it->second;
  165. }
  166. // Returns the value for an ID.
  167. auto Get(StringId id) const -> llvm::StringRef {
  168. CARBON_CHECK(id.is_valid());
  169. return values_[id.index];
  170. }
  171. auto OutputYaml() const -> Yaml::OutputMapping {
  172. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  173. for (auto [i, val] : llvm::enumerate(values_)) {
  174. map.Add(PrintToString(StringId(i)), val);
  175. }
  176. });
  177. }
  178. private:
  179. llvm::DenseMap<llvm::StringRef, StringId> map_;
  180. llvm::SmallVector<llvm::StringRef> values_;
  181. };
  182. // A thin wrapper around a `ValueStore<StringId>` that provides a different IdT,
  183. // while using a unified storage for values. This avoids potentially
  184. // duplicative string hash maps, which are expensive.
  185. template <typename IdT>
  186. class StringStoreWrapper : public Printable<StringStoreWrapper<IdT>> {
  187. public:
  188. explicit StringStoreWrapper(ValueStore<StringId>* values) : values_(values) {}
  189. auto Add(llvm::StringRef value) -> IdT {
  190. return IdT(values_->Add(value).index);
  191. }
  192. auto Get(IdT id) const -> llvm::StringRef {
  193. return values_->Get(StringId(id.index));
  194. }
  195. auto Print(llvm::raw_ostream& out) const -> void { out << *values_; }
  196. private:
  197. ValueStore<StringId>* values_;
  198. };
  199. // Stores that will be used across compiler phases for a given compilation unit.
  200. // This is provided mainly so that they don't need to be passed separately.
  201. class SharedValueStores : public Yaml::Printable<SharedValueStores> {
  202. public:
  203. explicit SharedValueStores()
  204. : identifiers_(&strings_), string_literals_(&strings_) {}
  205. // Not copyable or movable.
  206. SharedValueStores(const SharedValueStores&) = delete;
  207. auto operator=(const SharedValueStores&) -> SharedValueStores& = delete;
  208. auto identifiers() -> StringStoreWrapper<IdentifierId>& {
  209. return identifiers_;
  210. }
  211. auto identifiers() const -> const StringStoreWrapper<IdentifierId>& {
  212. return identifiers_;
  213. }
  214. auto integers() -> ValueStore<IntegerId>& { return integers_; }
  215. auto integers() const -> const ValueStore<IntegerId>& { return integers_; }
  216. auto reals() -> ValueStore<RealId>& { return reals_; }
  217. auto reals() const -> const ValueStore<RealId>& { return reals_; }
  218. auto string_literals() -> StringStoreWrapper<StringLiteralId>& {
  219. return string_literals_;
  220. }
  221. auto string_literals() const -> const StringStoreWrapper<StringLiteralId>& {
  222. return string_literals_;
  223. }
  224. auto OutputYaml(std::optional<llvm::StringRef> filename = std::nullopt) const
  225. -> Yaml::OutputMapping {
  226. return Yaml::OutputMapping([&, filename](Yaml::OutputMapping::Map map) {
  227. if (filename) {
  228. map.Add("filename", *filename);
  229. }
  230. map.Add("shared_values",
  231. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  232. map.Add("integers", integers_.OutputYaml());
  233. map.Add("reals", reals_.OutputYaml());
  234. map.Add("strings", strings_.OutputYaml());
  235. }));
  236. });
  237. }
  238. private:
  239. ValueStore<IntegerId> integers_;
  240. ValueStore<RealId> reals_;
  241. ValueStore<StringId> strings_;
  242. StringStoreWrapper<IdentifierId> identifiers_;
  243. StringStoreWrapper<StringLiteralId> string_literals_;
  244. };
  245. } // namespace Carbon
  246. // Support use of IdentifierId as DenseMap/DenseSet keys.
  247. // TODO: Remove once NameId is used in checking.
  248. template <>
  249. struct llvm::DenseMapInfo<Carbon::IdentifierId>
  250. : public Carbon::IndexMapInfo<Carbon::IdentifierId> {};
  251. // Support use of StringId as DenseMap/DenseSet keys.
  252. template <>
  253. struct llvm::DenseMapInfo<Carbon::StringId>
  254. : public Carbon::IndexMapInfo<Carbon::StringId> {};
  255. #endif // CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_