ids.cpp 4.3 KB

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