private AnalysisNet.Types.PointerType ExtractType(Cecil.PointerType typeref) { AnalysisNet.Types.IType target = ExtractType(typeref.ElementType); AnalysisNet.Types.PointerType type = new AnalysisNet.Types.PointerType(target); return(type); }
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 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; }
static bool comparePointerTypes(Type a, PointerType b) { if (!a.IsPointer) return false; return compareTypes(a.GetElementType(), b.ElementType); }
static int pointerTypeHashCode(PointerType a) { if (a == null) return 0; return typeSpecificationHashCode(a); }
static bool comparePointerTypes(PointerType a, PointerType b) { return compareTypeSpecifications(a, b); }
TypeReference GetTypeSpec (Type t, ImportContext context) { Stack s = new Stack (); while (t.HasElementType || IsGenericTypeSpec (t)) { s.Push (t); if (t.HasElementType) t = t.GetElementType (); else if (IsGenericTypeSpec (t)) { t = (Type) t.GetType ().GetMethod ("GetGenericTypeDefinition").Invoke (t, null); break; } } TypeReference elementType = ImportSystemType (t, context); while (s.Count > 0) { t = (Type) s.Pop (); if (t.IsPointer) elementType = new PointerType (elementType); else if (t.IsArray) elementType = new ArrayType (elementType, t.GetArrayRank ()); else if (t.IsByRef) elementType = new ReferenceType (elementType); else if (IsGenericTypeSpec (t)) elementType = GetGenericType (t, elementType, context); else throw new ReflectionException ("Unknown element type"); } return elementType; }
void doPointerType(PointerType pointerType) { bool present; if (pointerTypes.TryGetValue(pointerType, out present)) return; pointerTypes[pointerType] = true; addPointerType(pointerType); }
protected override bool VerifyIsConstrainedToNSObject(TypeReference type, out TypeReference constrained_type) { constrained_type = null; var gp = type as GenericParameter; if (gp != null) { if (!gp.HasConstraints) return false; foreach (var c in gp.Constraints) { if (IsNSObject (c)) { constrained_type = c; return true; } } return false; } var git = type as GenericInstanceType; if (git != null) { var rv = true; if (git.HasGenericArguments) { var newGit = new GenericInstanceType (git.ElementType); for (int i = 0; i < git.GenericArguments.Count; i++) { TypeReference constr; rv &= VerifyIsConstrainedToNSObject (git.GenericArguments [i], out constr); newGit.GenericArguments.Add (constr ?? git.GenericArguments [i]); } constrained_type = newGit; } return rv; } var el = type as ArrayType; if (el != null) { var rv = VerifyIsConstrainedToNSObject (el.ElementType, out constrained_type); if (constrained_type == null) return rv; constrained_type = new ArrayType (constrained_type, el.Rank); return rv; } var rt = type as ByReferenceType; if (rt != null) { var rv = VerifyIsConstrainedToNSObject (rt.ElementType, out constrained_type); if (constrained_type == null) return rv; constrained_type = new ByReferenceType (constrained_type); return rv; } var tr = type as PointerType; if (tr != null) { var rv = VerifyIsConstrainedToNSObject (tr.ElementType, out constrained_type); if (constrained_type == null) return rv; constrained_type = new PointerType (constrained_type); return rv; } return true; }
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()); }
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 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; }
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()); } }
private StaticPointerTypeWrapper MakePointerType(PointerType pointerTypeHandle) { return MakeType(pointerTypeHandle.ElementType).MakePointerType(); }
static void AppendTypeName(StringBuilder b, TypeReference type) { if (type == null) { // could happen when a TypeSpecification has no ElementType; e.g. function pointers in C++/CLI assemblies return; } if (type is GenericInstanceType) { GenericInstanceType giType = (GenericInstanceType)type; AppendTypeNameWithArguments(b, giType.ElementType, giType.GenericArguments); } else if (type is TypeSpecification) { AppendTypeName(b, ((TypeSpecification)type).ElementType); ArrayType arrayType = type as ArrayType; if (arrayType != null) { b.Append('['); for (int i = 0; i < arrayType.Dimensions.Count; i++) { if (i > 0) { b.Append(','); } ArrayDimension ad = arrayType.Dimensions[i]; if (ad.IsSized) { b.Append(ad.LowerBound); b.Append(':'); b.Append(ad.UpperBound); } } b.Append(']'); } ByReferenceType refType = type as ByReferenceType; if (refType != null) { b.Append('@'); } PointerType ptrType = type as PointerType; if (ptrType != null) { b.Append('*'); } } else { GenericParameter gp = type as GenericParameter; if (gp != null) { b.Append('`'); if (gp.Owner.GenericParameterType == GenericParameterType.Method) { b.Append('`'); } b.Append(gp.Position); } else if (type.DeclaringType != null) { AppendTypeName(b, type.DeclaringType); b.Append('.'); b.Append(type.Name); } else { b.Append(type.FullName); } } }
protected virtual PointerType updatePointerType(PointerType a) { return new PointerType(update(a.ElementType)); }
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; }
/* * // Check if 'type' has some decorations applied to it * if (type is Mono.Cecil.TypeSpecification) { * // Go through all levels of 'indirection', 'array dimensions' * // and 'generic types' - in the end, we should get the actual * // type of the ReturnType (but all data about its array * // dimensions, levels of indirection and even its generic * // parameters is correctly stored within ArrayCount and * // ArrayDimensions, PointerNestingLevel and GenericArguments * // respectively). * if (type is ArrayType) { * // This return type is obviously an array - add the rank * ArrayType at = (ArrayType) type; * if (arrays == null) * arrays = new Stack<int>(); * arrays.Push(at.Rank); * type = at.ElementType; * } else else if (type is Mono.Cecil.ReferenceType) { * Mono.Cecil.ReferenceType rt = (Mono.Cecil.ReferenceType) type; * byRef = true; * type = rt.ElementType; * } else if (type is PointerType) { * // The type is a pointer * PointerType pt = (PointerType) type; ++pointerNestingLevel; * type = pt.ElementType; * // Go down one level * } else { * // TODO: Check if we loose some relevant info here * type = ((TypeSpecification)type).ElementType; * }*/ public static DomReturnType GetReturnType(TypeReference typeReference) { if (typeReference == null) { return(new DomReturnType(DomReturnType.Void.ToInvariantString())); } if (typeReference is Mono.Cecil.GenericInstanceType) { Mono.Cecil.GenericInstanceType genType = (Mono.Cecil.GenericInstanceType)typeReference; DomReturnType result = GetReturnType(genType.ElementType); foreach (TypeReference typeRef in genType.GenericArguments) { DomReturnType param = GetReturnType(typeRef); foreach (IReturnTypePart part in result.Parts) { if (part.Tag is TypeDefinition) { TypeDefinition typeDef = (TypeDefinition)part.Tag; foreach (TypeReference typeParam in typeDef.GenericParameters) { if (typeParam.Name == param.Name) { part.AddTypeParameter(param); goto skip; } } } } result.AddTypeParameter(param); skip :; } return(result); } if (typeReference is Mono.Cecil.ArrayType) { Mono.Cecil.ArrayType arrType = (Mono.Cecil.ArrayType)typeReference; DomReturnType result = GetReturnType(arrType.ElementType); result.ArrayDimensions++; result.SetDimension(result.ArrayDimensions - 1, arrType.Rank - 1); return(result); } if (typeReference is Mono.Cecil.PointerType) { Mono.Cecil.PointerType ptrType = (Mono.Cecil.PointerType)typeReference; DomReturnType result = GetReturnType(ptrType.ElementType); if (result.ArrayDimensions > 0) { result.ArrayPointerNestingLevel++; } else { result.PointerNestingLevel++; } return(result); } if (typeReference is Mono.Cecil.ByReferenceType) { return(GetReturnType(((Mono.Cecil.ByReferenceType)typeReference).ElementType)); } if (typeReference is Mono.Cecil.TypeDefinition) { Mono.Cecil.TypeDefinition typeDefinition = (Mono.Cecil.TypeDefinition)typeReference; DomReturnType result; if (typeDefinition.DeclaringType != null) { result = GetReturnType(typeDefinition.DeclaringType); result.Parts.Add(new ReturnTypePart(typeDefinition.Name)); result.Tag = typeDefinition; } else { result = new DomReturnType(typeDefinition.Name); result.Namespace = typeDefinition.Namespace; result.Tag = typeDefinition; } return(result); } return(new DomReturnType(DomCecilType.RemoveGenericParamSuffix(typeReference.FullName))); }
TypeReference GetTypeSpec(TypeReference t, ImportContext context) { Stack s = new Stack (); while (t is TypeSpecification) { s.Push (t); t = (t as TypeSpecification).ElementType; } TypeReference elementType = ImportTypeReference (t, context); while (s.Count > 0) { t = s.Pop () as TypeReference; if (t is PointerType) elementType = new PointerType (elementType); else if (t is ArrayType) // deal with complex arrays elementType = new ArrayType (elementType); else if (t is ReferenceType) elementType = new ReferenceType (elementType); else if (t is GenericInstanceType) { GenericInstanceType git = t as GenericInstanceType; GenericInstanceType genElemType = new GenericInstanceType (elementType); foreach (TypeReference arg in git.GenericArguments) genElemType.GenericArguments.Add (ImportTypeReference (arg, context)); elementType = genElemType; } else throw new ReflectionException ("Unknown element type: {0}", t.GetType ().Name); } return elementType; }
void addPointerType(PointerType pt) { if (pt == null) return; addTypeSpecification(pt); }
void doPointerType(PointerType pointerType) { if (pointerTypes.ContainsKey(pointerType)) return; pointerTypes[pointerType] = true; addPointerType(pointerType); }
public static PointerType ChangePointerType(this PointerType type, TypeReference elementType) { if (elementType != type.ElementType) { var result = new PointerType(elementType); if (type.HasGenericParameters) SetGenericParameters(result, type.GenericParameters); return result; } return type; }
public override void EmitConvertManagedToNative(MarshalCodeContext context) { var corlib = context.Assembly.MainModule.Import(typeof(void)).Resolve().Module.Assembly; // Note: we consider LPTStr (platform dependent) to be unicode (not valid on Win98/WinME, but well...) if (context.IsCleanupInlined && (nativeType == NativeType.LPTStr || nativeType == NativeType.LPWStr)) { // fixed (char* c = str) var charPtr = new PointerType(context.Assembly.MainModule.Import(typeof(char))); var @string = context.Assembly.MainModule.Import(typeof(string)); var runtimeHelpers = corlib.MainModule.GetType(typeof(RuntimeHelpers).FullName); var pinnedStringVariable = new VariableDefinition(new PinnedType(@string)); var charPtrVariable = new VariableDefinition(charPtr); context.Method.Body.Variables.Add(charPtrVariable); context.Method.Body.Variables.Add(pinnedStringVariable); // Pin string context.ManagedEmitters.Peek().Emit(context.ILProcessor); context.ILProcessor.Emit(OpCodes.Stloc, pinnedStringVariable); context.ILProcessor.Emit(OpCodes.Ldloc, pinnedStringVariable); // Load character start var storeCharPtrInst = Instruction.Create(OpCodes.Stloc, charPtrVariable); context.ILProcessor.Emit(OpCodes.Conv_I); context.ILProcessor.Emit(OpCodes.Dup); context.ILProcessor.Emit(OpCodes.Brfalse, storeCharPtrInst); context.ILProcessor.Emit(OpCodes.Call, context.Assembly.MainModule.Import(runtimeHelpers.Methods.First(x => x.Name == "get_OffsetToStringData"))); context.ILProcessor.Emit(OpCodes.Add); // Optional: Store it in a variable (for easier debugging?) context.ILProcessor.Append(storeCharPtrInst); context.ILProcessor.Emit(OpCodes.Ldloc, charPtrVariable); } else { string stringToHGlobalName; switch (nativeType) { case NativeType.LPTStr: // Let's ignore Win98/WinME case NativeType.LPWStr: stringToHGlobalName = "StringToHGlobalUni"; break; case NativeType.LPStr: stringToHGlobalName = "StringToHGlobalAnsi"; break; case NativeType.BStr: case NativeType.TBStr: case NativeType.ANSIBStr: throw new NotImplementedException("BSTR is not supported in String Marshaller"); default: throw new ArgumentOutOfRangeException("nativeType"); } // Call StringToHGlobalUni(str) var marshal = corlib.MainModule.GetType(typeof(Marshal).FullName); var stringToHGlobal = context.Assembly.MainModule.Import(marshal.Methods.First(x => x.Name == stringToHGlobalName)); context.ManagedEmitters.Peek().Emit(context.ILProcessor); context.ILProcessor.Emit(OpCodes.Call, stringToHGlobal); } }
TypeReference HandleConversion(int targetBitSize, bool targetSigned, ILExpression arg, TypeReference expectedType, TypeReference targetType) { if (targetBitSize >= NativeInt && expectedType is PointerType) { InferTypeForExpression(arg, expectedType); return expectedType; } TypeReference argType = InferTypeForExpression(arg, null); if (targetBitSize >= NativeInt && argType is ByReferenceType) { // conv instructions on managed references mean that the GC should stop tracking them, so they become pointers: PointerType ptrType = new PointerType(((ByReferenceType)argType).ElementType); InferTypeForExpression(arg, ptrType); return ptrType; } else if (targetBitSize >= NativeInt && argType is PointerType) { return argType; } TypeReference resultType = (GetInformationAmount(expectedType) == targetBitSize && IsSigned(expectedType) == targetSigned) ? expectedType : targetType; arg.ExpectedType = resultType; // store the expected type in the argument so that AstMethodBodyBuilder will insert a cast return resultType; }
private TypeReference ParseTypeReference() { TypeReference tr = ClassEditHelper.ParseTypeReference(cboType.SelectedItem, _method.Module); if (tr == null) tr = ClassEditHelper.ParseTypeReference(cboType.Text.Trim(), _method.Module); if (tr != null) { switch (cboSpecification.SelectedItem as string) { case "Array": tr = new ArrayType(tr); break; case "Reference": tr = new ByReferenceType(tr); break; case "Pointer": tr = new PointerType(tr); break; default: break; } } return tr; }