ids.cpp 7.4 KB

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