value_store.h 9.7 KB

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