Skip to content

Commit fa12d85

Browse files
committed
AST: Clean up associated type default representation a bit
1 parent a08c563 commit fa12d85

File tree

11 files changed

+33
-24
lines changed

11 files changed

+33
-24
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,14 +3058,22 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
30583058
return cast<ProtocolDecl>(getDeclContext());
30593059
}
30603060

3061+
/// Check if we have a default definition type.
3062+
bool hasDefaultDefinitionType() const {
3063+
// If we have a TypeRepr, return true immediately without kicking off
3064+
// a request.
3065+
return !DefaultDefinition.isNull() || getDefaultDefinitionType();
3066+
}
3067+
30613068
/// Retrieve the default definition type.
3062-
Type getDefaultDefinitionType() const {
3063-
return getDefaultDefinitionLoc().getType();
3069+
Type getDefaultDefinitionType() const;
3070+
3071+
TypeLoc &getDefaultDefinitionLoc() {
3072+
return DefaultDefinition;
30643073
}
30653074

3066-
TypeLoc &getDefaultDefinitionLoc();
30673075
const TypeLoc &getDefaultDefinitionLoc() const {
3068-
return const_cast<AssociatedTypeDecl *>(this)->getDefaultDefinitionLoc();
3076+
return DefaultDefinition;
30693077
}
30703078

30713079
/// Retrieve the trailing where clause for this associated type, if any.
@@ -3076,7 +3084,7 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
30763084
TrailingWhere = trailingWhereClause;
30773085
}
30783086

3079-
/// Set the interface type of this associated type declaration to a dependen
3087+
/// Set the interface type of this associated type declaration to a dependent
30803088
/// member type of 'Self'.
30813089
void computeType();
30823090

include/swift/AST/LazyResolver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class alignas(void*) LazyMemberLoader {
159159
SmallVectorImpl<ProtocolConformance *> &Conformances) = 0;
160160

161161
/// Returns the default definition type for \p ATD.
162-
virtual TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
163-
uint64_t contextData) = 0;
162+
virtual Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
163+
uint64_t contextData) = 0;
164164

165165
/// Returns the generic environment.
166166
virtual GenericEnvironment *loadGenericEnvironment(const DeclContext *decl,

include/swift/Serialization/ModuleFile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,8 @@ class ModuleFile
827827
loadAllConformances(const Decl *D, uint64_t contextData,
828828
SmallVectorImpl<ProtocolConformance*> &Conforms) override;
829829

830-
virtual TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
831-
uint64_t contextData) override;
830+
virtual Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
831+
uint64_t contextData) override;
832832

833833
virtual void finishNormalConformance(NormalProtocolConformance *conformance,
834834
uint64_t contextData) override;

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,9 +2321,9 @@ void PrintAST::visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
23212321
printInherited(decl);
23222322
}
23232323

2324-
if (!decl->getDefaultDefinitionLoc().isNull()) {
2324+
if (decl->hasDefaultDefinitionType()) {
23252325
Printer << " = ";
2326-
decl->getDefaultDefinitionLoc().getType().print(Printer, Options);
2326+
decl->getDefaultDefinitionType().print(Printer, Options);
23272327
}
23282328

23292329
// As with protocol's trailing where clauses, use the requirement signature

lib/AST/Decl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,13 +3526,14 @@ void AssociatedTypeDecl::computeType() {
35263526
setInterfaceType(MetatypeType::get(interfaceTy, ctx));
35273527
}
35283528

3529-
TypeLoc &AssociatedTypeDecl::getDefaultDefinitionLoc() {
3529+
Type AssociatedTypeDecl::getDefaultDefinitionType() const {
35303530
if (Resolver) {
3531-
DefaultDefinition =
3532-
Resolver->loadAssociatedTypeDefault(this, ResolverContextData);
3533-
Resolver = nullptr;
3531+
const_cast<AssociatedTypeDecl *>(this)->DefaultDefinition
3532+
= TypeLoc::withoutLoc(
3533+
Resolver->loadAssociatedTypeDefault(this, ResolverContextData));
3534+
const_cast<AssociatedTypeDecl *>(this)->Resolver = nullptr;
35343535
}
3535-
return DefaultDefinition;
3536+
return DefaultDefinition.getType();
35363537
}
35373538

35383539
SourceRange AssociatedTypeDecl::getSourceRange() const {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4282,7 +4282,7 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42824282
source->kind == RequirementSource::RequirementSignatureSelf &&
42834283
!assocTypeDecl->getAttrs().hasAttribute<NonOverrideAttr>() &&
42844284
!assocTypeDecl->getAttrs().hasAttribute<OverrideAttr>() &&
4285-
assocTypeDecl->getDefaultDefinitionLoc().isNull() &&
4285+
!assocTypeDecl->hasDefaultDefinitionType() &&
42864286
(!assocTypeDecl->getInherited().empty() ||
42874287
assocTypeDecl->getTrailingWhereClause() ||
42884288
getASTContext().LangOpts.WarnImplicitOverrides);

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
12611261
uint64_t unused) override;
12621262

12631263
/// Returns the default definition type for \p ATD.
1264-
TypeLoc loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
1265-
uint64_t contextData) override {
1264+
Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
1265+
uint64_t contextData) override {
12661266
llvm_unreachable("unimplemented for ClangImporter");
12671267
}
12681268

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43604360
continue;
43614361
// FIXME: Also exclude the type alias that has already been specified.
43624362
if (!Conformance->hasTypeWitness(ATD) ||
4363-
!ATD->getDefaultDefinitionLoc().isNull())
4363+
ATD->hasDefaultDefinitionType())
43644364
continue;
43654365
addTypeAlias(ATD,
43664366
DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,
26162616
}
26172617
}
26182618
if (auto MissingTypeWitness = dyn_cast<AssociatedTypeDecl>(Requirement)) {
2619-
if (!MissingTypeWitness->getDefaultDefinitionLoc().isNull()) {
2619+
if (MissingTypeWitness->hasDefaultDefinitionType()) {
26202620
// For type witnesses with default definitions, we don't print the stub.
26212621
return false;
26222622
}

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ AssociatedTypeDecl *AssociatedTypeInference::findDefaultedAssociatedType(
769769
AssociatedTypeDecl *assocType) {
770770
// If this associated type has a default, we're done.
771771
tc.validateDecl(assocType);
772-
if (!assocType->getDefaultDefinitionLoc().isNull())
772+
if (assocType->hasDefaultDefinitionType())
773773
return assocType;
774774

775775
// Look at overridden associated types.

0 commit comments

Comments
 (0)