ids.cpp 4.2 KB

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