type_structure.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. explicit TypeStructureBuilder(Context& context) : context_(context) {}
  105. auto Run(SemIR::InstId self_inst_id,
  106. SemIR::SpecificInterface interface_constraint) -> TypeStructure {
  107. CARBON_CHECK(work_list_.empty());
  108. first_symbolic_distance_ = TypeStructure::InfiniteDistance;
  109. structure_.clear();
  110. // The self type comes first in the type structure, so we push it last, as
  111. // the queue works from the back.
  112. Push(interface_constraint);
  113. PushInstId(self_inst_id);
  114. BuildTypeStructure();
  115. return TypeStructure(std::exchange(structure_, {}),
  116. first_symbolic_distance_);
  117. }
  118. private:
  119. auto BuildTypeStructure() -> void {
  120. while (!work_list_.empty()) {
  121. auto next = work_list_.back();
  122. work_list_.pop_back();
  123. if (std::holds_alternative<CloseType>(next)) {
  124. AppendStructural(TypeStructure::Structural::ConcreteCloseParen);
  125. continue;
  126. }
  127. if (const auto* interface =
  128. std::get_if<SemIR::SpecificInterface>(&next)) {
  129. auto args = GetSpecificArgs(interface->specific_id);
  130. if (args.empty()) {
  131. AppendStructural(TypeStructure::Structural::Concrete);
  132. } else {
  133. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  134. Push(CloseType());
  135. PushArgs(args);
  136. }
  137. continue;
  138. }
  139. if (std::holds_alternative<SymbolicType>(next)) {
  140. AppendStructural(TypeStructure::Structural::Symbolic);
  141. continue;
  142. }
  143. if (std::holds_alternative<NonTypeValue>(next)) {
  144. // TODO: Include the value's type into the structure, with the type
  145. // coming first and paired together with the value, like:
  146. // `{TypeWithPossibleNestedTypes, Concrete}`.
  147. // We might want a different bracket marker than ConcreteOpenParen for
  148. // this so that it can look different in the type structure when dumped.
  149. AppendStructural(TypeStructure::Structural::Concrete);
  150. continue;
  151. }
  152. SemIR::TypeId next_type_id = std::get<SemIR::TypeId>(next);
  153. auto inst_id = context_.types().GetInstId(next_type_id);
  154. auto inst = context_.insts().Get(inst_id);
  155. CARBON_KIND_SWITCH(inst) {
  156. // ==== Symbolic types ====
  157. case SemIR::BindSymbolicName::Kind:
  158. case SemIR::SymbolicBindingPattern::Kind:
  159. case SemIR::FacetAccessType::Kind: {
  160. Push(SymbolicType());
  161. break;
  162. }
  163. // ==== Concrete types ====
  164. case SemIR::AssociatedEntityType::Kind:
  165. case SemIR::BoolType::Kind:
  166. case SemIR::FloatType::Kind:
  167. case SemIR::GenericClassType::Kind:
  168. case SemIR::GenericInterfaceType::Kind:
  169. case SemIR::ImplWitnessAccess::Kind:
  170. case SemIR::IntLiteralType::Kind:
  171. case SemIR::LegacyFloatType::Kind:
  172. case SemIR::StringType::Kind:
  173. case SemIR::TypeType::Kind: {
  174. AppendStructural(TypeStructure::Structural::Concrete);
  175. break;
  176. }
  177. case CARBON_KIND(SemIR::FacetType facet_type): {
  178. (void)facet_type;
  179. // A `FacetType` instruction shows up in the self type of impl lookup
  180. // queries like `C(D)` where `C` requires its parameter to satisfy
  181. // some `FacetType` `Z`. The `D` argument is converted to a
  182. // `FacetValue` satisfying `Z`, and the type of `C` in the self type
  183. // has a specific with the type of that `FacetValue`, which is the
  184. // `FacetType` satisfying `Z` we see here.
  185. //
  186. // The `FacetValue` may still be symbolic in generic code but its
  187. // type, the `FacetType` here, is concrete.
  188. AppendStructural(TypeStructure::Structural::Concrete);
  189. break;
  190. }
  191. case CARBON_KIND(SemIR::IntType int_type): {
  192. if (context_.constant_values().Get(inst_id).is_concrete()) {
  193. AppendStructural(TypeStructure::Structural::Concrete);
  194. } else {
  195. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  196. Push(CloseType());
  197. PushArgs({int_type.bit_width_id});
  198. }
  199. break;
  200. }
  201. // ==== Aggregate types ====
  202. case CARBON_KIND(SemIR::ArrayType array_type): {
  203. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  204. Push(CloseType());
  205. Push(array_type.element_type_id);
  206. PushInstId(array_type.bound_id);
  207. break;
  208. }
  209. case CARBON_KIND(SemIR::ClassType class_type): {
  210. auto args = GetSpecificArgs(class_type.specific_id);
  211. if (args.empty()) {
  212. AppendStructural(TypeStructure::Structural::Concrete);
  213. } else {
  214. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  215. Push(CloseType());
  216. PushArgs(args);
  217. }
  218. break;
  219. }
  220. case CARBON_KIND(SemIR::ConstType const_type): {
  221. // We don't put the `const` into the type structure since it is a
  222. // modifier; just move to the inner type.
  223. Push(const_type.inner_id);
  224. break;
  225. }
  226. case CARBON_KIND(SemIR::PointerType pointer_type): {
  227. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  228. Push(CloseType());
  229. Push(pointer_type.pointee_id);
  230. break;
  231. }
  232. case CARBON_KIND(SemIR::TupleType tuple_type): {
  233. auto inner_types = context_.type_blocks().Get(tuple_type.elements_id);
  234. if (inner_types.empty()) {
  235. AppendStructural(TypeStructure::Structural::Concrete);
  236. } else {
  237. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  238. Push(CloseType());
  239. for (auto type :
  240. context_.type_blocks().Get(tuple_type.elements_id)) {
  241. Push(type);
  242. }
  243. }
  244. break;
  245. }
  246. case CARBON_KIND(SemIR::StructType struct_type): {
  247. auto fields =
  248. context_.struct_type_fields().Get(struct_type.fields_id);
  249. if (fields.empty()) {
  250. AppendStructural(TypeStructure::Structural::Concrete);
  251. } else {
  252. AppendStructural(TypeStructure::Structural::ConcreteOpenParen);
  253. Push(CloseType());
  254. for (const auto& field : fields) {
  255. Push(field.type_id);
  256. }
  257. }
  258. break;
  259. }
  260. case CARBON_KIND(SemIR::TypeOfInst type_of): {
  261. (void)type_of;
  262. auto const_id = context_.constant_values().Get(inst_id);
  263. // TODO: TypeOfInst should not be encountered in impl lookup with a
  264. // template-dependent value, since impl lookup on such values should
  265. // be deferred to type-checking the specific. So this should become a
  266. // CARBON_FATAL code path. For now it is possible, though, to reach
  267. // here and it results in a diagnostic.
  268. //
  269. // However, TypeOfInst can be determined to be a concrete value for a
  270. // template value with a non-template dependent type. This currently
  271. // does not happen though, the TypeOfInst is always template
  272. // dependent. If it does in the future then we will want to use the
  273. // concrete constant value instruction of `inst_id` in the type
  274. // structure:
  275. // if (const_id.is_concrete()) {
  276. // PushInstId(context_.constant_values().GetInstId(const_id));
  277. // }
  278. //
  279. // If TypeOfInst was used for more general metaprogramming then we may
  280. // need to handle both concrete and perhaps symbolic (non-template)
  281. // values of TypeOfInst.
  282. CARBON_CHECK(const_id.is_symbolic());
  283. auto sym = context_.constant_values().GetSymbolicConstant(const_id);
  284. CARBON_CHECK(sym.dependence == SemIR::ConstantDependence::Template,
  285. "TypeOfInst with non-template symbolic value?");
  286. context_.TODO(context_.insts().GetLocId(inst_id),
  287. "Impl lookup on template-dependent type value");
  288. break;
  289. }
  290. default:
  291. CARBON_FATAL("Unhandled type instruction {0}", inst_id);
  292. }
  293. }
  294. }
  295. // A work item to mark the closing paren for an aggregate concrete type.
  296. struct CloseType {};
  297. // A work item to mark a symbolic type.
  298. struct SymbolicType {};
  299. // A work item to mark a non-type value.
  300. struct NonTypeValue {};
  301. using WorkItem = std::variant<SemIR::TypeId, SymbolicType, NonTypeValue,
  302. SemIR::SpecificInterface, CloseType>;
  303. // Get the TypeId for an instruction that is not a facet value, otherwise
  304. // return SymbolicType to indicate the instruction is a symbolic facet value.
  305. //
  306. // If the instruction is not a type value, the return is TypeId::None.
  307. //
  308. // We reuse the `SymbolicType` work item here to give a nice return type.
  309. auto TryGetInstIdAsTypeId(SemIR::InstId inst_id) const
  310. -> std::variant<SemIR::TypeId, SymbolicType> {
  311. if (auto facet_value =
  312. context_.insts().TryGetAs<SemIR::FacetValue>(inst_id)) {
  313. inst_id = facet_value->type_inst_id;
  314. }
  315. auto type_id_of_inst_id = context_.insts().Get(inst_id).type_id();
  316. // All instructions of type FacetType are symbolic except for FacetValue:
  317. // - In non-generic code, values of type FacetType are only created through
  318. // conversion to a FacetType (e.g. `Class as Iface`), which produces a
  319. // non-symbolic FacetValue.
  320. // - In generic code, binding values of type FacetType are symbolic as they
  321. // refer to an unknown type. Non-binding values would be FacetValues like
  322. // in non-generic code, but would be symbolic as well.
  323. // - In specifics of generic code, when deducing a value for a symbolic
  324. // binding of type FacetType, we always produce a FacetValue (which may or
  325. // may not itself be symbolic) through conversion.
  326. //
  327. // FacetValues are handled earlier by getting the type instruction from
  328. // them. That type instruction is never of type FacetType. If it refers to a
  329. // FacetType it does so through a FacetAccessType, which is of type TypeType
  330. // and thus does not match here.
  331. if (context_.types().Is<SemIR::FacetType>(type_id_of_inst_id)) {
  332. return SymbolicType();
  333. }
  334. // Non-type values are concrete, only types are symbolic.
  335. if (type_id_of_inst_id != SemIR::TypeType::SingletonTypeId) {
  336. return SemIR::TypeId::None;
  337. }
  338. return context_.types().GetTypeIdForTypeInstId(inst_id);
  339. }
  340. // Get the instructions in the specific's instruction block as an ArrayRef.
  341. auto GetSpecificArgs(SemIR::SpecificId specific_id)
  342. -> llvm::ArrayRef<SemIR::InstId> {
  343. if (specific_id == SemIR::SpecificId::None) {
  344. return {};
  345. }
  346. auto specific = context_.specifics().Get(specific_id);
  347. return context_.inst_blocks().Get(specific.args_id);
  348. }
  349. // Push all arguments from the array into the work queue.
  350. auto PushArgs(llvm::ArrayRef<SemIR::InstId> args) -> void {
  351. for (auto arg_id : llvm::reverse(args)) {
  352. PushInstId(arg_id);
  353. }
  354. }
  355. // Push an instruction's type value into the work queue, or a marker if the
  356. // instruction has a symbolic value.
  357. auto PushInstId(SemIR::InstId inst_id) -> void {
  358. auto maybe_type_id = TryGetInstIdAsTypeId(inst_id);
  359. if (std::holds_alternative<SymbolicType>(maybe_type_id)) {
  360. Push(SymbolicType());
  361. } else if (auto type_id = std::get<SemIR::TypeId>(maybe_type_id);
  362. type_id.has_value()) {
  363. Push(type_id);
  364. } else {
  365. Push(NonTypeValue());
  366. }
  367. }
  368. // Push the next step into the work queue.
  369. auto Push(WorkItem item) -> void { work_list_.push_back(item); }
  370. // Append a structural element to the TypeStructure being built.
  371. auto AppendStructural(TypeStructure::Structural structural) -> void {
  372. if (structural == TypeStructure::Structural::Symbolic) {
  373. // Sets the `distance` in `first_symbolic_distance_` if it does not
  374. // already have a non-infinite value.
  375. if (first_symbolic_distance_ == TypeStructure::InfiniteDistance) {
  376. first_symbolic_distance_ = structure_.size();
  377. }
  378. }
  379. structure_.push_back(structural);
  380. }
  381. Context& context_;
  382. llvm::SmallVector<WorkItem> work_list_;
  383. int first_symbolic_distance_;
  384. llvm::SmallVector<TypeStructure::Structural> structure_;
  385. };
  386. auto BuildTypeStructure(Context& context, SemIR::InstId self_inst_id,
  387. SemIR::SpecificInterface interface) -> TypeStructure {
  388. TypeStructureBuilder builder(context);
  389. return builder.Run(self_inst_id, interface);
  390. }
  391. } // namespace Carbon::Check