type_structure.cpp 15 KB

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