context.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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/decl_name_stack.h"
  11. #include "toolchain/check/eval.h"
  12. #include "toolchain/check/import_ref.h"
  13. #include "toolchain/check/inst_block_stack.h"
  14. #include "toolchain/lex/tokenized_buffer.h"
  15. #include "toolchain/parse/node_ids.h"
  16. #include "toolchain/parse/node_kind.h"
  17. #include "toolchain/sem_ir/builtin_kind.h"
  18. #include "toolchain/sem_ir/file.h"
  19. #include "toolchain/sem_ir/ids.h"
  20. #include "toolchain/sem_ir/inst.h"
  21. #include "toolchain/sem_ir/inst_kind.h"
  22. #include "toolchain/sem_ir/typed_insts.h"
  23. #include "toolchain/sem_ir/value_stores.h"
  24. namespace Carbon::Check {
  25. Context::Context(const Lex::TokenizedBuffer& tokens, DiagnosticEmitter& emitter,
  26. const Parse::Tree& parse_tree, SemIR::File& sem_ir,
  27. llvm::raw_ostream* vlog_stream)
  28. : tokens_(&tokens),
  29. emitter_(&emitter),
  30. parse_tree_(&parse_tree),
  31. sem_ir_(&sem_ir),
  32. vlog_stream_(vlog_stream),
  33. node_stack_(parse_tree, vlog_stream),
  34. inst_block_stack_("inst_block_stack_", sem_ir, vlog_stream),
  35. params_or_args_stack_("params_or_args_stack_", sem_ir, vlog_stream),
  36. args_type_info_stack_("args_type_info_stack_", sem_ir, vlog_stream),
  37. decl_name_stack_(this),
  38. scope_stack_(sem_ir_->identifiers()) {
  39. // Map the builtin `<error>` and `type` type constants to their corresponding
  40. // special `TypeId` values.
  41. type_ids_for_type_constants_.insert(
  42. {SemIR::ConstantId::ForTemplateConstant(SemIR::InstId::BuiltinError),
  43. SemIR::TypeId::Error});
  44. type_ids_for_type_constants_.insert(
  45. {SemIR::ConstantId::ForTemplateConstant(SemIR::InstId::BuiltinTypeType),
  46. SemIR::TypeId::TypeType});
  47. }
  48. auto Context::TODO(Parse::NodeId parse_node, std::string label) -> bool {
  49. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  50. std::string);
  51. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  52. return false;
  53. }
  54. auto Context::VerifyOnFinish() -> void {
  55. // Information in all the various context objects should be cleaned up as
  56. // various pieces of context go out of scope. At this point, nothing should
  57. // remain.
  58. // node_stack_ will still contain top-level entities.
  59. scope_stack_.VerifyOnFinish();
  60. CARBON_CHECK(inst_block_stack_.empty()) << inst_block_stack_.size();
  61. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  62. }
  63. auto Context::AddInstInNoBlock(SemIR::ParseNodeAndInst parse_node_and_inst)
  64. -> SemIR::InstId {
  65. auto inst_id = sem_ir().insts().AddInNoBlock(parse_node_and_inst);
  66. CARBON_VLOG() << "AddInst: " << parse_node_and_inst.inst << "\n";
  67. auto const_id = TryEvalInst(*this, inst_id, parse_node_and_inst.inst);
  68. if (const_id.is_constant()) {
  69. CARBON_VLOG() << "Constant: " << parse_node_and_inst.inst << " -> "
  70. << const_id.inst_id() << "\n";
  71. constant_values().Set(inst_id, const_id);
  72. }
  73. return inst_id;
  74. }
  75. auto Context::AddInst(SemIR::ParseNodeAndInst parse_node_and_inst)
  76. -> SemIR::InstId {
  77. auto inst_id = AddInstInNoBlock(parse_node_and_inst);
  78. inst_block_stack_.AddInstId(inst_id);
  79. return inst_id;
  80. }
  81. auto Context::AddPlaceholderInstInNoBlock(
  82. SemIR::ParseNodeAndInst parse_node_and_inst) -> SemIR::InstId {
  83. auto inst_id = sem_ir().insts().AddInNoBlock(parse_node_and_inst);
  84. CARBON_VLOG() << "AddPlaceholderInst: " << parse_node_and_inst.inst << "\n";
  85. constant_values().Set(inst_id, SemIR::ConstantId::Invalid);
  86. return inst_id;
  87. }
  88. auto Context::AddPlaceholderInst(SemIR::ParseNodeAndInst parse_node_and_inst)
  89. -> SemIR::InstId {
  90. auto inst_id = AddPlaceholderInstInNoBlock(parse_node_and_inst);
  91. inst_block_stack_.AddInstId(inst_id);
  92. return inst_id;
  93. }
  94. auto Context::AddConstant(SemIR::Inst inst, bool is_symbolic)
  95. -> SemIR::ConstantId {
  96. auto const_id = constants().GetOrAdd(inst, is_symbolic);
  97. CARBON_VLOG() << "AddConstant: " << inst << "\n";
  98. return const_id;
  99. }
  100. auto Context::AddInstAndPush(SemIR::ParseNodeAndInst parse_node_and_inst)
  101. -> void {
  102. auto inst_id = AddInst(parse_node_and_inst);
  103. node_stack_.Push(parse_node_and_inst.parse_node, inst_id);
  104. }
  105. auto Context::ReplaceInstBeforeConstantUse(
  106. SemIR::InstId inst_id, SemIR::ParseNodeAndInst parse_node_and_inst)
  107. -> void {
  108. sem_ir().insts().Set(inst_id, parse_node_and_inst);
  109. CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> "
  110. << parse_node_and_inst.inst << "\n";
  111. // Redo evaluation. This is only safe to do if this instruction has not
  112. // already been used as a constant, which is the caller's responsibility to
  113. // ensure.
  114. auto const_id = TryEvalInst(*this, inst_id, parse_node_and_inst.inst);
  115. if (const_id.is_constant()) {
  116. CARBON_VLOG() << "Constant: " << parse_node_and_inst.inst << " -> "
  117. << const_id.inst_id() << "\n";
  118. }
  119. constant_values().Set(inst_id, const_id);
  120. }
  121. auto Context::DiagnoseDuplicateName(SemIR::InstId dup_def_id,
  122. SemIR::InstId prev_def_id) -> void {
  123. CARBON_DIAGNOSTIC(NameDeclDuplicate, Error,
  124. "Duplicate name being declared in the same scope.");
  125. CARBON_DIAGNOSTIC(NameDeclPrevious, Note,
  126. "Name is previously declared here.");
  127. emitter_->Build(dup_def_id, NameDeclDuplicate)
  128. .Note(prev_def_id, NameDeclPrevious)
  129. .Emit();
  130. }
  131. auto Context::DiagnoseNameNotFound(Parse::NodeId parse_node,
  132. SemIR::NameId name_id) -> void {
  133. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  134. SemIR::NameId);
  135. emitter_->Emit(parse_node, NameNotFound, name_id);
  136. }
  137. auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
  138. DiagnosticBuilder& builder) -> void {
  139. const auto& class_info = classes().Get(class_id);
  140. CARBON_CHECK(!class_info.is_defined()) << "Class is not incomplete";
  141. if (class_info.definition_id.is_valid()) {
  142. CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
  143. "Class is incomplete within its definition.");
  144. builder.Note(class_info.definition_id, ClassIncompleteWithinDefinition);
  145. } else {
  146. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  147. "Class was forward declared here.");
  148. builder.Note(class_info.decl_id, ClassForwardDeclaredHere);
  149. }
  150. }
  151. auto Context::NoteUndefinedInterface(SemIR::InterfaceId interface_id,
  152. DiagnosticBuilder& builder) -> void {
  153. const auto& interface_info = interfaces().Get(interface_id);
  154. CARBON_CHECK(!interface_info.is_defined()) << "Interface is not incomplete";
  155. if (interface_info.definition_id.is_valid()) {
  156. CARBON_DIAGNOSTIC(InterfaceUndefinedWithinDefinition, Note,
  157. "Interface is currently being defined.");
  158. builder.Note(interface_info.definition_id,
  159. InterfaceUndefinedWithinDefinition);
  160. } else {
  161. CARBON_DIAGNOSTIC(InterfaceForwardDeclaredHere, Note,
  162. "Interface was forward declared here.");
  163. builder.Note(interface_info.decl_id, InterfaceForwardDeclaredHere);
  164. }
  165. }
  166. auto Context::AddPackageImports(Parse::NodeId import_node,
  167. IdentifierId package_id,
  168. llvm::ArrayRef<const SemIR::File*> sem_irs,
  169. bool has_load_error) -> void {
  170. CARBON_CHECK(has_load_error || !sem_irs.empty())
  171. << "There should be either a load error or at least one IR.";
  172. auto name_id = SemIR::NameId::ForIdentifier(package_id);
  173. SemIR::ImportIRId first_id(import_irs().size());
  174. for (const auto* sem_ir : sem_irs) {
  175. import_irs().Add(sem_ir);
  176. }
  177. if (has_load_error) {
  178. import_irs().Add(nullptr);
  179. }
  180. SemIR::ImportIRId last_id(import_irs().size() - 1);
  181. auto type_id = GetBuiltinType(SemIR::BuiltinKind::NamespaceType);
  182. auto inst_id =
  183. AddInst({import_node, SemIR::Import{.type_id = type_id,
  184. .first_import_ir_id = first_id,
  185. .last_import_ir_id = last_id}});
  186. // Add the import to lookup. Should always succeed because imports will be
  187. // uniquely named.
  188. AddNameToLookup(name_id, inst_id);
  189. // Add a name for formatted output. This isn't used in name lookup in order
  190. // to reduce indirection, but it's separate from the Import because it
  191. // otherwise fits in an Inst.
  192. auto bind_name_id = bind_names().Add(
  193. {.name_id = name_id, .enclosing_scope_id = SemIR::NameScopeId::Package});
  194. AddInst({import_node, SemIR::BindName{.type_id = type_id,
  195. .bind_name_id = bind_name_id,
  196. .value_id = inst_id}});
  197. }
  198. auto Context::AddNameToLookup(SemIR::NameId name_id, SemIR::InstId target_id)
  199. -> void {
  200. if (auto existing = scope_stack().LookupOrAddName(name_id, target_id);
  201. existing.is_valid()) {
  202. DiagnoseDuplicateName(target_id, existing);
  203. }
  204. }
  205. auto Context::LookupNameInDecl(Parse::NodeId /*parse_node*/,
  206. SemIR::NameId name_id,
  207. SemIR::NameScopeId scope_id) -> SemIR::InstId {
  208. if (!scope_id.is_valid()) {
  209. // Look for a name in the current scope only. There are two cases where the
  210. // name would be in an outer scope:
  211. //
  212. // - The name is the sole component of the declared name:
  213. //
  214. // class A;
  215. // fn F() {
  216. // class A;
  217. // }
  218. //
  219. // In this case, the inner A is not the same class as the outer A, so
  220. // lookup should not find the outer A.
  221. //
  222. // - The name is a qualifier of some larger declared name:
  223. //
  224. // class A { class B; }
  225. // fn F() {
  226. // class A.B {}
  227. // }
  228. //
  229. // In this case, we're not in the correct scope to define a member of
  230. // class A, so we should reject, and we achieve this by not finding the
  231. // name A from the outer scope.
  232. auto result = scope_stack().LookupInCurrentScope(name_id);
  233. if (result.is_valid()) {
  234. TryResolveImportRefUnused(*this, result);
  235. }
  236. return result;
  237. } else {
  238. // We do not look into `extend`ed scopes here. A qualified name in a
  239. // declaration must specify the exact scope in which the name was originally
  240. // introduced:
  241. //
  242. // base class A { fn F(); }
  243. // class B { extend base: A; }
  244. //
  245. // // Error, no `F` in `B`.
  246. // fn B.F() {}
  247. return LookupNameInExactScope(name_id, name_scopes().Get(scope_id));
  248. }
  249. }
  250. auto Context::LookupUnqualifiedName(Parse::NodeId parse_node,
  251. SemIR::NameId name_id) -> SemIR::InstId {
  252. // TODO: Check for shadowed lookup results.
  253. // Find the results from enclosing lexical scopes. These will be combined with
  254. // results from non-lexical scopes such as namespaces and classes.
  255. auto [lexical_result, non_lexical_scopes] =
  256. scope_stack().LookupInEnclosingScopes(name_id);
  257. // Walk the non-lexical scopes and perform lookups into each of them.
  258. for (auto [index, name_scope_id] : llvm::reverse(non_lexical_scopes)) {
  259. if (auto non_lexical_result =
  260. LookupQualifiedName(parse_node, name_id, name_scope_id,
  261. /*required=*/false);
  262. non_lexical_result.is_valid()) {
  263. return non_lexical_result;
  264. }
  265. }
  266. if (lexical_result.is_valid()) {
  267. TryResolveImportRefUnused(*this, lexical_result);
  268. return lexical_result;
  269. }
  270. // We didn't find anything at all.
  271. DiagnoseNameNotFound(parse_node, name_id);
  272. return SemIR::InstId::BuiltinError;
  273. }
  274. auto Context::LookupNameInExactScope(SemIR::NameId name_id,
  275. const SemIR::NameScope& scope)
  276. -> SemIR::InstId {
  277. if (auto it = scope.names.find(name_id); it != scope.names.end()) {
  278. TryResolveImportRefUnused(*this, it->second);
  279. return it->second;
  280. }
  281. return SemIR::InstId::Invalid;
  282. }
  283. auto Context::LookupQualifiedName(Parse::NodeId parse_node,
  284. SemIR::NameId name_id,
  285. SemIR::NameScopeId scope_id, bool required)
  286. -> SemIR::InstId {
  287. llvm::SmallVector<SemIR::NameScopeId> scope_ids = {scope_id};
  288. auto result_id = SemIR::InstId::Invalid;
  289. bool has_error = false;
  290. // Walk this scope and, if nothing is found here, the scopes it extends.
  291. while (!scope_ids.empty()) {
  292. const auto& scope = name_scopes().Get(scope_ids.pop_back_val());
  293. has_error |= scope.has_error;
  294. auto scope_result_id = LookupNameInExactScope(name_id, scope);
  295. if (!scope_result_id.is_valid()) {
  296. // Nothing found in this scope: also look in its extended scopes.
  297. auto extended = llvm::reverse(scope.extended_scopes);
  298. scope_ids.append(extended.begin(), extended.end());
  299. continue;
  300. }
  301. // If this is our second lookup result, diagnose an ambiguity.
  302. if (result_id.is_valid()) {
  303. // TODO: This is currently not reachable because the only scope that can
  304. // extend is a class scope, and it can only extend a single base class.
  305. // Add test coverage once this is possible.
  306. CARBON_DIAGNOSTIC(
  307. NameAmbiguousDueToExtend, Error,
  308. "Ambiguous use of name `{0}` found in multiple extended scopes.",
  309. SemIR::NameId);
  310. emitter_->Emit(parse_node, NameAmbiguousDueToExtend, name_id);
  311. // TODO: Add notes pointing to the scopes.
  312. return SemIR::InstId::BuiltinError;
  313. }
  314. result_id = scope_result_id;
  315. }
  316. if (required && !result_id.is_valid()) {
  317. if (!has_error) {
  318. DiagnoseNameNotFound(parse_node, name_id);
  319. }
  320. return SemIR::InstId::BuiltinError;
  321. }
  322. return result_id;
  323. }
  324. template <typename BranchNode, typename... Args>
  325. static auto AddDominatedBlockAndBranchImpl(Context& context,
  326. Parse::NodeId parse_node,
  327. Args... args) -> SemIR::InstBlockId {
  328. if (!context.inst_block_stack().is_current_block_reachable()) {
  329. return SemIR::InstBlockId::Unreachable;
  330. }
  331. auto block_id = context.inst_blocks().AddDefaultValue();
  332. context.AddInst({parse_node, BranchNode{block_id, args...}});
  333. return block_id;
  334. }
  335. auto Context::AddDominatedBlockAndBranch(Parse::NodeId parse_node)
  336. -> SemIR::InstBlockId {
  337. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  338. }
  339. auto Context::AddDominatedBlockAndBranchWithArg(Parse::NodeId parse_node,
  340. SemIR::InstId arg_id)
  341. -> SemIR::InstBlockId {
  342. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  343. arg_id);
  344. }
  345. auto Context::AddDominatedBlockAndBranchIf(Parse::NodeId parse_node,
  346. SemIR::InstId cond_id)
  347. -> SemIR::InstBlockId {
  348. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  349. cond_id);
  350. }
  351. auto Context::AddConvergenceBlockAndPush(Parse::NodeId parse_node,
  352. int num_blocks) -> void {
  353. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  354. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  355. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  356. if (inst_block_stack().is_current_block_reachable()) {
  357. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  358. new_block_id = inst_blocks().AddDefaultValue();
  359. }
  360. AddInst({parse_node, SemIR::Branch{new_block_id}});
  361. }
  362. inst_block_stack().Pop();
  363. }
  364. inst_block_stack().Push(new_block_id);
  365. }
  366. auto Context::AddConvergenceBlockWithArgAndPush(
  367. Parse::NodeId parse_node, std::initializer_list<SemIR::InstId> block_args)
  368. -> SemIR::InstId {
  369. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  370. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  371. for (auto arg_id : block_args) {
  372. if (inst_block_stack().is_current_block_reachable()) {
  373. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  374. new_block_id = inst_blocks().AddDefaultValue();
  375. }
  376. AddInst({parse_node, SemIR::BranchWithArg{new_block_id, arg_id}});
  377. }
  378. inst_block_stack().Pop();
  379. }
  380. inst_block_stack().Push(new_block_id);
  381. // Acquire the result value.
  382. SemIR::TypeId result_type_id = insts().Get(*block_args.begin()).type_id();
  383. return AddInst({parse_node, SemIR::BlockArg{result_type_id, new_block_id}});
  384. }
  385. // Add the current code block to the enclosing function.
  386. auto Context::AddCurrentCodeBlockToFunction(Parse::NodeId parse_node) -> void {
  387. CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
  388. if (return_scope_stack().empty()) {
  389. CARBON_CHECK(parse_node.is_valid())
  390. << "No current function, but parse_node not provided";
  391. TODO(parse_node,
  392. "Control flow expressions are currently only supported inside "
  393. "functions.");
  394. return;
  395. }
  396. if (!inst_block_stack().is_current_block_reachable()) {
  397. // Don't include unreachable blocks in the function.
  398. return;
  399. }
  400. auto function_id =
  401. insts()
  402. .GetAs<SemIR::FunctionDecl>(return_scope_stack().back().decl_id)
  403. .function_id;
  404. functions()
  405. .Get(function_id)
  406. .body_block_ids.push_back(inst_block_stack().PeekOrAdd());
  407. }
  408. auto Context::is_current_position_reachable() -> bool {
  409. if (!inst_block_stack().is_current_block_reachable()) {
  410. return false;
  411. }
  412. // Our current position is at the end of a reachable block. That position is
  413. // reachable unless the previous instruction is a terminator instruction.
  414. auto block_contents = inst_block_stack().PeekCurrentBlockContents();
  415. if (block_contents.empty()) {
  416. return true;
  417. }
  418. const auto& last_inst = insts().Get(block_contents.back());
  419. return last_inst.kind().terminator_kind() !=
  420. SemIR::TerminatorKind::Terminator;
  421. }
  422. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  423. auto Context::ParamOrArgComma() -> void {
  424. // Support expressions, parameters, and other nodes like `StructFieldValue`
  425. // that produce InstIds.
  426. ParamOrArgSave(node_stack_.Pop<SemIR::InstId>());
  427. }
  428. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  429. if (!node_stack_.PeekIs(start_kind)) {
  430. // Support expressions, parameters, and other nodes like `StructFieldValue`
  431. // that produce InstIds.
  432. ParamOrArgSave(node_stack_.Pop<SemIR::InstId>());
  433. }
  434. }
  435. auto Context::ParamOrArgPop() -> SemIR::InstBlockId {
  436. return params_or_args_stack_.Pop();
  437. }
  438. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::InstBlockId {
  439. ParamOrArgEndNoPop(start_kind);
  440. return ParamOrArgPop();
  441. }
  442. auto Context::FinalizeGlobalInit() -> void {
  443. inst_block_stack().PushGlobalInit();
  444. if (!inst_block_stack().PeekCurrentBlockContents().empty()) {
  445. AddInst({Parse::NodeId::Invalid, SemIR::Return{}});
  446. // Pop the GlobalInit block here to finalize it.
  447. inst_block_stack().Pop();
  448. // __global_init is only added if there are initialization instructions.
  449. auto name_id = sem_ir().identifiers().Add("__global_init");
  450. sem_ir().functions().Add(
  451. {.name_id = SemIR::NameId::ForIdentifier(name_id),
  452. .enclosing_scope_id = SemIR::NameScopeId::Package,
  453. .implicit_param_refs_id = SemIR::InstBlockId::Empty,
  454. .param_refs_id = SemIR::InstBlockId::Empty,
  455. .return_type_id = SemIR::TypeId::Invalid,
  456. .return_slot_id = SemIR::InstId::Invalid,
  457. .body_block_ids = {SemIR::InstBlockId::GlobalInit}});
  458. } else {
  459. inst_block_stack().PopGlobalInit();
  460. }
  461. }
  462. namespace {
  463. // Worklist-based type completion mechanism.
  464. //
  465. // When attempting to complete a type, we may find other types that also need to
  466. // be completed: types nested within that type, and the value representation of
  467. // the type. In order to complete a type without recursing arbitrarily deeply,
  468. // we use a worklist of tasks:
  469. //
  470. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  471. // nested within a type to the work list.
  472. // - A `BuildValueRepr` step computes the value representation for a
  473. // type, once all of its nested types are complete, and marks the type as
  474. // complete.
  475. class TypeCompleter {
  476. public:
  477. TypeCompleter(
  478. Context& context,
  479. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  480. diagnoser)
  481. : context_(context), diagnoser_(diagnoser) {}
  482. // Attempts to complete the given type. Returns true if it is now complete,
  483. // false if it could not be completed.
  484. auto Complete(SemIR::TypeId type_id) -> bool {
  485. Push(type_id);
  486. while (!work_list_.empty()) {
  487. if (!ProcessStep()) {
  488. return false;
  489. }
  490. }
  491. return true;
  492. }
  493. private:
  494. // Adds `type_id` to the work list, if it's not already complete.
  495. auto Push(SemIR::TypeId type_id) -> void {
  496. if (!context_.types().IsComplete(type_id)) {
  497. work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes});
  498. }
  499. }
  500. // Runs the next step.
  501. auto ProcessStep() -> bool {
  502. auto [type_id, phase] = work_list_.back();
  503. // We might have enqueued the same type more than once. Just skip the
  504. // type if it's already complete.
  505. if (context_.types().IsComplete(type_id)) {
  506. work_list_.pop_back();
  507. return true;
  508. }
  509. auto inst_id = context_.types().GetInstId(type_id);
  510. auto inst = context_.insts().Get(inst_id);
  511. auto old_work_list_size = work_list_.size();
  512. switch (phase) {
  513. case Phase::AddNestedIncompleteTypes:
  514. if (!AddNestedIncompleteTypes(inst)) {
  515. return false;
  516. }
  517. CARBON_CHECK(work_list_.size() >= old_work_list_size)
  518. << "AddNestedIncompleteTypes should not remove work items";
  519. work_list_[old_work_list_size - 1].phase = Phase::BuildValueRepr;
  520. break;
  521. case Phase::BuildValueRepr: {
  522. auto value_rep = BuildValueRepr(type_id, inst);
  523. context_.sem_ir().CompleteType(type_id, value_rep);
  524. CARBON_CHECK(old_work_list_size == work_list_.size())
  525. << "BuildValueRepr should not change work items";
  526. work_list_.pop_back();
  527. // Also complete the value representation type, if necessary. This
  528. // should never fail: the value representation shouldn't require any
  529. // additional nested types to be complete.
  530. if (!context_.types().IsComplete(value_rep.type_id)) {
  531. work_list_.push_back({value_rep.type_id, Phase::BuildValueRepr});
  532. }
  533. // For a pointer representation, the pointee also needs to be complete.
  534. if (value_rep.kind == SemIR::ValueRepr::Pointer) {
  535. if (value_rep.type_id == SemIR::TypeId::Error) {
  536. break;
  537. }
  538. auto pointee_type_id =
  539. context_.sem_ir().GetPointeeType(value_rep.type_id);
  540. if (!context_.types().IsComplete(pointee_type_id)) {
  541. work_list_.push_back({pointee_type_id, Phase::BuildValueRepr});
  542. }
  543. }
  544. break;
  545. }
  546. }
  547. return true;
  548. }
  549. // Adds any types nested within `type_inst` that need to be complete for
  550. // `type_inst` to be complete to our work list.
  551. auto AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool {
  552. switch (type_inst.kind()) {
  553. case SemIR::ArrayType::Kind:
  554. Push(type_inst.As<SemIR::ArrayType>().element_type_id);
  555. break;
  556. case SemIR::StructType::Kind:
  557. for (auto field_id : context_.inst_blocks().Get(
  558. type_inst.As<SemIR::StructType>().fields_id)) {
  559. Push(context_.insts()
  560. .GetAs<SemIR::StructTypeField>(field_id)
  561. .field_type_id);
  562. }
  563. break;
  564. case SemIR::TupleType::Kind:
  565. for (auto element_type_id : context_.type_blocks().Get(
  566. type_inst.As<SemIR::TupleType>().elements_id)) {
  567. Push(element_type_id);
  568. }
  569. break;
  570. case SemIR::ClassType::Kind: {
  571. auto class_type = type_inst.As<SemIR::ClassType>();
  572. auto& class_info = context_.classes().Get(class_type.class_id);
  573. if (!class_info.is_defined()) {
  574. if (diagnoser_) {
  575. auto builder = (*diagnoser_)();
  576. context_.NoteIncompleteClass(class_type.class_id, builder);
  577. builder.Emit();
  578. }
  579. return false;
  580. }
  581. Push(class_info.object_repr_id);
  582. break;
  583. }
  584. case SemIR::ConstType::Kind:
  585. Push(type_inst.As<SemIR::ConstType>().inner_id);
  586. break;
  587. default:
  588. break;
  589. }
  590. return true;
  591. }
  592. // Makes an empty value representation, which is used for types that have no
  593. // state, such as empty structs and tuples.
  594. auto MakeEmptyValueRepr() const -> SemIR::ValueRepr {
  595. return {.kind = SemIR::ValueRepr::None,
  596. .type_id = context_.GetTupleType({})};
  597. }
  598. // Makes a value representation that uses pass-by-copy, copying the given
  599. // type.
  600. auto MakeCopyValueRepr(SemIR::TypeId rep_id,
  601. SemIR::ValueRepr::AggregateKind aggregate_kind =
  602. SemIR::ValueRepr::NotAggregate) const
  603. -> SemIR::ValueRepr {
  604. return {.kind = SemIR::ValueRepr::Copy,
  605. .aggregate_kind = aggregate_kind,
  606. .type_id = rep_id};
  607. }
  608. // Makes a value representation that uses pass-by-address with the given
  609. // pointee type.
  610. auto MakePointerValueRepr(SemIR::TypeId pointee_id,
  611. SemIR::ValueRepr::AggregateKind aggregate_kind =
  612. SemIR::ValueRepr::NotAggregate) const
  613. -> SemIR::ValueRepr {
  614. // TODO: Should we add `const` qualification to `pointee_id`?
  615. return {.kind = SemIR::ValueRepr::Pointer,
  616. .aggregate_kind = aggregate_kind,
  617. .type_id = context_.GetPointerType(pointee_id)};
  618. }
  619. // Gets the value representation of a nested type, which should already be
  620. // complete.
  621. auto GetNestedValueRepr(SemIR::TypeId nested_type_id) const {
  622. CARBON_CHECK(context_.types().IsComplete(nested_type_id))
  623. << "Nested type should already be complete";
  624. auto value_rep = context_.types().GetValueRepr(nested_type_id);
  625. CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown)
  626. << "Complete type should have a value representation";
  627. return value_rep;
  628. };
  629. auto BuildBuiltinValueRepr(SemIR::TypeId type_id,
  630. SemIR::Builtin builtin) const -> SemIR::ValueRepr {
  631. switch (builtin.builtin_kind) {
  632. case SemIR::BuiltinKind::TypeType:
  633. case SemIR::BuiltinKind::Error:
  634. case SemIR::BuiltinKind::Invalid:
  635. case SemIR::BuiltinKind::BoolType:
  636. case SemIR::BuiltinKind::IntType:
  637. case SemIR::BuiltinKind::FloatType:
  638. case SemIR::BuiltinKind::NamespaceType:
  639. case SemIR::BuiltinKind::FunctionType:
  640. case SemIR::BuiltinKind::BoundMethodType:
  641. return MakeCopyValueRepr(type_id);
  642. case SemIR::BuiltinKind::StringType:
  643. // TODO: Decide on string value semantics. This should probably be a
  644. // custom value representation carrying a pointer and size or
  645. // similar.
  646. return MakePointerValueRepr(type_id);
  647. }
  648. llvm_unreachable("All builtin kinds were handled above");
  649. }
  650. auto BuildImportRefUsedValueRepr(SemIR::TypeId type_id,
  651. SemIR::ImportRefUsed import_ref) const
  652. -> SemIR::ValueRepr {
  653. const auto& import_ir = context_.import_irs().Get(import_ref.ir_id);
  654. auto import_inst = import_ir->insts().Get(import_ref.inst_id);
  655. CARBON_CHECK(import_inst.kind() != SemIR::InstKind::ImportRefUsed)
  656. << "If ImportRefUsed can point at another, this would be recursive.";
  657. return BuildValueRepr(type_id, import_inst);
  658. }
  659. auto BuildStructOrTupleValueRepr(std::size_t num_elements,
  660. SemIR::TypeId elementwise_rep,
  661. bool same_as_object_rep) const
  662. -> SemIR::ValueRepr {
  663. SemIR::ValueRepr::AggregateKind aggregate_kind =
  664. same_as_object_rep ? SemIR::ValueRepr::ValueAndObjectAggregate
  665. : SemIR::ValueRepr::ValueAggregate;
  666. if (num_elements == 1) {
  667. // The value representation for a struct or tuple with a single element
  668. // is a struct or tuple containing the value representation of the
  669. // element.
  670. // TODO: Consider doing the same whenever `elementwise_rep` is
  671. // sufficiently small.
  672. return MakeCopyValueRepr(elementwise_rep, aggregate_kind);
  673. }
  674. // For a struct or tuple with multiple fields, we use a pointer
  675. // to the elementwise value representation.
  676. return MakePointerValueRepr(elementwise_rep, aggregate_kind);
  677. }
  678. auto BuildStructTypeValueRepr(SemIR::TypeId type_id,
  679. SemIR::StructType struct_type) const
  680. -> SemIR::ValueRepr {
  681. // TODO: Share more code with tuples.
  682. auto fields = context_.inst_blocks().Get(struct_type.fields_id);
  683. if (fields.empty()) {
  684. return MakeEmptyValueRepr();
  685. }
  686. // Find the value representation for each field, and construct a struct
  687. // of value representations.
  688. llvm::SmallVector<SemIR::InstId> value_rep_fields;
  689. value_rep_fields.reserve(fields.size());
  690. bool same_as_object_rep = true;
  691. for (auto field_id : fields) {
  692. auto field = context_.insts().GetAs<SemIR::StructTypeField>(field_id);
  693. auto field_value_rep = GetNestedValueRepr(field.field_type_id);
  694. if (field_value_rep.type_id != field.field_type_id) {
  695. same_as_object_rep = false;
  696. field.field_type_id = field_value_rep.type_id;
  697. // TODO: Use `TryEvalInst` to form this value.
  698. field_id = context_
  699. .AddConstant(field, context_.constant_values()
  700. .Get(context_.types().GetInstId(
  701. field.field_type_id))
  702. .is_symbolic())
  703. .inst_id();
  704. }
  705. value_rep_fields.push_back(field_id);
  706. }
  707. auto value_rep = same_as_object_rep
  708. ? type_id
  709. : context_.GetStructType(
  710. context_.inst_blocks().Add(value_rep_fields));
  711. return BuildStructOrTupleValueRepr(fields.size(), value_rep,
  712. same_as_object_rep);
  713. }
  714. auto BuildTupleTypeValueRepr(SemIR::TypeId type_id,
  715. SemIR::TupleType tuple_type) const
  716. -> SemIR::ValueRepr {
  717. // TODO: Share more code with structs.
  718. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  719. if (elements.empty()) {
  720. return MakeEmptyValueRepr();
  721. }
  722. // Find the value representation for each element, and construct a tuple
  723. // of value representations.
  724. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  725. value_rep_elements.reserve(elements.size());
  726. bool same_as_object_rep = true;
  727. for (auto element_type_id : elements) {
  728. auto element_value_rep = GetNestedValueRepr(element_type_id);
  729. if (element_value_rep.type_id != element_type_id) {
  730. same_as_object_rep = false;
  731. }
  732. value_rep_elements.push_back(element_value_rep.type_id);
  733. }
  734. auto value_rep = same_as_object_rep
  735. ? type_id
  736. : context_.GetTupleType(value_rep_elements);
  737. return BuildStructOrTupleValueRepr(elements.size(), value_rep,
  738. same_as_object_rep);
  739. }
  740. // Builds and returns the value representation for the given type. All nested
  741. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  742. auto BuildValueRepr(SemIR::TypeId type_id, SemIR::Inst inst) const
  743. -> SemIR::ValueRepr {
  744. // TODO: This can emit new SemIR instructions. Consider emitting them into a
  745. // dedicated file-scope instruction block where possible, or somewhere else
  746. // that better reflects the definition of the type, rather than wherever the
  747. // type happens to first be required to be complete.
  748. switch (inst.kind()) {
  749. case SemIR::AddrOf::Kind:
  750. case SemIR::AddrPattern::Kind:
  751. case SemIR::ArrayIndex::Kind:
  752. case SemIR::ArrayInit::Kind:
  753. case SemIR::Assign::Kind:
  754. case SemIR::BaseDecl::Kind:
  755. case SemIR::BindAlias::Kind:
  756. case SemIR::BindName::Kind:
  757. case SemIR::BindValue::Kind:
  758. case SemIR::BlockArg::Kind:
  759. case SemIR::BoolLiteral::Kind:
  760. case SemIR::BoundMethod::Kind:
  761. case SemIR::Branch::Kind:
  762. case SemIR::BranchIf::Kind:
  763. case SemIR::BranchWithArg::Kind:
  764. case SemIR::Call::Kind:
  765. case SemIR::ClassDecl::Kind:
  766. case SemIR::ClassElementAccess::Kind:
  767. case SemIR::ClassInit::Kind:
  768. case SemIR::Converted::Kind:
  769. case SemIR::Deref::Kind:
  770. case SemIR::FieldDecl::Kind:
  771. case SemIR::FunctionDecl::Kind:
  772. case SemIR::ImplDecl::Kind:
  773. case SemIR::Import::Kind:
  774. case SemIR::InitializeFrom::Kind:
  775. case SemIR::InterfaceDecl::Kind:
  776. case SemIR::IntLiteral::Kind:
  777. case SemIR::ImportRefUnused::Kind:
  778. case SemIR::NameRef::Kind:
  779. case SemIR::Namespace::Kind:
  780. case SemIR::Param::Kind:
  781. case SemIR::RealLiteral::Kind:
  782. case SemIR::Return::Kind:
  783. case SemIR::ReturnExpr::Kind:
  784. case SemIR::SpliceBlock::Kind:
  785. case SemIR::StringLiteral::Kind:
  786. case SemIR::StructAccess::Kind:
  787. case SemIR::StructTypeField::Kind:
  788. case SemIR::StructLiteral::Kind:
  789. case SemIR::StructInit::Kind:
  790. case SemIR::StructValue::Kind:
  791. case SemIR::Temporary::Kind:
  792. case SemIR::TemporaryStorage::Kind:
  793. case SemIR::TupleAccess::Kind:
  794. case SemIR::TupleIndex::Kind:
  795. case SemIR::TupleLiteral::Kind:
  796. case SemIR::TupleInit::Kind:
  797. case SemIR::TupleValue::Kind:
  798. case SemIR::UnaryOperatorNot::Kind:
  799. case SemIR::ValueAsRef::Kind:
  800. case SemIR::ValueOfInitializer::Kind:
  801. case SemIR::VarStorage::Kind:
  802. CARBON_FATAL() << "Type refers to non-type inst " << inst;
  803. case SemIR::ArrayType::Kind: {
  804. // For arrays, it's convenient to always use a pointer representation,
  805. // even when the array has zero or one element, in order to support
  806. // indexing.
  807. return MakePointerValueRepr(type_id, SemIR::ValueRepr::ObjectAggregate);
  808. }
  809. case SemIR::ImportRefUsed::Kind:
  810. return BuildImportRefUsedValueRepr(type_id,
  811. inst.As<SemIR::ImportRefUsed>());
  812. case SemIR::StructType::Kind:
  813. return BuildStructTypeValueRepr(type_id, inst.As<SemIR::StructType>());
  814. case SemIR::TupleType::Kind:
  815. return BuildTupleTypeValueRepr(type_id, inst.As<SemIR::TupleType>());
  816. case SemIR::ClassType::Kind:
  817. // The value representation for a class is a pointer to the object
  818. // representation.
  819. // TODO: Support customized value representations for classes.
  820. // TODO: Pick a better value representation when possible.
  821. return MakePointerValueRepr(
  822. context_.classes()
  823. .Get(inst.As<SemIR::ClassType>().class_id)
  824. .object_repr_id,
  825. SemIR::ValueRepr::ObjectAggregate);
  826. case SemIR::InterfaceType::Kind:
  827. // TODO: Should we model the value representation as a witness?
  828. return MakeEmptyValueRepr();
  829. case SemIR::Builtin::Kind:
  830. return BuildBuiltinValueRepr(type_id, inst.As<SemIR::Builtin>());
  831. case SemIR::BindSymbolicName::Kind:
  832. case SemIR::PointerType::Kind:
  833. case SemIR::UnboundElementType::Kind:
  834. return MakeCopyValueRepr(type_id);
  835. case SemIR::ConstType::Kind:
  836. // The value representation of `const T` is the same as that of `T`.
  837. // Objects are not modifiable through their value representations.
  838. return GetNestedValueRepr(inst.As<SemIR::ConstType>().inner_id);
  839. }
  840. }
  841. enum class Phase : int8_t {
  842. // The next step is to add nested types to the list of types to complete.
  843. AddNestedIncompleteTypes,
  844. // The next step is to build the value representation for the type.
  845. BuildValueRepr,
  846. };
  847. struct WorkItem {
  848. SemIR::TypeId type_id;
  849. Phase phase;
  850. };
  851. Context& context_;
  852. llvm::SmallVector<WorkItem> work_list_;
  853. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  854. diagnoser_;
  855. };
  856. } // namespace
  857. auto Context::TryToCompleteType(
  858. SemIR::TypeId type_id,
  859. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser)
  860. -> bool {
  861. return TypeCompleter(*this, diagnoser).Complete(type_id);
  862. }
  863. auto Context::GetTypeIdForTypeConstant(SemIR::ConstantId constant_id)
  864. -> SemIR::TypeId {
  865. CARBON_CHECK(constant_id.is_constant())
  866. << "Canonicalizing non-constant type: " << constant_id;
  867. auto [it, added] = type_ids_for_type_constants_.insert(
  868. {constant_id, SemIR::TypeId::Invalid});
  869. if (added) {
  870. it->second = types().Add({.constant_id = constant_id});
  871. }
  872. return it->second;
  873. }
  874. template <typename InstT, typename... EachArgT>
  875. static auto GetTypeImpl(Context& context, EachArgT... each_arg)
  876. -> SemIR::TypeId {
  877. // TODO: Remove inst_id parameter from TryEvalInst.
  878. return context.GetTypeIdForTypeConstant(
  879. TryEvalInst(context, SemIR::InstId::Invalid,
  880. InstT{SemIR::TypeId::TypeType, each_arg...}));
  881. }
  882. auto Context::GetStructType(SemIR::InstBlockId refs_id) -> SemIR::TypeId {
  883. return GetTypeImpl<SemIR::StructType>(*this, refs_id);
  884. }
  885. auto Context::GetTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids)
  886. -> SemIR::TypeId {
  887. // TODO: Deduplicate the type block here. Currently requesting the same tuple
  888. // type more than once will create multiple type blocks, all but one of which
  889. // is unused.
  890. return GetTypeImpl<SemIR::TupleType>(*this, type_blocks().Add(type_ids));
  891. }
  892. auto Context::GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId {
  893. CARBON_CHECK(kind != SemIR::BuiltinKind::Invalid);
  894. auto type_id = GetTypeIdForTypeConstant(
  895. constant_values().Get(SemIR::InstId::ForBuiltin(kind)));
  896. // To keep client code simpler, complete builtin types before returning them.
  897. bool complete = TryToCompleteType(type_id);
  898. CARBON_CHECK(complete) << "Failed to complete builtin type";
  899. return type_id;
  900. }
  901. auto Context::GetClassType(SemIR::ClassId class_id) -> SemIR::TypeId {
  902. return GetTypeImpl<SemIR::ClassType>(*this, class_id);
  903. }
  904. auto Context::GetPointerType(SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  905. return GetTypeImpl<SemIR::PointerType>(*this, pointee_type_id);
  906. }
  907. auto Context::GetUnboundElementType(SemIR::TypeId class_type_id,
  908. SemIR::TypeId element_type_id)
  909. -> SemIR::TypeId {
  910. return GetTypeImpl<SemIR::UnboundElementType>(*this, class_type_id,
  911. element_type_id);
  912. }
  913. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  914. if (auto const_type = types().TryGetAs<SemIR::ConstType>(type_id)) {
  915. return const_type->inner_id;
  916. }
  917. return type_id;
  918. }
  919. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  920. node_stack_.PrintForStackDump(output);
  921. inst_block_stack_.PrintForStackDump(output);
  922. params_or_args_stack_.PrintForStackDump(output);
  923. args_type_info_stack_.PrintForStackDump(output);
  924. }
  925. } // namespace Carbon::Check