type_structure.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <concepts>
  6. #include <utility>
  7. #include <variant>
  8. #include "toolchain/base/kind_switch.h"
  9. #include "toolchain/check/context.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/type_iterator.h"
  12. namespace Carbon::Check {
  13. auto TypeStructure::CompareStructure(CompareTest test,
  14. 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. const auto* lhs_concrete_cursor = concrete_types_.begin();
  20. const auto* rhs_concrete_cursor = other.concrete_types_.begin();
  21. while (true) {
  22. // If both structures end at the same time, they match.
  23. if (lhs_cursor == lhs.end() && rhs_cursor == rhs.end()) {
  24. return true;
  25. }
  26. // If one structure ends sooner than the other, they don't match.
  27. if (lhs_cursor == lhs.end() || rhs_cursor == rhs.end()) {
  28. return false;
  29. }
  30. // Same structural element on both sides. Compare concrete values if
  31. // possible to ensure they match. Both will be consumed.
  32. if (*lhs_cursor == *rhs_cursor) {
  33. // Each Concrete and ConcreteOpenParen shape entry has a paired concrete
  34. // value.
  35. if (*lhs_cursor == Structural::Concrete ||
  36. *lhs_cursor == Structural::ConcreteOpenParen) {
  37. if (*lhs_concrete_cursor != *rhs_concrete_cursor) {
  38. return false;
  39. }
  40. // Move past the shape and concrete value together.
  41. ++lhs_concrete_cursor;
  42. ++rhs_concrete_cursor;
  43. }
  44. ++lhs_cursor;
  45. ++rhs_cursor;
  46. continue;
  47. }
  48. // If the element on each side is concrete but they not the same structural
  49. // shape, then the structures don't match.
  50. if (*lhs_cursor != Structural::Symbolic &&
  51. *rhs_cursor != Structural::Symbolic) {
  52. return false;
  53. }
  54. // From here we know one side is a Symbolic and the other is not. We can
  55. // match the Symbolic against either a single Concrete or a larger bracketed
  56. // set of Concrete structural elements.
  57. switch (test) {
  58. case CompareTest::IsEqualToOrMoreSpecificThan:
  59. // If the symbolic is on the LHS, then the RHS structure is more
  60. // specific and we return false. If the symbolic is on the RHS, we
  61. // consume it and match it against the structure on the LHS.
  62. if (*lhs_cursor == Structural::Symbolic) {
  63. return false;
  64. }
  65. if (!ConsumeRhsSymbolic(lhs_cursor, lhs_concrete_cursor, rhs_cursor)) {
  66. return false;
  67. }
  68. break;
  69. case CompareTest::HasOverlap:
  70. // The symbolic can be on either side, and whichever side it is on, we
  71. // consume it and match it against the structure on the other side.
  72. if (*lhs_cursor == Structural::Symbolic) {
  73. if (!ConsumeRhsSymbolic(rhs_cursor, rhs_concrete_cursor,
  74. lhs_cursor)) {
  75. return false;
  76. }
  77. } else {
  78. if (!ConsumeRhsSymbolic(lhs_cursor, lhs_concrete_cursor,
  79. rhs_cursor)) {
  80. return false;
  81. }
  82. }
  83. break;
  84. }
  85. }
  86. return true;
  87. }
  88. // Returns false if the lhs and rhs can not match, true if we should
  89. // continue checking for compatibility.
  90. auto TypeStructure::ConsumeRhsSymbolic(
  91. llvm::SmallVector<Structural>::const_iterator& lhs_cursor,
  92. llvm::SmallVector<ConcreteType>::const_iterator& lhs_concrete_cursor,
  93. llvm::SmallVector<Structural>::const_iterator& rhs_cursor) -> bool {
  94. // Consume the symbolic on the RHS.
  95. ++rhs_cursor;
  96. // The symbolic on the RHS is in the same position as a close paren on the
  97. // LHS, which means the structures can not match.
  98. //
  99. // Example:
  100. // - ((c))
  101. // - ((c?))
  102. if (*lhs_cursor == TypeStructure::Structural::ConcreteCloseParen) {
  103. return false;
  104. }
  105. // There's either a Concrete element or an open paren on the LHS. If it's
  106. // the former, the Symbolic just matches with it. If it's the latter, the
  107. // Symbolic matches with everything on the LHS up to the matching closing
  108. // paren.
  109. CARBON_CHECK(*lhs_cursor == Structural::Concrete ||
  110. *lhs_cursor == Structural::ConcreteOpenParen);
  111. int depth = 0;
  112. do {
  113. switch (*lhs_cursor) {
  114. case Structural::ConcreteOpenParen:
  115. depth += 1;
  116. // Each Concrete and ConcreteOpenParen shape entry has a paired
  117. // concrete value. Skip the shape and concrete value together.
  118. ++lhs_concrete_cursor;
  119. break;
  120. case Structural::ConcreteCloseParen:
  121. depth -= 1;
  122. break;
  123. case Structural::Concrete:
  124. // Each Concrete and ConcreteOpenParen shape entry has a paired
  125. // concrete value. Skip the shape and concrete value together.
  126. ++lhs_concrete_cursor;
  127. break;
  128. case Structural::Symbolic:
  129. break;
  130. }
  131. ++lhs_cursor;
  132. } while (depth > 0);
  133. return true;
  134. }
  135. // A class that builds a `TypeStructure` for an `Impl`, or an impl lookup query,
  136. // that represents its self type and interface.
  137. class TypeStructureBuilder {
  138. public:
  139. // `context` must not be null.
  140. explicit TypeStructureBuilder(Context* context) : context_(context) {}
  141. auto Run(SemIR::InstId self_inst_id,
  142. SemIR::SpecificInterface interface_constraint)
  143. -> std::optional<TypeStructure> {
  144. structure_.clear();
  145. symbolic_type_indices_.clear();
  146. concrete_types_.clear();
  147. SemIR::TypeIterator type_iter(&context_->sem_ir());
  148. // The self type comes first in the type structure, so we push it last, as
  149. // the iterator starts with the last thing added.
  150. type_iter.Add(interface_constraint);
  151. if (self_inst_id.has_value()) {
  152. type_iter.Add(self_inst_id);
  153. }
  154. return Build(std::move(type_iter));
  155. }
  156. private:
  157. auto Build(SemIR::TypeIterator type_iter) -> std::optional<TypeStructure>;
  158. // Append a structural element to the TypeStructure being built.
  159. auto AppendStructuralConcrete(TypeStructure::ConcreteType type) -> void {
  160. concrete_types_.push_back(type);
  161. structure_.push_back(TypeStructure::Structural::Concrete);
  162. }
  163. auto AppendStructuralConcreteOpenParen(TypeStructure::ConcreteType type)
  164. -> void {
  165. concrete_types_.push_back(type);
  166. structure_.push_back(TypeStructure::Structural::ConcreteOpenParen);
  167. }
  168. auto AppendStructuralConcreteCloseParen() -> void {
  169. structure_.push_back(TypeStructure::Structural::ConcreteCloseParen);
  170. }
  171. auto AppendStructuralSymbolic() -> void {
  172. symbolic_type_indices_.push_back(structure_.size());
  173. structure_.push_back(TypeStructure::Structural::Symbolic);
  174. }
  175. Context* context_;
  176. // In-progress state for the equivalent `TypeStructure` fields.
  177. llvm::SmallVector<TypeStructure::Structural> structure_;
  178. llvm::SmallVector<int> symbolic_type_indices_;
  179. llvm::SmallVector<TypeStructure::ConcreteType> concrete_types_;
  180. };
  181. // Builds the type structure and returns it.
  182. auto TypeStructureBuilder::Build(SemIR::TypeIterator type_iter)
  183. -> std::optional<TypeStructure> {
  184. while (true) {
  185. using Step = SemIR::TypeIterator::Step;
  186. CARBON_KIND_SWITCH(type_iter.Next().any) {
  187. case CARBON_KIND(Step::Done _): {
  188. // TODO: This requires 4 SmallVector moves (two here and two in the
  189. // constructor). Find a way to reduce that.
  190. return TypeStructure(std::exchange(structure_, {}),
  191. std::exchange(symbolic_type_indices_, {}),
  192. std::exchange(concrete_types_, {}));
  193. }
  194. case CARBON_KIND(Step::End _): {
  195. AppendStructuralConcreteCloseParen();
  196. break;
  197. }
  198. case CARBON_KIND(Step::Error _): {
  199. return std::nullopt;
  200. }
  201. case CARBON_KIND(Step::ConcreteType concrete): {
  202. AppendStructuralConcrete(concrete.type_id);
  203. break;
  204. }
  205. case CARBON_KIND(Step::SymbolicType _): {
  206. AppendStructuralSymbolic();
  207. break;
  208. }
  209. case CARBON_KIND(Step::SymbolicBinding _): {
  210. AppendStructuralSymbolic();
  211. break;
  212. }
  213. case CARBON_KIND(Step::TemplateType _): {
  214. AppendStructuralSymbolic();
  215. break;
  216. }
  217. case CARBON_KIND(Step::ConcreteValue value): {
  218. AppendStructuralConcrete(
  219. context_->constant_values().Get(value.inst_id));
  220. break;
  221. }
  222. case CARBON_KIND(Step::SymbolicValue _): {
  223. AppendStructuralSymbolic();
  224. break;
  225. }
  226. case CARBON_KIND(Step::StructFieldName field_name): {
  227. AppendStructuralConcrete(field_name.name_id);
  228. break;
  229. }
  230. case CARBON_KIND(Step::ClassStartOnly class_start): {
  231. AppendStructuralConcrete(class_start.class_id);
  232. break;
  233. }
  234. case CARBON_KIND(Step::ClassStart class_start): {
  235. AppendStructuralConcreteOpenParen(class_start.class_id);
  236. break;
  237. }
  238. case CARBON_KIND(Step::StructStartOnly _): {
  239. AppendStructuralConcrete(TypeStructure::ConcreteTypeStart::Struct);
  240. break;
  241. }
  242. case CARBON_KIND(Step::StructStart _): {
  243. AppendStructuralConcreteOpenParen(
  244. TypeStructure::ConcreteTypeStart::Struct);
  245. break;
  246. }
  247. case CARBON_KIND(Step::TupleStartOnly _): {
  248. AppendStructuralConcrete(TypeStructure::ConcreteTypeStart::Tuple);
  249. break;
  250. }
  251. case CARBON_KIND(Step::TupleStart _): {
  252. AppendStructuralConcreteOpenParen(
  253. TypeStructure::ConcreteTypeStart::Tuple);
  254. break;
  255. }
  256. case CARBON_KIND(Step::InterfaceStartOnly interface_start): {
  257. AppendStructuralConcrete(interface_start.interface_id);
  258. break;
  259. }
  260. case CARBON_KIND(Step::InterfaceStart interface_start): {
  261. AppendStructuralConcreteOpenParen(interface_start.interface_id);
  262. break;
  263. }
  264. case CARBON_KIND(Step::IntStart int_start): {
  265. AppendStructuralConcreteOpenParen(int_start.type_id);
  266. break;
  267. }
  268. // Types which only have an `OpenParen` shape.
  269. case CARBON_KIND(Step::ArrayStart _): {
  270. AppendStructuralConcreteOpenParen(
  271. TypeStructure::ConcreteTypeStart::Array);
  272. break;
  273. }
  274. case CARBON_KIND(Step::ConstStart _): {
  275. AppendStructuralConcreteOpenParen(
  276. TypeStructure::ConcreteTypeStart::Const);
  277. break;
  278. }
  279. case CARBON_KIND(Step::MaybeUnformedStart _): {
  280. AppendStructuralConcreteOpenParen(
  281. TypeStructure::ConcreteTypeStart::MaybeUnformed);
  282. break;
  283. }
  284. case CARBON_KIND(Step::PartialStart _): {
  285. AppendStructuralConcreteOpenParen(
  286. TypeStructure::ConcreteTypeStart::Partial);
  287. break;
  288. }
  289. case CARBON_KIND(Step::PointerStart _): {
  290. AppendStructuralConcreteOpenParen(
  291. TypeStructure::ConcreteTypeStart::Pointer);
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. auto BuildTypeStructure(Context& context, SemIR::InstId self_inst_id,
  298. SemIR::SpecificInterface interface)
  299. -> std::optional<TypeStructure> {
  300. TypeStructureBuilder builder(&context);
  301. return builder.Run(self_inst_id, interface);
  302. }
  303. } // namespace Carbon::Check