summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-08-15 13:53:58 -0700
committerGitHub <noreply@github.com>2024-08-15 13:53:58 -0700
commit0c468a3e7e693b12a59f643be4c7fea5ee29e5e3 (patch)
treeddb76a1366fc782b057d3a40140c085aa9841d4c
parent2db09d58f74376d792c36711cbc48c4a3d5eba60 (diff)
Variadic Generics Part 3: language server (#4850)
-rw-r--r--source/slang/slang-ast-iterator.h18
-rw-r--r--source/slang/slang-ast-print.cpp96
-rw-r--r--source/slang/slang-language-server-ast-lookup.cpp29
-rw-r--r--source/slang/slang-language-server.cpp4
4 files changed, 111 insertions, 36 deletions
diff --git a/source/slang/slang-ast-iterator.h b/source/slang/slang-ast-iterator.h
index 2b6a2d3e2..40a943dc0 100644
--- a/source/slang/slang-ast-iterator.h
+++ b/source/slang/slang-ast-iterator.h
@@ -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);
diff --git a/source/slang/slang-ast-print.cpp b/source/slang/slang-ast-print.cpp
index d3e028901..f5f99f01d 100644
--- a/source/slang/slang-ast-print.cpp
+++ b/source/slang/slang-ast-print.cpp
@@ -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())
@@ -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
{
}
@@ -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 << ")";
diff --git a/source/slang/slang-language-server-ast-lookup.cpp b/source/slang/slang-language-server-ast-lookup.cpp
index 755805b51..5c73d2ab9 100644
--- a/source/slang/slang-language-server-ast-lookup.cpp
+++ b/source/slang/slang-language-server-ast-lookup.cpp
@@ -461,7 +461,16 @@ public:
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);
@@ -473,6 +482,24 @@ public:
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)
diff --git a/source/slang/slang-language-server.cpp b/source/slang/slang-language-server.cpp
index ed03a5dc4..81c7d24cf 100644
--- a/source/slang/slang-language-server.cpp
+++ b/source/slang/slang-language-server.cpp
@@ -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) ";