ids.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_concrete()) {
  23. if (disambiguate) {
  24. out << "concrete_constant(";
  25. }
  26. out << concrete_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. // Double-check the special value mapping and constexpr evaluation.
  71. static_assert(NameId::SpecialNameId::Vptr == *NameId::Vptr.AsSpecialNameId());
  72. auto NameId::ForIdentifier(IdentifierId id) -> NameId {
  73. if (id.index >= 0) {
  74. return NameId(id.index);
  75. } else if (!id.has_value()) {
  76. return NameId::None;
  77. } else {
  78. CARBON_FATAL("Unexpected identifier ID {0}", id);
  79. }
  80. }
  81. auto NameId::ForPackageName(PackageNameId id) -> NameId {
  82. if (auto identifier_id = id.AsIdentifierId(); identifier_id.has_value()) {
  83. return ForIdentifier(identifier_id);
  84. } else if (id == PackageNameId::Core) {
  85. return NameId::Core;
  86. } else if (!id.has_value()) {
  87. return NameId::None;
  88. } else {
  89. CARBON_FATAL("Unexpected package ID {0}", id);
  90. }
  91. }
  92. auto NameId::Print(llvm::raw_ostream& out) const -> void {
  93. if (!has_value() || index >= 0) {
  94. IdBase::Print(out);
  95. return;
  96. }
  97. out << Label << "(";
  98. auto special_name_id = AsSpecialNameId();
  99. CARBON_CHECK(special_name_id, "Unknown index {0}", index);
  100. switch (*special_name_id) {
  101. #define CARBON_SPECIAL_NAME_ID_FOR_PRINT(Name) \
  102. case SpecialNameId::Name: \
  103. out << #Name; \
  104. break;
  105. CARBON_SPECIAL_NAME_ID(CARBON_SPECIAL_NAME_ID_FOR_PRINT)
  106. #undef CARBON_SPECIAL_NAME_ID_FOR_PRINT
  107. }
  108. out << ")";
  109. }
  110. auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
  111. if (*this == Unreachable) {
  112. out << "unreachable";
  113. } else if (*this == Empty) {
  114. out << Label << "_empty";
  115. } else if (*this == Exports) {
  116. out << "exports";
  117. } else if (*this == ImportRefs) {
  118. out << "import_refs";
  119. } else if (*this == GlobalInit) {
  120. out << "global_init";
  121. } else {
  122. IdBase::Print(out);
  123. }
  124. }
  125. auto TypeId::Print(llvm::raw_ostream& out) const -> void {
  126. out << Label << "(";
  127. if (*this == TypeType::SingletonTypeId) {
  128. out << "TypeType";
  129. } else if (*this == AutoType::SingletonTypeId) {
  130. out << "AutoType";
  131. } else if (*this == ErrorInst::SingletonTypeId) {
  132. out << "Error";
  133. } else {
  134. AsConstantId().Print(out, /*disambiguate=*/false);
  135. }
  136. out << ")";
  137. }
  138. auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
  139. -> LibraryNameId {
  140. CARBON_CHECK(id.index >= NoneIndex, "Unexpected library name ID {0}", id);
  141. if (id == StringLiteralValueId::None) {
  142. // Prior to SemIR, we use `None` to indicate `default`.
  143. return LibraryNameId::Default;
  144. } else {
  145. return LibraryNameId(id.index);
  146. }
  147. }
  148. auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
  149. if (*this == Default) {
  150. out << Label << "Default";
  151. } else if (*this == Error) {
  152. out << Label << "<error>";
  153. } else {
  154. IdBase::Print(out);
  155. }
  156. }
  157. auto LocId::Print(llvm::raw_ostream& out) const -> void {
  158. out << Label << "_";
  159. if (is_node_id() || !has_value()) {
  160. out << node_id();
  161. } else {
  162. out << import_ir_inst_id();
  163. }
  164. }
  165. } // namespace Carbon::SemIR