value_ids.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_IDS_H_
  5. #define CARBON_TOOLCHAIN_BASE_VALUE_IDS_H_
  6. #include "common/ostream.h"
  7. #include "llvm/ADT/APFloat.h"
  8. #include "llvm/ADT/APInt.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/Support/YAMLParser.h"
  11. #include "toolchain/base/index_base.h"
  12. namespace Carbon {
  13. // The value of a real literal token.
  14. //
  15. // This is either a dyadic fraction (mantissa * 2^exponent) or a decadic
  16. // fraction (mantissa * 10^exponent).
  17. //
  18. // These values are not canonicalized, because we don't expect them to repeat
  19. // and don't use them in SemIR values.
  20. class Real : public Printable<Real> {
  21. public:
  22. auto Print(llvm::raw_ostream& output_stream) const -> void {
  23. mantissa.print(output_stream, /*isSigned=*/false);
  24. output_stream << "*" << (is_decimal ? "10" : "2") << "^" << exponent;
  25. }
  26. // The mantissa, represented as an unsigned integer.
  27. llvm::APInt mantissa;
  28. // The exponent, represented as a signed integer.
  29. llvm::APInt exponent;
  30. // If false, the value is mantissa * 2^exponent.
  31. // If true, the value is mantissa * 10^exponent.
  32. // TODO: This field increases Real from 32 bytes to 40 bytes. Consider
  33. // changing how it's tracked for space savings.
  34. bool is_decimal;
  35. };
  36. // Corresponds to a float value represented by an APFloat. This is used for
  37. // floating-point values in SemIR.
  38. struct FloatId : public IdBase, public Printable<FloatId> {
  39. using ValueType = llvm::APFloat;
  40. static const FloatId Invalid;
  41. using IdBase::IdBase;
  42. auto Print(llvm::raw_ostream& out) const -> void {
  43. out << "float";
  44. IdBase::Print(out);
  45. }
  46. };
  47. constexpr FloatId FloatId::Invalid(FloatId::InvalidIndex);
  48. // Corresponds to a Real value.
  49. struct RealId : public IdBase, public Printable<RealId> {
  50. using ValueType = Real;
  51. static const RealId Invalid;
  52. using IdBase::IdBase;
  53. auto Print(llvm::raw_ostream& out) const -> void {
  54. out << "real";
  55. IdBase::Print(out);
  56. }
  57. };
  58. constexpr RealId RealId::Invalid(RealId::InvalidIndex);
  59. // Corresponds to StringRefs for identifiers.
  60. //
  61. // `NameId` relies on the values of this type other than `Invalid` all being
  62. // non-negative.
  63. struct IdentifierId : public IdBase, public Printable<IdentifierId> {
  64. using ValueType = llvm::StringRef;
  65. static const IdentifierId Invalid;
  66. using IdBase::IdBase;
  67. auto Print(llvm::raw_ostream& out) const -> void {
  68. out << "identifier";
  69. IdBase::Print(out);
  70. }
  71. };
  72. constexpr IdentifierId IdentifierId::Invalid(IdentifierId::InvalidIndex);
  73. // Corresponds to StringRefs for string literals.
  74. struct StringLiteralValueId : public IdBase,
  75. public Printable<StringLiteralValueId> {
  76. using ValueType = llvm::StringRef;
  77. static const StringLiteralValueId Invalid;
  78. using IdBase::IdBase;
  79. auto Print(llvm::raw_ostream& out) const -> void {
  80. out << "string";
  81. IdBase::Print(out);
  82. }
  83. };
  84. constexpr StringLiteralValueId StringLiteralValueId::Invalid(
  85. StringLiteralValueId::InvalidIndex);
  86. } // namespace Carbon
  87. #endif // CARBON_TOOLCHAIN_BASE_VALUE_IDS_H_