file.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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_valid() || 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 AssociatedEntityType::Kind:
  177. case BindAlias::Kind:
  178. case BindSymbolicName::Kind:
  179. case Builtin::Kind:
  180. case ClassType::Kind:
  181. case FacetTypeAccess::Kind:
  182. case ImportRefUsed::Kind:
  183. case InterfaceType::Kind:
  184. case NameRef::Kind:
  185. case StructType::Kind:
  186. case TupleType::Kind:
  187. case UnboundElementType::Kind:
  188. return 0;
  189. case ConstType::Kind:
  190. return -1;
  191. case PointerType::Kind:
  192. return -2;
  193. case AddrOf::Kind:
  194. case AddrPattern::Kind:
  195. case ArrayIndex::Kind:
  196. case ArrayInit::Kind:
  197. case Assign::Kind:
  198. case AssociatedConstantDecl::Kind:
  199. case AssociatedEntity::Kind:
  200. case BaseDecl::Kind:
  201. case BindName::Kind:
  202. case BindValue::Kind:
  203. case BlockArg::Kind:
  204. case BoolLiteral::Kind:
  205. case BoundMethod::Kind:
  206. case Branch::Kind:
  207. case BranchIf::Kind:
  208. case BranchWithArg::Kind:
  209. case Call::Kind:
  210. case ClassDecl::Kind:
  211. case ClassElementAccess::Kind:
  212. case ClassInit::Kind:
  213. case Converted::Kind:
  214. case Deref::Kind:
  215. case FieldDecl::Kind:
  216. case FunctionDecl::Kind:
  217. case ImplDecl::Kind:
  218. case ImportRefUnused::Kind:
  219. case InitializeFrom::Kind:
  220. case InterfaceDecl::Kind:
  221. case InterfaceWitness::Kind:
  222. case InterfaceWitnessAccess::Kind:
  223. case IntLiteral::Kind:
  224. case Namespace::Kind:
  225. case Param::Kind:
  226. case RealLiteral::Kind:
  227. case Return::Kind:
  228. case ReturnExpr::Kind:
  229. case SpliceBlock::Kind:
  230. case StringLiteral::Kind:
  231. case StructAccess::Kind:
  232. case StructTypeField::Kind:
  233. case StructLiteral::Kind:
  234. case StructInit::Kind:
  235. case StructValue::Kind:
  236. case Temporary::Kind:
  237. case TemporaryStorage::Kind:
  238. case TupleAccess::Kind:
  239. case TupleIndex::Kind:
  240. case TupleLiteral::Kind:
  241. case TupleInit::Kind:
  242. case TupleValue::Kind:
  243. case UnaryOperatorNot::Kind:
  244. case ValueAsRef::Kind:
  245. case ValueOfInitializer::Kind:
  246. case VarStorage::Kind:
  247. CARBON_FATAL() << "GetTypePrecedence for non-type inst kind " << kind;
  248. }
  249. }
  250. // Implements File::StringifyTypeExpr. Static to prevent accidental use of
  251. // member functions while traversing IRs.
  252. static auto StringifyTypeExprImpl(const SemIR::File& outer_sem_ir,
  253. InstId outer_inst_id) {
  254. std::string str;
  255. llvm::raw_string_ostream out(str);
  256. struct Step {
  257. // The instruction's file.
  258. const File& sem_ir;
  259. // The instruction to print.
  260. InstId inst_id;
  261. // The index into inst_id to print. Not used by all types.
  262. int index = 0;
  263. auto Next() const -> Step {
  264. return {.sem_ir = sem_ir, .inst_id = inst_id, .index = index + 1};
  265. }
  266. };
  267. llvm::SmallVector<Step> steps = {
  268. Step{.sem_ir = outer_sem_ir, .inst_id = outer_inst_id}};
  269. while (!steps.empty()) {
  270. auto step = steps.pop_back_val();
  271. if (!step.inst_id.is_valid()) {
  272. out << "<invalid type>";
  273. continue;
  274. }
  275. // Builtins have designated labels.
  276. if (step.inst_id.is_builtin()) {
  277. out << step.inst_id.builtin_kind().label();
  278. continue;
  279. }
  280. const auto& sem_ir = step.sem_ir;
  281. // Helper for instructions with the current sem_ir.
  282. auto push_inst_id = [&](InstId inst_id) {
  283. steps.push_back({.sem_ir = sem_ir, .inst_id = inst_id});
  284. };
  285. auto inst = sem_ir.insts().Get(step.inst_id);
  286. switch (inst.kind()) {
  287. case ArrayType::Kind: {
  288. auto array = inst.As<ArrayType>();
  289. if (step.index == 0) {
  290. out << "[";
  291. steps.push_back(step.Next());
  292. push_inst_id(sem_ir.types().GetInstId(array.element_type_id));
  293. } else if (step.index == 1) {
  294. out << "; " << sem_ir.GetArrayBoundValue(array.bound_id) << "]";
  295. }
  296. break;
  297. }
  298. case AssociatedEntityType::Kind: {
  299. auto assoc = inst.As<AssociatedEntityType>();
  300. if (step.index == 0) {
  301. out << "<associated ";
  302. steps.push_back(step.Next());
  303. push_inst_id(sem_ir.types().GetInstId(assoc.entity_type_id));
  304. } else {
  305. auto interface_name_id =
  306. sem_ir.interfaces().Get(assoc.interface_id).name_id;
  307. out << " in " << sem_ir.names().GetFormatted(interface_name_id)
  308. << ">";
  309. }
  310. break;
  311. }
  312. case BindAlias::Kind:
  313. case BindSymbolicName::Kind: {
  314. auto name_id = inst.As<AnyBindName>().bind_name_id;
  315. out << sem_ir.names().GetFormatted(
  316. sem_ir.bind_names().Get(name_id).name_id);
  317. break;
  318. }
  319. case ClassType::Kind: {
  320. auto class_name_id =
  321. sem_ir.classes().Get(inst.As<ClassType>().class_id).name_id;
  322. out << sem_ir.names().GetFormatted(class_name_id);
  323. break;
  324. }
  325. case ConstType::Kind: {
  326. if (step.index == 0) {
  327. out << "const ";
  328. // Add parentheses if required.
  329. auto inner_type_inst_id =
  330. sem_ir.types().GetInstId(inst.As<ConstType>().inner_id);
  331. if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
  332. GetTypePrecedence(inst.kind())) {
  333. out << "(";
  334. steps.push_back(step.Next());
  335. }
  336. push_inst_id(inner_type_inst_id);
  337. } else if (step.index == 1) {
  338. out << ")";
  339. }
  340. break;
  341. }
  342. case FacetTypeAccess::Kind: {
  343. // Print `T as type` as simply `T`.
  344. push_inst_id(inst.As<FacetTypeAccess>().facet_id);
  345. break;
  346. }
  347. case ImportRefUsed::Kind: {
  348. auto import_ref = inst.As<ImportRefUsed>();
  349. steps.push_back({.sem_ir = *sem_ir.import_irs().Get(import_ref.ir_id),
  350. .inst_id = import_ref.inst_id});
  351. break;
  352. }
  353. case InterfaceType::Kind: {
  354. auto interface_name_id = sem_ir.interfaces()
  355. .Get(inst.As<InterfaceType>().interface_id)
  356. .name_id;
  357. out << sem_ir.names().GetFormatted(interface_name_id);
  358. break;
  359. }
  360. case NameRef::Kind: {
  361. out << sem_ir.names().GetFormatted(inst.As<NameRef>().name_id);
  362. break;
  363. }
  364. case PointerType::Kind: {
  365. if (step.index == 0) {
  366. steps.push_back(step.Next());
  367. push_inst_id(
  368. sem_ir.types().GetInstId(inst.As<PointerType>().pointee_id));
  369. } else if (step.index == 1) {
  370. out << "*";
  371. }
  372. break;
  373. }
  374. case StructType::Kind: {
  375. auto refs = sem_ir.inst_blocks().Get(inst.As<StructType>().fields_id);
  376. if (refs.empty()) {
  377. out << "{}";
  378. break;
  379. } else if (step.index == 0) {
  380. out << "{";
  381. } else if (step.index < static_cast<int>(refs.size())) {
  382. out << ", ";
  383. } else {
  384. out << "}";
  385. break;
  386. }
  387. steps.push_back(step.Next());
  388. push_inst_id(refs[step.index]);
  389. break;
  390. }
  391. case StructTypeField::Kind: {
  392. auto field = inst.As<StructTypeField>();
  393. out << "." << sem_ir.names().GetFormatted(field.name_id) << ": ";
  394. push_inst_id(sem_ir.types().GetInstId(field.field_type_id));
  395. break;
  396. }
  397. case TupleType::Kind: {
  398. auto refs = sem_ir.type_blocks().Get(inst.As<TupleType>().elements_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. // A tuple of one element has a comma to disambiguate from an
  408. // expression.
  409. if (step.index == 1) {
  410. out << ",";
  411. }
  412. out << ")";
  413. break;
  414. }
  415. steps.push_back(step.Next());
  416. push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
  417. break;
  418. }
  419. case UnboundElementType::Kind: {
  420. if (step.index == 0) {
  421. out << "<unbound element of class ";
  422. steps.push_back(step.Next());
  423. push_inst_id(sem_ir.types().GetInstId(
  424. inst.As<UnboundElementType>().class_type_id));
  425. } else {
  426. out << ">";
  427. }
  428. break;
  429. }
  430. case AddrOf::Kind:
  431. case AddrPattern::Kind:
  432. case ArrayIndex::Kind:
  433. case ArrayInit::Kind:
  434. case Assign::Kind:
  435. case AssociatedConstantDecl::Kind:
  436. case AssociatedEntity::Kind:
  437. case BaseDecl::Kind:
  438. case BindName::Kind:
  439. case BindValue::Kind:
  440. case BlockArg::Kind:
  441. case BoolLiteral::Kind:
  442. case BoundMethod::Kind:
  443. case Branch::Kind:
  444. case BranchIf::Kind:
  445. case BranchWithArg::Kind:
  446. case Builtin::Kind:
  447. case Call::Kind:
  448. case ClassDecl::Kind:
  449. case ClassElementAccess::Kind:
  450. case ClassInit::Kind:
  451. case Converted::Kind:
  452. case Deref::Kind:
  453. case FieldDecl::Kind:
  454. case FunctionDecl::Kind:
  455. case ImplDecl::Kind:
  456. case ImportRefUnused::Kind:
  457. case InitializeFrom::Kind:
  458. case InterfaceDecl::Kind:
  459. case InterfaceWitness::Kind:
  460. case InterfaceWitnessAccess::Kind:
  461. case IntLiteral::Kind:
  462. case Namespace::Kind:
  463. case Param::Kind:
  464. case RealLiteral::Kind:
  465. case Return::Kind:
  466. case ReturnExpr::Kind:
  467. case SpliceBlock::Kind:
  468. case StringLiteral::Kind:
  469. case StructAccess::Kind:
  470. case StructLiteral::Kind:
  471. case StructInit::Kind:
  472. case StructValue::Kind:
  473. case Temporary::Kind:
  474. case TemporaryStorage::Kind:
  475. case TupleAccess::Kind:
  476. case TupleIndex::Kind:
  477. case TupleLiteral::Kind:
  478. case TupleInit::Kind:
  479. case TupleValue::Kind:
  480. case UnaryOperatorNot::Kind:
  481. case ValueAsRef::Kind:
  482. case ValueOfInitializer::Kind:
  483. case VarStorage::Kind:
  484. // We don't need to handle stringification for instructions that don't
  485. // show up in errors, but make it clear what's going on so that it's
  486. // clearer when stringification is needed.
  487. out << "<cannot stringify " << step.inst_id << ">";
  488. break;
  489. }
  490. }
  491. return str;
  492. }
  493. auto File::StringifyType(TypeId type_id) const -> std::string {
  494. return StringifyTypeExprImpl(*this, types().GetInstId(type_id));
  495. }
  496. auto File::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  497. return StringifyTypeExprImpl(*this, outer_inst_id);
  498. }
  499. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  500. const File* ir = &file;
  501. // The overall expression category if the current instruction is a value
  502. // expression.
  503. ExprCategory value_category = ExprCategory::Value;
  504. while (true) {
  505. auto inst = ir->insts().Get(inst_id);
  506. switch (inst.kind()) {
  507. case Assign::Kind:
  508. case BaseDecl::Kind:
  509. case Branch::Kind:
  510. case BranchIf::Kind:
  511. case BranchWithArg::Kind:
  512. case FieldDecl::Kind:
  513. case FunctionDecl::Kind:
  514. case ImplDecl::Kind:
  515. case ImportRefUnused::Kind:
  516. case Namespace::Kind:
  517. case Return::Kind:
  518. case ReturnExpr::Kind:
  519. case StructTypeField::Kind:
  520. return ExprCategory::NotExpr;
  521. case ImportRefUsed::Kind: {
  522. auto import_ref = inst.As<ImportRefUsed>();
  523. ir = ir->import_irs().Get(import_ref.ir_id);
  524. inst_id = import_ref.inst_id;
  525. continue;
  526. }
  527. case BindAlias::Kind: {
  528. inst_id = inst.As<BindAlias>().value_id;
  529. continue;
  530. }
  531. case NameRef::Kind: {
  532. inst_id = inst.As<NameRef>().value_id;
  533. continue;
  534. }
  535. case Converted::Kind: {
  536. inst_id = inst.As<Converted>().result_id;
  537. continue;
  538. }
  539. case AddrOf::Kind:
  540. case AddrPattern::Kind:
  541. case ArrayType::Kind:
  542. case AssociatedConstantDecl::Kind:
  543. case AssociatedEntity::Kind:
  544. case AssociatedEntityType::Kind:
  545. case BindSymbolicName::Kind:
  546. case BindValue::Kind:
  547. case BlockArg::Kind:
  548. case BoolLiteral::Kind:
  549. case BoundMethod::Kind:
  550. case ClassDecl::Kind:
  551. case ClassType::Kind:
  552. case ConstType::Kind:
  553. case FacetTypeAccess::Kind:
  554. case InterfaceDecl::Kind:
  555. case InterfaceType::Kind:
  556. case InterfaceWitness::Kind:
  557. case InterfaceWitnessAccess::Kind:
  558. case IntLiteral::Kind:
  559. case Param::Kind:
  560. case PointerType::Kind:
  561. case RealLiteral::Kind:
  562. case StringLiteral::Kind:
  563. case StructValue::Kind:
  564. case StructType::Kind:
  565. case TupleValue::Kind:
  566. case TupleType::Kind:
  567. case UnaryOperatorNot::Kind:
  568. case UnboundElementType::Kind:
  569. case ValueOfInitializer::Kind:
  570. return value_category;
  571. case Builtin::Kind: {
  572. if (inst.As<Builtin>().builtin_kind == BuiltinKind::Error) {
  573. return ExprCategory::Error;
  574. }
  575. return value_category;
  576. }
  577. case BindName::Kind: {
  578. inst_id = inst.As<BindName>().value_id;
  579. continue;
  580. }
  581. case ArrayIndex::Kind: {
  582. inst_id = inst.As<ArrayIndex>().array_id;
  583. continue;
  584. }
  585. case ClassElementAccess::Kind: {
  586. inst_id = inst.As<ClassElementAccess>().base_id;
  587. // A value of class type is a pointer to an object representation.
  588. // Therefore, if the base is a value, the result is an ephemeral
  589. // reference.
  590. value_category = ExprCategory::EphemeralRef;
  591. continue;
  592. }
  593. case StructAccess::Kind: {
  594. inst_id = inst.As<StructAccess>().struct_id;
  595. continue;
  596. }
  597. case TupleAccess::Kind: {
  598. inst_id = inst.As<TupleAccess>().tuple_id;
  599. continue;
  600. }
  601. case TupleIndex::Kind: {
  602. inst_id = inst.As<TupleIndex>().tuple_id;
  603. continue;
  604. }
  605. case SpliceBlock::Kind: {
  606. inst_id = inst.As<SpliceBlock>().result_id;
  607. continue;
  608. }
  609. case StructLiteral::Kind:
  610. case TupleLiteral::Kind:
  611. return ExprCategory::Mixed;
  612. case ArrayInit::Kind:
  613. case Call::Kind:
  614. case InitializeFrom::Kind:
  615. case ClassInit::Kind:
  616. case StructInit::Kind:
  617. case TupleInit::Kind:
  618. return ExprCategory::Initializing;
  619. case Deref::Kind:
  620. case VarStorage::Kind:
  621. return ExprCategory::DurableRef;
  622. case Temporary::Kind:
  623. case TemporaryStorage::Kind:
  624. case ValueAsRef::Kind:
  625. return ExprCategory::EphemeralRef;
  626. }
  627. }
  628. }
  629. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr {
  630. auto value_rep = GetValueRepr(file, type_id);
  631. switch (value_rep.kind) {
  632. case ValueRepr::None:
  633. return {.kind = InitRepr::None};
  634. case ValueRepr::Copy:
  635. // TODO: Use in-place initialization for types that have non-trivial
  636. // destructive move.
  637. return {.kind = InitRepr::ByCopy};
  638. case ValueRepr::Pointer:
  639. case ValueRepr::Custom:
  640. return {.kind = InitRepr::InPlace};
  641. case ValueRepr::Unknown:
  642. CARBON_FATAL()
  643. << "Attempting to perform initialization of incomplete type";
  644. }
  645. }
  646. } // namespace Carbon::SemIR