Skip to content

Commit 1487d59

Browse files
authored
Merge pull request #4617 from slavapestov/kill-canonical-mangling-signature
Kill canonical mangling signature
2 parents 9948a35 + 0c30979 commit 1487d59

File tree

72 files changed

+1251
-1490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1251
-1490
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ class ASTContext {
230230

231231
/// Cache of remapped types (useful for diagnostics).
232232
llvm::StringMap<Type> RemappedTypes;
233-
234-
/// Cache for generic mangling signatures.
235-
llvm::DenseMap<std::pair<GenericSignature*, ModuleDecl*>,
236-
CanGenericSignature> ManglingSignatures;
237233

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

841-
/// Set the stored archetype builder for the given canonical generic
842-
/// signature and module.
843-
void setArchetypeBuilder(CanGenericSignature sig,
844-
ModuleDecl *mod,
845-
std::unique_ptr<ArchetypeBuilder> builder);
846-
847837
/// Retrieve the inherited name set for the given class.
848838
const InheritedNameSet *getAllPropertyNames(ClassDecl *classDecl,
849839
bool forInstance);

include/swift/AST/ArchetypeBuilder.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,21 @@ class RequirementSource {
6363
/// The requirement was explicitly stated in the generic parameter
6464
/// clause.
6565
Explicit,
66-
/// The requirement was explicitly stated in the generic parameter clause
67-
/// but is redundant with some other requirement.
68-
Redundant,
69-
/// The requirement was part of a protocol requirement, e.g., an
70-
/// inherited protocol or a requirement on an associated type.
71-
Protocol,
72-
///
73-
/// The requirement was inferred from part of the signature.
66+
/// The requirement was inferred from the function's parameter or
67+
/// result types.
7468
Inferred,
69+
70+
/// The requirement was part of a protocol requirement on an
71+
/// associated type.
72+
///
73+
/// These are dropped when building the GenericSignature.
74+
Protocol,
75+
76+
/// The requirement is redundant with some other requirement.
77+
///
78+
/// These are dropped when building the GenericSignature.
79+
Redundant,
80+
7581
/// The requirement came from an outer scope.
7682
/// FIXME: eliminate this in favor of keeping requirement sources in
7783
/// GenericSignatures, at least non-canonical ones?
@@ -242,9 +248,10 @@ class ArchetypeBuilder {
242248
/// \brief Add all of a generic signature's parameters and requirements.
243249
///
244250
/// FIXME: Requirements from the generic signature are treated as coming from
245-
/// an outer scope in order to avoid disturbing the AllDependentTypes.
246-
/// Setting \c treatRequirementsAsExplicit to true disables this behavior.
247-
void addGenericSignature(GenericSignature *sig, bool adoptArchetypes,
251+
/// an outer scope. Setting \c treatRequirementsAsExplicit to true disables
252+
/// this behavior.
253+
void addGenericSignature(GenericSignature *sig,
254+
GenericEnvironment *genericEnv,
248255
bool treatRequirementsAsExplicit = false);
249256

250257
/// \brief Get a generic signature based on the provided complete list
@@ -342,13 +349,6 @@ class ArchetypeBuilder {
342349
using SameTypeRequirement
343350
= std::pair<PotentialArchetype *,
344351
PointerUnion<Type, PotentialArchetype*>>;
345-
346-
/// Retrieve the set of same-type requirements that apply to the potential
347-
/// archetypes known to this builder.
348-
ArrayRef<SameTypeRequirement> getSameTypeRequirements() const;
349-
350-
// FIXME: Compute the set of 'extra' witness tables needed to express this
351-
// requirement set.
352352

353353
/// \brief Dump all of the requirements, both specified and inferred.
354354
LLVM_ATTRIBUTE_DEPRECATED(
@@ -617,15 +617,15 @@ class ArchetypeBuilder::PotentialArchetype {
617617
}
618618

619619
void setIsRecursive() { IsRecursive = true; }
620-
bool isRecursive() { return IsRecursive; }
620+
bool isRecursive() const { return IsRecursive; }
621621

622-
bool isInvalid() { return Invalid; }
622+
bool isInvalid() const { return Invalid; }
623623

624624
void setInvalid() { Invalid = true; }
625625

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

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

637637
/// Whether this potential archetype makes a better archetype anchor than
638638
/// the given archetype anchor.
639-
bool isBetterArchetypeAnchor(PotentialArchetype *other);
639+
bool isBetterArchetypeAnchor(PotentialArchetype *other) const;
640640

641641
void dump(llvm::raw_ostream &Out, SourceManager *SrcMgr,
642642
unsigned Indent);

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,8 @@ NOTE(no_witnesses_type,none,
13331333
"protocol requires nested type %0; do you want to add it?", (Identifier))
13341334
NOTE(default_associated_type_req_fail,none,
13351335
"default type %0 for associated type %1 (from protocol %2) "
1336-
"does not conform to %3",
1337-
(Type, DeclName, Type, Type))
1336+
"does not %select{inherit from|conform to}4 %3",
1337+
(Type, DeclName, Type, Type, bool))
13381338
ERROR(associated_type_access,none,
13391339
"associated type in "
13401340
"%select{PRIVATE|a fileprivate|an internal|a public}0 protocol uses "
@@ -1347,8 +1347,8 @@ NOTE(bad_associated_type_deduction,none,
13471347
(DeclName, DeclName))
13481348
NOTE(associated_type_deduction_witness_failed,none,
13491349
"inferred type %1 (by matching requirement %0) is invalid: "
1350-
"does not conform to %2",
1351-
(DeclName, Type, DeclName))
1350+
"does not %select{inherit from|conform to}3 %2",
1351+
(DeclName, Type, DeclName, bool))
13521352
NOTE(ambiguous_associated_type_deduction,none,
13531353
"ambiguous inference of associated type %0: %1 vs. %2",
13541354
(DeclName, Type, Type))
@@ -1406,7 +1406,8 @@ NOTE(protocol_witness_not_objc,none,
14061406
NOTE(protocol_witness_type,none,
14071407
"possibly intended match", ())
14081408
NOTE(protocol_witness_nonconform_type,none,
1409-
"possibly intended match %0 does not conform to %1", (Type, Type))
1409+
"possibly intended match %0 does not "
1410+
"%select{inherit from|conform to}2 %1", (Type, Type, bool))
14101411

14111412
NOTE(protocol_requirement_here,none,
14121413
"requirement %0 declared here", (DeclName))

include/swift/AST/GenericEnvironment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class GenericEnvironment final {
6969

7070
ArrayRef<Substitution>
7171
getForwardingSubstitutions(ModuleDecl *M, GenericSignature *sig) const;
72+
73+
void dump() const;
7274
};
7375

7476
} // end namespace swift

include/swift/AST/GenericSignature.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ namespace swift {
2828
class ArchetypeBuilder;
2929
class ProtocolType;
3030

31+
using TypeConformanceMap
32+
= llvm::DenseMap<TypeBase *, ArrayRef<ProtocolConformanceRef>>;
33+
3134
/// Iterator that walks the generic parameter types declared in a generic
3235
/// signature and their dependent members.
3336
class GenericSignatureWitnessIterator {
@@ -171,15 +174,27 @@ class GenericSignature final : public llvm::FoldingSetNode,
171174
/// the order of generic parameters in getGenericParams().
172175
TypeSubstitutionMap getSubstitutionMap(ArrayRef<Substitution> args) const;
173176

177+
/// Variant of the above that also returns conformances.
178+
void getSubstitutionMap(ArrayRef<Substitution> subs,
179+
TypeSubstitutionMap &subMap,
180+
TypeConformanceMap &conformanceMap) const;
181+
174182
using LookupConformanceFn =
175-
llvm::function_ref<ProtocolConformanceRef(Type, ProtocolType *)>;
183+
llvm::function_ref<ProtocolConformanceRef(CanType, Type, ProtocolType *)>;
176184

177185
/// Build an array of substitutions from an interface type substitution map,
178186
/// using the given function to look up conformances.
179187
void getSubstitutions(ModuleDecl &mod,
180-
const TypeSubstitutionMap &subs,
188+
const TypeSubstitutionMap &subMap,
181189
LookupConformanceFn lookupConformance,
182-
SmallVectorImpl<Substitution> &result);
190+
SmallVectorImpl<Substitution> &result) const;
191+
192+
/// Build an array of substitutions from an interface type substitution map,
193+
/// using the given function to look up conformances.
194+
void getSubstitutions(ModuleDecl &mod,
195+
const TypeSubstitutionMap &subMap,
196+
const TypeConformanceMap &conformanceMap,
197+
SmallVectorImpl<Substitution> &result) const;
183198

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

196211
/// Canonicalize the components of a generic signature.
197212
CanGenericSignature getCanonicalSignature() const;
198-
199-
/// Canonicalize a generic signature down to its essential requirements,
200-
/// for mangling purposes.
201-
///
202-
/// TODO: This is what getCanonicalSignature() ought to do, but currently
203-
/// does not due to former implementation dependencies on
204-
/// 'getAllDependentTypes' order matching 'getAllArchetypes' order of a
205-
/// generic param list. Now that 'getAllArchetypes' is gone, we might
206-
/// be able to move forward here.
207-
CanGenericSignature getCanonicalManglingSignature(ModuleDecl &M) const;
208213

209214
/// Uniquing for the ASTContext.
210215
void Profile(llvm::FoldingSetNodeID &ID) {

include/swift/AST/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ class SourceFile final : public FileUnit {
952952
SourceLoc diagLoc = {});
953953
/// @}
954954

955-
ReferencedNameTracker *getReferencedNameTracker() {
955+
ReferencedNameTracker *getReferencedNameTracker() const {
956956
return ReferencedNames;
957957
}
958958
void setReferencedNameTracker(ReferencedNameTracker *Tracker) {

include/swift/AST/ProtocolConformance.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
296296
/// applies to the substituted type.
297297
ProtocolConformance *subst(ModuleDecl *module,
298298
Type substType,
299-
ArrayRef<Substitution> subs,
300299
TypeSubstitutionMap &subMap,
301300
ArchetypeConformanceMap &conformanceMap);
302301
};

include/swift/AST/Requirement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class Requirement {
7474
Type getSecondType() const {
7575
return SecondType;
7676
}
77+
78+
void dump() const;
7779
};
7880

7981
} // end namespace swift

include/swift/AST/Substitution.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ class Substitution {
7171
GenericEnvironment *env,
7272
ArrayRef<Substitution> subs) const;
7373

74-
private:
75-
friend class ProtocolConformance;
76-
7774
Substitution subst(ModuleDecl *module,
78-
ArrayRef<Substitution> subs,
7975
TypeSubstitutionMap &subMap,
8076
ArchetypeConformanceMap &conformanceMap) const;
77+
78+
private:
79+
friend class ProtocolConformance;
8180
};
8281

8382
void dump(const ArrayRef<Substitution> &subs);

include/swift/AST/Types.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,19 @@ class alignas(1 << TypeAlignInBits) TypeBase {
636636
/// a type's subclass.
637637
bool isExactSuperclassOf(Type ty, LazyResolver *resolver);
638638

639+
/// \brief Get the substituted base class type, starting from a base class
640+
/// declaration and a substituted derived class type.
641+
///
642+
/// For example, given the following declarations:
643+
///
644+
/// class A<T, U> {}
645+
/// class B<V> : A<Int, V> {}
646+
/// class C<X, Y> : B<Y> {}
647+
///
648+
/// Calling `C<String, NSObject>`->getSuperclassForDecl(`A`) will return
649+
/// `A<Int, NSObject>`.
650+
Type getSuperclassForDecl(const ClassDecl *classDecl, LazyResolver *resolver);
651+
639652
/// \brief True if this type is the superclass of another type, or a generic
640653
/// type that could be bound to the superclass.
641654
///

include/swift/SIL/SILModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class SILModule {
503503
lookUpWitnessTable(const ProtocolConformance *C, bool deserializeLazily=true);
504504

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

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#ifndef SIL_TypeLowering_h
1414
#define SIL_TypeLowering_h
1515

16-
#include "swift/AST/ArchetypeBuilder.h"
1716
#include "swift/AST/CaptureInfo.h"
1817
#include "swift/ABI/MetadataValues.h"
1918
#include "swift/SIL/AbstractionPattern.h"

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ ArchetypeBuilder *ASTContext::getOrCreateArchetypeBuilder(
14501450

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

14561456
// Store this archetype builder.
@@ -1459,15 +1459,6 @@ ArchetypeBuilder *ASTContext::getOrCreateArchetypeBuilder(
14591459
return builder;
14601460
}
14611461

1462-
void ASTContext::setArchetypeBuilder(CanGenericSignature sig,
1463-
ModuleDecl *mod,
1464-
std::unique_ptr<ArchetypeBuilder> builder) {
1465-
if (Impl.ArchetypeBuilders.find({sig, mod})
1466-
== Impl.ArchetypeBuilders.end()) {
1467-
Impl.ArchetypeBuilders[{sig, mod}] = move(builder);
1468-
}
1469-
}
1470-
14711462
Module *
14721463
ASTContext::getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) {
14731464
assert(!ModulePath.empty());
@@ -3709,6 +3700,11 @@ GenericSignature *GenericSignature::get(ArrayRef<GenericTypeParamType *> params,
37093700
bool isKnownCanonical) {
37103701
assert(!params.empty());
37113702

3703+
#ifndef NDEBUG
3704+
for (auto req : requirements)
3705+
assert(req.getFirstType()->isTypeParameter());
3706+
#endif
3707+
37123708
// Check for an existing generic signature.
37133709
llvm::FoldingSetNodeID ID;
37143710
GenericSignature::Profile(ID, params, requirements);

lib/AST/ASTDumper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTPrinter.h"
2020
#include "swift/AST/ASTVisitor.h"
2121
#include "swift/AST/ForeignErrorConvention.h"
22+
#include "swift/AST/GenericEnvironment.h"
2223
#include "swift/AST/ParameterList.h"
2324
#include "swift/AST/TypeVisitor.h"
2425
#include "swift/Basic/STLExtras.h"
@@ -3092,3 +3093,11 @@ void TypeBase::dump(raw_ostream &os, unsigned indent) const {
30923093
llvm::SaveAndRestore<bool> X(ctx.LangOpts.DebugConstraintSolver, true);
30933094
Type(const_cast<TypeBase *>(this)).dump(os, indent);
30943095
}
3096+
3097+
void GenericEnvironment::dump() const {
3098+
llvm::errs() << "Generic environment:\n";
3099+
for (auto pair : getInterfaceToArchetypeMap()) {
3100+
pair.first->dump();
3101+
pair.second->dump();
3102+
}
3103+
}

lib/AST/ASTPrinter.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4383,6 +4383,27 @@ void GenericSignature::dump() const {
43834383
llvm::errs() << '\n';
43844384
}
43854385

4386+
void Requirement::dump() const {
4387+
switch (getKind()) {
4388+
case RequirementKind::WitnessMarker:
4389+
llvm::errs() << "witness_marker: ";
4390+
break;
4391+
case RequirementKind::Conformance:
4392+
llvm::errs() << "conforms_to: ";
4393+
break;
4394+
case RequirementKind::Superclass:
4395+
llvm::errs() << "superclass: ";
4396+
break;
4397+
case RequirementKind::SameType:
4398+
llvm::errs() << "same_type: ";
4399+
break;
4400+
}
4401+
4402+
if (getFirstType()) llvm::errs() << getFirstType() << " ";
4403+
if (getSecondType()) llvm::errs() << getSecondType();
4404+
llvm::errs() << "\n";
4405+
}
4406+
43864407
std::string GenericSignature::getAsString() const {
43874408
std::string result;
43884409
llvm::raw_string_ostream out(result);

0 commit comments

Comments
 (0)