ids.cpp 7.3 KB

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