file.cpp 21 KB

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