matching_impl_set.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef CARBON_EXPLORER_INTERPRETER_MATCHING_IMPL_SET_H_
  5. #define CARBON_EXPLORER_INTERPRETER_MATCHING_IMPL_SET_H_
  6. #include <vector>
  7. #include "common/ostream.h"
  8. #include "explorer/ast/declaration.h"
  9. #include "explorer/ast/value.h"
  10. #include "explorer/common/nonnull.h"
  11. #include "explorer/interpreter/impl_scope.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. namespace Carbon {
  14. // A set of impl matches that we're currently performing. Each `Match`
  15. // represents an attempt to match a `type as interface` query against an `impl`
  16. // declaration. This is used to detect and reject non-termination when impl
  17. // matching recursively triggers further impl matching.
  18. //
  19. // The language rule we use to detect potential non-termination is to count the
  20. // number of times each "label" appears within the type and interface, where a
  21. // label is the name of a declared entity such as a class or interface, or a
  22. // primitive like `type` or `bool`. For example, `Optional(i32*)` contains the
  23. // labels for `Optional`, `i32`, and `*` (built-in pointer type) once each. If
  24. // we ever try matching the same `impl` twice, where the inner match contains
  25. // at least as many appearances of each label as the outer match, we reject the
  26. // program as invalid. We also reject if a query results in the exact same
  27. // query being performed again.
  28. //
  29. // This class is an implementation detail of `TypeChecker::MatchImpl`.
  30. class MatchingImplSet {
  31. private:
  32. class LeafCollector;
  33. enum class Label : int;
  34. public:
  35. // An RAII type that tracks an impl match that we're currently performing.
  36. // One instance of this class will exist for each in-progress call to
  37. // `MatchImpl`.
  38. class Match {
  39. public:
  40. explicit Match(Nonnull<MatchingImplSet*> parent,
  41. Nonnull<const ImplScope::ImplFact*> impl,
  42. Nonnull<const Value*> type, Nonnull<const Value*> interface);
  43. ~Match();
  44. Match(const Match&) = delete;
  45. auto operator=(const Match&) -> Match& = delete;
  46. // Check to see if this match duplicates any prior one within the same set,
  47. // or if there's a simpler form of this match in the set. If so, returns a
  48. // suitable error. This should be delayed until we know that the impl
  49. // structurally matches the type and interface.
  50. auto DiagnosePotentialCycle(SourceLocation source_loc) -> ErrorOr<Success>;
  51. private:
  52. friend class LeafCollector;
  53. // The set that this match is part of.
  54. Nonnull<MatchingImplSet*> parent_;
  55. // The `impl` that is being matched against.
  56. Nonnull<const ImplScope::ImplFact*> impl_;
  57. // The type that is being matched against the impl.
  58. Nonnull<const Value*> type_;
  59. // The interface that is being matched against the impl.
  60. Nonnull<const Value*> interface_;
  61. // The number of times each label appears in the type or interface.
  62. llvm::DenseMap<Label, int> signature_;
  63. };
  64. private:
  65. friend class llvm::DenseMapInfo<Label>;
  66. // An opaque integer used to identify a particular label appearing in a type,
  67. // such as a class name. The named enumerators represent builtins, and values
  68. // >= `FirstDeclarationLabel` represent declarations from the program.
  69. enum class Label : int {
  70. // Label for `type` type constant.
  71. TypeType,
  72. // Label for `bool` type constant.
  73. BoolType,
  74. // Label for `i32` type constant.
  75. IntType,
  76. // Label for `String` type constant.
  77. StringType,
  78. // Label for `[_;_]` type constructor.
  79. ArrayType,
  80. // Label for `_*` type constructor.
  81. PointerType,
  82. // Label for `{.a: _, .b: _, ...}` struct type constructor. We use the same
  83. // label regardless of the arity of the struct type and any field names.
  84. StructType,
  85. // Label for `(_, _, ..., _)` tuple type constructor. We use the same label
  86. // regardless of the arity of the tuple type.
  87. TupleType,
  88. // First Label value corresponding to a Declaration. Must be kept at the
  89. // end of the enum.
  90. FirstDeclarationLabel
  91. };
  92. // Get the Label that represents a given declaration.
  93. auto GetLabelForDeclaration(const Declaration& declaration) -> Label;
  94. // The known declarations and their labels.
  95. llvm::DenseMap<const Declaration*, Label> declaration_labels_;
  96. // The matches that are currently being performed, in order from outermost to
  97. // innermost.
  98. std::vector<Match*> matches_;
  99. };
  100. } // namespace Carbon
  101. // Support use of Label as a DenseMap key.
  102. template <>
  103. struct llvm::DenseMapInfo<Carbon::MatchingImplSet::Label> {
  104. using Base = llvm::DenseMapInfo<int>;
  105. using Label = Carbon::MatchingImplSet::Label;
  106. static inline auto getEmptyKey() -> Label {
  107. return static_cast<Label>(Base::getEmptyKey());
  108. }
  109. static inline auto getTombstoneKey() -> Label {
  110. return static_cast<Label>(Base::getTombstoneKey());
  111. }
  112. static inline auto getHashValue(Label label) -> unsigned {
  113. return Base::getHashValue(static_cast<int>(label));
  114. }
  115. static auto isEqual(Label a, Label b) -> bool { return a == b; }
  116. };
  117. #endif // CARBON_EXPLORER_INTERPRETER_MATCHING_IMPL_SET_H_