Files
swift-mirror/lib/AST/RequirementMachine/Symbol.h
Josh Soref 81d3ad76ac Spelling ast (#42463)
* spelling: accessor

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: accommodates

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: argument

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: associated

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: availability

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: available

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: belongs

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: bookkeeping

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: building

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: clazz

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: clonable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: closure

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: concatenated

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conformance

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: context

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conversion

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: correspondence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: declarations

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: declared

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: defining

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: delayed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: dependency

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: deployed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: descendants

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: diagnose

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: diagnostic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: equitable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: evaluation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: exclusivity

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: existence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: existential

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: explicit

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: expressed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: for

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: foreign

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: function

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: identifier

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implicit

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: indices

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: information

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instance

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interchangeable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interface

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: introduced

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: invalid

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: kind-in

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: least

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: library

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: location

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: namespace

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: necessary

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: nonexistent

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: not

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: number

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: obtains

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: occurs

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: opaque

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overridden

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: parameter

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: precede

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: preceding

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: property

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: protocol

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: qualified

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: recognized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: recursively

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: references

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: relaxing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: represented

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: request

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: requirement

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: requirements

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: retrieve

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: returned

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: satisfied

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: satisfy

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: scanner

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: siblings

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simplified

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: something

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: source

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: specializations

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: specially

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: statement

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: stripped

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: structure

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: substitution

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: transform

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: transformed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: transitively

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: transparent

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: typecheck

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unknown

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unlabeled

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unqualified

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: whether

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: with

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: scanner

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 12:57:16 -07:00

287 lines
8.4 KiB
C++

//===--- 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 "swift/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#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::getConcreteSubstitutionSchema().
///
/// 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,
//////
////// "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 = 8;
static const 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;
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 forLayout(LayoutConstraint layout,
RewriteContext &ctx);
static Symbol forSuperclass(CanType type,
ArrayRef<Term> substitutions,
RewriteContext &ctx);
static Symbol forConcreteType(CanType type,
ArrayRef<Term> substitutions,
RewriteContext &ctx);
static Symbol forConcreteConformance(CanType type,
ArrayRef<Term> substitutions,
const ProtocolDecl *proto,
RewriteContext &ctx);
const ProtocolDecl *getRootProtocol() const;
Optional<int> compare(Symbol other, RewriteContext &ctx) const;
Symbol withConcreteSubstitutions(
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