1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
//===--- Symbol.h - The generics rewrite system alphabet --------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include <optional>
#ifndef SWIFT_RQM_SYMBOL_H
#define SWIFT_RQM_SYMBOL_H
namespace llvm {
class raw_ostream;
}
namespace swift {
class CanType;
class ProtocolDecl;
class GenericTypeParamType;
class Identifier;
class LayoutConstraint;
namespace rewriting {
class MutableTerm;
class RewriteContext;
class Term;
/// The smallest element in the rewrite system.
///
/// enum Symbol {
/// case conformance(CanType, substitutions: [Term], proto: Protocol)
/// case protocol(Protocol)
/// case associatedType(Protocol, Identifier)
/// case genericParam(index: Int, depth: Int)
/// case name(Identifier)
/// case layout(LayoutConstraint)
/// case superclass(CanType, substitutions: [Term])
/// case concrete(CanType, substitutions: [Term])
/// }
///
/// For the concrete type symbol kinds (`superclass`, `concrete` and
/// `conformance`), arbitrary type parameters are replaced with generic
/// parameters with depth 0. The index is the generic parameter is an
/// index into the `substitutions` array.
///
/// This transformation allows DependentMemberTypes to be manipulated as
/// terms, with the actual concrete type structure remaining opaque to
/// the requirement machine. This transformation is implemented in
/// RewriteContext::getSubstitutionSchemaFromType().
///
/// For example, the superclass requirement
/// "T : MyClass<U.X, (Int) -> V.A.B>" is denoted with a symbol
/// structured as follows:
///
/// - type: MyClass<τ_0_0, (Int) -> τ_0_1>
/// - substitutions:
/// - U.X
/// - V.A.B
///
/// Out-of-line methods are documented in Symbol.cpp.
class Symbol final {
public:
enum class Kind : uint8_t {
//////
////// Special symbol kind that is both type-like and property-like:
//////
/// When appearing at the end of a term, denotes that the term's
/// concrete type or superclass conforms concretely to a protocol.
///
/// Introduced by property map construction when a term has both
/// a concrete type or superclass requirement and a protocol
/// conformance requirement.
///
/// This orders before Kind::Protocol, so that a rule of the form
/// T.[concrete: C : P] => T orders before T.[P] => T. This ensures
/// that homotopy reduction will try to eliminate the latter rule
/// first, if possible.
ConcreteConformance,
/// When appearing at the start of a term, denotes a nested
/// type of a protocol 'Self' type.
///
/// When appearing at the end of a term, denotes that the
/// term's type conforms to the protocol.
Protocol,
//////
////// "Type-like" symbol kinds:
//////
/// An associated type [P:T]. The parent term must be known to
/// conform to P.
AssociatedType,
/// A generic parameter, uniquely identified by depth and
/// index. Can only appear at the beginning of a term, where
/// it denotes a generic parameter of the top-level generic
/// signature.
GenericParam,
/// An unbound identifier name.
Name,
/// A shape term 'T.[shape]'. The parent term must be a
/// generic parameter.
Shape,
//////
////// "Property-like" symbol kinds:
//////
/// When appearing at the end of a term, denotes that the
/// term's type satisfies the layout constraint.
Layout,
/// When appearing at the end of a term, denotes that the term
/// is a subclass of the superclass constraint.
Superclass,
/// When appearing at the end of a term, denotes that the term
/// is exactly equal to the concrete type.
ConcreteType,
};
static const unsigned NumKinds = 9;
static const llvm::StringRef Kinds[];
private:
friend class RewriteContext;
struct Storage;
private:
const Storage *Ptr;
Symbol(const Storage *ptr) : Ptr(ptr) {}
public:
Kind getKind() const;
/// A property records something about a type term; either a protocol
/// conformance, a layout constraint, or a superclass or concrete type
/// constraint.
bool isProperty() const {
auto kind = getKind();
return (kind == Symbol::Kind::ConcreteConformance ||
kind == Symbol::Kind::Protocol ||
kind == Symbol::Kind::Layout ||
kind == Symbol::Kind::Superclass ||
kind == Symbol::Kind::ConcreteType);
}
bool hasSubstitutions() const {
auto kind = getKind();
return (kind == Kind::Superclass ||
kind == Kind::ConcreteType ||
kind == Kind::ConcreteConformance);
}
Identifier getName() const;
const ProtocolDecl *getProtocol() const;
GenericTypeParamType *getGenericParam() const;
LayoutConstraint getLayoutConstraint() const;
CanType getConcreteType() const;
llvm::ArrayRef<Term> getSubstitutions() const;
/// Returns an opaque pointer that uniquely identifies this symbol.
const void *getOpaquePointer() const {
return Ptr;
}
static Symbol fromOpaquePointer(void *ptr) {
return Symbol((Storage *) ptr);
}
static Symbol forName(Identifier name,
RewriteContext &ctx);
static Symbol forProtocol(const ProtocolDecl *proto,
RewriteContext &ctx);
static Symbol forAssociatedType(const ProtocolDecl *proto,
Identifier name,
RewriteContext &ctx);
static Symbol forGenericParam(GenericTypeParamType *param,
RewriteContext &ctx);
static Symbol forShape(RewriteContext &ctx);
static Symbol forLayout(LayoutConstraint layout,
RewriteContext &ctx);
static Symbol forSuperclass(CanType type, llvm::ArrayRef<Term> substitutions,
RewriteContext &ctx);
static Symbol forConcreteType(CanType type,
llvm::ArrayRef<Term> substitutions,
RewriteContext &ctx);
static Symbol forConcreteConformance(CanType type,
llvm::ArrayRef<Term> substitutions,
const ProtocolDecl *proto,
RewriteContext &ctx);
const ProtocolDecl *getRootProtocol() const;
std::optional<int> compare(Symbol other, RewriteContext &ctx) const;
Symbol withConcreteSubstitutions(llvm::ArrayRef<Term> substitutions,
RewriteContext &ctx) const;
Symbol transformConcreteSubstitutions(
llvm::function_ref<Term(Term)> fn,
RewriteContext &ctx) const;
Symbol prependPrefixToConcreteSubstitutions(
const MutableTerm &prefix,
RewriteContext &ctx) const;
void dump(llvm::raw_ostream &out) const;
friend bool operator==(Symbol lhs, Symbol rhs) {
return lhs.Ptr == rhs.Ptr;
}
friend bool operator!=(Symbol lhs, Symbol rhs) {
return !(lhs == rhs);
}
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out, Symbol symbol) {
symbol.dump(out);
return out;
}
};
} // end namespace rewriting
} // end namespace swift
namespace llvm {
template<> struct DenseMapInfo<swift::rewriting::Symbol> {
static swift::rewriting::Symbol getEmptyKey() {
return swift::rewriting::Symbol::fromOpaquePointer(
llvm::DenseMapInfo<void *>::getEmptyKey());
}
static swift::rewriting::Symbol getTombstoneKey() {
return swift::rewriting::Symbol::fromOpaquePointer(
llvm::DenseMapInfo<void *>::getTombstoneKey());
}
static unsigned getHashValue(swift::rewriting::Symbol Val) {
return DenseMapInfo<void *>::getHashValue(Val.getOpaquePointer());
}
static bool isEqual(swift::rewriting::Symbol LHS,
swift::rewriting::Symbol RHS) {
return LHS == RHS;
}
};
template<>
struct PointerLikeTypeTraits<swift::rewriting::Symbol> {
public:
static inline void *getAsVoidPointer(swift::rewriting::Symbol Val) {
return const_cast<void *>(Val.getOpaquePointer());
}
static inline swift::rewriting::Symbol getFromVoidPointer(void *Ptr) {
return swift::rewriting::Symbol::fromOpaquePointer(Ptr);
}
enum { NumLowBitsAvailable = 1 };
};
} // end namespace llvm
#endif
|