Skip to content

Commit 44a4d69

Browse files
authored
Merge pull request #67111 from AnthonyLatsis/astgen-decls
ASTGen: An assortment of strides and improvements
2 parents e2eaad5 + ae83632 commit 44a4d69

32 files changed

+2871
-1224
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,14 @@ class ASTContext final {
13471347
getInheritedConformance(Type type, ProtocolConformance *inherited);
13481348

13491349
/// Get the lazy data for the given declaration.
1350+
LazyContextData *getLazyContextData(const Decl *decl) const;
1351+
1352+
/// Get or otherwise allocate the lazy data for the given declaration.
13501353
///
13511354
/// \param lazyLoader If non-null, the lazy loader to use when creating the
13521355
/// lazy data. The pointer must either be null or be consistent
1353-
/// across all calls for the same \p func.
1354-
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
1356+
/// across all calls for the same \p decl.
1357+
LazyContextData *getOrCreateLazyContextData(const Decl *decl,
13551358
LazyMemberLoader *lazyLoader);
13561359

13571360
/// Get the lazy iterable context for the given iterable declaration context.

include/swift/AST/CASTBridging.h

Lines changed: 253 additions & 61 deletions
Large diffs are not rendered by default.

include/swift/AST/Decl.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
548548
IsOpaqueType : 1
549549
);
550550

551+
SWIFT_INLINE_BITFIELD_FULL(AssociatedTypeDecl, TypeDecl, 1,
552+
/// Whether we have computed the default type.
553+
IsDefaultDefinitionTypeComputed : 1
554+
);
555+
551556
SWIFT_INLINE_BITFIELD_EMPTY(GenericTypeDecl, TypeDecl);
552557

553558
SWIFT_INLINE_BITFIELD(TypeAliasDecl, GenericTypeDecl, 1+1,
@@ -3692,24 +3697,28 @@ class AssociatedTypeDecl : public TypeDecl {
36923697
SourceLoc KeywordLoc;
36933698

36943699
/// The default definition.
3695-
TypeRepr *DefaultDefinition;
3700+
TypeLoc DefaultDefinition;
36963701

36973702
/// The where clause attached to the associated type.
36983703
TrailingWhereClause *TrailingWhere;
36993704

3700-
LazyMemberLoader *Resolver = nullptr;
3701-
uint64_t ResolverContextData;
3702-
37033705
friend class DefaultDefinitionTypeRequest;
37043706

3705-
public:
37063707
AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc, Identifier name,
37073708
SourceLoc nameLoc, TypeRepr *defaultDefinition,
37083709
TrailingWhereClause *trailingWhere);
3709-
AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc, Identifier name,
3710-
SourceLoc nameLoc, TrailingWhereClause *trailingWhere,
3711-
LazyMemberLoader *definitionResolver,
3712-
uint64_t resolverData);
3710+
3711+
public:
3712+
static AssociatedTypeDecl *createParsed(ASTContext &ctx, DeclContext *dc,
3713+
SourceLoc keywordLoc, Identifier name,
3714+
SourceLoc nameLoc,
3715+
TypeRepr *defaultDefinition,
3716+
TrailingWhereClause *trailingWhere);
3717+
3718+
static AssociatedTypeDecl *createDeserialized(
3719+
ASTContext &ctx, DeclContext *dc, SourceLoc keywordLoc, Identifier name,
3720+
SourceLoc nameLoc, TrailingWhereClause *trailingWhere,
3721+
LazyMemberLoader *lazyLoader, uint64_t defaultDefinitionTypeData);
37133722

37143723
/// Get the protocol in which this associated type is declared.
37153724
ProtocolDecl *getProtocol() const {
@@ -3720,15 +3729,25 @@ class AssociatedTypeDecl : public TypeDecl {
37203729
bool hasDefaultDefinitionType() const {
37213730
// If we have a TypeRepr, return true immediately without kicking off
37223731
// a request.
3723-
return DefaultDefinition || getDefaultDefinitionType();
3732+
return DefaultDefinition.getTypeRepr() || getDefaultDefinitionType();
37243733
}
37253734

37263735
/// Retrieve the default definition type.
37273736
Type getDefaultDefinitionType() const;
37283737

3738+
/// Retrieve the default definition type if computed, `None` otherwise.
3739+
///
3740+
/// \Note Should only be used for dumping.
3741+
llvm::Optional<Type> getCachedDefaultDefinitionType() const;
3742+
3743+
private:
3744+
/// Set the computed default definition type.
3745+
void setDefaultDefinitionType(Type ty);
3746+
3747+
public:
37293748
/// Retrieve the default definition as written in the source.
37303749
TypeRepr *getDefaultDefinitionTypeRepr() const {
3731-
return DefaultDefinition;
3750+
return DefaultDefinition.getTypeRepr();
37323751
}
37333752

37343753
/// Retrieve the trailing where clause for this associated type, if any.

include/swift/AST/DefaultArgumentKind.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ enum class DefaultArgumentKind : uint8_t {
5454
};
5555
enum { NumDefaultArgumentKindBits = 4 };
5656

57+
/// Determine the kind of a default argument given a parsed expression that has
58+
/// not yet been type-checked.
59+
/// FIXME: Requestify/internalize the computation of the default arg expr and its kind (given a parsed expr) once the old parser no longer needs this.
60+
DefaultArgumentKind getDefaultArgKind(Expr *init);
61+
5762
struct ArgumentAttrs {
5863
DefaultArgumentKind argumentKind;
5964
bool isUnavailableInSwift = false;

include/swift/AST/LazyResolver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ class LazyIterableDeclContextData : public LazyContextData {
5757
uint64_t allConformancesData = 0;
5858
};
5959

60+
class LazyAssociatedTypeData : public LazyContextData {
61+
public:
62+
/// The context data used for loading the default type.
63+
uint64_t defaultDefinitionTypeData = 0;
64+
};
65+
6066
/// Context data for protocols.
6167
class LazyProtocolData : public LazyIterableDeclContextData {
6268
public:

include/swift/AST/TypeCheckRequests.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ class RequirementSignatureRequest :
528528
};
529529

530530
/// Compute the default definition type of an associated type.
531-
class DefaultDefinitionTypeRequest :
532-
public SimpleRequest<DefaultDefinitionTypeRequest,
533-
Type(AssociatedTypeDecl *),
534-
RequestFlags::Cached> {
531+
class DefaultDefinitionTypeRequest
532+
: public SimpleRequest<DefaultDefinitionTypeRequest,
533+
Type(AssociatedTypeDecl *),
534+
RequestFlags::SeparatelyCached> {
535535
public:
536536
using SimpleRequest::SimpleRequest;
537537

@@ -544,6 +544,8 @@ class DefaultDefinitionTypeRequest :
544544
public:
545545
// Caching.
546546
bool isCached() const { return true; }
547+
llvm::Optional<Type> getCachedResult() const;
548+
void cacheResult(Type value) const;
547549
};
548550

549551
/// Describes the owner of a where clause, from which we can extract

lib/AST/ASTContext.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ struct ASTContext::Implementation {
341341
DelayedMissingWitnesses;
342342

343343
/// Stores information about lazy deserialization of various declarations.
344-
llvm::DenseMap<const DeclContext *, LazyContextData *> LazyContexts;
344+
llvm::DenseMap<const Decl *, LazyContextData *> LazyContexts;
345345

346346
/// A fake generic parameter list <Self> for parsing @opened archetypes
347347
/// in textual SIL.
@@ -2829,23 +2829,30 @@ PackConformance *PackConformance::get(PackType *conformingType,
28292829
return result;
28302830
}
28312831

2832-
LazyContextData *ASTContext::getOrCreateLazyContextData(
2833-
const DeclContext *dc,
2834-
LazyMemberLoader *lazyLoader) {
2835-
LazyContextData *&entry = getImpl().LazyContexts[dc];
2836-
if (entry) {
2832+
LazyContextData *ASTContext::getLazyContextData(const Decl *decl) const {
2833+
return getImpl().LazyContexts.lookup(decl);
2834+
}
2835+
2836+
LazyContextData *
2837+
ASTContext::getOrCreateLazyContextData(const Decl *decl,
2838+
LazyMemberLoader *lazyLoader) {
2839+
if (auto *data = getLazyContextData(decl)) {
28372840
// Make sure we didn't provide an incompatible lazy loader.
2838-
assert(!lazyLoader || lazyLoader == entry->loader);
2839-
return entry;
2841+
assert(!lazyLoader || lazyLoader == data->loader);
2842+
return data;
28402843
}
28412844

2845+
LazyContextData *&entry = getImpl().LazyContexts[decl];
2846+
28422847
// Create new lazy context data with the given loader.
28432848
assert(lazyLoader && "Queried lazy data for non-lazy iterable context");
2844-
if (isa<ProtocolDecl>(dc))
2849+
if (isa<ProtocolDecl>(decl)) {
28452850
entry = Allocate<LazyProtocolData>();
2846-
else {
2847-
assert(isa<NominalTypeDecl>(dc) || isa<ExtensionDecl>(dc));
2851+
} else if (isa<NominalTypeDecl>(decl) || isa<ExtensionDecl>(decl)) {
28482852
entry = Allocate<LazyIterableDeclContextData>();
2853+
} else {
2854+
assert(isa<AssociatedTypeDecl>(decl));
2855+
entry = Allocate<LazyAssociatedTypeData>();
28492856
}
28502857

28512858
entry->loader = lazyLoader;

lib/AST/ASTDumper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,9 +1110,14 @@ namespace {
11101110

11111111
void visitAssociatedTypeDecl(AssociatedTypeDecl *decl, StringRef label) {
11121112
printCommon(decl, "associated_type_decl", label);
1113-
if (auto defaultDef = decl->getDefaultDefinitionType()) {
1114-
printFieldQuoted(defaultDef, "default");
1113+
1114+
StringRef fieldName("default");
1115+
if (auto defaultDef = decl->getCachedDefaultDefinitionType()) {
1116+
printFieldQuoted(*defaultDef, fieldName);
1117+
} else {
1118+
printField("<not computed>", fieldName);
11151119
}
1120+
11161121
printWhereRequirements(decl);
11171122
if (decl->overriddenDeclsComputed()) {
11181123
printFieldQuotedRaw([&](raw_ostream &OS) {

0 commit comments

Comments
 (0)