value_ids.h 4.7 KB

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