Skip to content

Commit 2ae194d

Browse files
authored
Fix ConstantIntVal::toText when the val is a enum. (#6224)
* Fix ConstantIntVal::toText when the val is a enum. * Fix comment.
1 parent cbcb97a commit 2ae194d

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

source/slang/slang-ast-decl.h

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class EnumCaseDecl : public Decl
210210

211211
// Tag value
212212
Expr* tagExpr = nullptr;
213+
214+
IntVal* tagVal = nullptr;
213215
};
214216

215217
// A member of InterfaceDecl representing the abstract ThisType.

source/slang/slang-ast-val.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@ void Val::_toTextOverride(StringBuilder& out)
149149

150150
void ConstantIntVal::_toTextOverride(StringBuilder& out)
151151
{
152+
if (auto enumTypeDecl = isDeclRefTypeOf<EnumDecl>(getType()))
153+
{
154+
// If this is an enum type, then we want to print the name of the
155+
// corresponding enum case, instead of the raw integer value, if possible.
156+
//
157+
// We will look up the enum case that corresponds to the value, and
158+
// print its name if we can find one.
159+
//
160+
for (auto enumCase : enumTypeDecl.getDecl()->getMembersOfType<EnumCaseDecl>())
161+
{
162+
if (auto constVal = as<ConstantIntVal>(enumCase->tagVal))
163+
{
164+
if (constVal->getValue() == getValue())
165+
{
166+
out << DeclRef(enumCase);
167+
return;
168+
}
169+
}
170+
}
171+
172+
// Fallback to explicit cast to the enum type.
173+
out << getType() << "(" << getValue() << ")";
174+
return;
175+
}
152176
out << getValue();
153177
}
154178

source/slang/slang-check-decl.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -7967,9 +7967,8 @@ void SemanticsDeclBodyVisitor::visitEnumCaseDecl(EnumCaseDecl* decl)
79677967
initExpr = coerce(CoercionSite::General, tagType, initExpr);
79687968

79697969
// We want to enforce that this is an integer constant
7970-
// expression, but we don't actually care to retain
7971-
// the value.
7972-
CheckIntegerConstantExpression(
7970+
// expression.
7971+
decl->tagVal = CheckIntegerConstantExpression(
79737972
initExpr,
79747973
IntegerConstantExpressionCoercionType::AnyInteger,
79757974
nullptr,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//TEST:LANG_SERVER(filecheck=CHECK):
2+
3+
namespace ns {
4+
enum Test : uint32_t
5+
{
6+
A = 1,
7+
B = 2,
8+
}
9+
10+
struct Foo<let T : Test>
11+
{
12+
}
13+
}
14+
15+
void f()
16+
{
17+
//HOVER:18,25
18+
ns.Foo<ns.Test.A> first;
19+
//HOVER:20,27
20+
ns.Foo<ns.Test(3)> second;
21+
}
22+
23+
// CHECK: ns.Foo<ns.Test.A>
24+
// CHECK: ns.Foo<ns.Test(3)>

0 commit comments

Comments
 (0)