Skip to content

ASTGen: An assortment of strides and improvements #67111

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 32 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
655e4c1
[NFC] ASTGen: Remove unused `SwiftParser` imports
AnthonyLatsis Jul 4, 2023
af6bfe9
[NFC] ASTGen: Move `visit` methods from "Misc.swift" to "ASTGen.swift"
AnthonyLatsis Aug 28, 2023
3e0c368
[NFC] ASTGen: Rename "Misc.swift" → "Bridge.swift"
AnthonyLatsis Aug 28, 2023
59c2623
[NFC] ASTGen: Extract `TokenSyntax` → `BridgedIdentifier` mapping int…
AnthonyLatsis May 26, 2023
92c1bdc
[NFC] ASTGen: Define `visit(_:)` overloads that take an optional to a…
AnthonyLatsis Aug 24, 2023
ea164cd
[NFC] ASTGen: Optimize and flatten the transform + `withBridgedArrayR…
AnthonyLatsis Jul 4, 2023
cd55d76
ASTGen: Skip leading trivia when forming bridged source locations
AnthonyLatsis Jun 15, 2023
e72b421
[NFC] ASTGen: Rename "Diagnostics.swift" → "DiagnosticsBridge.swift"
AnthonyLatsis Sep 7, 2023
4b2cded
ASTGen: Start diagnosing instances of an invalid syntax tree
AnthonyLatsis Aug 31, 2023
4b314a7
ASTGen: Fix enough source locations that we can start diffing AST dumps
AnthonyLatsis May 20, 2023
a141fba
[NFC] ASTGen: Don't unnecessarily bridge `TypeAliasDecl::setUnderlyin…
AnthonyLatsis May 26, 2023
1291333
ASTGen: Cache parsed `IterableDeclContext` members using the request …
AnthonyLatsis May 24, 2023
034a39a
ASTGen: Set brace locations for iterable declaration contexts
AnthonyLatsis May 20, 2023
808ab1b
[NFC] ASTGen: Extract `ParamDecl` and `ParameterList` translation int…
AnthonyLatsis Jun 1, 2023
6293332
ASTGen: Translate default arguments
AnthonyLatsis Jun 16, 2023
7b25bfc
ASTGen: Translate function generic parameter clauses
AnthonyLatsis May 22, 2023
a49bf19
ASTGen: Translate class generic parameter clauses
AnthonyLatsis May 20, 2023
345a7f8
ASTGen: Translate protocol declarations
AnthonyLatsis May 20, 2023
da427ad
ASTGen: Translate inheritance clauses
AnthonyLatsis May 21, 2023
b6be6da
ASTGen: Translate associated type declarations
AnthonyLatsis May 18, 2023
ca8f911
ASTGen: Translate generic where clauses
AnthonyLatsis May 30, 2023
b4b4a58
ASTGen: Translate actor declarations
AnthonyLatsis May 20, 2023
4a5325a
ASTGen: Translate enumeration declarations
AnthonyLatsis May 18, 2023
fa1c9fd
ASTGen: Translate enum case declarations
AnthonyLatsis May 19, 2023
76e2cdc
ASTGen: Translate enum element payloads
AnthonyLatsis May 19, 2023
de155c9
ASTGen: Translate extension declarations
AnthonyLatsis May 21, 2023
96f13f5
ASTGen: Translate operator declarations
AnthonyLatsis May 27, 2023
58ce5d1
ASTGen: Translate deinitializer declarations
AnthonyLatsis Jun 1, 2023
5e46243
ASTGen: Translate initializer declarations
AnthonyLatsis Jun 1, 2023
a8db87a
ASTGen: Translate import declarations
AnthonyLatsis Jun 2, 2023
a1ba72c
ASTGen: Translate precedence group declarations
AnthonyLatsis Jun 28, 2023
ae83632
[NFC] ASTGen: Add Swift names to bridging functions for decls
AnthonyLatsis Aug 8, 2023
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
7 changes: 5 additions & 2 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,14 @@ class ASTContext final {
getInheritedConformance(Type type, ProtocolConformance *inherited);

/// Get the lazy data for the given declaration.
LazyContextData *getLazyContextData(const Decl *decl) const;

/// Get or otherwise allocate the lazy data for the given declaration.
///
/// \param lazyLoader If non-null, the lazy loader to use when creating the
/// lazy data. The pointer must either be null or be consistent
/// across all calls for the same \p func.
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
/// across all calls for the same \p decl.
LazyContextData *getOrCreateLazyContextData(const Decl *decl,
LazyMemberLoader *lazyLoader);

/// Get the lazy iterable context for the given iterable declaration context.
Expand Down
314 changes: 253 additions & 61 deletions include/swift/AST/CASTBridging.h

Large diffs are not rendered by default.

41 changes: 30 additions & 11 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
IsOpaqueType : 1
);

SWIFT_INLINE_BITFIELD_FULL(AssociatedTypeDecl, TypeDecl, 1,
/// Whether we have computed the default type.
IsDefaultDefinitionTypeComputed : 1
);

SWIFT_INLINE_BITFIELD_EMPTY(GenericTypeDecl, TypeDecl);

SWIFT_INLINE_BITFIELD(TypeAliasDecl, GenericTypeDecl, 1+1,
Expand Down Expand Up @@ -3677,24 +3682,28 @@ class AssociatedTypeDecl : public TypeDecl {
SourceLoc KeywordLoc;

/// The default definition.
TypeRepr *DefaultDefinition;
TypeLoc DefaultDefinition;

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

LazyMemberLoader *Resolver = nullptr;
uint64_t ResolverContextData;

friend class DefaultDefinitionTypeRequest;

public:
AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc, Identifier name,
SourceLoc nameLoc, TypeRepr *defaultDefinition,
TrailingWhereClause *trailingWhere);
AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc, Identifier name,
SourceLoc nameLoc, TrailingWhereClause *trailingWhere,
LazyMemberLoader *definitionResolver,
uint64_t resolverData);

public:
static AssociatedTypeDecl *createParsed(ASTContext &ctx, DeclContext *dc,
SourceLoc keywordLoc, Identifier name,
SourceLoc nameLoc,
TypeRepr *defaultDefinition,
TrailingWhereClause *trailingWhere);

static AssociatedTypeDecl *createDeserialized(
ASTContext &ctx, DeclContext *dc, SourceLoc keywordLoc, Identifier name,
SourceLoc nameLoc, TrailingWhereClause *trailingWhere,
LazyMemberLoader *lazyLoader, uint64_t defaultDefinitionTypeData);
Comment on lines +3697 to +3706
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


/// Get the protocol in which this associated type is declared.
ProtocolDecl *getProtocol() const {
Expand All @@ -3705,15 +3714,25 @@ class AssociatedTypeDecl : public TypeDecl {
bool hasDefaultDefinitionType() const {
// If we have a TypeRepr, return true immediately without kicking off
// a request.
return DefaultDefinition || getDefaultDefinitionType();
return DefaultDefinition.getTypeRepr() || getDefaultDefinitionType();
}

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

/// Retrieve the default definition type if computed, `None` otherwise.
///
/// \Note Should only be used for dumping.
llvm::Optional<Type> getCachedDefaultDefinitionType() const;

private:
/// Set the computed default definition type.
void setDefaultDefinitionType(Type ty);

public:
/// Retrieve the default definition as written in the source.
TypeRepr *getDefaultDefinitionTypeRepr() const {
return DefaultDefinition;
return DefaultDefinition.getTypeRepr();
}

/// Retrieve the trailing where clause for this associated type, if any.
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/DefaultArgumentKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ enum class DefaultArgumentKind : uint8_t {
};
enum { NumDefaultArgumentKindBits = 4 };

/// Determine the kind of a default argument given a parsed expression that has
/// not yet been type-checked.
/// 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.
DefaultArgumentKind getDefaultArgKind(Expr *init);

struct ArgumentAttrs {
DefaultArgumentKind argumentKind;
bool isUnavailableInSwift = false;
Expand Down
6 changes: 6 additions & 0 deletions include/swift/AST/LazyResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class LazyIterableDeclContextData : public LazyContextData {
uint64_t allConformancesData = 0;
};

class LazyAssociatedTypeData : public LazyContextData {
public:
/// The context data used for loading the default type.
uint64_t defaultDefinitionTypeData = 0;
};

/// Context data for protocols.
class LazyProtocolData : public LazyIterableDeclContextData {
public:
Expand Down
10 changes: 6 additions & 4 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,10 @@ class RequirementSignatureRequest :
};

/// Compute the default definition type of an associated type.
class DefaultDefinitionTypeRequest :
public SimpleRequest<DefaultDefinitionTypeRequest,
Type(AssociatedTypeDecl *),
RequestFlags::Cached> {
class DefaultDefinitionTypeRequest
: public SimpleRequest<DefaultDefinitionTypeRequest,
Type(AssociatedTypeDecl *),
RequestFlags::SeparatelyCached> {
public:
using SimpleRequest::SimpleRequest;

Expand All @@ -544,6 +544,8 @@ class DefaultDefinitionTypeRequest :
public:
// Caching.
bool isCached() const { return true; }
llvm::Optional<Type> getCachedResult() const;
void cacheResult(Type value) const;
};

/// Describes the owner of a where clause, from which we can extract
Expand Down
29 changes: 18 additions & 11 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ struct ASTContext::Implementation {
DelayedMissingWitnesses;

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

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

LazyContextData *ASTContext::getOrCreateLazyContextData(
const DeclContext *dc,
LazyMemberLoader *lazyLoader) {
LazyContextData *&entry = getImpl().LazyContexts[dc];
if (entry) {
LazyContextData *ASTContext::getLazyContextData(const Decl *decl) const {
return getImpl().LazyContexts.lookup(decl);
}

LazyContextData *
ASTContext::getOrCreateLazyContextData(const Decl *decl,
LazyMemberLoader *lazyLoader) {
if (auto *data = getLazyContextData(decl)) {
// Make sure we didn't provide an incompatible lazy loader.
assert(!lazyLoader || lazyLoader == entry->loader);
return entry;
assert(!lazyLoader || lazyLoader == data->loader);
return data;
}

LazyContextData *&entry = getImpl().LazyContexts[decl];

// Create new lazy context data with the given loader.
assert(lazyLoader && "Queried lazy data for non-lazy iterable context");
if (isa<ProtocolDecl>(dc))
if (isa<ProtocolDecl>(decl)) {
entry = Allocate<LazyProtocolData>();
else {
assert(isa<NominalTypeDecl>(dc) || isa<ExtensionDecl>(dc));
} else if (isa<NominalTypeDecl>(decl) || isa<ExtensionDecl>(decl)) {
entry = Allocate<LazyIterableDeclContextData>();
} else {
assert(isa<AssociatedTypeDecl>(decl));
entry = Allocate<LazyAssociatedTypeData>();
}

entry->loader = lazyLoader;
Expand Down
9 changes: 7 additions & 2 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,14 @@ namespace {

void visitAssociatedTypeDecl(AssociatedTypeDecl *decl, StringRef label) {
printCommon(decl, "associated_type_decl", label);
if (auto defaultDef = decl->getDefaultDefinitionType()) {
printFieldQuoted(defaultDef, "default");

StringRef fieldName("default");
if (auto defaultDef = decl->getCachedDefaultDefinitionType()) {
printFieldQuoted(*defaultDef, fieldName);
} else {
printField("<not computed>", fieldName);
}

printWhereRequirements(decl);
if (decl->overriddenDeclsComputed()) {
printFieldQuotedRaw([&](raw_ostream &OS) {
Expand Down
Loading