From e09215d0d7675e23459c705bfe9578d0f2c84bbf Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 9 Apr 2018 22:12:55 -0700 Subject: [PATCH] [Feature] Clean up unneeded 'GetTypeInfo()' calls in interpreter code --- .../engine/interpreter/AddInstruction.cs | 5 ++--- .../interpreter/CallInstruction.Generated.cs | 15 ++++++--------- .../engine/interpreter/CallInstruction.cs | 2 +- .../engine/interpreter/DivInstruction.cs | 3 +-- .../engine/interpreter/EqualInstruction.cs | 5 ++--- .../engine/interpreter/GreaterThanInstruction.cs | 3 +-- .../engine/interpreter/InstructionList.cs | 10 ++++------ .../engine/interpreter/LessThanInstruction.cs | 3 +-- .../engine/interpreter/LightCompiler.cs | 14 +++++++------- .../engine/interpreter/MulInstruction.cs | 5 ++--- .../engine/interpreter/NotEqualInstruction.cs | 5 ++--- .../engine/interpreter/SubInstruction.cs | 5 ++--- .../engine/interpreter/Utilities.cs | 9 ++++----- 13 files changed, 35 insertions(+), 49 deletions(-) diff --git a/src/System.Management.Automation/engine/interpreter/AddInstruction.cs b/src/System.Management.Automation/engine/interpreter/AddInstruction.cs index 0a5f4e46443..445e8d61bcb 100644 --- a/src/System.Management.Automation/engine/interpreter/AddInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/AddInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -127,7 +126,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new AddInt16()); @@ -259,7 +258,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new AddOvfInt16()); diff --git a/src/System.Management.Automation/engine/interpreter/CallInstruction.Generated.cs b/src/System.Management.Automation/engine/interpreter/CallInstruction.Generated.cs index 4f34eff30fd..1d1baab5d91 100644 --- a/src/System.Management.Automation/engine/interpreter/CallInstruction.Generated.cs +++ b/src/System.Management.Automation/engine/interpreter/CallInstruction.Generated.cs @@ -98,11 +98,10 @@ private static CallInstruction FastCreate(MethodInfo target, ParameterInfo[] pi) return new ActionCallInstruction(target); } - var typeInfo = t.GetTypeInfo(); - if (typeInfo.IsEnum) return SlowCreate(target, pi); + if (t.IsEnum) return SlowCreate(target, pi); switch (t.GetTypeCode()) { case TypeCode.Object: { - if (t != typeof(object) && (IndexIsNotReturnType(0, target, pi) || typeInfo.IsValueType)) { + if (t != typeof(object) && (IndexIsNotReturnType(0, target, pi) || t.IsValueType)) { // if we're on the return type relaxed delegates makes it ok to use object goto default; } @@ -136,11 +135,10 @@ private static CallInstruction FastCreate(MethodInfo target, ParameterInfo[] return new FuncCallInstruction(target); } - var typeInfo = t.GetTypeInfo(); - if (typeInfo.IsEnum) return SlowCreate(target, pi); + if (t.IsEnum) return SlowCreate(target, pi); switch (t.GetTypeCode()) { case TypeCode.Object: { - if (t != typeof(object) && (IndexIsNotReturnType(1, target, pi) || typeInfo.IsValueType)) { + if (t != typeof(object) && (IndexIsNotReturnType(1, target, pi) || t.IsValueType)) { // if we're on the return type relaxed delegates makes it ok to use object goto default; } @@ -174,12 +172,11 @@ private static CallInstruction FastCreate(MethodInfo target, ParameterIn return new FuncCallInstruction(target); } - var typeInfo = t.GetTypeInfo(); - if (typeInfo.IsEnum) return SlowCreate(target, pi); + if (t.IsEnum) return SlowCreate(target, pi); switch (t.GetTypeCode()) { case TypeCode.Object: { Debug.Assert(pi.Length == 2); - if (typeInfo.IsValueType) goto default; + if (t.IsValueType) goto default; return new FuncCallInstruction(target); } diff --git a/src/System.Management.Automation/engine/interpreter/CallInstruction.cs b/src/System.Management.Automation/engine/interpreter/CallInstruction.cs index bcaca8bda38..3bac9e5df09 100644 --- a/src/System.Management.Automation/engine/interpreter/CallInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/CallInstruction.cs @@ -57,7 +57,7 @@ public static CallInstruction Create(MethodInfo info, ParameterInfo[] parameters return GetArrayAccessor(info, argumentCount); } - if (info is DynamicMethod || !info.IsStatic && info.DeclaringType.GetTypeInfo().IsValueType) + if (info is DynamicMethod || !info.IsStatic && info.DeclaringType.IsValueType) { return new MethodInfoCallInstruction(info, argumentCount); } diff --git a/src/System.Management.Automation/engine/interpreter/DivInstruction.cs b/src/System.Management.Automation/engine/interpreter/DivInstruction.cs index 292e5e5c810..e8e60cd4f00 100644 --- a/src/System.Management.Automation/engine/interpreter/DivInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/DivInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -127,7 +126,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new DivInt16()); diff --git a/src/System.Management.Automation/engine/interpreter/EqualInstruction.cs b/src/System.Management.Automation/engine/interpreter/EqualInstruction.cs index 4047fd1b11d..e493692d89c 100644 --- a/src/System.Management.Automation/engine/interpreter/EqualInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/EqualInstruction.cs @@ -149,9 +149,8 @@ public override int Run(InterpretedFrame frame) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] public static Instruction Create(Type type) { - var typeInfo = type.GetTypeInfo(); // Boxed enums can be unboxed as their underlying types: - var typeToUse = typeInfo.IsEnum ? Enum.GetUnderlyingType(type) : type; + var typeToUse = type.IsEnum ? Enum.GetUnderlyingType(type) : type; switch (typeToUse.GetTypeCode()) { case TypeCode.Boolean: return s_boolean ?? (s_boolean = new EqualBoolean()); @@ -170,7 +169,7 @@ public static Instruction Create(Type type) case TypeCode.Double: return s_double ?? (s_double = new EqualDouble()); case TypeCode.Object: - if (!typeInfo.IsValueType) + if (!type.IsValueType) { return s_reference ?? (s_reference = new EqualReference()); } diff --git a/src/System.Management.Automation/engine/interpreter/GreaterThanInstruction.cs b/src/System.Management.Automation/engine/interpreter/GreaterThanInstruction.cs index 61a7e3aab10..10815af0462 100644 --- a/src/System.Management.Automation/engine/interpreter/GreaterThanInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/GreaterThanInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -141,7 +140,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.SByte: return s_SByte ?? (s_SByte = new GreaterThanSByte()); diff --git a/src/System.Management.Automation/engine/interpreter/InstructionList.cs b/src/System.Management.Automation/engine/interpreter/InstructionList.cs index afd472aace9..ec2329d12af 100644 --- a/src/System.Management.Automation/engine/interpreter/InstructionList.cs +++ b/src/System.Management.Automation/engine/interpreter/InstructionList.cs @@ -355,7 +355,7 @@ public void EmitLoad(object value, Type type) return; } - if (type == null || type.GetTypeInfo().IsValueType) + if (type == null || type.IsValueType) { if (value is bool) { @@ -625,7 +625,7 @@ public void EmitInitializeLocal(int index, Type type) { Emit(new InitializeLocalInstruction.ImmutableValue(index, value)); } - else if (type.GetTypeInfo().IsValueType) + else if (type.IsValueType) { Emit(new InitializeLocalInstruction.MutableValue(index, type)); } @@ -712,8 +712,7 @@ public void EmitNewRuntimeVariables(int count) public void EmitGetArrayItem(Type arrayType) { var elementType = arrayType.GetElementType(); - var elementTypeInfo = elementType.GetTypeInfo(); - if (elementTypeInfo.IsClass || elementTypeInfo.IsInterface) + if (elementType.IsClass || elementType.IsInterface) { Emit(InstructionFactory.Factory.GetArrayItem()); } @@ -726,8 +725,7 @@ public void EmitGetArrayItem(Type arrayType) public void EmitSetArrayItem(Type arrayType) { var elementType = arrayType.GetElementType(); - var elementTypeInfo = elementType.GetTypeInfo(); - if (elementTypeInfo.IsClass || elementTypeInfo.IsInterface) + if (elementType.IsClass || elementType.IsInterface) { Emit(InstructionFactory.Factory.SetArrayItem()); } diff --git a/src/System.Management.Automation/engine/interpreter/LessThanInstruction.cs b/src/System.Management.Automation/engine/interpreter/LessThanInstruction.cs index 6b5f4fcae80..992f30eb579 100644 --- a/src/System.Management.Automation/engine/interpreter/LessThanInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/LessThanInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -141,7 +140,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.SByte: return s_SByte ?? (s_SByte = new LessThanSByte()); diff --git a/src/System.Management.Automation/engine/interpreter/LightCompiler.cs b/src/System.Management.Automation/engine/interpreter/LightCompiler.cs index 2dac5e8ee64..048f48df304 100644 --- a/src/System.Management.Automation/engine/interpreter/LightCompiler.cs +++ b/src/System.Management.Automation/engine/interpreter/LightCompiler.cs @@ -381,7 +381,7 @@ private void CompileDefaultExpression(Type type) { if (type != typeof(void)) { - if (type.GetTypeInfo().IsValueType) + if (type.IsValueType) { object value = ScriptingRuntimeHelpers.GetPrimitiveDefaultValue(type); if (value != null) @@ -790,7 +790,7 @@ private void CompileBinaryExpression(Expression expr) private void CompileEqual(Expression left, Expression right) { Debug.Assert(left.Type == right.Type || - !left.Type.GetTypeInfo().IsValueType && !right.Type.GetTypeInfo().IsValueType); + !left.Type.IsValueType && !right.Type.IsValueType); Compile(left); Compile(right); _instructions.EmitEqual(left.Type); @@ -799,7 +799,7 @@ private void CompileEqual(Expression left, Expression right) private void CompileNotEqual(Expression left, Expression right) { Debug.Assert(left.Type == right.Type || - !left.Type.GetTypeInfo().IsValueType && !right.Type.GetTypeInfo().IsValueType); + !left.Type.IsValueType && !right.Type.IsValueType); Compile(left); Compile(right); _instructions.EmitNotEqual(left.Type); @@ -1550,9 +1550,9 @@ private void CompileMethodCallExpression(Expression expr) // force compilation for now for ref types // also could be a mutable value type, Delegate.CreateDelegate and MethodInfo.Invoke both can't handle this, we // need to generate code. - var declaringTypeInfo = node.Method.DeclaringType.GetTypeInfo(); + var declaringType = node.Method.DeclaringType; if (!parameters.TrueForAll(p => !p.ParameterType.IsByRef) || - (!node.Method.IsStatic && declaringTypeInfo.IsValueType && !declaringTypeInfo.IsPrimitive)) + (!node.Method.IsStatic && declaringType.IsValueType && !declaringType.IsPrimitive)) { _forceCompile = true; } @@ -1595,7 +1595,7 @@ private void CompileNewExpression(Expression expr) } else { - Debug.Assert(expr.Type.GetTypeInfo().IsValueType); + Debug.Assert(expr.Type.IsValueType); _instructions.EmitDefaultValue(node.Type); } } @@ -1844,7 +1844,7 @@ private void CompileTypeIsExpression(Expression expr) Compile(node.Expression); // use TypeEqual for sealed types: - if (node.TypeOperand.GetTypeInfo().IsSealed) + if (node.TypeOperand.IsSealed) { _instructions.EmitLoad(node.TypeOperand); _instructions.EmitTypeEquals(); diff --git a/src/System.Management.Automation/engine/interpreter/MulInstruction.cs b/src/System.Management.Automation/engine/interpreter/MulInstruction.cs index 5a05c71367c..4c74db7092e 100644 --- a/src/System.Management.Automation/engine/interpreter/MulInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/MulInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -127,7 +126,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new MulInt16()); @@ -259,7 +258,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new MulOvfInt16()); diff --git a/src/System.Management.Automation/engine/interpreter/NotEqualInstruction.cs b/src/System.Management.Automation/engine/interpreter/NotEqualInstruction.cs index 4f207028d15..882815d68c3 100644 --- a/src/System.Management.Automation/engine/interpreter/NotEqualInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/NotEqualInstruction.cs @@ -149,9 +149,8 @@ public override int Run(InterpretedFrame frame) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] public static Instruction Create(Type type) { - var typeInfo = type.GetTypeInfo(); // Boxed enums can be unboxed as their underlying types: - var typeToUse = typeInfo.IsEnum ? Enum.GetUnderlyingType(type) : type; + var typeToUse = type.IsEnum ? Enum.GetUnderlyingType(type) : type; switch (typeToUse.GetTypeCode()) { case TypeCode.Boolean: return s_boolean ?? (s_boolean = new NotEqualBoolean()); @@ -170,7 +169,7 @@ public static Instruction Create(Type type) case TypeCode.Double: return s_double ?? (s_double = new NotEqualDouble()); case TypeCode.Object: - if (!typeInfo.IsValueType) + if (!type.IsValueType) { return s_reference ?? (s_reference = new NotEqualReference()); } diff --git a/src/System.Management.Automation/engine/interpreter/SubInstruction.cs b/src/System.Management.Automation/engine/interpreter/SubInstruction.cs index fd6985e602d..78ba259fbca 100644 --- a/src/System.Management.Automation/engine/interpreter/SubInstruction.cs +++ b/src/System.Management.Automation/engine/interpreter/SubInstruction.cs @@ -14,7 +14,6 @@ * ***************************************************************************/ using System.Diagnostics; -using System.Reflection; namespace System.Management.Automation.Interpreter { @@ -127,7 +126,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new SubInt16()); @@ -259,7 +258,7 @@ public override int Run(InterpretedFrame frame) public static Instruction Create(Type type) { - Debug.Assert(!type.GetTypeInfo().IsEnum); + Debug.Assert(!type.IsEnum); switch (type.GetTypeCode()) { case TypeCode.Int16: return s_int16 ?? (s_int16 = new SubOvfInt16()); diff --git a/src/System.Management.Automation/engine/interpreter/Utilities.cs b/src/System.Management.Automation/engine/interpreter/Utilities.cs index a68f801bd08..de37d530749 100644 --- a/src/System.Management.Automation/engine/interpreter/Utilities.cs +++ b/src/System.Management.Automation/engine/interpreter/Utilities.cs @@ -26,7 +26,7 @@ internal static Type GetNonNullableType(this Type type) internal static Type GetNullableType(Type type) { Debug.Assert(type != null, "type cannot be null"); - if (type.GetTypeInfo().IsValueType && !IsNullableType(type)) + if (type.IsValueType && !IsNullableType(type)) { return typeof(Nullable<>).MakeGenericType(type); } @@ -35,8 +35,7 @@ internal static Type GetNullableType(Type type) internal static bool IsNullableType(Type type) { - var typeInfo = type.GetTypeInfo(); - return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>); + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); } internal static bool IsBool(Type type) @@ -47,7 +46,7 @@ internal static bool IsBool(Type type) internal static bool IsNumeric(Type type) { type = GetNonNullableType(type); - if (!type.GetTypeInfo().IsEnum) + if (!type.IsEnum) { switch (type.GetTypeCode()) { @@ -91,7 +90,7 @@ internal static bool IsNumeric(TypeCode typeCode) internal static bool IsArithmetic(Type type) { type = GetNonNullableType(type); - if (!type.GetTypeInfo().IsEnum) + if (!type.IsEnum) { switch (type.GetTypeCode()) {