Skip to content

Commit

Permalink
Preserve name in DeclRefExpr for correct highlighitng of This. (#4980)
Browse files Browse the repository at this point in the history
* Preserve name in DeclRefExpr for correct highlighitng of `This`.

* Fix test.
  • Loading branch information
csyonghe authored Sep 5, 2024
1 parent dc3f2d6 commit a7e9119
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 37 deletions.
4 changes: 2 additions & 2 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ namespace Slang
{
auto* invoke = visitor->getASTBuilder()->create<InvokeExpr>();
auto member = visitor->getASTBuilder()->getMemberDeclRef(declRefType->getDeclRef(), defaultCtor);
invoke->functionExpr = visitor->ConstructDeclRefExpr(member, nullptr, defaultCtor->loc, nullptr);
invoke->functionExpr = visitor->ConstructDeclRefExpr(member, nullptr, defaultCtor->getName(), defaultCtor->loc, nullptr);
return invoke;
}
else
Expand Down Expand Up @@ -10103,7 +10103,7 @@ namespace Slang
// aggregate type, we want to form a full declref with default arguments.
declRef = createDefaultSubstitutionsIfNeeded(astBuilder, visitor, declRef);

auto declRefExpr = visitor->ConstructDeclRefExpr(declRef, nullptr, derivativeOfAttr->loc, nullptr);
auto declRefExpr = visitor->ConstructDeclRefExpr(declRef, nullptr, declRef.getName(), derivativeOfAttr->loc, nullptr);
declRefExpr->type.type = nullptr;
derivativeAttr->args.add(declRefExpr);
derivativeAttr->funcExpr = declRefExpr;
Expand Down
35 changes: 20 additions & 15 deletions source/slang/slang-check-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,11 @@ namespace Slang
}

DeclRefExpr* SemanticsVisitor::ConstructDeclRefExpr(
DeclRef<Decl> declRef,
Expr* baseExpr,
DeclRef<Decl> declRef,
Expr* baseExpr,
Name* name,
SourceLoc loc,
Expr* originalExpr)
Expr* originalExpr)
{
// Compute the type that this declaration reference will have in context.
//
Expand Down Expand Up @@ -382,7 +383,7 @@ namespace Slang
expr->loc = loc;
expr->type = type;
expr->baseExpression = baseExpr;
expr->name = declRef.getName();
expr->name = name;
expr->declRef = declRef;
expr->memberOperatorLoc = _getMemberOpLoc(originalExpr);
return expr;
Expand All @@ -399,7 +400,7 @@ namespace Slang
expr->loc = loc;
expr->type = type;
expr->baseExpression = baseTypeExpr;
expr->name = declRef.getName();
expr->name = name;
expr->declRef = declRef;
expr->memberOperatorLoc = _getMemberOpLoc(originalExpr);
return expr;
Expand All @@ -413,7 +414,7 @@ namespace Slang
expr->loc = loc;
expr->type = type;
expr->baseExpression = baseExpr;
expr->name = declRef.getName();
expr->name = name;
expr->declRef = declRef;
expr->memberOperatorLoc = _getMemberOpLoc(originalExpr);

Expand Down Expand Up @@ -464,7 +465,7 @@ namespace Slang
//
auto expr = m_astBuilder->create<VarExpr>();
expr->loc = loc;
expr->name = declRef.getName();
expr->name = name;
expr->type = type;
expr->declRef = declRef;
// Keep a reference to the original expr if it was a genericApp/member.
Expand Down Expand Up @@ -666,13 +667,15 @@ namespace Slang
return ConstructDeclRefExpr(
synthDeclMemberRef,
nullptr,
item.declRef.getName(),
originalExpr ? originalExpr->loc : SourceLoc(),
originalExpr);
}

Expr* SemanticsVisitor::ConstructLookupResultExpr(
LookupResultItem const& item,
Expr* baseExpr,
Expr* baseExpr,
Name* name,
SourceLoc loc,
Expr* originalExpr)
{
Expand All @@ -696,7 +699,7 @@ namespace Slang
switch (breadcrumb->kind)
{
case LookupResultItem::Breadcrumb::Kind::Member:
bb = ConstructDeclRefExpr(breadcrumb->declRef, bb, loc, originalExpr);
bb = ConstructDeclRefExpr(breadcrumb->declRef, bb, name, loc, originalExpr);
break;

case LookupResultItem::Breadcrumb::Kind::Deref:
Expand Down Expand Up @@ -827,7 +830,7 @@ namespace Slang
}
}

return ConstructDeclRefExpr(item.declRef, bb, loc, originalExpr);
return ConstructDeclRefExpr(item.declRef, bb, name, loc, originalExpr);
}

void SemanticsVisitor::suggestCompletionItems(
Expand All @@ -844,9 +847,9 @@ namespace Slang


Expr* SemanticsVisitor::createLookupResultExpr(
Name* name,
LookupResult const& lookupResult,
Expr* baseExpr,
Name* name,
LookupResult const& lookupResult,
Expr* baseExpr,
SourceLoc loc,
Expr* originalExpr)
{
Expand All @@ -864,7 +867,7 @@ namespace Slang
}
else
{
return ConstructLookupResultExpr(lookupResult.item, baseExpr, loc, originalExpr);
return ConstructLookupResultExpr(lookupResult.item, baseExpr, name, loc, originalExpr);
}
}

Expand Down Expand Up @@ -1058,7 +1061,7 @@ namespace Slang
// expression.
//
return ConstructLookupResultExpr(
lookupResult.item, overloadedExpr->base, overloadedExpr->loc, overloadedExpr);
lookupResult.item, overloadedExpr->base, overloadedExpr->name, overloadedExpr->loc, overloadedExpr);
}

// Otherwise, we weren't able to resolve the overloading given
Expand Down Expand Up @@ -1166,6 +1169,7 @@ namespace Slang
auto diffTypeExpr = ConstructLookupResultExpr(
diffTypeLookupResult.item,
baseTypeExpr,
declRefType->getDeclRef().getName(),
declRefType->getDeclRef().getLoc(),
baseTypeExpr);

Expand Down Expand Up @@ -3145,6 +3149,7 @@ namespace Slang
{
auto lookupResultExpr = semantics->ConstructLookupResultExpr(item,
nullptr,
overloadedExpr->name,
overloadedExpr->loc,
nullptr);
auto candidateExpr = actions->createHigherOrderInvokeExpr(semantics);
Expand Down
18 changes: 10 additions & 8 deletions source/slang/slang-check-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1219,10 +1219,11 @@ namespace Slang
}

DeclRefExpr* ConstructDeclRefExpr(
DeclRef<Decl> declRef,
Expr* baseExpr,
DeclRef<Decl> declRef,
Expr* baseExpr,
Name* name,
SourceLoc loc,
Expr* originalExpr);
Expr* originalExpr);

Expr* ConstructDerefExpr(
Expr* base,
Expand All @@ -1236,16 +1237,17 @@ namespace Slang

Expr* ConstructLookupResultExpr(
LookupResultItem const& item,
Expr* baseExpr,
Expr* baseExpr,
Name* name,
SourceLoc loc,
Expr* originalExpr);

Expr* createLookupResultExpr(
Name* name,
LookupResult const& lookupResult,
Expr* baseExpr,
Name* name,
LookupResult const& lookupResult,
Expr* baseExpr,
SourceLoc loc,
Expr* originalExpr);
Expr* originalExpr);

DeclVisibility getTypeVisibility(Type* type);
bool isDeclVisibleFromScope(DeclRef<Decl> declRef, Scope* scope);
Expand Down
4 changes: 3 additions & 1 deletion source/slang/slang-check-overload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ namespace Slang
return ConstructDeclRefExpr(
innerDeclRef,
base,
innerDeclRef.getName(),
originalExpr->loc,
originalExpr);
}
Expand Down Expand Up @@ -1037,6 +1038,7 @@ namespace Slang
baseExpr = ConstructLookupResultExpr(
candidate.item,
context.baseExpr,
candidate.item.declRef.getName(),
context.funcLoc,
context.originalExpr);
break;
Expand Down Expand Up @@ -2201,7 +2203,7 @@ namespace Slang
if (lastInner)
{
auto baseExpr = GetBaseExpr(funcDeclRefExpr);
lastInner->baseFunction = ConstructLookupResultExpr(candidate.item, baseExpr, funcDeclRefExpr->loc, funcDeclRefExpr);
lastInner->baseFunction = ConstructLookupResultExpr(candidate.item, baseExpr, funcDeclRefExpr->name, funcDeclRefExpr->loc, funcDeclRefExpr);
}
candidate.exprVal = expr;
expr->type.type = diffFuncType;
Expand Down
9 changes: 5 additions & 4 deletions source/slang/slang-language-server-semantic-tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ List<SemanticToken> getSemanticTokens(Linkage* linkage, Module* module, UnownedS
token.type != SemanticTokenType::NormalText)
result.add(token);
};
auto handleDeclRef = [&](DeclRef<Decl> declRef, Expr* originalExpr, SourceLoc loc)
auto handleDeclRef = [&](DeclRef<Decl> declRef, Expr* originalExpr, Name* name, SourceLoc loc)
{
if (!declRef)
return;
Expand All @@ -59,7 +59,8 @@ List<SemanticToken> getSemanticTokens(Linkage* linkage, Module* module, UnownedS
decl = genDecl->inner;
if (!decl)
return;
auto name = declRef.getDecl()->getName();
if (!name)
name = declRef.getDecl()->getName();
if (!name)
return;
// Don't look at the expr if it is defined in a different file.
Expand Down Expand Up @@ -134,13 +135,13 @@ List<SemanticToken> getSemanticTokens(Linkage* linkage, Module* module, UnownedS
{
if (auto declRefExpr = as<DeclRefExpr>(node))
{
handleDeclRef(declRefExpr->declRef, declRefExpr->originalExpr, declRefExpr->loc);
handleDeclRef(declRefExpr->declRef, declRefExpr->originalExpr, declRefExpr->name, declRefExpr->loc);
}
else if (auto overloadedExpr = as<OverloadedExpr>(node))
{
if (overloadedExpr->lookupResult2.items.getCount())
{
handleDeclRef(overloadedExpr->lookupResult2.items[0].declRef, overloadedExpr->originalExpr, overloadedExpr->loc);
handleDeclRef(overloadedExpr->lookupResult2.items[0].declRef, overloadedExpr->originalExpr, overloadedExpr->name, overloadedExpr->loc);
}
}
else if (auto accessorDecl = as<AccessorDecl>(node))
Expand Down
15 changes: 8 additions & 7 deletions source/slang/slang-language-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ SlangResult LanguageServer::hover(
}
};

auto fillDeclRefHoverInfo = [&](DeclRef<Decl> declRef)
auto fillDeclRefHoverInfo = [&](DeclRef<Decl> declRef, Name* name)
{
if (declRef.getDecl())
{
Expand Down Expand Up @@ -664,7 +664,8 @@ SlangResult LanguageServer::hover(
hover.range.start.line,
hover.range.start.character);
hover.range.end = hover.range.start;
auto name = declRef.getName();
if (!name)
name = declRef.getName();
if (auto ctorDecl = declRef.as<ConstructorDecl>())
{
auto parent = ctorDecl.getDecl()->parentDecl;
Expand Down Expand Up @@ -692,7 +693,7 @@ SlangResult LanguageServer::hover(
auto fillExprHoverInfo = [&](Expr* expr)
{
if (auto declRefExpr = as<DeclRefExpr>(expr))
return fillDeclRefHoverInfo(declRefExpr->declRef);
return fillDeclRefHoverInfo(declRefExpr->declRef, declRefExpr->name);
else if (as<ThisExpr>(expr))
{
if (expr->type)
Expand Down Expand Up @@ -751,12 +752,12 @@ SlangResult LanguageServer::hover(
};
if (auto declRefExpr = as<DeclRefExpr>(leafNode))
{
fillDeclRefHoverInfo(declRefExpr->declRef);
fillDeclRefHoverInfo(declRefExpr->declRef, declRefExpr->name);
}
else if (auto overloadedExpr = as<OverloadedExpr>(leafNode))
{
LookupResultItem& item = overloadedExpr->lookupResult2.item;
fillDeclRefHoverInfo(item.declRef);
fillDeclRefHoverInfo(item.declRef, overloadedExpr->name);
}
else if (auto overloadedExpr2 = as<OverloadedExpr2>(leafNode))
{
Expand Down Expand Up @@ -826,11 +827,11 @@ SlangResult LanguageServer::hover(
}
else if (auto decl = as<Decl>(leafNode))
{
fillDeclRefHoverInfo(makeDeclRef(decl));
fillDeclRefHoverInfo(makeDeclRef(decl), nullptr);
}
else if (auto attr = as<Attribute>(leafNode))
{
fillDeclRefHoverInfo(makeDeclRef(attr->attributeDecl));
fillDeclRefHoverInfo(makeDeclRef(attr->attributeDecl), nullptr);
hover.range.end.character = hover.range.start.character + (int)attr->originalIdentifierToken.getContentLength();
}
if (sb.getLength() == 0)
Expand Down
12 changes: 12 additions & 0 deletions tests/language-server/this-type-hover.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//TEST:LANG_SERVER(filecheck=CHECK):

struct G
{}

extension G
{
//HOVER:9,7
This my();
}

// CHECK: struct G

0 comments on commit a7e9119

Please sign in to comment.