ids.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "llvm/Support/ConvertUTF.h"
  6. #include "llvm/Support/NativeFormatting.h"
  7. #include "toolchain/base/value_ids.h"
  8. #include "toolchain/sem_ir/singleton_insts.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::SemIR {
  11. auto InstId::Print(llvm::raw_ostream& out) const -> void {
  12. if (IsSingletonInstId(*this)) {
  13. out << Label << "(" << SingletonInstKinds[index] << ")";
  14. } else {
  15. auto [ir_id, simple_index] = IdTag::DecomposeWithBestEffort(index);
  16. if (ir_id == -1) {
  17. IdBase::Print(out);
  18. } else {
  19. out << "ir" << ir_id << ".inst" << simple_index;
  20. }
  21. }
  22. }
  23. auto ConstantId::Print(llvm::raw_ostream& out, bool disambiguate) const
  24. -> void {
  25. if (!has_value()) {
  26. IdBase::Print(out);
  27. return;
  28. }
  29. if (is_concrete()) {
  30. if (disambiguate) {
  31. out << "concrete_constant(";
  32. }
  33. out << concrete_inst_id();
  34. if (disambiguate) {
  35. out << ")";
  36. }
  37. } else if (is_symbolic()) {
  38. out << symbolic_id();
  39. } else {
  40. CARBON_CHECK(!is_constant());
  41. out << "runtime";
  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 CharId::Print(llvm::raw_ostream& out) const -> void {
  62. // TODO: If we switch to C++23, `std::format("`{:?}`")` might be a better
  63. // choice.
  64. out << "U+";
  65. llvm::write_hex(out, index, llvm::HexPrintStyle::Upper, 4);
  66. }
  67. auto IntKind::Print(llvm::raw_ostream& out) const -> void {
  68. if (*this == Unsigned) {
  69. out << "unsigned";
  70. } else if (*this == Signed) {
  71. out << "signed";
  72. } else {
  73. CARBON_FATAL("Invalid int kind value {0}", index);
  74. }
  75. }
  76. static auto FloatKindToStringLiteral(FloatKind kind) -> llvm::StringLiteral {
  77. switch (kind.index) {
  78. case FloatKind::None.index:
  79. return "<none>";
  80. case FloatKind::Binary16.index:
  81. return "f16";
  82. case FloatKind::Binary32.index:
  83. return "f32";
  84. case FloatKind::Binary64.index:
  85. return "f64";
  86. case FloatKind::Binary128.index:
  87. return "f128";
  88. case FloatKind::BFloat16.index:
  89. return "f16_brain";
  90. case FloatKind::X87Float80.index:
  91. return "f80_x87";
  92. case FloatKind::PPCFloat128.index:
  93. return "f128_ppc";
  94. default:
  95. return "<invalid>";
  96. }
  97. }
  98. auto FloatKind::Print(llvm::raw_ostream& out) const -> void {
  99. out << FloatKindToStringLiteral(*this);
  100. }
  101. auto FloatKind::Semantics() const -> const llvm::fltSemantics& {
  102. switch (this->index) {
  103. case Binary16.index:
  104. return llvm::APFloat::IEEEhalf();
  105. case Binary32.index:
  106. return llvm::APFloat::IEEEsingle();
  107. case Binary64.index:
  108. return llvm::APFloat::IEEEdouble();
  109. case Binary128.index:
  110. return llvm::APFloat::IEEEquad();
  111. case BFloat16.index:
  112. return llvm::APFloat::BFloat();
  113. case X87Float80.index:
  114. return llvm::APFloat::x87DoubleExtended();
  115. case PPCFloat128.index:
  116. return llvm::APFloat::PPCDoubleDouble();
  117. default:
  118. CARBON_FATAL("Unexpected float kind {0}", *this);
  119. }
  120. }
  121. // Double-check the special value mapping and constexpr evaluation.
  122. static_assert(NameId::SpecialNameId::Vptr == *NameId::Vptr.AsSpecialNameId());
  123. auto NameId::ForIdentifier(IdentifierId id) -> NameId {
  124. if (id.index >= 0) {
  125. return NameId(id.index);
  126. } else if (!id.has_value()) {
  127. return NameId::None;
  128. } else {
  129. CARBON_FATAL("Unexpected identifier ID {0}", id);
  130. }
  131. }
  132. auto NameId::ForPackageName(PackageNameId id) -> NameId {
  133. if (auto identifier_id = id.AsIdentifierId(); identifier_id.has_value()) {
  134. return ForIdentifier(identifier_id);
  135. } else if (id == PackageNameId::Core) {
  136. return NameId::Core;
  137. } else if (!id.has_value()) {
  138. return NameId::None;
  139. } else {
  140. CARBON_FATAL("Unexpected package ID {0}", id);
  141. }
  142. }
  143. auto NameId::Print(llvm::raw_ostream& out) const -> void {
  144. if (!has_value() || index >= 0) {
  145. IdBase::Print(out);
  146. return;
  147. }
  148. out << Label << "(";
  149. auto special_name_id = AsSpecialNameId();
  150. CARBON_CHECK(special_name_id, "Unknown index {0}", index);
  151. switch (*special_name_id) {
  152. #define CARBON_SPECIAL_NAME_ID_FOR_PRINT(Name) \
  153. case SpecialNameId::Name: \
  154. out << #Name; \
  155. break;
  156. CARBON_SPECIAL_NAME_ID(CARBON_SPECIAL_NAME_ID_FOR_PRINT)
  157. #undef CARBON_SPECIAL_NAME_ID_FOR_PRINT
  158. }
  159. out << ")";
  160. }
  161. auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
  162. if (*this == Unreachable) {
  163. out << "unreachable";
  164. } else if (*this == Empty) {
  165. out << Label << "_empty";
  166. } else if (*this == Exports) {
  167. out << "exports";
  168. } else if (*this == Imports) {
  169. out << "imports";
  170. } else if (*this == GlobalInit) {
  171. out << "global_init";
  172. } else {
  173. IdBase::Print(out);
  174. }
  175. }
  176. auto TypeId::Print(llvm::raw_ostream& out) const -> void {
  177. out << Label << "(";
  178. if (*this == TypeType::TypeId) {
  179. out << "TypeType";
  180. } else if (*this == AutoType::TypeId) {
  181. out << "AutoType";
  182. } else if (*this == ErrorInst::TypeId) {
  183. out << "Error";
  184. } else {
  185. AsConstantId().Print(out, /*disambiguate=*/false);
  186. }
  187. out << ")";
  188. }
  189. auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
  190. -> LibraryNameId {
  191. CARBON_CHECK(id.index >= NoneIndex, "Unexpected library name ID {0}", id);
  192. if (id == StringLiteralValueId::None) {
  193. // Prior to SemIR, we use `None` to indicate `default`.
  194. return LibraryNameId::Default;
  195. } else {
  196. return LibraryNameId(id.index);
  197. }
  198. }
  199. auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
  200. if (*this == Default) {
  201. out << Label << "Default";
  202. } else if (*this == Error) {
  203. out << Label << "<error>";
  204. } else {
  205. IdBase::Print(out);
  206. }
  207. }
  208. auto LocId::Print(llvm::raw_ostream& out) const -> void {
  209. switch (kind()) {
  210. case Kind::None:
  211. IdBase::Print(out);
  212. break;
  213. case Kind::ImportIRInstId:
  214. out << Label << "_" << import_ir_inst_id();
  215. break;
  216. case Kind::InstId:
  217. out << Label << "_" << inst_id();
  218. break;
  219. case Kind::NodeId:
  220. out << Label << "_" << node_id();
  221. if (is_desugared()) {
  222. out << "_desugared";
  223. }
  224. break;
  225. }
  226. }
  227. } // namespace Carbon::SemIR