type_structure.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_structure.h"
  5. #include <utility>
  6. #include <variant>
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/facet_type_info.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/impl.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. auto TypeStructure::IsCompatibleWith(const TypeStructure& other) const -> bool {
  16. const auto& lhs = structure_;
  17. const auto& rhs = other.structure_;
  18. const auto* lhs_cursor = lhs.begin();
  19. const auto* rhs_cursor = rhs.begin();
  20. while (true) {
  21. // If both structures end at the same time, they match.
  22. if (lhs_cursor == lhs.end() && rhs_cursor == rhs.end()) {
  23. return true;
  24. }
  25. // If one structure ends sooner than the other, they don't match.
  26. if (lhs_cursor == lhs.end() || rhs_cursor == rhs.end()) {
  27. return false;
  28. }
  29. // Same structural element on both sides, they match and both are consumed.
  30. //
  31. // TODO: If we kept the constant value of the concrete element in the type
  32. // structure, then we could compare them and use that to eliminate matching
  33. // impls that are not actually compatible.
  34. if (*lhs_cursor == *rhs_cursor) {
  35. ++lhs_cursor;
  36. ++rhs_cursor;
  37. continue;
  38. }
  39. // If the element on each side is concrete but they not the same structural
  40. // shape, then the structures don't match.
  41. if (*lhs_cursor != Structural::Symbolic &&
  42. *rhs_cursor != Structural::Symbolic) {
  43. return false;
  44. }
  45. // From here we know one side is a Symbolic and the other is not. We can
  46. // match the Symbolic against either a single Concrete or a larger bracketed
  47. // set of Concrete structural elements.
  48. // Returns false if the lhs and rhs can not match, true if we should
  49. // continue checking for compatibility.
  50. auto consume_symbolic = [](const auto*& lhs_cursor,
  51. const auto*& rhs_cursor) -> bool {
  52. // Consume the symbolic on the RHS.
  53. ++rhs_cursor;
  54. // The symbolic on the RHS is in the same position as a close paren on the
  55. // LHS, which means the structures can not match.
  56. //
  57. // Example:
  58. // - ((c))
  59. // - ((c?))
  60. if (*lhs_cursor == Structural::ConcreteCloseParen) {
  61. return false;
  62. }
  63. // There's either a Concrete element or an open paren on the LHS. If it's
  64. // the former, the Symbolic just matches with it. If it's the latter, the
  65. // Symbolic matches with everything on the LHS up to the matching closing
  66. // paren.
  67. CARBON_CHECK(*lhs_cursor == Structural::Concrete ||
  68. *lhs_cursor == Structural::ConcreteOpenParen);
  69. int depth = 0;
  70. do {
  71. switch (*lhs_cursor) {
  72. case Structural::ConcreteOpenParen:
  73. depth += 1;
  74. break;
  75. case Structural::ConcreteCloseParen:
  76. depth -= 1;
  77. break;
  78. case Structural::Concrete:
  79. break;
  80. case Structural::Symbolic:
  81. break;
  82. }
  83. ++lhs_cursor;
  84. } while (depth > 0);
  85. return true;
  86. };
  87. // We move the symbolic to the RHS to make only one case to handle in the
  88. // lambda.
  89. if (*lhs_cursor == Structural::Symbolic) {
  90. if (!consume_symbolic(rhs_cursor, lhs_cursor)) {
  91. return false;
  92. }
  93. } else {
  94. if (!consume_symbolic(lhs_cursor, rhs_cursor)) {
  95. return false;
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. // A class that builds a `TypeStructure` for an `Impl`, or an impl lookup query,
  102. // that represents its self type and interface.
  103. class TypeStructureBuilder {
  104. public:
  105. // `context` must not be null.
  106. explicit TypeStructureBuilder(Context* context) : context_(context) {}
  107. auto Run(SemIR::InstId self_inst_id,
  108. SemIR::SpecificInterface interface_constraint) -> TypeStructure {
  109. CARBON_CHECK(work_list_.empty());
  110. structure_.clear();
  111. symbolic_type_indices_.clear();
  112. concrete_types_.clear();
  113. // The self type comes first in the type structure, so we push it last, as
  114. // the queue works from the back.
  115. Push(interface_constraint);
  116. if (self_inst_id.has_value()) {
  117. PushInstId(self_inst_id);
  118. }
  119. BuildTypeStructure();
  120. // TODO: This requires 4 SmallVector moves (two here and two in the
  121. // constructor). Find a way to reduce that.
  122. return TypeStructure(std::exchange(structure_, {}),
  123. std::exchange(symbolic_type_indices_, {}),
  124. std::exchange(concrete_types_, {}));
  125. }
  126. private:
  127. auto BuildTypeStructure() -> void {
  128. while (!work_list_.empty()) {
  129. auto next = work_list_.back();
  130. work_list_.pop_back();
  131. if (std::holds_alternative<CloseType>(next)) {
  132. AppendStructuralConcreteCloseParen();
  133. continue;
  134. }
  135. if (const auto* interface =
  136. std::get_if<SemIR::SpecificInterface>(&next)) {
  137. auto args = GetSpecificArgs(interface->specific_id);
  138. if (args.empty()) {
  139. AppendStructuralConcrete(interface->interface_id);
  140. } else {
  141. AppendStructuralConcreteOpenParen(interface->interface_id);
  142. Push(CloseType());
  143. PushArgs(args);
  144. }
  145. continue;
  146. }
  147. if (std::holds_alternative<SymbolicType>(next)) {
  148. AppendStructuralSymbolic();
  149. continue;
  150. }
  151. if (std::holds_alternative<NonTypeValue>(next)) {
  152. // TODO: Include the value's type into the structure, with the type
  153. // coming first and paired together with the value, like:
  154. // `{TypeWithPossibleNestedTypes, Concrete}`.
  155. // We might want a different bracket marker than ConcreteOpenParen for
  156. // this so that it can look different in the type structure when dumped.
  157. AppendStructuralConcrete(SemIR::ErrorInst::TypeId);
  158. continue;
  159. }
  160. SemIR::TypeId type_id = std::get<SemIR::TypeId>(next);
  161. auto inst_id = context_->types().GetInstId(type_id);
  162. auto inst = context_->insts().Get(inst_id);
  163. CARBON_KIND_SWITCH(inst) {
  164. // ==== Symbolic types ====
  165. case SemIR::BindSymbolicName::Kind:
  166. case SemIR::SymbolicBindingPattern::Kind:
  167. case SemIR::FacetAccessType::Kind: {
  168. Push(SymbolicType());
  169. break;
  170. }
  171. case SemIR::TypeOfInst::Kind: {
  172. // TODO: For a template value with a fixed type, such as `template n:!
  173. // i32`, we could look at the type of the value to see if it's
  174. // template-dependent (which it's not here) and add that type to the
  175. // type structure?
  176. // https://github.com/carbon-language/carbon-lang/pull/5124#discussion_r2006617038
  177. Push(SymbolicType());
  178. break;
  179. }
  180. // ==== Concrete types ====
  181. case SemIR::AssociatedEntityType::Kind:
  182. case SemIR::BoolType::Kind:
  183. case SemIR::FloatType::Kind:
  184. case SemIR::GenericClassType::Kind:
  185. case SemIR::GenericInterfaceType::Kind:
  186. case SemIR::ImplWitnessAccess::Kind:
  187. case SemIR::IntLiteralType::Kind:
  188. case SemIR::LegacyFloatType::Kind:
  189. case SemIR::NamespaceType::Kind:
  190. case SemIR::StringType::Kind:
  191. case SemIR::TypeType::Kind:
  192. case SemIR::WitnessType::Kind:
  193. AppendStructuralConcrete(type_id);
  194. break;
  195. case CARBON_KIND(SemIR::FacetType facet_type): {
  196. (void)facet_type;
  197. // A `FacetType` instruction shows up in the self type of impl lookup
  198. // queries like `C(D)` where `C` requires its parameter to satisfy
  199. // some `FacetType` `Z`. The `D` argument is converted to a
  200. // `FacetValue` satisfying `Z`, and the type of `C` in the self type
  201. // has a specific with the type of that `FacetValue`, which is the
  202. // `FacetType` satisfying `Z` we see here.
  203. //
  204. // The `FacetValue` may still be symbolic in generic code but its
  205. // type, the `FacetType` here, is concrete.
  206. AppendStructuralConcrete(type_id);
  207. break;
  208. }
  209. case CARBON_KIND(SemIR::IntType int_type): {
  210. if (type_id.is_concrete()) {
  211. AppendStructuralConcrete(type_id);
  212. } else {
  213. AppendStructuralConcreteOpenParen(type_id);
  214. Push(CloseType());
  215. PushArgs({int_type.bit_width_id});
  216. }
  217. break;
  218. }
  219. // ==== Aggregate types ====
  220. case CARBON_KIND(SemIR::ArrayType array_type): {
  221. AppendStructuralConcreteOpenParen(TypeStructure::ConcreteNoneType());
  222. Push(CloseType());
  223. PushInstId(array_type.element_type_inst_id);
  224. PushInstId(array_type.bound_id);
  225. break;
  226. }
  227. case CARBON_KIND(SemIR::ClassType class_type): {
  228. auto args = GetSpecificArgs(class_type.specific_id);
  229. if (args.empty()) {
  230. AppendStructuralConcrete(class_type.class_id);
  231. } else {
  232. AppendStructuralConcreteOpenParen(type_id);
  233. Push(CloseType());
  234. PushArgs(args);
  235. }
  236. break;
  237. }
  238. case CARBON_KIND(SemIR::ConstType const_type): {
  239. // We don't put the `const` into the type structure since it is a
  240. // modifier; just move to the inner type.
  241. PushInstId(const_type.inner_id);
  242. break;
  243. }
  244. case CARBON_KIND(SemIR::ImplWitnessAssociatedConstant assoc): {
  245. Push(assoc.type_id);
  246. break;
  247. }
  248. case CARBON_KIND(SemIR::PointerType pointer_type): {
  249. AppendStructuralConcreteOpenParen(TypeStructure::ConcreteNoneType());
  250. Push(CloseType());
  251. PushInstId(pointer_type.pointee_id);
  252. break;
  253. }
  254. case CARBON_KIND(SemIR::TupleType tuple_type): {
  255. auto inner_types =
  256. context_->inst_blocks().Get(tuple_type.type_elements_id);
  257. if (inner_types.empty()) {
  258. AppendStructuralConcrete(type_id);
  259. } else {
  260. AppendStructuralConcreteOpenParen(
  261. TypeStructure::ConcreteNoneType());
  262. Push(CloseType());
  263. PushArgs(context_->inst_blocks().Get(tuple_type.type_elements_id));
  264. }
  265. break;
  266. }
  267. case CARBON_KIND(SemIR::StructType struct_type): {
  268. auto fields =
  269. context_->struct_type_fields().Get(struct_type.fields_id);
  270. if (fields.empty()) {
  271. AppendStructuralConcrete(type_id);
  272. } else {
  273. AppendStructuralConcreteOpenParen(type_id);
  274. Push(CloseType());
  275. for (const auto& field : llvm::reverse(fields)) {
  276. PushInstId(field.type_inst_id);
  277. }
  278. }
  279. break;
  280. }
  281. default:
  282. CARBON_FATAL("Unhandled type instruction {0}", inst_id);
  283. }
  284. }
  285. }
  286. // A work item to mark the closing paren for an aggregate concrete type.
  287. struct CloseType {};
  288. // A work item to mark a symbolic type.
  289. struct SymbolicType {};
  290. // A work item to mark a non-type value.
  291. struct NonTypeValue {
  292. // The type of the value.
  293. SemIR::TypeId type_id;
  294. };
  295. using WorkItem = std::variant<SemIR::TypeId, SymbolicType, NonTypeValue,
  296. SemIR::SpecificInterface, CloseType>;
  297. // Get the TypeId for an instruction that is not a facet value, otherwise
  298. // return SymbolicType to indicate the instruction is a symbolic facet value.
  299. //
  300. // If the instruction is not a type value, the return is TypeId::None.
  301. //
  302. // We reuse the `SymbolicType` work item here to give a nice return type.
  303. auto TryGetInstIdAsTypeId(SemIR::InstId inst_id) const
  304. -> std::variant<SemIR::TypeId, SymbolicType> {
  305. if (auto facet_value =
  306. context_->insts().TryGetAs<SemIR::FacetValue>(inst_id)) {
  307. inst_id = facet_value->type_inst_id;
  308. }
  309. auto type_id_of_inst_id = context_->insts().Get(inst_id).type_id();
  310. // All instructions of type FacetType are symbolic except for FacetValue:
  311. // - In non-generic code, values of type FacetType are only created through
  312. // conversion to a FacetType (e.g. `Class as Iface`), which produces a
  313. // non-symbolic FacetValue.
  314. // - In generic code, binding values of type FacetType are symbolic as they
  315. // refer to an unknown type. Non-binding values would be FacetValues like
  316. // in non-generic code, but would be symbolic as well.
  317. // - In specifics of generic code, when deducing a value for a symbolic
  318. // binding of type FacetType, we always produce a FacetValue (which may or
  319. // may not itself be symbolic) through conversion.
  320. //
  321. // FacetValues are handled earlier by getting the type instruction from
  322. // them. That type instruction is never of type FacetType. If it refers to a
  323. // FacetType it does so through a FacetAccessType, which is of type TypeType
  324. // and thus does not match here.
  325. if (context_->types().Is<SemIR::FacetType>(type_id_of_inst_id)) {
  326. return SymbolicType();
  327. }
  328. // Non-type values are concrete, only types are symbolic.
  329. if (type_id_of_inst_id != SemIR::TypeType::TypeId) {
  330. return SemIR::TypeId::None;
  331. }
  332. return context_->types().GetTypeIdForTypeInstId(inst_id);
  333. }
  334. // Get the instructions in the specific's instruction block as an ArrayRef.
  335. auto GetSpecificArgs(SemIR::SpecificId specific_id)
  336. -> llvm::ArrayRef<SemIR::InstId> {
  337. if (specific_id == SemIR::SpecificId::None) {
  338. return {};
  339. }
  340. auto specific = context_->specifics().Get(specific_id);
  341. return context_->inst_blocks().Get(specific.args_id);
  342. }
  343. // Push all arguments from the array into the work queue.
  344. auto PushArgs(llvm::ArrayRef<SemIR::InstId> args) -> void {
  345. for (auto arg_id : llvm::reverse(args)) {
  346. PushInstId(arg_id);
  347. }
  348. }
  349. // Push an instruction's type value into the work queue, or a marker if the
  350. // instruction has a symbolic value.
  351. auto PushInstId(SemIR::InstId inst_id) -> void {
  352. auto maybe_type_id = TryGetInstIdAsTypeId(inst_id);
  353. if (std::holds_alternative<SymbolicType>(maybe_type_id)) {
  354. Push(SymbolicType());
  355. } else if (auto type_id = std::get<SemIR::TypeId>(maybe_type_id);
  356. type_id.has_value()) {
  357. Push(type_id);
  358. } else {
  359. Push(NonTypeValue{.type_id = context_->insts().Get(inst_id).type_id()});
  360. }
  361. }
  362. // Push the next step into the work queue.
  363. auto Push(WorkItem item) -> void { work_list_.push_back(item); }
  364. // Append a structural element to the TypeStructure being built.
  365. auto AppendStructuralConcrete(TypeStructure::ConcreteType type) -> void {
  366. CARBON_CHECK(
  367. !std::holds_alternative<TypeStructure::ConcreteNoneType>(type));
  368. concrete_types_.push_back(type);
  369. structure_.push_back(TypeStructure::Structural::Concrete);
  370. }
  371. auto AppendStructuralConcreteOpenParen(TypeStructure::ConcreteType type)
  372. -> void {
  373. concrete_types_.push_back(type);
  374. structure_.push_back(TypeStructure::Structural::ConcreteOpenParen);
  375. }
  376. auto AppendStructuralConcreteCloseParen() -> void {
  377. structure_.push_back(TypeStructure::Structural::ConcreteCloseParen);
  378. }
  379. auto AppendStructuralSymbolic() -> void {
  380. symbolic_type_indices_.push_back(structure_.size());
  381. structure_.push_back(TypeStructure::Structural::Symbolic);
  382. }
  383. Context* context_;
  384. llvm::SmallVector<WorkItem> work_list_;
  385. // In-progress state for the equivalent `TypeStructure` fields.
  386. llvm::SmallVector<TypeStructure::Structural> structure_;
  387. llvm::SmallVector<int> symbolic_type_indices_;
  388. llvm::SmallVector<TypeStructure::ConcreteType> concrete_types_;
  389. };
  390. auto BuildTypeStructure(Context& context, SemIR::InstId self_inst_id,
  391. SemIR::SpecificInterface interface) -> TypeStructure {
  392. TypeStructureBuilder builder(&context);
  393. return builder.Run(self_inst_id, interface);
  394. }
  395. } // namespace Carbon::Check