context.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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/inst_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/inst.h"
  16. #include "toolchain/sem_ir/inst_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. inst_block_stack_("inst_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::InstId::BuiltinError, SemIR::TypeId::Error});
  34. canonical_types_.insert(
  35. {SemIR::InstId::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(inst_block_stack_.empty()) << inst_block_stack_.size();
  51. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  52. }
  53. auto Context::AddInst(SemIR::Inst inst) -> SemIR::InstId {
  54. auto inst_id = inst_block_stack_.AddInst(inst);
  55. CARBON_VLOG() << "AddInst: " << inst << "\n";
  56. return inst_id;
  57. }
  58. auto Context::AddConstantInst(SemIR::Inst inst) -> SemIR::InstId {
  59. auto inst_id = insts().AddInNoBlock(inst);
  60. constants().Add(inst_id);
  61. CARBON_VLOG() << "AddConstantInst: " << inst << "\n";
  62. return inst_id;
  63. }
  64. auto Context::AddInstAndPush(Parse::Node parse_node, SemIR::Inst inst) -> void {
  65. auto inst_id = AddInst(inst);
  66. node_stack_.Push(parse_node, inst_id);
  67. }
  68. auto Context::DiagnoseDuplicateName(Parse::Node parse_node,
  69. SemIR::InstId prev_def_id) -> void {
  70. CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
  71. "Duplicate name being declared in the same scope.");
  72. CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
  73. "Name is previously declared here.");
  74. auto prev_def = insts().Get(prev_def_id);
  75. emitter_->Build(parse_node, NameDeclarationDuplicate)
  76. .Note(prev_def.parse_node(), NameDeclarationPrevious)
  77. .Emit();
  78. }
  79. auto Context::DiagnoseNameNotFound(Parse::Node parse_node,
  80. SemIR::NameId name_id) -> void {
  81. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  82. llvm::StringRef);
  83. emitter_->Emit(parse_node, NameNotFound, names().GetFormatted(name_id));
  84. }
  85. auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
  86. DiagnosticBuilder& builder) -> void {
  87. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  88. "Class was forward declared here.");
  89. CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
  90. "Class is incomplete within its definition.");
  91. const auto& class_info = classes().Get(class_id);
  92. CARBON_CHECK(!class_info.is_defined()) << "Class is not incomplete";
  93. if (class_info.definition_id.is_valid()) {
  94. builder.Note(insts().Get(class_info.definition_id).parse_node(),
  95. ClassIncompleteWithinDefinition);
  96. } else {
  97. builder.Note(insts().Get(class_info.declaration_id).parse_node(),
  98. ClassForwardDeclaredHere);
  99. }
  100. }
  101. auto Context::AddNameToLookup(Parse::Node name_node, SemIR::NameId name_id,
  102. SemIR::InstId target_id) -> void {
  103. if (current_scope().names.insert(name_id).second) {
  104. // TODO: Reject if we previously performed a failed lookup for this name in
  105. // this scope or a scope nested within it.
  106. auto& lexical_results = name_lookup_[name_id];
  107. CARBON_CHECK(lexical_results.empty() ||
  108. lexical_results.back().scope_index < current_scope_index())
  109. << "Failed to clean up after scope nested within the current scope";
  110. lexical_results.push_back(
  111. {.node_id = target_id, .scope_index = current_scope_index()});
  112. } else {
  113. DiagnoseDuplicateName(name_node, name_lookup_[name_id].back().node_id);
  114. }
  115. }
  116. auto Context::LookupNameInDeclaration(Parse::Node parse_node,
  117. SemIR::NameId name_id,
  118. SemIR::NameScopeId scope_id)
  119. -> SemIR::InstId {
  120. if (scope_id == SemIR::NameScopeId::Invalid) {
  121. // Look for a name in the current scope only. There are two cases where the
  122. // name would be in an outer scope:
  123. //
  124. // - The name is the sole component of the declared name:
  125. //
  126. // class A;
  127. // fn F() {
  128. // class A;
  129. // }
  130. //
  131. // In this case, the inner A is not the same class as the outer A, so
  132. // lookup should not find the outer A.
  133. //
  134. // - The name is a qualifier of some larger declared name:
  135. //
  136. // class A { class B; }
  137. // fn F() {
  138. // class A.B {}
  139. // }
  140. //
  141. // In this case, we're not in the correct scope to define a member of
  142. // class A, so we should reject, and we achieve this by not finding the
  143. // name A from the outer scope.
  144. if (auto name_it = name_lookup_.find(name_id);
  145. name_it != name_lookup_.end()) {
  146. CARBON_CHECK(!name_it->second.empty())
  147. << "Should have been erased: " << names().GetFormatted(name_id);
  148. auto result = name_it->second.back();
  149. if (result.scope_index == current_scope_index()) {
  150. return result.node_id;
  151. }
  152. }
  153. return SemIR::InstId::Invalid;
  154. } else {
  155. // TODO: Once we support `extend`, do not look into `extend`ed scopes here,
  156. // following the same logic as above.
  157. return LookupQualifiedName(parse_node, name_id, scope_id,
  158. /*required=*/false);
  159. }
  160. }
  161. auto Context::LookupUnqualifiedName(Parse::Node parse_node,
  162. SemIR::NameId name_id) -> SemIR::InstId {
  163. // TODO: Check for shadowed lookup results.
  164. // Find the results from enclosing lexical scopes. These will be combined with
  165. // results from non-lexical scopes such as namespaces and classes.
  166. llvm::ArrayRef<LexicalLookupResult> lexical_results;
  167. if (auto name_it = name_lookup_.find(name_id);
  168. name_it != name_lookup_.end()) {
  169. lexical_results = name_it->second;
  170. CARBON_CHECK(!lexical_results.empty())
  171. << "Should have been erased: " << names().GetFormatted(name_id);
  172. }
  173. // Walk the non-lexical scopes and perform lookups into each of them.
  174. for (auto [index, name_scope_id] : llvm::reverse(non_lexical_scope_stack_)) {
  175. // If the innermost lexical result is within this non-lexical scope, then
  176. // it shadows all further non-lexical results and we're done.
  177. if (!lexical_results.empty() &&
  178. lexical_results.back().scope_index > index) {
  179. return lexical_results.back().node_id;
  180. }
  181. auto non_lexical_result =
  182. LookupQualifiedName(parse_node, name_id, name_scope_id,
  183. /*required=*/false);
  184. if (non_lexical_result.is_valid()) {
  185. return non_lexical_result;
  186. }
  187. }
  188. if (!lexical_results.empty()) {
  189. return lexical_results.back().node_id;
  190. }
  191. // We didn't find anything at all.
  192. DiagnoseNameNotFound(parse_node, name_id);
  193. return SemIR::InstId::BuiltinError;
  194. }
  195. auto Context::LookupQualifiedName(Parse::Node parse_node, SemIR::NameId name_id,
  196. SemIR::NameScopeId scope_id, bool required)
  197. -> SemIR::InstId {
  198. CARBON_CHECK(scope_id.is_valid()) << "No scope to perform lookup into";
  199. const auto& scope = name_scopes().Get(scope_id);
  200. auto it = scope.find(name_id);
  201. if (it == scope.end()) {
  202. // TODO: Also perform lookups into `extend`ed scopes.
  203. if (required) {
  204. DiagnoseNameNotFound(parse_node, name_id);
  205. return SemIR::InstId::BuiltinError;
  206. }
  207. return SemIR::InstId::Invalid;
  208. }
  209. return it->second;
  210. }
  211. auto Context::PushScope(SemIR::InstId scope_inst_id,
  212. SemIR::NameScopeId scope_id) -> void {
  213. scope_stack_.push_back({.index = next_scope_index_,
  214. .scope_inst_id = scope_inst_id,
  215. .scope_id = scope_id});
  216. if (scope_id.is_valid()) {
  217. non_lexical_scope_stack_.push_back({next_scope_index_, scope_id});
  218. }
  219. // TODO: Handle this case more gracefully.
  220. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max())
  221. << "Ran out of scopes";
  222. ++next_scope_index_.index;
  223. }
  224. auto Context::PopScope() -> void {
  225. auto scope = scope_stack_.pop_back_val();
  226. for (const auto& str_id : scope.names) {
  227. auto it = name_lookup_.find(str_id);
  228. CARBON_CHECK(it->second.back().scope_index == scope.index)
  229. << "Inconsistent scope index for name " << names().GetFormatted(str_id);
  230. if (it->second.size() == 1) {
  231. // Erase names that no longer resolve.
  232. name_lookup_.erase(it);
  233. } else {
  234. it->second.pop_back();
  235. }
  236. }
  237. if (scope.scope_id.is_valid()) {
  238. CARBON_CHECK(non_lexical_scope_stack_.back().first == scope.index);
  239. non_lexical_scope_stack_.pop_back();
  240. }
  241. }
  242. auto Context::PopToScope(ScopeIndex index) -> void {
  243. while (current_scope_index() > index) {
  244. PopScope();
  245. }
  246. CARBON_CHECK(current_scope_index() == index)
  247. << "Scope index " << index << " does not enclose the current scope "
  248. << current_scope_index();
  249. }
  250. auto Context::FollowNameReferences(SemIR::InstId inst_id) -> SemIR::InstId {
  251. while (auto name_ref = insts().Get(inst_id).TryAs<SemIR::NameReference>()) {
  252. inst_id = name_ref->value_id;
  253. }
  254. return inst_id;
  255. }
  256. auto Context::GetConstantValue(SemIR::InstId inst_id) -> SemIR::InstId {
  257. // TODO: The constant value of an instruction should be computed as we build
  258. // the instruction, or at least cached once computed.
  259. while (true) {
  260. auto inst = insts().Get(inst_id);
  261. switch (inst.kind()) {
  262. case SemIR::NameReference::Kind:
  263. inst_id = inst.As<SemIR::NameReference>().value_id;
  264. break;
  265. case SemIR::BindName::Kind:
  266. inst_id = inst.As<SemIR::BindName>().value_id;
  267. break;
  268. case SemIR::Field::Kind:
  269. case SemIR::FunctionDeclaration::Kind:
  270. return inst_id;
  271. default:
  272. // TODO: Handle the remaining cases.
  273. return SemIR::InstId::Invalid;
  274. }
  275. }
  276. }
  277. template <typename BranchNode, typename... Args>
  278. static auto AddDominatedBlockAndBranchImpl(Context& context,
  279. Parse::Node parse_node, Args... args)
  280. -> SemIR::InstBlockId {
  281. if (!context.inst_block_stack().is_current_block_reachable()) {
  282. return SemIR::InstBlockId::Unreachable;
  283. }
  284. auto block_id = context.inst_blocks().AddDefaultValue();
  285. context.AddInst(BranchNode{parse_node, block_id, args...});
  286. return block_id;
  287. }
  288. auto Context::AddDominatedBlockAndBranch(Parse::Node parse_node)
  289. -> SemIR::InstBlockId {
  290. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  291. }
  292. auto Context::AddDominatedBlockAndBranchWithArg(Parse::Node parse_node,
  293. SemIR::InstId arg_id)
  294. -> SemIR::InstBlockId {
  295. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  296. arg_id);
  297. }
  298. auto Context::AddDominatedBlockAndBranchIf(Parse::Node parse_node,
  299. SemIR::InstId cond_id)
  300. -> SemIR::InstBlockId {
  301. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  302. cond_id);
  303. }
  304. auto Context::AddConvergenceBlockAndPush(Parse::Node parse_node, int num_blocks)
  305. -> void {
  306. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  307. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  308. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  309. if (inst_block_stack().is_current_block_reachable()) {
  310. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  311. new_block_id = inst_blocks().AddDefaultValue();
  312. }
  313. AddInst(SemIR::Branch{parse_node, new_block_id});
  314. }
  315. inst_block_stack().Pop();
  316. }
  317. inst_block_stack().Push(new_block_id);
  318. }
  319. auto Context::AddConvergenceBlockWithArgAndPush(
  320. Parse::Node parse_node, std::initializer_list<SemIR::InstId> block_args)
  321. -> SemIR::InstId {
  322. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  323. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  324. for (auto arg_id : block_args) {
  325. if (inst_block_stack().is_current_block_reachable()) {
  326. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  327. new_block_id = inst_blocks().AddDefaultValue();
  328. }
  329. AddInst(SemIR::BranchWithArg{parse_node, new_block_id, arg_id});
  330. }
  331. inst_block_stack().Pop();
  332. }
  333. inst_block_stack().Push(new_block_id);
  334. // Acquire the result value.
  335. SemIR::TypeId result_type_id = insts().Get(*block_args.begin()).type_id();
  336. return AddInst(SemIR::BlockArg{parse_node, result_type_id, new_block_id});
  337. }
  338. // Add the current code block to the enclosing function.
  339. auto Context::AddCurrentCodeBlockToFunction(Parse::Node parse_node) -> void {
  340. CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
  341. if (return_scope_stack().empty()) {
  342. CARBON_CHECK(parse_node.is_valid())
  343. << "No current function, but parse_node not provided";
  344. TODO(parse_node,
  345. "Control flow expressions are currently only supported inside "
  346. "functions.");
  347. return;
  348. }
  349. if (!inst_block_stack().is_current_block_reachable()) {
  350. // Don't include unreachable blocks in the function.
  351. return;
  352. }
  353. auto function_id =
  354. insts()
  355. .GetAs<SemIR::FunctionDeclaration>(return_scope_stack().back())
  356. .function_id;
  357. functions()
  358. .Get(function_id)
  359. .body_block_ids.push_back(inst_block_stack().PeekOrAdd());
  360. }
  361. auto Context::is_current_position_reachable() -> bool {
  362. if (!inst_block_stack().is_current_block_reachable()) {
  363. return false;
  364. }
  365. // Our current position is at the end of a reachable block. That position is
  366. // reachable unless the previous instruction is a terminator instruction.
  367. auto block_contents = inst_block_stack().PeekCurrentBlockContents();
  368. if (block_contents.empty()) {
  369. return true;
  370. }
  371. const auto& last_inst = insts().Get(block_contents.back());
  372. return last_inst.kind().terminator_kind() !=
  373. SemIR::TerminatorKind::Terminator;
  374. }
  375. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  376. auto Context::ParamOrArgComma() -> void {
  377. ParamOrArgSave(node_stack_.PopExpression());
  378. }
  379. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  380. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  381. ParamOrArgSave(node_stack_.PopExpression());
  382. }
  383. }
  384. auto Context::ParamOrArgPop() -> SemIR::InstBlockId {
  385. return params_or_args_stack_.Pop();
  386. }
  387. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::InstBlockId {
  388. ParamOrArgEndNoPop(start_kind);
  389. return ParamOrArgPop();
  390. }
  391. namespace {
  392. // Worklist-based type completion mechanism.
  393. //
  394. // When attempting to complete a type, we may find other types that also need to
  395. // be completed: types nested within that type, and the value representation of
  396. // the type. In order to complete a type without recursing arbitrarily deeply,
  397. // we use a worklist of tasks:
  398. //
  399. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  400. // nested within a type to the work list.
  401. // - A `BuildValueRepresentation` step computes the value representation for a
  402. // type, once all of its nested types are complete, and marks the type as
  403. // complete.
  404. class TypeCompleter {
  405. public:
  406. TypeCompleter(
  407. Context& context,
  408. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  409. diagnoser)
  410. : context_(context), diagnoser_(diagnoser) {}
  411. // Attempts to complete the given type. Returns true if it is now complete,
  412. // false if it could not be completed.
  413. auto Complete(SemIR::TypeId type_id) -> bool {
  414. Push(type_id);
  415. while (!work_list_.empty()) {
  416. if (!ProcessStep()) {
  417. return false;
  418. }
  419. }
  420. return true;
  421. }
  422. private:
  423. // Adds `type_id` to the work list, if it's not already complete.
  424. auto Push(SemIR::TypeId type_id) -> void {
  425. if (!context_.sem_ir().IsTypeComplete(type_id)) {
  426. work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes});
  427. }
  428. }
  429. // Runs the next step.
  430. auto ProcessStep() -> bool {
  431. auto [type_id, phase] = work_list_.back();
  432. // We might have enqueued the same type more than once. Just skip the
  433. // type if it's already complete.
  434. if (context_.sem_ir().IsTypeComplete(type_id)) {
  435. work_list_.pop_back();
  436. return true;
  437. }
  438. auto inst_id = context_.sem_ir().GetTypeAllowBuiltinTypes(type_id);
  439. auto inst = context_.insts().Get(inst_id);
  440. auto old_work_list_size = work_list_.size();
  441. switch (phase) {
  442. case Phase::AddNestedIncompleteTypes:
  443. if (!AddNestedIncompleteTypes(inst)) {
  444. return false;
  445. }
  446. CARBON_CHECK(work_list_.size() >= old_work_list_size)
  447. << "AddNestedIncompleteTypes should not remove work items";
  448. work_list_[old_work_list_size - 1].phase =
  449. Phase::BuildValueRepresentation;
  450. break;
  451. case Phase::BuildValueRepresentation: {
  452. auto value_rep = BuildValueRepresentation(type_id, inst);
  453. context_.sem_ir().CompleteType(type_id, value_rep);
  454. CARBON_CHECK(old_work_list_size == work_list_.size())
  455. << "BuildValueRepresentation should not change work items";
  456. work_list_.pop_back();
  457. // Also complete the value representation type, if necessary. This
  458. // should never fail: the value representation shouldn't require any
  459. // additional nested types to be complete.
  460. if (!context_.sem_ir().IsTypeComplete(value_rep.type_id)) {
  461. work_list_.push_back(
  462. {value_rep.type_id, Phase::BuildValueRepresentation});
  463. }
  464. // For a pointer representation, the pointee also needs to be complete.
  465. if (value_rep.kind == SemIR::ValueRepresentation::Pointer) {
  466. auto pointee_type_id =
  467. context_.sem_ir().GetPointeeType(value_rep.type_id);
  468. if (!context_.sem_ir().IsTypeComplete(pointee_type_id)) {
  469. work_list_.push_back(
  470. {pointee_type_id, Phase::BuildValueRepresentation});
  471. }
  472. }
  473. break;
  474. }
  475. }
  476. return true;
  477. }
  478. // Adds any types nested within `type_inst` that need to be complete for
  479. // `type_inst` to be complete to our work list.
  480. auto AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool {
  481. switch (type_inst.kind()) {
  482. case SemIR::ArrayType::Kind:
  483. Push(type_inst.As<SemIR::ArrayType>().element_type_id);
  484. break;
  485. case SemIR::StructType::Kind:
  486. for (auto field_id : context_.inst_blocks().Get(
  487. type_inst.As<SemIR::StructType>().fields_id)) {
  488. Push(context_.insts()
  489. .GetAs<SemIR::StructTypeField>(field_id)
  490. .field_type_id);
  491. }
  492. break;
  493. case SemIR::TupleType::Kind:
  494. for (auto element_type_id : context_.type_blocks().Get(
  495. type_inst.As<SemIR::TupleType>().elements_id)) {
  496. Push(element_type_id);
  497. }
  498. break;
  499. case SemIR::ClassType::Kind: {
  500. auto class_type = type_inst.As<SemIR::ClassType>();
  501. auto& class_info = context_.classes().Get(class_type.class_id);
  502. if (!class_info.is_defined()) {
  503. if (diagnoser_) {
  504. auto builder = (*diagnoser_)();
  505. context_.NoteIncompleteClass(class_type.class_id, builder);
  506. builder.Emit();
  507. }
  508. return false;
  509. }
  510. Push(class_info.object_representation_id);
  511. break;
  512. }
  513. case SemIR::ConstType::Kind:
  514. Push(type_inst.As<SemIR::ConstType>().inner_id);
  515. break;
  516. default:
  517. break;
  518. }
  519. return true;
  520. }
  521. // Makes an empty value representation, which is used for types that have no
  522. // state, such as empty structs and tuples.
  523. auto MakeEmptyRepresentation(Parse::Node parse_node) const
  524. -> SemIR::ValueRepresentation {
  525. return {.kind = SemIR::ValueRepresentation::None,
  526. .type_id = context_.CanonicalizeTupleType(parse_node, {})};
  527. }
  528. // Makes a value representation that uses pass-by-copy, copying the given
  529. // type.
  530. auto MakeCopyRepresentation(
  531. SemIR::TypeId rep_id,
  532. SemIR::ValueRepresentation::AggregateKind aggregate_kind =
  533. SemIR::ValueRepresentation::NotAggregate) const
  534. -> SemIR::ValueRepresentation {
  535. return {.kind = SemIR::ValueRepresentation::Copy,
  536. .aggregate_kind = aggregate_kind,
  537. .type_id = rep_id};
  538. }
  539. // Makes a value representation that uses pass-by-address with the given
  540. // pointee type.
  541. auto MakePointerRepresentation(
  542. Parse::Node parse_node, SemIR::TypeId pointee_id,
  543. SemIR::ValueRepresentation::AggregateKind aggregate_kind =
  544. SemIR::ValueRepresentation::NotAggregate) const
  545. -> SemIR::ValueRepresentation {
  546. // TODO: Should we add `const` qualification to `pointee_id`?
  547. return {.kind = SemIR::ValueRepresentation::Pointer,
  548. .aggregate_kind = aggregate_kind,
  549. .type_id = context_.GetPointerType(parse_node, pointee_id)};
  550. }
  551. // Gets the value representation of a nested type, which should already be
  552. // complete.
  553. auto GetNestedValueRepresentation(SemIR::TypeId nested_type_id) const {
  554. CARBON_CHECK(context_.sem_ir().IsTypeComplete(nested_type_id))
  555. << "Nested type should already be complete";
  556. auto value_rep = context_.sem_ir().GetValueRepresentation(nested_type_id);
  557. CARBON_CHECK(value_rep.kind != SemIR::ValueRepresentation::Unknown)
  558. << "Complete type should have a value representation";
  559. return value_rep;
  560. };
  561. auto BuildCrossReferenceValueRepresentation(SemIR::TypeId type_id,
  562. SemIR::CrossReference xref) const
  563. -> SemIR::ValueRepresentation {
  564. auto xref_inst = context_.sem_ir()
  565. .GetCrossReferenceIR(xref.ir_id)
  566. .insts()
  567. .Get(xref.inst_id);
  568. // The canonical description of a type should only have cross-references
  569. // for entities owned by another File, such as builtins, which are owned
  570. // by the prelude, and named entities like classes and interfaces, which
  571. // we don't support yet.
  572. CARBON_CHECK(xref_inst.kind() == SemIR::Builtin::Kind)
  573. << "TODO: Handle other kinds of inst cross-references";
  574. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  575. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  576. switch (xref_inst.As<SemIR::Builtin>().builtin_kind) {
  577. case SemIR::BuiltinKind::TypeType:
  578. case SemIR::BuiltinKind::Error:
  579. case SemIR::BuiltinKind::Invalid:
  580. case SemIR::BuiltinKind::BoolType:
  581. case SemIR::BuiltinKind::IntegerType:
  582. case SemIR::BuiltinKind::FloatingPointType:
  583. case SemIR::BuiltinKind::NamespaceType:
  584. case SemIR::BuiltinKind::FunctionType:
  585. case SemIR::BuiltinKind::BoundMethodType:
  586. return MakeCopyRepresentation(type_id);
  587. case SemIR::BuiltinKind::StringType:
  588. // TODO: Decide on string value semantics. This should probably be a
  589. // custom value representation carrying a pointer and size or
  590. // similar.
  591. return MakePointerRepresentation(Parse::Node::Invalid, type_id);
  592. }
  593. llvm_unreachable("All builtin kinds were handled above");
  594. }
  595. auto BuildStructOrTupleValueRepresentation(Parse::Node parse_node,
  596. std::size_t num_elements,
  597. SemIR::TypeId elementwise_rep,
  598. bool same_as_object_rep) const
  599. -> SemIR::ValueRepresentation {
  600. SemIR::ValueRepresentation::AggregateKind aggregate_kind =
  601. same_as_object_rep ? SemIR::ValueRepresentation::ValueAndObjectAggregate
  602. : SemIR::ValueRepresentation::ValueAggregate;
  603. if (num_elements == 1) {
  604. // The value representation for a struct or tuple with a single element
  605. // is a struct or tuple containing the value representation of the
  606. // element.
  607. // TODO: Consider doing the same whenever `elementwise_rep` is
  608. // sufficiently small.
  609. return MakeCopyRepresentation(elementwise_rep, aggregate_kind);
  610. }
  611. // For a struct or tuple with multiple fields, we use a pointer
  612. // to the elementwise value representation.
  613. return MakePointerRepresentation(parse_node, elementwise_rep,
  614. aggregate_kind);
  615. }
  616. auto BuildStructTypeValueRepresentation(SemIR::TypeId type_id,
  617. SemIR::StructType struct_type) const
  618. -> SemIR::ValueRepresentation {
  619. // TODO: Share more code with tuples.
  620. auto fields = context_.inst_blocks().Get(struct_type.fields_id);
  621. if (fields.empty()) {
  622. return MakeEmptyRepresentation(struct_type.parse_node);
  623. }
  624. // Find the value representation for each field, and construct a struct
  625. // of value representations.
  626. llvm::SmallVector<SemIR::InstId> value_rep_fields;
  627. value_rep_fields.reserve(fields.size());
  628. bool same_as_object_rep = true;
  629. for (auto field_id : fields) {
  630. auto field = context_.insts().GetAs<SemIR::StructTypeField>(field_id);
  631. auto field_value_rep = GetNestedValueRepresentation(field.field_type_id);
  632. if (field_value_rep.type_id != field.field_type_id) {
  633. same_as_object_rep = false;
  634. field.field_type_id = field_value_rep.type_id;
  635. field_id = context_.AddConstantInst(field);
  636. }
  637. value_rep_fields.push_back(field_id);
  638. }
  639. auto value_rep = same_as_object_rep
  640. ? type_id
  641. : context_.CanonicalizeStructType(
  642. struct_type.parse_node,
  643. context_.inst_blocks().Add(value_rep_fields));
  644. return BuildStructOrTupleValueRepresentation(
  645. struct_type.parse_node, fields.size(), value_rep, same_as_object_rep);
  646. }
  647. auto BuildTupleTypeValueRepresentation(SemIR::TypeId type_id,
  648. SemIR::TupleType tuple_type) const
  649. -> SemIR::ValueRepresentation {
  650. // TODO: Share more code with structs.
  651. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  652. if (elements.empty()) {
  653. return MakeEmptyRepresentation(tuple_type.parse_node);
  654. }
  655. // Find the value representation for each element, and construct a tuple
  656. // of value representations.
  657. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  658. value_rep_elements.reserve(elements.size());
  659. bool same_as_object_rep = true;
  660. for (auto element_type_id : elements) {
  661. auto element_value_rep = GetNestedValueRepresentation(element_type_id);
  662. if (element_value_rep.type_id != element_type_id) {
  663. same_as_object_rep = false;
  664. }
  665. value_rep_elements.push_back(element_value_rep.type_id);
  666. }
  667. auto value_rep = same_as_object_rep
  668. ? type_id
  669. : context_.CanonicalizeTupleType(tuple_type.parse_node,
  670. value_rep_elements);
  671. return BuildStructOrTupleValueRepresentation(
  672. tuple_type.parse_node, elements.size(), value_rep, same_as_object_rep);
  673. }
  674. // Builds and returns the value representation for the given type. All nested
  675. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  676. auto BuildValueRepresentation(SemIR::TypeId type_id, SemIR::Inst inst) const
  677. -> SemIR::ValueRepresentation {
  678. // TODO: This can emit new SemIR instructions. Consider emitting them into a
  679. // dedicated file-scope instruction block where possible, or somewhere else
  680. // that better reflects the definition of the type, rather than wherever the
  681. // type happens to first be required to be complete.
  682. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  683. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  684. switch (inst.kind()) {
  685. case SemIR::AddressOf::Kind:
  686. case SemIR::ArrayIndex::Kind:
  687. case SemIR::ArrayInit::Kind:
  688. case SemIR::Assign::Kind:
  689. case SemIR::BinaryOperatorAdd::Kind:
  690. case SemIR::BindName::Kind:
  691. case SemIR::BindValue::Kind:
  692. case SemIR::BlockArg::Kind:
  693. case SemIR::BoolLiteral::Kind:
  694. case SemIR::BoundMethod::Kind:
  695. case SemIR::Branch::Kind:
  696. case SemIR::BranchIf::Kind:
  697. case SemIR::BranchWithArg::Kind:
  698. case SemIR::Call::Kind:
  699. case SemIR::ClassDeclaration::Kind:
  700. case SemIR::ClassFieldAccess::Kind:
  701. case SemIR::ClassInit::Kind:
  702. case SemIR::Converted::Kind:
  703. case SemIR::Dereference::Kind:
  704. case SemIR::Field::Kind:
  705. case SemIR::FunctionDeclaration::Kind:
  706. case SemIR::InitializeFrom::Kind:
  707. case SemIR::IntegerLiteral::Kind:
  708. case SemIR::NameReference::Kind:
  709. case SemIR::Namespace::Kind:
  710. case SemIR::NoOp::Kind:
  711. case SemIR::Parameter::Kind:
  712. case SemIR::RealLiteral::Kind:
  713. case SemIR::Return::Kind:
  714. case SemIR::ReturnExpression::Kind:
  715. case SemIR::SelfParameter::Kind:
  716. case SemIR::SpliceBlock::Kind:
  717. case SemIR::StringLiteral::Kind:
  718. case SemIR::StructAccess::Kind:
  719. case SemIR::StructTypeField::Kind:
  720. case SemIR::StructLiteral::Kind:
  721. case SemIR::StructInit::Kind:
  722. case SemIR::StructValue::Kind:
  723. case SemIR::Temporary::Kind:
  724. case SemIR::TemporaryStorage::Kind:
  725. case SemIR::TupleAccess::Kind:
  726. case SemIR::TupleIndex::Kind:
  727. case SemIR::TupleLiteral::Kind:
  728. case SemIR::TupleInit::Kind:
  729. case SemIR::TupleValue::Kind:
  730. case SemIR::UnaryOperatorNot::Kind:
  731. case SemIR::ValueAsReference::Kind:
  732. case SemIR::ValueOfInitializer::Kind:
  733. case SemIR::VarStorage::Kind:
  734. CARBON_FATAL() << "Type refers to non-type inst " << inst;
  735. case SemIR::CrossReference::Kind:
  736. return BuildCrossReferenceValueRepresentation(
  737. type_id, inst.As<SemIR::CrossReference>());
  738. case SemIR::ArrayType::Kind: {
  739. // For arrays, it's convenient to always use a pointer representation,
  740. // even when the array has zero or one element, in order to support
  741. // indexing.
  742. return MakePointerRepresentation(
  743. inst.parse_node(), type_id,
  744. SemIR::ValueRepresentation::ObjectAggregate);
  745. }
  746. case SemIR::StructType::Kind:
  747. return BuildStructTypeValueRepresentation(type_id,
  748. inst.As<SemIR::StructType>());
  749. case SemIR::TupleType::Kind:
  750. return BuildTupleTypeValueRepresentation(type_id,
  751. inst.As<SemIR::TupleType>());
  752. case SemIR::ClassType::Kind:
  753. // The value representation for a class is a pointer to the object
  754. // representation.
  755. // TODO: Support customized value representations for classes.
  756. // TODO: Pick a better value representation when possible.
  757. return MakePointerRepresentation(
  758. inst.parse_node(),
  759. context_.classes()
  760. .Get(inst.As<SemIR::ClassType>().class_id)
  761. .object_representation_id,
  762. SemIR::ValueRepresentation::ObjectAggregate);
  763. case SemIR::Builtin::Kind:
  764. CARBON_FATAL() << "Builtins should be named as cross-references";
  765. case SemIR::PointerType::Kind:
  766. case SemIR::UnboundFieldType::Kind:
  767. return MakeCopyRepresentation(type_id);
  768. case SemIR::ConstType::Kind:
  769. // The value representation of `const T` is the same as that of `T`.
  770. // Objects are not modifiable through their value representations.
  771. return GetNestedValueRepresentation(
  772. inst.As<SemIR::ConstType>().inner_id);
  773. }
  774. }
  775. enum class Phase : int8_t {
  776. // The next step is to add nested types to the list of types to complete.
  777. AddNestedIncompleteTypes,
  778. // The next step is to build the value representation for the type.
  779. BuildValueRepresentation,
  780. };
  781. struct WorkItem {
  782. SemIR::TypeId type_id;
  783. Phase phase;
  784. };
  785. Context& context_;
  786. llvm::SmallVector<WorkItem> work_list_;
  787. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  788. diagnoser_;
  789. };
  790. } // namespace
  791. auto Context::TryToCompleteType(
  792. SemIR::TypeId type_id,
  793. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser)
  794. -> bool {
  795. return TypeCompleter(*this, diagnoser).Complete(type_id);
  796. }
  797. auto Context::CanonicalizeTypeImpl(
  798. SemIR::InstKind kind,
  799. llvm::function_ref<bool(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  800. llvm::function_ref<SemIR::InstId()> make_inst) -> SemIR::TypeId {
  801. llvm::FoldingSetNodeID canonical_id;
  802. kind.Profile(canonical_id);
  803. if (!profile_type(canonical_id)) {
  804. return SemIR::TypeId::Error;
  805. }
  806. void* insert_pos;
  807. auto* node =
  808. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  809. if (node != nullptr) {
  810. return node->type_id();
  811. }
  812. auto inst_id = make_inst();
  813. auto type_id = types().Add({.inst_id = inst_id});
  814. CARBON_CHECK(canonical_types_.insert({inst_id, type_id}).second);
  815. type_node_storage_.push_back(
  816. std::make_unique<TypeNode>(canonical_id, type_id));
  817. // In a debug build, check that our insertion position is still valid. It
  818. // could have been invalidated by a misbehaving `make_inst`.
  819. CARBON_DCHECK([&] {
  820. void* check_insert_pos;
  821. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  822. canonical_id, check_insert_pos);
  823. return !check_node && insert_pos == check_insert_pos;
  824. }()) << "Type was created recursively during canonicalization";
  825. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  826. return type_id;
  827. }
  828. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  829. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  830. llvm::FoldingSetNodeID& canonical_id) -> void {
  831. for (auto type_id : type_ids) {
  832. canonical_id.AddInteger(type_id.index);
  833. }
  834. }
  835. // Compute a fingerprint for a type, for use as a key in a folding set. Returns
  836. // false if not supported, which is presently the case for compile-time
  837. // expressions.
  838. // TODO: Once support is more complete, in particular ensuring that various
  839. // valid compile-time expressions are supported, it may be desirable to switch
  840. // the default to a CARBON_FATAL error.
  841. static auto ProfileType(Context& semantics_context, SemIR::Inst inst,
  842. llvm::FoldingSetNodeID& canonical_id) -> bool {
  843. switch (inst.kind()) {
  844. case SemIR::ArrayType::Kind: {
  845. auto array_type = inst.As<SemIR::ArrayType>();
  846. canonical_id.AddInteger(
  847. semantics_context.sem_ir().GetArrayBoundValue(array_type.bound_id));
  848. canonical_id.AddInteger(array_type.element_type_id.index);
  849. break;
  850. }
  851. case SemIR::Builtin::Kind:
  852. canonical_id.AddInteger(inst.As<SemIR::Builtin>().builtin_kind.AsInt());
  853. break;
  854. case SemIR::ClassType::Kind:
  855. canonical_id.AddInteger(inst.As<SemIR::ClassType>().class_id.index);
  856. break;
  857. case SemIR::CrossReference::Kind: {
  858. // TODO: Cross-references should be canonicalized by looking at their
  859. // target rather than treating them as new unique types.
  860. auto xref = inst.As<SemIR::CrossReference>();
  861. canonical_id.AddInteger(xref.ir_id.index);
  862. canonical_id.AddInteger(xref.inst_id.index);
  863. break;
  864. }
  865. case SemIR::ConstType::Kind:
  866. canonical_id.AddInteger(
  867. semantics_context
  868. .GetUnqualifiedType(inst.As<SemIR::ConstType>().inner_id)
  869. .index);
  870. break;
  871. case SemIR::PointerType::Kind:
  872. canonical_id.AddInteger(inst.As<SemIR::PointerType>().pointee_id.index);
  873. break;
  874. case SemIR::StructType::Kind: {
  875. auto fields = semantics_context.inst_blocks().Get(
  876. inst.As<SemIR::StructType>().fields_id);
  877. for (const auto& field_id : fields) {
  878. auto field =
  879. semantics_context.insts().GetAs<SemIR::StructTypeField>(field_id);
  880. canonical_id.AddInteger(field.name_id.index);
  881. canonical_id.AddInteger(field.field_type_id.index);
  882. }
  883. break;
  884. }
  885. case SemIR::TupleType::Kind:
  886. ProfileTupleType(semantics_context.type_blocks().Get(
  887. inst.As<SemIR::TupleType>().elements_id),
  888. canonical_id);
  889. break;
  890. case SemIR::UnboundFieldType::Kind: {
  891. auto unbound_field_type = inst.As<SemIR::UnboundFieldType>();
  892. canonical_id.AddInteger(unbound_field_type.class_type_id.index);
  893. canonical_id.AddInteger(unbound_field_type.field_type_id.index);
  894. break;
  895. }
  896. default: {
  897. // Right now, this is only expected to occur in calls from
  898. // ExpressionAsType. Diagnostics are issued there.
  899. return false;
  900. }
  901. }
  902. return true;
  903. }
  904. auto Context::CanonicalizeTypeAndAddInstIfNew(SemIR::Inst inst)
  905. -> SemIR::TypeId {
  906. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  907. return ProfileType(*this, inst, canonical_id);
  908. };
  909. auto make_inst = [&] { return AddConstantInst(inst); };
  910. return CanonicalizeTypeImpl(inst.kind(), profile_node, make_inst);
  911. }
  912. auto Context::CanonicalizeType(SemIR::InstId inst_id) -> SemIR::TypeId {
  913. while (auto converted = insts().Get(inst_id).TryAs<SemIR::Converted>()) {
  914. inst_id = converted->result_id;
  915. }
  916. inst_id = FollowNameReferences(inst_id);
  917. auto it = canonical_types_.find(inst_id);
  918. if (it != canonical_types_.end()) {
  919. return it->second;
  920. }
  921. auto inst = insts().Get(inst_id);
  922. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  923. return ProfileType(*this, inst, canonical_id);
  924. };
  925. auto make_inst = [&] { return inst_id; };
  926. return CanonicalizeTypeImpl(inst.kind(), profile_node, make_inst);
  927. }
  928. auto Context::CanonicalizeStructType(Parse::Node parse_node,
  929. SemIR::InstBlockId refs_id)
  930. -> SemIR::TypeId {
  931. return CanonicalizeTypeAndAddInstIfNew(
  932. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  933. }
  934. auto Context::CanonicalizeTupleType(Parse::Node parse_node,
  935. llvm::ArrayRef<SemIR::TypeId> type_ids)
  936. -> SemIR::TypeId {
  937. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  938. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  939. ProfileTupleType(type_ids, canonical_id);
  940. return true;
  941. };
  942. auto make_tuple_inst = [&] {
  943. return AddConstantInst(SemIR::TupleType{parse_node, SemIR::TypeId::TypeType,
  944. type_blocks().Add(type_ids)});
  945. };
  946. return CanonicalizeTypeImpl(SemIR::TupleType::Kind, profile_tuple,
  947. make_tuple_inst);
  948. }
  949. auto Context::GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId {
  950. CARBON_CHECK(kind != SemIR::BuiltinKind::Invalid);
  951. auto type_id = CanonicalizeType(SemIR::InstId::ForBuiltin(kind));
  952. // To keep client code simpler, complete builtin types before returning them.
  953. bool complete = TryToCompleteType(type_id);
  954. CARBON_CHECK(complete) << "Failed to complete builtin type";
  955. return type_id;
  956. }
  957. auto Context::GetPointerType(Parse::Node parse_node,
  958. SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  959. return CanonicalizeTypeAndAddInstIfNew(
  960. SemIR::PointerType{parse_node, SemIR::TypeId::TypeType, pointee_type_id});
  961. }
  962. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  963. SemIR::Inst type_inst =
  964. insts().Get(sem_ir_->GetTypeAllowBuiltinTypes(type_id));
  965. if (auto const_type = type_inst.TryAs<SemIR::ConstType>()) {
  966. return const_type->inner_id;
  967. }
  968. return type_id;
  969. }
  970. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  971. node_stack_.PrintForStackDump(output);
  972. inst_block_stack_.PrintForStackDump(output);
  973. params_or_args_stack_.PrintForStackDump(output);
  974. args_type_info_stack_.PrintForStackDump(output);
  975. }
  976. } // namespace Carbon::Check