Skip to content

Kill canonical mangling signature #4617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
21ad46b
AST: Replace ArchetypeBuilder's adoptArchetypes flag with generic env…
slavapestov Aug 29, 2016
fb347db
AST: Add dump() method to Requirement and GenericEnvironment
slavapestov Sep 1, 2016
247852c
AST: Remove unused parameter from {ProtocolConformance,Substitution}:…
slavapestov Aug 31, 2016
798a07a
AST: Fix Type::transform() building of GenericSignatures
slavapestov Aug 31, 2016
fbe9573
AST: Add some utility methods to GenericSignature
slavapestov Aug 31, 2016
19c982b
AST: Add new TypeBase::getSuperclassForDecl()
slavapestov Sep 2, 2016
c285293
AST: Tweak conformance lookup for class-constrained archetypes
slavapestov Sep 3, 2016
07790d3
SILGen: Re-work witness thunk emission a bit
slavapestov Aug 31, 2016
772cf3a
SIL Optimizer: More principled substitution remapping in devirtualizer
slavapestov Aug 31, 2016
890863d
ArchetypeBuilder: const correctness
slavapestov Aug 29, 2016
71cf591
ArchetypeBuilder: More principled usage of RequirementSource
slavapestov Aug 29, 2016
f19192d
ArchetypeBuilder: Sort requirements in enumerateRequirements()
slavapestov Aug 30, 2016
7a7af62
ArchetypeBuilder: use enumerateRequirements() in getGenericSignature()
slavapestov Aug 31, 2016
9771ec8
Sema: Improve checking of associated type superclass constraints
slavapestov Sep 3, 2016
15fd3da
SIL: Prepare parser for removal of getCanonicalManglingSignature()
slavapestov Sep 3, 2016
3b17217
AST: Nuke GenericSignature::getCanonicalManglingSignature()
slavapestov Sep 2, 2016
38e58b2
AST: Fix regression with reference-dependencies-members test
slavapestov Sep 4, 2016
dc49f82
Sema: Eagerly mark associated type conformances as used
slavapestov Sep 5, 2016
db4464b
Sema: Remove unused parameter from ConformanceChecker::recordTypeWitn…
slavapestov Sep 5, 2016
2149061
Sema: Clean up hack forcing use of NSError-related conformances
slavapestov Sep 5, 2016
6e77f62
AST: Add some handy assertions to GenericSignature
slavapestov Aug 29, 2016
af56a01
Sema: Add a couple of comments
slavapestov Sep 5, 2016
0c30979
Resolve some crashers
slavapestov Sep 1, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ class ASTContext {

/// Cache of remapped types (useful for diagnostics).
llvm::StringMap<Type> RemappedTypes;

/// Cache for generic mangling signatures.
llvm::DenseMap<std::pair<GenericSignature*, ModuleDecl*>,
CanGenericSignature> ManglingSignatures;

private:
/// \brief The current generation number, which reflects the number of
Expand Down Expand Up @@ -838,12 +834,6 @@ class ASTContext {
ArchetypeBuilder *getOrCreateArchetypeBuilder(CanGenericSignature sig,
ModuleDecl *mod);

/// Set the stored archetype builder for the given canonical generic
/// signature and module.
void setArchetypeBuilder(CanGenericSignature sig,
ModuleDecl *mod,
std::unique_ptr<ArchetypeBuilder> builder);

/// Retrieve the inherited name set for the given class.
const InheritedNameSet *getAllPropertyNames(ClassDecl *classDecl,
bool forInstance);
Expand Down
44 changes: 22 additions & 22 deletions include/swift/AST/ArchetypeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ class RequirementSource {
/// The requirement was explicitly stated in the generic parameter
/// clause.
Explicit,
/// The requirement was explicitly stated in the generic parameter clause
/// but is redundant with some other requirement.
Redundant,
/// The requirement was part of a protocol requirement, e.g., an
/// inherited protocol or a requirement on an associated type.
Protocol,
///
/// The requirement was inferred from part of the signature.
/// The requirement was inferred from the function's parameter or
/// result types.
Inferred,

/// The requirement was part of a protocol requirement on an
/// associated type.
///
/// These are dropped when building the GenericSignature.
Protocol,

/// The requirement is redundant with some other requirement.
///
/// These are dropped when building the GenericSignature.
Redundant,

/// The requirement came from an outer scope.
/// FIXME: eliminate this in favor of keeping requirement sources in
/// GenericSignatures, at least non-canonical ones?
Expand Down Expand Up @@ -242,9 +248,10 @@ class ArchetypeBuilder {
/// \brief Add all of a generic signature's parameters and requirements.
///
/// FIXME: Requirements from the generic signature are treated as coming from
/// an outer scope in order to avoid disturbing the AllDependentTypes.
/// Setting \c treatRequirementsAsExplicit to true disables this behavior.
void addGenericSignature(GenericSignature *sig, bool adoptArchetypes,
/// an outer scope. Setting \c treatRequirementsAsExplicit to true disables
/// this behavior.
void addGenericSignature(GenericSignature *sig,
GenericEnvironment *genericEnv,
bool treatRequirementsAsExplicit = false);

/// \brief Get a generic signature based on the provided complete list
Expand Down Expand Up @@ -342,13 +349,6 @@ class ArchetypeBuilder {
using SameTypeRequirement
= std::pair<PotentialArchetype *,
PointerUnion<Type, PotentialArchetype*>>;

/// Retrieve the set of same-type requirements that apply to the potential
/// archetypes known to this builder.
ArrayRef<SameTypeRequirement> getSameTypeRequirements() const;

// FIXME: Compute the set of 'extra' witness tables needed to express this
// requirement set.

/// \brief Dump all of the requirements, both specified and inferred.
LLVM_ATTRIBUTE_DEPRECATED(
Expand Down Expand Up @@ -617,15 +617,15 @@ class ArchetypeBuilder::PotentialArchetype {
}

void setIsRecursive() { IsRecursive = true; }
bool isRecursive() { return IsRecursive; }
bool isRecursive() const { return IsRecursive; }

bool isInvalid() { return Invalid; }
bool isInvalid() const { return Invalid; }

void setInvalid() { Invalid = true; }

/// Determine whether this archetype was renamed due to typo
/// correction. If so, \c getName() retrieves the new name.
bool wasRenamed() { return Renamed; }
bool wasRenamed() const { return Renamed; }

/// Note that this potential archetype was renamed (due to typo
/// correction), providing the new name.
Expand All @@ -636,7 +636,7 @@ class ArchetypeBuilder::PotentialArchetype {

/// Whether this potential archetype makes a better archetype anchor than
/// the given archetype anchor.
bool isBetterArchetypeAnchor(PotentialArchetype *other);
bool isBetterArchetypeAnchor(PotentialArchetype *other) const;

void dump(llvm::raw_ostream &Out, SourceManager *SrcMgr,
unsigned Indent);
Expand Down
11 changes: 6 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,8 @@ NOTE(no_witnesses_type,none,
"protocol requires nested type %0; do you want to add it?", (Identifier))
NOTE(default_associated_type_req_fail,none,
"default type %0 for associated type %1 (from protocol %2) "
"does not conform to %3",
(Type, DeclName, Type, Type))
"does not %select{inherit from|conform to}4 %3",
(Type, DeclName, Type, Type, bool))
ERROR(associated_type_access,none,
"associated type in "
"%select{PRIVATE|a fileprivate|an internal|a public}0 protocol uses "
Expand All @@ -1346,8 +1346,8 @@ NOTE(bad_associated_type_deduction,none,
(DeclName, DeclName))
NOTE(associated_type_deduction_witness_failed,none,
"inferred type %1 (by matching requirement %0) is invalid: "
"does not conform to %2",
(DeclName, Type, DeclName))
"does not %select{inherit from|conform to}3 %2",
(DeclName, Type, DeclName, bool))
NOTE(ambiguous_associated_type_deduction,none,
"ambiguous inference of associated type %0: %1 vs. %2",
(DeclName, Type, Type))
Expand Down Expand Up @@ -1405,7 +1405,8 @@ NOTE(protocol_witness_not_objc,none,
NOTE(protocol_witness_type,none,
"possibly intended match", ())
NOTE(protocol_witness_nonconform_type,none,
"possibly intended match %0 does not conform to %1", (Type, Type))
"possibly intended match %0 does not "
"%select{inherit from|conform to}2 %1", (Type, Type, bool))

NOTE(protocol_requirement_here,none,
"requirement %0 declared here", (DeclName))
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/GenericEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class GenericEnvironment final {

ArrayRef<Substitution>
getForwardingSubstitutions(ModuleDecl *M, GenericSignature *sig) const;

void dump() const;
};

} // end namespace swift
Expand Down
31 changes: 18 additions & 13 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace swift {
class ArchetypeBuilder;
class ProtocolType;

using TypeConformanceMap
= llvm::DenseMap<TypeBase *, ArrayRef<ProtocolConformanceRef>>;

/// Iterator that walks the generic parameter types declared in a generic
/// signature and their dependent members.
class GenericSignatureWitnessIterator {
Expand Down Expand Up @@ -171,15 +174,27 @@ class GenericSignature final : public llvm::FoldingSetNode,
/// the order of generic parameters in getGenericParams().
TypeSubstitutionMap getSubstitutionMap(ArrayRef<Substitution> args) const;

/// Variant of the above that also returns conformances.
void getSubstitutionMap(ArrayRef<Substitution> subs,
TypeSubstitutionMap &subMap,
TypeConformanceMap &conformanceMap) const;

using LookupConformanceFn =
llvm::function_ref<ProtocolConformanceRef(Type, ProtocolType *)>;
llvm::function_ref<ProtocolConformanceRef(CanType, Type, ProtocolType *)>;

/// Build an array of substitutions from an interface type substitution map,
/// using the given function to look up conformances.
void getSubstitutions(ModuleDecl &mod,
const TypeSubstitutionMap &subs,
const TypeSubstitutionMap &subMap,
LookupConformanceFn lookupConformance,
SmallVectorImpl<Substitution> &result);
SmallVectorImpl<Substitution> &result) const;

/// Build an array of substitutions from an interface type substitution map,
/// using the given function to look up conformances.
void getSubstitutions(ModuleDecl &mod,
const TypeSubstitutionMap &subMap,
const TypeConformanceMap &conformanceMap,
SmallVectorImpl<Substitution> &result) const;

/// Return a range that iterates through first all of the generic parameters
/// of the signature, followed by all of their recursive member types exposed
Expand All @@ -195,16 +210,6 @@ class GenericSignature final : public llvm::FoldingSetNode,

/// Canonicalize the components of a generic signature.
CanGenericSignature getCanonicalSignature() const;

/// Canonicalize a generic signature down to its essential requirements,
/// for mangling purposes.
///
/// TODO: This is what getCanonicalSignature() ought to do, but currently
/// does not due to former implementation dependencies on
/// 'getAllDependentTypes' order matching 'getAllArchetypes' order of a
/// generic param list. Now that 'getAllArchetypes' is gone, we might
/// be able to move forward here.
CanGenericSignature getCanonicalManglingSignature(ModuleDecl &M) const;

/// Uniquing for the ASTContext.
void Profile(llvm::FoldingSetNodeID &ID) {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ class SourceFile final : public FileUnit {
SourceLoc diagLoc = {});
/// @}

ReferencedNameTracker *getReferencedNameTracker() {
ReferencedNameTracker *getReferencedNameTracker() const {
return ReferencedNames;
}
void setReferencedNameTracker(ReferencedNameTracker *Tracker) {
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/ProtocolConformance.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
/// applies to the substituted type.
ProtocolConformance *subst(ModuleDecl *module,
Type substType,
ArrayRef<Substitution> subs,
TypeSubstitutionMap &subMap,
ArchetypeConformanceMap &conformanceMap);
};
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/Requirement.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Requirement {
Type getSecondType() const {
return SecondType;
}

void dump() const;
};

} // end namespace swift
Expand Down
7 changes: 3 additions & 4 deletions include/swift/AST/Substitution.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ class Substitution {
GenericEnvironment *env,
ArrayRef<Substitution> subs) const;

private:
friend class ProtocolConformance;

Substitution subst(ModuleDecl *module,
ArrayRef<Substitution> subs,
TypeSubstitutionMap &subMap,
ArchetypeConformanceMap &conformanceMap) const;

private:
friend class ProtocolConformance;
};

void dump(const ArrayRef<Substitution> &subs);
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,19 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// a type's subclass.
bool isExactSuperclassOf(Type ty, LazyResolver *resolver);

/// \brief Get the substituted base class type, starting from a base class
/// declaration and a substituted derived class type.
///
/// For example, given the following declarations:
///
/// class A<T, U> {}
/// class B<V> : A<Int, V> {}
/// class C<X, Y> : B<Y> {}
///
/// Calling `C<String, NSObject>`->getSuperclassForDecl(`A`) will return
/// `A<Int, NSObject>`.
Type getSuperclassForDecl(const ClassDecl *classDecl, LazyResolver *resolver);

/// \brief True if this type is the superclass of another type, or a generic
/// type that could be bound to the superclass.
///
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class SILModule {
lookUpWitnessTable(const ProtocolConformance *C, bool deserializeLazily=true);

/// Attempt to lookup \p Member in the witness table for \p C.
std::tuple<SILFunction *, SILWitnessTable *, ArrayRef<Substitution>>
std::pair<SILFunction *, SILWitnessTable *>
lookUpFunctionInWitnessTable(ProtocolConformanceRef C,
SILDeclRef Requirement);

Expand Down
1 change: 0 additions & 1 deletion include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#ifndef SIL_TypeLowering_h
#define SIL_TypeLowering_h

#include "swift/AST/ArchetypeBuilder.h"
#include "swift/AST/CaptureInfo.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/SIL/AbstractionPattern.h"
Expand Down
16 changes: 6 additions & 10 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ ArchetypeBuilder *ASTContext::getOrCreateArchetypeBuilder(

// Create a new archetype builder with the given signature.
auto builder = new ArchetypeBuilder(*mod, Diags);
builder->addGenericSignature(sig, /*adoptArchetypes=*/false,
builder->addGenericSignature(sig, nullptr,
/*treatRequirementsAsExplicit=*/true);

// Store this archetype builder.
Expand All @@ -1459,15 +1459,6 @@ ArchetypeBuilder *ASTContext::getOrCreateArchetypeBuilder(
return builder;
}

void ASTContext::setArchetypeBuilder(CanGenericSignature sig,
ModuleDecl *mod,
std::unique_ptr<ArchetypeBuilder> builder) {
if (Impl.ArchetypeBuilders.find({sig, mod})
== Impl.ArchetypeBuilders.end()) {
Impl.ArchetypeBuilders[{sig, mod}] = move(builder);
}
}

Module *
ASTContext::getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) {
assert(!ModulePath.empty());
Expand Down Expand Up @@ -3709,6 +3700,11 @@ GenericSignature *GenericSignature::get(ArrayRef<GenericTypeParamType *> params,
bool isKnownCanonical) {
assert(!params.empty());

#ifndef NDEBUG
for (auto req : requirements)
assert(req.getFirstType()->isTypeParameter());
#endif

// Check for an existing generic signature.
llvm::FoldingSetNodeID ID;
GenericSignature::Profile(ID, params, requirements);
Expand Down
9 changes: 9 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/Basic/STLExtras.h"
Expand Down Expand Up @@ -3092,3 +3093,11 @@ void TypeBase::dump(raw_ostream &os, unsigned indent) const {
llvm::SaveAndRestore<bool> X(ctx.LangOpts.DebugConstraintSolver, true);
Type(const_cast<TypeBase *>(this)).dump(os, indent);
}

void GenericEnvironment::dump() const {
llvm::errs() << "Generic environment:\n";
for (auto pair : getInterfaceToArchetypeMap()) {
pair.first->dump();
pair.second->dump();
}
}
21 changes: 21 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4383,6 +4383,27 @@ void GenericSignature::dump() const {
llvm::errs() << '\n';
}

void Requirement::dump() const {
switch (getKind()) {
case RequirementKind::WitnessMarker:
llvm::errs() << "witness_marker: ";
break;
case RequirementKind::Conformance:
llvm::errs() << "conforms_to: ";
break;
case RequirementKind::Superclass:
llvm::errs() << "superclass: ";
break;
case RequirementKind::SameType:
llvm::errs() << "same_type: ";
break;
}

if (getFirstType()) llvm::errs() << getFirstType() << " ";
if (getSecondType()) llvm::errs() << getSecondType();
llvm::errs() << "\n";
}

std::string GenericSignature::getAsString() const {
std::string result;
llvm::raw_string_ostream out(result);
Expand Down
Loading