file.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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)
  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. {Builtin{BuiltinKind::Name == BuiltinKind::Error ? TypeId::Error \
  69. : TypeId::TypeType, \
  70. BuiltinKind::Name}});
  71. #include "toolchain/sem_ir/builtin_kind.def"
  72. for (auto [i, inst] : llvm::enumerate(insts_.array_ref())) {
  73. auto builtin_id = SemIR::InstId(i);
  74. constant_values_.Set(builtin_id,
  75. SemIR::ConstantId::ForTemplateConstant(builtin_id));
  76. }
  77. CARBON_CHECK(insts_.size() == BuiltinKind::ValidCount)
  78. << "Builtins should produce " << BuiltinKind::ValidCount
  79. << " insts, actual: " << insts_.size();
  80. }
  81. File::File(SharedValueStores& value_stores, std::string filename,
  82. const File* builtins)
  83. : value_stores_(&value_stores),
  84. filename_(std::move(filename)),
  85. type_blocks_(allocator_),
  86. inst_blocks_(allocator_),
  87. constants_(*this, allocator_) {
  88. CARBON_CHECK(builtins != nullptr);
  89. auto builtins_id = cross_ref_irs_.Add(builtins);
  90. CARBON_CHECK(builtins_id == CrossRefIRId::Builtins)
  91. << "Builtins must be the first IR";
  92. // Copy builtins over.
  93. insts_.Reserve(BuiltinKind::ValidCount);
  94. static constexpr auto BuiltinIR = CrossRefIRId(0);
  95. for (auto [i, inst] : llvm::enumerate(builtins->insts_.array_ref())) {
  96. // We can reuse the type IDs from the builtins IR because they're
  97. // special-cased values.
  98. auto type_id = inst.type_id();
  99. auto builtin_id = SemIR::InstId(i);
  100. insts_.AddInNoBlock(
  101. {Parse::NodeId::Invalid, CrossRef{type_id, BuiltinIR, builtin_id}});
  102. constant_values_.Set(builtin_id,
  103. SemIR::ConstantId::ForTemplateConstant(builtin_id));
  104. }
  105. }
  106. auto File::Verify() const -> ErrorOr<Success> {
  107. // Invariants don't necessarily hold for invalid IR.
  108. if (has_errors_) {
  109. return Success();
  110. }
  111. // Check that every code block has a terminator sequence that appears at the
  112. // end of the block.
  113. for (const Function& function : functions_.array_ref()) {
  114. for (InstBlockId block_id : function.body_block_ids) {
  115. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  116. for (InstId inst_id : inst_blocks().Get(block_id)) {
  117. TerminatorKind inst_kind =
  118. insts().Get(inst_id).kind().terminator_kind();
  119. if (prior_kind == TerminatorKind::Terminator) {
  120. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  121. inst_id, block_id));
  122. }
  123. if (prior_kind > inst_kind) {
  124. return Error(
  125. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  126. "terminator sequence",
  127. inst_id, block_id));
  128. }
  129. prior_kind = inst_kind;
  130. }
  131. if (prior_kind != TerminatorKind::Terminator) {
  132. return Error(llvm::formatv("No terminator in block {0}", block_id));
  133. }
  134. }
  135. }
  136. // TODO: Check that an instruction only references other instructions that are
  137. // either global or that dominate it.
  138. return Success();
  139. }
  140. auto File::OutputYaml(bool include_builtins) const -> Yaml::OutputMapping {
  141. return Yaml::OutputMapping([this,
  142. include_builtins](Yaml::OutputMapping::Map map) {
  143. map.Add("filename", filename_);
  144. map.Add(
  145. "sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  146. map.Add("cross_ref_irs_size",
  147. Yaml::OutputScalar(cross_ref_irs_.size()));
  148. map.Add("name_scopes", name_scopes_.OutputYaml());
  149. map.Add("bind_names", bind_names_.OutputYaml());
  150. map.Add("functions", functions_.OutputYaml());
  151. map.Add("classes", classes_.OutputYaml());
  152. map.Add("types", types_.OutputYaml());
  153. map.Add("type_blocks", type_blocks_.OutputYaml());
  154. map.Add("insts",
  155. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  156. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  157. for (int i : llvm::seq(start, insts_.size())) {
  158. auto id = InstId(i);
  159. map.Add(PrintToString(id),
  160. Yaml::OutputScalar(insts_.Get(id)));
  161. }
  162. }));
  163. map.Add("constant_values",
  164. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  165. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  166. for (int i : llvm::seq(start, insts_.size())) {
  167. auto id = InstId(i);
  168. auto value = constant_values_.Get(id);
  169. if (value.is_constant()) {
  170. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  171. }
  172. }
  173. }));
  174. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  175. }));
  176. });
  177. }
  178. // Map an instruction kind representing a type into an integer describing the
  179. // precedence of that type's syntax. Higher numbers correspond to higher
  180. // precedence.
  181. static auto GetTypePrecedence(InstKind kind) -> int {
  182. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  183. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  184. switch (kind) {
  185. case ArrayType::Kind:
  186. case BindSymbolicName::Kind:
  187. case Builtin::Kind:
  188. case ClassType::Kind:
  189. case ImportRefUsed::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 ImportRefUnused::Kind:
  228. case InitializeFrom::Kind:
  229. case InterfaceDecl::Kind:
  230. case IntLiteral::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 ImportRefUsed::Kind:
  329. out << "<TODO: ImportRefUsed " << step.inst_id << ">";
  330. break;
  331. case NameRef::Kind: {
  332. out << names().GetFormatted(inst.As<NameRef>().name_id);
  333. break;
  334. }
  335. case PointerType::Kind: {
  336. if (step.index == 0) {
  337. steps.push_back(step.Next());
  338. steps.push_back({.inst_id = types().GetInstId(
  339. inst.As<PointerType>().pointee_id)});
  340. } else if (step.index == 1) {
  341. out << "*";
  342. }
  343. break;
  344. }
  345. case StructType::Kind: {
  346. auto refs = inst_blocks().Get(inst.As<StructType>().fields_id);
  347. if (refs.empty()) {
  348. out << "{}";
  349. break;
  350. } else if (step.index == 0) {
  351. out << "{";
  352. } else if (step.index < static_cast<int>(refs.size())) {
  353. out << ", ";
  354. } else {
  355. out << "}";
  356. break;
  357. }
  358. steps.push_back(step.Next());
  359. steps.push_back({.inst_id = refs[step.index]});
  360. break;
  361. }
  362. case StructTypeField::Kind: {
  363. auto field = inst.As<StructTypeField>();
  364. out << "." << names().GetFormatted(field.name_id) << ": ";
  365. steps.push_back({.inst_id = types().GetInstId(field.field_type_id)});
  366. break;
  367. }
  368. case TupleType::Kind: {
  369. auto refs = type_blocks().Get(inst.As<TupleType>().elements_id);
  370. if (refs.empty()) {
  371. out << "()";
  372. break;
  373. } else if (step.index == 0) {
  374. out << "(";
  375. } else if (step.index < static_cast<int>(refs.size())) {
  376. out << ", ";
  377. } else {
  378. // A tuple of one element has a comma to disambiguate from an
  379. // expression.
  380. if (step.index == 1) {
  381. out << ",";
  382. }
  383. out << ")";
  384. break;
  385. }
  386. steps.push_back(step.Next());
  387. steps.push_back({.inst_id = types().GetInstId(refs[step.index])});
  388. break;
  389. }
  390. case UnboundElementType::Kind: {
  391. if (step.index == 0) {
  392. out << "<unbound element of class ";
  393. steps.push_back(step.Next());
  394. steps.push_back({.inst_id = types().GetInstId(
  395. inst.As<UnboundElementType>().class_type_id)});
  396. } else {
  397. out << ">";
  398. }
  399. break;
  400. }
  401. case AddrOf::Kind:
  402. case AddrPattern::Kind:
  403. case ArrayIndex::Kind:
  404. case ArrayInit::Kind:
  405. case Assign::Kind:
  406. case BaseDecl::Kind:
  407. case BindName::Kind:
  408. case BindValue::Kind:
  409. case BlockArg::Kind:
  410. case BoolLiteral::Kind:
  411. case BoundMethod::Kind:
  412. case Branch::Kind:
  413. case BranchIf::Kind:
  414. case BranchWithArg::Kind:
  415. case Builtin::Kind:
  416. case Call::Kind:
  417. case ClassDecl::Kind:
  418. case ClassElementAccess::Kind:
  419. case ClassInit::Kind:
  420. case Converted::Kind:
  421. case CrossRef::Kind:
  422. case Deref::Kind:
  423. case FieldDecl::Kind:
  424. case FunctionDecl::Kind:
  425. case Import::Kind:
  426. case ImportRefUnused::Kind:
  427. case InitializeFrom::Kind:
  428. case InterfaceDecl::Kind:
  429. case IntLiteral::Kind:
  430. case Namespace::Kind:
  431. case Param::Kind:
  432. case RealLiteral::Kind:
  433. case Return::Kind:
  434. case ReturnExpr::Kind:
  435. case SpliceBlock::Kind:
  436. case StringLiteral::Kind:
  437. case StructAccess::Kind:
  438. case StructLiteral::Kind:
  439. case StructInit::Kind:
  440. case StructValue::Kind:
  441. case Temporary::Kind:
  442. case TemporaryStorage::Kind:
  443. case TupleAccess::Kind:
  444. case TupleIndex::Kind:
  445. case TupleLiteral::Kind:
  446. case TupleInit::Kind:
  447. case TupleValue::Kind:
  448. case UnaryOperatorNot::Kind:
  449. case ValueAsRef::Kind:
  450. case ValueOfInitializer::Kind:
  451. case VarStorage::Kind:
  452. // We don't need to handle stringification for instructions that don't
  453. // show up in errors, but make it clear what's going on so that it's
  454. // clearer when stringification is needed.
  455. out << "<cannot stringify " << step.inst_id << ">";
  456. break;
  457. }
  458. }
  459. return str;
  460. }
  461. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  462. const File* ir = &file;
  463. // The overall expression category if the current instruction is a value
  464. // expression.
  465. ExprCategory value_category = ExprCategory::Value;
  466. while (true) {
  467. auto inst = ir->insts().Get(inst_id);
  468. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  469. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  470. switch (inst.kind()) {
  471. case Assign::Kind:
  472. case BaseDecl::Kind:
  473. case Branch::Kind:
  474. case BranchIf::Kind:
  475. case BranchWithArg::Kind:
  476. case ClassDecl::Kind:
  477. case FieldDecl::Kind:
  478. case FunctionDecl::Kind:
  479. case Import::Kind:
  480. case ImportRefUnused::Kind:
  481. case ImportRefUsed::Kind:
  482. case InterfaceDecl::Kind:
  483. case Namespace::Kind:
  484. case Return::Kind:
  485. case ReturnExpr::Kind:
  486. case StructTypeField::Kind:
  487. return ExprCategory::NotExpr;
  488. case CrossRef::Kind: {
  489. auto xref = inst.As<CrossRef>();
  490. ir = ir->cross_ref_irs().Get(xref.ir_id);
  491. inst_id = xref.inst_id;
  492. continue;
  493. }
  494. case NameRef::Kind: {
  495. inst_id = inst.As<NameRef>().value_id;
  496. continue;
  497. }
  498. case Converted::Kind: {
  499. inst_id = inst.As<Converted>().result_id;
  500. continue;
  501. }
  502. case AddrOf::Kind:
  503. case AddrPattern::Kind:
  504. case ArrayType::Kind:
  505. case BindSymbolicName::Kind:
  506. case BindValue::Kind:
  507. case BlockArg::Kind:
  508. case BoolLiteral::Kind:
  509. case BoundMethod::Kind:
  510. case ClassType::Kind:
  511. case ConstType::Kind:
  512. case IntLiteral::Kind:
  513. case Param::Kind:
  514. case PointerType::Kind:
  515. case RealLiteral::Kind:
  516. case StringLiteral::Kind:
  517. case StructValue::Kind:
  518. case StructType::Kind:
  519. case TupleValue::Kind:
  520. case TupleType::Kind:
  521. case UnaryOperatorNot::Kind:
  522. case UnboundElementType::Kind:
  523. case ValueOfInitializer::Kind:
  524. return value_category;
  525. case Builtin::Kind: {
  526. if (inst.As<Builtin>().builtin_kind == BuiltinKind::Error) {
  527. return ExprCategory::Error;
  528. }
  529. return value_category;
  530. }
  531. case BindName::Kind: {
  532. inst_id = inst.As<BindName>().value_id;
  533. continue;
  534. }
  535. case ArrayIndex::Kind: {
  536. inst_id = inst.As<ArrayIndex>().array_id;
  537. continue;
  538. }
  539. case ClassElementAccess::Kind: {
  540. inst_id = inst.As<ClassElementAccess>().base_id;
  541. // A value of class type is a pointer to an object representation.
  542. // Therefore, if the base is a value, the result is an ephemeral
  543. // reference.
  544. value_category = ExprCategory::EphemeralRef;
  545. continue;
  546. }
  547. case StructAccess::Kind: {
  548. inst_id = inst.As<StructAccess>().struct_id;
  549. continue;
  550. }
  551. case TupleAccess::Kind: {
  552. inst_id = inst.As<TupleAccess>().tuple_id;
  553. continue;
  554. }
  555. case TupleIndex::Kind: {
  556. inst_id = inst.As<TupleIndex>().tuple_id;
  557. continue;
  558. }
  559. case SpliceBlock::Kind: {
  560. inst_id = inst.As<SpliceBlock>().result_id;
  561. continue;
  562. }
  563. case StructLiteral::Kind:
  564. case TupleLiteral::Kind:
  565. return ExprCategory::Mixed;
  566. case ArrayInit::Kind:
  567. case Call::Kind:
  568. case InitializeFrom::Kind:
  569. case ClassInit::Kind:
  570. case StructInit::Kind:
  571. case TupleInit::Kind:
  572. return ExprCategory::Initializing;
  573. case Deref::Kind:
  574. case VarStorage::Kind:
  575. return ExprCategory::DurableRef;
  576. case Temporary::Kind:
  577. case TemporaryStorage::Kind:
  578. case ValueAsRef::Kind:
  579. return ExprCategory::EphemeralRef;
  580. }
  581. }
  582. }
  583. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr {
  584. auto value_rep = GetValueRepr(file, type_id);
  585. switch (value_rep.kind) {
  586. case ValueRepr::None:
  587. return {.kind = InitRepr::None};
  588. case ValueRepr::Copy:
  589. // TODO: Use in-place initialization for types that have non-trivial
  590. // destructive move.
  591. return {.kind = InitRepr::ByCopy};
  592. case ValueRepr::Pointer:
  593. case ValueRepr::Custom:
  594. return {.kind = InitRepr::InPlace};
  595. case ValueRepr::Unknown:
  596. CARBON_FATAL()
  597. << "Attempting to perform initialization of incomplete type";
  598. }
  599. }
  600. } // namespace Carbon::SemIR