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 GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
  38. out << "generic_inst";
  39. if (has_value()) {
  40. out << (region() == Declaration ? "_in_decl" : "_in_def") << index();
  41. } else {
  42. out << "<none>";
  43. }
  44. }
  45. auto BoolValue::Print(llvm::raw_ostream& out) const -> void {
  46. if (*this == False) {
  47. out << "false";
  48. } else if (*this == True) {
  49. out << "true";
  50. } else {
  51. CARBON_FATAL("Invalid bool value {0}", index);
  52. }
  53. }
  54. auto IntKind::Print(llvm::raw_ostream& out) const -> void {
  55. if (*this == Unsigned) {
  56. out << "unsigned";
  57. } else if (*this == Signed) {
  58. out << "signed";
  59. } else {
  60. CARBON_FATAL("Invalid int kind value {0}", index);
  61. }
  62. }
  63. // Double-check the special value mapping and constexpr evaluation.
  64. static_assert(NameId::SpecialNameId::Vptr == *NameId::Vptr.AsSpecialNameId());
  65. auto NameId::ForIdentifier(IdentifierId id) -> NameId {
  66. if (id.index >= 0) {
  67. return NameId(id.index);
  68. } else if (!id.has_value()) {
  69. return NameId::None;
  70. } else {
  71. CARBON_FATAL("Unexpected identifier ID {0}", id);
  72. }
  73. }
  74. auto NameId::ForPackageName(PackageNameId id) -> NameId {
  75. if (auto identifier_id = id.AsIdentifierId(); identifier_id.has_value()) {
  76. return ForIdentifier(identifier_id);
  77. } else if (id == PackageNameId::Core) {
  78. return NameId::Core;
  79. } else if (!id.has_value()) {
  80. return NameId::None;
  81. } else {
  82. CARBON_FATAL("Unexpected package ID {0}", id);
  83. }
  84. }
  85. auto NameId::Print(llvm::raw_ostream& out) const -> void {
  86. if (!has_value() || index >= 0) {
  87. IdBase::Print(out);
  88. return;
  89. }
  90. out << Label << "(";
  91. auto special_name_id = AsSpecialNameId();
  92. CARBON_CHECK(special_name_id, "Unknown index {0}", index);
  93. switch (*special_name_id) {
  94. #define CARBON_SPECIAL_NAME_ID_FOR_PRINT(Name) \
  95. case SpecialNameId::Name: \
  96. out << #Name; \
  97. break;
  98. CARBON_SPECIAL_NAME_ID(CARBON_SPECIAL_NAME_ID_FOR_PRINT)
  99. #undef CARBON_SPECIAL_NAME_ID_FOR_PRINT
  100. }
  101. out << ")";
  102. }
  103. auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
  104. if (*this == Unreachable) {
  105. out << "unreachable";
  106. } else if (*this == Empty) {
  107. out << Label << "_empty";
  108. } else if (*this == Exports) {
  109. out << "exports";
  110. } else if (*this == ImportRefs) {
  111. out << "import_refs";
  112. } else if (*this == GlobalInit) {
  113. out << "global_init";
  114. } else {
  115. IdBase::Print(out);
  116. }
  117. }
  118. auto TypeId::Print(llvm::raw_ostream& out) const -> void {
  119. out << Label << "(";
  120. if (*this == TypeType::SingletonTypeId) {
  121. out << "TypeType";
  122. } else if (*this == AutoType::SingletonTypeId) {
  123. out << "AutoType";
  124. } else if (*this == ErrorInst::SingletonTypeId) {
  125. out << "Error";
  126. } else {
  127. AsConstantId().Print(out, /*disambiguate=*/false);
  128. }
  129. out << ")";
  130. }
  131. auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
  132. -> LibraryNameId {
  133. CARBON_CHECK(id.index >= NoneIndex, "Unexpected library name ID {0}", id);
  134. if (id == StringLiteralValueId::None) {
  135. // Prior to SemIR, we use `None` to indicate `default`.
  136. return LibraryNameId::Default;
  137. } else {
  138. return LibraryNameId(id.index);
  139. }
  140. }
  141. auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
  142. if (*this == Default) {
  143. out << Label << "Default";
  144. } else if (*this == Error) {
  145. out << Label << "<error>";
  146. } else {
  147. IdBase::Print(out);
  148. }
  149. }
  150. auto LocId::Print(llvm::raw_ostream& out) const -> void {
  151. switch (kind()) {
  152. case Kind::None:
  153. IdBase::Print(out);
  154. break;
  155. case Kind::ImportIRInstId:
  156. out << Label << "_" << import_ir_inst_id();
  157. break;
  158. case Kind::InstId:
  159. out << Label << "_" << inst_id();
  160. break;
  161. case Kind::NodeId:
  162. out << Label << "_" << node_id();
  163. break;
  164. }
  165. }
  166. } // namespace Carbon::SemIR