file.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/file.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/STLExtras.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/base/kind_switch.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/base/yaml.h"
  11. #include "toolchain/parse/node_ids.h"
  12. #include "toolchain/sem_ir/builtin_kind.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/inst.h"
  15. #include "toolchain/sem_ir/inst_kind.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::SemIR {
  18. auto Function::GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  19. -> std::pair<InstId, Param> {
  20. auto ref = sem_ir.insts().Get(param_ref_id);
  21. if (auto addr_pattern = ref.TryAs<SemIR::AddrPattern>()) {
  22. param_ref_id = addr_pattern->inner_id;
  23. ref = sem_ir.insts().Get(param_ref_id);
  24. }
  25. if (auto bind_name = ref.TryAs<SemIR::AnyBindName>()) {
  26. param_ref_id = bind_name->value_id;
  27. ref = sem_ir.insts().Get(param_ref_id);
  28. }
  29. return {param_ref_id, ref.As<SemIR::Param>()};
  30. }
  31. auto ValueRepr::Print(llvm::raw_ostream& out) const -> void {
  32. out << "{kind: ";
  33. switch (kind) {
  34. case Unknown:
  35. out << "unknown";
  36. break;
  37. case None:
  38. out << "none";
  39. break;
  40. case Copy:
  41. out << "copy";
  42. break;
  43. case Pointer:
  44. out << "pointer";
  45. break;
  46. case Custom:
  47. out << "custom";
  48. break;
  49. }
  50. out << ", type: " << type_id << "}";
  51. }
  52. auto TypeInfo::Print(llvm::raw_ostream& out) const -> void {
  53. out << "{constant: " << constant_id << ", value_rep: " << value_repr << "}";
  54. }
  55. File::File(SharedValueStores& value_stores, std::string filename,
  56. const File* builtins, llvm::function_ref<void()> init_builtins)
  57. : value_stores_(&value_stores),
  58. filename_(std::move(filename)),
  59. type_blocks_(allocator_),
  60. constant_values_(ConstantId::NotConstant),
  61. inst_blocks_(allocator_),
  62. constants_(*this, allocator_) {
  63. CARBON_CHECK(builtins != nullptr);
  64. auto builtins_id =
  65. import_irs_.Add({.node_id = Parse::NodeId::Invalid, .sem_ir = builtins});
  66. CARBON_CHECK(builtins_id == ImportIRId::Builtins)
  67. << "Builtins must be the first IR";
  68. auto api_placeholder_id =
  69. import_irs_.Add({.node_id = Parse::NodeId::Invalid, .sem_ir = nullptr});
  70. CARBON_CHECK(api_placeholder_id == ImportIRId::ApiForImpl)
  71. << "ApiForImpl must be the second IR";
  72. insts_.Reserve(BuiltinKind::ValidCount);
  73. init_builtins();
  74. CARBON_CHECK(insts_.size() == BuiltinKind::ValidCount)
  75. << "Builtins should produce " << BuiltinKind::ValidCount
  76. << " insts, actual: " << insts_.size();
  77. for (auto i : llvm::seq(BuiltinKind::ValidCount)) {
  78. auto builtin_id = SemIR::InstId(i);
  79. constant_values_.Set(builtin_id,
  80. SemIR::ConstantId::ForTemplateConstant(builtin_id));
  81. }
  82. }
  83. File::File(SharedValueStores& value_stores)
  84. : File(value_stores, "<builtins>", this, [&]() {
  85. // Error uses a self-referential type so that it's not accidentally treated as
  86. // a normal type. Every other builtin is a type, including the
  87. // self-referential TypeType.
  88. #define CARBON_SEM_IR_BUILTIN_KIND(Name, ...) \
  89. insts_.AddInNoBlock( \
  90. {Builtin{BuiltinKind::Name == BuiltinKind::Error ? TypeId::Error \
  91. : TypeId::TypeType, \
  92. BuiltinKind::Name}});
  93. #include "toolchain/sem_ir/builtin_kind.def"
  94. }) {
  95. }
  96. File::File(SharedValueStores& value_stores, std::string filename,
  97. const File* builtins)
  98. : File(value_stores, filename, builtins, [&]() {
  99. for (auto [i, inst] : llvm::enumerate(builtins->insts_.array_ref())) {
  100. // We can reuse the type_id from the builtin IR's inst because they're
  101. // special-cased values.
  102. auto import_ir_inst_id = import_ir_insts_.Add(
  103. {.ir_id = ImportIRId::Builtins, .inst_id = SemIR::InstId(i)});
  104. insts_.AddInNoBlock(
  105. ImportRefLoaded{inst.type_id(), import_ir_inst_id});
  106. }
  107. }) {}
  108. auto File::Verify() const -> ErrorOr<Success> {
  109. // Invariants don't necessarily hold for invalid IR.
  110. if (has_errors_) {
  111. return Success();
  112. }
  113. // Check that every code block has a terminator sequence that appears at the
  114. // end of the block.
  115. for (const Function& function : functions_.array_ref()) {
  116. for (InstBlockId block_id : function.body_block_ids) {
  117. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  118. for (InstId inst_id : inst_blocks().Get(block_id)) {
  119. TerminatorKind inst_kind =
  120. insts().Get(inst_id).kind().terminator_kind();
  121. if (prior_kind == TerminatorKind::Terminator) {
  122. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  123. inst_id, block_id));
  124. }
  125. if (prior_kind > inst_kind) {
  126. return Error(
  127. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  128. "terminator sequence",
  129. inst_id, block_id));
  130. }
  131. prior_kind = inst_kind;
  132. }
  133. if (prior_kind != TerminatorKind::Terminator) {
  134. return Error(llvm::formatv("No terminator in block {0}", block_id));
  135. }
  136. }
  137. }
  138. // TODO: Check that an instruction only references other instructions that are
  139. // either global or that dominate it.
  140. return Success();
  141. }
  142. auto File::OutputYaml(bool include_builtins) const -> Yaml::OutputMapping {
  143. return Yaml::OutputMapping([this,
  144. include_builtins](Yaml::OutputMapping::Map map) {
  145. map.Add("filename", filename_);
  146. map.Add(
  147. "sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  148. map.Add("import_irs_size", Yaml::OutputScalar(import_irs_.size()));
  149. map.Add("name_scopes", name_scopes_.OutputYaml());
  150. map.Add("bind_names", bind_names_.OutputYaml());
  151. map.Add("functions", functions_.OutputYaml());
  152. map.Add("classes", classes_.OutputYaml());
  153. map.Add("types", types_.OutputYaml());
  154. map.Add("type_blocks", type_blocks_.OutputYaml());
  155. map.Add("insts",
  156. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  157. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  158. for (int i : llvm::seq(start, insts_.size())) {
  159. auto id = InstId(i);
  160. map.Add(PrintToString(id),
  161. Yaml::OutputScalar(insts_.Get(id)));
  162. }
  163. }));
  164. map.Add("constant_values",
  165. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  166. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  167. for (int i : llvm::seq(start, insts_.size())) {
  168. auto id = InstId(i);
  169. auto value = constant_values_.Get(id);
  170. if (!value.is_valid() || value.is_constant()) {
  171. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  172. }
  173. }
  174. }));
  175. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  176. }));
  177. });
  178. }
  179. // Map an instruction kind representing a type into an integer describing the
  180. // precedence of that type's syntax. Higher numbers correspond to higher
  181. // precedence.
  182. static auto GetTypePrecedence(InstKind kind) -> int {
  183. switch (kind) {
  184. case ArrayType::Kind:
  185. case AssociatedEntityType::Kind:
  186. case BindAlias::Kind:
  187. case BindSymbolicName::Kind:
  188. case Builtin::Kind:
  189. case ClassType::Kind:
  190. case FacetTypeAccess::Kind:
  191. case ImportRefLoaded::Kind:
  192. case ImportRefUsed::Kind:
  193. case InterfaceType::Kind:
  194. case IntType::Kind:
  195. case NameRef::Kind:
  196. case StructType::Kind:
  197. case TupleType::Kind:
  198. case UnboundElementType::Kind:
  199. return 0;
  200. case ConstType::Kind:
  201. return -1;
  202. case PointerType::Kind:
  203. return -2;
  204. case AdaptDecl::Kind:
  205. case AddrOf::Kind:
  206. case AddrPattern::Kind:
  207. case ArrayIndex::Kind:
  208. case ArrayInit::Kind:
  209. case Assign::Kind:
  210. case AssociatedConstantDecl::Kind:
  211. case AssociatedEntity::Kind:
  212. case BaseDecl::Kind:
  213. case BindName::Kind:
  214. case BindValue::Kind:
  215. case BlockArg::Kind:
  216. case BoolLiteral::Kind:
  217. case BoundMethod::Kind:
  218. case Branch::Kind:
  219. case BranchIf::Kind:
  220. case BranchWithArg::Kind:
  221. case Call::Kind:
  222. case ClassDecl::Kind:
  223. case ClassElementAccess::Kind:
  224. case ClassInit::Kind:
  225. case Converted::Kind:
  226. case Deref::Kind:
  227. case FieldDecl::Kind:
  228. case FunctionDecl::Kind:
  229. case ImplDecl::Kind:
  230. case ImportRefUnloaded::Kind:
  231. case InitializeFrom::Kind:
  232. case InterfaceDecl::Kind:
  233. case InterfaceWitness::Kind:
  234. case InterfaceWitnessAccess::Kind:
  235. case IntLiteral::Kind:
  236. case Namespace::Kind:
  237. case Param::Kind:
  238. case RealLiteral::Kind:
  239. case Return::Kind:
  240. case ReturnExpr::Kind:
  241. case SpliceBlock::Kind:
  242. case StringLiteral::Kind:
  243. case StructAccess::Kind:
  244. case StructTypeField::Kind:
  245. case StructLiteral::Kind:
  246. case StructInit::Kind:
  247. case StructValue::Kind:
  248. case Temporary::Kind:
  249. case TemporaryStorage::Kind:
  250. case TupleAccess::Kind:
  251. case TupleIndex::Kind:
  252. case TupleLiteral::Kind:
  253. case TupleInit::Kind:
  254. case TupleValue::Kind:
  255. case UnaryOperatorNot::Kind:
  256. case ValueAsRef::Kind:
  257. case ValueOfInitializer::Kind:
  258. case VarStorage::Kind:
  259. CARBON_FATAL() << "GetTypePrecedence for non-type inst kind " << kind;
  260. }
  261. }
  262. // Implements File::StringifyTypeExpr. Static to prevent accidental use of
  263. // member functions while traversing IRs.
  264. static auto StringifyTypeExprImpl(const SemIR::File& outer_sem_ir,
  265. InstId outer_inst_id) {
  266. std::string str;
  267. llvm::raw_string_ostream out(str);
  268. struct Step {
  269. // The instruction's file.
  270. const File& sem_ir;
  271. // The instruction to print.
  272. InstId inst_id;
  273. // The index into inst_id to print. Not used by all types.
  274. int index = 0;
  275. auto Next() const -> Step {
  276. return {.sem_ir = sem_ir, .inst_id = inst_id, .index = index + 1};
  277. }
  278. };
  279. llvm::SmallVector<Step> steps = {
  280. Step{.sem_ir = outer_sem_ir, .inst_id = outer_inst_id}};
  281. while (!steps.empty()) {
  282. auto step = steps.pop_back_val();
  283. if (!step.inst_id.is_valid()) {
  284. out << "<invalid type>";
  285. continue;
  286. }
  287. // Builtins have designated labels.
  288. if (step.inst_id.is_builtin()) {
  289. out << step.inst_id.builtin_kind().label();
  290. continue;
  291. }
  292. const auto& sem_ir = step.sem_ir;
  293. // Helper for instructions with the current sem_ir.
  294. auto push_inst_id = [&](InstId inst_id) {
  295. steps.push_back({.sem_ir = sem_ir, .inst_id = inst_id});
  296. };
  297. auto untyped_inst = sem_ir.insts().Get(step.inst_id);
  298. CARBON_KIND_SWITCH(untyped_inst) {
  299. case CARBON_KIND(ArrayType inst): {
  300. if (step.index == 0) {
  301. out << "[";
  302. steps.push_back(step.Next());
  303. push_inst_id(sem_ir.types().GetInstId(inst.element_type_id));
  304. } else if (step.index == 1) {
  305. out << "; " << sem_ir.GetArrayBoundValue(inst.bound_id) << "]";
  306. }
  307. break;
  308. }
  309. case CARBON_KIND(AssociatedEntityType inst): {
  310. if (step.index == 0) {
  311. out << "<associated ";
  312. steps.push_back(step.Next());
  313. push_inst_id(sem_ir.types().GetInstId(inst.entity_type_id));
  314. } else {
  315. auto interface_name_id =
  316. sem_ir.interfaces().Get(inst.interface_id).name_id;
  317. out << " in " << sem_ir.names().GetFormatted(interface_name_id)
  318. << ">";
  319. }
  320. break;
  321. }
  322. case BindAlias::Kind:
  323. case BindSymbolicName::Kind: {
  324. auto name_id = untyped_inst.As<AnyBindName>().bind_name_id;
  325. out << sem_ir.names().GetFormatted(
  326. sem_ir.bind_names().Get(name_id).name_id);
  327. break;
  328. }
  329. case CARBON_KIND(ClassType inst): {
  330. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  331. out << sem_ir.names().GetFormatted(class_name_id);
  332. break;
  333. }
  334. case CARBON_KIND(ConstType inst): {
  335. if (step.index == 0) {
  336. out << "const ";
  337. // Add parentheses if required.
  338. auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
  339. if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
  340. GetTypePrecedence(SemIR::ConstType::Kind)) {
  341. out << "(";
  342. steps.push_back(step.Next());
  343. }
  344. push_inst_id(inner_type_inst_id);
  345. } else if (step.index == 1) {
  346. out << ")";
  347. }
  348. break;
  349. }
  350. case CARBON_KIND(FacetTypeAccess inst): {
  351. // Print `T as type` as simply `T`.
  352. push_inst_id(inst.facet_id);
  353. break;
  354. }
  355. case ImportRefLoaded::Kind:
  356. case ImportRefUsed::Kind: {
  357. auto import_ir_inst = sem_ir.import_ir_insts().Get(
  358. untyped_inst.As<AnyImportRef>().import_ir_inst_id);
  359. steps.push_back(
  360. {.sem_ir = *sem_ir.import_irs().Get(import_ir_inst.ir_id).sem_ir,
  361. .inst_id = import_ir_inst.inst_id});
  362. break;
  363. }
  364. case CARBON_KIND(InterfaceType inst): {
  365. auto interface_name_id =
  366. sem_ir.interfaces().Get(inst.interface_id).name_id;
  367. out << sem_ir.names().GetFormatted(interface_name_id);
  368. break;
  369. }
  370. case CARBON_KIND(IntType inst): {
  371. if (step.index == 1) {
  372. out << ")";
  373. } else if (auto width_value =
  374. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  375. out << (inst.int_kind.is_signed() ? "i" : "u");
  376. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  377. } else {
  378. out << (inst.int_kind.is_signed() ? "Core.Int(" : "Core.UInt(");
  379. steps.push_back(step.Next());
  380. push_inst_id(inst.bit_width_id);
  381. }
  382. break;
  383. }
  384. case CARBON_KIND(NameRef inst): {
  385. out << sem_ir.names().GetFormatted(inst.name_id);
  386. break;
  387. }
  388. case CARBON_KIND(PointerType inst): {
  389. if (step.index == 0) {
  390. steps.push_back(step.Next());
  391. push_inst_id(sem_ir.types().GetInstId(inst.pointee_id));
  392. } else if (step.index == 1) {
  393. out << "*";
  394. }
  395. break;
  396. }
  397. case CARBON_KIND(StructType inst): {
  398. auto refs = sem_ir.inst_blocks().Get(inst.fields_id);
  399. if (refs.empty()) {
  400. out << "{}";
  401. break;
  402. } else if (step.index == 0) {
  403. out << "{";
  404. } else if (step.index < static_cast<int>(refs.size())) {
  405. out << ", ";
  406. } else {
  407. out << "}";
  408. break;
  409. }
  410. steps.push_back(step.Next());
  411. push_inst_id(refs[step.index]);
  412. break;
  413. }
  414. case CARBON_KIND(StructTypeField inst): {
  415. out << "." << sem_ir.names().GetFormatted(inst.name_id) << ": ";
  416. push_inst_id(sem_ir.types().GetInstId(inst.field_type_id));
  417. break;
  418. }
  419. case CARBON_KIND(TupleType inst): {
  420. auto refs = sem_ir.type_blocks().Get(inst.elements_id);
  421. if (refs.empty()) {
  422. out << "()";
  423. break;
  424. } else if (step.index == 0) {
  425. out << "(";
  426. } else if (step.index < static_cast<int>(refs.size())) {
  427. out << ", ";
  428. } else {
  429. // A tuple of one element has a comma to disambiguate from an
  430. // expression.
  431. if (step.index == 1) {
  432. out << ",";
  433. }
  434. out << ")";
  435. break;
  436. }
  437. steps.push_back(step.Next());
  438. push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
  439. break;
  440. }
  441. case CARBON_KIND(UnboundElementType inst): {
  442. if (step.index == 0) {
  443. out << "<unbound element of class ";
  444. steps.push_back(step.Next());
  445. push_inst_id(sem_ir.types().GetInstId(inst.class_type_id));
  446. } else {
  447. out << ">";
  448. }
  449. break;
  450. }
  451. case AdaptDecl::Kind:
  452. case AddrOf::Kind:
  453. case AddrPattern::Kind:
  454. case ArrayIndex::Kind:
  455. case ArrayInit::Kind:
  456. case Assign::Kind:
  457. case AssociatedConstantDecl::Kind:
  458. case AssociatedEntity::Kind:
  459. case BaseDecl::Kind:
  460. case BindName::Kind:
  461. case BindValue::Kind:
  462. case BlockArg::Kind:
  463. case BoolLiteral::Kind:
  464. case BoundMethod::Kind:
  465. case Branch::Kind:
  466. case BranchIf::Kind:
  467. case BranchWithArg::Kind:
  468. case Builtin::Kind:
  469. case Call::Kind:
  470. case ClassDecl::Kind:
  471. case ClassElementAccess::Kind:
  472. case ClassInit::Kind:
  473. case Converted::Kind:
  474. case Deref::Kind:
  475. case FieldDecl::Kind:
  476. case FunctionDecl::Kind:
  477. case ImplDecl::Kind:
  478. case ImportRefUnloaded::Kind:
  479. case InitializeFrom::Kind:
  480. case InterfaceDecl::Kind:
  481. case InterfaceWitness::Kind:
  482. case InterfaceWitnessAccess::Kind:
  483. case IntLiteral::Kind:
  484. case Namespace::Kind:
  485. case Param::Kind:
  486. case RealLiteral::Kind:
  487. case Return::Kind:
  488. case ReturnExpr::Kind:
  489. case SpliceBlock::Kind:
  490. case StringLiteral::Kind:
  491. case StructAccess::Kind:
  492. case StructLiteral::Kind:
  493. case StructInit::Kind:
  494. case StructValue::Kind:
  495. case Temporary::Kind:
  496. case TemporaryStorage::Kind:
  497. case TupleAccess::Kind:
  498. case TupleIndex::Kind:
  499. case TupleLiteral::Kind:
  500. case TupleInit::Kind:
  501. case TupleValue::Kind:
  502. case UnaryOperatorNot::Kind:
  503. case ValueAsRef::Kind:
  504. case ValueOfInitializer::Kind:
  505. case VarStorage::Kind:
  506. // We don't need to handle stringification for instructions that don't
  507. // show up in errors, but make it clear what's going on so that it's
  508. // clearer when stringification is needed.
  509. out << "<cannot stringify " << step.inst_id << ">";
  510. break;
  511. }
  512. }
  513. return str;
  514. }
  515. auto File::StringifyType(TypeId type_id) const -> std::string {
  516. return StringifyTypeExprImpl(*this, types().GetInstId(type_id));
  517. }
  518. auto File::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  519. return StringifyTypeExprImpl(*this, outer_inst_id);
  520. }
  521. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  522. const File* ir = &file;
  523. // The overall expression category if the current instruction is a value
  524. // expression.
  525. ExprCategory value_category = ExprCategory::Value;
  526. while (true) {
  527. auto untyped_inst = ir->insts().Get(inst_id);
  528. CARBON_KIND_SWITCH(untyped_inst) {
  529. case AdaptDecl::Kind:
  530. case Assign::Kind:
  531. case BaseDecl::Kind:
  532. case Branch::Kind:
  533. case BranchIf::Kind:
  534. case BranchWithArg::Kind:
  535. case FieldDecl::Kind:
  536. case FunctionDecl::Kind:
  537. case ImplDecl::Kind:
  538. case ImportRefUnloaded::Kind:
  539. case Namespace::Kind:
  540. case Return::Kind:
  541. case ReturnExpr::Kind:
  542. case StructTypeField::Kind:
  543. return ExprCategory::NotExpr;
  544. case ImportRefLoaded::Kind:
  545. case ImportRefUsed::Kind: {
  546. auto import_ir_inst = ir->import_ir_insts().Get(
  547. untyped_inst.As<AnyImportRef>().import_ir_inst_id);
  548. ir = ir->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  549. inst_id = import_ir_inst.inst_id;
  550. continue;
  551. }
  552. case CARBON_KIND(BindAlias inst): {
  553. inst_id = inst.value_id;
  554. continue;
  555. }
  556. case CARBON_KIND(NameRef inst): {
  557. inst_id = inst.value_id;
  558. continue;
  559. }
  560. case CARBON_KIND(Converted inst): {
  561. inst_id = inst.result_id;
  562. continue;
  563. }
  564. case AddrOf::Kind:
  565. case AddrPattern::Kind:
  566. case ArrayType::Kind:
  567. case AssociatedConstantDecl::Kind:
  568. case AssociatedEntity::Kind:
  569. case AssociatedEntityType::Kind:
  570. case BindSymbolicName::Kind:
  571. case BindValue::Kind:
  572. case BlockArg::Kind:
  573. case BoolLiteral::Kind:
  574. case BoundMethod::Kind:
  575. case ClassDecl::Kind:
  576. case ClassType::Kind:
  577. case ConstType::Kind:
  578. case FacetTypeAccess::Kind:
  579. case InterfaceDecl::Kind:
  580. case InterfaceType::Kind:
  581. case InterfaceWitness::Kind:
  582. case InterfaceWitnessAccess::Kind:
  583. case IntLiteral::Kind:
  584. case IntType::Kind:
  585. case Param::Kind:
  586. case PointerType::Kind:
  587. case RealLiteral::Kind:
  588. case StringLiteral::Kind:
  589. case StructValue::Kind:
  590. case StructType::Kind:
  591. case TupleValue::Kind:
  592. case TupleType::Kind:
  593. case UnaryOperatorNot::Kind:
  594. case UnboundElementType::Kind:
  595. case ValueOfInitializer::Kind:
  596. return value_category;
  597. case CARBON_KIND(Builtin inst): {
  598. if (inst.builtin_kind == BuiltinKind::Error) {
  599. return ExprCategory::Error;
  600. }
  601. return value_category;
  602. }
  603. case CARBON_KIND(BindName inst): {
  604. inst_id = inst.value_id;
  605. continue;
  606. }
  607. case CARBON_KIND(ArrayIndex inst): {
  608. inst_id = inst.array_id;
  609. continue;
  610. }
  611. case CARBON_KIND(ClassElementAccess inst): {
  612. inst_id = inst.base_id;
  613. // A value of class type is a pointer to an object representation.
  614. // Therefore, if the base is a value, the result is an ephemeral
  615. // reference.
  616. value_category = ExprCategory::EphemeralRef;
  617. continue;
  618. }
  619. case CARBON_KIND(StructAccess inst): {
  620. inst_id = inst.struct_id;
  621. continue;
  622. }
  623. case CARBON_KIND(TupleAccess inst): {
  624. inst_id = inst.tuple_id;
  625. continue;
  626. }
  627. case CARBON_KIND(TupleIndex inst): {
  628. inst_id = inst.tuple_id;
  629. continue;
  630. }
  631. case CARBON_KIND(SpliceBlock inst): {
  632. inst_id = inst.result_id;
  633. continue;
  634. }
  635. case StructLiteral::Kind:
  636. case TupleLiteral::Kind:
  637. return ExprCategory::Mixed;
  638. case ArrayInit::Kind:
  639. case Call::Kind:
  640. case InitializeFrom::Kind:
  641. case ClassInit::Kind:
  642. case StructInit::Kind:
  643. case TupleInit::Kind:
  644. return ExprCategory::Initializing;
  645. case Deref::Kind:
  646. case VarStorage::Kind:
  647. return ExprCategory::DurableRef;
  648. case Temporary::Kind:
  649. case TemporaryStorage::Kind:
  650. case ValueAsRef::Kind:
  651. return ExprCategory::EphemeralRef;
  652. }
  653. }
  654. }
  655. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr {
  656. auto value_rep = GetValueRepr(file, type_id);
  657. switch (value_rep.kind) {
  658. case ValueRepr::None:
  659. return {.kind = InitRepr::None};
  660. case ValueRepr::Copy:
  661. // TODO: Use in-place initialization for types that have non-trivial
  662. // destructive move.
  663. return {.kind = InitRepr::ByCopy};
  664. case ValueRepr::Pointer:
  665. case ValueRepr::Custom:
  666. return {.kind = InitRepr::InPlace};
  667. case ValueRepr::Unknown:
  668. CARBON_FATAL()
  669. << "Attempting to perform initialization of incomplete type";
  670. }
  671. }
  672. } // namespace Carbon::SemIR