Skip to content

Commit f970923

Browse files
authored
Remove unnecessary cast for num elements expression (#72302)
1 parent 3719058 commit f970923

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,40 @@ public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext conte
7979

8080
private ExpressionSyntax GetNumElementsExpressionFromMarshallingInfo(TypePositionInfo info, CountInfo count, StubCodeContext context)
8181
{
82-
return count switch
82+
switch (count)
8383
{
84-
SizeAndParamIndexInfo(int size, SizeAndParamIndexInfo.UnspecifiedParam) => GetConstSizeExpression(size),
85-
ConstSizeCountInfo(int size) => GetConstSizeExpression(size),
86-
SizeAndParamIndexInfo(SizeAndParamIndexInfo.UnspecifiedConstSize, TypePositionInfo param) => CheckedExpression(SyntaxKind.CheckedExpression, GetExpressionForParam(param)),
87-
SizeAndParamIndexInfo(int size, TypePositionInfo param) => CheckedExpression(SyntaxKind.CheckedExpression, BinaryExpression(SyntaxKind.AddExpression, GetConstSizeExpression(size), GetExpressionForParam(param))),
88-
CountElementCountInfo(TypePositionInfo elementInfo) => CheckedExpression(SyntaxKind.CheckedExpression, GetExpressionForParam(elementInfo)),
89-
_ => throw new MarshallingNotSupportedException(info, context)
84+
case SizeAndParamIndexInfo(int size, SizeAndParamIndexInfo.UnspecifiedParam):
85+
return GetConstSizeExpression(size);
86+
case ConstSizeCountInfo(int size):
87+
return GetConstSizeExpression(size);
88+
case SizeAndParamIndexInfo(SizeAndParamIndexInfo.UnspecifiedConstSize, TypePositionInfo param):
9089
{
91-
NotSupportedDetails = SR.ArraySizeMustBeSpecified
92-
},
93-
};
90+
ExpressionSyntax expr = GetExpressionForParam(param, out bool isIntType);
91+
return isIntType ? expr : CheckedExpression(SyntaxKind.CheckedExpression, expr);
92+
}
93+
case SizeAndParamIndexInfo(int size, TypePositionInfo param):
94+
return CheckedExpression(SyntaxKind.CheckedExpression,
95+
BinaryExpression(SyntaxKind.AddExpression,
96+
GetConstSizeExpression(size),
97+
GetExpressionForParam(param, out _)));
98+
case CountElementCountInfo(TypePositionInfo elementInfo):
99+
{
100+
ExpressionSyntax expr = GetExpressionForParam(elementInfo, out bool isIntType);
101+
return isIntType ? expr : CheckedExpression(SyntaxKind.CheckedExpression, expr);
102+
}
103+
default:
104+
throw new MarshallingNotSupportedException(info, context)
105+
{
106+
NotSupportedDetails = SR.ArraySizeMustBeSpecified
107+
};
108+
}
94109

95110
static LiteralExpressionSyntax GetConstSizeExpression(int size)
96111
{
97112
return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(size));
98113
}
99114

100-
ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo)
115+
ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo, out bool isIntType)
101116
{
102117
ExpressionSyntax numElementsExpression = GetIndexedNumElementsExpression(
103118
context,
@@ -132,7 +147,10 @@ ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo)
132147
};
133148
}
134149

135-
return CastExpression(
150+
isIntType = specialType.SpecialType == SpecialType.System_Int32;
151+
return isIntType
152+
? numElementsExpression
153+
: CastExpression(
136154
PredefinedType(Token(SyntaxKind.IntKeyword)),
137155
ParenthesizedExpression(numElementsExpression));
138156
}

0 commit comments

Comments
 (0)