semantics_ir.cpp 22 KB

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