type_completion.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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/type_completion.h"
  5. #include "llvm/ADT/SmallVector.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/generic.h"
  8. namespace Carbon::Check {
  9. namespace {
  10. // Worklist-based type completion mechanism.
  11. //
  12. // When attempting to complete a type, we may find other types that also need to
  13. // be completed: types nested within that type, and the value representation of
  14. // the type. In order to complete a type without recursing arbitrarily deeply,
  15. // we use a worklist of tasks:
  16. //
  17. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  18. // nested within a type to the work list.
  19. // - A `BuildValueRepr` step computes the value representation for a
  20. // type, once all of its nested types are complete, and marks the type as
  21. // complete.
  22. class TypeCompleter {
  23. public:
  24. TypeCompleter(Context& context, SemIRLoc loc,
  25. Context::BuildDiagnosticFn diagnoser)
  26. : context_(context), loc_(loc), diagnoser_(diagnoser) {}
  27. // Attempts to complete the given type. Returns true if it is now complete,
  28. // false if it could not be completed.
  29. auto Complete(SemIR::TypeId type_id) -> bool;
  30. private:
  31. enum class Phase : int8_t {
  32. // The next step is to add nested types to the list of types to complete.
  33. AddNestedIncompleteTypes,
  34. // The next step is to build the value representation for the type.
  35. BuildValueRepr,
  36. };
  37. struct WorkItem {
  38. SemIR::TypeId type_id;
  39. Phase phase;
  40. };
  41. // Adds `type_id` to the work list, if it's not already complete.
  42. auto Push(SemIR::TypeId type_id) -> void;
  43. // Runs the next step.
  44. auto ProcessStep() -> bool;
  45. // Adds any types nested within `type_inst` that need to be complete for
  46. // `type_inst` to be complete to our work list.
  47. auto AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool;
  48. // Makes an empty value representation, which is used for types that have no
  49. // state, such as empty structs and tuples.
  50. auto MakeEmptyValueRepr() const -> SemIR::ValueRepr;
  51. // Makes a value representation that uses pass-by-copy, copying the given
  52. // type.
  53. auto MakeCopyValueRepr(SemIR::TypeId rep_id,
  54. SemIR::ValueRepr::AggregateKind aggregate_kind =
  55. SemIR::ValueRepr::NotAggregate) const
  56. -> SemIR::ValueRepr;
  57. // Makes a value representation that uses pass-by-address with the given
  58. // pointee type.
  59. auto MakePointerValueRepr(SemIR::TypeId pointee_id,
  60. SemIR::ValueRepr::AggregateKind aggregate_kind =
  61. SemIR::ValueRepr::NotAggregate) const
  62. -> SemIR::ValueRepr;
  63. // Gets the value representation of a nested type, which should already be
  64. // complete.
  65. auto GetNestedValueRepr(SemIR::TypeId nested_type_id) const
  66. -> SemIR::ValueRepr;
  67. template <typename InstT>
  68. requires(
  69. InstT::Kind.template IsAnyOf<
  70. SemIR::AutoType, SemIR::BoolType, SemIR::BoundMethodType,
  71. SemIR::ErrorInst, SemIR::FloatType, SemIR::IntType,
  72. SemIR::IntLiteralType, SemIR::LegacyFloatType, SemIR::NamespaceType,
  73. SemIR::PointerType, SemIR::SpecificFunctionType, SemIR::TypeType,
  74. SemIR::VtableType, SemIR::WitnessType>())
  75. auto BuildValueReprForInst(SemIR::TypeId type_id, InstT /*inst*/) const
  76. -> SemIR::ValueRepr {
  77. return MakeCopyValueRepr(type_id);
  78. }
  79. auto BuildValueReprForInst(SemIR::TypeId type_id,
  80. SemIR::StringType /*inst*/) const
  81. -> SemIR::ValueRepr;
  82. auto BuildStructOrTupleValueRepr(size_t num_elements,
  83. SemIR::TypeId elementwise_rep,
  84. bool same_as_object_rep) const
  85. -> SemIR::ValueRepr;
  86. auto BuildValueReprForInst(SemIR::TypeId type_id,
  87. SemIR::StructType struct_type) const
  88. -> SemIR::ValueRepr;
  89. auto BuildValueReprForInst(SemIR::TypeId type_id,
  90. SemIR::TupleType tuple_type) const
  91. -> SemIR::ValueRepr;
  92. auto BuildValueReprForInst(SemIR::TypeId type_id,
  93. SemIR::ArrayType /*inst*/) const
  94. -> SemIR::ValueRepr;
  95. auto BuildValueReprForInst(SemIR::TypeId /*type_id*/,
  96. SemIR::ClassType inst) const -> SemIR::ValueRepr;
  97. template <typename InstT>
  98. requires(InstT::Kind.template IsAnyOf<
  99. SemIR::AssociatedEntityType, SemIR::FacetType, SemIR::FunctionType,
  100. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  101. SemIR::GenericInterfaceType, SemIR::UnboundElementType,
  102. SemIR::WhereExpr>())
  103. auto BuildValueReprForInst(SemIR::TypeId /*type_id*/, InstT /*inst*/) const
  104. -> SemIR::ValueRepr {
  105. // These types have no runtime operations, so we use an empty value
  106. // representation.
  107. //
  108. // TODO: There is information we could model here:
  109. // - For an interface, we could use a witness.
  110. // - For an associated entity, we could use an index into the witness.
  111. // - For an unbound element, we could use an index or offset.
  112. return MakeEmptyValueRepr();
  113. }
  114. auto BuildValueReprForInst(SemIR::TypeId /*type_id*/,
  115. SemIR::ConstType inst) const -> SemIR::ValueRepr;
  116. template <typename InstT>
  117. requires(InstT::Kind.constant_kind() ==
  118. SemIR::InstConstantKind::SymbolicOnly ||
  119. InstT::Kind.is_type() == SemIR::InstIsType::Never)
  120. auto BuildValueReprForInst(SemIR::TypeId type_id, InstT inst) const
  121. -> SemIR::ValueRepr {
  122. if constexpr (InstT::Kind.is_type() == SemIR::InstIsType::Never) {
  123. CARBON_FATAL("Type refers to non-type inst {0}", inst);
  124. } else {
  125. // For symbolic types, we arbitrarily pick a copy representation.
  126. return MakeCopyValueRepr(type_id);
  127. }
  128. }
  129. // Builds and returns the value representation for the given type. All nested
  130. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  131. auto BuildValueRepr(SemIR::TypeId type_id, SemIR::Inst inst) const
  132. -> SemIR::ValueRepr;
  133. Context& context_;
  134. llvm::SmallVector<WorkItem> work_list_;
  135. SemIRLoc loc_;
  136. Context::BuildDiagnosticFn diagnoser_;
  137. };
  138. } // namespace
  139. auto TypeCompleter::Complete(SemIR::TypeId type_id) -> bool {
  140. Push(type_id);
  141. while (!work_list_.empty()) {
  142. if (!ProcessStep()) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. auto TypeCompleter::Push(SemIR::TypeId type_id) -> void {
  149. if (!context_.types().IsComplete(type_id)) {
  150. work_list_.push_back(
  151. {.type_id = type_id, .phase = Phase::AddNestedIncompleteTypes});
  152. }
  153. }
  154. auto TypeCompleter::ProcessStep() -> bool {
  155. auto [type_id, phase] = work_list_.back();
  156. // We might have enqueued the same type more than once. Just skip the
  157. // type if it's already complete.
  158. if (context_.types().IsComplete(type_id)) {
  159. work_list_.pop_back();
  160. return true;
  161. }
  162. auto inst_id = context_.types().GetInstId(type_id);
  163. auto inst = context_.insts().Get(inst_id);
  164. auto old_work_list_size = work_list_.size();
  165. switch (phase) {
  166. case Phase::AddNestedIncompleteTypes:
  167. if (!AddNestedIncompleteTypes(inst)) {
  168. return false;
  169. }
  170. CARBON_CHECK(work_list_.size() >= old_work_list_size,
  171. "AddNestedIncompleteTypes should not remove work items");
  172. work_list_[old_work_list_size - 1].phase = Phase::BuildValueRepr;
  173. break;
  174. case Phase::BuildValueRepr: {
  175. auto value_rep = BuildValueRepr(type_id, inst);
  176. context_.types().SetValueRepr(type_id, value_rep);
  177. CARBON_CHECK(old_work_list_size == work_list_.size(),
  178. "BuildValueRepr should not change work items");
  179. work_list_.pop_back();
  180. // Also complete the value representation type, if necessary. This
  181. // should never fail: the value representation shouldn't require any
  182. // additional nested types to be complete.
  183. if (!context_.types().IsComplete(value_rep.type_id)) {
  184. work_list_.push_back(
  185. {.type_id = value_rep.type_id, .phase = Phase::BuildValueRepr});
  186. }
  187. // For a pointer representation, the pointee also needs to be complete.
  188. if (value_rep.kind == SemIR::ValueRepr::Pointer) {
  189. if (value_rep.type_id == SemIR::ErrorInst::SingletonTypeId) {
  190. break;
  191. }
  192. auto pointee_type_id =
  193. context_.sem_ir().GetPointeeType(value_rep.type_id);
  194. if (!context_.types().IsComplete(pointee_type_id)) {
  195. work_list_.push_back(
  196. {.type_id = pointee_type_id, .phase = Phase::BuildValueRepr});
  197. }
  198. }
  199. break;
  200. }
  201. }
  202. return true;
  203. }
  204. auto TypeCompleter::AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool {
  205. CARBON_KIND_SWITCH(type_inst) {
  206. case CARBON_KIND(SemIR::ArrayType inst): {
  207. Push(inst.element_type_id);
  208. break;
  209. }
  210. case CARBON_KIND(SemIR::StructType inst): {
  211. for (auto field : context_.struct_type_fields().Get(inst.fields_id)) {
  212. Push(field.type_id);
  213. }
  214. break;
  215. }
  216. case CARBON_KIND(SemIR::TupleType inst): {
  217. for (auto element_type_id :
  218. context_.type_blocks().Get(inst.elements_id)) {
  219. Push(element_type_id);
  220. }
  221. break;
  222. }
  223. case CARBON_KIND(SemIR::ClassType inst): {
  224. auto& class_info = context_.classes().Get(inst.class_id);
  225. if (!class_info.is_defined()) {
  226. if (diagnoser_) {
  227. auto builder = diagnoser_();
  228. NoteIncompleteClass(context_, inst.class_id, builder);
  229. builder.Emit();
  230. }
  231. return false;
  232. }
  233. if (inst.specific_id.has_value()) {
  234. ResolveSpecificDefinition(context_, loc_, inst.specific_id);
  235. }
  236. if (auto adapted_type_id =
  237. class_info.GetAdaptedType(context_.sem_ir(), inst.specific_id);
  238. adapted_type_id.has_value()) {
  239. Push(adapted_type_id);
  240. } else {
  241. Push(class_info.GetObjectRepr(context_.sem_ir(), inst.specific_id));
  242. }
  243. break;
  244. }
  245. case CARBON_KIND(SemIR::ConstType inst): {
  246. Push(inst.inner_id);
  247. break;
  248. }
  249. default:
  250. break;
  251. }
  252. return true;
  253. }
  254. auto TypeCompleter::MakeEmptyValueRepr() const -> SemIR::ValueRepr {
  255. return {.kind = SemIR::ValueRepr::None, .type_id = context_.GetTupleType({})};
  256. }
  257. auto TypeCompleter::MakeCopyValueRepr(
  258. SemIR::TypeId rep_id, SemIR::ValueRepr::AggregateKind aggregate_kind) const
  259. -> SemIR::ValueRepr {
  260. return {.kind = SemIR::ValueRepr::Copy,
  261. .aggregate_kind = aggregate_kind,
  262. .type_id = rep_id};
  263. }
  264. auto TypeCompleter::MakePointerValueRepr(
  265. SemIR::TypeId pointee_id,
  266. SemIR::ValueRepr::AggregateKind aggregate_kind) const -> SemIR::ValueRepr {
  267. // TODO: Should we add `const` qualification to `pointee_id`?
  268. return {.kind = SemIR::ValueRepr::Pointer,
  269. .aggregate_kind = aggregate_kind,
  270. .type_id = context_.GetPointerType(pointee_id)};
  271. }
  272. auto TypeCompleter::GetNestedValueRepr(SemIR::TypeId nested_type_id) const
  273. -> SemIR::ValueRepr {
  274. CARBON_CHECK(context_.types().IsComplete(nested_type_id),
  275. "Nested type should already be complete");
  276. auto value_rep = context_.types().GetValueRepr(nested_type_id);
  277. CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown,
  278. "Complete type should have a value representation");
  279. return value_rep;
  280. }
  281. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId type_id,
  282. SemIR::StringType /*inst*/) const
  283. -> SemIR::ValueRepr {
  284. // TODO: Decide on string value semantics. This should probably be a
  285. // custom value representation carrying a pointer and size or
  286. // similar.
  287. return MakePointerValueRepr(type_id);
  288. }
  289. auto TypeCompleter::BuildStructOrTupleValueRepr(size_t num_elements,
  290. SemIR::TypeId elementwise_rep,
  291. bool same_as_object_rep) const
  292. -> SemIR::ValueRepr {
  293. SemIR::ValueRepr::AggregateKind aggregate_kind =
  294. same_as_object_rep ? SemIR::ValueRepr::ValueAndObjectAggregate
  295. : SemIR::ValueRepr::ValueAggregate;
  296. if (num_elements == 1) {
  297. // The value representation for a struct or tuple with a single element
  298. // is a struct or tuple containing the value representation of the
  299. // element.
  300. // TODO: Consider doing the same whenever `elementwise_rep` is
  301. // sufficiently small.
  302. return MakeCopyValueRepr(elementwise_rep, aggregate_kind);
  303. }
  304. // For a struct or tuple with multiple fields, we use a pointer
  305. // to the elementwise value representation.
  306. return MakePointerValueRepr(elementwise_rep, aggregate_kind);
  307. }
  308. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId type_id,
  309. SemIR::StructType struct_type) const
  310. -> SemIR::ValueRepr {
  311. auto fields = context_.struct_type_fields().Get(struct_type.fields_id);
  312. if (fields.empty()) {
  313. return MakeEmptyValueRepr();
  314. }
  315. // Find the value representation for each field, and construct a struct
  316. // of value representations.
  317. llvm::SmallVector<SemIR::StructTypeField> value_rep_fields;
  318. value_rep_fields.reserve(fields.size());
  319. bool same_as_object_rep = true;
  320. for (auto field : fields) {
  321. auto field_value_rep = GetNestedValueRepr(field.type_id);
  322. if (!field_value_rep.IsCopyOfObjectRepr(context_.sem_ir(), field.type_id)) {
  323. same_as_object_rep = false;
  324. field.type_id = field_value_rep.type_id;
  325. }
  326. value_rep_fields.push_back(field);
  327. }
  328. auto value_rep =
  329. same_as_object_rep
  330. ? type_id
  331. : context_.GetStructType(
  332. context_.struct_type_fields().AddCanonical(value_rep_fields));
  333. return BuildStructOrTupleValueRepr(fields.size(), value_rep,
  334. same_as_object_rep);
  335. }
  336. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId type_id,
  337. SemIR::TupleType tuple_type) const
  338. -> SemIR::ValueRepr {
  339. // TODO: Share more code with structs.
  340. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  341. if (elements.empty()) {
  342. return MakeEmptyValueRepr();
  343. }
  344. // Find the value representation for each element, and construct a tuple
  345. // of value representations.
  346. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  347. value_rep_elements.reserve(elements.size());
  348. bool same_as_object_rep = true;
  349. for (auto element_type_id : elements) {
  350. auto element_value_rep = GetNestedValueRepr(element_type_id);
  351. if (!element_value_rep.IsCopyOfObjectRepr(context_.sem_ir(),
  352. element_type_id)) {
  353. same_as_object_rep = false;
  354. }
  355. value_rep_elements.push_back(element_value_rep.type_id);
  356. }
  357. auto value_rep =
  358. same_as_object_rep ? type_id : context_.GetTupleType(value_rep_elements);
  359. return BuildStructOrTupleValueRepr(elements.size(), value_rep,
  360. same_as_object_rep);
  361. }
  362. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId type_id,
  363. SemIR::ArrayType /*inst*/) const
  364. -> SemIR::ValueRepr {
  365. // For arrays, it's convenient to always use a pointer representation,
  366. // even when the array has zero or one element, in order to support
  367. // indexing.
  368. return MakePointerValueRepr(type_id, SemIR::ValueRepr::ObjectAggregate);
  369. }
  370. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId /*type_id*/,
  371. SemIR::ClassType inst) const
  372. -> SemIR::ValueRepr {
  373. auto& class_info = context_.classes().Get(inst.class_id);
  374. // The value representation of an adapter is the value representation of
  375. // its adapted type.
  376. if (auto adapted_type_id =
  377. class_info.GetAdaptedType(context_.sem_ir(), inst.specific_id);
  378. adapted_type_id.has_value()) {
  379. return GetNestedValueRepr(adapted_type_id);
  380. }
  381. // Otherwise, the value representation for a class is a pointer to the
  382. // object representation.
  383. // TODO: Support customized value representations for classes.
  384. // TODO: Pick a better value representation when possible.
  385. return MakePointerValueRepr(
  386. class_info.GetObjectRepr(context_.sem_ir(), inst.specific_id),
  387. SemIR::ValueRepr::ObjectAggregate);
  388. }
  389. auto TypeCompleter::BuildValueReprForInst(SemIR::TypeId /*type_id*/,
  390. SemIR::ConstType inst) const
  391. -> SemIR::ValueRepr {
  392. // The value representation of `const T` is the same as that of `T`.
  393. // Objects are not modifiable through their value representations.
  394. return GetNestedValueRepr(inst.inner_id);
  395. }
  396. // Builds and returns the value representation for the given type. All nested
  397. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  398. auto TypeCompleter::BuildValueRepr(SemIR::TypeId type_id,
  399. SemIR::Inst inst) const -> SemIR::ValueRepr {
  400. // Use overload resolution to select the implementation, producing compile
  401. // errors when BuildValueReprForInst isn't defined for a given instruction.
  402. CARBON_KIND_SWITCH(inst) {
  403. #define CARBON_SEM_IR_INST_KIND(Name) \
  404. case CARBON_KIND(SemIR::Name typed_inst): { \
  405. return BuildValueReprForInst(type_id, typed_inst); \
  406. }
  407. #include "toolchain/sem_ir/inst_kind.def"
  408. }
  409. }
  410. auto TryToCompleteType(Context& context, SemIR::TypeId type_id, SemIRLoc loc,
  411. Context::BuildDiagnosticFn diagnoser) -> bool {
  412. return TypeCompleter(context, loc, diagnoser).Complete(type_id);
  413. }
  414. auto CompleteTypeOrCheckFail(Context& context, SemIR::TypeId type_id) -> void {
  415. bool complete =
  416. TypeCompleter(context, SemIR::LocId::None, nullptr).Complete(type_id);
  417. CARBON_CHECK(complete, "Expected {0} to be a complete type",
  418. context.types().GetAsInst(type_id));
  419. }
  420. auto RequireCompleteType(Context& context, SemIR::TypeId type_id,
  421. SemIR::LocId loc_id,
  422. Context::BuildDiagnosticFn diagnoser) -> bool {
  423. CARBON_CHECK(diagnoser);
  424. if (!TypeCompleter(context, loc_id, diagnoser).Complete(type_id)) {
  425. return false;
  426. }
  427. // For a symbolic type, create an instruction to require the corresponding
  428. // specific type to be complete.
  429. if (type_id.AsConstantId().is_symbolic()) {
  430. // TODO: Deduplicate these.
  431. context.AddInstInNoBlock(SemIR::LocIdAndInst(
  432. loc_id,
  433. SemIR::RequireCompleteType{.type_id = context.GetSingletonType(
  434. SemIR::WitnessType::SingletonInstId),
  435. .complete_type_id = type_id}));
  436. }
  437. return true;
  438. }
  439. // Adds a note to a diagnostic explaining that a class is abstract.
  440. static auto NoteAbstractClass(Context& context, SemIR::ClassId class_id,
  441. Context::DiagnosticBuilder& builder) -> void {
  442. const auto& class_info = context.classes().Get(class_id);
  443. CARBON_CHECK(
  444. class_info.inheritance_kind == SemIR::Class::InheritanceKind::Abstract,
  445. "Class is not abstract");
  446. CARBON_DIAGNOSTIC(ClassAbstractHere, Note,
  447. "class was declared abstract here");
  448. builder.Note(class_info.definition_id, ClassAbstractHere);
  449. }
  450. auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
  451. SemIR::LocId loc_id,
  452. Context::BuildDiagnosticFn diagnoser,
  453. Context::BuildDiagnosticFn abstract_diagnoser)
  454. -> bool {
  455. CARBON_CHECK(abstract_diagnoser);
  456. if (!RequireCompleteType(context, type_id, loc_id, diagnoser)) {
  457. return false;
  458. }
  459. if (auto class_type = context.types().TryGetAs<SemIR::ClassType>(type_id)) {
  460. auto& class_info = context.classes().Get(class_type->class_id);
  461. if (class_info.inheritance_kind !=
  462. SemIR::Class::InheritanceKind::Abstract) {
  463. return true;
  464. }
  465. auto builder = abstract_diagnoser();
  466. if (!builder) {
  467. return false;
  468. }
  469. NoteAbstractClass(context, class_type->class_id, builder);
  470. builder.Emit();
  471. return false;
  472. }
  473. return true;
  474. }
  475. auto RequireDefinedType(Context& context, SemIR::TypeId type_id,
  476. SemIR::LocId loc_id,
  477. Context::BuildDiagnosticFn diagnoser) -> bool {
  478. if (!RequireCompleteType(context, type_id, loc_id, diagnoser)) {
  479. return false;
  480. }
  481. if (auto facet_type = context.types().TryGetAs<SemIR::FacetType>(type_id)) {
  482. const auto& facet_type_info =
  483. context.facet_types().Get(facet_type->facet_type_id);
  484. for (auto interface : facet_type_info.impls_constraints) {
  485. auto interface_id = interface.interface_id;
  486. if (!context.interfaces().Get(interface_id).is_defined()) {
  487. auto builder = diagnoser();
  488. NoteUndefinedInterface(context, interface_id, builder);
  489. builder.Emit();
  490. return false;
  491. }
  492. if (interface.specific_id.has_value()) {
  493. ResolveSpecificDefinition(context, loc_id, interface.specific_id);
  494. }
  495. }
  496. // TODO: Finish facet type resolution.
  497. //
  498. // Note that we will need Self to be passed into facet type resolution.
  499. // The `.Self` of a facet type created by `where` will then be bound to the
  500. // provided self type.
  501. //
  502. // For example, in `T:! X where ...`, we will bind the `.Self` of the
  503. // `where` facet type to `T`, and in `(X where ...) where ...`, we will bind
  504. // the inner `.Self` to the outer `.Self`.
  505. //
  506. // If the facet type contains a rewrite, we may have deferred converting the
  507. // rewritten value to the type of the associated constant. That conversion
  508. // should also be performed as part of resolution, and may depend on the
  509. // Self type.
  510. }
  511. return true;
  512. }
  513. auto AsCompleteType(Context& context, SemIR::TypeId type_id,
  514. SemIR::LocId loc_id, Context::BuildDiagnosticFn diagnoser)
  515. -> SemIR::TypeId {
  516. return RequireCompleteType(context, type_id, loc_id, diagnoser)
  517. ? type_id
  518. : SemIR::ErrorInst::SingletonTypeId;
  519. }
  520. // Returns the type `type_id` if it is a concrete type, or produces an
  521. // incomplete or abstract type error and returns an error type. This is a
  522. // convenience wrapper around `RequireConcreteType`.
  523. auto AsConcreteType(Context& context, SemIR::TypeId type_id,
  524. SemIR::LocId loc_id, Context::BuildDiagnosticFn diagnoser,
  525. Context::BuildDiagnosticFn abstract_diagnoser)
  526. -> SemIR::TypeId {
  527. return RequireConcreteType(context, type_id, loc_id, diagnoser,
  528. abstract_diagnoser)
  529. ? type_id
  530. : SemIR::ErrorInst::SingletonTypeId;
  531. }
  532. auto NoteIncompleteClass(Context& context, SemIR::ClassId class_id,
  533. Context::DiagnosticBuilder& builder) -> void {
  534. const auto& class_info = context.classes().Get(class_id);
  535. CARBON_CHECK(!class_info.is_defined(), "Class is not incomplete");
  536. if (class_info.has_definition_started()) {
  537. CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
  538. "class is incomplete within its definition");
  539. builder.Note(class_info.definition_id, ClassIncompleteWithinDefinition);
  540. } else {
  541. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  542. "class was forward declared here");
  543. builder.Note(class_info.latest_decl_id(), ClassForwardDeclaredHere);
  544. }
  545. }
  546. auto NoteUndefinedInterface(Context& context, SemIR::InterfaceId interface_id,
  547. Context::DiagnosticBuilder& builder) -> void {
  548. const auto& interface_info = context.interfaces().Get(interface_id);
  549. CARBON_CHECK(!interface_info.is_defined(), "Interface is not incomplete");
  550. if (interface_info.is_being_defined()) {
  551. CARBON_DIAGNOSTIC(InterfaceUndefinedWithinDefinition, Note,
  552. "interface is currently being defined");
  553. builder.Note(interface_info.definition_id,
  554. InterfaceUndefinedWithinDefinition);
  555. } else {
  556. CARBON_DIAGNOSTIC(InterfaceForwardDeclaredHere, Note,
  557. "interface was forward declared here");
  558. builder.Note(interface_info.latest_decl_id(), InterfaceForwardDeclaredHere);
  559. }
  560. }
  561. } // namespace Carbon::Check