value_ids.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/APFloat.h"
  9. #include "llvm/ADT/APInt.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/Support/YAMLParser.h"
  12. #include "toolchain/base/index_base.h"
  13. namespace Carbon {
  14. // The value of a real literal token.
  15. //
  16. // This is either a dyadic fraction (mantissa * 2^exponent) or a decadic
  17. // fraction (mantissa * 10^exponent).
  18. //
  19. // These values are not canonicalized, because we don't expect them to repeat
  20. // and don't use them in SemIR values.
  21. struct Real : public Printable<Real> {
  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<FloatId> {
  39. static constexpr llvm::StringLiteral Label = "float";
  40. using ValueType = llvm::APFloat;
  41. static const FloatId None;
  42. using IdBase::IdBase;
  43. };
  44. constexpr FloatId FloatId::None(FloatId::NoneIndex);
  45. // Corresponds to a Real value.
  46. struct RealId : public IdBase<RealId> {
  47. static constexpr llvm::StringLiteral Label = "real";
  48. using ValueType = Real;
  49. static const RealId None;
  50. using IdBase::IdBase;
  51. };
  52. constexpr RealId RealId::None(RealId::NoneIndex);
  53. // Corresponds to StringRefs for identifiers.
  54. //
  55. // `NameId` relies on the values of this type other than `None` all being
  56. // non-negative.
  57. struct IdentifierId : public IdBase<IdentifierId> {
  58. static constexpr llvm::StringLiteral Label = "identifier";
  59. using ValueType = llvm::StringRef;
  60. static const IdentifierId None;
  61. using IdBase::IdBase;
  62. };
  63. constexpr IdentifierId IdentifierId::None(IdentifierId::NoneIndex);
  64. // The name of a package, which is either an identifier or the special `Core`
  65. // package name.
  66. //
  67. // TODO: Consider also treating `Main` and `Cpp` as special package names.
  68. struct PackageNameId : public IdBase<PackageNameId> {
  69. static constexpr llvm::StringLiteral Label = "package";
  70. static const PackageNameId None;
  71. static const PackageNameId Core;
  72. // Returns the PackageNameId corresponding to a particular IdentifierId.
  73. static auto ForIdentifier(IdentifierId id) -> PackageNameId {
  74. return PackageNameId(id.index);
  75. }
  76. using IdBase::IdBase;
  77. // Returns the IdentifierId corresponding to this PackageNameId, or `None` if
  78. // this is a special package name.
  79. auto AsIdentifierId() const -> IdentifierId {
  80. return index >= 0 ? IdentifierId(index) : IdentifierId::None;
  81. }
  82. // Returns the special package name corresponding to this PackageNameId.
  83. // Requires that this name is not an identifier name.
  84. auto AsSpecialName() const -> llvm::StringLiteral {
  85. if (*this == None) {
  86. return "Main";
  87. }
  88. if (*this == Core) {
  89. return "Core";
  90. }
  91. CARBON_FATAL("Unknown special package name kind {0}", *this);
  92. }
  93. };
  94. constexpr PackageNameId PackageNameId::None(PackageNameId::NoneIndex);
  95. constexpr PackageNameId PackageNameId::Core(PackageNameId::NoneIndex - 1);
  96. // Corresponds to StringRefs for string literals.
  97. struct StringLiteralValueId : public IdBase<StringLiteralValueId> {
  98. static constexpr llvm::StringLiteral Label = "string";
  99. using ValueType = llvm::StringRef;
  100. static const StringLiteralValueId None;
  101. using IdBase::IdBase;
  102. };
  103. constexpr StringLiteralValueId StringLiteralValueId::None(
  104. StringLiteralValueId::NoneIndex);
  105. } // namespace Carbon
  106. #endif // CARBON_TOOLCHAIN_BASE_VALUE_IDS_H_