Skip to content

Commit

Permalink
Variadic Generics Part 3: language server (shader-slang#4850)
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Aug 15, 2024
1 parent 2db09d5 commit 0c468a3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 36 deletions.
18 changes: 18 additions & 0 deletions source/slang/slang-ast-iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,30 @@ struct ASTIterator
for (auto arg : expr->arguments)
dispatchIfNotNull(arg);
}
void visitPackExpr(PackExpr* expr)
{
for (auto arg : expr->args)
dispatchIfNotNull(arg);
}

void visitExpandExpr(ExpandExpr* expr)
{
iterator->maybeDispatchCallback(expr);
dispatchIfNotNull(expr->baseExpr);
}

void visitEachExpr(EachExpr* expr)
{
iterator->maybeDispatchCallback(expr);
dispatchIfNotNull(expr->baseExpr);
}

void visitDerefExpr(DerefExpr* expr)
{
iterator->maybeDispatchCallback(expr);
dispatchIfNotNull(expr->base);
}

void visitMatrixSwizzleExpr(MatrixSwizzleExpr* expr)
{
iterator->maybeDispatchCallback(expr);
Expand Down
96 changes: 61 additions & 35 deletions source/slang/slang-ast-print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void ASTPrinter::_addDeclPathRec(const DeclRef<Decl>& declRef, Index depth)
// signature
if (parentGenericDeclRef &&
!declRef.as<GenericValueParamDecl>() &&
!declRef.as<GenericTypeParamDecl>())
!declRef.as<GenericTypeParamDeclBase>())
{
auto substArgs = tryGetGenericArguments(SubstitutionSet(declRef), parentGenericDeclRef.getDecl());
if (substArgs.getCount())
Expand Down Expand Up @@ -250,6 +250,16 @@ void ASTPrinter::addGenericParams(const DeclRef<GenericDecl>& genericDeclRef)
addType(getType(m_astBuilder, genericValParam));
}
}
else if (auto genericTypePackParam = paramDeclRef.as<GenericTypePackParamDecl>())
{
if (!first) sb << ", ";
first = false;
{
ScopePart scopePart(this, Part::Type::GenericParamType);
sb << "each ";
sb << getText(genericTypePackParam.getName());
}
}
else
{
}
Expand All @@ -269,57 +279,73 @@ void ASTPrinter::addDeclParams(const DeclRef<Decl>& declRef, List<Range<Index>>*
bool first = true;
for (auto paramDeclRef : getParameters(m_astBuilder, funcDeclRef))
{
if (!first) sb << ", ";

auto rangeStart = sb.getLength();

ParamDecl* paramDecl = paramDeclRef.getDecl();
auto paramType = getType(m_astBuilder, paramDeclRef);

auto addParamElement = [&](Type* type, Index elementIndex)
{
ScopePart scopePart(this, Part::Type::ParamType);

// Seems these apply to parameters/VarDeclBase and are not part of the 'type'
// but seems more appropriate to put in the Type Part
if (!first) sb << ", ";

if (paramDecl->hasModifier<InOutModifier>())
{
sb << toSlice("inout ");
}
else if (paramDecl->hasModifier<OutModifier>())
// Type part.
{
sb << toSlice("out ");
}
else if (paramDecl->hasModifier<InModifier>())
{
sb << toSlice("in ");
ScopePart scopePart(this, Part::Type::ParamType);

// Seems these apply to parameters/VarDeclBase and are not part of the 'type'
// but seems more appropriate to put in the Type Part

if (paramDecl->hasModifier<InOutModifier>())
{
sb << toSlice("inout ");
}
else if (paramDecl->hasModifier<OutModifier>())
{
sb << toSlice("out ");
}
else if (paramDecl->hasModifier<InModifier>())
{
sb << toSlice("in ");
}

// And this to params/variables (not the type)
if (paramDecl->hasModifier<ConstModifier>())
{
sb << toSlice("const ");
}

addType(type);
}

// And this to params/variables (not the type)
if (paramDecl->hasModifier<ConstModifier>())
// Output the parameter name if there is one, and it's enabled in the options
if (m_optionFlags & OptionFlag::ParamNames && paramDecl->getName())
{
sb << toSlice("const ");
sb << " ";
{
ScopePart scopePart(this, Part::Type::ParamName);
sb << paramDecl->getName()->text;
if (elementIndex != -1)
sb << "_" << elementIndex;
}
}

addType(getType(m_astBuilder, paramDeclRef));
}
auto rangeEnd = sb.getLength();

// Output the parameter name if there is one, and it's enabled in the options
if (m_optionFlags & OptionFlag::ParamNames && paramDecl->getName())
if (outParamRange)
outParamRange->add(makeRange<Index>(rangeStart, rangeEnd));
first = false;
};
if (auto typePack = as<ConcreteTypePack>(paramType))
{
sb << " ";

for (Index elementIndex = 0; elementIndex < typePack->getTypeCount(); ++elementIndex)
{
ScopePart scopePart(this, Part::Type::ParamName);
sb << paramDecl->getName()->text;
addParamElement(typePack->getElementType(elementIndex), elementIndex);
}
}

auto rangeEnd = sb.getLength();

if (outParamRange)
outParamRange->add(makeRange<Index>(rangeStart, rangeEnd));

first = false;
else
{
addParamElement(paramType, -1);
}
}

sb << ")";
Expand Down
29 changes: 28 additions & 1 deletion source/slang/slang-language-server-ast-lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,16 @@ struct ASTLookupExprVisitor: public ExprVisitor<ASTLookupExprVisitor, bool>
return false;
}
bool visitTryExpr(TryExpr* expr) { return dispatchIfNotNull(expr->base); }
bool visitHigherOrderInvokeExpr(HigherOrderInvokeExpr* expr)
bool visitPackExpr(PackExpr* expr)
{
for (auto arg : expr->args)
{
if(dispatchIfNotNull(arg))
return true;
}
return false;
}
bool reportLookupResultIfInExprLeadingIdentifierRange(Expr* expr)
{
auto humaneLoc = context->sourceManager->getHumaneLoc(expr->loc, SourceLocType::Actual);
auto tokenLen = context->doc->getTokenLength(humaneLoc.line, humaneLoc.column);
Expand All @@ -473,6 +482,24 @@ struct ASTLookupExprVisitor: public ExprVisitor<ASTLookupExprVisitor, bool>
context->results.add(result);
return true;
}
return false;
}
bool visitExpandExpr(ExpandExpr* expr)
{
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
return true;
return dispatchIfNotNull(expr->baseExpr);
}
bool visitEachExpr(EachExpr* expr)
{
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
return true;
return dispatchIfNotNull(expr->baseExpr);
}
bool visitHigherOrderInvokeExpr(HigherOrderInvokeExpr* expr)
{
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
return true;
return dispatchIfNotNull(expr->baseFunction);
}
bool visitTreatAsDifferentiableExpr(TreatAsDifferentiableExpr* expr)
Expand Down
4 changes: 4 additions & 0 deletions source/slang/slang-language-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ String getDeclKindString(DeclRef<Decl> declRef)
{
return "(generic type parameter) ";
}
else if (declRef.as<GenericTypePackParamDecl>())
{
return "(generic type pack parameter) ";
}
else if (declRef.as<GenericValueParamDecl>())
{
return "(generic value parameter) ";
Expand Down

0 comments on commit 0c468a3

Please sign in to comment.