dotnet / linker

389 stars 126 forks source link

Use enum for method parameter indexing #2993

Closed jtschuster closed 2 years ago

jtschuster commented 2 years ago

I've run into multiple issues when I think an int parameterIndex is the source-based index when it is the IL-based index (offset by 1 for this) or vice versa.

In order to fix this, we can use different enums for each definition of "Parameter Index" to enforce the correct usage and avoid the off by one errors. The enums rely on the underlying int value to provide the index number.

This change adds ILParameterIndex to represent the IL-based indexing where 0 may refer to this, and SourceParameterIndex where 0 is the first "real" parameter in C# source code. Converting from ILParameterIndex to SourceParameterIndex returns a SourceParameterKind to differentiate betweenthisand other parameters and ensure that anySourceParameterIndexwill be able to indexMethodReference.Parameters`.

It uses these in ParameterReferenceValue as well as GetMethodParameterValue() and adds a GetMethodThisParameterValue(), ThisParameterReferenceValue, and bubbles the changes where necessary.

Most places should use SourceParameterIndex, and convert to ILParameterIndex when necessary or at the boundary of updated and old code.

I'll continue to update the code to use these enums in other PRs.

jtschuster commented 2 years ago

Sorry for the force push, I ran into issues with rebasing and it seemed like the cleanest option. I've updated the description to describe the updated code. This version differentiates SourceParameterIndex and this parameters to ensure the SourceParameterIndex will always be able to index MethodReference.Parameters