value_ids.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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. using ValueType = llvm::APFloat;
  40. static const FloatId None;
  41. using IdBase::IdBase;
  42. };
  43. constexpr FloatId FloatId::None(FloatId::NoneIndex);
  44. // Corresponds to a Real value.
  45. struct RealId : public IdBase<RealId> {
  46. static constexpr llvm::StringLiteral Label = "real";
  47. using ValueType = Real;
  48. static const RealId None;
  49. using IdBase::IdBase;
  50. };
  51. constexpr RealId RealId::None(RealId::NoneIndex);
  52. // Corresponds to StringRefs for identifiers.
  53. //
  54. // `NameId` relies on the values of this type other than `None` all being
  55. // non-negative.
  56. struct IdentifierId : public IdBase<IdentifierId> {
  57. static constexpr llvm::StringLiteral Label = "identifier";
  58. using ValueType = llvm::StringRef;
  59. static const IdentifierId None;
  60. using IdBase::IdBase;
  61. };
  62. constexpr IdentifierId IdentifierId::None(IdentifierId::NoneIndex);
  63. // Corresponds to StringRefs for string literals.
  64. struct StringLiteralValueId : public IdBase<StringLiteralValueId> {
  65. static constexpr llvm::StringLiteral Label = "string";
  66. using ValueType = llvm::StringRef;
  67. static const StringLiteralValueId None;
  68. using IdBase::IdBase;
  69. };
  70. constexpr StringLiteralValueId StringLiteralValueId::None(
  71. StringLiteralValueId::NoneIndex);
  72. } // namespace Carbon
  73. #endif // CARBON_TOOLCHAIN_BASE_VALUE_IDS_H_