ids.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "toolchain/sem_ir/ids.h"
  5. #include "toolchain/sem_ir/singleton_insts.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. namespace Carbon::SemIR {
  8. auto InstId::Print(llvm::raw_ostream& out) const -> void {
  9. if (!is_valid()) {
  10. IdBase::Print(out);
  11. return;
  12. }
  13. out << Label;
  14. if (index < static_cast<int>(SingletonInstKinds.size())) {
  15. out << "(" << SingletonInstKinds[index] << ")";
  16. } else {
  17. // Use the `+` as a small reminder that this is a delta, rather than an
  18. // absolute index.
  19. out << "+" << index - SingletonInstKinds.size();
  20. }
  21. }
  22. auto ConstantId::Print(llvm::raw_ostream& out, bool disambiguate) const
  23. -> void {
  24. if (!is_valid()) {
  25. IdBase::Print(out);
  26. return;
  27. }
  28. if (is_template()) {
  29. if (disambiguate) {
  30. out << "template_constant(";
  31. }
  32. out << template_inst_id();
  33. if (disambiguate) {
  34. out << ")";
  35. }
  36. } else if (is_symbolic()) {
  37. out << "symbolic_constant" << symbolic_index();
  38. } else {
  39. CARBON_CHECK(!is_constant());
  40. out << "runtime";
  41. }
  42. }
  43. auto RuntimeParamIndex::Print(llvm::raw_ostream& out) const -> void {
  44. if (*this == Unknown) {
  45. out << Label << "<unknown>";
  46. } else {
  47. IndexBase::Print(out);
  48. }
  49. }
  50. auto GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
  51. out << "generic_inst";
  52. if (is_valid()) {
  53. out << (region() == Declaration ? "_in_decl" : "_in_def") << index();
  54. } else {
  55. out << "<invalid>";
  56. }
  57. }
  58. auto BoolValue::Print(llvm::raw_ostream& out) const -> void {
  59. if (*this == False) {
  60. out << "false";
  61. } else if (*this == True) {
  62. out << "true";
  63. } else {
  64. CARBON_FATAL("Invalid bool value {0}", index);
  65. }
  66. }
  67. auto IntKind::Print(llvm::raw_ostream& out) const -> void {
  68. if (*this == Unsigned) {
  69. out << "unsigned";
  70. } else if (*this == Signed) {
  71. out << "signed";
  72. } else {
  73. CARBON_FATAL("Invalid int kind value {0}", index);
  74. }
  75. }
  76. auto NameId::ForIdentifier(IdentifierId id) -> NameId {
  77. if (id.index >= 0) {
  78. return NameId(id.index);
  79. } else if (!id.is_valid()) {
  80. return NameId::Invalid;
  81. } else {
  82. CARBON_FATAL("Unexpected identifier ID {0}", id);
  83. }
  84. }
  85. auto NameId::Print(llvm::raw_ostream& out) const -> void {
  86. if (!is_valid() || index >= 0) {
  87. IdBase::Print(out);
  88. return;
  89. }
  90. out << Label << "(";
  91. if (*this == SelfValue) {
  92. out << "SelfValue";
  93. } else if (*this == SelfType) {
  94. out << "SelfType";
  95. } else if (*this == PeriodSelf) {
  96. out << "PeriodSelf";
  97. } else if (*this == ReturnSlot) {
  98. out << "ReturnSlot";
  99. } else if (*this == PackageNamespace) {
  100. out << "PackageNamespace";
  101. } else if (*this == Base) {
  102. out << "Base";
  103. } else {
  104. CARBON_FATAL("Unknown index {0}", index);
  105. IdBase::Print(out);
  106. }
  107. out << ")";
  108. }
  109. auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
  110. if (*this == Unreachable) {
  111. out << "unreachable";
  112. } else if (*this == Empty) {
  113. out << Label << "_empty";
  114. } else if (*this == Exports) {
  115. out << "exports";
  116. } else if (*this == ImportRefs) {
  117. out << "import_refs";
  118. } else if (*this == GlobalInit) {
  119. out << "global_init";
  120. } else {
  121. IdBase::Print(out);
  122. }
  123. }
  124. auto TypeId::Print(llvm::raw_ostream& out) const -> void {
  125. out << Label << "(";
  126. if (*this == TypeType::SingletonTypeId) {
  127. out << "TypeType";
  128. } else if (*this == AutoType::SingletonTypeId) {
  129. out << "AutoType";
  130. } else if (*this == ErrorInst::SingletonTypeId) {
  131. out << "Error";
  132. } else {
  133. AsConstantId().Print(out, /*disambiguate=*/false);
  134. }
  135. out << ")";
  136. }
  137. auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
  138. -> LibraryNameId {
  139. CARBON_CHECK(id.index >= InvalidIndex, "Unexpected library name ID {0}", id);
  140. if (id == StringLiteralValueId::Invalid) {
  141. // Prior to SemIR, we use invalid to indicate `default`.
  142. return LibraryNameId::Default;
  143. } else {
  144. return LibraryNameId(id.index);
  145. }
  146. }
  147. auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
  148. if (*this == Default) {
  149. out << Label << "Default";
  150. } else if (*this == Error) {
  151. out << Label << "<error>";
  152. } else {
  153. IdBase::Print(out);
  154. }
  155. }
  156. auto LocId::Print(llvm::raw_ostream& out) const -> void {
  157. out << Label << "_";
  158. if (is_node_id() || !is_valid()) {
  159. out << node_id();
  160. } else {
  161. out << import_ir_inst_id();
  162. }
  163. }
  164. } // namespace Carbon::SemIR