Skip to content

Commit 63104c2

Browse files
Merge pull request #5399 from swiftwasm/main
[pull] swiftwasm from main
2 parents d435fc5 + e4a0b9e commit 63104c2

37 files changed

+464
-314
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ enum class MacroIntroducedDeclNameKind {
9797
Prefixed,
9898
Suffixed,
9999
Arbitrary,
100+
101+
// NOTE: When adding a new name kind, also add it to
102+
// `getAllMacroIntroducedDeclNameKinds`.
100103
};
101104

105+
/// Returns an enumeratable list of all macro introduced decl name kinds.
106+
std::vector<MacroIntroducedDeclNameKind> getAllMacroIntroducedDeclNameKinds();
107+
102108
/// Whether a macro-introduced name of this kind requires an argument.
103109
bool macroIntroducedNameRequiresArgument(MacroIntroducedDeclNameKind kind);
104110

include/swift/IDE/CodeCompletionResult.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ class ContextFreeCodeCompletionResult {
549549
return NotRecommended;
550550
}
551551

552+
ContextualNotRecommendedReason calculateContextualNotRecommendedReason(
553+
ContextualNotRecommendedReason explicitReason,
554+
bool canCurrDeclContextHandleAsync) const;
555+
556+
CodeCompletionResultTypeRelation calculateContextualTypeRelation(
557+
const DeclContext *dc, const ExpectedTypeContext *typeContext,
558+
const USRBasedTypeContext *usrTypeContext) const;
559+
552560
CodeCompletionDiagnosticSeverity getDiagnosticSeverity() const {
553561
return DiagnosticSeverity;
554562
}
@@ -611,6 +619,7 @@ class CodeCompletionResult {
611619
CodeCompletionResultTypeRelation TypeDistance : 3;
612620
static_assert(int(CodeCompletionResultTypeRelation::MAX_VALUE) < 1 << 3, "");
613621

622+
public:
614623
/// Memberwise initializer
615624
/// The \c ContextFree result must outlive this result. Typically, this is
616625
/// done by allocating the two in the same sink or adopting the context free
@@ -624,25 +633,6 @@ class CodeCompletionResult {
624633
Flair(Flair.toRaw()), NotRecommended(NotRecommended),
625634
NumBytesToErase(NumBytesToErase), TypeDistance(TypeDistance) {}
626635

627-
public:
628-
/// Enrich a \c ContextFreeCodeCompletionResult with the following contextual
629-
/// information.
630-
/// This computes the type relation between the completion item and its
631-
/// expected type context.
632-
/// See \c CodeCompletionResultType::calculateTypeRelation for documentation
633-
/// on \p USRTypeContext.
634-
/// The \c ContextFree result must outlive this result. Typically, this is
635-
/// done by allocating the two in the same sink or adopting the context free
636-
/// sink in the sink that allocates this result.
637-
CodeCompletionResult(const ContextFreeCodeCompletionResult &ContextFree,
638-
SemanticContextKind SemanticContext,
639-
CodeCompletionFlair Flair, uint8_t NumBytesToErase,
640-
const ExpectedTypeContext *TypeContext,
641-
const DeclContext *DC,
642-
const USRBasedTypeContext *USRTypeContext,
643-
bool CanCurrDeclContextHandleAsync,
644-
ContextualNotRecommendedReason NotRecommended);
645-
646636
const ContextFreeCodeCompletionResult &getContextFreeResult() const {
647637
return ContextFree;
648638
}

include/swift/IDE/CompletionLookup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
449449
CodeCompletionKeywordKind KeyKind = CodeCompletionKeywordKind::None,
450450
CodeCompletionFlair flair = {});
451451

452-
void addDeclAttrParamKeyword(StringRef Name, StringRef Annotation,
453-
bool NeedSpecify);
452+
void addDeclAttrParamKeyword(StringRef Name, ArrayRef<StringRef> Parameters,
453+
StringRef Annotation, bool NeedSpecify);
454454

455455
void addDeclAttrKeyword(StringRef Name, StringRef Annotation);
456456

@@ -586,7 +586,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
586586
void getAttributeDeclCompletions(bool IsInSil, Optional<DeclKind> DK);
587587

588588
void getAttributeDeclParamCompletions(CustomSyntaxAttributeKind AttrKind,
589-
int ParamIndex);
589+
int ParamIndex, bool HasLabel);
590590

591591
void getTypeAttributeKeywordCompletions();
592592

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ class CodeCompletionCallbacks {
194194

195195
/// Complete the parameters in attribute, for instance, version specifier for
196196
/// @available.
197-
virtual void completeDeclAttrParam(CustomSyntaxAttributeKind DK, int Index){};
197+
/// If `HasLabel` is `true`, then the argument already has a label specified,
198+
/// e.g. we're completing after `names: ` in a macro declaration.
199+
virtual void completeDeclAttrParam(CustomSyntaxAttributeKind DK, int Index,
200+
bool HasLabel){};
198201

199202
/// Complete 'async' and 'throws' at effects specifier position.
200203
virtual void completeEffectsSpecifier(bool hasAsync, bool hasThrows) {};

lib/AST/Decl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10149,6 +10149,17 @@ StringRef swift::getMacroRoleString(MacroRole role) {
1014910149
}
1015010150
}
1015110151

10152+
std::vector<MacroIntroducedDeclNameKind>
10153+
swift::getAllMacroIntroducedDeclNameKinds() {
10154+
return {
10155+
MacroIntroducedDeclNameKind::Named,
10156+
MacroIntroducedDeclNameKind::Overloaded,
10157+
MacroIntroducedDeclNameKind::Prefixed,
10158+
MacroIntroducedDeclNameKind::Suffixed,
10159+
MacroIntroducedDeclNameKind::Arbitrary,
10160+
};
10161+
}
10162+
1015210163
bool swift::macroIntroducedNameRequiresArgument(
1015310164
MacroIntroducedDeclNameKind kind
1015410165
) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6437,9 +6437,7 @@ static bool hasIteratorAPIAttr(const clang::Decl *decl) {
64376437
static bool hasPointerInSubobjects(const clang::CXXRecordDecl *decl) {
64386438
// Probably a class template that has not yet been specialized:
64396439
if (!decl->getDefinition())
6440-
// If the definition is unknown, there is no way to determine if the type
6441-
// stores pointers. Stay on the safe side and assume that it does.
6442-
return true;
6440+
return false;
64436441

64446442
auto checkType = [](clang::QualType t) {
64456443
if (t->isPointerType())
@@ -6734,10 +6732,9 @@ bool IsSafeUseOfCxxDecl::evaluate(Evaluator &evaluator,
67346732
return false;
67356733
}
67366734

6735+
// Mark this as safe to help our diganostics down the road.
67376736
if (!cxxRecordReturnType->getDefinition()) {
6738-
// This is a templated type that has not been instantiated yet. We do
6739-
// not know if it is safe. Assume that it isn't.
6740-
return false;
6737+
return true;
67416738
}
67426739

67436740
if (!cxxRecordReturnType->hasUserDeclaredCopyConstructor() &&

lib/IDE/CodeCompletion.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks,
127127
CodeCompletionCallbacks::PrecedenceGroupCompletionKind SyntxKind;
128128

129129
int AttrParamIndex;
130+
bool AttrParamHasLabel;
130131
bool IsInSil = false;
131132
bool HasSpace = false;
132133
bool ShouldCompleteCallPatternAfterParen = true;
@@ -270,7 +271,8 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks,
270271
void completeCaseStmtKeyword() override;
271272
void completeCaseStmtBeginning(CodeCompletionExpr *E) override;
272273
void completeDeclAttrBeginning(bool Sil, bool isIndependent) override;
273-
void completeDeclAttrParam(CustomSyntaxAttributeKind DK, int Index) override;
274+
void completeDeclAttrParam(CustomSyntaxAttributeKind DK, int Index,
275+
bool HasLabel) override;
274276
void completeEffectsSpecifier(bool hasAsync, bool hasThrows) override;
275277
void completeInPrecedenceGroup(
276278
CodeCompletionCallbacks::PrecedenceGroupCompletionKind SK) override;
@@ -457,10 +459,11 @@ void CodeCompletionCallbacksImpl::completeTypeSimpleBeginning() {
457459
}
458460

459461
void CodeCompletionCallbacksImpl::completeDeclAttrParam(
460-
CustomSyntaxAttributeKind DK, int Index) {
462+
CustomSyntaxAttributeKind DK, int Index, bool HasLabel) {
461463
Kind = CompletionKind::AttributeDeclParen;
462464
AttrKind = DK;
463465
AttrParamIndex = Index;
466+
AttrParamHasLabel = HasLabel;
464467
CurDeclContext = P.CurDeclContext;
465468
}
466469

@@ -1844,7 +1847,8 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
18441847
break;
18451848
}
18461849
case CompletionKind::AttributeDeclParen: {
1847-
Lookup.getAttributeDeclParamCompletions(AttrKind, AttrParamIndex);
1850+
Lookup.getAttributeDeclParamCompletions(AttrKind, AttrParamIndex,
1851+
AttrParamHasLabel);
18481852
break;
18491853
}
18501854
case CompletionKind::PoundAvailablePlatform: {

lib/IDE/CodeCompletionConsumer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ static MutableArrayRef<CodeCompletionResult *> copyCodeCompletionResults(
8080
if (!shouldIncludeResult(contextFreeResult)) {
8181
continue;
8282
}
83+
84+
CodeCompletionResultTypeRelation typeRelation =
85+
contextFreeResult->calculateContextualTypeRelation(DC, TypeContext,
86+
&USRTypeContext);
87+
ContextualNotRecommendedReason notRecommendedReason =
88+
contextFreeResult->calculateContextualNotRecommendedReason(
89+
ContextualNotRecommendedReason::None,
90+
CanCurrDeclContextHandleAsync);
91+
8392
auto contextualResult = new (*targetSink.Allocator) CodeCompletionResult(
8493
*contextFreeResult, SemanticContextKind::OtherModule,
8594
CodeCompletionFlair(),
86-
/*numBytesToErase=*/0, TypeContext, DC, &USRTypeContext,
87-
CanCurrDeclContextHandleAsync, ContextualNotRecommendedReason::None);
95+
/*numBytesToErase=*/0, typeRelation, notRecommendedReason);
8896
targetSink.Results.push_back(contextualResult);
8997
}
9098

lib/IDE/CodeCompletionResult.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -392,39 +392,41 @@ bool ContextFreeCodeCompletionResult::getDeclIsSystem(const Decl *D) {
392392
return D->getModuleContext()->isNonUserModule();
393393
}
394394

395-
// MARK: - CodeCompletionResult
396-
397-
static ContextualNotRecommendedReason
398-
getNotRecommenedReason(const ContextFreeCodeCompletionResult &ContextFree,
399-
bool CanCurrDeclContextHandleAsync,
400-
ContextualNotRecommendedReason ExplicitReason) {
401-
if (ExplicitReason != ContextualNotRecommendedReason::None) {
402-
return ExplicitReason;
403-
}
404-
if (ContextFree.isAsync() && !CanCurrDeclContextHandleAsync) {
395+
ContextualNotRecommendedReason
396+
ContextFreeCodeCompletionResult::calculateContextualNotRecommendedReason(
397+
ContextualNotRecommendedReason explicitReason,
398+
bool canCurrDeclContextHandleAsync) const {
399+
if (explicitReason != ContextualNotRecommendedReason::None) {
400+
return explicitReason;
401+
}
402+
if (IsAsync && !canCurrDeclContextHandleAsync) {
405403
return ContextualNotRecommendedReason::InvalidAsyncContext;
406404
}
407-
if (ContextFree.hasAsyncAlternative() && CanCurrDeclContextHandleAsync) {
405+
if (HasAsyncAlternative && canCurrDeclContextHandleAsync) {
408406
return ContextualNotRecommendedReason::
409407
NonAsyncAlternativeUsedInAsyncContext;
410408
}
411409
return ContextualNotRecommendedReason::None;
412410
}
413411

414-
CodeCompletionResult::CodeCompletionResult(
415-
const ContextFreeCodeCompletionResult &ContextFree,
416-
SemanticContextKind SemanticContext, CodeCompletionFlair Flair,
417-
uint8_t NumBytesToErase, const ExpectedTypeContext *TypeContext,
418-
const DeclContext *DC, const USRBasedTypeContext *USRTypeContext,
419-
bool CanCurrDeclContextHandleAsync,
420-
ContextualNotRecommendedReason NotRecommended)
421-
: ContextFree(ContextFree), SemanticContext(SemanticContext),
422-
Flair(Flair.toRaw()),
423-
NotRecommended(getNotRecommenedReason(
424-
ContextFree, CanCurrDeclContextHandleAsync, NotRecommended)),
425-
NumBytesToErase(NumBytesToErase),
426-
TypeDistance(ContextFree.getResultType().calculateTypeRelation(
427-
TypeContext, DC, USRTypeContext)) {}
412+
CodeCompletionResultTypeRelation
413+
ContextFreeCodeCompletionResult::calculateContextualTypeRelation(
414+
const DeclContext *dc, const ExpectedTypeContext *typeContext,
415+
const USRBasedTypeContext *usrTypeContext) const {
416+
CodeCompletionResultTypeRelation typeRelation =
417+
getResultType().calculateTypeRelation(typeContext, dc, usrTypeContext);
418+
if (typeRelation >= CodeCompletionResultTypeRelation::Convertible ||
419+
!typeContext)
420+
return typeRelation;
421+
422+
CodeCompletionMacroRoles expectedRoles =
423+
getCompletionMacroRoles(typeContext->getExpectedCustomAttributeKinds());
424+
if (MacroRoles & expectedRoles)
425+
return CodeCompletionResultTypeRelation::Convertible;
426+
return typeRelation;
427+
}
428+
429+
// MARK: - CodeCompletionResult
428430

429431
CodeCompletionResult *
430432
CodeCompletionResult::withFlair(CodeCompletionFlair NewFlair,

lib/IDE/CodeCompletionResultBuilder.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,27 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
162162
// we don't need to compute any contextual properties.
163163
return new (Allocator) CodeCompletionResult(
164164
*ContextFreeResult, SemanticContextKind::None, CodeCompletionFlair(),
165-
/*NumBytesToErase=*/0, /*TypeContext=*/nullptr,
166-
/*DC=*/nullptr, /*USRTypeContext=*/nullptr,
167-
/*CanCurrDeclContextHandleAsync=*/false,
165+
/*NumBytesToErase=*/0, CodeCompletionResultTypeRelation::Unrelated,
168166
ContextualNotRecommendedReason::None);
169167
} else {
170168
assert(
171169
ContextFreeResult != nullptr &&
172170
"ContextFreeResult should have been constructed by the switch above");
171+
173172
// We know that the ContextFreeResult has an AST-based type because it was
174173
// just computed and not read from the cache and
175174
// Sink.shouldProduceContextFreeResults() is false. So we can pass nullptr
176175
// for USRTypeContext.
176+
CodeCompletionResultTypeRelation typeRelation =
177+
ContextFreeResult->calculateContextualTypeRelation(
178+
DC, TypeContext, /*usrTypeContext=*/nullptr);
179+
ContextualNotRecommendedReason notRecommendedReason =
180+
ContextFreeResult->calculateContextualNotRecommendedReason(
181+
ContextualNotRecReason, CanCurrDeclContextHandleAsync);
182+
177183
return new (Allocator) CodeCompletionResult(
178184
*ContextFreeResult, SemanticContext, Flair, NumBytesToErase,
179-
TypeContext, DC, /*USRTypeContext=*/nullptr,
180-
CanCurrDeclContextHandleAsync, ContextualNotRecReason);
185+
typeRelation, notRecommendedReason);
181186
}
182187
}
183188

0 commit comments

Comments
 (0)