__CastResult __CanCastToClassNoGC(DmdType target) { if (target.__HasVariance()) { return(__CastResult.MaybeCast); } if (HasTypeEquivalence || target.HasTypeEquivalence) { return(__CastResult.MaybeCast); } else { DmdType?type = this; for (int i = 0; i < 1000; i++) { if (type == target) { return(__CastResult.CanCast); } type = type.BaseType; if (type is null) { break; } } } return(__CastResult.CannotCast); }
static Info?GetInfo(DmdType?td) { if (td is null) { return(null); } if (td.IsWindowsRuntime) { return(null); } string?scope = null, identifier = null; var tia = td.FindCustomAttribute("System.Runtime.InteropServices.TypeIdentifierAttribute", inherit: false); if (!(tia is null)) { if (tia.ConstructorArguments.Count >= 2) { if (tia.ConstructorArguments[0].ArgumentType != td.AppDomain.System_String) { return(null); } if (tia.ConstructorArguments[1].ArgumentType != td.AppDomain.System_String) { return(null); } scope = tia.ConstructorArguments[0].Value as string; identifier = tia.ConstructorArguments[1].Value as string; } }
// recursionCounter[31] = check type equivalence bool __ImplementsInterface(DmdType ifaceType, int recursionCounter) { if ((recursionCounter & int.MaxValue) >= 100) { return(false); } DmdType?type = this; for (;;) { var comparer = new DmdSigComparer(recursionCounter < 0 ? DmdMemberInfoEqualityComparer.DefaultTypeOptions | DmdSigComparerOptions.CheckTypeEquivalence : DmdMemberInfoEqualityComparer.DefaultTypeOptions); foreach (var iface in type.GetInterfaces()) { if (comparer.Equals(iface, ifaceType) || iface.__ImplementsInterface(ifaceType, recursionCounter + 1)) { return(true); } } type = type.BaseType; if (type is null) { break; } } return(false); }
DmdExceptionHandlingClause[] ReadSmallExceptionHandlers() { int count = GetNumberOfExceptionHandlers((uint)reader.ReadByte() / 12); var res = new DmdExceptionHandlingClause[count]; reader.Position += 2; for (int i = 0; i < res.Length; i++) { var flags = (DmdExceptionHandlingClauseOptions)reader.ReadUInt16(); int tryOffset = reader.ReadUInt16(); int tryLength = reader.ReadByte(); int handlerOffset = reader.ReadUInt16(); int handlerLength = reader.ReadByte(); DmdType?catchType = null; int filterOffset = 0; if (flags == DmdExceptionHandlingClauseOptions.Clause) { catchType = methodBodyResolver.ResolveType(reader.ReadInt32(), genericTypeArguments, genericMethodArguments, DmdResolveOptions.None); } else if (flags == DmdExceptionHandlingClauseOptions.Filter) { filterOffset = reader.ReadInt32(); } else { reader.Position += 4; } res[i] = new DmdExceptionHandlingClause(flags, tryOffset, tryLength, handlerOffset, handlerLength, filterOffset, catchType); } return(res); }
DmdMarshalType(string?marshalType, DmdType?marshalTypeRef, string?marshalCookie) { Value = UnmanagedType.CustomMarshaler; MarshalType = marshalType; MarshalTypeRef = marshalTypeRef; MarshalCookie = marshalCookie; }
void AddCastBegin(StringBuilder sb, DmdType?castType) { if (castType is null) { return; } sb.Append("CType("); }
void AddCastEnd(StringBuilder sb, DmdType?castType) { if (castType is null) { return; } sb.Append(')'); }
/// <summary> /// Constructor /// </summary> /// <param name="flags">Flags</param> /// <param name="tryOffset">Try offset</param> /// <param name="tryLength">Try length</param> /// <param name="handlerOffset">Handler offset</param> /// <param name="handlerLength">Handler length</param> /// <param name="filterOffset">Filter offset</param> /// <param name="catchType">Catch type</param> public DmdExceptionHandlingClause(DmdExceptionHandlingClauseOptions flags, int tryOffset, int tryLength, int handlerOffset, int handlerLength, int filterOffset, DmdType?catchType) { Flags = flags; TryOffset = tryOffset; TryLength = tryLength; HandlerOffset = handlerOffset; HandlerLength = handlerLength; this.filterOffset = filterOffset; this.catchType = catchType; }
void AddCastEnd(StringBuilder sb, DmdType?castType) { if (castType is null) { return; } sb.Append(", "); new Formatters.VisualBasic.VisualBasicTypeFormatter(new DbgStringBuilderTextWriter(sb), TypeFormatterOptions, null).Format(castType, null); sb.Append(')'); }
void AddCastBegin(StringBuilder sb, DmdType?castType) { if (castType is null) { return; } sb.Append("(("); new Formatters.CSharp.CSharpTypeFormatter(new DbgStringBuilderTextWriter(sb), TypeFormatterOptions, null).Format(castType, null); sb.Append(')'); }
public TypeState(DmdType type, string typeExpression, MemberValueNodeInfoCollection instanceMembers, MemberValueNodeInfoCollection staticMembers, TupleField[] tupleFields) { Type = type; EnumerableType = GetEnumerableType(type); Flags = GetFlags(type, tupleFields); TypeExpression = typeExpression; HasNoChildren = false; InstanceMembers = instanceMembers; StaticMembers = staticMembers; TupleFields = tupleFields; }
public override string GetExpression(string baseExpression, int index, DmdType?castType, bool addParens) { baseExpression = RemoveFormatSpecifiers(baseExpression); var sb = ObjectCache.AllocStringBuilder(); AddCastBegin(sb, castType); AddParens(sb, baseExpression, addParens); AddCastEnd(sb, castType); sb.Append('['); sb.Append(index.ToString()); sb.Append(']'); return(ObjectCache.FreeAndToString(ref sb)); }
object?ReadEnumValue(DmdType?underlyingType) { if (!(underlyingType is null)) { var typeCode = DmdType.GetTypeCode(underlyingType); if (typeCode < TypeCode.Boolean || typeCode > TypeCode.UInt64) { throw new CABlobParserException("Invalid enum underlying type"); } return(ReadValue(ToSerializationType(typeCode), underlyingType, out var realArgType)); } throw new CABlobParserException("Couldn't resolve enum type"); }
public AssemblyState(DmdAssembly assembly) { dict = new Dictionary <TypeKey, DmdType>(); var proxyAttr = assembly.AppDomain.GetWellKnownType(DmdWellKnownType.System_Diagnostics_DebuggerTypeProxyAttribute, isOptional: true); Debug2.Assert(!(proxyAttr is null)); if (!(proxyAttr is null)) { foreach (var ca in assembly.CustomAttributes) { if (ca.AttributeType != proxyAttr) { continue; } if (ca.ConstructorArguments.Count != 1) { continue; } var proxyType = DebuggerTypeProxyFinder.GetType(assembly, ca.ConstructorArguments[0].Value); if (proxyType is null) { continue; } DmdType?targetType = null; foreach (var namedArg in ca.NamedArguments) { var prop = namedArg.MemberInfo as DmdPropertyInfo; if (prop is null) { continue; } if (prop.Name == nameof(DebuggerTypeProxyAttribute.Target) || prop.Name == nameof(DebuggerTypeProxyAttribute.TargetTypeName)) { targetType = DebuggerTypeProxyFinder.GetType(assembly, namedArg.TypedValue.Value); break; } } if (targetType is null) { continue; } dict[new TypeKey(targetType)] = proxyType; } } }
bool __CanCastToClass(DmdType pTargetMT) { DmdType?type = this; if (pTargetMT.__HasVariance()) { for (int i = 0; i < 1000; i++) { if (type.IsEquivalentTo(pTargetMT)) { return(true); } if (type.__CanCastByVarianceToInterfaceOrDelegate(pTargetMT)) { return(true); } type = type.BaseType; if (type is null) { break; } } } else { for (int i = 0; i < 1000; i++) { if (type.IsEquivalentTo(pTargetMT)) { return(true); } type = type.BaseType; if (type is null) { break; } } } return(false); }
public override string GetExpression(string baseExpression, int[] indexes, DmdType?castType, bool addParens) { baseExpression = RemoveFormatSpecifiers(baseExpression); var sb = ObjectCache.AllocStringBuilder(); AddCastBegin(sb, castType); AddParens(sb, baseExpression, addParens); AddCastEnd(sb, castType); sb.Append('['); for (int i = 0; i < indexes.Length; i++) { if (i > 0) { sb.Append(','); } sb.Append(indexes[i].ToString()); } sb.Append(']'); return(ObjectCache.FreeAndToString(ref sb)); }
static DmdConstructorInfo?GetConstructor(DmdType?proxyType, DmdType targetType) { if (proxyType is null) { return(null); } if (proxyType.IsConstructedGenericType) { return(null); } var proxyTypeGenericArgs = proxyType.GetGenericArguments(); var targetTypeGenericArgs = targetType.GetGenericArguments(); if (proxyTypeGenericArgs.Count != targetTypeGenericArgs.Count) { return(null); } if (targetTypeGenericArgs.Count != 0) { proxyType = proxyType.MakeGenericType(targetTypeGenericArgs); } var ctors = proxyType.GetConstructors(DmdBindingFlags.Public | DmdBindingFlags.NonPublic | DmdBindingFlags.Instance); foreach (var ctor in ctors) { var types = ctor.GetMethodSignature().GetParameterTypes(); if (types.Count != 1) { continue; } if (!types[0].IsAssignableFrom(targetType)) { continue; } return(ctor); } return(null); }
public DbgDotNetValueNodeImpl(LanguageValueNodeFactory valueNodeFactory, DbgDotNetValueNodeProvider?childNodeProvider, DbgDotNetText name, DbgDotNetValueNodeInfo?nodeInfo, string expression, string imageName, bool isReadOnly, bool causesSideEffects, DmdType?expectedType, DmdType?actualType, string?errorMessage, DbgDotNetText valueText, ReadOnlyCollection <string>?formatSpecifiers, ColumnFormatter?columnFormatter) { if (name.Parts is null && columnFormatter is null) { throw new ArgumentException(); } this.valueNodeFactory = valueNodeFactory ?? throw new ArgumentNullException(nameof(valueNodeFactory)); this.childNodeProvider = childNodeProvider; this.nodeInfo = nodeInfo; Name = name; Value = nodeInfo?.DisplayValue; Expression = expression ?? throw new ArgumentNullException(nameof(expression)); ImageName = imageName ?? throw new ArgumentNullException(nameof(imageName)); IsReadOnly = isReadOnly; CausesSideEffects = causesSideEffects; ExpectedType = expectedType; ActualType = actualType; ErrorMessage = errorMessage; this.valueText = valueText; this.formatSpecifiers = formatSpecifiers; this.columnFormatter = columnFormatter; }
static DmdConstructorInfo?GetProxyTypeConstructor(DmdType targetType) { var proxyAttr = targetType.AppDomain.GetWellKnownType(DmdWellKnownType.System_Diagnostics_DebuggerTypeProxyAttribute, isOptional: true); Debug2.Assert(!(proxyAttr is null)); if (proxyAttr is null) { return(null); } DmdType?currentType = targetType; for (;;) { DmdConstructorInfo?proxyCtor; var ca = currentType.FindCustomAttribute(proxyAttr, inherit: false); if (!(ca is null) && ca.ConstructorArguments.Count == 1) { proxyCtor = GetConstructor(GetType(currentType.Assembly, ca.ConstructorArguments[0].Value), currentType); if (!(proxyCtor is null)) { return(proxyCtor); } } var asmState = GetAssemblyState(currentType.Assembly); proxyCtor = GetConstructor(asmState.GetProxyType(currentType), currentType); if (!(proxyCtor is null)) { return(proxyCtor); } currentType = currentType.BaseType; if (currentType is null) { return(null); } } }
public static bool IsDefined(DmdType type, string attributeTypeFullName, bool inherit) { for (DmdType?currentType = type; !(currentType is null); currentType = currentType.BaseType) { var customAttributes = currentType.GetCustomAttributesData(); for (int i = 0; i < customAttributes.Count; i++) { var ca = customAttributes[i]; if ((object)currentType != type && ca.IsPseudoCustomAttribute) { continue; } if (ca.AttributeType.FullName == attributeTypeFullName) { return(true); } } if (!inherit) { break; } } return(false); }
public static bool IsDefined(DmdType type, DmdType?attributeType, bool inherit) { for (DmdType?currentType = type; !(currentType is null); currentType = currentType.BaseType) { var customAttributes = currentType.GetCustomAttributesData(); for (int i = 0; i < customAttributes.Count; i++) { var ca = customAttributes[i]; if ((object)currentType != type && ca.IsPseudoCustomAttribute) { continue; } if (DmdMemberInfoEqualityComparer.DefaultType.Equals(ca.AttributeType, attributeType)) { return(true); } } if (!inherit) { break; } } return(false); }
public static DmdCustomAttributeData?Find(DmdMethodInfo method, DmdType?attributeType, bool inherit) { for (DmdMethodInfo?currentMethod = method; !(currentMethod is null); currentMethod = currentMethod.GetParentDefinition()) { var customAttributes = currentMethod.GetCustomAttributesData(); for (int i = 0; i < customAttributes.Count; i++) { var ca = customAttributes[i]; if ((object)currentMethod != method && ca.IsPseudoCustomAttribute) { continue; } if (DmdMemberInfoEqualityComparer.DefaultType.Equals(ca.AttributeType, attributeType)) { return(ca); } } if (!inherit) { break; } } return(null); }
public unsafe EvalArgumentResult Convert(object?value, DmdType defaultType, out DmdType type) { if (value is null) { type = defaultType; return(new EvalArgumentResult(dnEval.CreateNull())); } if (value is DbgValue dbgValue) { value = dbgValue.InternalValue; if (value is null) { type = defaultType; return(new EvalArgumentResult(dnEval.CreateNull())); } } if (value is DbgDotNetValueImpl dnValueImpl) { type = dnValueImpl.Type; var corValue = dnValueImpl.TryGetCorValue(); if (!(corValue is null)) { return(new EvalArgumentResult(corValue)); } return(new EvalArgumentResult(PredefinedEvaluationErrorMessages.InternalDebuggerError)); } DmdType?origType = null; if (value is DbgDotNetValue dnValue) { var rawValue = dnValue.GetRawValue(); if (rawValue.HasRawValue) { value = rawValue.RawValue; if (value is null) { type = defaultType; return(new EvalArgumentResult(dnEval.CreateNull())); } } origType = dnValue.Type; } if (value is string s) { type = reflectionAppDomain.System_String; var res = dnEval.CreateString(s, out var hr); if (res?.ResultOrException is CorValue corValue) { return(new EvalArgumentResult(AddValue(reflectionAppDomain.System_String, corValue))); } return(EvalArgumentResult.Create(res, hr)); } switch (Type.GetTypeCode(value.GetType())) { case TypeCode.Boolean: return(CreateByte(type = origType ?? reflectionAppDomain.System_Boolean, (byte)((bool)value ? 1 : 0))); case TypeCode.Char: return(CreateUInt16(type = origType ?? reflectionAppDomain.System_Char, (char)value)); case TypeCode.SByte: return(CreateByte(type = origType ?? reflectionAppDomain.System_SByte, (byte)(sbyte)value)); case TypeCode.Byte: return(CreateByte(type = origType ?? reflectionAppDomain.System_Byte, (byte)value)); case TypeCode.Int16: return(CreateUInt16(type = origType ?? reflectionAppDomain.System_Int16, (ushort)(short)value)); case TypeCode.UInt16: return(CreateUInt16(type = origType ?? reflectionAppDomain.System_UInt16, (ushort)value)); case TypeCode.Int32: return(CreateUInt32(type = origType ?? reflectionAppDomain.System_Int32, (uint)(int)value)); case TypeCode.UInt32: return(CreateUInt32(type = origType ?? reflectionAppDomain.System_UInt32, (uint)value)); case TypeCode.Int64: return(CreateUInt64(type = origType ?? reflectionAppDomain.System_Int64, (ulong)(long)value)); case TypeCode.UInt64: return(CreateUInt64(type = origType ?? reflectionAppDomain.System_UInt64, (ulong)value)); case TypeCode.Single: type = origType ?? reflectionAppDomain.System_Single; return(CreateSingle((float)value)); case TypeCode.Double: type = origType ?? reflectionAppDomain.System_Double; return(CreateDouble((double)value)); case TypeCode.Decimal: type = reflectionAppDomain.System_Decimal; return(CreateDecimal((decimal)value)); default: if (value.GetType() == typeof(IntPtr)) { type = origType ?? reflectionAppDomain.System_IntPtr; if (IntPtr.Size == 4) { return(CreateUInt32(reflectionAppDomain.System_IntPtr, (uint)((IntPtr)value).ToInt32())); } return(CreateUInt64(reflectionAppDomain.System_IntPtr, (ulong)((IntPtr)value).ToInt64())); } if (value.GetType() == typeof(UIntPtr)) { type = origType ?? reflectionAppDomain.System_UIntPtr; if (IntPtr.Size == 4) { return(CreateUInt32(reflectionAppDomain.System_UIntPtr, ((UIntPtr)value).ToUInt32())); } return(CreateUInt64(reflectionAppDomain.System_UIntPtr, ((UIntPtr)value).ToUInt64())); } if (value is Array array && array.Rank == 1 && value.GetType().GetElementType() !.MakeArrayType() == value.GetType()) { switch (Type.GetTypeCode(value.GetType().GetElementType())) { case TypeCode.Boolean: var ba = (bool[])value; fixed(void *p = ba) return(ConvertSZArray(p, ba.Length, 1, reflectionAppDomain.System_Boolean, out type)); case TypeCode.Char: var bc = (char[])value; fixed(void *p = bc) return(ConvertSZArray(p, bc.Length, 2, reflectionAppDomain.System_Char, out type)); case TypeCode.SByte: var bsb = (sbyte[])value; fixed(void *p = bsb) return(ConvertSZArray(p, bsb.Length, 1, reflectionAppDomain.System_SByte, out type)); case TypeCode.Byte: var bb = (byte[])value; fixed(void *p = bb) return(ConvertSZArray(p, bb.Length, 1, reflectionAppDomain.System_Byte, out type)); case TypeCode.Int16: var bi16 = (short[])value; fixed(void *p = bi16) return(ConvertSZArray(p, bi16.Length, 2, reflectionAppDomain.System_Int16, out type)); case TypeCode.UInt16: var bu16 = (ushort[])value; fixed(void *p = bu16) return(ConvertSZArray(p, bu16.Length, 2, reflectionAppDomain.System_UInt16, out type)); case TypeCode.Int32: var bi32 = (int[])value; fixed(void *p = bi32) return(ConvertSZArray(p, bi32.Length, 4, reflectionAppDomain.System_Int32, out type)); case TypeCode.UInt32: var bu32 = (uint[])value; fixed(void *p = bu32) return(ConvertSZArray(p, bu32.Length, 4, reflectionAppDomain.System_UInt32, out type)); case TypeCode.Int64: var bi64 = (long[])value; fixed(void *p = bi64) return(ConvertSZArray(p, bi64.Length, 8, reflectionAppDomain.System_Int64, out type)); case TypeCode.UInt64: var bu64 = (ulong[])value; fixed(void *p = bu64) return(ConvertSZArray(p, bu64.Length, 8, reflectionAppDomain.System_UInt64, out type)); case TypeCode.Single: var br4 = (float[])value; fixed(void *p = br4) return(ConvertSZArray(p, br4.Length, 4, reflectionAppDomain.System_Single, out type)); case TypeCode.Double: var br8 = (double[])value; fixed(void *p = br8) return(ConvertSZArray(p, br8.Length, 8, reflectionAppDomain.System_Double, out type)); case TypeCode.String: return(ConvertSZArray((string[])value, out type)); default: break; } } break; } type = defaultType; return(new EvalArgumentResult($"Func-eval: Can't convert type {value.GetType()} to a debugger value")); }
/// <summary> /// Finds a custom attribute /// </summary> /// <param name="attributeType">Custom attribute type</param> /// <param name="inherit">true to check custom attributes in all base classes</param> /// <returns></returns> public virtual DmdCustomAttributeData?FindCustomAttribute(DmdType?attributeType, bool inherit) => CustomAttributesHelper.Find(GetCustomAttributesData(), attributeType);
/// <summary> /// Checks if a custom attribute is present /// </summary> /// <param name="attributeType">Custom attribute type</param> /// <param name="inherit">true to check custom attributes in all base classes</param> /// <returns></returns> public virtual bool IsDefined(DmdType?attributeType, bool inherit) => CustomAttributesHelper.IsDefined(GetCustomAttributesData(), attributeType);
public abstract string GetExpression(string baseExpression, int[] indexes, DmdType?castType, bool addParens);
public abstract string GetPropertyExpression(string baseExpression, string name, DmdType?castType, bool addParens);
DmdMarshalType(VarEnum safeArraySubType, DmdType?safeArrayUserDefinedSubType) { Value = UnmanagedType.SafeArray; SafeArraySubType = safeArraySubType; SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType; }
public TypeILValueImpl(DebuggerRuntimeImpl runtime, DbgDotNetValue objValue, DmdType?type = null) { this.runtime = runtime; __objValue_DONT_USE = objValue ?? throw new ArgumentNullException(nameof(objValue)); Type = type ?? objValue.Type; }
DmdType[] CreateParameterTypes(DmdType?lastType) { var rank = SafeRank; var types = new DmdType[rank + (lastType is null ? 0 : 1)];