file.cpp 22 KB

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