Skip to content

Commit 26e064d

Browse files
authored
Merge pull request swiftlang#63594 from DougGregor/sourcekit-attached-macros
2 parents 910ca17 + b882e0c commit 26e064d

23 files changed

+525
-135
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ArgumentList;
2424
class Decl;
2525
class Expr;
2626
class ClosureExpr;
27+
class CustomAttr;
2728
class ModuleDecl;
2829
class Stmt;
2930
class Pattern;
@@ -46,9 +47,17 @@ struct ReferenceMetaData {
4647
SemaReferenceKind Kind;
4748
llvm::Optional<AccessKind> AccKind;
4849
bool isImplicit = false;
49-
ReferenceMetaData(SemaReferenceKind Kind, llvm::Optional<AccessKind> AccKind,
50-
bool isImplicit = false)
51-
: Kind(Kind), AccKind(AccKind), isImplicit(isImplicit) {}
50+
51+
/// When non-none, this is a custom attribute reference.
52+
Optional<std::pair<const CustomAttr *, Decl *>> CustomAttrRef;
53+
54+
ReferenceMetaData(
55+
SemaReferenceKind Kind, llvm::Optional<AccessKind> AccKind,
56+
bool isImplicit = false,
57+
Optional<std::pair<const CustomAttr *, Decl *>> customAttrRef
58+
= None
59+
) : Kind(Kind), AccKind(AccKind), isImplicit(isImplicit),
60+
CustomAttrRef(customAttrRef) {}
5261
};
5362

5463
/// Specifies how the initialization expression of a \c lazy variable should be

include/swift/AST/TypeCheckRequests.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,36 +3825,60 @@ class ExpandMacroExpansionDeclRequest
38253825
bool isCached() const { return true; }
38263826
};
38273827

3828+
/// Expand all accessor macros attached to the given declaration.
3829+
///
3830+
/// Produces the set of macro expansion buffer IDs.
3831+
class ExpandAccessorMacros
3832+
: public SimpleRequest<ExpandAccessorMacros,
3833+
ArrayRef<unsigned>(AbstractStorageDecl *),
3834+
RequestFlags::Cached> {
3835+
public:
3836+
using SimpleRequest::SimpleRequest;
3837+
3838+
private:
3839+
friend SimpleRequest;
3840+
3841+
ArrayRef<unsigned> evaluate(
3842+
Evaluator &evaluator, AbstractStorageDecl *storage) const;
3843+
3844+
public:
3845+
bool isCached() const { return true; }
3846+
};
3847+
38283848
/// Expand all member attribute macros attached to the given
38293849
/// declaration.
3850+
///
3851+
/// Produces the set of macro expansion buffer IDs.
38303852
class ExpandMemberAttributeMacros
38313853
: public SimpleRequest<ExpandMemberAttributeMacros,
3832-
bool(Decl *),
3854+
ArrayRef<unsigned>(Decl *),
38333855
RequestFlags::Cached> {
38343856
public:
38353857
using SimpleRequest::SimpleRequest;
38363858

38373859
private:
38383860
friend SimpleRequest;
38393861

3840-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3862+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
38413863

38423864
public:
38433865
bool isCached() const { return true; }
38443866
};
38453867

38463868
/// Expand synthesized member macros attached to the given declaration.
3869+
///
3870+
/// Produces the set of macro expansion buffer IDs.
38473871
class ExpandSynthesizedMemberMacroRequest
38483872
: public SimpleRequest<ExpandSynthesizedMemberMacroRequest,
3849-
bool(Decl *),
3873+
ArrayRef<unsigned>(Decl *),
38503874
RequestFlags::Cached> {
38513875
public:
38523876
using SimpleRequest::SimpleRequest;
38533877

38543878
private:
38553879
friend SimpleRequest;
38563880

3857-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3881+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
38583882

38593883
public:
38603884
bool isCached() const { return true; }
@@ -3887,15 +3911,15 @@ class CompilerPluginLoadRequest
38873911
/// Expand peer macros attached to the given declaration.
38883912
class ExpandPeerMacroRequest
38893913
: public SimpleRequest<ExpandPeerMacroRequest,
3890-
bool(Decl *),
3914+
ArrayRef<unsigned>(Decl *),
38913915
RequestFlags::Cached> {
38923916
public:
38933917
using SimpleRequest::SimpleRequest;
38943918

38953919
private:
38963920
friend SimpleRequest;
38973921

3898-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3922+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
38993923

39003924
public:
39013925
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,13 @@ SWIFT_REQUEST(TypeChecker, ExpandMacroExpansionDeclRequest,
432432
ArrayRef<Decl *>(MacroExpansionDecl *),
433433
Cached, NoLocationInfo)
434434
SWIFT_REQUEST(TypeChecker, ExpandMemberAttributeMacros,
435-
bool(Decl *),
435+
ArrayRef<unsigned>(Decl *),
436436
Cached, NoLocationInfo)
437-
SWIFT_REQUEST(TypeCHecker, ExpandSynthesizedMemberMacroRequest,
438-
bool(Decl *),
437+
SWIFT_REQUEST(TypeChecker, ExpandAccessorMacros,
438+
ArrayRef<unsigned>(AbstractStorageDecl *),
439+
Cached, NoLocationInfo)
440+
SWIFT_REQUEST(TypeChecker, ExpandSynthesizedMemberMacroRequest,
441+
ArrayRef<unsigned>(Decl *),
439442
Cached, NoLocationInfo)
440443
SWIFT_REQUEST(TypeChecker, ExpandPeerMacroRequest,
441444
bool(Decl *),

include/swift/Basic/SourceManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ class GeneratedSourceInfo {
6363
/// which source code was generated. Conceptually, one can think of the
6464
/// buffer described by a \c GeneratedSource instance as replacing the
6565
/// code in the \c originalSourceRange.
66-
SourceRange originalSourceRange;
66+
CharSourceRange originalSourceRange;
6767

6868
/// The source range in the generated-source buffer where the generated
6969
/// code exists. This might be a subrange of the buffer containing the
7070
/// generated source, but it will never be from a different buffer.
71-
SourceRange generatedSourceRange;
71+
CharSourceRange generatedSourceRange;
7272

7373
/// The opaque pointer for an ASTNode for which this buffer was generated.
7474
void *astNode;

include/swift/IDE/Utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ struct ResolvedCursorInfo {
151151
bool IsRef = true;
152152
Type Ty;
153153
Type ContainerType;
154+
Optional<std::pair<const CustomAttr *, Decl *>> CustomAttrRef = None;
155+
154156
bool IsKeywordArgument = false;
155157
/// It this is a ref, whether it is "dynamic". See \c ide::isDynamicRef.
156158
bool IsDynamic = false;
@@ -262,6 +264,13 @@ struct ResolvedValueRefCursorInfo : public ResolvedCursorInfo {
262264
: ValueRefInfo.ValueD;
263265
}
264266

267+
Optional<std::pair<const CustomAttr *, Decl *>> getCustomAttrRef() const {
268+
return ValueRefInfo.CustomAttrRef;
269+
}
270+
void setCustomAttrRef(Optional<std::pair<const CustomAttr *, Decl *>> ref) {
271+
ValueRefInfo.CustomAttrRef = ref;
272+
}
273+
265274
static bool classof(const ResolvedCursorInfo *Info) {
266275
return Info->getKind() == CursorInfoKind::ValueRef;
267276
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,16 @@ ASTScopeImpl::findChildContaining(SourceLoc loc,
127127
// A map from enclosing source files to original source ranges of the macro
128128
// expansions within that file, recording the chain of macro expansions for
129129
// the given scope.
130-
llvm::SmallDenseMap<const SourceFile *, SourceRange> scopeExpansions;
130+
llvm::SmallDenseMap<const SourceFile *, CharSourceRange>
131+
scopeExpansions;
131132

132133
// Walk up the chain of macro expansion buffers for the scope, recording the
133134
// original source range of the macro expansion along the way using generated
134135
// source info.
135136
auto *scopeExpansion = scopeSourceFile;
136-
scopeExpansions[scopeExpansion] = scope->getSourceRangeOfThisASTNode();
137+
scopeExpansions[scopeExpansion] =
138+
Lexer::getCharSourceRangeFromSourceRange(
139+
sourceMgr, scope->getSourceRangeOfThisASTNode());
137140
while (auto *ancestor = scopeExpansion->getEnclosingSourceFile()) {
138141
auto generatedInfo =
139142
sourceMgr.getGeneratedSourceInfo(*scopeExpansion->getBufferID());
@@ -150,15 +153,14 @@ ASTScopeImpl::findChildContaining(SourceLoc loc,
150153
if (scopeExpansion != scopeExpansions.end()) {
151154
// Take the original expansion range within the LCA of the loc and
152155
// the scope to compare.
153-
rangeOfScope =
154-
Lexer::getCharSourceRangeFromSourceRange(sourceMgr, scopeExpansion->second);
156+
rangeOfScope = scopeExpansion->second;
155157
loc = expansionLoc;
156158
break;
157159
}
158160

159161
auto generatedInfo =
160162
sourceMgr.getGeneratedSourceInfo(*potentialLCA->getBufferID());
161-
expansionLoc = generatedInfo->originalSourceRange.Start;
163+
expansionLoc = generatedInfo->originalSourceRange.getStart();
162164
potentialLCA = potentialLCA->getEnclosingSourceFile();
163165
}
164166
}

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ void ASTScopeImpl::checkSourceRangeBeforeAddingChild(ASTScopeImpl *child,
7575
if (!generatedInfo)
7676
return false;
7777

78-
SourceRange expansionRange = generatedInfo->originalSourceRange;
78+
CharSourceRange expansionRange = generatedInfo->originalSourceRange;
7979
if (expansionRange.isInvalid())
8080
return false;
8181

82-
return containedInParent(
83-
Lexer::getCharSourceRangeFromSourceRange(sourceMgr, expansionRange));
82+
return containedInParent(expansionRange);
8483
};
8584

8685
auto childCharRange = child->getCharSourceRangeOfScope(sourceMgr);

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
369369

370370
DeclAttributes Decl::getSemanticAttrs() const {
371371
auto mutableThis = const_cast<Decl *>(this);
372-
evaluateOrDefault(getASTContext().evaluator,
373-
ExpandMemberAttributeMacros{mutableThis},
374-
false);
372+
(void)evaluateOrDefault(getASTContext().evaluator,
373+
ExpandMemberAttributeMacros{mutableThis},
374+
{ });
375375

376376
return getAttrs();
377377
}

lib/AST/DiagnosticEngine.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,11 +1241,8 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
12411241
bufferID,
12421242
GeneratedSourceInfo{
12431243
GeneratedSourceInfo::PrettyPrinted,
1244-
SourceRange(),
1245-
SourceRange(
1246-
memBufferStartLoc,
1247-
memBufferStartLoc.getAdvancedLoc(buffer.size())
1248-
),
1244+
CharSourceRange(),
1245+
CharSourceRange(memBufferStartLoc, buffer.size()),
12491246
ASTNode(const_cast<Decl *>(ppDecl)).getOpaqueValue(),
12501247
nullptr
12511248
}

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,9 +2082,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
20822082

20832083
// Expand synthesized member macros.
20842084
auto &ctx = current->getASTContext();
2085-
evaluateOrDefault(ctx.evaluator,
2086-
ExpandSynthesizedMemberMacroRequest{current},
2087-
false);
2085+
(void)evaluateOrDefault(ctx.evaluator,
2086+
ExpandSynthesizedMemberMacroRequest{current},
2087+
false);
20882088

20892089
// Look for results within the current nominal type and its extensions.
20902090
bool currentIsProtocol = isa<ProtocolDecl>(current);

0 commit comments

Comments
 (0)