8000 Clean up 'GetTypeInfo()' calls under engine/parser by daxian-dbw · Pull Request #6636 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Clean up 'GetTypeInfo()' calls under engine/parser #6636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 72 additions & 87 deletions src/System.Management.Automation/engine/CoreAdapter.cs

Large diffs are not rendered by default.

58 changes: 26 additions & 32 deletions src/System.Management.Automation/engine/LanguagePrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ internal EnumerableTWrapper(object enumerable, Type enumerableType)
private void CreateGetEnumerator()
{
_getEnumerator = new DynamicMethod("GetEnumerator", typeof(object),
new Type[] { typeof(object) }, typeof(LanguagePrimitives).GetTypeInfo().Module, true);
new Type[] { typeof(object) }, typeof(LanguagePrimitives).Module, true);

ILGenerator emitter = _getEnumerator.GetILGenerator();

Expand All @@ -399,8 +399,7 @@ private static IEnumerable GetEnumerableFromIEnumerableT(object obj)
{
foreach (Type i in obj.GetType().GetInterfaces())
{
TypeInfo typeinfo = i.GetTypeInfo();
if (typeinfo.IsGenericType && typeinfo.GetGenericTypeDefinition() == typeof(IEnumerable<>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return new EnumerableTWrapper(obj, i);
}
Expand Down Expand Up @@ -1211,7 +1210,7 @@ internal static bool IsCimIntrinsicScalarType(Type type)
// - TimeSpan part of "datetime"
// - <classname> ref
TypeCode typeCode = LanguagePrimitives.GetTypeCode(type);
if (LanguagePrimitives.IsCimIntrinsicScalarType(typeCode) && !type.GetTypeInfo().IsEnum)
if (LanguagePrimitives.IsCimIntrinsicScalarType(typeCode) && !type.IsEnum)
{
return true;
}
Expand Down Expand Up @@ -1281,8 +1280,7 @@ internal static void DoConversionsForSetInGenericDictionary(IDictionary dictiona
{
foreach (Type i in dictionary.GetType().GetInterfaces())
{
TypeInfo typeInfo = i.GetTypeInfo();
if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(IDictionary<,>))
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
{
// If we get here, we know the target implements IDictionary. We will assume
// that the non-generic implementation of the indexer property just forwards
Expand Down Expand Up @@ -1366,7 +1364,7 @@ internal static object GetConverter(Type type, TypeTable backupTypeTable)
return typesXmlConverter;
}

var typeConverters = type.GetTypeInfo().GetCustomAttributes(typeof(TypeConverterAttribute), false);
var typeConverters = type.GetCustomAttributes(typeof(TypeConverterAttribute), false);
foreach (var typeConverter in typeConverters)
{
var attr = (TypeConverterAttribute)typeConverter;
Expand Down Expand Up @@ -1829,7 +1827,7 @@ private static EnumHashEntry GetEnumHashEntry(Type enumType)

// See if the [Flag] attribute is set on this type...
// MemberInfo.GetCustomAttributes returns IEnumerable<Attribute> in CoreCLR.
bool hasFlagsAttribute = enumType.GetTypeInfo().GetCustomAttributes(typeof(FlagsAttribute), false).Any();
bool hasFlagsAttribute = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any();

returnValue = new EnumHashEntry(Enum.GetNames(enumType), values, allValues, hasNegativeValue, hasFlagsAttribute);
s_enumTable.Add(enumType, returnValue);
Expand All @@ -1839,7 +1837,7 @@ private static EnumHashEntry GetEnumHashEntry(Type enumType)

public override bool CanConvertFrom(object sourceValue, Type destinationType)
{
return sourceValue is string && destinationType.GetTypeInfo().IsEnum;
return sourceValue is string && destinationType.IsEnum;
}

/// <summary>
Expand Down Expand Up @@ -1967,7 +1965,7 @@ protected static object BaseConvertFrom(object sourceValue, Type destinationType
ExtendedTypeSystem.InvalidCastException,
sourceValue, ObjectToTypeNameString(sourceValue), destinationType);
}
Diagnostics.Assert(destinationType.GetTypeInfo().IsEnum, "EnumSingleTypeConverter is only applied to enumerations");
Diagnostics.Assert(destinationType.IsEnum, "EnumSingleTypeConverter is only applied to enumerations");
if (sourceValueString.Length == 0)
{
throw new PSInvalidCastException("InvalidCastEnumFromEmptyString", null,
Expand Down Expand Up @@ -4579,7 +4577,7 @@ internal static Tuple<string, string> GetInvalidCastMessages(object valueToConve
string errorId, errorMsg;
if (PSObject.Base(valueToConvert) == null)
{
if (resultType.GetTypeInfo().IsEnum)
if (resultType.IsEnum)
{
typeConversion.WriteLine("Issuing an error message about not being able to convert null to an Enum type.");
// a nice error message specifically for null being converted to enum
Expand Down Expand Up @@ -4649,11 +4647,11 @@ private static ConversionData FigureLanguageConversion(Type fromType, Type toTyp
{
converter = LanguagePrimitives.ConvertIListToBool;
}
else if (fromType.GetTypeInfo().IsEnum)
else if (fromType.IsEnum)
{
converter = LanguagePrimitives.CreateNumericToBoolConverter(fromType);
}
else if (fromType.GetTypeInfo().IsValueType)
else if (fromType.IsValueType)
{
converter = LanguagePrimitives.ConvertValueToBool;
}
Expand All @@ -4666,7 +4664,7 @@ private static ConversionData FigureLanguageConversion(Type fromType, Type toTyp

if (toType == typeof(string))
{
Dbg.Assert(!LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(fromType)) || fromType.GetTypeInfo().IsEnum,
Dbg.Assert(!LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(fromType)) || fromType.IsEnum,
"Number to string should be cached on initialization of cache table");
return CacheConversion<string>(fromType, toType, LanguagePrimitives.ConvertNonNumericToString, ConversionRank.ToString);
}
Expand Down Expand Up @@ -4769,7 +4767,7 @@ private static ConversionData FigureLanguageConversion(Type fromType, Type toTyp
}

TypeCode fromTypeCode = LanguagePrimitives.GetTypeCode(fromType);
if (LanguagePrimitives.IsInteger(fromTypeCode) && toType.GetTypeInfo().IsEnum)
if (LanguagePrimitives.IsInteger(fromTypeCode) && toType.IsEnum)
{
return CacheConversion<object>(fromType, toType, LanguagePrimitives.ConvertIntegerToEnum, ConversionRank.Language);
}
Expand Down Expand Up @@ -4852,7 +4850,7 @@ private static PSConverter<object> FigureStaticCreateMethodConversion(Type fromT

private static PSConverter<object> FigureParseConversion(Type fromType, Type toType)
{
if (toType.GetTypeInfo().IsEnum)
if (toType.IsEnum)
{
if (fromType == typeof(string))
{
Expand Down Expand Up @@ -4923,8 +4921,7 @@ private static PSConverter<object> FigureParseConversion(Type fromType, Type toT
internal static Tuple<PSConverter<object>, ConversionRank> FigureIEnumerableConstructorConversion(Type fromType, Type toType)
{
// Win8: 653180. If toType is an Abstract type then we cannot construct it anyway. So, bailing out fast.
TypeInfo toTypeInfo = toType.GetTypeInfo();
if (toTypeInfo.IsAbstract == true)
if (toType.IsAbstract == true)
{
return null;
}
Expand All @@ -4936,7 +4933,7 @@ internal static Tuple<PSConverter<object>, ConversionRank> FigureIEnumerableCons
Type elementType = null;
ConstructorInfo resultConstructor = null;

if (toTypeInfo.IsGenericType && !toTypeInfo.ContainsGenericParameters &&
if (toType.IsGenericType && !toType.ContainsGenericParameters &&
(typeof(IList).IsAssignableFrom(toType) ||
typeof(ICollection).IsAssignableFrom(toType) ||
typeof(IEnumerable).IsAssignableFrom(toType)))
Expand Down Expand Up @@ -5077,7 +5074,7 @@ internal static PSConverter<object> FigureConstructorConversion(Type fromType, T
{
ParameterInfo[] targetParams = resultConstructor.GetParameters();
Type targetParamType = targetParams[0].ParameterType;
bool useExplicitConversion = targetParamType.GetTypeInfo().IsValueType && fromType != targetParamType && Nullable.GetUnderlyingType(targetParamType) == null;
bool useExplicitConversion = targetParamType.IsValueType && fromType != targetParamType && Nullable.GetUnderlyingType(targetParamType) == null;
converter.TargetCtorLambda = CreateCtorLambdaClosure<object, object>(resultConstructor, targetParamType, useExplicitConversion);
}
catch (Exception e)
Expand Down Expand Up @@ -5105,8 +5102,7 @@ private static bool IsIntegralType(Type type)

internal static PSConverter<object> FigurePropertyConversion(Type fromType, Type toType, ref ConversionRank rank)
{
TypeInfo toTypeInfo = toType.GetTypeInfo();
if ((!typeof(PSObject).IsAssignableFrom(fromType)) || (toTypeInfo.IsAbstract))
if ((!typeof(PSObject).IsAssignableFrom(fromType)) || (toType.IsAbstract))
{
return null;
}
Expand All @@ -5125,7 +5121,7 @@ internal static PSConverter<object> FigurePropertyConversion(Type fromType, Type
typeConversion.WriteLine("Exception finding Constructor: \"{0}\".", e.Message);
}

if (toConstructor == null && !toTypeInfo.IsValueType)
if (toConstructor == null && !toType.IsValueType)
{
return null;
}
Expand Down Expand Up @@ -5195,7 +5191,7 @@ private static bool TypeConverterPossiblyExists(Type type)
}

// GetCustomAttributes returns IEnumerable<Attribute> in CoreCLR
var typeConverters = type.GetTypeInfo().GetCustomAttributes(typeof(TypeConverterAttribute), false);
var typeConverters = type.GetCustomAttributes(typeof(TypeConverterAttribute), false);
if (typeConverters.Any())
{
return true;
Expand Down Expand Up @@ -5270,17 +5266,16 @@ internal static ConversionData FigureConversion(Type fromType, Type toType)
}
}
A92E }
#if CORECLR

// Assemblies in CoreCLR might not allow reflection execution on their internal types.
TypeInfo typeInfo = toType.GetTypeInfo();
if (!TypeResolver.IsPublic(typeInfo) && DotNetAdapter.DisallowPrivateReflection(typeInfo))
if (!TypeResolver.IsPublic(toType) && DotNetAdapter.DisallowPrivateReflection(toType))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a breaking change with 6.0 GA?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think this is a breaking change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that before the change we can use reflection get access to private members. If so it is formally a breaking change. My request is only set the label on the PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there is no behavioral change ... The removed if/def is #if CORECLR ... #endif, so it has always been this way. Did you misread it to #if !CORECLR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry.

{
// If the type is non-public and reflection execution is not allowed on it, then we return
// 'ConvertNoConversion', because we won't be able to invoke constructor, methods or set
// properties on an instance of this type through reflection.
return CacheConversion(fromType, toType, ConvertNoConversion, ConversionRank.None);
}
#endif

PSConverter<object> valueDependentConversion = null;
ConversionRank valueDependentRank = ConversionRank.None;
ConversionData conversionData = FigureLanguageConversion(fromType, toType, out valueDependentConversion, out valueDependentRank);
Expand All @@ -5305,7 +5300,7 @@ internal static ConversionData FigureConversion(Type fromType, Type toType)
{
if (typeof(IConvertible).IsAssignableFrom(fromType))
{
if (LanguagePrimitives.IsNumeric(GetTypeCode(fromType)) && !fromType.GetTypeInfo().IsEnum)
if (LanguagePrimitives.IsNumeric(GetTypeCode(fromType)) && !fromType.IsEnum)
{
if (!toType.IsArray)
{
Expand All @@ -5332,8 +5327,7 @@ internal static ConversionData FigureConversion(Type fromType, Type toType)
// So, we need to check only for the first condition
ConstructorInfo resultConstructor = toType.GetConstructor(PSTypeExtensions.EmptyTypes);

TypeInfo toTypeInfo = toType.GetTypeInfo();
if (resultConstructor != null || (toTypeInfo.IsValueType && !toTypeInfo.IsPrimitive))
if (resultConstructor != null || (toType.IsValueType && !toType.IsPrimitive))
{
ConvertViaNoArgumentConstructor noArgumentConstructorConverter = new ConvertViaNoArgumentConstructor(resultConstructor, toType);
converter = noArgumentConstructorConverter.Convert;
Expand Down Expand Up @@ -5416,7 +5410,7 @@ private static ConversionData FigureConversionFromNull(Type toType)
{
return CacheConversion<object>(typeof(Null), toType, LanguagePrimitives.ConvertNullToNullable, ConversionRank.NullToValue);
}
else if (!toType.GetTypeInfo().IsValueType)
else if (!toType.IsValueType)
{
return CacheConversion<object>(typeof(Null), toType, LanguagePrimitives.ConvertNullToRef, ConversionRank.NullToRef);
}
Expand Down
18 changes: 9 additions & 9 deletions src/System.Management.Automation/engine/parser/PSType.cs
DCD2
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private static CustomAttributeBuilder GetAttributeBuilder(Parser parser, Attribu
Diagnostics.Assert(attributeType != null, "Semantic checks should have verified attribute type exists");

Diagnostics.Assert(
attributeType.GetTypeInfo().GetCustomAttribute<AttributeUsageAttribute>(true) == null ||
(attributeType.GetTypeInfo().GetCustomAttribute<AttributeUsageAttribute>(true).ValidOn & attributeTargets) != 0, "Semantic checks should have verified attribute usage");
attributeType.GetCustomAttribute<AttributeUsageAttribute>(true) == null ||
(attributeType.GetCustomAttribute<AttributeUsageAttribute>(true).ValidOn & attributeTargets) != 0, "Semantic checks should have verified attribute usage");

var positionalArgs = new object[attributeAst.PositionalArguments.Count];
var cvv = new ConstantValueVisitor { AttributeArgument = false };
Expand Down Expand Up @@ -330,7 +330,7 @@ private Type GetBaseTypes(Parser parser, TypeDefinitionAst typeDefinitionAst, ou
else

{
if (baseClass.GetTypeInfo().IsSealed)
if (baseClass.IsSealed)
{
parser.ReportError(firstBaseTypeAst.Extent,
nameof(ParserStrings.SealedBaseClass),
Expand All @@ -339,7 +339,7 @@ private Type GetBaseTypes(Parser parser, TypeDefinitionAst typeDefinitionAst, ou
// ignore base type if it's sealed.
baseClass = null;
}
else if (baseClass.GetTypeInfo().IsGenericType && !baseClass.IsConstructedGenericType)
else if (baseClass.IsGenericType && !baseClass.IsConstructedGenericType)
{
parser.ReportError(firstBaseTypeAst.Extent,
nameof(ParserStrings.SubtypeUnclosedGeneric),
Expand All @@ -348,7 +348,7 @@ private Type GetBaseTypes(Parser parser, TypeDefinitionAst typeDefinitionAst, ou
// ignore base type, we cannot inherit from unclosed generic.
baseClass = null;
}
else if (baseClass.GetTypeInfo().IsInterface)
else if (baseClass.IsInterface)
{
// First Ast can represent interface as well as BaseClass.
interfaces.Add(baseClass);
Expand Down Expand Up @@ -405,7 +405,7 @@ private Type GetBaseTypes(Parser parser, TypeDefinitionAst typeDefinitionAst, ou
}
else
{
if (interfaceType.GetTypeInfo().IsInterface)
if (interfaceType.IsInterface)
{
interfaces.Add(interfaceType);
}
Expand Down Expand Up @@ -620,7 +620,7 @@ private PropertyBuilder EmitPropertyIl(PropertyMemberAst propertyMemberAst, Type
setIlGen.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle")); // load current Type on stack
setIlGen.Emit(OpCodes.Ldstr, propertyMemberAst.Name); // load name of Property
setIlGen.Emit(propertyMemberAst.IsStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1); // load set value
if (type.GetTypeInfo().IsValueType)
if (type.IsValueType)
{
setIlGen.Emit(OpCodes.Box, type);
}
Expand Down Expand Up @@ -728,7 +728,7 @@ private Type[] GetParameterTypes(FunctionMemberAst functionMemberAst)
typeConstraint.TypeName.FullName);
anyErrors = true;
}
else if (paramType == typeof(void) || paramType.GetTypeInfo().IsGenericTypeDefinition)
else if (paramType == typeof(void) || paramType.IsGenericTypeDefinition)
{
_parser.ReportError(typeConstraint.Extent,
nameof(ParserStrings.TypeNotAllowedForParameter),
Expand Down Expand Up @@ -900,7 +900,7 @@ private void DefineMethodBody(
ilGenerator.Emit(OpCodes.Ldloc, local); // load array
EmitLdc(ilGenerator, i); // index to save at
EmitLdarg(ilGenerator, j); // load argument (skipping this)
if (parameterTypes[i].GetTypeInfo().IsValueType) // value types must be boxed
if (parameterTypes[i].IsValueType) // value types must be boxed
{
ilGenerator.Emit(OpCodes.Box, parameterTypes[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
}
else
{
var usage = attributeType.GetTypeInfo().GetCustomAttribute<AttributeUsageAttribute>(true);
var usage = attributeType.GetCustomAttribute<AttributeUsageAttribute>(true);
if (usage != null && (usage.ValidOn & attributeTargets) == 0)
{
_parser.ReportError(attributeAst.Extent,
Expand Down Expand Up @@ -1384,7 +1384,7 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem
// Type must be resolved, but if it's not, the error was reported by the symbol resolver.
var type = propertyMemberAst.PropertyType.TypeName.GetReflectionType();

if (type != null && (type == typeof(void) || type.GetTypeInfo().IsGenericTypeDefinition))
if (type != null && (type == typeof(void) || type.IsGenericTypeDefinition))
{
_parser.ReportError(propertyMemberAst.PropertyType.Extent,
nameof(ParserStrings.TypeNotAllowedForProperty),
Expand Down
Loading
0