protected static TypeDef GetDelegateType(RPContext ctx, MethodSig sig) { TypeDef ret; if (ctx.Delegates.TryGetValue(sig, out ret)) return ret; ret = new TypeDefUser(ctx.Name.ObfuscateName(ctx.Method.DeclaringType.Namespace, RenameMode.Unicode), ctx.Name.RandomName(), ctx.Module.CorLibTypes.GetTypeRef("System", "MulticastDelegate")); ret.Attributes = TypeAttributes.NotPublic | TypeAttributes.Sealed; var ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(ctx.Module.CorLibTypes.Void, ctx.Module.CorLibTypes.Object, ctx.Module.CorLibTypes.IntPtr)); ctor.Attributes = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName; ctor.ImplAttributes = MethodImplAttributes.Runtime; ret.Methods.Add(ctor); var invoke = new MethodDefUser("Invoke", sig.Clone()); invoke.MethodSig.HasThis = true; invoke.Attributes = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot; invoke.ImplAttributes = MethodImplAttributes.Runtime; ret.Methods.Add(invoke); ctx.Module.Types.Add(ret); foreach (IDnlibDef def in ret.FindDefinitions()) { ctx.Marker.Mark(def); ctx.Name.SetCanRename(def, false); } ctx.Delegates[sig] = ret; return ret; }
public static MethodDefOptions Create(UTF8String name, MethodSig methodSig) { return new MethodDefOptions { ImplAttributes = MethodImplAttributes.IL | MethodImplAttributes.Managed, Attributes = MethodAttributes.Public | MethodAttributes.ReuseSlot | MethodAttributes.HideBySig | (methodSig.HasThis ? 0 : MethodAttributes.Static), Name = name, MethodSig = methodSig, ImplMap = null, }; }
MethodSig ResolveGenericArgs(MethodSig sig, MethodSig old) { sig.RetType = ResolveGenericArgs(old.RetType); foreach (TypeSig p in old.Params) sig.Params.Add(ResolveGenericArgs(p)); sig.GenParamCount = old.GenParamCount; if (sig.ParamsAfterSentinel != null) { foreach (TypeSig p in old.ParamsAfterSentinel) sig.ParamsAfterSentinel.Add(ResolveGenericArgs(p)); } return sig; }
MethodSig ResolveGenericArgs(MethodSig sig) { if (sig == null) return null; if (!recursionCounter.Increment()) return null; MethodSig result = ResolveGenericArgs(new MethodSig(sig.GetCallingConvention()), sig); recursionCounter.Decrement(); return result; }
public CorPropertyInfo(CorType ownerType, uint token, uint getToken, uint setToken, string name, MethodSig getSig, MethodSig setSig, MethodAttributes getMethodAttributes, DebuggerBrowsableState? debuggerBrowsableState) { this.OwnerType = ownerType; this.Token = token; this.GetToken = getToken; this.SetToken = setToken; this.Name = name; this.GetSig = getSig; this.SetSig = setSig; this.GetMethodAttributes = getMethodAttributes; this.DebuggerBrowsableState = debuggerBrowsableState; }
public MethodDefOptions(MethodDef method) { this.ImplAttributes = method.ImplAttributes; this.Attributes = method.Attributes; this.Name = method.Name; this.MethodSig = method.MethodSig; this.ImplMap = method.ImplMap; this.CustomAttributes.AddRange(method.CustomAttributes); this.DeclSecurities.AddRange(method.DeclSecurities); this.ParamDefs.AddRange(method.ParamDefs); this.GenericParameters.AddRange(method.GenericParameters); this.Overrides.AddRange(method.Overrides); }
/// <summary> /// Resolves the method signature with the specified generic arguments. /// </summary> /// <param name="methodSig">The method signature.</param> /// <param name="typeGenArgs">The type generic arguments.</param> /// <returns>Resolved method signature.</returns> /// <exception cref="System.ArgumentException">No generic arguments to resolve.</exception> public static MethodSig Resolve(MethodSig methodSig, IList<TypeSig> typeGenArgs) { if (typeGenArgs == null) throw new ArgumentException("No generic arguments to resolve."); var resolver = new GenericArgumentResolver(); resolver.genericArguments = new GenericArguments(); resolver.recursionCounter = new RecursionCounter(); if (typeGenArgs != null) resolver.genericArguments.PushTypeArgs(typeGenArgs); return resolver.ResolveGenericArgs(methodSig); }
public MethodDefOptions(MethodDef method) { ImplAttributes = method.ImplAttributes; Attributes = method.Attributes; SemanticsAttributes = method.SemanticsAttributes; RVA = method.RVA; Name = method.Name; MethodSig = method.MethodSig; ImplMap = method.ImplMap; CustomAttributes.AddRange(method.CustomAttributes); DeclSecurities.AddRange(method.DeclSecurities); ParamDefs.AddRange(method.ParamDefs); GenericParameters.AddRange(method.GenericParameters); Overrides.AddRange(method.Overrides); }
public static string GetMethodSigFullName(MethodSig methodSig) { if (methodSig == null) return string.Empty; var sb = new StringBuilder(); sb.Append(FullNameCreator.FullName(methodSig.RetType, false)); sb.Append("("); PrintArgs(sb, methodSig.Params, true); if (methodSig.ParamsAfterSentinel != null) { if (methodSig.Params.Count > 0) sb.Append(","); sb.Append("...,"); PrintArgs(sb, methodSig.ParamsAfterSentinel, false); } sb.Append(")"); return sb.ToString(); }
public MethodWriter(CSharpLanguage lang, ITextOutput output, IMethod method) { this.lang = lang; this.output = output; this.typeGenericParams = null; this.methodGenericParams = null; this.methodSig = method.MethodSig; this.md = method as MethodDef; var ms = method as MethodSpec; var mr = method as MemberRef; if (ms != null) { var ts = ms.Method == null ? null : ms.Method.DeclaringType as TypeSpec; if (ts != null) { var gp = ts.TypeSig.RemovePinnedAndModifiers() as GenericInstSig; if (gp != null) typeGenericParams = gp.GenericArguments; } var gsSig = ms.GenericInstMethodSig; if (gsSig != null) methodGenericParams = gsSig.GenericArguments; this.md = ms.Method.ResolveMethodDef(); } else if (mr != null) { var ts = mr.DeclaringType as TypeSpec; if (ts != null) { var gp = ts.TypeSig.RemovePinnedAndModifiers() as GenericInstSig; if (gp != null) typeGenericParams = gp.GenericArguments; } this.md = mr.ResolveMethod(); } if (typeGenericParams != null || methodGenericParams != null) this.methodSig = GenericArgumentResolver.Resolve(methodSig, typeGenericParams, methodGenericParams); }
/// <summary> /// Returns the full name of a method /// </summary> /// <param name="declaringType">Declaring type full name or <c>null</c> if none</param> /// <param name="name">Name of method or <c>null</c> if none</param> /// <param name="methodSig">Method signature</param> /// <returns>Method full name</returns> public static string MethodFullName(string declaringType, string name, MethodSig methodSig) { return MethodFullName(declaringType, name, methodSig, null, null); }
/// <summary> /// Returns the full name of a method /// </summary> /// <param name="declaringType">Declaring type full name or <c>null</c> if none</param> /// <param name="name">Name of method or <c>null</c> if none</param> /// <param name="methodSig">Method signature</param> /// <returns>Method full name</returns> public static string MethodFullName(string declaringType, UTF8String name, MethodSig methodSig) { return MethodFullName(declaringType, UTF8String.ToSystemString(name), methodSig, null, null); }
public static void Write(this ITextOutput writer, MethodSig sig, IMethod method = null) { if (sig == null && method != null) sig = method.MethodSig; if (sig == null) return; if (sig.ExplicitThis) { writer.Write("instance", TextTokenType.Keyword); writer.WriteSpace(); writer.Write("explicit", TextTokenType.Keyword); writer.WriteSpace(); } else if (sig.HasThis) { writer.Write("instance", TextTokenType.Keyword); writer.WriteSpace(); } sig.RetType.WriteTo(writer, ILNameSyntax.SignatureNoNamedTypeParameters); writer.WriteSpace(); if (method != null) { if (method.DeclaringType != null) { method.DeclaringType.WriteTo(writer, ILNameSyntax.TypeName); writer.Write("::", TextTokenType.Operator); } MethodDef md = method as MethodDef; if (md != null && md.IsCompilerControlled) { writer.WriteReference(Escape(method.Name + "$PST" + method.MDToken.ToInt32().ToString("X8")), method, TextTokenHelper.GetTextTokenType(method)); } else { writer.WriteReference(Escape(method.Name), method, TextTokenHelper.GetTextTokenType(method)); } } MethodSpec gim = method as MethodSpec; if (gim != null && gim.GenericInstMethodSig != null) { writer.Write('<', TextTokenType.Operator); for (int i = 0; i < gim.GenericInstMethodSig.GenericArguments.Count; i++) { if (i > 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } gim.GenericInstMethodSig.GenericArguments[i].WriteTo(writer); } writer.Write('>', TextTokenType.Operator); } writer.Write("(", TextTokenType.Operator); var parameters = sig.GetParameters(); for(int i = 0; i < parameters.Count; ++i) { if (i > 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } parameters[i].WriteTo(writer, ILNameSyntax.SignatureNoNamedTypeParameters); } writer.Write(")", TextTokenType.Operator); }
/// <summary> /// Constructor /// </summary> /// <param name="module">Owner module</param> /// <param name="name">Name of method ref</param> /// <param name="sig">Method sig</param> public MemberRefUser(ModuleDef module, UTF8String name, MethodSig sig) : this(module, name, sig, null) { }
/// <summary> /// Constructor /// </summary> /// <param name="methodSig">A method sig</param> public StandAloneSigUser(MethodSig methodSig) { this.signature = methodSig; }
/// <summary> /// Constructor /// </summary> /// <param name="name">Method name</param> /// <param name="methodSig">Method sig</param> /// <param name="flags">Flags</param> public MethodDefUser(UTF8String name, MethodSig methodSig, MethodAttributes flags) : this(name, methodSig, 0, flags) { }
private MethodDef CreateBridge(RPContext ctx, TypeDef delegateType, FieldDef field, MethodSig sig) { var method = new MethodDefUser(ctx.Name.RandomName(), sig); method.Attributes = MethodAttributes.PrivateScope | MethodAttributes.Static; method.ImplAttributes = MethodImplAttributes.Managed | MethodImplAttributes.IL; method.Body = new CilBody(); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldsfld, field)); for (int i = 0; i < method.Parameters.Count; i++) method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, method.Parameters[i])); method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, delegateType.FindMethod("Invoke"))); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); delegateType.Methods.Add(method); ctx.Context.Registry.GetService<IMarkerService>().Mark(method); ctx.Name.SetCanRename(method, false); return method; }
void ISimpleILPrinter.Write(IDecompilerOutput output, MethodSig sig) => output.Write(sig);
private static bool MatchMethod(MethodDef mCandidate, MethodSig mCandidateSig, MethodDef mMethod) { if (mCandidate == null || mCandidateSig == null || mMethod == null) return false; if (mCandidate.Name != mMethod.Name) return false; if (mCandidate.HasOverrides) return false; if (mCandidate.IsSpecialName != mMethod.IsSpecialName) return false; if (mCandidate.HasGenericParameters || mMethod.HasGenericParameters) { if (!mCandidate.HasGenericParameters || !mMethod.HasGenericParameters || mCandidate.GenericParameters.Count != mMethod.GenericParameters.Count) return false; } if (mMethod.MethodSig == null || mCandidateSig.Params.Count != mMethod.MethodSig.Params.Count) return false; if (mCandidate.Parameters.Count != mMethod.Parameters.Count) return false; for (int i = 0; i < mCandidate.Parameters.Count; i++) { var p1 = mCandidate.Parameters[i]; var p2 = mMethod.Parameters[i]; if (p1.IsHiddenThisParameter != p2.IsHiddenThisParameter) return false; if (p1.IsHiddenThisParameter) continue; var pd1 = p1.ParamDef ?? new ParamDefUser(); var pd2 = p2.ParamDef ?? new ParamDefUser(); if (pd1.IsIn != pd2.IsIn || pd1.IsOut != pd2.IsOut) return false; } return new SigComparer().Equals(mCandidateSig.Params, mMethod.MethodSig.Params); }
private static bool MatchProperty(PropertyDef mCandidate, MethodSig mCandidateSig, PropertyDef mProperty) { if (mCandidate == null || mCandidateSig == null || mProperty == null) return false; if (mCandidate.Name != mProperty.Name) return false; var accMeth = mCandidate.GetMethod ?? mCandidate.SetMethod; if (accMeth != null && accMeth.HasOverrides) return false; if (mProperty.PropertySig == null || mCandidateSig.GenParamCount != mProperty.PropertySig.GenParamCount) return false; return new SigComparer().Equals(mCandidateSig.Params, mProperty.PropertySig.Params); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Method name</param> /// <param name="methodSig">Method sig</param> public MethodDefUser(UTF8String name, MethodSig methodSig) : this(name, methodSig, 0, 0) { }
/// <summary> /// Returns the full name of a method sig /// </summary> /// <param name="methodSig">Method sig</param> /// <returns>Method sig full name</returns> public static string MethodSigFullName(MethodSig methodSig) { var fnc = new FullNameCreator(false, null); fnc.CreateMethodFullName(null, null, methodSig, null); return fnc.Result; }
/// <summary> /// Resolves the generic parameters in the specified method signature. /// </summary> /// <param name="methodSig">The method signature.</param> /// <returns>Resolved method signature.</returns> public MethodSig Resolve(MethodSig methodSig) { return ResolveGenericArgs(methodSig); }
/// <summary> /// Constructor /// </summary> /// <param name="module">Owner module</param> /// <param name="name">Name of method ref</param> /// <param name="sig">Method sig</param> /// <param name="class">Owner of method</param> public MemberRefUser(ModuleDef module, UTF8String name, MethodSig sig, IMemberRefParent @class) { this.module = module; this.name = name; this.@class = @class; this.signature = sig; }
/// <summary> /// Returns the full name of a method /// </summary> /// <param name="declaringType">Declaring type full name or <c>null</c> if none</param> /// <param name="name">Name of method or <c>null</c> if none</param> /// <param name="methodSig">Method signature</param> /// <param name="typeGenArgs">Type generic arguments or <c>null</c> if none</param> /// <param name="methodGenArgs">Method generic arguments or <c>null</c> if none</param> /// <returns>Method full name</returns> public static string MethodFullName(string declaringType, string name, MethodSig methodSig, IList<TypeSig> typeGenArgs, IList<TypeSig> methodGenArgs) { var fnc = new FullNameCreator(false, null); if (typeGenArgs != null || methodGenArgs != null) fnc.genericArguments = new GenericArguments(); if (typeGenArgs != null) fnc.genericArguments.PushTypeArgs(typeGenArgs); if (methodGenArgs != null) fnc.genericArguments.PushMethodArgs(methodGenArgs); fnc.CreateMethodFullName(declaringType, name, methodSig); return fnc.Result; }
internal VTableSignature(TypeSig iface, MethodSig sig, string name) { if (iface.ScopeType.ResolveTypeDefThrow().IsInterface) InterfaceType = iface; else InterfaceType = null; MethodSig = sig; Name = name; }
/// <summary> /// Find a MethodDef from a declaring type and some MethodData. Will generate /// a list of possible MethodSigs and check against each of them, returning the /// first-found MethodDef that matches the method name and signature. /// </summary> /// <param name="declaringType">Declaring type</param> /// <param name="data">MethodData</param> /// <param name="detectedSig">The detected MethodSig</param> /// <returns>MethodDef if found, null if none found</returns> MethodDef FindMethodCheckBaseType(ITypeDefOrRef declaringType, MethodData data, out MethodSig detectedSig) { detectedSig = null; TypeDef declaringDef = declaringType.ResolveTypeDef(); if (declaringDef == null) return null; MethodDef method = null; MethodSig methodSig = GetMethodSig(data); var possibleSigs = PossibleMethodSigs(declaringType, methodSig, data); detectedSig = possibleSigs.FirstOrDefault(sig => { return (method = declaringDef.FindMethodCheckBaseType(data.Name, sig)) != null; }); return method; }
/// <summary> /// Returns the full name of a method /// </summary> /// <param name="declaringType">Declaring type full name or <c>null</c> if none</param> /// <param name="name">Name of method or <c>null</c> if none</param> /// <param name="methodSig">Method signature</param> /// <param name="typeGenArgs">Type generic arguments or <c>null</c> if none</param> /// <param name="methodGenArgs">Method generic arguments or <c>null</c> if none</param> /// <returns>Method full name</returns> public static string MethodFullName(string declaringType, UTF8String name, MethodSig methodSig, IList<TypeSig> typeGenArgs, IList<TypeSig> methodGenArgs) { return MethodFullName(declaringType, UTF8String.ToSystemString(name), methodSig, typeGenArgs, methodGenArgs); }
IList<MethodSig> PossibleMethodSigs(ITypeDefOrRef declaringType, MethodSig sig, MethodData data) { // Setup generic types IList<TypeSig> typeGenerics = new List<TypeSig>(), methodGenerics = new List<TypeSig>(); // Add all declaring spec generic types TypeSpec declaringSpec = declaringType as TypeSpec; if (declaringSpec != null) { var genericInstSig = declaringSpec.TryGetGenericInstSig(); foreach (var garg in genericInstSig.GenericArguments) typeGenerics.Add(garg); } // Add all method generic types if (data.HasGenericArguments) { foreach (var operand in data.GenericArguments) { var gtype = this.ResolveType_NoLock(operand.Position); methodGenerics.Add(gtype.ToTypeSig()); } } // Todo: Combinations factoring in the possibility that return type might match // a generic type TypeSig returnType = ResolveType(data.ReturnType); IList<TypeSig> returnTypes = GenericUtils.PossibleTypeSigs(returnType, typeGenerics, methodGenerics); TypeSig[] paramTypes = new TypeSig[data.Parameters.Length]; for (Int32 i = 0; i < paramTypes.Length; i++) { paramTypes[i] = ResolveType(data.Parameters[i]); } UInt32 genericTypeCount = (UInt32)data.GenericArguments.Length; IList<MethodSig> signatures = new List<MethodSig>(); var paramCombos = GenericUtils.CreateGenericParameterCombinations(paramTypes, typeGenerics, methodGenerics); foreach (var rType in returnTypes) { foreach (var combo in paramCombos) { var paramCombo = combo.ToArray(); MethodSig methodSig; if (genericTypeCount == 0) { if (data.IsStatic) methodSig = MethodSig.CreateStatic(rType, paramCombo); else methodSig = MethodSig.CreateInstance(rType, paramCombo); } else { if (data.IsStatic) methodSig = MethodSig.CreateStaticGeneric(genericTypeCount, rType, paramCombo); else methodSig = MethodSig.CreateInstanceGeneric(genericTypeCount, rType, paramCombo); } signatures.Add(methodSig); } } return signatures; }
/// <summary> /// Returns the full name of a method /// </summary> /// <param name="declaringType">Declaring type full name or <c>null</c> if none</param> /// <param name="name">Name of method or <c>null</c> if none</param> /// <param name="methodSig">Method signature</param> /// <param name="typeGenArgs">Type generic arguments or <c>null</c> if none</param> /// <returns>Method full name</returns> public static string MethodFullName(string declaringType, string name, MethodSig methodSig, IList<TypeSig> typeGenArgs) { return MethodFullName(declaringType, name, methodSig, typeGenArgs, null); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Method name</param> /// <param name="methodSig">Method sig</param> /// <param name="implFlags">Impl flags</param> public MethodDefUser(UTF8String name, MethodSig methodSig, MethodImplAttributes implFlags) : this(name, methodSig, implFlags, 0) { }
private static void ReMethodSig(MethodSig sig, ModuleDef module) { var retType = sig.RetType; var ts = ReferenceType(sig.RetType, module); if (ts != null) sig.RetType = ts; for (var i = 0; i < sig.Params.Count; ++i) { ts = ReferenceType(sig.Params[i], module); if (ts == null) continue; sig.Params[i] = ts; } }
public CorMethodInfo(CorType ownerType, uint token, string name, MethodSig methodSig, MethodAttributes attrs, MethodImplAttributes implAttrs, bool compilerGeneratedAttribute) { this.OwnerType = ownerType; this.Token = token; this.Name = name; this.MethodSig = methodSig; this.MethodAttributes = attrs; this.MethodImplAttributes = implAttrs; this.CompilerGeneratedAttribute = compilerGeneratedAttribute; }