Skip to content

Commit 0c468a3

Browse files
authored
Variadic Generics Part 3: language server (#4850)
1 parent 2db09d5 commit 0c468a3

4 files changed

+111
-36
lines changed

source/slang/slang-ast-iterator.h

+18
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,30 @@ struct ASTIterator
132132
for (auto arg : expr->arguments)
133133
dispatchIfNotNull(arg);
134134
}
135+
void visitPackExpr(PackExpr* expr)
136+
{
137+
for (auto arg : expr->args)
138+
dispatchIfNotNull(arg);
139+
}
140+
141+
void visitExpandExpr(ExpandExpr* expr)
142+
{
143+
iterator->maybeDispatchCallback(expr);
144+
dispatchIfNotNull(expr->baseExpr);
145+
}
146+
147+
void visitEachExpr(EachExpr* expr)
148+
{
149+
iterator->maybeDispatchCallback(expr);
150+
dispatchIfNotNull(expr->baseExpr);
151+
}
135152

136153
void visitDerefExpr(DerefExpr* expr)
137154
{
138155
iterator->maybeDispatchCallback(expr);
139156
dispatchIfNotNull(expr->base);
140157
}
158+
141159
void visitMatrixSwizzleExpr(MatrixSwizzleExpr* expr)
142160
{
143161
iterator->maybeDispatchCallback(expr);

source/slang/slang-ast-print.cpp

+61-35
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void ASTPrinter::_addDeclPathRec(const DeclRef<Decl>& declRef, Index depth)
170170
// signature
171171
if (parentGenericDeclRef &&
172172
!declRef.as<GenericValueParamDecl>() &&
173-
!declRef.as<GenericTypeParamDecl>())
173+
!declRef.as<GenericTypeParamDeclBase>())
174174
{
175175
auto substArgs = tryGetGenericArguments(SubstitutionSet(declRef), parentGenericDeclRef.getDecl());
176176
if (substArgs.getCount())
@@ -250,6 +250,16 @@ void ASTPrinter::addGenericParams(const DeclRef<GenericDecl>& genericDeclRef)
250250
addType(getType(m_astBuilder, genericValParam));
251251
}
252252
}
253+
else if (auto genericTypePackParam = paramDeclRef.as<GenericTypePackParamDecl>())
254+
{
255+
if (!first) sb << ", ";
256+
first = false;
257+
{
258+
ScopePart scopePart(this, Part::Type::GenericParamType);
259+
sb << "each ";
260+
sb << getText(genericTypePackParam.getName());
261+
}
262+
}
253263
else
254264
{
255265
}
@@ -269,57 +279,73 @@ void ASTPrinter::addDeclParams(const DeclRef<Decl>& declRef, List<Range<Index>>*
269279
bool first = true;
270280
for (auto paramDeclRef : getParameters(m_astBuilder, funcDeclRef))
271281
{
272-
if (!first) sb << ", ";
273-
274282
auto rangeStart = sb.getLength();
275283

276284
ParamDecl* paramDecl = paramDeclRef.getDecl();
285+
auto paramType = getType(m_astBuilder, paramDeclRef);
277286

287+
auto addParamElement = [&](Type* type, Index elementIndex)
278288
{
279-
ScopePart scopePart(this, Part::Type::ParamType);
280-
281-
// Seems these apply to parameters/VarDeclBase and are not part of the 'type'
282-
// but seems more appropriate to put in the Type Part
289+
if (!first) sb << ", ";
283290

284-
if (paramDecl->hasModifier<InOutModifier>())
285-
{
286-
sb << toSlice("inout ");
287-
}
288-
else if (paramDecl->hasModifier<OutModifier>())
291+
// Type part.
289292
{
290-
sb << toSlice("out ");
291-
}
292-
else if (paramDecl->hasModifier<InModifier>())
293-
{
294-
sb << toSlice("in ");
293+
ScopePart scopePart(this, Part::Type::ParamType);
294+
295+
// Seems these apply to parameters/VarDeclBase and are not part of the 'type'
296+
// but seems more appropriate to put in the Type Part
297+
298+
if (paramDecl->hasModifier<InOutModifier>())
299+
{
300+
sb << toSlice("inout ");
301+
}
302+
else if (paramDecl->hasModifier<OutModifier>())
303+
{
304+
sb << toSlice("out ");
305+
}
306+
else if (paramDecl->hasModifier<InModifier>())
307+
{
308+
sb << toSlice("in ");
309+
}
310+
311+
// And this to params/variables (not the type)
312+
if (paramDecl->hasModifier<ConstModifier>())
313+
{
314+
sb << toSlice("const ");
315+
}
316+
317+
addType(type);
295318
}
296319

297-
// And this to params/variables (not the type)
298-
if (paramDecl->hasModifier<ConstModifier>())
320+
// Output the parameter name if there is one, and it's enabled in the options
321+
if (m_optionFlags & OptionFlag::ParamNames && paramDecl->getName())
299322
{
300-
sb << toSlice("const ");
323+
sb << " ";
324+
{
325+
ScopePart scopePart(this, Part::Type::ParamName);
326+
sb << paramDecl->getName()->text;
327+
if (elementIndex != -1)
328+
sb << "_" << elementIndex;
329+
}
301330
}
302331

303-
addType(getType(m_astBuilder, paramDeclRef));
304-
}
332+
auto rangeEnd = sb.getLength();
305333

306-
// Output the parameter name if there is one, and it's enabled in the options
307-
if (m_optionFlags & OptionFlag::ParamNames && paramDecl->getName())
334+
if (outParamRange)
335+
outParamRange->add(makeRange<Index>(rangeStart, rangeEnd));
336+
first = false;
337+
};
338+
if (auto typePack = as<ConcreteTypePack>(paramType))
308339
{
309-
sb << " ";
310-
340+
for (Index elementIndex = 0; elementIndex < typePack->getTypeCount(); ++elementIndex)
311341
{
312-
ScopePart scopePart(this, Part::Type::ParamName);
313-
sb << paramDecl->getName()->text;
342+
addParamElement(typePack->getElementType(elementIndex), elementIndex);
314343
}
315344
}
316-
317-
auto rangeEnd = sb.getLength();
318-
319-
if (outParamRange)
320-
outParamRange->add(makeRange<Index>(rangeStart, rangeEnd));
321-
322-
first = false;
345+
else
346+
{
347+
addParamElement(paramType, -1);
348+
}
323349
}
324350

325351
sb << ")";

source/slang/slang-language-server-ast-lookup.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,16 @@ struct ASTLookupExprVisitor: public ExprVisitor<ASTLookupExprVisitor, bool>
461461
return false;
462462
}
463463
bool visitTryExpr(TryExpr* expr) { return dispatchIfNotNull(expr->base); }
464-
bool visitHigherOrderInvokeExpr(HigherOrderInvokeExpr* expr)
464+
bool visitPackExpr(PackExpr* expr)
465+
{
466+
for (auto arg : expr->args)
467+
{
468+
if(dispatchIfNotNull(arg))
469+
return true;
470+
}
471+
return false;
472+
}
473+
bool reportLookupResultIfInExprLeadingIdentifierRange(Expr* expr)
465474
{
466475
auto humaneLoc = context->sourceManager->getHumaneLoc(expr->loc, SourceLocType::Actual);
467476
auto tokenLen = context->doc->getTokenLength(humaneLoc.line, humaneLoc.column);
@@ -473,6 +482,24 @@ struct ASTLookupExprVisitor: public ExprVisitor<ASTLookupExprVisitor, bool>
473482
context->results.add(result);
474483
return true;
475484
}
485+
return false;
486+
}
487+
bool visitExpandExpr(ExpandExpr* expr)
488+
{
489+
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
490+
return true;
491+
return dispatchIfNotNull(expr->baseExpr);
492+
}
493+
bool visitEachExpr(EachExpr* expr)
494+
{
495+
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
496+
return true;
497+
return dispatchIfNotNull(expr->baseExpr);
498+
}
499+
bool visitHigherOrderInvokeExpr(HigherOrderInvokeExpr* expr)
500+
{
501+
if (reportLookupResultIfInExprLeadingIdentifierRange(expr))
502+
return true;
476503
return dispatchIfNotNull(expr->baseFunction);
477504
}
478505
bool visitTreatAsDifferentiableExpr(TreatAsDifferentiableExpr* expr)

source/slang/slang-language-server.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ String getDeclKindString(DeclRef<Decl> declRef)
245245
{
246246
return "(generic type parameter) ";
247247
}
248+
else if (declRef.as<GenericTypePackParamDecl>())
249+
{
250+
return "(generic type pack parameter) ";
251+
}
248252
else if (declRef.as<GenericValueParamDecl>())
249253
{
250254
return "(generic value parameter) ";

0 commit comments

Comments
 (0)