ids.cpp 7.4 KB

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