ids.cpp 7.0 KB

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