mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
We want `T.A == U.B` to imply `shape(T) == shape(U)` if T (and thus U)
is a parameter pack.
To do this, we introduce some new rewrite rules:
1) For each associated type symbol `[P:A]`, a rule `([P:A].[shape] => [P:A])`.
2) For each non-pack generic parameter `τ_d_i`, a rule `τ_d_i.[shape] => [shape]`.
Now consider a rewrite rule `(τ_d_i.[P:A] => τ_D_I.[Q:B])`. The left-hand
side overlaps with the rule `([P:A].[shape] => [shape])` on the term
`τ_d_i.[P:A].[shape]`. Resolving the overlap gives us a new rule
t_d_i.[shape] => T_D_I.[shape]
If T is a term corresponding to some type parameter, we say that `T.[shape]` is
a shape term. If `T'.[shape]` is a reduced term, we say that T' is the reduced
shape of T.
Recall that shape requirements are represented as rules of the form:
τ_d_i.[shape] => τ_D_I.[shape]
Now, the rules of the first kind reduce our shape term `T.[shape]` to
`τ_d_i.[shape]`, where `τ_d_i` is the root generic parameter of T.
If `τ_d_i` is not a pack, a rule of the second kind reduces it to `[shape]`,
so the reduced shape of a non-pack parameter T is the empty term.
Otherwise, if `τ_d_i` is a pack, `τ_d_i.[shape]` might reduce to `τ_D_I.[shape]`
via a shape requirement. In this case, `τ_D_I` is the reduced shape of T.
Fixes rdar://problem/101813873.
131 lines
5.0 KiB
C++
131 lines
5.0 KiB
C++
//===--- RuleBuilder.h - Lowering desugared requirements to rules ---------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_RULEBUILDER_H
|
|
#define SWIFT_RULEBUILDER_H
|
|
|
|
#include "swift/AST/ASTContext.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/DenseSet.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include <vector>
|
|
#include "RewriteContext.h"
|
|
#include "Rule.h"
|
|
#include "Symbol.h"
|
|
#include "Term.h"
|
|
|
|
namespace llvm {
|
|
class raw_ostream;
|
|
}
|
|
|
|
namespace swift {
|
|
|
|
class AssociatedTypeDecl;
|
|
class ProtocolDecl;
|
|
class ProtocolTypeAlias;
|
|
class Requirement;
|
|
|
|
namespace rewriting {
|
|
|
|
/// A utility class for building rewrite rules from the top-level requirements
|
|
/// of a generic signature.
|
|
///
|
|
/// This also collects requirements from the transitive closure of all protocols
|
|
/// appearing on the right hand side of conformance requirements.
|
|
struct RuleBuilder {
|
|
RewriteContext &Context;
|
|
|
|
/// The transitive closure of all protocols appearing on the right hand
|
|
/// side of conformance requirements.
|
|
llvm::DenseSet<const ProtocolDecl *> &ReferencedProtocols;
|
|
|
|
/// A subset of the above in insertion order, consisting of the protocols
|
|
/// whose rules we are going to import.
|
|
///
|
|
/// If this is a rewrite system built from a generic signature, this vector
|
|
/// contains all elements in the above set.
|
|
///
|
|
/// If this is a rewrite system built from a strongly connected component
|
|
/// of the protocol, this vector contains all elements in the above set
|
|
/// except for the protocols belonging to the component representing the
|
|
/// rewrite system itself; those protocols are added directly instead of
|
|
/// being imported.
|
|
std::vector<const ProtocolDecl *> ProtocolsToImport;
|
|
|
|
/// The rules representing a complete rewrite system for the above vector,
|
|
/// pulled in by collectRulesFromReferencedProtocols().
|
|
std::vector<Rule> ImportedRules;
|
|
|
|
/// New rules to add which will be marked 'permanent'. These are rules for
|
|
/// introducing associated types, and relationships between layout,
|
|
/// superclass and concrete type symbols. They are not eliminated by
|
|
/// homotopy reduction, since they are always added when the rewrite system
|
|
/// is built.
|
|
std::vector<std::pair<MutableTerm, MutableTerm>> PermanentRules;
|
|
|
|
/// New rules derived from requirements written by the user, which can be
|
|
/// eliminated by homotopy reduction.
|
|
std::vector<std::tuple<MutableTerm, MutableTerm, llvm::Optional<unsigned>>>
|
|
RequirementRules;
|
|
|
|
/// Requirements written in source code. The requirement ID in the above
|
|
/// \c RequirementRules vector is an index into this array.
|
|
std::vector<StructuralRequirement> WrittenRequirements;
|
|
|
|
/// Enables debugging output. Controlled by the -dump-requirement-machine
|
|
/// frontend flag.
|
|
unsigned Dump : 1;
|
|
|
|
/// Used to ensure the initWith*() methods are only called once.
|
|
unsigned Initialized : 1;
|
|
|
|
RuleBuilder(RewriteContext &ctx,
|
|
llvm::DenseSet<const ProtocolDecl *> &referencedProtocols)
|
|
: Context(ctx), ReferencedProtocols(referencedProtocols) {
|
|
Dump = ctx.getASTContext().LangOpts.DumpRequirementMachine;
|
|
Initialized = 0;
|
|
}
|
|
|
|
void initWithGenericSignature(ArrayRef<GenericTypeParamType *> genericParams,
|
|
ArrayRef<Requirement> requirements);
|
|
void initWithWrittenRequirements(ArrayRef<GenericTypeParamType *> genericParams,
|
|
ArrayRef<StructuralRequirement> requirements);
|
|
void initWithProtocolSignatureRequirements(ArrayRef<const ProtocolDecl *> proto);
|
|
void initWithProtocolWrittenRequirements(
|
|
ArrayRef<const ProtocolDecl *> component,
|
|
const llvm::DenseMap<const ProtocolDecl *,
|
|
SmallVector<StructuralRequirement, 4>> protos);
|
|
void initWithConditionalRequirements(ArrayRef<Requirement> requirements,
|
|
ArrayRef<Term> substitutions);
|
|
void addReferencedProtocol(const ProtocolDecl *proto);
|
|
void collectRulesFromReferencedProtocols();
|
|
void collectPackShapeRules(ArrayRef<GenericTypeParamType *> genericParams);
|
|
|
|
private:
|
|
void addPermanentProtocolRules(const ProtocolDecl *proto);
|
|
void addAssociatedType(const AssociatedTypeDecl *type,
|
|
const ProtocolDecl *proto);
|
|
void addRequirement(const Requirement &req, const ProtocolDecl *proto,
|
|
llvm::Optional<ArrayRef<Term>> substitutions = llvm::None,
|
|
llvm::Optional<unsigned> requirementID = llvm::None);
|
|
void addRequirement(const StructuralRequirement &req,
|
|
const ProtocolDecl *proto);
|
|
void addTypeAlias(const ProtocolTypeAlias &alias,
|
|
const ProtocolDecl *proto);
|
|
};
|
|
|
|
} // end namespace rewriting
|
|
|
|
} // end namespace swift
|
|
|
|
#endif
|