public override void VisitVariableDefinitionCollection (VariableDefinitionCollection variables) { MethodBody body = variables.Container as MethodBody; if (body == null || body.LocalVarToken == 0) return; StandAloneSigTable sasTable = m_reflectReader.TableReader.GetStandAloneSigTable (); StandAloneSigRow sasRow = sasTable [(int) GetRid (body.LocalVarToken) - 1]; LocalVarSig sig = m_reflectReader.SigReader.GetLocalVarSig (sasRow.Signature); for (int i = 0; i < sig.Count; i++) { LocalVarSig.LocalVariable lv = sig.LocalVariables [i]; TypeReference varType = m_reflectReader.GetTypeRefFromSig ( lv.Type, new GenericContext (body.Method)); if (lv.ByRef) varType = new ReferenceType (varType); if ((lv.Constraint & Constraint.Pinned) != 0) varType = new PinnedType (varType); varType = m_reflectReader.GetModifierType (lv.CustomMods, varType); body.Variables.Add (new VariableDefinition ( string.Concat ("V_", i), i, body.Method, varType)); } }
void doPinnedType(PinnedType pinnedType) { bool present; if (pinnedTypes.TryGetValue(pinnedType, out present)) return; pinnedTypes[pinnedType] = true; addPinnedType(pinnedType); }
public static PinnedType ChangePinnedType(this PinnedType type, TypeReference elementType) { if (elementType != type.ElementType) { var result = new PinnedType(elementType); if (type.HasGenericParameters) SetGenericParameters(result, type.GenericParameters); return result; } return type; }
static TypeReference ImportType(TypeReference typeRef, ModuleDefinition mod, MethodReference context, Dictionary<MetadataToken, IMemberDefinition> mems) { TypeReference ret = typeRef; if (typeRef is TypeSpecification) { if (typeRef is ArrayType) { ArrayType _spec = typeRef as ArrayType; ret = new ArrayType(ImportType(_spec.ElementType, mod, context, mems)); (ret as ArrayType).Dimensions.Clear(); foreach (var i in _spec.Dimensions) (ret as ArrayType).Dimensions.Add(i); } else if (typeRef is GenericInstanceType) { GenericInstanceType _spec = typeRef as GenericInstanceType; ret = new GenericInstanceType(ImportType(_spec.ElementType, mod, context, mems)); foreach (var i in _spec.GenericArguments) (ret as GenericInstanceType).GenericArguments.Add(ImportType(i, mod, context, mems)); } else if (typeRef is OptionalModifierType) { ret = new OptionalModifierType(ImportType((typeRef as OptionalModifierType).ModifierType, mod, context, mems), ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); } else if (typeRef is RequiredModifierType) { ret = new RequiredModifierType(ImportType((typeRef as RequiredModifierType).ModifierType, mod, context, mems), ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); } else if (typeRef is ByReferenceType) ret = new ByReferenceType(ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); else if (typeRef is PointerType) ret = new PointerType(ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); else if (typeRef is PinnedType) ret = new PinnedType(ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); else if (typeRef is SentinelType) ret = new SentinelType(ImportType((typeRef as TypeSpecification).ElementType, mod, context, mems)); else throw new NotSupportedException(); } else if (typeRef is GenericParameter) { if (context == null || (typeRef as GenericParameter).Owner is TypeReference || (typeRef as GenericParameter).Position >= context.GenericParameters.Count) return typeRef; return context.GenericParameters[(typeRef as GenericParameter).Position]; } else { if (mems != null && mems.ContainsKey(typeRef.MetadataToken)) ret = mems[typeRef.MetadataToken] as TypeReference; else if (!(ret is TypeDefinition) && typeRef.Scope.Name != "Confuser.Core.Injections.dll") ret = mod.Import(ret); } return ret; }
void addPinnedType(PinnedType pt) { if (pt == null) return; addTypeSpecification(pt); }
private TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context) { ElementType etype = type.etype; if (etype > ElementType.CModOpt) { if (etype == ElementType.Sentinel) { SentinelType type6 = (SentinelType)type; return(new SentinelType(this.ImportType(type6.ElementType, context))); } if (etype == ElementType.Pinned) { PinnedType type5 = (PinnedType)type; return(new PinnedType(this.ImportType(type5.ElementType, context))); } } else { switch (etype) { case ElementType.Ptr: { PointerType type3 = (PointerType)type; return(new PointerType(this.ImportType(type3.ElementType, context))); } case ElementType.ByRef: { ByReferenceType type4 = (ByReferenceType)type; return(new ByReferenceType(this.ImportType(type4.ElementType, context))); } case ElementType.ValueType: case ElementType.Class: break; case ElementType.Var: { GenericParameter parameter = (GenericParameter)type; if (parameter.DeclaringType == null) { throw new InvalidOperationException(); } return(context.TypeParameter(parameter.DeclaringType.FullName, parameter.Position)); } case ElementType.Array: { ArrayType type9 = (ArrayType)type; ArrayType type10 = new ArrayType(this.ImportType(type9.ElementType, context)); if (!type9.IsVector) { Collection <ArrayDimension> dimensions = type9.Dimensions; Collection <ArrayDimension> collection2 = type10.Dimensions; collection2.Clear(); for (int i = 0; i < dimensions.Count; i++) { ArrayDimension dimension = dimensions[i]; collection2.Add(new ArrayDimension(dimension.LowerBound, dimension.UpperBound)); } } return(type10); } case ElementType.GenericInst: { GenericInstanceType type11 = (GenericInstanceType)type; GenericInstanceType type12 = new GenericInstanceType(this.ImportType(type11.ElementType, context)); Collection <TypeReference> genericArguments = type11.GenericArguments; Collection <TypeReference> collection4 = type12.GenericArguments; for (int i = 0; i < genericArguments.Count; i++) { collection4.Add(this.ImportType(genericArguments[i], context)); } return(type12); } default: switch (etype) { case ElementType.SzArray: { ArrayType type2 = (ArrayType)type; return(new ArrayType(this.ImportType(type2.ElementType, context))); } case ElementType.MVar: { GenericParameter parameter2 = (GenericParameter)type; if (parameter2.DeclaringMethod == null) { throw new InvalidOperationException(); } return(context.MethodParameter(parameter2.DeclaringMethod.Name, parameter2.Position)); } case ElementType.CModReqD: { RequiredModifierType type8 = (RequiredModifierType)type; return(new RequiredModifierType(this.ImportType(type8.ModifierType, context), this.ImportType(type8.ElementType, context))); } case ElementType.CModOpt: { OptionalModifierType type7 = (OptionalModifierType)type; return(new OptionalModifierType(this.ImportType(type7.ModifierType, context), this.ImportType(type7.ElementType, context))); } default: break; } break; } } throw new NotSupportedException(type.etype.ToString()); }
static bool comparePinnedTypes(Type a, PinnedType b) { return compareTypes(a, b.ElementType); }
static bool comparePinnedTypes(PinnedType a, PinnedType b) { return compareTypeSpecifications(a, b); }
static int pinnedTypeHashCode(PinnedType a) { if (a == null) return 0; return typeSpecificationHashCode(a); }
void doPinnedType(PinnedType pinnedType) { if (pinnedTypes.ContainsKey(pinnedType)) return; pinnedTypes[pinnedType] = true; addPinnedType(pinnedType); }
private TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context) { ElementType elementType = type.etype; if (elementType > ElementType.CModOpt) { if (elementType == ElementType.Sentinel) { SentinelType sentinelType = (SentinelType)type; return(new SentinelType(this.ImportType(sentinelType.ElementType, context))); } if (elementType == ElementType.Pinned) { PinnedType pinnedType = (PinnedType)type; return(new PinnedType(this.ImportType(pinnedType.ElementType, context))); } } else { switch (elementType) { case ElementType.Ptr: { PointerType pointerType = (PointerType)type; return(new PointerType(this.ImportType(pointerType.ElementType, context))); } case ElementType.ByRef: { ByReferenceType byReferenceType = (ByReferenceType)type; return(new ByReferenceType(this.ImportType(byReferenceType.ElementType, context))); } case ElementType.ValueType: case ElementType.Class: { break; } case ElementType.Var: { GenericParameter genericParameter = (GenericParameter)type; if (genericParameter.DeclaringType == null) { throw new InvalidOperationException(); } return(context.TypeParameter(genericParameter.DeclaringType.FullName, genericParameter.Position)); } case ElementType.Array: { ArrayType arrayType = (ArrayType)type; ArrayType arrayType1 = new ArrayType(this.ImportType(arrayType.ElementType, context)); if (arrayType.IsVector) { return(arrayType1); } Collection <ArrayDimension> dimensions = arrayType.Dimensions; Collection <ArrayDimension> arrayDimensions = arrayType1.Dimensions; arrayDimensions.Clear(); for (int i = 0; i < dimensions.Count; i++) { ArrayDimension item = dimensions[i]; arrayDimensions.Add(new ArrayDimension(item.LowerBound, item.UpperBound)); } return(arrayType1); } case ElementType.GenericInst: { GenericInstanceType genericInstanceType = (GenericInstanceType)type; GenericInstanceType genericInstanceType1 = new GenericInstanceType(this.ImportType(genericInstanceType.ElementType, context)); Collection <TypeReference> genericArguments = genericInstanceType.GenericArguments; Collection <TypeReference> typeReferences = genericInstanceType1.GenericArguments; for (int j = 0; j < genericArguments.Count; j++) { typeReferences.Add(this.ImportType(genericArguments[j], context)); } return(genericInstanceType1); } default: { switch (elementType) { case ElementType.SzArray: { ArrayType arrayType2 = (ArrayType)type; return(new ArrayType(this.ImportType(arrayType2.ElementType, context))); } case ElementType.MVar: { GenericParameter genericParameter1 = (GenericParameter)type; if (genericParameter1.DeclaringMethod == null) { throw new InvalidOperationException(); } return(context.MethodParameter(context.NormalizeMethodName(genericParameter1.DeclaringMethod), genericParameter1.Position)); } case ElementType.CModReqD: { RequiredModifierType requiredModifierType = (RequiredModifierType)type; return(new RequiredModifierType(this.ImportType(requiredModifierType.ModifierType, context), this.ImportType(requiredModifierType.ElementType, context))); } case ElementType.CModOpt: { OptionalModifierType optionalModifierType = (OptionalModifierType)type; return(new OptionalModifierType(this.ImportType(optionalModifierType.ModifierType, context), this.ImportType(optionalModifierType.ElementType, context))); } } break; } } } throw new NotSupportedException(type.etype.ToString()); }
private TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context) { switch (type.etype) { case ElementType.SzArray: { ArrayType arrayType = (ArrayType)type; return(new ArrayType(ImportType(arrayType.ElementType, context))); } case ElementType.Ptr: { PointerType pointerType = (PointerType)type; return(new PointerType(ImportType(pointerType.ElementType, context))); } case ElementType.ByRef: { ByReferenceType byReferenceType = (ByReferenceType)type; return(new ByReferenceType(ImportType(byReferenceType.ElementType, context))); } case ElementType.Pinned: { PinnedType pinnedType = (PinnedType)type; return(new PinnedType(ImportType(pinnedType.ElementType, context))); } case ElementType.Sentinel: { SentinelType sentinelType = (SentinelType)type; return(new SentinelType(ImportType(sentinelType.ElementType, context))); } case ElementType.FnPtr: { FunctionPointerType functionPointerType = (FunctionPointerType)type; FunctionPointerType functionPointerType2 = new FunctionPointerType { HasThis = functionPointerType.HasThis, ExplicitThis = functionPointerType.ExplicitThis, CallingConvention = functionPointerType.CallingConvention, ReturnType = ImportType(functionPointerType.ReturnType, context) }; if (!functionPointerType.HasParameters) { return(functionPointerType2); } for (int j = 0; j < functionPointerType.Parameters.Count; j++) { functionPointerType2.Parameters.Add(new ParameterDefinition(ImportType(functionPointerType.Parameters[j].ParameterType, context))); } return(functionPointerType2); } case ElementType.CModOpt: { OptionalModifierType optionalModifierType = (OptionalModifierType)type; return(new OptionalModifierType(ImportType(optionalModifierType.ModifierType, context), ImportType(optionalModifierType.ElementType, context))); } case ElementType.CModReqD: { RequiredModifierType requiredModifierType = (RequiredModifierType)type; return(new RequiredModifierType(ImportType(requiredModifierType.ModifierType, context), ImportType(requiredModifierType.ElementType, context))); } case ElementType.Array: { ArrayType arrayType2 = (ArrayType)type; ArrayType arrayType3 = new ArrayType(ImportType(arrayType2.ElementType, context)); if (arrayType2.IsVector) { return(arrayType3); } Collection <ArrayDimension> dimensions = arrayType2.Dimensions; Collection <ArrayDimension> dimensions2 = arrayType3.Dimensions; dimensions2.Clear(); for (int k = 0; k < dimensions.Count; k++) { ArrayDimension arrayDimension = dimensions[k]; dimensions2.Add(new ArrayDimension(arrayDimension.LowerBound, arrayDimension.UpperBound)); } return(arrayType3); } case ElementType.GenericInst: { GenericInstanceType genericInstanceType = (GenericInstanceType)type; GenericInstanceType genericInstanceType2 = new GenericInstanceType(ImportType(genericInstanceType.ElementType, context)); Collection <TypeReference> genericArguments = genericInstanceType.GenericArguments; Collection <TypeReference> genericArguments2 = genericInstanceType2.GenericArguments; for (int i = 0; i < genericArguments.Count; i++) { genericArguments2.Add(ImportType(genericArguments[i], context)); } return(genericInstanceType2); } case ElementType.Var: { GenericParameter genericParameter2 = (GenericParameter)type; if (genericParameter2.DeclaringType == null) { throw new InvalidOperationException(); } return(context.TypeParameter(genericParameter2.DeclaringType.FullName, genericParameter2.Position)); } case ElementType.MVar: { GenericParameter genericParameter = (GenericParameter)type; if (genericParameter.DeclaringMethod == null) { throw new InvalidOperationException(); } return(context.MethodParameter(context.NormalizeMethodName(genericParameter.DeclaringMethod), genericParameter.Position)); } default: throw new NotSupportedException(type.etype.ToString()); } }
protected virtual PinnedType updatePinnedType(PinnedType a) { return new PinnedType(update(a.ElementType)); }
TypeSpecification GetTypeSpec(TypeSpecification original, ImportContext context) { TypeSpecification typeSpec; TypeReference elementType = ImportTypeReference (original.ElementType, context); if (original is PointerType) { typeSpec = new PointerType (elementType); } else if (original is PinnedType) { typeSpec = new PinnedType (elementType); } else if (original is ArrayType) { // deal with complex arrays typeSpec = new ArrayType (elementType); } else if (original is ReferenceType) { typeSpec = new ReferenceType (elementType); } else if (original is GenericInstanceType) { GenericInstanceType git = original as GenericInstanceType; GenericInstanceType genElemType = new GenericInstanceType (elementType); context.GenericContext.CheckProvider (genElemType.GetOriginalType (), git.GenericArguments.Count); foreach (TypeReference arg in git.GenericArguments) genElemType.GenericArguments.Add (ImportTypeReference (arg, context)); typeSpec = genElemType; } else if (original is ModifierOptional) { TypeReference mt = (original as ModifierOptional).ModifierType; typeSpec = new ModifierOptional (elementType, ImportTypeReference (mt, context)); } else if (original is ModifierRequired) { TypeReference mt = (original as ModifierRequired).ModifierType; typeSpec = new ModifierRequired (elementType, ImportTypeReference (mt, context)); } else if (original is SentinelType) { typeSpec = new SentinelType (elementType); } else if (original is FunctionPointerType) { FunctionPointerType ori = original as FunctionPointerType; FunctionPointerType fnptr = new FunctionPointerType ( ori.HasThis, ori.ExplicitThis, ori.CallingConvention, new MethodReturnType (ImportTypeReference (ori.ReturnType.ReturnType, context))); foreach (ParameterDefinition parameter in ori.Parameters) fnptr.Parameters.Add (new ParameterDefinition (ImportTypeReference (parameter.ParameterType, context))); typeSpec = fnptr; } else throw new ReflectionException ("Unknown element type: {0}", original.GetType ().Name); return typeSpec; }