Engine: Make some minor cleanup changes (#6609)

- Remove the calls to `GetTypeInfo()` in the generated `.resx` binding C# binding code (change in the tool `ResGen`).
- Remove the unused methods in `SessionState.cs`.
- Remove the calls to `GetTypeInfo()` from `Compiler.cs` and other files in `engine\runtime`.
- Fix typos in `VariableAnalysis.cs` and `ClassOps.cs`.
- Minor perf improvement in `MutableTuple.cs` by using indexing instead of linq extension method `last()`.
This commit is contained in:
Dongbo Wang 2018-04-10 18:03:20 -07:00 committed by GitHub
parent 68b59e3d9b
commit 153d677839
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 90 deletions

View file

@ -140,7 +140,7 @@ internal class {0} {{
internal static global::System.Resources.ResourceManager ResourceManager {{
get {{
if (object.ReferenceEquals(resourceMan, null)) {{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager(""{1}.resources.{3}"", typeof({0}).GetTypeInfo().Assembly);
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager(""{1}.resources.{3}"", typeof({0}).Assembly);
resourceMan = temp;
}}
return resourceMan;

View file

@ -376,55 +376,6 @@ namespace System.Management.Automation
this.GlobalScope.SetVariable(v.Name, v, false, true, this, CommandOrigin.Internal, fastPath: true);
}
/// <summary>
/// Add all of the default built-in functions to this session state instance...
/// </summary>
internal void AddBuiltInEntries(bool addSetStrictMode)
{
// Other built-in variables
AddBuiltInVariables();
AddBuiltInFunctions();
AddBuiltInAliases();
if (addSetStrictMode)
{
SessionStateFunctionEntry f = new SessionStateFunctionEntry("Set-StrictMode", "");
this.AddSessionStateEntry(f);
}
}
/// <summary>
/// Add the built-in variables to this instance of session state...
/// </summary>
internal void AddBuiltInVariables()
{
foreach (SessionStateVariableEntry e in InitialSessionState.BuiltInVariables)
{
this.AddSessionStateEntry(e);
}
}
/// <summary>
/// Add the built-in functions to this instance of session state...
/// </summary>
internal void AddBuiltInFunctions()
{
foreach (SessionStateFunctionEntry f in InitialSessionState.BuiltInFunctions)
{
this.AddSessionStateEntry(f);
}
}
/// <summary>
/// Add the built-in aliases to this instance of session state...
/// </summary>
internal void AddBuiltInAliases()
{
foreach (SessionStateAliasEntry ae in InitialSessionState.BuiltInAliases)
{
this.AddSessionStateEntry(ae, StringLiterals.Global);
}
}
/// <summary>
/// Check to see if an application is allowed to be run.
/// </summary>

View file

@ -596,7 +596,7 @@ namespace System.Management.Automation.Language
return Expression.Convert(expr, type);
}
if (type.GetTypeInfo().ContainsGenericParameters)
if (type.ContainsGenericParameters)
{
return Expression.Call(
CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException,
@ -614,7 +614,7 @@ namespace System.Management.Automation.Language
return expr;
}
if ((expr.Type.IsFloating() || expr.Type == typeof(Decimal)) && type.GetTypeInfo().IsPrimitive)
if ((expr.Type.IsFloating() || expr.Type == typeof(Decimal)) && type.IsPrimitive)
{
// Convert correctly handles most "primitive" conversions for PowerShell,
// but it does not correctly handle floating point.
@ -1513,7 +1513,7 @@ namespace System.Management.Automation.Language
return true;
}
if (type.GetTypeInfo().IsClass)
if (type.IsClass)
{
value = null;
return true;
@ -1531,7 +1531,7 @@ namespace System.Management.Automation.Language
return true;
}
if (LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(type)) && !type.GetTypeInfo().IsEnum)
if (LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(type)) && !type.IsEnum)
{
value = 0;
return true;
@ -4856,7 +4856,7 @@ namespace System.Management.Automation.Language
var isType = (Type)((ConstantExpression)rhs).Value;
if (!(isType == typeof(PSCustomObject)) && !(isType == typeof(PSObject)))
{
lhs = lhs.Type.GetTypeInfo().IsValueType ? lhs : Expression.Call(CachedReflectionInfo.PSObject_Base, lhs);
lhs = lhs.Type.IsValueType ? lhs : Expression.Call(CachedReflectionInfo.PSObject_Base, lhs);
if (binaryExpressionAst.Operator == TokenKind.Is)
return Expression.TypeIs(lhs, isType);
return Expression.Not(Expression.TypeIs(lhs, isType));
@ -5162,7 +5162,7 @@ namespace System.Management.Automation.Language
var newValue = DynamicExpression.Dynamic(PSUnaryOperationBinder.Get(valueToAdd == 1 ? ExpressionType.Increment : ExpressionType.Decrement),
typeof(object), tmp);
exprs.Add(av.SetValue(this, newValue));
if (tmp.Type.GetTypeInfo().IsValueType)
if (tmp.Type.IsValueType)
{
// This is the result of the expression - it might be unused, but we don't bother knowing if it is used or not.
exprs.Add(tmp);
@ -5363,7 +5363,7 @@ namespace System.Management.Automation.Language
if (memberExpressionAst.Static && (memberExpressionAst.Expression is TypeExpressionAst))
{
var type = ((TypeExpressionAst)memberExpressionAst.Expression).TypeName.GetReflectionType();
if (type != null && !type.GetTypeInfo().IsGenericTypeDefinition)
if (type != null && !type.IsGenericTypeDefinition)
{
var member = memberExpressionAst.Member as StringConstantExpressionAst;
if (member != null)
@ -5419,7 +5419,7 @@ namespace System.Management.Automation.Language
TypeDefinitionAst typeDefinitionAst = Ast.GetAncestorTypeDefinitionAst(invokeMemberExpressionAst);
if (typeDefinitionAst != null)
{
targetTypeConstraint = (typeDefinitionAst as TypeDefinitionAst).Type.GetTypeInfo().BaseType;
targetTypeConstraint = (typeDefinitionAst as TypeDefinitionAst).Type.BaseType;
}
else
{

View file

@ -290,7 +290,7 @@ namespace System.Management.Automation.Language
return AstVisitAction.SkipChildren;
}
// Return true if the variable is newly allocated and should be allocated in the locals tuple.
// Add a variable to the variable dictionary
private void NoteVariable(string variableName, int index, Type type, bool automatic = false, bool preferenceVariable = false)
{
if (!_variables.ContainsKey(variableName))
@ -504,7 +504,7 @@ namespace System.Management.Automation.Language
{
_variables = FindAllVariablesVisitor.Visit(exprAst);
// We disable optimizations for trap because it simplifies what we need to do when invoking
// We disable optimizations for expression because it simplifies what we need to do when invoking
// the default argument, and it's assumed that the code inside a default argument rarely, if ever, actually creates
// any local variables.
_disableOptimizations = true;
@ -731,7 +731,7 @@ namespace System.Management.Automation.Language
// $value.Property = 42
// We make sure we never allocate an instance of such mutable types in the MutableType.
return (type.GetTypeInfo().IsValueType && PSVariableAssignmentBinder.IsValueTypeMutable(type)) && typeof(SwitchParameter) != type;
return (type.IsValueType && PSVariableAssignmentBinder.IsValueTypeMutable(type)) && typeof(SwitchParameter) != type;
}
private static void FixTupleIndex(Ast ast, int newIndex)

View file

@ -410,7 +410,7 @@ namespace System.Management.Automation.Language
BindingRestrictions newRestrictions = binder.Restrictions;
newRestrictions = args.Aggregate(newRestrictions, (current, arg) =>
{
if (arg.LimitType.GetTypeInfo().IsValueType)
if (arg.LimitType.IsValueType)
{
return current.Merge(arg.GetSimpleTypeRestriction());
}
@ -646,7 +646,7 @@ namespace System.Management.Automation.Language
foreach (var i in targetValue.GetType().GetInterfaces())
{
if (i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return (new DynamicMetaObject(
MaybeDebase(this, e => Expression.Call(
@ -689,7 +689,7 @@ namespace System.Management.Automation.Language
return false;
}
if (type.GetTypeInfo().IsSealed && !typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IEnumerator).IsAssignableFrom(type))
if (type.IsSealed && !typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IEnumerator).IsAssignableFrom(type))
{
return false;
}
@ -853,7 +853,7 @@ namespace System.Management.Automation.Language
if (enumerable == null)
{
var bindingResult = PSVariableAssignmentBinder.Get().Bind(target, Utils.EmptyArray<DynamicMetaObject>());
var restrictions = target.LimitType.GetTypeInfo().IsValueType
var restrictions = target.LimitType.IsValueType
? bindingResult.Restrictions
: target.PSGetTypeRestriction();
return (new DynamicMetaObject(
@ -1871,7 +1871,7 @@ namespace System.Management.Automation.Language
{
var baseObjType = baseObject.GetType();
restrictions = restrictions.Merge(BindingRestrictions.GetTypeRestriction(baseObjExpr, baseObjType));
if (baseObjType.GetTypeInfo().IsValueType)
if (baseObjType.IsValueType)
{
expr = GetExprForValueType(baseObjType, Expression.Convert(baseObjExpr, baseObjType), expr, ref restrictions);
}
@ -1888,7 +1888,7 @@ namespace System.Management.Automation.Language
}
var type = value.GetType();
if (type.GetTypeInfo().IsValueType)
if (type.IsValueType)
{
var expr = target.Expression;
var restrictions = target.PSGetTypeRestriction();
@ -2032,7 +2032,7 @@ namespace System.Management.Automation.Language
internal static void NoteTypeHasInstanceMemberOrTypeName(Type type)
{
if (!type.GetTypeInfo().IsValueType || !IsValueTypeMutable(type))
if (!type.IsValueType || !IsValueTypeMutable(type))
{
return;
}
@ -2457,7 +2457,7 @@ namespace System.Management.Automation.Language
catchExpr);
}
if (target.LimitType.GetTypeInfo().IsEnum)
if (target.LimitType.IsEnum)
{
// Make sure the result type is an enum unless we were expecting a bool.
switch (Operation)
@ -2558,7 +2558,7 @@ namespace System.Management.Automation.Language
}
bool boolToDecimal = false;
if (arg.LimitType.IsNumericOrPrimitive() && !arg.LimitType.GetTypeInfo().IsEnum)
if (arg.LimitType.IsNumericOrPrimitive() && !arg.LimitType.IsEnum)
{
if (!(targetType == typeof(Decimal) && arg.LimitType == typeof(bool)))
{
@ -2573,7 +2573,7 @@ namespace System.Management.Automation.Language
// ConstrainedLanguage note - calls to this conversion only target numeric types.
var conversion = LanguagePrimitives.FigureConversion(arg.Value, targetType, out debase);
if (conversion.Rank == ConversionRank.ImplicitCast || boolToDecimal || arg.LimitType.GetTypeInfo().IsEnum)
if (conversion.Rank == ConversionRank.ImplicitCast || boolToDecimal || arg.LimitType.IsEnum)
{
return new DynamicMetaObject(
PSConvertBinder.InvokeConverter(conversion, arg.Expression, targetType, debase, ExpressionCache.InvariantCulture),
@ -2887,8 +2887,8 @@ namespace System.Management.Automation.Language
return new DynamicMetaObject(ExpressionCache.Constant(0).Cast(typeof(object)), target.CombineRestrictions(arg));
}
var targetUnderlyingType = (target.LimitType.GetTypeInfo().IsEnum) ? Enum.GetUnderlyingType(target.LimitType) : target.LimitType;
var argUnderlyingType = (arg.LimitType.GetTypeInfo().IsEnum) ? Enum.GetUnderlyingType(arg.LimitType) : arg.LimitType;
var targetUnderlyingType = (target.LimitType.IsEnum) ? Enum.GetUnderlyingType(target.LimitType) : target.LimitType;
var argUnderlyingType = (arg.LimitType.IsEnum) ? Enum.GetUnderlyingType(arg.LimitType) : arg.LimitType;
if (targetUnderlyingType.IsNumericOrPrimitive() || argUnderlyingType.IsNumericOrPrimitive())
{
@ -2952,7 +2952,7 @@ namespace System.Management.Automation.Language
var expr = exprGenerator(numericTarget.Expression.Cast(numericTarget.LimitType).Cast(opType),
numericArg.Expression.Cast(numericArg.LimitType).Cast(opType));
if (target.LimitType.GetTypeInfo().IsEnum)
if (target.LimitType.IsEnum)
{
expr = expr.Cast(target.LimitType);
}
@ -3271,7 +3271,7 @@ namespace System.Management.Automation.Language
{
foreach (var i in target.Value.GetType().GetInterfaces())
{
if (i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IComparable<>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IComparable<>))
{
return new DynamicMetaObject(
toResult(Expression.Call(Expression.Convert(target.Expression, i),
@ -3523,7 +3523,7 @@ namespace System.Management.Automation.Language
var typeCode = LanguagePrimitives.GetTypeCode(target.LimitType);
if (typeCode < TypeCode.Int32)
{
targetExpr = target.LimitType.GetTypeInfo().IsEnum
targetExpr = target.LimitType.IsEnum
? target.Expression.Cast(Enum.GetUnderlyingType(target.LimitType))
: target.Expression.Cast(target.LimitType);
targetExpr = targetExpr.Cast(typeof(int));
@ -3531,7 +3531,7 @@ namespace System.Management.Automation.Language
else if (typeCode <= TypeCode.UInt64)
{
var targetConvertType = target.LimitType;
if (targetConvertType.GetTypeInfo().IsEnum)
if (targetConvertType.IsEnum)
{
targetConvertType = Enum.GetUnderlyingType(targetConvertType);
}
@ -3551,7 +3551,7 @@ namespace System.Management.Automation.Language
if (targetExpr != null)
{
Expression result = Expression.OnesComplement(targetExpr);
if (target.LimitType.GetTypeInfo().IsEnum)
if (target.LimitType.IsEnum)
{
result = result.Cast(target.LimitType);
}
@ -3809,7 +3809,7 @@ namespace System.Management.Automation.Language
{
return conv;
}
if (resultType.GetTypeInfo().IsValueType && Nullable.GetUnderlyingType(resultType) == null)
if (resultType.IsValueType && Nullable.GetUnderlyingType(resultType) == null)
{
return Expression.Unbox(conv, resultType);
}
@ -3925,7 +3925,7 @@ namespace System.Management.Automation.Language
foreach (var i in target.LimitType.GetInterfaces())
{
if (i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
{
var result = GetIndexDictionary(target, indexes, i);
if (result != null)
@ -4041,7 +4041,7 @@ namespace System.Management.Automation.Language
}
// target implements IList<T>?
return limitType.GetInterfaces().Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>));
return limitType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>));
}
private DynamicMetaObject IndexWithNegativeChecks(
@ -5085,7 +5085,7 @@ namespace System.Management.Automation.Language
var adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry;
Diagnostics.Assert(adapterData != null, "We have an unknown PSProperty that we aren't correctly optimizing.");
if (!adapterData.member.DeclaringType.GetTypeInfo().IsGenericTypeDefinition)
if (!adapterData.member.DeclaringType.IsGenericTypeDefinition)
{
// For static property access, the target expr must be null. For non-static, we must convert
// because target.Expression is typeof(object) because this is a dynamic site.
@ -5199,7 +5199,7 @@ namespace System.Management.Automation.Language
bool isGeneric = false;
foreach (var i in value.GetType().GetInterfaces())
{
if (i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
{
isGeneric = true;
var genericArguments = i.GetGenericArguments();
@ -5252,7 +5252,7 @@ namespace System.Management.Automation.Language
// In case that castToType is System.Object and expr.Type is Nullable<ValueType>, expr.Cast(System.Object) will
// get the underlying value by default. So "GetTargetExpr(target).Cast(typeof(Object))" is actually the same as
// "GetTargetExpr(target, typeof(Object))".
expr = type.GetTypeInfo().IsValueType
expr = type.IsValueType
? (Nullable.GetUnderlyingType(expr.Type) != null
? (Expression)Expression.Property(expr, "Value")
: Expression.Unbox(expr, type))
@ -6026,7 +6026,7 @@ namespace System.Management.Automation.Language
{
Expression expr;
if (data.member.DeclaringType.GetTypeInfo().IsGenericTypeDefinition)
if (data.member.DeclaringType.IsGenericTypeDefinition)
{
Expression innerException = Expression.New(CachedReflectionInfo.SetValueException_ctor,
Expression.Constant("PropertyAssignmentException"),
@ -6689,7 +6689,7 @@ namespace System.Management.Automation.Language
// Likewise, when calling methods in types defined by PowerShell, we don't
// want to wrap the exception.
if (methodInfo.DeclaringType.GetTypeInfo().Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any())
if (methodInfo.DeclaringType.Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any())
{
return new DynamicMetaObject(expr, restrictions);
}

View file

@ -47,11 +47,12 @@ namespace System.Management.Automation
// slow path
MutableTuple nestedTuple = this;
var accessPath = GetAccessPath(_size, index).ToArray();
for (int i = 0; i < accessPath.Length - 1; ++i)
int length = accessPath.Length;
for (int i = 0; i < length - 1; ++i)
{
nestedTuple = (MutableTuple)nestedTuple.GetValueImpl(accessPath[i]);
}
return nestedTuple._valuesSet[accessPath.Last()];
return nestedTuple._valuesSet[accessPath[length-1]];
}
internal void SetAutomaticVariable(AutomaticVariable auto, object value, ExecutionContext context)

View file

@ -162,7 +162,7 @@ namespace System.Management.Automation.Internal
/// Initialization happens when the script that defines PowerShell class is executed.
/// This initialization is required only if this wrapper is for a static method.
/// </summary>
/// <remakr>
/// <remark>
/// When the same script file gets executed multiple times, the .NET type generated from the PowerShell class
/// defined in the file will be shared in those executions, and thus this method will be called multiple times
/// possibly in the contexts of different Runspace/SessionState.
@ -174,7 +174,7 @@ namespace System.Management.Automation.Internal
/// is declared, and thus we can always get the correct SessionState to use by querying the 'SessionStateKeeper'.
/// The default SessionState is used only if a static method is called from a Runspace where the class is never
/// defined, or is called on a thread without a default Runspace.
/// </remakr>
/// </remark>
internal void InitAtRuntime()
{
if (_isStatic)