예제 #1
0
        void PerformAssignment(ILFunction function)
        {
            // remove unused variables before assigning names
            function.Variables.RemoveDead();
            int numDisplayClassLocals = 0;
            Dictionary <int, string> assignedLocalSignatureIndices = new Dictionary <int, string>();

            foreach (var v in function.Variables.OrderBy(v => v.Name))
            {
                switch (v.Kind)
                {
                case VariableKind.Parameter:                         // ignore
                    break;

                case VariableKind.InitializerTarget:                         // keep generated names
                    AddExistingName(reservedVariableNames, v.Name);
                    break;

                case VariableKind.DisplayClassLocal:
                    v.Name = "CS$<>8__locals" + (numDisplayClassLocals++);
                    break;

                case VariableKind.Local when v.Index != null:
                    if (assignedLocalSignatureIndices.TryGetValue(v.Index.Value, out string name))
                    {
                        // make sure all local ILVariables that refer to the same slot in the locals signature
                        // are assigned the same name.
                        v.Name = name;
                    }
                    else
                    {
                        AssignName();
                        // Remember the newly assigned name:
                        assignedLocalSignatureIndices.Add(v.Index.Value, v.Name);
                    }
                    break;

                default:
                    AssignName();
                    break;
                }

                void AssignName()
                {
                    if (v.HasGeneratedName || !IsValidName(v.Name) || ConflictWithLocal(v))
                    {
                        // don't use the name from the debug symbols if it looks like a generated name
                        v.Name = null;
                    }
                    else
                    {
                        // use the name from the debug symbols
                        // (but ensure we don't use the same name for two variables)
                        v.Name = GetAlternativeName(v.Name);
                    }
                }
            }
            foreach (var localFunction in function.LocalFunctions)
            {
                if (!LocalFunctionDecompiler.ParseLocalFunctionName(localFunction.Name, out _, out var newName) || !IsValidName(newName))
                {
                    newName = null;
                }
                localFunction.Name = newName;
                localFunction.ReducedMethod.Name = newName;
            }
            // Now generate names:
            var mapping = new Dictionary <ILVariable, string>(ILVariableEqualityComparer.Instance);

            foreach (var inst in function.Descendants.OfType <IInstructionWithVariableOperand>())
            {
                var v = inst.Variable;
                if (!mapping.TryGetValue(v, out string name))
                {
                    if (string.IsNullOrEmpty(v.Name))
                    {
                        v.Name = GenerateNameForVariable(v);
                    }
                    mapping.Add(v, v.Name);
                }
                else
                {
                    v.Name = name;
                }
            }
            foreach (var localFunction in function.LocalFunctions)
            {
                var newName = localFunction.Name;
                if (newName == null)
                {
                    newName = GetAlternativeName("f");
                }
                localFunction.Name = newName;
                localFunction.ReducedMethod.Name = newName;
                localFunctionMapping[(MethodDefinitionHandle)localFunction.ReducedMethod.MetadataToken] = newName;
            }
            foreach (var inst in function.Descendants)
            {
                LocalFunctionMethod localFunction;
                switch (inst)
                {
                case Call call:
                    localFunction = call.Method as LocalFunctionMethod;
                    break;

                case LdFtn ldftn:
                    localFunction = ldftn.Method as LocalFunctionMethod;
                    break;

                default:
                    localFunction = null;
                    break;
                }
                if (localFunction == null || !localFunctionMapping.TryGetValue((MethodDefinitionHandle)localFunction.MetadataToken, out var name))
                {
                    continue;
                }
                localFunction.Name = name;
            }
        }
예제 #2
0
        void PerformAssignment(ILFunction function)
        {
            // remove unused variables before assigning names
            function.Variables.RemoveDead();
            int numDisplayClassLocals = 0;

            foreach (var v in function.Variables.OrderBy(v => v.Name))
            {
                switch (v.Kind)
                {
                case VariableKind.Parameter:                         // ignore
                    break;

                case VariableKind.InitializerTarget:                         // keep generated names
                    AddExistingName(reservedVariableNames, v.Name);
                    break;

                case VariableKind.DisplayClassLocal:
                    v.Name = "CS$<>8__locals" + (numDisplayClassLocals++);
                    break;

                default:
                    if (v.HasGeneratedName || !IsValidName(v.Name) || ConflictWithLocal(v))
                    {
                        // don't use the name from the debug symbols if it looks like a generated name
                        v.Name = null;
                    }
                    else
                    {
                        // use the name from the debug symbols
                        // (but ensure we don't use the same name for two variables)
                        v.Name = GetAlternativeName(v.Name);
                    }
                    break;
                }
            }
            foreach (var localFunction in function.LocalFunctions)
            {
                if (!LocalFunctionDecompiler.ParseLocalFunctionName(localFunction.Name, out _, out var newName) || !IsValidName(newName))
                {
                    newName = null;
                }
                localFunction.Name = newName;
            }
            // Now generate names:
            var mapping = new Dictionary <ILVariable, string>(ILVariableEqualityComparer.Instance);

            foreach (var inst in function.Descendants.OfType <IInstructionWithVariableOperand>())
            {
                var v = inst.Variable;
                if (!mapping.TryGetValue(v, out string name))
                {
                    if (string.IsNullOrEmpty(v.Name))
                    {
                        v.Name = GenerateNameForVariable(v);
                    }
                    mapping.Add(v, v.Name);
                }
                else
                {
                    v.Name = name;
                }
            }
            foreach (var localFunction in function.LocalFunctions)
            {
                var newName = localFunction.Name;
                if (newName == null)
                {
                    newName = GetAlternativeName("f");
                }
                localFunction.Name = newName;
            }
        }