@@ -776,6 +776,7 @@ struct DeclInfo {
776
776
Type ContainerType;
777
777
bool IsRef;
778
778
bool IsDynamic;
779
+ Expr* LitExpr;
779
780
ArrayRef<NominalTypeDecl *> ReceiverTypes;
780
781
781
782
// / If VD is a synthesized property wrapper backing storage (_foo) or
@@ -788,10 +789,10 @@ struct DeclInfo {
788
789
bool InSynthesizedExtension = false ;
789
790
790
791
DeclInfo (const ValueDecl *VD, Type ContainerType, bool IsRef, bool IsDynamic,
791
- ArrayRef<NominalTypeDecl *> ReceiverTypes,
792
+ Expr *LitExpr, ArrayRef<NominalTypeDecl *> ReceiverTypes,
792
793
const CompilerInvocation &Invoc)
793
794
: VD(VD), ContainerType(ContainerType), IsRef(IsRef),
794
- IsDynamic (IsDynamic), ReceiverTypes(ReceiverTypes) {
795
+ IsDynamic (IsDynamic), LitExpr(LitExpr), ReceiverTypes(ReceiverTypes) {
795
796
if (VD == nullptr )
796
797
return ;
797
798
@@ -944,12 +945,17 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
944
945
SmallString<256 > Buffer;
945
946
SmallVector<StringRef, 4 > Strings;
946
947
llvm::raw_svector_ostream OS (Buffer);
948
+ bool isLiteral = DInfo.LitExpr != nullptr ;
947
949
948
950
Symbol.DeclarationLang = SwiftLangSupport::getUIDForDeclLanguage (DInfo.VD );
949
- Symbol.Kind = SwiftLangSupport::getUIDForDecl (DInfo.VD , DInfo.IsRef );
950
951
951
- SwiftLangSupport::printDisplayName (DInfo.VD , OS);
952
- Symbol.Name = copyAndClearString (Allocator, Buffer);
952
+ if (isLiteral) {
953
+ Symbol.Kind = SwiftLangSupport::getUIDForLiteral (DInfo.LitExpr );
954
+ } else {
955
+ Symbol.Kind = SwiftLangSupport::getUIDForDecl (DInfo.VD , DInfo.IsRef );
956
+ SwiftLangSupport::printDisplayName (DInfo.VD , OS);
957
+ Symbol.Name = copyAndClearString (Allocator, Buffer);
958
+ }
953
959
954
960
SwiftLangSupport::printUSR (DInfo.OriginalProperty , OS);
955
961
if (DInfo.InSynthesizedExtension ) {
@@ -958,10 +964,17 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
958
964
}
959
965
Symbol.USR = copyAndClearString (Allocator, Buffer);
960
966
967
+ Type InterfaceType;
968
+ if (isLiteral) {
969
+ InterfaceType = DInfo.LitExpr ->getType ();
970
+ } else {
971
+ InterfaceType = DInfo.VD ->getInterfaceType ();
972
+ }
973
+
961
974
{
962
975
PrintOptions Options;
963
976
Options.PrintTypeAliasUnderlyingType = true ;
964
- DInfo. VD -> getInterfaceType () .print (OS, Options);
977
+ InterfaceType .print (OS, Options);
965
978
}
966
979
Symbol.TypeName = copyAndClearString (Allocator, Buffer);
967
980
@@ -970,7 +983,7 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
970
983
// But ParameterizedProtocolType can currently occur in 'typealias'
971
984
// declarations. rdar://99176683
972
985
// To avoid crashing in USR generation, return an error for now.
973
- if (auto Ty = DInfo. VD -> getInterfaceType () ) {
986
+ if (Type Ty = InterfaceType ) {
974
987
while (auto MetaTy = Ty->getAs <MetatypeType>()) {
975
988
Ty = MetaTy->getInstanceType ();
976
989
}
@@ -981,7 +994,11 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
981
994
}
982
995
}
983
996
984
- SwiftLangSupport::printDeclTypeUSR (DInfo.VD , OS);
997
+ if (isLiteral) {
998
+ SwiftLangSupport::printTypeUSR (DInfo.LitExpr ->getType (), OS);
999
+ } else {
1000
+ SwiftLangSupport::printDeclTypeUSR (DInfo.VD , OS);
1001
+ }
985
1002
Symbol.TypeUSR = copyAndClearString (Allocator, Buffer);
986
1003
987
1004
if (DInfo.ContainerType && !DInfo.ContainerType ->hasArchetype ()) {
@@ -1151,15 +1168,40 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
1151
1168
return llvm::Error::success ();
1152
1169
}
1153
1170
1171
+ static bool
1172
+ addCursorInfoForLiteral (CursorInfoData &Data, Expr *LitExpr,
1173
+ SwiftLangSupport &Lang,
1174
+ const CompilerInvocation &CompInvoc,
1175
+ SourceLoc CursorLoc,
1176
+ ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps) {
1177
+ Type LitType = LitExpr->getType ();
1178
+ if (!LitType || !LitType->getAnyNominal ()) {
1179
+ return false ;
1180
+ }
1181
+ TypeDecl *Decl = LitType->getAnyNominal ();
1182
+ auto &Symbol = Data.Symbols .emplace_back ();
1183
+ DeclInfo Info (Decl, nullptr , false , false , LitExpr, {}, CompInvoc);
1184
+ auto Err = fillSymbolInfo (Symbol, Info, CursorLoc, false , Lang, CompInvoc,
1185
+ PreviousSnaps, Data.Allocator );
1186
+ bool Success = true ;
1187
+ llvm::handleAllErrors (
1188
+ std::move (Err), [&](const llvm::StringError &E) {
1189
+ Data.InternalDiagnostic =
1190
+ copyCString (E.getMessage (), Data.Allocator );
1191
+ Success = false ;
1192
+ });
1193
+ return Success;
1194
+ }
1195
+
1154
1196
static bool
1155
1197
addCursorInfoForDecl (CursorInfoData &Data, ResolvedValueRefCursorInfoPtr Info,
1156
1198
bool AddRefactorings, bool AddSymbolGraph,
1157
1199
SwiftLangSupport &Lang, const CompilerInvocation &Invoc,
1158
1200
std::string &Diagnostic,
1159
1201
ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps) {
1160
1202
DeclInfo OrigInfo (Info->getValueD (), Info->getContainerType (), Info->isRef (),
1161
- Info->isDynamic (), Info->getReceiverTypes (), Invoc);
1162
- DeclInfo CtorTypeInfo (Info->getCtorTyRef (), Type (), true , false ,
1203
+ Info->isDynamic (), nullptr , Info->getReceiverTypes (), Invoc);
1204
+ DeclInfo CtorTypeInfo (Info->getCtorTyRef (), Type (), true , false , nullptr ,
1163
1205
ArrayRef<NominalTypeDecl *>(), Invoc);
1164
1206
DeclInfo &MainInfo = CtorTypeInfo.VD ? CtorTypeInfo : OrigInfo;
1165
1207
if (MainInfo.Unavailable ) {
@@ -1196,7 +1238,7 @@ addCursorInfoForDecl(CursorInfoData &Data, ResolvedValueRefCursorInfoPtr Info,
1196
1238
if (!Info->isRef ()) {
1197
1239
for (auto D : Info->getShorthandShadowedDecls ()) {
1198
1240
CursorSymbolInfo &SymbolInfo = Data.Symbols .emplace_back ();
1199
- DeclInfo DInfo (D, Type (), /* IsRef=*/ true , /* IsDynamic=*/ false ,
1241
+ DeclInfo DInfo (D, Type (), /* IsRef=*/ true , /* IsDynamic=*/ false , nullptr ,
1200
1242
ArrayRef<NominalTypeDecl *>(), Invoc);
1201
1243
if (auto Err =
1202
1244
fillSymbolInfo (SymbolInfo, DInfo, Info->getLoc (), AddSymbolGraph,
@@ -1630,21 +1672,54 @@ static void resolveCursor(
1630
1672
}
1631
1673
case CursorInfoKind::ExprStart:
1632
1674
case CursorInfoKind::StmtStart: {
1675
+ // If code under cursor is literal expression returns Expr,
1676
+ // otherwise nullptr
1677
+ auto tryGetLiteralExpr = [](auto CI) -> Expr * {
1678
+ auto *ExprInfo = dyn_cast<ResolvedExprStartCursorInfo>(CI);
1679
+ if (!ExprInfo || !ExprInfo->getTrailingExpr ()) {
1680
+ return nullptr ;
1681
+ }
1682
+ Expr* E = ExprInfo->getTrailingExpr ();
1683
+ if (dyn_cast<LiteralExpr>(E)) {
1684
+ return E;
1685
+ }
1686
+ switch (E->getKind ()) {
1687
+ case ExprKind::Array:
1688
+ case ExprKind::Dictionary:
1689
+ case ExprKind::KeyPath: {
1690
+ return E;
1691
+ }
1692
+ default :
1693
+ return nullptr ;
1694
+ }
1695
+ };
1696
+
1697
+ CursorInfoData Data;
1698
+
1633
1699
if (Actionables) {
1634
1700
addRefactorings (
1635
1701
Actions, collectRefactorings (CursorInfo, /* ExcludeRename=*/ true ));
1636
- if (!Actions.empty ()) {
1637
- CursorInfoData Data;
1638
- Data.AvailableActions = Actions;
1639
- Receiver (RequestResult<CursorInfoData>::fromResult (Data));
1640
- return ;
1702
+ Data.AvailableActions = Actions;
1703
+ }
1704
+
1705
+ // Handle literal expression
1706
+ if (auto *LitExpr = tryGetLiteralExpr (CursorInfo)) {
1707
+ bool Success = addCursorInfoForLiteral (Data, LitExpr, Lang, CompInvok,
1708
+ CursorInfo->getLoc (),
1709
+ getPreviousASTSnaps ());
1710
+ if (!Success) {
1711
+ Data.Symbols .clear ();
1712
+ Data.AvailableActions .clear ();
1641
1713
}
1642
1714
}
1643
1715
1644
- CursorInfoData Info;
1645
- Info.InternalDiagnostic =
1646
- " Resolved to incomplete expression or statement." ;
1647
- Receiver (RequestResult<CursorInfoData>::fromResult (Info));
1716
+ if (Data.InternalDiagnostic .empty () && Data.AvailableActions .empty () &&
1717
+ Data.Symbols .empty ()) {
1718
+ Data.InternalDiagnostic =
1719
+ " Resolved to incomplete expression or statement." ;
1720
+ }
1721
+
1722
+ Receiver (RequestResult<CursorInfoData>::fromResult (Data));
1648
1723
return ;
1649
1724
}
1650
1725
case CursorInfoKind::Invalid:
0 commit comments