semantics_ir.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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/semantics/semantics_ir.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/Sequence.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/base/pretty_stack_trace_function.h"
  9. #include "toolchain/parser/parse_tree_node_location_translator.h"
  10. #include "toolchain/semantics/semantics_builtin_kind.h"
  11. #include "toolchain/semantics/semantics_context.h"
  12. #include "toolchain/semantics/semantics_node.h"
  13. #include "toolchain/semantics/semantics_node_kind.h"
  14. namespace Carbon::SemIR {
  15. auto File::MakeBuiltinIR() -> File {
  16. File semantics_ir(/*builtin_ir=*/nullptr);
  17. semantics_ir.nodes_.reserve(BuiltinKind::ValidCount);
  18. // Error uses a self-referential type so that it's not accidentally treated as
  19. // a normal type. Every other builtin is a type, including the
  20. // self-referential TypeType.
  21. #define CARBON_SEMANTICS_BUILTIN_KIND(Name, ...) \
  22. semantics_ir.nodes_.push_back(Node::Builtin::Make( \
  23. BuiltinKind::Name, BuiltinKind::Name == BuiltinKind::Error \
  24. ? TypeId::Error \
  25. : TypeId::TypeType));
  26. #include "toolchain/semantics/semantics_builtin_kind.def"
  27. CARBON_CHECK(semantics_ir.node_blocks_.size() == 1)
  28. << "BuildBuiltins should only have the empty block, actual: "
  29. << semantics_ir.node_blocks_.size();
  30. CARBON_CHECK(semantics_ir.nodes_.size() == BuiltinKind::ValidCount)
  31. << "BuildBuiltins should produce " << BuiltinKind::ValidCount
  32. << " nodes, actual: " << semantics_ir.nodes_.size();
  33. return semantics_ir;
  34. }
  35. auto File::MakeFromParseTree(const File& builtin_ir,
  36. const TokenizedBuffer& tokens,
  37. const ParseTree& parse_tree,
  38. DiagnosticConsumer& consumer,
  39. llvm::raw_ostream* vlog_stream) -> File {
  40. File semantics_ir(&builtin_ir);
  41. // Copy builtins over.
  42. semantics_ir.nodes_.resize_for_overwrite(BuiltinKind::ValidCount);
  43. static constexpr auto BuiltinIR = CrossReferenceIRId(0);
  44. for (int i : llvm::seq(BuiltinKind::ValidCount)) {
  45. // We can reuse the type node ID because the offsets of cross-references
  46. // will be the same in this IR.
  47. auto type = builtin_ir.nodes_[i].type_id();
  48. semantics_ir.nodes_[i] =
  49. Node::CrossReference::Make(type, BuiltinIR, NodeId(i));
  50. }
  51. ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);
  52. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  53. DiagnosticEmitter<ParseTree::Node> emitter(translator, err_tracker);
  54. Check::Context context(tokens, emitter, parse_tree, semantics_ir,
  55. vlog_stream);
  56. PrettyStackTraceFunction context_dumper(
  57. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  58. // Add a block for the ParseTree.
  59. context.node_block_stack().Push();
  60. context.PushScope();
  61. // Loops over all nodes in the tree. On some errors, this may return early,
  62. // for example if an unrecoverable state is encountered.
  63. for (auto parse_node : parse_tree.postorder()) {
  64. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  65. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  66. switch (auto parse_kind = parse_tree.node_kind(parse_node)) {
  67. #define CARBON_PARSE_NODE_KIND(Name) \
  68. case ParseNodeKind::Name: { \
  69. if (!Check::Handle##Name(context, parse_node)) { \
  70. semantics_ir.has_errors_ = true; \
  71. return semantics_ir; \
  72. } \
  73. break; \
  74. }
  75. #include "toolchain/parser/parse_node_kind.def"
  76. }
  77. }
  78. // Pop information for the file-level scope.
  79. semantics_ir.top_node_block_id_ = context.node_block_stack().Pop();
  80. context.PopScope();
  81. context.VerifyOnFinish();
  82. semantics_ir.has_errors_ = err_tracker.seen_error();
  83. #ifndef NDEBUG
  84. if (auto verify = semantics_ir.Verify(); !verify.ok()) {
  85. CARBON_FATAL() << semantics_ir
  86. << "Built invalid semantics IR: " << verify.error() << "\n";
  87. }
  88. #endif
  89. return semantics_ir;
  90. }
  91. auto File::Verify() const -> ErrorOr<Success> {
  92. // Invariants don't necessarily hold for invalid IR.
  93. if (has_errors_) {
  94. return Success();
  95. }
  96. // Check that every code block has a terminator sequence that appears at the
  97. // end of the block.
  98. for (const Function& function : functions_) {
  99. for (NodeBlockId block_id : function.body_block_ids) {
  100. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  101. for (NodeId node_id : GetNodeBlock(block_id)) {
  102. TerminatorKind node_kind = GetNode(node_id).kind().terminator_kind();
  103. if (prior_kind == TerminatorKind::Terminator) {
  104. return Error(llvm::formatv("Node {0} in block {1} follows terminator",
  105. node_id, block_id));
  106. }
  107. if (prior_kind > node_kind) {
  108. return Error(
  109. llvm::formatv("Non-terminator node {0} in block {1} follows "
  110. "terminator sequence",
  111. node_id, block_id));
  112. }
  113. prior_kind = node_kind;
  114. }
  115. if (prior_kind != TerminatorKind::Terminator) {
  116. return Error(llvm::formatv("No terminator in block {0}", block_id));
  117. }
  118. }
  119. }
  120. // TODO: Check that a node only references other nodes that are either global
  121. // or that dominate it.
  122. return Success();
  123. }
  124. static constexpr int Indent = 2;
  125. template <typename T>
  126. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  127. const llvm::SmallVector<T>& list) {
  128. out << name << ": [\n";
  129. for (const auto& element : list) {
  130. out.indent(Indent);
  131. out << element << ",\n";
  132. }
  133. out << "]\n";
  134. }
  135. template <typename T>
  136. static auto PrintBlock(llvm::raw_ostream& out, llvm::StringLiteral block_name,
  137. const llvm::SmallVector<T>& blocks) {
  138. out << block_name << ": [\n";
  139. for (const auto& block : blocks) {
  140. out.indent(Indent);
  141. out << "[\n";
  142. for (const auto& node : block) {
  143. out.indent(2 * Indent);
  144. out << node << ",\n";
  145. }
  146. out.indent(Indent);
  147. out << "],\n";
  148. }
  149. out << "]\n";
  150. }
  151. auto File::Print(llvm::raw_ostream& out, bool include_builtins) const -> void {
  152. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  153. PrintList(out, "functions", functions_);
  154. PrintList(out, "integer_literals", integer_literals_);
  155. PrintList(out, "real_literals", real_literals_);
  156. PrintList(out, "strings", strings_);
  157. PrintList(out, "types", types_);
  158. PrintBlock(out, "type_blocks", type_blocks_);
  159. out << "nodes: [\n";
  160. for (int i = include_builtins ? 0 : BuiltinKind::ValidCount;
  161. i < static_cast<int>(nodes_.size()); ++i) {
  162. const auto& element = nodes_[i];
  163. out.indent(Indent);
  164. out << element << ",\n";
  165. }
  166. out << "]\n";
  167. PrintBlock(out, "node_blocks", node_blocks_);
  168. }
  169. // Map a node kind representing a type into an integer describing the
  170. // precedence of that type's syntax. Higher numbers correspond to higher
  171. // precedence.
  172. static auto GetTypePrecedence(NodeKind kind) -> int {
  173. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  174. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  175. switch (kind) {
  176. case NodeKind::ArrayType:
  177. case NodeKind::Builtin:
  178. case NodeKind::StructType:
  179. case NodeKind::TupleType:
  180. return 0;
  181. case NodeKind::ConstType:
  182. return -1;
  183. case NodeKind::PointerType:
  184. return -2;
  185. case NodeKind::CrossReference:
  186. // TODO: Once we support stringification of cross-references, we'll need
  187. // to determine the precedence of the target of the cross-reference. For
  188. // now, all cross-references refer to builtin types from the prelude.
  189. return 0;
  190. case NodeKind::AddressOf:
  191. case NodeKind::ArrayIndex:
  192. case NodeKind::ArrayValue:
  193. case NodeKind::Assign:
  194. case NodeKind::BinaryOperatorAdd:
  195. case NodeKind::BindValue:
  196. case NodeKind::BlockArg:
  197. case NodeKind::BoolLiteral:
  198. case NodeKind::Branch:
  199. case NodeKind::BranchIf:
  200. case NodeKind::BranchWithArg:
  201. case NodeKind::Call:
  202. case NodeKind::Dereference:
  203. case NodeKind::FunctionDeclaration:
  204. case NodeKind::IntegerLiteral:
  205. case NodeKind::Invalid:
  206. case NodeKind::MaterializeTemporary:
  207. case NodeKind::Namespace:
  208. case NodeKind::NoOp:
  209. case NodeKind::Parameter:
  210. case NodeKind::RealLiteral:
  211. case NodeKind::Return:
  212. case NodeKind::ReturnExpression:
  213. case NodeKind::StringLiteral:
  214. case NodeKind::StructAccess:
  215. case NodeKind::StructTypeField:
  216. case NodeKind::StructValue:
  217. case NodeKind::StubReference:
  218. case NodeKind::TupleIndex:
  219. case NodeKind::TupleValue:
  220. case NodeKind::UnaryOperatorNot:
  221. case NodeKind::VarStorage:
  222. CARBON_FATAL() << "GetTypePrecedence for non-type node kind " << kind;
  223. }
  224. }
  225. auto File::StringifyType(TypeId type_id, bool in_type_context) const
  226. -> std::string {
  227. std::string str;
  228. llvm::raw_string_ostream out(str);
  229. struct Step {
  230. // The node to print.
  231. NodeId node_id;
  232. // The index into node_id to print. Not used by all types.
  233. int index = 0;
  234. auto Next() const -> Step {
  235. return {.node_id = node_id, .index = index + 1};
  236. }
  237. };
  238. auto outer_node_id = GetTypeAllowBuiltinTypes(type_id);
  239. llvm::SmallVector<Step> steps = {{.node_id = outer_node_id}};
  240. while (!steps.empty()) {
  241. auto step = steps.pop_back_val();
  242. // Invalid node IDs will use the default invalid printing.
  243. if (!step.node_id.is_valid()) {
  244. out << step.node_id;
  245. continue;
  246. }
  247. // Builtins have designated labels.
  248. if (step.node_id.index < BuiltinKind::ValidCount) {
  249. out << BuiltinKind::FromInt(step.node_id.index).label();
  250. continue;
  251. }
  252. auto node = GetNode(step.node_id);
  253. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  254. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  255. switch (node.kind()) {
  256. case NodeKind::ArrayType: {
  257. auto [bound_id, type_id] = node.GetAsArrayType();
  258. if (step.index == 0) {
  259. out << "[";
  260. steps.push_back(step.Next());
  261. steps.push_back({.node_id = GetTypeAllowBuiltinTypes(type_id)});
  262. } else if (step.index == 1) {
  263. out << "; " << GetArrayBoundValue(bound_id) << "]";
  264. }
  265. break;
  266. }
  267. case NodeKind::ConstType: {
  268. if (step.index == 0) {
  269. out << "const ";
  270. // Add parentheses if required.
  271. auto inner_type_node_id =
  272. GetTypeAllowBuiltinTypes(node.GetAsConstType());
  273. if (GetTypePrecedence(GetNode(inner_type_node_id).kind()) <
  274. GetTypePrecedence(node.kind())) {
  275. out << "(";
  276. steps.push_back(step.Next());
  277. }
  278. steps.push_back({.node_id = inner_type_node_id});
  279. } else if (step.index == 1) {
  280. out << ")";
  281. }
  282. break;
  283. }
  284. case NodeKind::PointerType: {
  285. if (step.index == 0) {
  286. steps.push_back(step.Next());
  287. steps.push_back(
  288. {.node_id = GetTypeAllowBuiltinTypes(node.GetAsPointerType())});
  289. } else if (step.index == 1) {
  290. out << "*";
  291. }
  292. break;
  293. }
  294. case NodeKind::StructType: {
  295. auto refs = GetNodeBlock(node.GetAsStructType());
  296. if (refs.empty()) {
  297. out << "{}";
  298. break;
  299. } else if (step.index == 0) {
  300. out << "{";
  301. } else if (step.index < static_cast<int>(refs.size())) {
  302. out << ", ";
  303. } else {
  304. out << "}";
  305. break;
  306. }
  307. steps.push_back(step.Next());
  308. steps.push_back({.node_id = refs[step.index]});
  309. break;
  310. }
  311. case NodeKind::StructTypeField: {
  312. auto [name_id, type_id] = node.GetAsStructTypeField();
  313. out << "." << GetString(name_id) << ": ";
  314. steps.push_back({.node_id = GetTypeAllowBuiltinTypes(type_id)});
  315. break;
  316. }
  317. case NodeKind::TupleType: {
  318. auto refs = GetTypeBlock(node.GetAsTupleType());
  319. if (refs.empty()) {
  320. out << "()";
  321. break;
  322. } else if (step.index == 0) {
  323. out << "(";
  324. } else if (step.index < static_cast<int>(refs.size())) {
  325. out << ", ";
  326. } else {
  327. // A tuple of one element has a comma to disambiguate from an
  328. // expression.
  329. if (step.index == 1) {
  330. out << ",";
  331. }
  332. out << ")";
  333. break;
  334. }
  335. steps.push_back(step.Next());
  336. steps.push_back(
  337. {.node_id = GetTypeAllowBuiltinTypes(refs[step.index])});
  338. break;
  339. }
  340. case NodeKind::AddressOf:
  341. case NodeKind::ArrayIndex:
  342. case NodeKind::ArrayValue:
  343. case NodeKind::Assign:
  344. case NodeKind::BinaryOperatorAdd:
  345. case NodeKind::BindValue:
  346. case NodeKind::BlockArg:
  347. case NodeKind::BoolLiteral:
  348. case NodeKind::Branch:
  349. case NodeKind::BranchIf:
  350. case NodeKind::BranchWithArg:
  351. case NodeKind::Builtin:
  352. case NodeKind::Call:
  353. case NodeKind::CrossReference:
  354. case NodeKind::Dereference:
  355. case NodeKind::FunctionDeclaration:
  356. case NodeKind::IntegerLiteral:
  357. case NodeKind::MaterializeTemporary:
  358. case NodeKind::Namespace:
  359. case NodeKind::NoOp:
  360. case NodeKind::Parameter:
  361. case NodeKind::RealLiteral:
  362. case NodeKind::Return:
  363. case NodeKind::ReturnExpression:
  364. case NodeKind::StringLiteral:
  365. case NodeKind::StructAccess:
  366. case NodeKind::StructValue:
  367. case NodeKind::StubReference:
  368. case NodeKind::TupleIndex:
  369. case NodeKind::TupleValue:
  370. case NodeKind::UnaryOperatorNot:
  371. case NodeKind::VarStorage:
  372. // We don't need to handle stringification for nodes that don't show up
  373. // in errors, but make it clear what's going on so that it's clearer
  374. // when stringification is needed.
  375. out << "<cannot stringify " << step.node_id << ">";
  376. break;
  377. case NodeKind::Invalid:
  378. llvm_unreachable("NodeKind::Invalid is never used.");
  379. }
  380. }
  381. // For `{}` or any tuple type, we've printed a non-type expression, so add a
  382. // conversion to type `type` if it's not implied by the context.
  383. if (!in_type_context) {
  384. auto outer_node = GetNode(outer_node_id);
  385. if (outer_node.kind() == NodeKind::TupleType ||
  386. (outer_node.kind() == NodeKind::StructType &&
  387. GetNodeBlock(outer_node.GetAsStructType()).empty())) {
  388. out << " as type";
  389. }
  390. }
  391. return str;
  392. }
  393. auto GetExpressionCategory(const File& file, NodeId node_id)
  394. -> ExpressionCategory {
  395. const File* ir = &file;
  396. while (true) {
  397. auto node = ir->GetNode(node_id);
  398. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  399. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  400. switch (node.kind()) {
  401. case NodeKind::Invalid:
  402. case NodeKind::Assign:
  403. case NodeKind::Branch:
  404. case NodeKind::BranchIf:
  405. case NodeKind::BranchWithArg:
  406. case NodeKind::FunctionDeclaration:
  407. case NodeKind::Namespace:
  408. case NodeKind::NoOp:
  409. case NodeKind::Return:
  410. case NodeKind::ReturnExpression:
  411. case NodeKind::StructTypeField:
  412. return ExpressionCategory::NotExpression;
  413. case NodeKind::CrossReference: {
  414. auto [xref_id, xref_node_id] = node.GetAsCrossReference();
  415. ir = &ir->GetCrossReferenceIR(xref_id);
  416. node_id = xref_node_id;
  417. continue;
  418. }
  419. case NodeKind::AddressOf:
  420. case NodeKind::ArrayType:
  421. case NodeKind::BinaryOperatorAdd:
  422. case NodeKind::BindValue:
  423. case NodeKind::BlockArg:
  424. case NodeKind::BoolLiteral:
  425. case NodeKind::Builtin:
  426. case NodeKind::ConstType:
  427. case NodeKind::IntegerLiteral:
  428. case NodeKind::Parameter:
  429. case NodeKind::PointerType:
  430. case NodeKind::RealLiteral:
  431. case NodeKind::StringLiteral:
  432. case NodeKind::StructType:
  433. case NodeKind::TupleType:
  434. case NodeKind::UnaryOperatorNot:
  435. return ExpressionCategory::Value;
  436. case NodeKind::ArrayIndex: {
  437. auto [base_id, index_id] = node.GetAsArrayIndex();
  438. node_id = base_id;
  439. continue;
  440. }
  441. case NodeKind::StructAccess: {
  442. auto [base_id, member_index] = node.GetAsStructAccess();
  443. node_id = base_id;
  444. continue;
  445. }
  446. case NodeKind::TupleIndex: {
  447. auto [base_id, index_id] = node.GetAsTupleIndex();
  448. node_id = base_id;
  449. continue;
  450. }
  451. case NodeKind::StubReference: {
  452. node_id = node.GetAsStubReference();
  453. continue;
  454. }
  455. case NodeKind::ArrayValue:
  456. case NodeKind::StructValue:
  457. case NodeKind::TupleValue:
  458. // TODO: Eventually these will depend on the context in which the value
  459. // is used, and could be either Value or Initializing. We may want
  460. // different node kinds for a struct/tuple initializer versus a
  461. // struct/tuple value construction.
  462. return ExpressionCategory::Value;
  463. case NodeKind::Call:
  464. return ExpressionCategory::Initializing;
  465. case NodeKind::Dereference:
  466. case NodeKind::VarStorage:
  467. return ExpressionCategory::DurableReference;
  468. case NodeKind::MaterializeTemporary:
  469. return ExpressionCategory::EphemeralReference;
  470. }
  471. }
  472. }
  473. auto GetValueRepresentation(const File& file, TypeId type_id)
  474. -> ValueRepresentation {
  475. const File* ir = &file;
  476. NodeId node_id = ir->GetTypeAllowBuiltinTypes(type_id);
  477. while (true) {
  478. auto node = ir->GetNode(node_id);
  479. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  480. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  481. switch (node.kind()) {
  482. case NodeKind::AddressOf:
  483. case NodeKind::ArrayIndex:
  484. case NodeKind::ArrayValue:
  485. case NodeKind::Assign:
  486. case NodeKind::BinaryOperatorAdd:
  487. case NodeKind::BindValue:
  488. case NodeKind::BlockArg:
  489. case NodeKind::BoolLiteral:
  490. case NodeKind::Branch:
  491. case NodeKind::BranchIf:
  492. case NodeKind::BranchWithArg:
  493. case NodeKind::Call:
  494. case NodeKind::Dereference:
  495. case NodeKind::FunctionDeclaration:
  496. case NodeKind::IntegerLiteral:
  497. case NodeKind::Invalid:
  498. case NodeKind::MaterializeTemporary:
  499. case NodeKind::Namespace:
  500. case NodeKind::NoOp:
  501. case NodeKind::Parameter:
  502. case NodeKind::RealLiteral:
  503. case NodeKind::Return:
  504. case NodeKind::ReturnExpression:
  505. case NodeKind::StringLiteral:
  506. case NodeKind::StructAccess:
  507. case NodeKind::StructTypeField:
  508. case NodeKind::StructValue:
  509. case NodeKind::TupleIndex:
  510. case NodeKind::TupleValue:
  511. case NodeKind::UnaryOperatorNot:
  512. case NodeKind::VarStorage:
  513. CARBON_FATAL() << "Type refers to non-type node " << node;
  514. case NodeKind::CrossReference: {
  515. auto [xref_id, xref_node_id] = node.GetAsCrossReference();
  516. ir = &ir->GetCrossReferenceIR(xref_id);
  517. node_id = xref_node_id;
  518. continue;
  519. }
  520. case NodeKind::StubReference: {
  521. node_id = node.GetAsStubReference();
  522. continue;
  523. }
  524. case NodeKind::ArrayType:
  525. // For arrays, it's convenient to always use a pointer representation,
  526. // even when the array has zero or one element, in order to support
  527. // indexing.
  528. return {.kind = ValueRepresentation::Pointer, .type = type_id};
  529. case NodeKind::StructType: {
  530. const auto& fields = ir->GetNodeBlock(node.GetAsStructType());
  531. if (fields.empty()) {
  532. // An empty struct has an empty representation.
  533. return {.kind = ValueRepresentation::None, .type = TypeId::Invalid};
  534. }
  535. if (fields.size() == 1) {
  536. // A struct with one field has the same representation as its field.
  537. auto [field_name_id, field_type_id] =
  538. ir->GetNode(fields.front()).GetAsStructTypeField();
  539. node_id = ir->GetTypeAllowBuiltinTypes(field_type_id);
  540. continue;
  541. }
  542. // For any other struct, use a pointer representation.
  543. return {.kind = ValueRepresentation::Pointer, .type = type_id};
  544. }
  545. case NodeKind::TupleType: {
  546. const auto& elements = ir->GetTypeBlock(node.GetAsTupleType());
  547. if (elements.empty()) {
  548. // An empty tuple has an empty representation.
  549. return {.kind = ValueRepresentation::None, .type = TypeId::Invalid};
  550. }
  551. if (elements.size() == 1) {
  552. // A one-tuple has the same representation as its sole element.
  553. node_id = ir->GetTypeAllowBuiltinTypes(elements.front());
  554. continue;
  555. }
  556. // For any other tuple, use a pointer representation.
  557. return {.kind = ValueRepresentation::Pointer, .type = type_id};
  558. }
  559. case NodeKind::Builtin:
  560. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  561. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  562. switch (node.GetAsBuiltin()) {
  563. case BuiltinKind::TypeType:
  564. case BuiltinKind::Error:
  565. case BuiltinKind::Invalid:
  566. return {.kind = ValueRepresentation::None, .type = TypeId::Invalid};
  567. case BuiltinKind::BoolType:
  568. case BuiltinKind::IntegerType:
  569. case BuiltinKind::FloatingPointType:
  570. return {.kind = ValueRepresentation::Copy, .type = type_id};
  571. case BuiltinKind::StringType:
  572. // TODO: Decide on string value semantics. This should probably be a
  573. // custom value representation carrying a pointer and size or
  574. // similar.
  575. return {.kind = ValueRepresentation::Pointer, .type = type_id};
  576. }
  577. case NodeKind::PointerType:
  578. return {.kind = ValueRepresentation::Copy, .type = type_id};
  579. case NodeKind::ConstType:
  580. node_id = ir->GetTypeAllowBuiltinTypes(node.GetAsConstType());
  581. continue;
  582. }
  583. }
  584. }
  585. auto GetInitializingRepresentation(const File& file, TypeId type_id)
  586. -> InitializingRepresentation {
  587. auto value_rep = GetValueRepresentation(file, type_id);
  588. switch (value_rep.kind) {
  589. case ValueRepresentation::None:
  590. return {.kind = InitializingRepresentation::None};
  591. case ValueRepresentation::Copy:
  592. // TODO: Use in-place initialization for types that have non-trivial
  593. // destructive move.
  594. return {.kind = InitializingRepresentation::ByCopy};
  595. case ValueRepresentation::Pointer:
  596. case ValueRepresentation::Custom:
  597. return {.kind = InitializingRepresentation::InPlace};
  598. }
  599. }
  600. } // namespace Carbon::SemIR