file.cpp 21 KB

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