context.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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/check/context.h"
  5. #include <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "common/vlog.h"
  9. #include "llvm/ADT/Sequence.h"
  10. #include "toolchain/check/declaration_name_stack.h"
  11. #include "toolchain/check/node_block_stack.h"
  12. #include "toolchain/lex/tokenized_buffer.h"
  13. #include "toolchain/parse/node_kind.h"
  14. #include "toolchain/sem_ir/file.h"
  15. #include "toolchain/sem_ir/node.h"
  16. #include "toolchain/sem_ir/node_kind.h"
  17. namespace Carbon::Check {
  18. Context::Context(const Lex::TokenizedBuffer& tokens, DiagnosticEmitter& emitter,
  19. const Parse::Tree& parse_tree, SemIR::File& sem_ir,
  20. llvm::raw_ostream* vlog_stream)
  21. : tokens_(&tokens),
  22. emitter_(&emitter),
  23. parse_tree_(&parse_tree),
  24. sem_ir_(&sem_ir),
  25. vlog_stream_(vlog_stream),
  26. node_stack_(parse_tree, vlog_stream),
  27. node_block_stack_("node_block_stack_", sem_ir, vlog_stream),
  28. params_or_args_stack_("params_or_args_stack_", sem_ir, vlog_stream),
  29. args_type_info_stack_("args_type_info_stack_", sem_ir, vlog_stream),
  30. declaration_name_stack_(this) {
  31. // Inserts the "Error" and "Type" types as "used types" so that
  32. // canonicalization can skip them. We don't emit either for lowering.
  33. canonical_types_.insert({SemIR::NodeId::BuiltinError, SemIR::TypeId::Error});
  34. canonical_types_.insert(
  35. {SemIR::NodeId::BuiltinTypeType, SemIR::TypeId::TypeType});
  36. }
  37. auto Context::TODO(Parse::Node parse_node, std::string label) -> bool {
  38. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  39. std::string);
  40. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  41. return false;
  42. }
  43. auto Context::VerifyOnFinish() -> void {
  44. // Information in all the various context objects should be cleaned up as
  45. // various pieces of context go out of scope. At this point, nothing should
  46. // remain.
  47. // node_stack_ will still contain top-level entities.
  48. CARBON_CHECK(name_lookup_.empty()) << name_lookup_.size();
  49. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  50. CARBON_CHECK(node_block_stack_.empty()) << node_block_stack_.size();
  51. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  52. }
  53. auto Context::AddNode(SemIR::Node node) -> SemIR::NodeId {
  54. auto node_id = node_block_stack_.AddNode(node);
  55. CARBON_VLOG() << "AddNode: " << node << "\n";
  56. return node_id;
  57. }
  58. auto Context::AddNodeAndPush(Parse::Node parse_node, SemIR::Node node) -> void {
  59. auto node_id = AddNode(node);
  60. node_stack_.Push(parse_node, node_id);
  61. }
  62. auto Context::DiagnoseDuplicateName(Parse::Node parse_node,
  63. SemIR::NodeId prev_def_id) -> void {
  64. CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
  65. "Duplicate name being declared in the same scope.");
  66. CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
  67. "Name is previously declared here.");
  68. auto prev_def = nodes().Get(prev_def_id);
  69. emitter_->Build(parse_node, NameDeclarationDuplicate)
  70. .Note(prev_def.parse_node(), NameDeclarationPrevious)
  71. .Emit();
  72. }
  73. auto Context::DiagnoseNameNotFound(Parse::Node parse_node, StringId name_id)
  74. -> void {
  75. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  76. llvm::StringRef);
  77. emitter_->Emit(parse_node, NameNotFound, strings().Get(name_id));
  78. }
  79. auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
  80. DiagnosticBuilder& builder) -> void {
  81. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  82. "Class was forward declared here.");
  83. const auto& class_info = classes().Get(class_id);
  84. builder.Note(nodes().Get(class_info.declaration_id).parse_node(),
  85. ClassForwardDeclaredHere);
  86. }
  87. auto Context::AddNameToLookup(Parse::Node name_node, StringId name_id,
  88. SemIR::NodeId target_id) -> void {
  89. if (current_scope().names.insert(name_id).second) {
  90. name_lookup_[name_id].push_back(target_id);
  91. } else {
  92. DiagnoseDuplicateName(name_node, name_lookup_[name_id].back());
  93. }
  94. }
  95. auto Context::LookupName(Parse::Node parse_node, StringId name_id,
  96. SemIR::NameScopeId scope_id, bool print_diagnostics)
  97. -> SemIR::NodeId {
  98. if (scope_id == SemIR::NameScopeId::Invalid) {
  99. auto it = name_lookup_.find(name_id);
  100. if (it == name_lookup_.end()) {
  101. if (print_diagnostics) {
  102. DiagnoseNameNotFound(parse_node, name_id);
  103. }
  104. return SemIR::NodeId::BuiltinError;
  105. }
  106. CARBON_CHECK(!it->second.empty())
  107. << "Should have been erased: " << strings().Get(name_id);
  108. // TODO: Check for ambiguous lookups.
  109. return it->second.back();
  110. } else {
  111. const auto& scope = name_scopes().Get(scope_id);
  112. auto it = scope.find(name_id);
  113. if (it == scope.end()) {
  114. if (print_diagnostics) {
  115. DiagnoseNameNotFound(parse_node, name_id);
  116. }
  117. return SemIR::NodeId::BuiltinError;
  118. }
  119. return it->second;
  120. }
  121. }
  122. auto Context::PushScope(SemIR::NodeId scope_node_id,
  123. SemIR::NameScopeId scope_id) -> void {
  124. scope_stack_.push_back(
  125. {.scope_node_id = scope_node_id, .scope_id = scope_id});
  126. }
  127. auto Context::PopScope() -> void {
  128. auto scope = scope_stack_.pop_back_val();
  129. for (const auto& str_id : scope.names) {
  130. auto it = name_lookup_.find(str_id);
  131. if (it->second.size() == 1) {
  132. // Erase names that no longer resolve.
  133. name_lookup_.erase(it);
  134. } else {
  135. it->second.pop_back();
  136. }
  137. }
  138. }
  139. auto Context::FollowNameReferences(SemIR::NodeId node_id) -> SemIR::NodeId {
  140. while (auto name_ref = nodes().Get(node_id).TryAs<SemIR::NameReference>()) {
  141. node_id = name_ref->value_id;
  142. }
  143. return node_id;
  144. }
  145. template <typename BranchNode, typename... Args>
  146. static auto AddDominatedBlockAndBranchImpl(Context& context,
  147. Parse::Node parse_node, Args... args)
  148. -> SemIR::NodeBlockId {
  149. if (!context.node_block_stack().is_current_block_reachable()) {
  150. return SemIR::NodeBlockId::Unreachable;
  151. }
  152. auto block_id = context.node_blocks().AddDefaultValue();
  153. context.AddNode(BranchNode{parse_node, block_id, args...});
  154. return block_id;
  155. }
  156. auto Context::AddDominatedBlockAndBranch(Parse::Node parse_node)
  157. -> SemIR::NodeBlockId {
  158. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  159. }
  160. auto Context::AddDominatedBlockAndBranchWithArg(Parse::Node parse_node,
  161. SemIR::NodeId arg_id)
  162. -> SemIR::NodeBlockId {
  163. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  164. arg_id);
  165. }
  166. auto Context::AddDominatedBlockAndBranchIf(Parse::Node parse_node,
  167. SemIR::NodeId cond_id)
  168. -> SemIR::NodeBlockId {
  169. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  170. cond_id);
  171. }
  172. auto Context::AddConvergenceBlockAndPush(Parse::Node parse_node, int num_blocks)
  173. -> void {
  174. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  175. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  176. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  177. if (node_block_stack().is_current_block_reachable()) {
  178. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  179. new_block_id = node_blocks().AddDefaultValue();
  180. }
  181. AddNode(SemIR::Branch{parse_node, new_block_id});
  182. }
  183. node_block_stack().Pop();
  184. }
  185. node_block_stack().Push(new_block_id);
  186. }
  187. auto Context::AddConvergenceBlockWithArgAndPush(
  188. Parse::Node parse_node, std::initializer_list<SemIR::NodeId> block_args)
  189. -> SemIR::NodeId {
  190. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  191. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  192. for (auto arg_id : block_args) {
  193. if (node_block_stack().is_current_block_reachable()) {
  194. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  195. new_block_id = node_blocks().AddDefaultValue();
  196. }
  197. AddNode(SemIR::BranchWithArg{parse_node, new_block_id, arg_id});
  198. }
  199. node_block_stack().Pop();
  200. }
  201. node_block_stack().Push(new_block_id);
  202. // Acquire the result value.
  203. SemIR::TypeId result_type_id = nodes().Get(*block_args.begin()).type_id();
  204. return AddNode(SemIR::BlockArg{parse_node, result_type_id, new_block_id});
  205. }
  206. // Add the current code block to the enclosing function.
  207. auto Context::AddCurrentCodeBlockToFunction() -> void {
  208. CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
  209. CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
  210. if (!node_block_stack().is_current_block_reachable()) {
  211. // Don't include unreachable blocks in the function.
  212. return;
  213. }
  214. auto function_id =
  215. nodes()
  216. .GetAs<SemIR::FunctionDeclaration>(return_scope_stack().back())
  217. .function_id;
  218. functions()
  219. .Get(function_id)
  220. .body_block_ids.push_back(node_block_stack().PeekOrAdd());
  221. }
  222. auto Context::is_current_position_reachable() -> bool {
  223. if (!node_block_stack().is_current_block_reachable()) {
  224. return false;
  225. }
  226. // Our current position is at the end of a reachable block. That position is
  227. // reachable unless the previous instruction is a terminator instruction.
  228. auto block_contents = node_block_stack().PeekCurrentBlockContents();
  229. if (block_contents.empty()) {
  230. return true;
  231. }
  232. const auto& last_node = nodes().Get(block_contents.back());
  233. return last_node.kind().terminator_kind() !=
  234. SemIR::TerminatorKind::Terminator;
  235. }
  236. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  237. auto Context::ParamOrArgComma() -> void {
  238. ParamOrArgSave(node_stack_.PopExpression());
  239. }
  240. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  241. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  242. ParamOrArgSave(node_stack_.PopExpression());
  243. }
  244. }
  245. auto Context::ParamOrArgPop() -> SemIR::NodeBlockId {
  246. return params_or_args_stack_.Pop();
  247. }
  248. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::NodeBlockId {
  249. ParamOrArgEndNoPop(start_kind);
  250. return ParamOrArgPop();
  251. }
  252. namespace {
  253. // Worklist-based type completion mechanism.
  254. //
  255. // When attempting to complete a type, we may find other types that also need to
  256. // be completed: types nested within that type, and the value representation of
  257. // the type. In order to complete a type without recursing arbitrarily deeply,
  258. // we use a worklist of tasks:
  259. //
  260. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  261. // nested within a type to the work list.
  262. // - A `BuildValueRepresentation` step computes the value representation for a
  263. // type, once all of its nested types are complete, and marks the type as
  264. // complete.
  265. class TypeCompleter {
  266. public:
  267. TypeCompleter(
  268. Context& context,
  269. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  270. diagnoser)
  271. : context_(context), diagnoser_(diagnoser) {}
  272. // Attempts to complete the given type. Returns true if it is now complete,
  273. // false if it could not be completed.
  274. auto Complete(SemIR::TypeId type_id) -> bool {
  275. Push(type_id);
  276. while (!work_list_.empty()) {
  277. if (!ProcessStep()) {
  278. return false;
  279. }
  280. }
  281. return true;
  282. }
  283. private:
  284. // Adds `type_id` to the work list, if it's not already complete.
  285. auto Push(SemIR::TypeId type_id) -> void {
  286. if (!context_.sem_ir().IsTypeComplete(type_id)) {
  287. work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes});
  288. }
  289. }
  290. // Runs the next step.
  291. auto ProcessStep() -> bool {
  292. auto [type_id, phase] = work_list_.back();
  293. // We might have enqueued the same type more than once. Just skip the
  294. // type if it's already complete.
  295. if (context_.sem_ir().IsTypeComplete(type_id)) {
  296. work_list_.pop_back();
  297. return true;
  298. }
  299. auto node_id = context_.sem_ir().GetTypeAllowBuiltinTypes(type_id);
  300. auto node = context_.nodes().Get(node_id);
  301. auto old_work_list_size = work_list_.size();
  302. switch (phase) {
  303. case Phase::AddNestedIncompleteTypes:
  304. if (!AddNestedIncompleteTypes(node)) {
  305. return false;
  306. }
  307. CARBON_CHECK(work_list_.size() >= old_work_list_size)
  308. << "AddNestedIncompleteTypes should not remove work items";
  309. work_list_[old_work_list_size - 1].phase =
  310. Phase::BuildValueRepresentation;
  311. break;
  312. case Phase::BuildValueRepresentation: {
  313. auto value_rep = BuildValueRepresentation(type_id, node);
  314. context_.sem_ir().CompleteType(type_id, value_rep);
  315. CARBON_CHECK(old_work_list_size == work_list_.size())
  316. << "BuildValueRepresentation should not change work items";
  317. work_list_.pop_back();
  318. // Also complete the value representation type, if necessary. This
  319. // should never fail: the value representation shouldn't require any
  320. // additional nested types to be complete.
  321. if (!context_.sem_ir().IsTypeComplete(value_rep.type_id)) {
  322. work_list_.push_back(
  323. {value_rep.type_id, Phase::BuildValueRepresentation});
  324. }
  325. // For a pointer representation, the pointee also needs to be complete.
  326. if (value_rep.kind == SemIR::ValueRepresentation::Pointer) {
  327. auto pointee_type_id =
  328. context_.sem_ir().GetPointeeType(value_rep.type_id);
  329. if (!context_.sem_ir().IsTypeComplete(pointee_type_id)) {
  330. work_list_.push_back(
  331. {pointee_type_id, Phase::BuildValueRepresentation});
  332. }
  333. }
  334. break;
  335. }
  336. }
  337. return true;
  338. }
  339. // Adds any types nested within `type_node` that need to be complete for
  340. // `type_node` to be complete to our work list.
  341. auto AddNestedIncompleteTypes(SemIR::Node type_node) -> bool {
  342. switch (type_node.kind()) {
  343. case SemIR::ArrayType::Kind:
  344. Push(type_node.As<SemIR::ArrayType>().element_type_id);
  345. break;
  346. case SemIR::StructType::Kind:
  347. for (auto field_id : context_.node_blocks().Get(
  348. type_node.As<SemIR::StructType>().fields_id)) {
  349. Push(context_.nodes()
  350. .GetAs<SemIR::StructTypeField>(field_id)
  351. .field_type_id);
  352. }
  353. break;
  354. case SemIR::TupleType::Kind:
  355. for (auto element_type_id : context_.type_blocks().Get(
  356. type_node.As<SemIR::TupleType>().elements_id)) {
  357. Push(element_type_id);
  358. }
  359. break;
  360. case SemIR::ClassType::Kind:
  361. // TODO: Support class definitions and complete class types.
  362. if (diagnoser_) {
  363. auto builder = (*diagnoser_)();
  364. context_.NoteIncompleteClass(
  365. type_node.As<SemIR::ClassType>().class_id, builder);
  366. builder.Emit();
  367. }
  368. return false;
  369. case SemIR::ConstType::Kind:
  370. Push(type_node.As<SemIR::ConstType>().inner_id);
  371. break;
  372. default:
  373. break;
  374. }
  375. return true;
  376. }
  377. // Makes an empty value representation, which is used for types that have no
  378. // state, such as empty structs and tuples.
  379. auto MakeEmptyRepresentation(Parse::Node parse_node) const
  380. -> SemIR::ValueRepresentation {
  381. return {.kind = SemIR::ValueRepresentation::None,
  382. .type_id = context_.CanonicalizeTupleType(parse_node, {})};
  383. }
  384. // Makes a value representation that uses pass-by-copy, copying the given
  385. // type.
  386. auto MakeCopyRepresentation(SemIR::TypeId rep_id) const
  387. -> SemIR::ValueRepresentation {
  388. return {.kind = SemIR::ValueRepresentation::Copy, .type_id = rep_id};
  389. }
  390. // Makes a value representation that uses pass-by-address with the given
  391. // pointee type.
  392. auto MakePointerRepresentation(Parse::Node parse_node,
  393. SemIR::TypeId pointee_id) const
  394. -> SemIR::ValueRepresentation {
  395. // TODO: Should we add `const` qualification to `pointee_id`?
  396. return {.kind = SemIR::ValueRepresentation::Pointer,
  397. .type_id = context_.GetPointerType(parse_node, pointee_id)};
  398. }
  399. // Gets the value representation of a nested type, which should already be
  400. // complete.
  401. auto GetNestedValueRepresentation(SemIR::TypeId nested_type_id) const {
  402. CARBON_CHECK(context_.sem_ir().IsTypeComplete(nested_type_id))
  403. << "Nested type should already be complete";
  404. auto value_rep = context_.sem_ir().GetValueRepresentation(nested_type_id);
  405. CARBON_CHECK(value_rep.kind != SemIR::ValueRepresentation::Unknown)
  406. << "Complete type should have a value representation";
  407. return value_rep;
  408. };
  409. auto BuildCrossReferenceValueRepresentation(SemIR::TypeId type_id,
  410. SemIR::CrossReference xref) const
  411. -> SemIR::ValueRepresentation {
  412. auto xref_node = context_.sem_ir()
  413. .GetCrossReferenceIR(xref.ir_id)
  414. .nodes()
  415. .Get(xref.node_id);
  416. // The canonical description of a type should only have cross-references
  417. // for entities owned by another File, such as builtins, which are owned
  418. // by the prelude, and named entities like classes and interfaces, which
  419. // we don't support yet.
  420. CARBON_CHECK(xref_node.kind() == SemIR::Builtin::Kind)
  421. << "TODO: Handle other kinds of node cross-references";
  422. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  423. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  424. switch (xref_node.As<SemIR::Builtin>().builtin_kind) {
  425. case SemIR::BuiltinKind::TypeType:
  426. case SemIR::BuiltinKind::Error:
  427. case SemIR::BuiltinKind::Invalid:
  428. case SemIR::BuiltinKind::BoolType:
  429. case SemIR::BuiltinKind::IntegerType:
  430. case SemIR::BuiltinKind::FloatingPointType:
  431. case SemIR::BuiltinKind::NamespaceType:
  432. case SemIR::BuiltinKind::FunctionType:
  433. return MakeCopyRepresentation(type_id);
  434. case SemIR::BuiltinKind::StringType:
  435. // TODO: Decide on string value semantics. This should probably be a
  436. // custom value representation carrying a pointer and size or
  437. // similar.
  438. return MakePointerRepresentation(Parse::Node::Invalid, type_id);
  439. }
  440. llvm_unreachable("All builtin kinds were handled above");
  441. }
  442. auto BuildStructTypeValueRepresentation(SemIR::TypeId type_id,
  443. SemIR::StructType struct_type) const
  444. -> SemIR::ValueRepresentation {
  445. // TODO: Share code with tuples.
  446. auto fields = context_.node_blocks().Get(struct_type.fields_id);
  447. if (fields.empty()) {
  448. return MakeEmptyRepresentation(struct_type.parse_node);
  449. }
  450. // Find the value representation for each field, and construct a struct
  451. // of value representations.
  452. llvm::SmallVector<SemIR::NodeId> value_rep_fields;
  453. value_rep_fields.reserve(fields.size());
  454. bool same_as_object_rep = true;
  455. for (auto field_id : fields) {
  456. auto field = context_.nodes().GetAs<SemIR::StructTypeField>(field_id);
  457. auto field_value_rep = GetNestedValueRepresentation(field.field_type_id);
  458. if (field_value_rep.type_id != field.field_type_id) {
  459. same_as_object_rep = false;
  460. field.field_type_id = field_value_rep.type_id;
  461. field_id = context_.AddNode(field);
  462. }
  463. value_rep_fields.push_back(field_id);
  464. }
  465. auto value_rep = same_as_object_rep
  466. ? type_id
  467. : context_.CanonicalizeStructType(
  468. struct_type.parse_node,
  469. context_.node_blocks().Add(value_rep_fields));
  470. if (fields.size() == 1) {
  471. // The value representation for a struct with a single field is a
  472. // struct containing the value representation of the field.
  473. // TODO: Consider doing the same for structs with multiple small
  474. // fields.
  475. return MakeCopyRepresentation(value_rep);
  476. }
  477. // For a struct with multiple fields, we use a pointer representation.
  478. return MakePointerRepresentation(struct_type.parse_node, value_rep);
  479. }
  480. auto BuildTupleTypeValueRepresentation(SemIR::TypeId type_id,
  481. SemIR::TupleType tuple_type) const
  482. -> SemIR::ValueRepresentation {
  483. // TODO: Share code with structs.
  484. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  485. if (elements.empty()) {
  486. return MakeEmptyRepresentation(tuple_type.parse_node);
  487. }
  488. // Find the value representation for each element, and construct a tuple
  489. // of value representations.
  490. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  491. value_rep_elements.reserve(elements.size());
  492. bool same_as_object_rep = true;
  493. for (auto element_type_id : elements) {
  494. auto element_value_rep = GetNestedValueRepresentation(element_type_id);
  495. if (element_value_rep.type_id != element_type_id) {
  496. same_as_object_rep = false;
  497. }
  498. value_rep_elements.push_back(element_value_rep.type_id);
  499. }
  500. auto value_rep = same_as_object_rep
  501. ? type_id
  502. : context_.CanonicalizeTupleType(tuple_type.parse_node,
  503. value_rep_elements);
  504. if (elements.size() == 1) {
  505. // The value representation for a tuple with a single element is a
  506. // tuple containing the value representation of that element.
  507. // TODO: Consider doing the same for tuples with multiple small
  508. // elements.
  509. return MakeCopyRepresentation(value_rep);
  510. }
  511. // For a tuple with multiple elements, we use a pointer representation.
  512. return MakePointerRepresentation(tuple_type.parse_node, value_rep);
  513. }
  514. // Builds and returns the value representation for the given type. All nested
  515. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  516. auto BuildValueRepresentation(SemIR::TypeId type_id, SemIR::Node node) const
  517. -> SemIR::ValueRepresentation {
  518. // TODO: This can emit new SemIR nodes. Consider emitting them into a
  519. // dedicated file-scope node block where possible, or somewhere else that
  520. // better reflects the definition of the type, rather than wherever the
  521. // type happens to first be required to be complete.
  522. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  523. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  524. switch (node.kind()) {
  525. case SemIR::AddressOf::Kind:
  526. case SemIR::ArrayIndex::Kind:
  527. case SemIR::ArrayInit::Kind:
  528. case SemIR::Assign::Kind:
  529. case SemIR::BinaryOperatorAdd::Kind:
  530. case SemIR::BindName::Kind:
  531. case SemIR::BindValue::Kind:
  532. case SemIR::BlockArg::Kind:
  533. case SemIR::BoolLiteral::Kind:
  534. case SemIR::Branch::Kind:
  535. case SemIR::BranchIf::Kind:
  536. case SemIR::BranchWithArg::Kind:
  537. case SemIR::Call::Kind:
  538. case SemIR::ClassDeclaration::Kind:
  539. case SemIR::Dereference::Kind:
  540. case SemIR::Field::Kind:
  541. case SemIR::FunctionDeclaration::Kind:
  542. case SemIR::InitializeFrom::Kind:
  543. case SemIR::IntegerLiteral::Kind:
  544. case SemIR::NameReference::Kind:
  545. case SemIR::Namespace::Kind:
  546. case SemIR::NoOp::Kind:
  547. case SemIR::Parameter::Kind:
  548. case SemIR::RealLiteral::Kind:
  549. case SemIR::Return::Kind:
  550. case SemIR::ReturnExpression::Kind:
  551. case SemIR::SpliceBlock::Kind:
  552. case SemIR::StringLiteral::Kind:
  553. case SemIR::StructAccess::Kind:
  554. case SemIR::StructTypeField::Kind:
  555. case SemIR::StructLiteral::Kind:
  556. case SemIR::StructInit::Kind:
  557. case SemIR::StructValue::Kind:
  558. case SemIR::Temporary::Kind:
  559. case SemIR::TemporaryStorage::Kind:
  560. case SemIR::TupleAccess::Kind:
  561. case SemIR::TupleIndex::Kind:
  562. case SemIR::TupleLiteral::Kind:
  563. case SemIR::TupleInit::Kind:
  564. case SemIR::TupleValue::Kind:
  565. case SemIR::UnaryOperatorNot::Kind:
  566. case SemIR::ValueAsReference::Kind:
  567. case SemIR::VarStorage::Kind:
  568. CARBON_FATAL() << "Type refers to non-type node " << node;
  569. case SemIR::CrossReference::Kind:
  570. return BuildCrossReferenceValueRepresentation(
  571. type_id, node.As<SemIR::CrossReference>());
  572. case SemIR::ArrayType::Kind: {
  573. // For arrays, it's convenient to always use a pointer representation,
  574. // even when the array has zero or one element, in order to support
  575. // indexing.
  576. return MakePointerRepresentation(node.parse_node(), type_id);
  577. }
  578. case SemIR::StructType::Kind:
  579. return BuildStructTypeValueRepresentation(type_id,
  580. node.As<SemIR::StructType>());
  581. case SemIR::TupleType::Kind:
  582. return BuildTupleTypeValueRepresentation(type_id,
  583. node.As<SemIR::TupleType>());
  584. case SemIR::ClassType::Kind:
  585. // TODO: Support class definitions and complete class types.
  586. CARBON_FATAL() << "Class types are currently never complete";
  587. case SemIR::Builtin::Kind:
  588. CARBON_FATAL() << "Builtins should be named as cross-references";
  589. case SemIR::PointerType::Kind:
  590. case SemIR::UnboundFieldType::Kind:
  591. return MakeCopyRepresentation(type_id);
  592. case SemIR::ConstType::Kind:
  593. // The value representation of `const T` is the same as that of `T`.
  594. // Objects are not modifiable through their value representations.
  595. return GetNestedValueRepresentation(
  596. node.As<SemIR::ConstType>().inner_id);
  597. }
  598. }
  599. enum class Phase : int8_t {
  600. // The next step is to add nested types to the list of types to complete.
  601. AddNestedIncompleteTypes,
  602. // The next step is to build the value representation for the type.
  603. BuildValueRepresentation,
  604. };
  605. struct WorkItem {
  606. SemIR::TypeId type_id;
  607. Phase phase;
  608. };
  609. Context& context_;
  610. llvm::SmallVector<WorkItem> work_list_;
  611. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  612. diagnoser_;
  613. };
  614. } // namespace
  615. auto Context::TryToCompleteType(
  616. SemIR::TypeId type_id,
  617. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser)
  618. -> bool {
  619. return TypeCompleter(*this, diagnoser).Complete(type_id);
  620. }
  621. auto Context::CanonicalizeTypeImpl(
  622. SemIR::NodeKind kind,
  623. llvm::function_ref<void(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  624. llvm::function_ref<SemIR::NodeId()> make_node) -> SemIR::TypeId {
  625. llvm::FoldingSetNodeID canonical_id;
  626. kind.Profile(canonical_id);
  627. profile_type(canonical_id);
  628. void* insert_pos;
  629. auto* node =
  630. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  631. if (node != nullptr) {
  632. return node->type_id();
  633. }
  634. auto node_id = make_node();
  635. auto type_id = types().Add({.node_id = node_id});
  636. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  637. type_node_storage_.push_back(
  638. std::make_unique<TypeNode>(canonical_id, type_id));
  639. // In a debug build, check that our insertion position is still valid. It
  640. // could have been invalidated by a misbehaving `make_node`.
  641. CARBON_DCHECK([&] {
  642. void* check_insert_pos;
  643. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  644. canonical_id, check_insert_pos);
  645. return !check_node && insert_pos == check_insert_pos;
  646. }()) << "Type was created recursively during canonicalization";
  647. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  648. return type_id;
  649. }
  650. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  651. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  652. llvm::FoldingSetNodeID& canonical_id) -> void {
  653. for (auto type_id : type_ids) {
  654. canonical_id.AddInteger(type_id.index);
  655. }
  656. }
  657. // Compute a fingerprint for a type, for use as a key in a folding set.
  658. static auto ProfileType(Context& semantics_context, SemIR::Node node,
  659. llvm::FoldingSetNodeID& canonical_id) -> void {
  660. switch (node.kind()) {
  661. case SemIR::ArrayType::Kind: {
  662. auto array_type = node.As<SemIR::ArrayType>();
  663. canonical_id.AddInteger(
  664. semantics_context.sem_ir().GetArrayBoundValue(array_type.bound_id));
  665. canonical_id.AddInteger(array_type.element_type_id.index);
  666. break;
  667. }
  668. case SemIR::Builtin::Kind:
  669. canonical_id.AddInteger(node.As<SemIR::Builtin>().builtin_kind.AsInt());
  670. break;
  671. case SemIR::ClassType::Kind:
  672. canonical_id.AddInteger(node.As<SemIR::ClassType>().class_id.index);
  673. break;
  674. case SemIR::CrossReference::Kind: {
  675. // TODO: Cross-references should be canonicalized by looking at their
  676. // target rather than treating them as new unique types.
  677. auto xref = node.As<SemIR::CrossReference>();
  678. canonical_id.AddInteger(xref.ir_id.index);
  679. canonical_id.AddInteger(xref.node_id.index);
  680. break;
  681. }
  682. case SemIR::ConstType::Kind:
  683. canonical_id.AddInteger(
  684. semantics_context
  685. .GetUnqualifiedType(node.As<SemIR::ConstType>().inner_id)
  686. .index);
  687. break;
  688. case SemIR::PointerType::Kind:
  689. canonical_id.AddInteger(node.As<SemIR::PointerType>().pointee_id.index);
  690. break;
  691. case SemIR::StructType::Kind: {
  692. auto fields = semantics_context.node_blocks().Get(
  693. node.As<SemIR::StructType>().fields_id);
  694. for (const auto& field_id : fields) {
  695. auto field =
  696. semantics_context.nodes().GetAs<SemIR::StructTypeField>(field_id);
  697. canonical_id.AddInteger(field.name_id.index);
  698. canonical_id.AddInteger(field.field_type_id.index);
  699. }
  700. break;
  701. }
  702. case SemIR::TupleType::Kind:
  703. ProfileTupleType(semantics_context.type_blocks().Get(
  704. node.As<SemIR::TupleType>().elements_id),
  705. canonical_id);
  706. break;
  707. case SemIR::UnboundFieldType::Kind: {
  708. auto unbound_field_type = node.As<SemIR::UnboundFieldType>();
  709. canonical_id.AddInteger(unbound_field_type.class_type_id.index);
  710. canonical_id.AddInteger(unbound_field_type.field_type_id.index);
  711. break;
  712. }
  713. default:
  714. CARBON_FATAL() << "Unexpected type node " << node;
  715. }
  716. }
  717. auto Context::CanonicalizeTypeAndAddNodeIfNew(SemIR::Node node)
  718. -> SemIR::TypeId {
  719. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  720. ProfileType(*this, node, canonical_id);
  721. };
  722. auto make_node = [&] { return AddNode(node); };
  723. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  724. }
  725. auto Context::CanonicalizeType(SemIR::NodeId node_id) -> SemIR::TypeId {
  726. node_id = FollowNameReferences(node_id);
  727. auto it = canonical_types_.find(node_id);
  728. if (it != canonical_types_.end()) {
  729. return it->second;
  730. }
  731. auto node = nodes().Get(node_id);
  732. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  733. ProfileType(*this, node, canonical_id);
  734. };
  735. auto make_node = [&] { return node_id; };
  736. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  737. }
  738. auto Context::CanonicalizeStructType(Parse::Node parse_node,
  739. SemIR::NodeBlockId refs_id)
  740. -> SemIR::TypeId {
  741. return CanonicalizeTypeAndAddNodeIfNew(
  742. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  743. }
  744. auto Context::CanonicalizeTupleType(Parse::Node parse_node,
  745. llvm::ArrayRef<SemIR::TypeId> type_ids)
  746. -> SemIR::TypeId {
  747. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  748. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  749. ProfileTupleType(type_ids, canonical_id);
  750. };
  751. auto make_tuple_node = [&] {
  752. return AddNode(SemIR::TupleType{parse_node, SemIR::TypeId::TypeType,
  753. type_blocks().Add(type_ids)});
  754. };
  755. return CanonicalizeTypeImpl(SemIR::TupleType::Kind, profile_tuple,
  756. make_tuple_node);
  757. }
  758. auto Context::GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId {
  759. CARBON_CHECK(kind != SemIR::BuiltinKind::Invalid);
  760. auto type_id = CanonicalizeType(SemIR::NodeId::ForBuiltin(kind));
  761. // To keep client code simpler, complete builtin types before returning them.
  762. bool complete = TryToCompleteType(type_id);
  763. CARBON_CHECK(complete) << "Failed to complete builtin type";
  764. return type_id;
  765. }
  766. auto Context::GetPointerType(Parse::Node parse_node,
  767. SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  768. return CanonicalizeTypeAndAddNodeIfNew(
  769. SemIR::PointerType{parse_node, SemIR::TypeId::TypeType, pointee_type_id});
  770. }
  771. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  772. SemIR::Node type_node =
  773. nodes().Get(sem_ir_->GetTypeAllowBuiltinTypes(type_id));
  774. if (auto const_type = type_node.TryAs<SemIR::ConstType>()) {
  775. return const_type->inner_id;
  776. }
  777. return type_id;
  778. }
  779. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  780. node_stack_.PrintForStackDump(output);
  781. node_block_stack_.PrintForStackDump(output);
  782. params_or_args_stack_.PrintForStackDump(output);
  783. args_type_info_stack_.PrintForStackDump(output);
  784. }
  785. } // namespace Carbon::Check