context.cpp 36 KB

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