file.cpp 20 KB

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