From 351c82e296ff48a8e718082b61ff704afd167c6a Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 9 Apr 2018 11:51:22 -0700 Subject: [PATCH] [Feature] Make some minor cleanup changes --- src/ResGen/Program.cs | 2 +- .../engine/SessionState.cs | 49 ------------------ .../engine/parser/Compiler.cs | 16 +++--- .../engine/parser/VariableAnalysis.cs | 6 +-- .../engine/runtime/Binding/Binders.cs | 50 +++++++++---------- .../engine/runtime/MutableTuple.cs | 5 +- .../engine/runtime/Operations/ClassOps.cs | 4 +- 7 files changed, 42 insertions(+), 90 deletions(-) diff --git a/src/ResGen/Program.cs b/src/ResGen/Program.cs index 1dd69f15386..35b8e8ec11a 100644 --- a/src/ResGen/Program.cs +++ b/src/ResGen/Program.cs @@ -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; diff --git a/src/System.Management.Automation/engine/SessionState.cs b/src/System.Management.Automation/engine/SessionState.cs index 97dc1b51bef..3a8074521a3 100644 --- a/src/System.Management.Automation/engine/SessionState.cs +++ b/src/System.Management.Automation/engine/SessionState.cs @@ -376,55 +376,6 @@ internal void InitializeFixedVariables() this.GlobalScope.SetVariable(v.Name, v, false, true, this, CommandOrigin.Internal, fastPath: true); } - /// - /// Add all of the default built-in functions to this session state instance... - /// - internal void AddBuiltInEntries(bool addSetStrictMode) - { - // Other built-in variables - AddBuiltInVariables(); - AddBuiltInFunctions(); - AddBuiltInAliases(); - if (addSetStrictMode) - { - SessionStateFunctionEntry f = new SessionStateFunctionEntry("Set-StrictMode", ""); - this.AddSessionStateEntry(f); - } - } - - /// - /// Add the built-in variables to this instance of session state... - /// - internal void AddBuiltInVariables() - { - foreach (SessionStateVariableEntry e in InitialSessionState.BuiltInVariables) - { - this.AddSessionStateEntry(e); - } - } - - /// - /// Add the built-in functions to this instance of session state... - /// - internal void AddBuiltInFunctions() - { - foreach (SessionStateFunctionEntry f in InitialSessionState.BuiltInFunctions) - { - this.AddSessionStateEntry(f); - } - } - - /// - /// Add the built-in aliases to this instance of session state... - /// - internal void AddBuiltInAliases() - { - foreach (SessionStateAliasEntry ae in InitialSessionState.BuiltInAliases) - { - this.AddSessionStateEntry(ae, StringLiterals.Global); - } - } - /// /// Check to see if an application is allowed to be run. /// diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 67d1e40fd20..0f7233c5b41 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -596,7 +596,7 @@ internal static Expression Convert(this Expression expr, Type type) return Expression.Convert(expr, type); } - if (type.GetTypeInfo().ContainsGenericParameters) + if (type.ContainsGenericParameters) { return Expression.Call( CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException, @@ -614,7 +614,7 @@ internal static Expression Cast(this Expression expr, Type type) 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 @@ internal static bool TryGetDefaultParameterValue(Type type, out object value) return true; } - if (type.GetTypeInfo().IsClass) + if (type.IsClass) { value = null; return true; @@ -1531,7 +1531,7 @@ internal static bool TryGetDefaultParameterValue(Type type, out object value) 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 @@ public object VisitBinaryExpression(BinaryExpressionAst binaryExpressionAst) 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 @@ private Expression CompileIncrementOrDecrement(ExpressionAst exprAst, int valueT 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 @@ public object VisitMemberExpression(MemberExpressionAst memberExpressionAst) 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 @@ internal static PSMethodInvocationConstraints GetInvokeMemberConstraints(BaseCto TypeDefinitionAst typeDefinitionAst = Ast.GetAncestorTypeDefinitionAst(invokeMemberExpressionAst); if (typeDefinitionAst != null) { - targetTypeConstraint = (typeDefinitionAst as TypeDefinitionAst).Type.GetTypeInfo().BaseType; + targetTypeConstraint = (typeDefinitionAst as TypeDefinitionAst).Type.BaseType; } else { diff --git a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs index 8f6e40af6ee..186f32abfff 100644 --- a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs +++ b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs @@ -290,7 +290,7 @@ public override AstVisitAction VisitTrap(TrapStatementAst trapStatementAst) 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 @@ private Tuple> AnalyzeImpl(ExpressionAst exprAst) { _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 @@ private static bool MustBeBoxed(Type type) // $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) diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index 3d376e52556..37e15287b63 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -410,7 +410,7 @@ internal static DynamicMetaObject UpdateComRestrictionsForPsObject(this DynamicM 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 @@ public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, Dyna 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 @@ internal static bool IsStaticTypePossiblyEnumerable(Type type) 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 @@ public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObje if (enumerable == null) { var bindingResult = PSVariableAssignmentBinder.Get().Bind(target, Utils.EmptyArray()); - var restrictions = target.LimitType.GetTypeInfo().IsValueType + var restrictions = target.LimitType.IsValueType ? bindingResult.Restrictions : target.PSGetTypeRestriction(); return (new DynamicMetaObject( @@ -1871,7 +1871,7 @@ public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObje { 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 @@ public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObje } var type = value.GetType(); - if (type.GetTypeInfo().IsValueType) + if (type.IsValueType) { var expr = target.Expression; var restrictions = target.PSGetTypeRestriction(); @@ -2032,7 +2032,7 @@ internal static bool IsValueTypeMutable(Type type) internal static void NoteTypeHasInstanceMemberOrTypeName(Type type) { - if (!type.GetTypeInfo().IsValueType || !IsValueTypeMutable(type)) + if (!type.IsValueType || !IsValueTypeMutable(type)) { return; } @@ -2457,7 +2457,7 @@ private DynamicMetaObject BinaryNumericOp(string methodName, DynamicMetaObject t 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 @@ private static DynamicMetaObject GetArgAsNumericOrPrimitive(DynamicMetaObject ar } 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 @@ private static DynamicMetaObject GetArgAsNumericOrPrimitive(DynamicMetaObject ar // 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 @@ private DynamicMetaObject BinaryBitwiseOp(DynamicMetaObject target, 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 @@ private DynamicMetaObject BinaryBitwiseOp(DynamicMetaObject target, 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 @@ private DynamicMetaObject BinaryComparison(DynamicMetaObject target, DynamicMeta { 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 @@ internal DynamicMetaObject BNot(DynamicMetaObject target, DynamicMetaObject erro 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 @@ internal DynamicMetaObject BNot(DynamicMetaObject target, DynamicMetaObject erro else if (typeCode <= TypeCode.UInt64) { var targetConvertType = target.LimitType; - if (targetConvertType.GetTypeInfo().IsEnum) + if (targetConvertType.IsEnum) { targetConvertType = Enum.GetUnderlyingType(targetConvertType); } @@ -3551,7 +3551,7 @@ internal DynamicMetaObject BNot(DynamicMetaObject target, DynamicMetaObject erro 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 @@ internal static Expression InvokeConverter(LanguagePrimitives.ConversionData con { 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 @@ public override DynamicMetaObject FallbackGetIndex(DynamicMetaObject target, Dyn 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 @@ internal static bool CanIndexFromEndWithNegativeIndex(DynamicMetaObject target) } // target implements IList? - 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 @@ public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, Dy 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 @@ internal static bool IsGenericDictionary(object value, ref Type genericTypeArg) 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 @@ internal static Expression GetTargetExpr(DynamicMetaObject target, Type castToTy // In case that castToType is System.Object and expr.Type is Nullable, 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 @@ public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, Dy { 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 @@ internal static DynamicMetaObject InvokeDotNetMethod( // 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); } diff --git a/src/System.Management.Automation/engine/runtime/MutableTuple.cs b/src/System.Management.Automation/engine/runtime/MutableTuple.cs index 26a45dacffa..f0e5c240b94 100644 --- a/src/System.Management.Automation/engine/runtime/MutableTuple.cs +++ b/src/System.Management.Automation/engine/runtime/MutableTuple.cs @@ -47,11 +47,12 @@ internal bool IsValueSet(int index) // 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) diff --git a/src/System.Management.Automation/engine/runtime/Operations/ClassOps.cs b/src/System.Management.Automation/engine/runtime/Operations/ClassOps.cs index 61c28727364..631323afe34 100644 --- a/src/System.Management.Automation/engine/runtime/Operations/ClassOps.cs +++ b/src/System.Management.Automation/engine/runtime/Operations/ClassOps.cs @@ -162,7 +162,7 @@ internal ScriptBlockMemberMethodWrapper(IParameterMetadataProvider ast) /// Initialization happens when the script that defines PowerShell class is executed. /// This initialization is required only if this wrapper is for a static method. /// - /// + /// /// 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 @@ internal ScriptBlockMemberMethodWrapper(IParameterMetadataProvider ast) /// 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. - /// + /// internal void InitAtRuntime() { if (_isStatic)