ids.cpp 6.8 KB

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