file.cpp 20 KB

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