ids.cpp 4.7 KB

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