value_ids.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/check.h"
  7. #include "common/ostream.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. struct Real : public Printable<Real> {
  21. auto Print(llvm::raw_ostream& output_stream) const -> void {
  22. mantissa.print(output_stream, /*isSigned=*/false);
  23. output_stream << "*" << (is_decimal ? "10" : "2") << "^" << exponent;
  24. }
  25. // The mantissa, represented as an unsigned integer.
  26. llvm::APInt mantissa;
  27. // The exponent, represented as a signed integer.
  28. llvm::APInt exponent;
  29. // If false, the value is mantissa * 2^exponent.
  30. // If true, the value is mantissa * 10^exponent.
  31. // TODO: This field increases Real from 32 bytes to 40 bytes. Consider
  32. // changing how it's tracked for space savings.
  33. bool is_decimal;
  34. };
  35. // Corresponds to a float value represented by an APFloat. This is used for
  36. // floating-point values in SemIR.
  37. struct FloatId : public IdBase<FloatId> {
  38. static constexpr llvm::StringLiteral Label = "float";
  39. static const FloatId None;
  40. using IdBase::IdBase;
  41. };
  42. constexpr FloatId FloatId::None(FloatId::NoneIndex);
  43. // Corresponds to a Real value.
  44. struct RealId : public IdBase<RealId> {
  45. static constexpr llvm::StringLiteral Label = "real";
  46. static const RealId None;
  47. using IdBase::IdBase;
  48. };
  49. constexpr RealId RealId::None(RealId::NoneIndex);
  50. // Corresponds to StringRefs for identifiers.
  51. //
  52. // `NameId` relies on the values of this type other than `None` all being
  53. // non-negative.
  54. struct IdentifierId : public IdBase<IdentifierId> {
  55. static constexpr llvm::StringLiteral Label = "identifier";
  56. static const IdentifierId None;
  57. using IdBase::IdBase;
  58. };
  59. constexpr IdentifierId IdentifierId::None(IdentifierId::NoneIndex);
  60. // The name of a package, which is either an identifier or the special `Core`
  61. // package name.
  62. //
  63. // TODO: Consider also treating `Main` and `Cpp` as special package names.
  64. struct PackageNameId : public IdBase<PackageNameId> {
  65. static constexpr llvm::StringLiteral Label = "package";
  66. static const PackageNameId None;
  67. static const PackageNameId Core;
  68. // Returns the PackageNameId corresponding to a particular IdentifierId.
  69. static auto ForIdentifier(IdentifierId id) -> PackageNameId {
  70. return PackageNameId(id.index);
  71. }
  72. using IdBase::IdBase;
  73. // Returns the IdentifierId corresponding to this PackageNameId, or `None` if
  74. // this is a special package name.
  75. auto AsIdentifierId() const -> IdentifierId {
  76. return index >= 0 ? IdentifierId(index) : IdentifierId::None;
  77. }
  78. // Returns the special package name corresponding to this PackageNameId.
  79. // Requires that this name is not an identifier name.
  80. auto AsSpecialName() const -> llvm::StringLiteral {
  81. if (*this == None) {
  82. return "Main";
  83. }
  84. if (*this == Core) {
  85. return "Core";
  86. }
  87. CARBON_FATAL("Unknown special package name kind {0}", *this);
  88. }
  89. };
  90. constexpr PackageNameId PackageNameId::None(PackageNameId::NoneIndex);
  91. constexpr PackageNameId PackageNameId::Core(PackageNameId::NoneIndex - 1);
  92. // Corresponds to StringRefs for string literals.
  93. struct StringLiteralValueId : public IdBase<StringLiteralValueId> {
  94. static constexpr llvm::StringLiteral Label = "string";
  95. static const StringLiteralValueId None;
  96. using IdBase::IdBase;
  97. };
  98. constexpr StringLiteralValueId StringLiteralValueId::None(
  99. StringLiteralValueId::NoneIndex);
  100. } // namespace Carbon
  101. #endif // CARBON_TOOLCHAIN_BASE_VALUE_IDS_H_