type_completion.cpp 24 KB

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