file.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 ImportDecl::Kind:
  433. case ImportRefLoaded::Kind:
  434. case ImportRefUnloaded::Kind:
  435. case InitializeFrom::Kind:
  436. case InterfaceDecl::Kind:
  437. case InterfaceWitness::Kind:
  438. case InterfaceWitnessAccess::Kind:
  439. case IntLiteral::Kind:
  440. case Namespace::Kind:
  441. case Param::Kind:
  442. case RealLiteral::Kind:
  443. case Return::Kind:
  444. case ReturnExpr::Kind:
  445. case SpliceBlock::Kind:
  446. case StringLiteral::Kind:
  447. case StructAccess::Kind:
  448. case StructLiteral::Kind:
  449. case StructInit::Kind:
  450. case StructValue::Kind:
  451. case Temporary::Kind:
  452. case TemporaryStorage::Kind:
  453. case TupleAccess::Kind:
  454. case TupleIndex::Kind:
  455. case TupleLiteral::Kind:
  456. case TupleInit::Kind:
  457. case TupleValue::Kind:
  458. case UnaryOperatorNot::Kind:
  459. case ValueAsRef::Kind:
  460. case ValueOfInitializer::Kind:
  461. case VarStorage::Kind:
  462. // We don't need to handle stringification for instructions that don't
  463. // show up in errors, but make it clear what's going on so that it's
  464. // clearer when stringification is needed.
  465. out << "<cannot stringify " << step.inst_id << ">";
  466. break;
  467. }
  468. }
  469. return str;
  470. }
  471. auto File::StringifyType(TypeId type_id) const -> std::string {
  472. return StringifyTypeExprImpl(*this, types().GetInstId(type_id));
  473. }
  474. auto File::StringifyType(ConstantId type_const_id) const -> std::string {
  475. return StringifyTypeExprImpl(*this,
  476. constant_values().GetInstId(type_const_id));
  477. }
  478. auto File::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  479. return StringifyTypeExprImpl(*this, outer_inst_id);
  480. }
  481. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  482. const File* ir = &file;
  483. // The overall expression category if the current instruction is a value
  484. // expression.
  485. ExprCategory value_category = ExprCategory::Value;
  486. while (true) {
  487. auto untyped_inst = ir->insts().Get(inst_id);
  488. CARBON_KIND_SWITCH(untyped_inst) {
  489. case AdaptDecl::Kind:
  490. case Assign::Kind:
  491. case BaseDecl::Kind:
  492. case Branch::Kind:
  493. case BranchIf::Kind:
  494. case BranchWithArg::Kind:
  495. case FieldDecl::Kind:
  496. case FunctionDecl::Kind:
  497. case ImplDecl::Kind:
  498. case Namespace::Kind:
  499. case Return::Kind:
  500. case ReturnExpr::Kind:
  501. case StructTypeField::Kind:
  502. return ExprCategory::NotExpr;
  503. case ImportRefUnloaded::Kind:
  504. case ImportRefLoaded::Kind: {
  505. auto import_ir_inst = ir->import_ir_insts().Get(
  506. untyped_inst.As<SemIR::AnyImportRef>().import_ir_inst_id);
  507. ir = ir->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  508. inst_id = import_ir_inst.inst_id;
  509. continue;
  510. }
  511. case CARBON_KIND(AsCompatible inst): {
  512. inst_id = inst.source_id;
  513. continue;
  514. }
  515. case CARBON_KIND(BindAlias inst): {
  516. inst_id = inst.value_id;
  517. continue;
  518. }
  519. case CARBON_KIND(ExportDecl inst): {
  520. inst_id = inst.value_id;
  521. continue;
  522. }
  523. case CARBON_KIND(NameRef inst): {
  524. inst_id = inst.value_id;
  525. continue;
  526. }
  527. case CARBON_KIND(Converted inst): {
  528. inst_id = inst.result_id;
  529. continue;
  530. }
  531. case AddrOf::Kind:
  532. case AddrPattern::Kind:
  533. case ArrayType::Kind:
  534. case AssociatedConstantDecl::Kind:
  535. case AssociatedEntity::Kind:
  536. case AssociatedEntityType::Kind:
  537. case BindSymbolicName::Kind:
  538. case BindValue::Kind:
  539. case BlockArg::Kind:
  540. case BoolLiteral::Kind:
  541. case BoundMethod::Kind:
  542. case ClassDecl::Kind:
  543. case ClassType::Kind:
  544. case ConstType::Kind:
  545. case FacetTypeAccess::Kind:
  546. case FloatLiteral::Kind:
  547. case FloatType::Kind:
  548. case FunctionType::Kind:
  549. case GenericClassType::Kind:
  550. case GenericInterfaceType::Kind:
  551. case ImportDecl::Kind:
  552. case InterfaceDecl::Kind:
  553. case InterfaceType::Kind:
  554. case InterfaceWitness::Kind:
  555. case InterfaceWitnessAccess::Kind:
  556. case IntLiteral::Kind:
  557. case IntType::Kind:
  558. case Param::Kind:
  559. case PointerType::Kind:
  560. case RealLiteral::Kind:
  561. case StringLiteral::Kind:
  562. case StructValue::Kind:
  563. case StructType::Kind:
  564. case TupleValue::Kind:
  565. case TupleType::Kind:
  566. case UnaryOperatorNot::Kind:
  567. case UnboundElementType::Kind:
  568. case ValueOfInitializer::Kind:
  569. return value_category;
  570. case CARBON_KIND(Builtin inst): {
  571. if (inst.builtin_kind == BuiltinKind::Error) {
  572. return ExprCategory::Error;
  573. }
  574. return value_category;
  575. }
  576. case CARBON_KIND(BindName inst): {
  577. inst_id = inst.value_id;
  578. continue;
  579. }
  580. case CARBON_KIND(ArrayIndex inst): {
  581. inst_id = inst.array_id;
  582. continue;
  583. }
  584. case CARBON_KIND(ClassElementAccess inst): {
  585. inst_id = inst.base_id;
  586. // A value of class type is a pointer to an object representation.
  587. // Therefore, if the base is a value, the result is an ephemeral
  588. // reference.
  589. value_category = ExprCategory::EphemeralRef;
  590. continue;
  591. }
  592. case CARBON_KIND(StructAccess inst): {
  593. inst_id = inst.struct_id;
  594. continue;
  595. }
  596. case CARBON_KIND(TupleAccess inst): {
  597. inst_id = inst.tuple_id;
  598. continue;
  599. }
  600. case CARBON_KIND(TupleIndex inst): {
  601. inst_id = inst.tuple_id;
  602. continue;
  603. }
  604. case CARBON_KIND(SpliceBlock inst): {
  605. inst_id = inst.result_id;
  606. continue;
  607. }
  608. case StructLiteral::Kind:
  609. case TupleLiteral::Kind:
  610. return ExprCategory::Mixed;
  611. case ArrayInit::Kind:
  612. case Call::Kind:
  613. case InitializeFrom::Kind:
  614. case ClassInit::Kind:
  615. case StructInit::Kind:
  616. case TupleInit::Kind:
  617. return ExprCategory::Initializing;
  618. case Deref::Kind:
  619. case VarStorage::Kind:
  620. return ExprCategory::DurableRef;
  621. case Temporary::Kind:
  622. case TemporaryStorage::Kind:
  623. case ValueAsRef::Kind:
  624. return ExprCategory::EphemeralRef;
  625. }
  626. }
  627. }
  628. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr {
  629. auto value_rep = GetValueRepr(file, type_id);
  630. switch (value_rep.kind) {
  631. case ValueRepr::None:
  632. return {.kind = InitRepr::None};
  633. case ValueRepr::Copy:
  634. // TODO: Use in-place initialization for types that have non-trivial
  635. // destructive move.
  636. return {.kind = InitRepr::ByCopy};
  637. case ValueRepr::Pointer:
  638. case ValueRepr::Custom:
  639. return {.kind = InitRepr::InPlace};
  640. case ValueRepr::Unknown:
  641. CARBON_FATAL()
  642. << "Attempting to perform initialization of incomplete type";
  643. }
  644. }
  645. } // namespace Carbon::SemIR